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