001/*
002 *  Copyright 2019 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.keywords;
017
018import java.text.Normalizer;
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import javax.jcr.RepositoryException;
027
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030
031import org.ametys.cms.tag.AbstractTagsDAO;
032import org.ametys.cms.tag.Tag;
033import org.ametys.cms.tag.TagProvider;
034import org.ametys.cms.tag.jcr.JCRTag;
035import org.ametys.core.right.RightManager;
036import org.ametys.core.right.RightManager.RightResult;
037import org.ametys.core.ui.Callable;
038import org.ametys.plugins.workspaces.project.ProjectConstants;
039
040/**
041 * DAO for manipulating project's tags
042 */
043public class KeywordsDAO extends AbstractTagsDAO
044{
045    /** The avalon role */
046    public static final String ROLE = KeywordsDAO.class.getName();
047
048    private RightManager _rightManager;
049    private KeywordJCRDAO _keywordJCRDAO;
050
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        super.service(manager);
055        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
056        _keywordJCRDAO = (KeywordJCRDAO) manager.lookup(KeywordJCRDAO.ROLE);
057    }
058    
059    @Override
060    public String getTagProviderEPRole()
061    {
062        return KeywordProviderExtensionPoint.ROLE;
063    }
064
065    @Override
066    protected List<TagProvider< ? extends Tag>> getCustomTagProvider()
067    {
068        List<TagProvider<? extends Tag>> providers = new ArrayList<>();
069        providers.add(_tagProviderExtPt.getExtension(KeywordJCRProvider.class.getName()));
070        
071        return providers;
072    }
073    
074    /**
075     * Add keywords from FO
076     * @param keywordNames The name of keywords to add
077     * @return The new keywords descriptions
078     */
079    public List<Map<String, Object>> addKeywords(String[] keywordNames)
080    {
081        try
082        {
083            List<Map<String, Object>> tags = new ArrayList<>();
084    
085            for (String tagName : keywordNames)
086            {
087                String filteredName = Normalizer.normalize(tagName.toLowerCase(), Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", "").trim(); 
088                filteredName = filteredName.replaceAll("œ", "oe").replaceAll("æ", "ae").replaceAll("[^a-z0-9]", "_").replaceAll("_+", "_");
089    
090                JCRTag tag = _keywordJCRDAO.addTag(null, filteredName.toUpperCase(), tagName, "", Collections.emptyMap(), Collections.emptyMap());
091                tags.add(_keyword2json(tag));
092            }
093            
094            return tags;
095        }
096        catch (RepositoryException e)
097        {
098            throw new IllegalStateException("Cannot create tags", e);
099        }
100    }
101
102    /**
103     * Get existing tags
104     * @return the tags (id and label)
105     */
106    @Callable
107    public Map<String, Object> getKeywords()
108    {
109        List<Map<String, Object>> tags = new ArrayList<>();
110        
111        for (String id : _tagProviderExtPt.getExtensionsIds())
112        {
113            TagProvider<? extends Tag> tagProvider = _tagProviderExtPt.getExtension(id);
114            Map<String, ? extends Tag> someTags = tagProvider.getTags(Collections.emptyMap());
115
116            tags.addAll(_getKeywords(someTags.values()));
117        }
118        
119        Map<String, Object> result = new HashMap<>();
120        result.put("keywords", tags);
121        result.put("canCreate", _rightManager.currentUserHasRight(ProjectConstants.RIGHT_PROJECT_HANDLE_PROJECTKEYWORDS, "/cms") == RightResult.RIGHT_ALLOW);
122        return result;
123    }
124
125    private List<Map<String, Object>> _getKeywords(Collection< ? extends Tag> tags)
126    {
127        List<Map<String, Object>> result = new ArrayList<>();
128
129        for (Tag tag : tags)
130        {
131            result.add(_keyword2json(tag));
132            
133            Map<String, ? extends Tag> subTags = tag.getTags();
134            if (subTags != null)
135            {
136                result.addAll(_getKeywords(subTags.values()));
137            }
138        }
139        
140        return result;
141    }
142    
143    private Map<String, Object> _keyword2json(JCRTag tag)
144    {
145        Map<String, Object> tagInfo = new HashMap<>();
146        tagInfo.put("id", tag.getId());
147        tagInfo.put("name", tag.getName());
148        tagInfo.put("text", tag.getTitle());
149        return tagInfo;
150    }
151    private Map<String, Object> _keyword2json(Tag tag)
152    {
153        Map<String, Object> tagInfo = new HashMap<>();
154        tagInfo.put("id", tag.getId());
155        tagInfo.put("name", tag.getName());
156        tagInfo.put("text", tag.getTitle());
157        return tagInfo;
158    }
159}