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 CMS Tag
025 */
026public class CMSTag implements Tag
027{
028    private String _id;
029    private String _name;
030    private I18nizableText _title;
031    private I18nizableText _description;
032    private TagVisibility _visibility;
033    private TagTargetType _targetType;
034    private Map<String, CMSTag> _tags;
035    private CMSTag _parent;
036
037    /** Tag visibility */
038    public enum TagVisibility
039    {
040        /** Public tag. */
041        PUBLIC,
042        /** Private tag. */
043        PRIVATE;
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     * @param target the type of target
054     */
055    public CMSTag(String id, String name, CMSTag parent, I18nizableText title, I18nizableText description, TagTargetType target)
056    {
057        this(id, name, parent, title, description, TagVisibility.PUBLIC, target);
058    }
059    
060    /**
061     * Constructor
062     * @param id The id of the tag. The id must be unique.
063     * @param name The name of the tag. The name must be unique. The name is the same as the id except for JCR tag.
064     * @param parent The parent tag (can be null if the tag has no parent)
065     * @param title the tag title
066     * @param description the tag description
067     * @param visibility the tag visibility.
068     * @param target The type of target
069     */
070    public CMSTag(String id, String name, CMSTag parent, I18nizableText title, I18nizableText description, TagVisibility visibility, TagTargetType target)
071    {
072        _id = id;
073        _name = name;
074        _parent = parent;
075        _title = title;
076        _description = description;
077        _visibility = visibility;
078        _targetType = target;
079    }
080    
081    @Override
082    public String getId ()
083    {
084        return _id;
085    }
086    
087    @Override
088    public String getName ()
089    {
090        return _name;
091    }
092    
093    @Override
094    public I18nizableText getTitle()
095    {
096        return _title;
097    }
098    
099    @Override
100    public I18nizableText getDescription()
101    {
102        return _description;
103    }
104    
105    @Override
106    public String getParentName()
107    {
108        return _parent != null ? _parent.getName() : null;
109    }
110    
111    @SuppressWarnings("unchecked")
112    @Override
113    public CMSTag getParent()
114    {
115        return _parent;
116    }
117    
118    @Override
119    public void addTag(Tag tag)
120    {
121        if (_tags == null)
122        {
123            _tags = new HashMap<>();
124        }
125        
126        _tags.put(tag.getId(), (CMSTag) tag);
127    }
128    
129    @Override
130    public Map<String, CMSTag> getTags()
131    {
132        if (_tags == null)
133        {
134            _tags = new HashMap<>();
135        }
136        
137        return _tags;
138    }
139    
140    @SuppressWarnings("unchecked")
141    @Override
142    public CMSTag getTag(String tagId)
143    {
144        if (_tags == null)
145        {
146            _tags = new HashMap<>();
147        }
148        
149        return _tags.get(tagId);
150    }
151    
152    @Override
153    public boolean hasTag(String tagId)
154    {
155        if (_tags == null)
156        {
157            _tags = new HashMap<>();
158        }
159        
160        return _tags.containsKey(tagId);
161    }
162    
163    /**
164     * Retrieves the visibility type.
165     * @return the visibility type.
166     */
167    public TagVisibility getVisibility()
168    {
169        return _visibility;
170    }
171    
172    /**
173     * Set the visibility type.
174     * @param visibility the visibility to set.
175     */
176    public void setVisibility(TagVisibility visibility)
177    {
178        _visibility = visibility;
179    }
180    
181    /**
182     * Retrieves the target type.
183     * @return the target type.
184     */
185    public TagTargetType getTarget()
186    {
187        return _targetType;
188    }
189    
190    /**
191     * Set the target type.
192     * @param target the target to set.
193     */
194    public void setTarget(TagTargetType target)
195    {
196        _targetType = target;
197    }
198
199    @SuppressWarnings("unchecked")
200    @Override
201    public void setTags(Map<String, ? extends Tag> tags)
202    {
203        _tags = (Map<String, CMSTag>) tags;
204    }
205}