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