001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.acacia.shared;
029
030import java.io.Serializable;
031import java.util.ArrayList;
032import java.util.Collections;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Map;
036
037/**
038 * CmsEntity attribute type data.<p>
039 */
040public class CmsType implements Serializable {
041
042    /** The choice attribute name. */
043    public static final String CHOICE_ATTRIBUTE_NAME = "ATTRIBUTE_CHOICE";
044
045    /** The serial version id. */
046    private static final long serialVersionUID = -7965094404314721990L;
047
048    /** Flag indicating if this is a choice type. */
049    private int m_choiceMaxOccurrence;
050
051    /** The type id. */
052    private String m_id;
053
054    /** The max occurrences of the type attributes. */
055    private Map<String, Integer> m_maxs;
056
057    /** The min occurrences of the type attributes. */
058    private Map<String, Integer> m_mins;
059
060    /** The attribute names. */
061    private List<String> m_names;
062
063    /** The attribute types. */
064    private Map<String, CmsType> m_types;
065
066    /**
067     * Constructor.<p>
068     *
069     * @param id the type id/name
070     */
071    public CmsType(String id) {
072
073        this();
074        m_id = id;
075    }
076
077    /**
078     * Constructor. For serialization only.<p>
079     */
080    protected CmsType() {
081
082        m_names = new ArrayList<String>();
083        m_types = new HashMap<String, CmsType>();
084        m_maxs = new HashMap<String, Integer>();
085        m_mins = new HashMap<String, Integer>();
086    }
087
088    /**
089     * Adds an attribute to the type.<p>
090     *
091     * @param attributeName the attribute name
092     * @param attributeType the attribute type
093     * @param minOccurrence the minimum occurrence of this attribute
094     * @param maxOccurrence the axnimum occurrence of this attribute
095     */
096    public void addAttribute(String attributeName, CmsType attributeType, int minOccurrence, int maxOccurrence) {
097
098        m_names.add(attributeName);
099        m_types.put(attributeName, attributeType);
100        m_mins.put(attributeName, Integer.valueOf(minOccurrence));
101        m_maxs.put(attributeName, Integer.valueOf(maxOccurrence));
102    }
103
104    /**
105     * Returns the maximum occurrence of the given attribute.<p>
106     *
107     * @param attributeName the attribute name
108     *
109     * @return the maximum occurrence
110     */
111    public int getAttributeMaxOccurrence(String attributeName) {
112
113        return m_maxs.get(attributeName).intValue();
114    }
115
116    /**
117     * Returns the minimum occurrence of the given attribute.<p>
118     *
119     * @param attributeName the attribute name
120     *
121     * @return the minimum occurrence
122     */
123    public int getAttributeMinOccurrence(String attributeName) {
124
125        return m_mins.get(attributeName).intValue();
126    }
127
128    /**
129     * The names of the attributes of this type.<p>
130     *
131     * @return the attribute names
132     */
133    public List<String> getAttributeNames() {
134
135        return Collections.unmodifiableList(m_names);
136    }
137
138    /**
139     * Returns the type of the given attribute.<p>
140     *
141     * @param attributeName the attribute name
142     *
143     * @return the type of the given attribute
144     */
145    public CmsType getAttributeType(String attributeName) {
146
147        return m_types.get(attributeName);
148    }
149
150    /**
151     * Returns the type name of the given attribute.<p>
152     *
153     * @param attributeName the attribute name
154     *
155     * @return the type name of the given attribute
156     */
157    public String getAttributeTypeName(String attributeName) {
158
159        CmsType attrType = m_types.get(attributeName);
160        return attrType == null ? null : attrType.getId();
161    }
162
163    /**
164     * Returns the maximum choice occurrence.<p>
165     *
166     * @return the maximum choice occurrence
167     */
168    public int getChoiceMaxOccurrence() {
169
170        return m_choiceMaxOccurrence;
171    }
172
173    /**
174     * Returns the name of the type.<p>
175     *
176     * @return the name of the type
177     */
178    public String getId() {
179
180        return m_id;
181    }
182
183    /**
184     * Returns if this is a choice type.<p>
185     *
186     * @return <code>true</code> if this is a choice type
187     */
188    public boolean isChoice() {
189
190        return m_choiceMaxOccurrence > 0;
191    }
192
193    /**
194     * Returns if this is a simple type. Simple types have no attributes.<p>
195     *
196     * @return <code>true</code> if this is a simple type
197     */
198    public boolean isSimpleType() {
199
200        return m_names.isEmpty();
201    }
202
203    /**
204     * Sets the maximum choice occurrence.<p>
205     *
206     * @param choiceMaxOccurrence the maximum choice occurrence
207     */
208    public void setChoiceMaxOccurrence(int choiceMaxOccurrence) {
209
210        m_choiceMaxOccurrence = choiceMaxOccurrence;
211    }
212}