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.Collection;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Map.Entry;
023import java.util.stream.Collectors;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.cms.tag.AbstractTagProvider;
031import org.ametys.cms.tag.CMSTag;
032import org.ametys.cms.tag.TagTargetType;
033import org.ametys.cms.tag.TagTargetTypeExtensionPoint;
034
035/**
036 * Class representing categories as CMS tag provider.
037 * This directly use {@link CategoryJCRProvider} and transform the categories in {@link CMSTag}
038 */
039public class CategoryTagProvider extends AbstractTagProvider<CMSTag> implements Serviceable
040{
041    /** Target types */
042    protected TagTargetTypeExtensionPoint _targetTypeEP;
043    
044    /** Category tag provider */
045    protected CategoryProviderExtensionPoint _categoryProviderEP;
046    
047    /** The JCR category provider */
048    protected CategoryJCRProvider _jcrCategoryProvider;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        _targetTypeEP = (TagTargetTypeExtensionPoint) smanager.lookup(TagTargetTypeExtensionPoint.ROLE);
054        _categoryProviderEP = (CategoryProviderExtensionPoint) smanager.lookup(CategoryProviderExtensionPoint.ROLE);
055        _jcrCategoryProvider = (CategoryJCRProvider) _categoryProviderEP.getExtension(CategoryJCRProvider.class.getName());
056    }
057    
058    public Map<String, CMSTag> getTags(Map<String, Object> contextualParameters)
059    {
060        Map<String, CMSTag> cmsTags = new HashMap<>();
061        
062        Map<String, Category> categories = _jcrCategoryProvider.getTags(contextualParameters);
063        
064        for (Entry<String, Category> category : categories.entrySet())
065        {
066            CategoryCMSTag categoryTag = _transformToCMSTag(category.getValue(), contextualParameters);
067            cmsTags.put(categoryTag.getId(), _transformToCMSTag(category.getValue(), contextualParameters));
068        }
069        
070        return cmsTags;
071    }
072    
073    public CMSTag getTag(String tagName, Map<String, Object> contextualParameters)
074    {
075        String categoryName = tagName;
076        if (categoryName.startsWith(CategoryCMSTag.TAG_PREFIX))
077        {
078            categoryName = StringUtils.substringAfter(tagName, CategoryCMSTag.TAG_PREFIX);
079        }
080        return _transformToCMSTag(_jcrCategoryProvider.getTag(categoryName, contextualParameters), contextualParameters);
081    }
082    
083    public Collection<CMSTag> getTags(String tagName, Map<String, Object> contextualParameters)
084    {
085        String categoryName = tagName;
086        if (categoryName.startsWith(CategoryCMSTag.TAG_PREFIX))
087        {
088            categoryName = StringUtils.substringAfter(tagName, CategoryCMSTag.TAG_PREFIX);
089        }
090        
091        Collection<Category> categories = _jcrCategoryProvider.getTags(categoryName, contextualParameters);
092        
093        return categories.stream()
094                .map(tag -> _transformToCMSTag(tag, contextualParameters))
095                .collect(Collectors.toList());
096    }
097    
098    public boolean hasTag(String tagName, Map<String, Object> contextualParameters)
099    {
100        String categoryName = tagName;
101        if (categoryName.startsWith(CategoryCMSTag.TAG_PREFIX))
102        {
103            categoryName = StringUtils.substringAfter(tagName, CategoryCMSTag.TAG_PREFIX);
104        }
105        return _jcrCategoryProvider.hasTag(categoryName, contextualParameters);
106    }
107    
108    /**
109     * Transform a {@link Category} in a {@link CategoryCMSTag}, forcing visibility to public and target to CONTENT
110     * @param category the category to transform
111     * @param contextualParameters the contextual parameters
112     * @return a {@link CategoryCMSTag} with the same values
113     */
114    protected CategoryCMSTag _transformToCMSTag(Category category, Map<String, Object> contextualParameters)
115    {
116        if (category != null)
117        {
118            CMSTag parentTag = null;
119            Category parentCategory = category.getParent();
120            if (parentCategory != null)
121            {
122                parentTag = this.getTag(parentCategory.getName(), contextualParameters);
123            }
124            
125            String typeName = "CONTENT";
126            TagTargetType targetType = _targetTypeEP.getTagTargetType(typeName);
127            
128            return new CategoryCMSTag(category, parentTag, targetType);
129        }
130        else
131        {
132            return null;
133        }
134    }
135    
136    @Override
137    public List<String> getCSSUrls(Map<String, Object> contextualParameters)
138    {
139        List<String> cssUrls = super.getCSSUrls(contextualParameters);
140        cssUrls.add("/plugins/workspaces/project-categories.min.css");
141        
142        return cssUrls;
143    }
144}