001/* 002 * Copyright 2020 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.comments; 017 018import java.util.Arrays; 019import java.util.Map; 020import java.util.Set; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.cocoon.components.ContextHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.cocoon.xml.XMLUtils; 027import org.apache.commons.lang3.StringUtils; 028import org.xml.sax.ContentHandler; 029import org.xml.sax.SAXException; 030 031import org.ametys.cms.repository.Content; 032import org.ametys.cms.repository.comment.Comment; 033import org.ametys.core.user.User; 034import org.ametys.core.user.population.PopulationContextHelper; 035import org.ametys.plugins.userdirectory.UserDirectoryHelper; 036import org.ametys.web.URIPrefixHandler; 037import org.ametys.web.WebConstants; 038import org.ametys.web.WebHelper; 039 040/** 041 * Comments DAO for workspaces 042 * 043 */ 044public class CommentsDAO extends org.ametys.web.repository.comment.CommentsDAO 045{ 046 /** The population context helper */ 047 protected PopulationContextHelper _populationContextHelper; 048 /** The user directory helper */ 049 protected UserDirectoryHelper _userDirectoryHelper; 050 /** The URI prefix handler */ 051 protected URIPrefixHandler _prefixHandler; 052 053 @Override 054 public void service(ServiceManager smanager) throws ServiceException 055 { 056 super.service(smanager); 057 _populationContextHelper = (PopulationContextHelper) smanager.lookup(PopulationContextHelper.ROLE); 058 _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE); 059 _prefixHandler = (URIPrefixHandler) smanager.lookup(URIPrefixHandler.ROLE); 060 } 061 062 @Override 063 public Map<String, Object> getComment(Comment comment, int level, Map<String, Object> contextualParameters) 064 { 065 Map<String, Object> comment2json = super.getComment(comment, level, contextualParameters); 066 067 String siteName = (String) contextualParameters.get("siteName"); 068 String lang = (String) contextualParameters.get("sitemapLanguage"); 069 070 User user = _getUserByMail(comment.getAuthorEmail(), siteName); 071 if (user != null) 072 { 073 // SAX additionnal user information 074 Content userContent = _userDirectoryHelper.getUserContent(user.getIdentity(), lang); 075 if (userContent != null) 076 { 077 _addAdditionalUserProperties(userContent, user, comment2json); 078 } 079 } 080 081 return comment2json; 082 } 083 084 @Override 085 protected void saxCommentAdditionalProperties(ContentHandler contentHandler, Comment comment, int level) throws SAXException 086 { 087 super.saxCommentAdditionalProperties(contentHandler, comment, level); 088 089 Request request = ContextHelper.getRequest(_context); 090 String siteName = WebHelper.getSiteName(request); 091 String lang = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME); 092 093 User user = _getUserByMail(comment.getAuthorEmail(), siteName); 094 if (user != null) 095 { 096 // SAX additionnal user information 097 Content userContent = _userDirectoryHelper.getUserContent(user.getIdentity(), lang); 098 if (userContent != null) 099 { 100 _saxAdditionalUserProperties(contentHandler, userContent, user); 101 } 102 else 103 { 104 _saxUserImage(contentHandler, user, lang); 105 } 106 } 107 } 108 109 /** 110 * Sax the user avatar 111 * @param contentHandler the content handler 112 * @param user the user 113 * @param language the language code (if image will be taken from user content) 114 * @throws SAXException if an error occurred while saxing 115 */ 116 protected void _saxUserImage(ContentHandler contentHandler, User user, String language) throws SAXException 117 { 118 Request request = ContextHelper.getRequest(_context); 119 String siteName = WebHelper.getSiteName(request); 120 String uriPrefix = _prefixHandler.getUriPrefix(siteName); 121 122 String authorImgUrl = "/_plugins/workspaces/user/" + user.getIdentity().getPopulationId() + "/" + user.getIdentity().getLogin() + "/image_45?lang=" + language; 123 XMLUtils.createElement(contentHandler, "author-img-url", uriPrefix + authorImgUrl); 124 } 125 126 /** 127 * Add additional properties for comment's author 128 * @param contentHandler the content handler 129 * @param userContent the user content 130 * @param user the user 131 * @throws SAXException if an error occurred while saxing 132 */ 133 protected void _saxAdditionalUserProperties(ContentHandler contentHandler, Content userContent, User user) throws SAXException 134 { 135 _saxUserImage(contentHandler, user, userContent.getLanguage()); 136 137 String function = userContent.getValue("function", false, null); 138 if (StringUtils.isNotEmpty(function)) 139 { 140 XMLUtils.createElement(contentHandler, "author-function", function); 141 } 142 String organisation = userContent.getValue("organisation", false, null); 143 if (StringUtils.isNotEmpty(organisation)) 144 { 145 XMLUtils.createElement(contentHandler, "author-organisation", organisation); 146 } 147 String organisationAccr = userContent.getValue("organisation-accronym", false, null); 148 if (StringUtils.isNotEmpty(organisationAccr)) 149 { 150 XMLUtils.createElement(contentHandler, "author-organisation-accronym", organisationAccr); 151 } 152 } 153 154 /** 155 * Add additional properties for comment's author 156 * @param userContent the user content 157 * @param user the user 158 * @param comment2json the comment properties 159 */ 160 protected void _addAdditionalUserProperties(Content userContent, User user, Map<String, Object> comment2json) 161 { 162 Request request = ContextHelper.getRequest(_context); 163 String siteName = WebHelper.getSiteName(request); 164 String uriPrefix = _prefixHandler.getUriPrefix(siteName); 165 166 String authorImgUrl = "/_plugins/workspaces/user/" + user.getIdentity().getPopulationId() + "/" + user.getIdentity().getLogin() + "/image_45?lang=" + userContent.getLanguage(); 167 comment2json.put("author-img-url", uriPrefix + authorImgUrl); 168 169 String function = userContent.getValue("function", false, null); 170 if (StringUtils.isNotEmpty(function)) 171 { 172 comment2json.put("author-function", function); 173 } 174 String organisation = userContent.getValue("organisation", false, null); 175 if (StringUtils.isNotEmpty(organisation)) 176 { 177 comment2json.put("author-organisation", function); 178 } 179 String organisationAccr = userContent.getValue("organisation-accronym", false, null); 180 if (StringUtils.isNotEmpty(organisationAccr)) 181 { 182 comment2json.put("author-organisation-accronym", organisationAccr); 183 } 184 } 185 186 private User _getUserByMail(String email, String siteName) 187 { 188 Set<String> userPopulationsOnSite = _populationContextHelper.getUserPopulationsOnContexts(Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName), false, false); 189 190 for (String populationId : userPopulationsOnSite) 191 { 192 User user = _userHelper.getUserByEmail(populationId, email); 193 if (user != null) 194 { 195 return user; 196 } 197 } 198 199 return null; 200 } 201}