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