001/*
002 *  Copyright 2016 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.documents;
017
018import java.util.HashMap;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.commons.lang.IllegalClassException;
027
028import org.ametys.core.right.RightManager.RightResult;
029import org.ametys.core.ui.Callable;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.plugins.explorer.ExplorerNode;
032import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
033import org.ametys.plugins.explorer.resources.jcr.JCRResourcesCollectionFactory;
034import org.ametys.plugins.repository.AmetysRepositoryException;
035import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
036import org.ametys.plugins.workspaces.AbstractWorkspaceModule;
037import org.ametys.plugins.workspaces.project.objects.Project;
038import org.ametys.runtime.i18n.I18nizableText;
039import org.ametys.web.repository.page.ModifiablePage;
040import org.ametys.web.repository.page.ModifiableZone;
041import org.ametys.web.repository.page.ModifiableZoneItem;
042import org.ametys.web.repository.page.ZoneItem.ZoneType;
043
044import com.google.common.collect.ImmutableSet;
045
046/**
047 * Helper component for managing documents
048 */
049public class DocumentWorkspaceModule extends AbstractWorkspaceModule
050{
051    /** The id of document module */
052    public static final String DODUMENT_MODULE_ID = DocumentWorkspaceModule.class.getName();
053    
054    /** Workspaces documents node name */
055    public static final String WORKSPACES_DOCUMENTS_NODE_NAME = "documents";
056    
057    @Override
058    public void contextualize(Context context) throws ContextException
059    {
060        _context = context;
061    }
062    
063    @Override
064    public String getId()
065    {
066        return DODUMENT_MODULE_ID;
067    }
068    
069    @Override
070    public String getModuleName()
071    {
072        return WORKSPACES_DOCUMENTS_NODE_NAME;
073    }
074    
075    public int getOrder()
076    {
077        return ORDER_DOCUMENTS;
078    }
079    
080    @Override
081    protected String getModulePageName()
082    {
083        return "documents";
084    }
085    
086    public I18nizableText getModuleTitle()
087    {
088        return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_SERVICE_MODULE_DOCUMENT_LABEL");
089    }
090    public I18nizableText getModuleDescription()
091    {
092        return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_SERVICE_MODULE_DOCUMENT_DESCRIPTION");
093    }
094    @Override
095    protected I18nizableText getModulePageTitle()
096    {
097        return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_WORKSPACE_PAGE_DOCUMENTS_TITLE");
098    }
099    
100    @Override
101    protected void initializeModulePage(ModifiablePage documentPage)
102    {
103        ModifiableZone defaultZone = documentPage.createZone("default");
104        
105        String serviceId = "org.ametys.plugins.workspaces.module.Document";
106        ModifiableZoneItem defaultZoneItem = defaultZone.addZoneItem();
107        defaultZoneItem.setType(ZoneType.SERVICE);
108        defaultZoneItem.setServiceId(serviceId);
109        
110        ModifiableModelAwareDataHolder serviceDataHolder = defaultZoneItem.getServiceParameters();
111        serviceDataHolder.setValue("xslt", _getDefaultXslt(serviceId));
112    }
113
114    /**
115     * Get the URI of a folder in project'site
116     * @param project The project
117     * @param collectionId The id of collection
118     * @return The thread uri
119     */
120    public String getFolderUri(Project project, String collectionId)
121    {
122        String moduleUrl = getModuleUrl(project);
123        if (moduleUrl != null)
124        {
125            StringBuilder sb = new StringBuilder();
126            sb.append(moduleUrl);
127            sb.append("#").append(collectionId);
128            
129            return sb.toString();
130        }
131        
132        return null;
133    }
134    
135    /**
136     * Retrieves the set of general rights used in the document module for the current user
137     * @return The map of right data. Keys are the rights id, and values indicates whether the current user has the right or not.
138     */
139    @Callable
140    public Map<String, Object> getModuleBaseRights()
141    {
142        Request request = ContextHelper.getRequest(_context);
143        
144        String projectName = (String) request.getAttribute("projectName");
145        Project project = _projectManager.getProject(projectName);
146        
147        ModifiableResourceCollection documentRoot = getModuleRoot(project, false);
148        
149        Map<String, Object> rightsData = new HashMap<>();
150        UserIdentity user = _currentUserProvider.getUser();
151        
152        // Add file / folder
153        rightsData.put("add-file", _rightManager.hasRight(user, "Plugin_Explorer_File_Add", documentRoot) == RightResult.RIGHT_ALLOW);
154        rightsData.put("add-folder", _rightManager.hasRight(user, "Plugin_Explorer_Folder_Add", documentRoot) == RightResult.RIGHT_ALLOW);
155        rightsData.put("add-cmis-folder", _rightManager.hasRight(user, "Plugin_Explorer_CMIS_Add", documentRoot) == RightResult.RIGHT_ALLOW);
156        
157        // Tags
158        rightsData.put("add-tag", _projectRightHelper.canAddTag(project));
159        rightsData.put("remove-tag", _projectRightHelper.canRemoveTag(project));
160        
161        return rightsData;
162    }
163    
164    @Override
165    public ModifiableResourceCollection getModuleRoot(Project project, boolean create)
166    {
167        try
168        {
169            ExplorerNode projectRootNode = project.getExplorerRootNode();
170            
171            if (projectRootNode instanceof ModifiableResourceCollection)
172            {
173                ModifiableResourceCollection projectRootNodeRc = (ModifiableResourceCollection) projectRootNode;
174                return _getAmetysObject(projectRootNodeRc, WORKSPACES_DOCUMENTS_NODE_NAME, JCRResourcesCollectionFactory.RESOURCESCOLLECTION_NODETYPE, create);
175            }
176            else
177            {
178                throw new IllegalClassException(ModifiableResourceCollection.class, projectRootNode.getClass());
179            }
180        }
181        catch (AmetysRepositoryException e)
182        {
183            throw new AmetysRepositoryException("Error getting the documents root node.", e);
184        }
185    }
186
187    @Override
188    public Set<String> getAllowedEventTypes()
189    {
190        return ImmutableSet.of("resource.created", "resource.updated", "resource.renamed");
191    }
192}