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.project.rights.convertor;
017
018import java.util.Collections;
019import java.util.List;
020import java.util.Map.Entry;
021import java.util.Objects;
022import java.util.Set;
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;
028
029import org.ametys.cms.contenttype.ContentTypesHelper;
030import org.ametys.cms.repository.Content;
031import org.ametys.core.right.RightContextConvertor;
032import org.ametys.plugins.explorer.resources.ResourceCollection;
033import org.ametys.plugins.workspaces.project.ProjectManager;
034import org.ametys.plugins.workspaces.project.modules.WorkspaceModule;
035import org.ametys.plugins.workspaces.project.objects.Project;
036import org.ametys.plugins.workspaces.project.rights.ProjectRightHelper;
037import org.ametys.web.repository.content.WebContent;
038
039/**
040 * This {@link RightContextConvertor} delegates rights to workspace module root
041 */
042public class ContentToModuleRightContextConvertor implements RightContextConvertor, Serviceable
043{
044    /** ContentTypes Helper */
045    protected ContentTypesHelper _cTypeHelper;
046    /** Project Manager */
047    protected ProjectManager _projectManager;
048    /** Project right helper */
049    protected ProjectRightHelper _projectRightHelper;
050    
051    private ServiceManager _manager;
052    
053
054    
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        _manager = manager;
058    }
059    
060    /**
061     * Get the project right helper instance
062     * @return the project right helper instance
063     */
064    protected synchronized ProjectRightHelper getProjectRightHelper()
065    {
066        if (_projectRightHelper == null)
067        {
068            try
069            {
070                _projectRightHelper = (ProjectRightHelper) _manager.lookup(ProjectRightHelper.ROLE);
071            }
072            catch (ServiceException e)
073            {
074                throw new RuntimeException(e);
075            }
076        }
077        return _projectRightHelper;
078        
079    }
080    
081    /**
082     * Get the project manager instance
083     * @return the project manager instance
084     */
085    protected synchronized ProjectManager getProjectManager()
086    {
087        if (_projectManager == null)
088        {
089            try
090            {
091                _projectManager = (ProjectManager) _manager.lookup(ProjectManager.ROLE);
092            }
093            catch (ServiceException e)
094            {
095                throw new RuntimeException(e);
096            }
097        }
098        return _projectManager;
099        
100    }
101    
102    /**
103     * Get the content types helper instance
104     * @return the content types helper instance
105     */
106    protected synchronized ContentTypesHelper getContentTypesHelper() 
107    {
108        if (_cTypeHelper == null)
109        {
110            try
111            {
112                _cTypeHelper = (ContentTypesHelper) _manager.lookup(ContentTypesHelper.ROLE);
113            }
114            catch (ServiceException e)
115            {
116                throw new RuntimeException(e);
117            }
118        }
119        return _cTypeHelper;
120    }
121    
122    @SuppressWarnings("unchecked")
123    public Set<Object> convert(Object object)
124    {
125        Set<? extends Object> result = _toModuleRoots(object);
126        return (Set<Object>) result;
127    }
128    
129    /**
130     * Get the module roots associated to current object
131     * @param object The object context
132     * @return the module roots associated to current object or empty set if the object is not linked to a existing module
133     */
134    protected Set<ResourceCollection> _toModuleRoots(Object object)
135    {
136        if (object instanceof WebContent)
137        {
138            WorkspaceModule module = _getWorkspaceModule((Content) object);
139            if (module != null)
140            {
141                List<Project> projects = getProjectManager().getProjectsForSite(((WebContent) object).getSite());
142                return projects.stream()
143                    .map(p -> module.getModuleRoot(p, false))
144                    .filter(Objects::nonNull)
145                    .collect(Collectors.toSet());
146            }
147        }
148        
149        return Collections.EMPTY_SET;
150    }
151    
152    /**
153     * Get the workspace module associated to a content
154     * @param content the content
155     * @return the workspace module or null if the content does not match any module
156     */
157    protected WorkspaceModule _getWorkspaceModule(Content content)
158    {
159        for (Entry<String, WorkspaceModule> entry : getProjectRightHelper().getProjectContentTypesAndModules().entrySet())
160        {
161            if (getContentTypesHelper().isInstanceOf(content, entry.getKey()))
162            {
163                return entry.getValue();
164            }
165        }
166        
167        return null;
168    }
169
170}