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