2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
30 package mircoders.localizer.basic;
32 import java.util.Arrays;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Vector;
38 import mir.entity.Entity;
39 import mir.session.Request;
40 import mir.session.Response;
41 import mir.session.Session;
42 import mir.session.SessionExc;
43 import mir.session.SessionFailure;
44 import mir.session.UploadedFile;
45 import mir.util.ExceptionFunctions;
46 import mircoders.entity.EntityComment;
47 import mircoders.global.MirGlobal;
48 import mircoders.media.MediaUploadProcessor;
49 import mircoders.module.ModuleComment;
50 import mircoders.storage.DatabaseComment;
51 import mircoders.storage.DatabaseCommentToMedia;
52 import mircoders.storage.DatabaseContent;
56 * <p>Title: Experimental session handler for comment postings </p>
57 * <p>Description: </p>
58 * <p>Copyright: Copyright (c) 2003</p>
60 * @author not attributable
64 public class MirBasicCommentPostingSessionHandler extends MirBasicPostingSessionHandler {
65 protected ModuleComment commentModule;
66 protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
68 public MirBasicCommentPostingSessionHandler() {
71 commentModule= new ModuleComment(DatabaseComment.getInstance());
74 public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
75 if (aSession.getAttribute("initialRequest")==null) {
76 initialRequest(aRequest, aSession, aResponse);
77 aSession.setAttribute("initialRequest", "no");
80 subsequentRequest(aRequest, aSession, aResponse);
84 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
85 super.initializeResponseData(aRequest, aSession, aResponse);
87 Iterator i = DatabaseComment.getInstance().getFields().iterator();
89 String field = (String) i.next();
90 aResponse.setResponseValue(field, aRequest.getParameter(field));
95 public void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure{
96 super.initialRequest(aRequest, aSession, aResponse);
98 String articleId = aRequest.getParameter("to_media");
99 if (articleId == null)
100 throw new SessionExc("MirBasicCommentPostingSessionHandler.initialRequest: article id not set!");
101 aSession.setAttribute("to_media", articleId);
103 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
106 public List validate(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
107 List result = new Vector();
109 testFieldEntered(aRequest, "title", "validationerror.missing", result);
110 testFieldEntered(aRequest, "description", "validationerror.missing", result);
111 testFieldEntered(aRequest, "creator", "validationerror.missing", result);
116 public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
118 initializeResponseData(aRequest, aSession, aResponse);
120 List validationErrors = validate(aRequest, aSession);
122 if (validationErrors != null && validationErrors.size()>0) {
123 returnValidationErrors(aRequest, aSession, aResponse, validationErrors);
126 EntityComment comment = (EntityComment) commentModule.createNew ();
127 Iterator i = DatabaseComment.getInstance().getFields().iterator();
128 // comment.setValues(commentFields);
130 finishComment(aRequest, aSession, comment);
132 String id = comment.insert();
134 afterDuplicateCommentPosting(aRequest, aSession, aResponse, comment);
135 logger.info("Dupe comment rejected");
136 aSession.terminate();
139 i = aRequest.getUploadedFiles().iterator();
140 while (i.hasNext()) {
141 UploadedFile file = (UploadedFile) i.next();
142 processMediaFile(aRequest, aSession, comment, file);
145 afterCommentPosting(aRequest, aSession, aResponse, comment);
146 MirGlobal.abuse().checkComment(comment, aRequest, null);
147 MirGlobal.localizer().openPostings().afterCommentPosting(comment);
148 logger.info("Comment posted");
149 aSession.terminate();
153 catch (Throwable t) {
154 ExceptionFunctions.traceCauseException(t).printStackTrace();
156 throw new SessionFailure("MirBasicCommentPostingSessionHandler.subsequentRequest: " + t.getMessage(), t);
160 public void initializeCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionFailure, SessionExc {
161 String articleId = aRequest.getParameter("to_media");
163 articleId = aRequest.getParameter("aid");
166 throw new SessionExc("initializeCommentPosting: article id not set!");
168 aSession.setAttribute("to_media", articleId);
169 processCommentPosting(aRequest, aSession, aResponse);
172 public void returnValidationErrors(Request aRequest, Session aSession, Response aResponse, List aValidationErrors) throws SessionFailure, SessionExc {
173 aResponse.setResponseValue("errors", aValidationErrors);
174 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
177 public void processCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
178 if (MirGlobal.abuse().getOpenPostingPassword()) {
179 String password = generateOnetimePassword();
180 aSession.setAttribute("password", password);
181 aResponse.setResponseValue("password", password);
182 aResponse.setResponseValue("passwd", password);
185 aResponse.setResponseValue("password", null);
188 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
191 public void processMediaFile(Request aRequest, Session aSession, EntityComment aComment, UploadedFile aFile) throws SessionExc, SessionFailure {
193 Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, new HashMap());
194 finishMedia(aRequest, aSession, aFile, mediaItem);
196 commentToMedia.addMedia(aComment.getId(), mediaItem.getId());
198 catch (Throwable t) {
199 throw new SessionFailure(t);
203 public void finishMedia(Request aRequest, Session aSession, UploadedFile aFile, Entity aMedia) throws SessionExc, SessionFailure {
206 public void finishComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
207 if (aSession.getAttribute("to_media") == null)
208 throw new SessionExc("missing to_media");
210 aComment.setValueForProperty("is_published", "1");
211 aComment.setValueForProperty("to_comment_status", "1");
212 aComment.setValueForProperty("is_html","0");
213 aComment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media"));
216 public void addMedia(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
219 public void afterCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) {
220 DatabaseContent.getInstance().setUnproduced("id=" + aComment.getValue("to_media"));
221 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
224 public void afterDuplicateCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) {
225 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate"));
228 public class ValidationError {
229 private String field;
230 private String message;
231 private List parameters;
233 public ValidationError(String aField, String aMessage) {
234 this (aField, aMessage, new String[] {});
237 public ValidationError(String aField, String aMessage, Object aParameter) {
238 this (aField, aMessage, new Object[] {aParameter});
241 public ValidationError(String aField, String aMessage, Object[] aParameters) {
244 parameters = Arrays.asList(aParameters);
247 public String getMessage() {
251 public String getField() {
255 public List getParameters() {