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.threads; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Set; 021 022import org.apache.cocoon.components.ContextHelper; 023import org.apache.cocoon.environment.Request; 024import org.apache.commons.lang.IllegalClassException; 025 026import org.ametys.core.right.RightManager.RightResult; 027import org.ametys.core.ui.Callable; 028import org.ametys.plugins.explorer.ExplorerNode; 029import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 030import org.ametys.plugins.explorer.resources.jcr.JCRResourcesCollectionFactory; 031import org.ametys.plugins.explorer.threads.actions.ThreadDAO; 032import org.ametys.plugins.repository.AmetysRepositoryException; 033import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder; 034import org.ametys.plugins.workspaces.AbstractWorkspaceModule; 035import org.ametys.plugins.workspaces.project.objects.Project; 036import org.ametys.runtime.i18n.I18nizableText; 037import org.ametys.web.repository.page.ModifiablePage; 038import org.ametys.web.repository.page.ModifiableZone; 039import org.ametys.web.repository.page.ModifiableZoneItem; 040import org.ametys.web.repository.page.ZoneItem.ZoneType; 041 042import com.google.common.collect.ImmutableSet; 043 044/** 045 * ThreadDAO to interact with projects aware threads 046 */ 047public class ThreadWorkspaceModule extends AbstractWorkspaceModule 048{ 049 /** The id of thread module */ 050 public static final String THREAD_MODULE_ID = ThreadWorkspaceModule.class.getName(); 051 052 /** Tag on the main page holding the thread module */ 053 private static final String __THREAD_MODULE_TAG = "WORKSPACES_MODULE_THREAD"; 054 055 /** Workspaces threads node name */ 056 private static final String __WORKSPACES_THREADS_NODE_NAME = "threads"; 057 058 /** Module i18n title key */ 059 private static final String __MODULE_TITLE_KEY = "PLUGINS_WORKSPACES_PROJECT_SERVICE_MODULE_THREAD_LABEL"; 060 061 @Override 062 public String getId() 063 { 064 return THREAD_MODULE_ID; 065 } 066 067 @Override 068 public String getModuleName() 069 { 070 return __WORKSPACES_THREADS_NODE_NAME; 071 } 072 073 @Override 074 protected String getModulePageName() 075 { 076 return "forum"; 077 } 078 079 @Override 080 public I18nizableText getModuleTitle() 081 { 082 return new I18nizableText("plugin." + _pluginName, __MODULE_TITLE_KEY); 083 } 084 085 @Override 086 protected I18nizableText getModulePageTitle() 087 { 088 return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_WORKSPACE_PAGE_FORUM_TITLE"); 089 } 090 091 @Override 092 protected String getModuleTagName() 093 { 094 return __THREAD_MODULE_TAG; 095 } 096 097 @Override 098 protected void initializeModulePage(ModifiablePage forumPage) 099 { 100 ModifiableZone defaultZone = forumPage.createZone("default"); 101 102 String serviceId = "org.ametys.plugins.workspaces.module.Thread"; 103 ModifiableZoneItem defaultZoneItem = defaultZone.addZoneItem(); 104 defaultZoneItem.setType(ZoneType.SERVICE); 105 defaultZoneItem.setServiceId(serviceId); 106 107 ModifiableModelAwareDataHolder serviceDataHolder = defaultZoneItem.getServiceParameters(); 108 serviceDataHolder.setValue("xslt", _getDefaultXslt(serviceId)); 109 } 110 111 /** 112 * Get the URI of a thread in project'site 113 * @param project The project 114 * @param threadId The id of thread 115 * @param language The sitemap language 116 * @return The thread uri 117 */ 118 public String getThreadUri(Project project, String threadId, String language) 119 { 120 String moduleUrl = getModuleUrl(project, language); 121 if (moduleUrl != null) 122 { 123 StringBuilder sb = new StringBuilder(); 124 sb.append(moduleUrl); 125 sb.append("#").append(threadId); 126 127 return sb.toString(); 128 } 129 130 return null; 131 } 132 133 /** 134 * Retrieve the current user rights on the thread module 135 * @return The map of rights 136 */ 137 @Callable 138 public Map<String, Object> getThreadModuleRights() 139 { 140 Map<String, Object> rights = new HashMap<>(); 141 142 Request request = ContextHelper.getRequest(_context); 143 String projectName = (String) request.getAttribute("projectName"); 144 Project project = _projectManager.getProject(projectName); 145 ModifiableResourceCollection threadRoot = getModuleRoot(project, false); 146 147 rights.put("threadAdd", threadRoot != null && _rightManager.hasRight(_currentUserProvider.getUser(), ThreadDAO.__RIGHTS_THREAD_ADD, threadRoot) == RightResult.RIGHT_ALLOW); 148 149 return rights; 150 } 151 152 @Override 153 public ModifiableResourceCollection getModuleRoot(Project project, boolean create) 154 { 155 try 156 { 157 ExplorerNode projectRootNode = project.getExplorerRootNode(); 158 159 if (projectRootNode instanceof ModifiableResourceCollection) 160 { 161 ModifiableResourceCollection projectRootNodeRc = (ModifiableResourceCollection) projectRootNode; 162 return _getAmetysObject(projectRootNodeRc, __WORKSPACES_THREADS_NODE_NAME, JCRResourcesCollectionFactory.RESOURCESCOLLECTION_NODETYPE, create); 163 } 164 else 165 { 166 throw new IllegalClassException(ModifiableResourceCollection.class, projectRootNode.getClass()); 167 } 168 } 169 catch (AmetysRepositoryException e) 170 { 171 throw new AmetysRepositoryException("Error getting the documents root node.", e); 172 } 173 } 174 175 @Override 176 public Set<String> getAllowedEventTypes() 177 { 178 return ImmutableSet.of("thread.created", "thread.post.created"); 179 } 180}