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 /** Workspaces threads node name */ 053 private static final String __WORKSPACES_THREADS_NODE_NAME = "threads"; 054 055 @Override 056 public String getId() 057 { 058 return THREAD_MODULE_ID; 059 } 060 061 @Override 062 public String getModuleName() 063 { 064 return __WORKSPACES_THREADS_NODE_NAME; 065 } 066 067 public int getOrder() 068 { 069 return ORDER_THREADS; 070 } 071 072 @Override 073 protected String getModulePageName() 074 { 075 return "forum"; 076 } 077 078 public I18nizableText getModuleTitle() 079 { 080 return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_SERVICE_MODULE_THREAD_LABEL"); 081 } 082 public I18nizableText getModuleDescription() 083 { 084 return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_SERVICE_MODULE_THREAD_DESCRIPTION"); 085 } 086 @Override 087 protected I18nizableText getModulePageTitle() 088 { 089 return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_WORKSPACE_PAGE_FORUM_TITLE"); 090 } 091 092 @Override 093 protected void initializeModulePage(ModifiablePage forumPage) 094 { 095 ModifiableZone defaultZone = forumPage.createZone("default"); 096 097 String serviceId = "org.ametys.plugins.workspaces.module.Thread"; 098 ModifiableZoneItem defaultZoneItem = defaultZone.addZoneItem(); 099 defaultZoneItem.setType(ZoneType.SERVICE); 100 defaultZoneItem.setServiceId(serviceId); 101 102 ModifiableModelAwareDataHolder serviceDataHolder = defaultZoneItem.getServiceParameters(); 103 serviceDataHolder.setValue("xslt", _getDefaultXslt(serviceId)); 104 } 105 106 /** 107 * Get the URI of a thread in project'site 108 * @param project The project 109 * @param threadId The id of thread 110 * @return The thread uri 111 */ 112 public String getThreadUri(Project project, String threadId) 113 { 114 String moduleUrl = getModuleUrl(project); 115 if (moduleUrl != null) 116 { 117 StringBuilder sb = new StringBuilder(); 118 sb.append(moduleUrl); 119 sb.append("#").append(threadId); 120 121 return sb.toString(); 122 } 123 124 return null; 125 } 126 127 /** 128 * Retrieve the current user rights on the thread module 129 * @return The map of rights 130 */ 131 @Callable 132 public Map<String, Object> getThreadModuleRights() 133 { 134 Map<String, Object> rights = new HashMap<>(); 135 136 Request request = ContextHelper.getRequest(_context); 137 String projectName = (String) request.getAttribute("projectName"); 138 Project project = _projectManager.getProject(projectName); 139 ModifiableResourceCollection threadRoot = getModuleRoot(project, false); 140 141 rights.put("threadAdd", threadRoot != null && _rightManager.hasRight(_currentUserProvider.getUser(), ThreadDAO.__RIGHTS_THREAD_ADD, threadRoot) == RightResult.RIGHT_ALLOW); 142 143 return rights; 144 } 145 146 @Override 147 public ModifiableResourceCollection getModuleRoot(Project project, boolean create) 148 { 149 try 150 { 151 ExplorerNode projectRootNode = project.getExplorerRootNode(); 152 153 if (projectRootNode instanceof ModifiableResourceCollection) 154 { 155 ModifiableResourceCollection projectRootNodeRc = (ModifiableResourceCollection) projectRootNode; 156 return _getAmetysObject(projectRootNodeRc, __WORKSPACES_THREADS_NODE_NAME, JCRResourcesCollectionFactory.RESOURCESCOLLECTION_NODETYPE, create); 157 } 158 else 159 { 160 throw new IllegalClassException(ModifiableResourceCollection.class, projectRootNode.getClass()); 161 } 162 } 163 catch (AmetysRepositoryException e) 164 { 165 throw new AmetysRepositoryException("Error getting the documents root node.", e); 166 } 167 } 168 169 @Override 170 public Set<String> getAllowedEventTypes() 171 { 172 return ImmutableSet.of("thread.created", "thread.post.created"); 173 } 174}