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.cms.repository.comment.actions;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031import org.apache.commons.lang.StringUtils;
032
033import org.ametys.cms.content.ContentHelper;
034import org.ametys.cms.repository.Content;
035import org.ametys.cms.repository.comment.Comment;
036import org.ametys.cms.repository.comment.CommentableContent;
037import org.ametys.core.cocoon.JSonReader;
038import org.ametys.core.right.RightManager;
039import org.ametys.core.right.RightManager.RightResult;
040import org.ametys.core.user.CurrentUserProvider;
041import org.ametys.core.user.UserIdentity;
042import org.ametys.core.util.DateUtils;
043import org.ametys.plugins.repository.AmetysObjectResolver;
044
045/**
046 * Action for getting the list of comments.
047 */
048public class GetCommentsAction extends ServiceableAction
049{
050    /** The ametys object resolver */
051    protected AmetysObjectResolver _resolver;
052   
053    /** The right manager */
054    protected RightManager _rightManager;
055    
056    /** The current user provider */
057    protected CurrentUserProvider _currentUserProvider;
058    
059    /** The content helper */
060    protected ContentHelper _contentHelper;
061
062    
063    @Override
064    public void service(ServiceManager smanager) throws ServiceException
065    {
066        super.service(smanager);
067        
068        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
069        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
070        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
071        _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE);
072    }
073    
074    @Override
075    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
076    {
077        Request request = ObjectModelHelper.getRequest(objectModel);
078        Map<String, Object> result = new HashMap<>();
079        
080        @SuppressWarnings("unchecked")
081        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
082        @SuppressWarnings("unchecked")
083        List<String> contentIds = (List<String>) jsParameters.get("contentIds");
084        
085        List<Map<String, Object>> contentComments = new ArrayList<>();
086        
087        UserIdentity user = _currentUserProvider.getUser();
088        
089        for (String contentId : contentIds)
090        {
091            Content content = _resolver.resolveById(contentId);
092            contentComments.add(_content2json(content, user));
093        }
094        
095        result.put("comments", contentComments);
096        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
097        
098        return EMPTY_MAP;
099    }
100    
101    /**
102     * Get the JSON representation of a content
103     * @param content The content
104     * @param user the current user
105     * @return The content as JSON object
106     */
107    protected Map<String, Object> _content2json (Content content, UserIdentity user)
108    {
109        Map<String, Object> object = new HashMap<>();
110        
111        object.put("contentId", content.getId());
112        object.put("commentId", "");
113        object.put("contentTitle", StringUtils.defaultString(content.getTitle()));
114        object.put("text", StringUtils.defaultString(content.getTitle()));
115        
116        if (content instanceof CommentableContent)
117        {
118            if (_rightManager.hasRight(user, "CMS_Rights_CommentModerate", content) == RightResult.RIGHT_ALLOW)
119            {
120                CommentableContent commentableContent = (CommentableContent) content;
121                List<Comment> comments = commentableContent.getComments(true, true);
122                
123                List<Map<String, Object>> commentsList = new ArrayList<>();
124                for (Comment comment : comments)
125                {
126                    commentsList.add(_comment2json(comment, content));
127                }
128                
129                object.put("comments", commentsList);
130            }
131        }
132        
133        return object;
134    }
135    
136    /**
137     * Get the JSON representation of a comment
138     * @param comment the comment
139     * @param content the content
140     * @return The comment as JSON object
141     */
142    protected Map<String, Object> _comment2json (Comment comment, Content content)
143    {
144        Map<String, Object> object = new HashMap<>();
145        
146        object.put("commentId", comment.getId());
147        object.put("contentId", content.getId());
148        object.put("contentTitle", StringUtils.defaultString(content.getTitle()));
149        object.put("text", comment.getContent());
150        object.put("authorName", comment.getAuthorName());
151        object.put("authorEmail", comment.getAuthorEmail());
152        object.put("authorUrl", comment.getAuthorURL());
153        object.put("nbReporters", comment.getNbReporters());
154        object.put("creationDate", DateUtils.dateToString(comment.getCreationDate()));
155        object.put("validated", Boolean.toString(comment.isValidated()));
156        object.put("authorIsEmailHidden", Boolean.toString(comment.isEmailHidden()));
157        
158        List<Comment> comments = comment.getSubComment(true, true);
159        
160        List<Map<String, Object>> commentsList = new ArrayList<>();
161        for (Comment subcomment : comments)
162        {
163            commentsList.add(_comment2json(subcomment, content));
164        }
165        object.put("comments", commentsList);
166        
167        return object;
168    }
169}