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