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.Collections; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Redirector; 026import org.apache.cocoon.environment.Request; 027import org.apache.cocoon.environment.SourceResolver; 028import org.apache.commons.lang.StringUtils; 029 030import org.ametys.cms.repository.comment.CommentsDAO; 031import org.ametys.core.cocoon.ActionResultGenerator; 032 033/** 034 * Sends the comment for a given post 035 */ 036public class AddCommentAction extends AbstractCommentAction 037{ 038 /** The request parameter name for author name */ 039 public static final String PARAMETER_AUTHOR_NAME = "name"; 040 /** The request parameter name for author email */ 041 public static final String PARAMETER_AUTHOR_EMAIL = "email"; 042 /** The request parameter name for author email to hide */ 043 public static final String PARAMETER_AUTHOR_HIDEEMAIL = "hide-email"; 044 /** The request parameter name for author url */ 045 public static final String PARAMETER_AUTHOR_URL = "url"; 046 /** The request parameter name for content */ 047 public static final String PARAMETER_CONTENTTEXT = "text"; 048 /** The request parameter name for captcha */ 049 public static final String PARAMETER_CAPTCHA_KEY = "captcha-key"; 050 /** The request parameter name for captcha */ 051 public static final String PARAMETER_CAPTCHA_VALUE = "captcha-value"; 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 String contentId = request.getParameter(PARAMETER_CONTENTID); 059 if (StringUtils.isBlank(contentId)) 060 { 061 throw new IllegalArgumentException("Missing or empty 'content-id' parameter to add a comment"); 062 } 063 064 String commentId = request.getParameter(PARAMETER_COMMENTID); 065 066 Map<String, Object> formValues = new HashMap<>(); 067 formValues.put(CommentsDAO.FORM_AUTHOR_NAME, request.getParameter(PARAMETER_AUTHOR_NAME)); 068 formValues.put(CommentsDAO.FORM_AUTHOR_EMAIL, request.getParameter(PARAMETER_AUTHOR_EMAIL)); 069 formValues.put(CommentsDAO.FORM_AUTHOR_URL, request.getParameter(PARAMETER_AUTHOR_URL)); 070 formValues.put(CommentsDAO.FORM_AUTHOR_HIDEEMAIL, request.getParameter(PARAMETER_AUTHOR_HIDEEMAIL) != null); 071 formValues.put(CommentsDAO.FORM_CONTENTTEXT, request.getParameter(PARAMETER_CONTENTTEXT)); 072 formValues.put(CommentsDAO.FORM_CAPTCHA_KEY, request.getParameter(PARAMETER_CAPTCHA_KEY)); 073 formValues.put(CommentsDAO.FORM_CAPTCHA_VALUE, request.getParameter(PARAMETER_CAPTCHA_VALUE)); 074 075 Map<String, Object> results = _commentsDAO.addComment(contentId, commentId, formValues, Collections.EMPTY_MAP); 076 077 if (!results.containsKey("errors")) 078 { 079 boolean published = (boolean) results.get("published"); 080 results.put("redirect", request.getHeader("Referer") + (published ? "?last-comment=highlight#" + (String) results.get("commentId") + "-" + contentId : "")); 081 } 082 083 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, results); 084 return EMPTY_MAP; 085 } 086}