001/*
002 *  Copyright 2020 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.Map;
019import java.util.stream.Collectors;
020
021import org.ametys.cms.tag.CMSTag;
022import org.ametys.cms.tag.ColorableCMSTag;
023import org.ametys.cms.tag.TagTargetType;
024
025/**
026 * This class represent a project category as a CMS Tag
027 */
028public class CategoryCMSTag extends ColorableCMSTag
029{
030    /** The prefix for all tags issue from categories */
031    public static final String TAG_PREFIX = "WORKSPACES_CATEGORY_";
032    
033    private Category _category;
034    
035    /**
036     * Constructor
037     * @param category the category
038     * @param parent the parent CMS tag
039     * @param targetType the target type
040     */
041    public CategoryCMSTag(Category category, CMSTag parent, TagTargetType targetType)
042    {
043        super(new CMSTag(TAG_PREFIX + category.getName(), TAG_PREFIX + category.getName(), parent, category.getTitle(), category.getDescription(), TagVisibility.PUBLIC, targetType), category.getColor(false), category.getColorComponent());
044        _category = category;
045    }
046    
047    @SuppressWarnings("unchecked")
048    @Override
049    public ColorableCMSTag getTag(String tagId)
050    {
051        Category category = _category.getTag(tagId);
052        return new CategoryCMSTag(category, this, this.getTarget());
053    }
054    
055    
056    @Override
057    public Map<String, CMSTag> getTags()
058    {
059        return _category.getTags().values()
060            .stream()
061            .map(category -> new CategoryCMSTag(category, this, this.getTarget()))
062            .collect(Collectors.toMap(tag -> tag.getId(), tag -> tag));
063    }
064}