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