001/*
002 *  Copyright 2014 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 */
016
017package org.ametys.cms.repository.comment.ui;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.ProcessingException;
028import org.apache.commons.lang.StringUtils;
029
030import org.ametys.cms.ObservationConstants;
031import org.ametys.cms.content.ContentHelper;
032import org.ametys.cms.repository.Content;
033import org.ametys.cms.repository.comment.Comment;
034import org.ametys.cms.repository.comment.CommentableContent;
035import org.ametys.core.observation.Event;
036import org.ametys.core.observation.ObservationManager;
037import org.ametys.core.right.RightManager.RightResult;
038import org.ametys.core.ui.Callable;
039import org.ametys.core.ui.StaticClientSideElement;
040import org.ametys.core.user.CurrentUserProvider;
041import org.ametys.core.user.UserIdentity;
042import org.ametys.core.util.DateUtils;
043import org.ametys.plugins.repository.AmetysObjectResolver;
044import org.ametys.plugins.repository.UnknownAmetysObjectException;
045import org.ametys.plugins.repository.metadata.UnknownMetadataException;
046
047/**
048 * This client site elements creates a button representing the validation state of a content's comment
049 */
050public class CommentClientSideElement extends StaticClientSideElement
051{
052    /** The constant for the name of the author from parameters */
053    public static final String PARAMETER_AUTHOR_NAME = "author-name";
054    /** The constant for the email of the author from parameters */
055    public static final String PARAMETER_AUTHOR_EMAIL = "author-email";
056    /** The constant for the hidden status of the email of the author from parameters */
057    public static final String PARAMETER_AUTHOR_EMAILHIDDEN = "author-emailhidden";
058    /** The constant for the url of the author from parameters */
059    public static final String PARAMETER_AUTHOR_URL = "author-url";
060    /** The constant for the text from parameters */
061    public static final String PARAMETER_TEXT = "text";
062    /** The constant for the content id from parameters */
063    public static final String PARAMETER_CONTENT_ID = "contentId";
064    /** The constant for the comment id from parameters */
065    public static final String PARAMETER_COMMENT_ID = "commentId";
066    /** The Ametys object resolver */
067    protected AmetysObjectResolver _resolver;
068    /** The current user provider */
069    protected CurrentUserProvider _userProvider;
070    /** The observation manager */
071    protected ObservationManager _observationManager;
072    /** The content helper */
073    private ContentHelper _contentHelper;
074
075    @Override
076    public void service(ServiceManager smanager) throws ServiceException
077    {
078        super.service(smanager);
079        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
080        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
081        _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE);
082        _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE);
083    }
084    
085    /**
086     * Return the list of comments with its validation state
087     * @param parameters The contents and their comments
088     * @return The comments and their validated status
089     */
090    @Callable
091    public Map<String, Object> getComments(Map<String, Object> parameters)
092    {
093        Map<String, Object> results = new HashMap<>();
094        results.put("comments", new ArrayList<Map<String, Object>>());
095        
096        for (String contentId : parameters.keySet())
097        {
098            try
099            {
100                Content content = _resolver.resolveById(contentId);
101                if (content instanceof CommentableContent)
102                {
103                    CommentableContent cContent = (CommentableContent) content;
104                    
105                    @SuppressWarnings("unchecked")
106                    List<String> commentIds = (List<String>) parameters.get(contentId);
107                    
108                    for (String commentId : commentIds)
109                    {
110                        try
111                        {
112                            Comment comment = cContent.getComment(commentId);
113                            @SuppressWarnings("unchecked")
114                            List<Map<String, Object>> comments = (List<Map<String, Object>>) results.get("comments");
115                            comments.add(getCommentParameters(cContent, comment));
116                        }
117                        catch (UnknownMetadataException e)
118                        {
119                            getLogger().warn("Ignoring request to get comment status " + contentId + " " + commentId + " because it does not exist", e);
120                        }
121                    }   
122                }
123                else
124                {
125                    getLogger().warn("Ignoring request to get comments status on non commentable content " + contentId);
126                }            
127            }
128            catch (UnknownAmetysObjectException e)
129            {
130                getLogger().warn("Ignoring request to get comments status on inexisting content " + contentId, e);
131            }
132        }
133        
134        return results;
135    }
136    
137    /**
138     * Get a comment properties
139     * @param contentId the id of the content
140     * @param commentId the id of the comment. Can be null
141     * @return results the server's response in JSON.
142     * @throws IOException If an error occured
143     * @throws ProcessingException If an error occured
144     */
145    @Callable
146    public Map<String, Object> getComment(String contentId, String commentId) throws IOException, ProcessingException
147    {
148        Map<String, Object> results = new HashMap<> ();
149               
150        UserIdentity user = _userProvider.getUser();
151        
152        Map<String, Object> comments = new HashMap<> ();
153        
154        Content content = _resolver.resolveById(contentId);
155        if (StringUtils.isNotEmpty(commentId))
156        {
157            if (content instanceof CommentableContent)
158            {
159                if (_rightManager.hasRight(user, "CMS_Rights_CommentModerate", content) == RightResult.RIGHT_ALLOW)
160                {
161                    CommentableContent commentableContent = (CommentableContent) content;
162                    Comment comment = commentableContent.getComment(commentId);
163                    comments = _jsonifyComment(comment, content);
164                }
165            }
166        }
167        
168        results.put("comments", comments);
169        return results;
170    }
171
172    /**
173     * Edit a comment if connected user has sufficient rights
174     * @param parameters the JS parameters. Necessarily contains the content and comment id, and the values to edit
175     * @return An empty map
176     */
177    @Callable 
178    public Map<String, Object> editComment(Map<String, Object> parameters)
179    {
180        String contentId = (String) parameters.get(PARAMETER_CONTENT_ID);
181        String commentId = (String) parameters.get(PARAMETER_COMMENT_ID);
182        
183        try
184        {
185            Content content = _resolver.resolveById(contentId);
186
187            if (!hasRight("CMS_Rights_CommentModerate", content))
188            {
189                String errorMessage = "User " + getCurrentUser() + " try to edit a comment on content of id '" + contentId + "' with no sufficient rights"; 
190                getLogger().error(errorMessage);
191                throw new IllegalStateException(errorMessage);
192            }
193            
194            if (!(content instanceof CommentableContent))
195            {
196                String errorMessage = "Can not edit comment for non-commentable content of id '" + contentId + "'"; 
197                getLogger().error(errorMessage);
198                throw new IllegalStateException(errorMessage);
199            }
200            
201            CommentableContent cContent = (CommentableContent) content;
202            
203            Comment comment = cContent.getComment(commentId);
204            
205            String oldAuthorName = comment.getAuthorName();
206            String oldAuthorEmail = comment.getAuthorEmail();
207            boolean oldAuthorEmailHidden = comment.isEmailHidden();
208            String oldAuthorURL = comment.getAuthorURL();
209            String oldContent = comment.getContent();
210            
211            boolean needSave = false;
212            
213            String authorName = (String) parameters.get(PARAMETER_AUTHOR_NAME);
214            if (!authorName.equals(oldAuthorName))
215            {
216                comment.setAuthorName(authorName);
217                needSave = true;
218            }
219            
220            String authorEmail = (String) parameters.get(PARAMETER_AUTHOR_EMAIL);
221            if (!authorEmail.equals(oldAuthorEmail))
222            {
223                comment.setAuthorEmail(authorEmail);
224                needSave = true;
225            }
226            
227            boolean authorEmailHidden = (Boolean) parameters.get(PARAMETER_AUTHOR_EMAILHIDDEN);
228            if (authorEmailHidden != oldAuthorEmailHidden)
229            {
230                comment.setEmailHiddenStatus(authorEmailHidden);
231                needSave = true;
232            }
233            
234            String authorUrl = (String) parameters.get(PARAMETER_AUTHOR_URL);
235            if (!authorUrl.equals(oldAuthorURL))
236            {
237                comment.setAuthorURL(authorUrl);
238                needSave = true;
239            }
240            
241            String text = (String) parameters.get(PARAMETER_TEXT);
242            if (!text.equals(oldContent))
243            {
244                comment.setContent(text);
245                needSave = true;
246            }
247            
248            if (needSave)
249            {
250                cContent.saveChanges();
251                
252                Map<String, Object> eventParams = new HashMap<>();
253                eventParams.put(ObservationConstants.ARGS_CONTENT, content);
254                eventParams.put(ObservationConstants.ARGS_COMMENT, comment);
255                eventParams.put("content.comment.old.author", oldAuthorName);
256                eventParams.put("content.comment.old.author.email", oldAuthorEmail);
257                eventParams.put("content.comment.old.author.email.hidden", oldAuthorEmailHidden);
258                eventParams.put("content.comment.old.author.url", oldAuthorURL);
259                eventParams.put("content.comment.old.content", oldContent);
260                
261                _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_MODIFYING, getCurrentUser(), eventParams));
262            }
263        }
264        catch (UnknownAmetysObjectException e)
265        {
266            getLogger().error("Unknown content of id '" + contentId + "'", e);
267            throw new IllegalArgumentException("Unknown content of id '" + contentId + "'", e);
268        }
269        catch (UnknownMetadataException e)
270        {
271            getLogger().error("Unknown comment of id '" + commentId + "' for content '" + contentId + "'", e);
272            throw new IllegalArgumentException("Unknown comment of id '" + commentId + "' for content '" + contentId + "'", e);
273        }
274        
275        return java.util.Collections.EMPTY_MAP;
276    }
277    
278    /**
279     * Removes comments
280     * @param contents the contents with comments to remove
281     * @return the JSON result with deleted comments or not
282     */
283    @SuppressWarnings("unchecked")
284    @Callable
285    public Map<String, Object> deleteComments(Map<String, List<String>> contents)
286    {
287        Map<String, Object> results = new HashMap<> ();
288        
289        results.put("deleted-comments", new ArrayList<Map<String, Object>>());
290        results.put("undeleted-comments", new ArrayList<Map<String, Object>>());
291        results.put("uncommentable-contents", new ArrayList<Map<String, Object>>());
292        results.put("noright-contents", new ArrayList<Map<String, Object>>());
293        results.put("unknown-contents", new ArrayList<Map<String, Object>>());
294        
295        for (String contentId : contents.keySet())
296        {
297            try
298            {
299                Content content = _resolver.resolveById(contentId);
300                
301                if (hasRight("CMS_Rights_CommentModerate", content))
302                {
303                    List<String> deleteCommentIds = new ArrayList<>();
304                    // For each associated comment
305                    for (String commentId : contents.get(contentId))
306                    {
307                        if (!_isParentCommentAlreadyDelete(deleteCommentIds, commentId))
308                        {
309                            Map<String, Object> commentParams = new HashMap<>();
310                            commentParams.put("id", commentId);
311                            commentParams.put("contentId", contentId);
312                            commentParams.put("contentTitle", _contentHelper.getTitle(content));
313                            
314                            if (content instanceof CommentableContent)
315                            {
316                                CommentableContent cContent = (CommentableContent) content;
317            
318                                try
319                                {
320                                    Comment comment = cContent.getComment(commentId);
321                                    
322                                    Map<String, Object> eventParams = new HashMap<>();
323                                    eventParams.put(ObservationConstants.ARGS_CONTENT, content);
324                                    eventParams.put(ObservationConstants.ARGS_COMMENT_ID, comment.getId());
325                                    eventParams.put(ObservationConstants.ARGS_COMMENT_AUTHOR, comment.getAuthorName());
326                                    eventParams.put(ObservationConstants.ARGS_COMMENT_AUTHOR_EMAIL, comment.getAuthorEmail());
327                                    eventParams.put(ObservationConstants.ARGS_COMMENT_VALIDATED, comment.isValidated());
328                                    eventParams.put("comment.content", comment.getContent());
329                                    
330                                    _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_DELETING, getCurrentUser(), eventParams));
331                                    
332                                    comment.remove();
333                                    cContent.saveChanges();
334            
335                                    List<Map<String, Object>> deletedComments = (List<Map<String, Object>>) results.get("deleted-comments");
336                                    deletedComments.add(commentParams);
337                                    deleteCommentIds.add(commentId);
338                                    
339                                    _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_DELETED, getCurrentUser(), eventParams));
340                                }
341                                catch (UnknownMetadataException e)
342                                {
343                                    getLogger().error("Can not remove a non existing comment", e);
344                                    
345                                    List<Map<String, Object>> undeletedComments = (List<Map<String, Object>>) results.get("undeleted-comments");
346                                    undeletedComments.add(commentParams);
347                                }
348                            }
349                            else
350                            {
351                                getLogger().error("Can not remove a comment on a non commentable content");
352                                
353                                List<Map<String, Object>> uncommentableContents = (List<Map<String, Object>>) results.get("uncommentable-contents");
354                                uncommentableContents.add(commentParams);
355                            }
356                        }
357                    }
358                }
359                else
360                {
361                    // No right
362                    getLogger().error("User '" + getCurrentUser() + "' does not have right to moderate comments on content '" + content.getId() + "'");
363                    
364                    Map<String, Object> contentParams = new HashMap<>();
365                    contentParams.put("contentId", contentId);
366                    contentParams.put("contentTitle", _contentHelper.getTitle(content));
367                    
368                    List<Map<String, Object>> norightContents = (List<Map<String, Object>>) results.get("noright-contents");
369                    norightContents.add(contentParams);
370                }
371            }
372            catch (UnknownAmetysObjectException e)
373            {
374                getLogger().error("Can not remove a comment on a non existing content", e);
375                
376                Map<String, Object> contentParams = new HashMap<>();
377                contentParams.put("contentId", contentId);
378                
379                List<Map<String, Object>> unknownContents = (List<Map<String, Object>>) results.get("unknown-contents");
380                unknownContents.add(contentParams);
381            }
382        }
383
384        return results;
385    }
386    
387    /**
388     * True if the a parent comment of the comment id is already deleted
389     * @param deleteCommentIds the delete comment ids
390     * @param commentId the comment id
391     * @return true if the a parent comment of the comment id is already deleted
392     */
393    protected boolean _isParentCommentAlreadyDelete(List<String> deleteCommentIds, String commentId)
394    {
395        for (String deleteCommentId : deleteCommentIds)
396        {
397            if (commentId.startsWith(deleteCommentId))
398            {
399                return true;
400            }
401        }
402        
403        return false;
404    }
405    
406    /**
407     * Validates comments when it is possible.
408     * @param contents the contents with comments to validate
409     * @return the JSON result with validated comments or not
410     */
411    @SuppressWarnings("unchecked")
412    @Callable
413    public Map<String, Object> validateComments(Map<String, List<String>> contents)
414    {
415        Map<String, Object> results = new HashMap<>();
416
417        results.put("validated-comments", new ArrayList<Map<String, Object>>());
418        results.put("error-comments", new ArrayList<Map<String, Object>>());
419        results.put("uncommentable-contents", new ArrayList<Map<String, Object>>());
420        results.put("noright-contents", new ArrayList<Map<String, Object>>());
421        results.put("unknown-contents", new ArrayList<Map<String, Object>>());
422        
423        for (String contentId : contents.keySet())
424        {
425            try
426            {
427                Content content = _resolver.resolveById(contentId);
428                
429                if (hasRight("CMS_Rights_CommentModerate", content))
430                {
431                    // For each associated comment
432                    for (String commentId : contents.get(contentId))
433                    {
434                        Map<String, Object> commentParams = new HashMap<>();
435                        commentParams.put("id", commentId);
436                        commentParams.put("contentId", contentId);
437                        commentParams.put("contentTitle", _contentHelper.getTitle(content));
438                        
439                        if (content instanceof CommentableContent)
440                        {
441                            CommentableContent cContent = (CommentableContent) content;
442        
443                            try
444                            {
445                                Comment comment = cContent.getComment(commentId);
446                                if (!comment.isValidated())
447                                {
448                                    comment.setValidated(true);
449                                    cContent.saveChanges();
450                                    
451                                    List<Map<String, Object>> validatedComments = (List<Map<String, Object>>) results.get("validated-comments");
452                                    validatedComments.add(commentParams);
453                                    
454                                    Map<String, Object> eventParams = new HashMap<>();
455                                    eventParams.put(ObservationConstants.ARGS_CONTENT, content);
456                                    eventParams.put(ObservationConstants.ARGS_COMMENT, comment);
457                                    _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_VALIDATED, getCurrentUser(), eventParams));
458                                }
459                            }
460                            catch (UnknownMetadataException e)
461                            {
462                                getLogger().error("Can not validate a non existing comment", e);
463                                
464                                List<Map<String, Object>> errorComments = (List<Map<String, Object>>) results.get("error-comments");
465                                errorComments.add(commentParams);
466                            }
467                        }
468                        else
469                        {
470                            getLogger().error("Can not validate a comment on a non commentable content");
471                            
472                            List<Map<String, Object>> errorComments = (List<Map<String, Object>>) results.get("error-comments");
473                            errorComments.add(commentParams);
474                        }
475                    }
476                }
477                else
478                {
479                    // No right
480                    getLogger().error("User '" + getCurrentUser() + "' does not have right to validate comments on content '" + content.getId() + "'");
481                    
482                    Map<String, Object> contentParams = new HashMap<>();
483                    contentParams.put("contentId", contentId);
484                    contentParams.put("contentTitle", _contentHelper.getTitle(content));
485                }
486            }
487            catch (UnknownAmetysObjectException e)
488            {
489                getLogger().error("Can not validate a comment on a non existing content", e);
490                
491                Map<String, Object> contentParams = new HashMap<>();
492                contentParams.put("contentId", contentId);
493                
494                List<Map<String, Object>> unknownContents = (List<Map<String, Object>>) results.get("unknown-contents");
495                unknownContents.add(contentParams);
496            }
497        }
498
499        return results;
500    }
501    
502    /**
503     * Invalidates comments when it is possible.
504     * @param contents the contents with comments to invalidate
505     * @return the JSON result with invalidated comments or not
506     */
507    @SuppressWarnings("unchecked")
508    @Callable
509    public Map<String, Object> invalidateComments(Map<String, List<String>> contents)
510    {
511        Map<String, Object> results = new HashMap<>();
512
513        results.put("unvalidated-comments", new ArrayList<Map<String, Object>>());
514        results.put("error-comments", new ArrayList<Map<String, Object>>());
515        results.put("uncommentable-contents", new ArrayList<Map<String, Object>>());
516        results.put("noright-contents", new ArrayList<Map<String, Object>>());
517        results.put("unknown-contents", new ArrayList<Map<String, Object>>());
518        
519        for (String contentId : contents.keySet())
520        {
521            try
522            {
523                Content content = _resolver.resolveById(contentId);
524                
525                if (hasRight("CMS_Rights_CommentModerate", content))
526                {
527                    // For each associated comment
528                    for (String commentId : contents.get(contentId))
529                    {
530                        Map<String, Object> commentParams = new HashMap<>();
531                        commentParams.put("id", commentId);
532                        commentParams.put("contentId", contentId);
533                        commentParams.put("contentTitle", _contentHelper.getTitle(content));
534                        
535                        if (content instanceof CommentableContent)
536                        {
537                            CommentableContent cContent = (CommentableContent) content;
538        
539                            try
540                            {
541                                Comment comment = cContent.getComment(commentId);
542                                if (comment.isValidated())
543                                {
544                                    comment.setValidated(false);
545                                    cContent.saveChanges();
546                                    
547                                    List<Map<String, Object>> validatedComments = (List<Map<String, Object>>) results.get("unvalidated-comments");
548                                    validatedComments.add(commentParams);
549                                    
550                                    Map<String, Object> eventParams = new HashMap<>();
551                                    eventParams.put(ObservationConstants.ARGS_CONTENT, content);
552                                    eventParams.put(ObservationConstants.ARGS_COMMENT, comment);
553                                    _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_UNVALIDATED, getCurrentUser(), eventParams));
554                                }
555                            }
556                            catch (UnknownMetadataException e)
557                            {
558                                getLogger().error("Can not validate a non existing comment", e);
559                                
560                                List<Map<String, Object>> errorComments = (List<Map<String, Object>>) results.get("error-comments");
561                                errorComments.add(commentParams);
562                            }
563                        }
564                        else
565                        {
566                            getLogger().error("Can not validate a comment on a non commentable content");
567                            
568                            List<Map<String, Object>> errorComments = (List<Map<String, Object>>) results.get("error-comments");
569                            errorComments.add(commentParams);
570                        }
571                    }
572                }
573                else
574                {
575                    // No right
576                    getLogger().error("User '" + getCurrentUser() + "' does not have right to validate comments on content '" + content.getId() + "'");
577                    
578                    Map<String, Object> contentParams = new HashMap<>();
579                    contentParams.put("contentId", contentId);
580                    contentParams.put("contentTitle", _contentHelper.getTitle(content));
581                }
582            }
583            catch (UnknownAmetysObjectException e)
584            {
585                getLogger().error("Can not validate a comment on a non existing content", e);
586                
587                Map<String, Object> contentParams = new HashMap<>();
588                contentParams.put("contentId", contentId);
589                
590                List<Map<String, Object>> unknownContents = (List<Map<String, Object>>) results.get("unknown-contents");
591                unknownContents.add(contentParams);
592            }
593        }
594
595        return results;
596    }
597    
598    /**
599     * Ignore reports on comments when it is possible.
600     * @param contents the contents with comments to invalidate
601     * @return the JSON result with comments with ignored reports or not
602     */
603    @SuppressWarnings("unchecked")
604    @Callable
605    public Map<String, Object> ignoreReportsOnComment(Map<String, List<String>> contents)
606    {
607        Map<String, Object> results = new HashMap<>();
608
609        results.put("comments-with-ignored-reports", new ArrayList<Map<String, Object>>());
610        results.put("error-comments", new ArrayList<Map<String, Object>>());
611        results.put("uncommentable-contents", new ArrayList<Map<String, Object>>());
612        results.put("noright-contents", new ArrayList<Map<String, Object>>());
613        results.put("unknown-contents", new ArrayList<Map<String, Object>>());
614        
615        for (String contentId : contents.keySet())
616        {
617            try
618            {
619                Content content = _resolver.resolveById(contentId);
620                
621                if (hasRight("CMS_Rights_CommentModerate", content))
622                {
623                    // For each associated comment
624                    for (String commentId : contents.get(contentId))
625                    {
626                        Map<String, Object> commentParams = new HashMap<>();
627                        commentParams.put("id", commentId);
628                        commentParams.put("contentId", contentId);
629                        commentParams.put("contentTitle", _contentHelper.getTitle(content));
630                        
631                        if (content instanceof CommentableContent)
632                        {
633                            CommentableContent cContent = (CommentableContent) content;
634        
635                            try
636                            {
637                                Comment comment = cContent.getComment(commentId);
638                                
639                                if (comment.getNbReporters() > 0)
640                                {
641                                    comment.resetNbReporters();
642                                    cContent.saveChanges();
643                                    
644                                    List<Map<String, Object>> validatedComments = (List<Map<String, Object>>) results.get("comments-with-ignored-reports");
645                                    validatedComments.add(commentParams);
646                                    
647                                    Map<String, Object> eventParams = new HashMap<>();
648                                    eventParams.put(ObservationConstants.ARGS_CONTENT, content);
649                                    eventParams.put(ObservationConstants.ARGS_COMMENT, comment);
650                                    _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_IGNORE_REPORTS, getCurrentUser(), eventParams));
651                                }
652                            }
653                            catch (UnknownMetadataException e)
654                            {
655                                getLogger().error("Can not ignore reports on a non existing comment", e);
656                                
657                                List<Map<String, Object>> errorComments = (List<Map<String, Object>>) results.get("error-comments");
658                                errorComments.add(commentParams);
659                            }
660                        }
661                        else
662                        {
663                            getLogger().error("Can not ignore reports on a comment on a non commentable content");
664                            
665                            List<Map<String, Object>> errorComments = (List<Map<String, Object>>) results.get("error-comments");
666                            errorComments.add(commentParams);
667                        }
668                    }
669                }
670                else
671                {
672                    // No right
673                    getLogger().error("User '" + getCurrentUser() + "' does not have right to ignore reports on comments on content '" + content.getId() + "'");
674                    
675                    Map<String, Object> contentParams = new HashMap<>();
676                    contentParams.put("contentId", contentId);
677                    contentParams.put("contentTitle", _contentHelper.getTitle(content));
678
679                    List<Map<String, Object>> norightContents = (List<Map<String, Object>>) results.get("noright-contents");
680                    norightContents.add(contentParams);
681                }
682            }
683            catch (UnknownAmetysObjectException e)
684            {
685                getLogger().error("Can not ignore reports on a comment on a non existing content", e);
686                
687                Map<String, Object> contentParams = new HashMap<>();
688                contentParams.put("contentId", contentId);
689                
690                List<Map<String, Object>> unknownContents = (List<Map<String, Object>>) results.get("unknown-contents");
691                unknownContents.add(contentParams);
692            }
693        }
694
695        return results;
696    }
697    
698    /**
699     * Jsonify the comment.
700     * @param comment The comment
701     * @param content The content
702     * @return commentMap the comment map
703     */
704    protected Map<String, Object> _jsonifyComment (Comment comment, Content content) 
705    {
706        Map<String, Object> result = new HashMap<> ();
707        
708        Map<String, Object> commentMap = new HashMap<> ();
709            
710        
711        commentMap.put("validated", Boolean.toString(comment.isValidated()));
712        commentMap.put("id", comment.getId());
713        commentMap.put("creationDate", DateUtils.dateToString(comment.getCreationDate()));
714        
715        String authorName = comment.getAuthorName();
716        if (authorName != null)
717        {
718            commentMap.put("author-name", authorName);
719        }
720        
721        String authorEmail = comment.getAuthorEmail();
722        if (authorEmail != null)
723        {
724            Map<String, Object> authorEmailMap = new HashMap<> ();
725            authorEmailMap.put("hidden", Boolean.toString(comment.isEmailHidden()));
726            authorEmailMap.put("value", authorEmail);
727            commentMap.put("author-email", authorEmailMap);
728        }
729        
730        String authorUrl = comment.getAuthorURL();
731        if (authorUrl != null)
732        {
733            commentMap.put("author-url", authorUrl);
734        }
735        
736        String text = comment.getContent();
737        if (text != null)
738        {
739            commentMap.put("text", comment.getContent());
740        }
741        commentMap.put("content", _jsonifyContent(content));
742        
743        result.put("comment", commentMap);
744        
745        return result;
746    }
747    
748    /**
749     * Jsonify the content.
750     * @param content The content
751     * @return contentMap the content map
752     */
753    protected Map<String, Object> _jsonifyContent (Content content) 
754    {
755        Map<String, Object> contentMap = new HashMap<> ();
756        
757        contentMap.put("id", content.getId());
758        contentMap.put("title", _contentHelper.getTitle(content));
759        contentMap.put("name", content.getName());
760        
761        return contentMap;
762    }
763
764    /**
765     * Get the parameters for a comment
766     * @param content The content
767     * @param comment The comment
768     * @return The parameters
769     */
770    protected Map<String, Object> getCommentParameters (Content content, Comment comment)
771    {
772        Map<String, Object> params = new HashMap<>();
773        
774        params.put("contentId", content.getId());
775        params.put("contentTitle", _contentHelper.getTitle(content));
776        params.put("id", comment.getId());
777        params.put("validated", comment.isValidated());
778        
779        return params;
780    }
781    
782    /**
783     * Get the current user
784     * @return The current user
785     */
786    protected UserIdentity getCurrentUser ()
787    {
788        return _userProvider.getUser();
789    }
790
791    /**
792     * Determines if connected user has right on content
793     * @param rightId The right id
794     * @param content The content
795     * @return true if user has right
796     */
797    protected boolean hasRight (String rightId, Content content)
798    {
799        UserIdentity user = _userProvider.getUser();
800        
801        return _rightManager.hasRight(user, "CMS_Rights_CommentModerate", content) == RightResult.RIGHT_ALLOW;
802    }
803}