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.linkdirectory.theme;
017
018import java.util.HashSet;
019import java.util.Map;
020import java.util.Set;
021
022import javax.jcr.RepositoryException;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.cms.tag.Tag;
029import org.ametys.cms.tag.TagProvider;
030import org.ametys.cms.tag.jcr.AbstractJCRTagsDAO;
031import org.ametys.cms.tag.jcr.JCRTag;
032import org.ametys.core.right.RightManager;
033import org.ametys.core.right.RightManager.RightResult;
034import org.ametys.core.user.UserIdentity;
035import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
036import org.ametys.plugins.repository.UnknownAmetysObjectException;
037
038/**
039 * Component for operations on JCR tags
040 */
041public class JCRThemesDAO extends AbstractJCRTagsDAO
042{
043    /** The Avalon role */
044    public static final String ROLE = JCRThemesDAO.class.getName();
045    
046    /** The tag provider extension point */
047    protected ThemeProviderExtensionPoint _tagProviderExtPt;
048    
049    /** The rights manager */
050    protected RightManager _rightManager;
051    
052    @Override
053    public void service(ServiceManager serviceManager) throws ServiceException
054    {
055        super.service(serviceManager);
056        _tagProviderExtPt = (ThemeProviderExtensionPoint) serviceManager.lookup(ThemeProviderExtensionPoint.ROLE);
057        _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE);
058    }
059    
060    @Override
061    public ModifiableTraversableAmetysObject _getTagRootObject (String tagProviderId, Map<String, Object> contextualParameters) throws RepositoryException
062    {
063        JCRThemeProvider provider = (JCRThemeProvider) _tagProviderExtPt.getExtension(tagProviderId);
064        return provider.getRootNode(contextualParameters);
065    }
066    
067    @Override
068    protected void _checkUserRight() throws IllegalStateException
069    {
070        String rightId = "LinkDirectory_Rights_Themes_Handle";
071        String context = "/cms";
072        
073        UserIdentity user = _currentUserProvider.getUser();
074        if (_rightManager.hasRight(user, rightId, context) != RightResult.RIGHT_ALLOW)
075        {
076            getLogger().error("User '" + user + "' tried to access a privileged feature without convenient right. Should have right '" + rightId + "' on context '" + context + "'");
077            throw new IllegalStateException("You have no right to access this feature.");
078        }
079    }
080
081    @Override
082    protected Tag _getTagFromName(String name, Map<String, Object> contextualParameters)
083    {
084        return _tagProviderExtPt.getTag(name, contextualParameters);
085    }
086    
087    @Override
088    protected JCRTag _createJCRTag(String parentId, String name, String title, String description, Map<String, Object> otherParameters, Map<String, Object> contextualParameters) throws RepositoryException
089    {
090        ModifiableTraversableAmetysObject parent = null;
091        if (StringUtils.isEmpty(parentId))
092        {
093            parent = _getTagRootObject(JCRThemeProvider.class.getName(), contextualParameters);
094        }
095        else
096        {
097            parent = _resolver.resolveById(parentId);
098        }
099        
100        if (parent.hasChild(name))
101        {
102            throw new RepositoryException("This theme already exists.");
103        }
104        ThemeJCR jcrTag = parent.createChild(name, ThemeFactory.TAG_NODETYPE);
105        jcrTag.setTitle(title);
106        jcrTag.setDescription(description);
107        
108        parent.saveChanges();
109        
110        return jcrTag;
111    }
112    
113    @Override
114    protected JCRTag _updateJCRTag(String tagId, String title, String description, Map<String, Object> otherParameters) throws UnknownAmetysObjectException
115    {
116        ThemeJCR jcrTag = _resolver.resolveById(tagId);
117        jcrTag.setTitle(title);
118        jcrTag.setDescription(description);
119        
120        jcrTag.saveChanges();
121        
122        return jcrTag;
123    }
124
125    @Override
126    protected Set<TagProvider< ? extends Tag>> _getTagProviders()
127    {
128        Set<TagProvider<? extends Tag>> providers = new HashSet<>();
129        
130        Set<String> ids = _tagProviderExtPt.getExtensionsIds();
131        for (String id : ids)
132        {
133            TagProvider<? extends Tag> provider = _tagProviderExtPt.getExtension(id);
134            providers.add(provider);
135        }
136        
137        return providers;
138    }
139}