001/* 002 * Copyright 2010 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.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.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Redirector; 028import org.apache.cocoon.environment.Request; 029import org.apache.cocoon.environment.SourceResolver; 030import org.apache.commons.lang.StringUtils; 031 032import org.ametys.cms.ObservationConstants; 033import org.ametys.cms.repository.comment.Comment; 034import org.ametys.cms.repository.comment.CommentManagerExtensionPoint; 035import org.ametys.cms.repository.comment.CommentableContent; 036import org.ametys.core.cocoon.ActionResultGenerator; 037import org.ametys.core.observation.Event; 038import org.ametys.runtime.i18n.I18nizableText; 039 040/** 041 * Sends the comment for a given post 042 */ 043public class AddCommentAction extends AbstractCommentAction 044{ 045 /** The request parameter name for author name */ 046 public static final String PARAMETER_AUTHOR_NAME = "name"; 047 /** The request parameter name for author email */ 048 public static final String PARAMETER_AUTHOR_EMAIL = "email"; 049 /** The request parameter name for author email to hide */ 050 public static final String PARAMETER_AUTHOR_HIDEEMAIL = "hide-email"; 051 /** The request parameter name for author url */ 052 public static final String PARAMETER_AUTHOR_URL = "url"; 053 /** The request parameter name for content */ 054 public static final String PARAMETER_CONTENTTEXT = "text"; 055 /** The request parameter name for captcha */ 056 public static final String PARAMETER_CAPTCHA_KEY = "captcha-key"; 057 /** The request parameter name for captcha */ 058 public static final String PARAMETER_CAPTCHA_VALUE = "captcha-value"; 059 060 /** The listeners for this action */ 061 protected CommentManagerExtensionPoint _commentManager; 062 063 @Override 064 public void service(ServiceManager smanager) throws ServiceException 065 { 066 super.service(smanager); 067 _commentManager = (CommentManagerExtensionPoint) smanager.lookup(CommentManagerExtensionPoint.ROLE); 068 } 069 070 @Override 071 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 072 { 073 Request request = ObjectModelHelper.getRequest(objectModel); 074 075 Map<String, Object> results = new HashMap<>(); 076 077 String contentId = request.getParameter(PARAMETER_CONTENTID); 078 if (StringUtils.isBlank(contentId)) 079 { 080 throw new IllegalArgumentException("Missing or empty 'content-id' parameter to add a comment"); 081 } 082 083 CommentableContent content = getContent(request, contentId); 084 085 boolean isValidated = _commentManager.isValidatedByDefault(content); 086 087 List<I18nizableText> errors = _commentManager.getErrors(content, objectModel); 088 089 if (errors.isEmpty()) 090 { 091 String commentId = request.getParameter(PARAMETER_COMMENTID); 092 // We have a comment id, so we add a sub comment to this comment 093 if (StringUtils.isNotBlank(commentId)) 094 { 095 Comment comment = content.getComment(commentId); 096 Comment subComment = comment.createSubComment(); 097 _setCommentAttributes(request, results, content, isValidated, subComment); 098 results.put("redirect", request.getHeader("Referer") + (isValidated ? "?last-comment=highlight#" + subComment.getId() + "-" + content.getId() : "")); 099 } 100 else // No comment id, create comment from content 101 { 102 Comment comment = content.createComment(); 103 _setCommentAttributes(request, results, content, isValidated, comment); 104 results.put("redirect", request.getHeader("Referer") + (isValidated ? "?last-comment=highlight#" + comment.getId() + "-" + content.getId() : "")); 105 } 106 107 } 108 else 109 { 110 results.put("errors", errors); 111 } 112 113 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, results); 114 return EMPTY_MAP; 115 } 116 117 /** 118 * Set comment attributes 119 * @param request the request 120 * @param results the results 121 * @param content the content 122 * @param isValidated true if is validated 123 * @param comment the comment 124 */ 125 protected void _setCommentAttributes(Request request, Map<String, Object> results, CommentableContent content, boolean isValidated, Comment comment) 126 { 127 comment.setAuthorName(request.getParameter(PARAMETER_AUTHOR_NAME)); 128 comment.setAuthorEmail(request.getParameter(PARAMETER_AUTHOR_EMAIL)); 129 comment.setEmailHiddenStatus(request.getParameter(PARAMETER_AUTHOR_HIDEEMAIL) != null); 130 comment.setAuthorURL(request.getParameter(PARAMETER_AUTHOR_URL)); 131 comment.setContent(request.getParameter(PARAMETER_CONTENTTEXT).replaceAll("\r", "")); 132 comment.setValidated(isValidated); 133 134 content.saveChanges(); 135 136 if (isValidated) 137 { 138 Map<String, Object> eventParams = new HashMap<>(); 139 eventParams.put(ObservationConstants.ARGS_CONTENT, content); 140 eventParams.put(ObservationConstants.ARGS_COMMENT, comment); 141 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_VALIDATED, getCurrentUser(), eventParams)); 142 } 143 else 144 { 145 Map<String, Object> eventParams = new HashMap<>(); 146 eventParams.put(ObservationConstants.ARGS_CONTENT, content); 147 eventParams.put(ObservationConstants.ARGS_COMMENT, comment); 148 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_ADDED, getCurrentUser(), eventParams)); 149 } 150 151 results.put("published", comment.isValidated()); 152 results.put("contentId", content.getId()); 153 results.put("commentId", comment.getId()); 154 } 155}