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