001/* 002 * Copyright 2018 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.members; 017 018import java.io.IOException; 019import java.util.Iterator; 020import java.util.List; 021import java.util.Locale; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.apache.cocoon.xml.AttributesImpl; 030import org.apache.cocoon.xml.XMLUtils; 031import org.apache.commons.lang.StringUtils; 032import org.apache.commons.lang3.ArrayUtils; 033import org.xml.sax.SAXException; 034 035import org.ametys.cms.contenttype.ContentType; 036import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 037import org.ametys.cms.contenttype.MetadataManager; 038import org.ametys.cms.contenttype.MetadataSet; 039import org.ametys.cms.repository.Content; 040import org.ametys.core.group.GroupManager; 041import org.ametys.core.right.RightManager; 042import org.ametys.core.user.User; 043import org.ametys.core.user.UserIdentity; 044import org.ametys.plugins.core.user.UserHelper; 045import org.ametys.plugins.repository.AmetysRepositoryException; 046import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 047import org.ametys.plugins.userdirectory.UserDirectoryHelper; 048import org.ametys.plugins.userdirectory.UserDirectoryPageHandler; 049import org.ametys.plugins.workspaces.members.JCRProjectMember.MemberType; 050import org.ametys.plugins.workspaces.members.ProjectMemberManager.ProjectMember; 051import org.ametys.plugins.workspaces.project.ProjectManager; 052import org.ametys.plugins.workspaces.project.objects.Project; 053import org.ametys.web.WebConstants; 054import org.ametys.web.repository.page.Page; 055import org.ametys.web.repository.page.ZoneItem; 056import org.ametys.web.usermanagement.UserSignUpConfiguration; 057 058/** 059 * Generator used sax project member 060 */ 061public class ProjectMembersGenerator extends ServiceableGenerator 062{ 063 /** The project manager component */ 064 protected ProjectManager _projectManager; 065 066 /** The project member manager */ 067 protected ProjectMemberManager _projectMemberManager; 068 069 /** The user helper */ 070 protected UserHelper _userHelper; 071 072 /** The user directory helper */ 073 protected UserDirectoryHelper _userDirectoryHelper; 074 075 /** The metadata manager */ 076 protected MetadataManager _metadataManager; 077 078 /** The content type extension point */ 079 protected ContentTypeExtensionPoint _cTypeEP; 080 081 /** The group manager */ 082 protected GroupManager _groupManager; 083 084 /** The sign up configuration */ 085 protected UserSignUpConfiguration _signupConfig; 086 087 /** The right manager */ 088 protected RightManager _rightManager; 089 090 @Override 091 public void service(ServiceManager serviceManager) throws ServiceException 092 { 093 _projectManager = (ProjectManager) serviceManager.lookup(ProjectManager.ROLE); 094 _projectMemberManager = (ProjectMemberManager) serviceManager.lookup(ProjectMemberManager.ROLE); 095 _userHelper = (UserHelper) serviceManager.lookup(UserHelper.ROLE); 096 _userDirectoryHelper = (UserDirectoryHelper) serviceManager.lookup(UserDirectoryHelper.ROLE); 097 _metadataManager = (MetadataManager) serviceManager.lookup(MetadataManager.ROLE); 098 _cTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE); 099 _groupManager = (GroupManager) serviceManager.lookup(GroupManager.ROLE); 100 _signupConfig = (UserSignUpConfiguration) serviceManager.lookup(UserSignUpConfiguration.ROLE); 101 _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE); 102 } 103 104 @Override 105 public void generate() throws IOException, SAXException, ProcessingException 106 { 107 Request request = ObjectModelHelper.getRequest(objectModel); 108 109 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 110 String lang = page.getSitemapName(); 111 112 ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM); 113 ModelAwareDataHolder serviceParameters2018 = zoneItem.getServiceParameters(); 114 115 long maxNbMembers = serviceParameters2018.getValue("nbMembers", false, -1L); 116 boolean expandGroup = serviceParameters2018.getValue("expandGroup", false, false); 117 118 contentHandler.startDocument(); 119 120 List<Project> projects = _projectManager.getProjectsForSite(page.getSite()); 121 Project project = projects.isEmpty() ? null : projects.get(0); 122 123 if (project == null) 124 { 125 throw new IllegalArgumentException("There is no project for site " + page.getSiteName()); 126 } 127 128 try 129 { 130 List<ProjectMember> members = _projectMemberManager.getProjectMembers(project, expandGroup); 131 132 AttributesImpl attrs = new AttributesImpl(); 133 attrs.addCDATAAttribute("nbMembers", String.valueOf(members.size())); 134 135 XMLUtils.startElement(contentHandler, "project", attrs); 136 137 UserIdentity[] managers = project.getManagers(); 138 139 int count = 0; 140 Iterator<ProjectMember> it = members.iterator(); 141 while (it.hasNext() && (maxNbMembers < 1 || count < maxNbMembers)) 142 { 143 ProjectMember member = it.next(); 144 _saxMember(member, managers, lang); 145 count++; 146 } 147 148 XMLUtils.endElement(contentHandler, "project"); 149 } 150 catch (IllegalAccessException | AmetysRepositoryException e) 151 { 152 throw new ProcessingException("Unable to get project members", e); 153 } 154 155 contentHandler.endDocument(); 156 } 157 158 /** 159 * SAX a member 160 * @param member the member 161 * @param managers the user identities of project's manager 162 * @param lang the current language 163 * @throws SAXException if an error occurred while saxing 164 * @throws AmetysRepositoryException if an error occurred 165 * @throws IOException if failed to sax user content 166 */ 167 protected void _saxMember (ProjectMember member, UserIdentity[] managers, String lang) throws SAXException, AmetysRepositoryException, IOException 168 { 169 AttributesImpl attrs = new AttributesImpl(); 170 attrs.addCDATAAttribute("manager", String.valueOf(member.isManager())); 171 172 if (member.isManager()) 173 { 174 // Get the position of this manager 175 int position = ArrayUtils.indexOf(managers, member.getUser().getIdentity()); 176 attrs.addCDATAAttribute("managerPosition", String.valueOf(position + 1)); 177 } 178 179 attrs.addCDATAAttribute("type", member.getType().toString()); 180 181 XMLUtils.startElement(contentHandler, "member", attrs); 182 183 XMLUtils.createElement(contentHandler, "title", member.getTitle()); 184 XMLUtils.createElement(contentHandler, "sortabletitle", member.getSortableTitle()); 185 String role = member.getRole(); 186 if (StringUtils.isNotBlank(role)) 187 { 188 XMLUtils.createElement(contentHandler, "role", role); 189 } 190 191 if (member.getType().equals(MemberType.USER)) 192 { 193 User user = member.getUser(); 194 _saxUser(user, lang); 195 } 196 197 XMLUtils.endElement(contentHandler, "member"); 198 } 199 200 /** 201 * Sax user 202 * @param user the user 203 * @param lang the lang 204 * @throws SAXException if an error occurs saxing data 205 * @throws AmetysRepositoryException if an repository error occurs 206 * @throws IOException if an IO exception error occurs 207 */ 208 protected void _saxUser(User user, String lang) throws SAXException, AmetysRepositoryException, IOException 209 { 210 XMLUtils.startElement(contentHandler, "user"); 211 _userHelper.saxUser(user, contentHandler, "info"); 212 Content userContent = _userDirectoryHelper.getUserContent(user.getIdentity(), lang); 213 if (userContent != null) 214 { 215 AttributesImpl contentAttrs = new AttributesImpl(); 216 contentAttrs.addCDATAAttribute("id", userContent.getId()); 217 XMLUtils.startElement(contentHandler, "content", contentAttrs); 218 ContentType contentType = _cTypeEP.getExtension(UserDirectoryPageHandler.ABSTRACT_USER_CONTENT_TYPE); 219 MetadataSet metadataSet = contentType.getMetadataSetForView("abstract"); 220 _metadataManager.saxMetadata(contentHandler, userContent, metadataSet, new Locale(lang)); 221 XMLUtils.endElement(contentHandler, "content"); 222 } 223 XMLUtils.endElement(contentHandler, "user"); 224 225 } 226}