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    
036    /** Attribute name for ordering */
037    public static final String ORDER = "order";
038    /** Attribute name for archiving */
039    public static final String ARCHIVED = "archived";
040    
041    private Content _entry;
042    
043    /**
044     * Constructor
045     * @param content The content representing the entry
046     */
047    public OdfReferenceTableEntry(Content content)
048    {
049        _entry = content;
050    }
051    
052    /**
053     * Get the identifier
054     * @return The identifier
055     */
056    public String getId()
057    {
058        return _entry.getId();
059    }
060    
061    /**
062     * Get the label
063     * @param lang The language
064     * @return The label
065     */
066    public String getLabel(String lang)
067    {
068        return _entry.getTitle(new Locale(lang));
069    }
070    
071    /**
072     * Get the code
073     * @return the code
074     */
075    public String getCode()
076    {
077        return _entry.getValue(CODE, false, StringUtils.EMPTY);
078    }
079    
080    /** 
081     * Get the CDM value
082     * @return the CDM value
083     */
084    public String getCdmValue()
085    {
086        return _entry.getValue(CDM_VALUE, false, StringUtils.EMPTY);
087    }
088    
089    /**
090     * Get the order
091     * @return the order
092     */
093    public Long getOrder()
094    {
095        return _entry.getValue(ORDER, false, Long.MAX_VALUE);
096    }
097    
098    /**
099     * Get the archiving state
100     * @return <code>true</code> if the entry is archived, <code>false</code> otherwise
101     */
102    public Boolean isArchived()
103    {
104        return _entry.getValue(ARCHIVED, false, false);
105    }
106    
107    /**
108     * Get the content
109     * @return the content
110     */
111    public Content getContent()
112    {
113        return _entry;
114    }
115
116    @Override
117    public int hashCode()
118    {
119        return Objects.hash(_entry.getId());
120    }
121
122    @Override
123    public boolean equals(Object obj)
124    {
125        if (this == obj)
126        {
127            return true;
128        }
129        if (obj == null)
130        {
131            return false;
132        }
133        if (getClass() != obj.getClass())
134        {
135            return false;
136        }
137        OdfReferenceTableEntry other = (OdfReferenceTableEntry) obj;
138        return Objects.equals(_entry.getId(), other._entry.getId());
139    }
140}