001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.odf.enumeration;
017
018import java.util.Locale;
019import java.util.Objects;
020
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.cms.repository.Content;
024
025/**
026 * This class represents a entry of ODF reference table
027 *
028 */
029public class OdfReferenceTableEntry
030{
031    /** Attribute name for code */
032    public static final String CODE = "code";
033    /** Attribute name for CDM value */
034    public static final String CDM_VALUE = "cdmValue";
035    /** Attribute name for ordering */
036    public static final String ORDER = "order";
037    
038    private Content _entry;
039    
040    /**
041     * Constructor
042     * @param content The content representing the entry
043     */
044    public OdfReferenceTableEntry(Content content)
045    {
046        _entry = content;
047    }
048    
049    /**
050     * Get the identifier
051     * @return The identifier
052     */
053    public String getId()
054    {
055        return _entry.getId();
056    }
057    
058    /**
059     * Get the label
060     * @param lang The language
061     * @return The label
062     */
063    public String getLabel(String lang)
064    {
065        return _entry.getTitle(new Locale(lang));
066    }
067    
068    /**
069     * Get the code
070     * @return the code
071     */
072    public String getCode()
073    {
074        return _entry.getValue(CODE, false, StringUtils.EMPTY);
075    }
076    
077    /** 
078     * Get the CDM value
079     * @return the CDM value
080     */
081    public String getCdmValue()
082    {
083        return _entry.getValue(CDM_VALUE, false, StringUtils.EMPTY);
084    }
085    
086    /**
087     * Get the order
088     * @return the order
089     */
090    public Long getOrder()
091    {
092        return _entry.getValue(ORDER, false, Long.MAX_VALUE);
093    }
094    
095    /**
096     * Get the content
097     * @return the content
098     */
099    public Content getContent()
100    {
101        return _entry;
102    }
103
104    @Override
105    public int hashCode()
106    {
107        return Objects.hash(_entry.getId());
108    }
109
110    @Override
111    public boolean equals(Object obj)
112    {
113        if (this == obj)
114        {
115            return true;
116        }
117        if (obj == null)
118        {
119            return false;
120        }
121        if (getClass() != obj.getClass())
122        {
123            return false;
124        }
125        OdfReferenceTableEntry other = (OdfReferenceTableEntry) obj;
126        return Objects.equals(_entry.getId(), other._entry.getId());
127    }
128}