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.directory.NotUniqueUserException;
035import org.ametys.core.user.population.PopulationContextHelper;
036import org.ametys.plugins.userdirectory.UserDirectoryHelper;
037import org.ametys.plugins.workspaces.WorkspacesHelper;
038import org.ametys.web.URIPrefixHandler;
039import org.ametys.web.WebConstants;
040import org.ametys.web.WebHelper;
041
042/**
043 * Comments DAO for workspaces
044 *
045 */
046public class CommentsDAO extends org.ametys.web.repository.comment.CommentsDAO
047{
048    /** The population context helper */
049    protected PopulationContextHelper _populationContextHelper;
050    /** The user directory helper */
051    protected UserDirectoryHelper _userDirectoryHelper;
052    /** The URI prefix handler */
053    protected URIPrefixHandler _prefixHandler;
054    private WorkspacesHelper _workspaceHelper;
055
056    @Override
057    public void service(ServiceManager smanager) throws ServiceException
058    {
059        super.service(smanager);
060        _populationContextHelper = (PopulationContextHelper) smanager.lookup(PopulationContextHelper.ROLE);
061        _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE);
062        _prefixHandler = (URIPrefixHandler) smanager.lookup(URIPrefixHandler.ROLE);
063        _workspaceHelper = (WorkspacesHelper) smanager.lookup(WorkspacesHelper.ROLE);
064    }
065    
066    @Override
067    public Map<String, Object> getComment(Comment comment, int level, Map<String, Object> contextualParameters)
068    {
069        Map<String, Object> comment2json = super.getComment(comment, level, contextualParameters);
070        
071        String siteName = (String) contextualParameters.get("siteName");
072        String lang = (String) contextualParameters.get("sitemapLanguage");
073        
074        User user = _getUserByMail(comment.getAuthorEmail(), siteName);
075        if (user != null)
076        {
077            // SAX additionnal user information
078            Content userContent = _userDirectoryHelper.getUserContent(user.getIdentity(), lang);
079            if (userContent != null)
080            {
081                _addAdditionalUserProperties(userContent, user, comment2json);
082            }
083            else
084            {
085                _addUserImage(user, lang, comment2json);
086            }
087        }
088        
089        return comment2json;
090    }
091    
092    @Override
093    protected void saxCommentAdditionalProperties(ContentHandler contentHandler, Comment comment, int level) throws SAXException
094    {
095        super.saxCommentAdditionalProperties(contentHandler, comment, level);
096        
097        Request request = ContextHelper.getRequest(_context);
098        String siteName = WebHelper.getSiteName(request);
099        String lang = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME);
100        
101        User user = _getUserByMail(comment.getAuthorEmail(), siteName);
102        if (user != null)
103        {
104            // SAX additionnal user information
105            Content userContent = _userDirectoryHelper.getUserContent(user.getIdentity(), lang);
106            if (userContent != null)
107            {
108                _saxAdditionalUserProperties(contentHandler, userContent, user);
109            }
110            else
111            {
112                _saxUserImage(contentHandler, user, lang);
113            }
114        }
115    }
116    
117    /**
118     * Sax the user avatar
119     * @param contentHandler the content handler
120     * @param user the user
121     * @param language the language code (if image will be taken from user content)
122     * @throws SAXException if an error occurred while saxing
123     */
124    protected void _saxUserImage(ContentHandler contentHandler,  User user, String language) throws SAXException
125    {
126        XMLUtils.createElement(contentHandler, "author-img-url", _workspaceHelper.getAvatar(user.getIdentity(), language, 45));
127    }
128    
129    /**
130     * Add additional properties for comment's author
131     * @param contentHandler the content handler
132     * @param userContent the user content
133     * @param user the user
134     * @throws SAXException if an error occurred while saxing
135     */
136    protected void _saxAdditionalUserProperties(ContentHandler contentHandler, Content userContent, User user) throws SAXException
137    {
138        _saxUserImage(contentHandler, user, userContent.getLanguage());
139        
140        if (userContent.hasDefinition("function"))
141        {
142            String function = userContent.getValue("function", false, null);
143            if (StringUtils.isNotEmpty(function))
144            {
145                XMLUtils.createElement(contentHandler, "author-function", function);
146            }
147        }
148        
149        if (userContent.hasDefinition("organisation"))
150        {
151            String organisation = userContent.getValue("organisation", false, null);
152            if (StringUtils.isNotEmpty(organisation))
153            {
154                XMLUtils.createElement(contentHandler, "author-organisation", organisation);
155            }
156        }
157        
158        if (userContent.hasDefinition("organisation-accronym"))
159        {
160            String organisationAccr = userContent.getValue("organisation-accronym", false, null);
161            if (StringUtils.isNotEmpty(organisationAccr))
162            {
163                XMLUtils.createElement(contentHandler, "author-organisation-accronym", organisationAccr);
164            }
165        }
166    }
167    
168    /**
169     * Add image for comment's author
170     * @param user the user
171     * @param language the language code (if image will be taken from user content)
172     * @param comment2json the comment properties
173     */
174    protected void _addUserImage(User user, String language, Map<String, Object> comment2json)
175    {
176        Request request = ContextHelper.getRequest(_context);
177        String siteName = WebHelper.getSiteName(request);
178        String uriPrefix = _prefixHandler.getUriPrefix(siteName);
179        
180        String authorImgUrl = "/_plugins/user-directory/user/" + user.getIdentity().getPopulationId() + "/" + user.getIdentity().getLogin() + "/image_45?lang=" + language;
181        comment2json.put("author-img-url", uriPrefix + authorImgUrl);
182    }
183    
184    /**
185     * Add additional properties for comment's author
186     * @param userContent the user content
187     * @param user the user
188     * @param comment2json the comment properties
189     */
190    protected void _addAdditionalUserProperties(Content userContent, User user, Map<String, Object> comment2json)
191    {
192        _addUserImage(user, userContent.getLanguage(), comment2json);
193        
194        if (userContent.hasDefinition("function"))
195        {
196            String function = userContent.getValue("function", false, null);
197            if (StringUtils.isNotEmpty(function))
198            {
199                comment2json.put("author-function", function);
200            }
201        }
202        
203        if (userContent.hasDefinition("organisation"))
204        {
205            String organisation = userContent.getValue("organisation", false, null);
206            if (StringUtils.isNotEmpty(organisation))
207            {
208                comment2json.put("author-organisation", organisation);
209            }
210        }
211        
212        if (userContent.hasDefinition("organisation-accronym"))
213        {
214            String organisationAccr = userContent.getValue("organisation-accronym", false, null);
215            if (StringUtils.isNotEmpty(organisationAccr))
216            {
217                comment2json.put("author-organisation-accronym", organisationAccr);
218            }
219        }
220    }
221    
222    private User _getUserByMail(String email, String siteName)
223    {
224        Set<String> userPopulationsOnSite = _populationContextHelper.getUserPopulationsOnContexts(Arrays.asList("/sites/" + siteName, "/sites-fo/" + siteName), false, false);
225        
226        try
227        {
228            return _userManager.getUserByEmail(userPopulationsOnSite, email);
229        }
230        catch (NotUniqueUserException e)
231        {
232            getLogger().error("Cannot find user in site '" + siteName + "' by email '" + email + "' because 2 or more users match", e);
233            return null;
234        }
235    }
236}