001/*
002 *  Copyright 2018 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.plugins.workspaces.categories;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.ametys.cms.color.AbstractColorsComponent;
022import org.ametys.cms.tag.ColorableTag;
023import org.ametys.cms.tag.Tag;
024import org.ametys.runtime.i18n.I18nizableText;
025
026/**
027 * Class representing a category. <br>
028 */
029public class Category implements ColorableTag
030{
031    private String _id;
032    private String _name;
033    private I18nizableText _title;
034    private I18nizableText _description;
035    private String _color;
036    private AbstractColorsComponent _colorsComponent;
037    private Map<String, Category> _tags;
038    private Category _parent;
039
040    /**
041     * Constructor
042     * @param id The id of the tag. The id must be unique.
043     */
044    public Category(String id)
045    {
046        _id = id;
047    }
048    
049    /**
050     * Constructor
051     * @param id The id of the tag. The id must be unique.
052     * @param name The name of the tag. The name must be unique. The name is the same as the id except for JCR tag.
053     * @param parent The parent tag (can be null if the tag has no parent)
054     * @param title the tag title
055     * @param description the tag description
056     */
057    public Category(String id, String name, Category parent, I18nizableText title, I18nizableText description)
058    {
059        _id = id;
060        _name = name;
061        _parent = parent;
062        _title = title;
063        _description = description;
064    }
065    
066    /**
067     * Constructor
068     * @param id The id of the tag. The id must be unique.
069     * @param name The name of the tag. The name must be unique. The name is the same as the id except for JCR tag.
070     * @param color the color
071     * @param colorsComponent the colors component
072     * @param parent The parent tag (can be null if the tag has no parent)
073     * @param title the tag title
074     * @param description the tag description
075     */
076    public Category(String id, String name, String color, AbstractColorsComponent colorsComponent, Category parent, I18nizableText title, I18nizableText description)
077    {
078        this(id, name, parent, title, description);
079        _color = color;
080        _colorsComponent = colorsComponent;
081    }
082    
083    @Override
084    public String getId ()
085    {
086        return _id;
087    }
088    
089    @Override
090    public String getName ()
091    {
092        return _name;
093    }
094    
095    @Override
096    public I18nizableText getTitle()
097    {
098        return _title;
099    }
100    
101    @Override
102    public I18nizableText getDescription()
103    {
104        return _description;
105    }
106    
107    @Override
108    public String getColor(boolean withDefaultValue)
109    {
110        return _color;
111    }
112    
113    @Override
114    public AbstractColorsComponent getColorComponent()
115    {
116        return _colorsComponent;
117    }
118    
119    @Override
120    public String getParentName()
121    {
122        return _parent != null ? _parent.getName() : null;
123    }
124    
125    @SuppressWarnings("unchecked")
126    @Override
127    public Category getParent()
128    {
129        return _parent;
130    }
131    
132    @Override
133    public void addTag(Tag tag)
134    {
135        if (_tags == null)
136        {
137            _tags = new HashMap<>();
138        }
139        
140        _tags.put(tag.getId(), (Category) tag);
141    }
142    
143    @Override
144    public Map<String, Category> getTags()
145    {
146        if (_tags == null)
147        {
148            _tags = new HashMap<>();
149        }
150        
151        return _tags;
152    }
153    
154    @SuppressWarnings("unchecked")
155    @Override
156    public Category getTag(String tagId)
157    {
158        if (_tags == null)
159        {
160            _tags = new HashMap<>();
161        }
162        
163        return _tags.get(tagId);
164    }
165    
166    @Override
167    public boolean hasTag(String tagId)
168    {
169        if (_tags == null)
170        {
171            _tags = new HashMap<>();
172        }
173        
174        return _tags.containsKey(tagId);
175    }
176    
177    @SuppressWarnings("unchecked")
178    @Override
179    public void setTags(Map<String, ? extends Tag> tags)
180    {
181        _tags = (Map<String, Category>) tags;
182    }
183}