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 */ 016 017package org.ametys.cms.repository.comment.actions; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.environment.SourceResolver; 029import org.apache.commons.lang.StringUtils; 030 031import org.ametys.cms.ObservationConstants; 032import org.ametys.cms.repository.comment.Comment; 033import org.ametys.cms.repository.comment.CommentManagerExtensionPoint; 034import org.ametys.cms.repository.comment.CommentableContent; 035import org.ametys.core.cocoon.ActionResultGenerator; 036import org.ametys.core.observation.Event; 037 038/** 039 * Report a comment 040 */ 041public class ReportCommentAction extends AbstractCommentAction 042{ 043 /** The listeners for this action */ 044 protected CommentManagerExtensionPoint _commentManager; 045 046 @Override 047 public void service(ServiceManager smanager) throws ServiceException 048 { 049 super.service(smanager); 050 _commentManager = (CommentManagerExtensionPoint) smanager.lookup(CommentManagerExtensionPoint.ROLE); 051 } 052 053 @Override 054 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 055 { 056 Request request = ObjectModelHelper.getRequest(objectModel); 057 058 Map<String, Object> results = new HashMap<>(); 059 060 String contentId = request.getParameter(PARAMETER_CONTENTID); 061 if (StringUtils.isBlank(contentId)) 062 { 063 throw new IllegalArgumentException("Missing or empty 'content-id' parameter to like a comment"); 064 } 065 066 String commentId = request.getParameter(PARAMETER_COMMENTID); 067 if (StringUtils.isBlank(commentId)) 068 { 069 throw new IllegalArgumentException("Missing or empty 'comment-id' parameter to like a comment"); 070 } 071 072 CommentableContent content = getContent(request, contentId); 073 Comment comment = content.getComment(commentId); 074 075 comment.addReport(); 076 content.saveChanges(); 077 078 Map<String, Object> eventParams = new HashMap<>(); 079 eventParams.put(ObservationConstants.ARGS_CONTENT, content); 080 eventParams.put(ObservationConstants.ARGS_COMMENT, comment); 081 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_REPORTED, getCurrentUser(), eventParams)); 082 083 results.put("redirect", request.getHeader("Referer") + "?last-comment=highlight#" + comment.getId()); 084 results.put("reported", true); 085 results.put("contentId", content.getId()); 086 results.put("commentId", comment.getId()); 087 088 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, results); 089 return EMPTY_MAP; 090 } 091}