2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mircoders.localizer.basic;
\r
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Vector;
40 import mir.entity.Entity;
41 import mir.session.Request;
42 import mir.session.Response;
43 import mir.session.Session;
44 import mir.session.SessionExc;
45 import mir.session.SessionFailure;
46 import mir.session.UploadedFile;
47 import mir.util.ExceptionFunctions;
48 import mircoders.entity.EntityComment;
49 import mircoders.global.MirGlobal;
50 import mircoders.media.MediaUploadProcessor;
51 import mircoders.module.ModuleComment;
52 import mircoders.storage.DatabaseComment;
53 import mircoders.storage.DatabaseCommentToMedia;
54 import mircoders.storage.DatabaseContent;
\r
58 * <p>Title: Experimental session handler for comment postings </p>
\r
59 * <p>Description: </p>
\r
60 * <p>Copyright: Copyright (c) 2003</p>
\r
62 * @author not attributable
\r
66 public class MirBasicCommentPostingSessionHandler extends MirBasicPostingSessionHandler {
\r
67 protected ModuleComment commentModule;
\r
68 protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
\r
70 public MirBasicCommentPostingSessionHandler() {
\r
73 commentModule= new ModuleComment(DatabaseComment.getInstance());
\r
76 public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
77 if (aSession.getAttribute("initialRequest")==null) {
\r
78 initialRequest(aRequest, aSession, aResponse);
\r
79 aSession.setAttribute("initialRequest", "no");
\r
82 subsequentRequest(aRequest, aSession, aResponse);
\r
86 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
87 super.initializeResponseData(aRequest, aSession, aResponse);
\r
89 Iterator i = DatabaseComment.getInstance().getFields().iterator();
\r
90 while (i.hasNext()) {
\r
91 String field = (String) i.next();
\r
92 aResponse.setResponseValue(field, aRequest.getParameter(field));
\r
97 public void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure{
\r
98 super.initialRequest(aRequest, aSession, aResponse);
\r
100 String articleId = aRequest.getParameter("to_media");
\r
101 if (articleId == null)
\r
102 throw new SessionExc("MirBasicCommentPostingSessionHandler.initialRequest: article id not set!");
\r
103 aSession.setAttribute("to_media", articleId);
\r
105 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
\r
108 public List validate(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
109 List result = new Vector();
\r
111 testFieldEntered(aRequest, "title", "validationerror.missing", result);
\r
112 testFieldEntered(aRequest, "description", "validationerror.missing", result);
\r
113 testFieldEntered(aRequest, "creator", "validationerror.missing", result);
\r
118 public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
120 initializeResponseData(aRequest, aSession, aResponse);
\r
122 List validationErrors = validate(aRequest, aSession);
\r
124 if (validationErrors != null && validationErrors.size()>0) {
\r
125 returnValidationErrors(aRequest, aSession, aResponse, validationErrors);
\r
128 EntityComment comment = (EntityComment) commentModule.createNew ();
\r
129 Iterator i = DatabaseComment.getInstance().getFields().iterator();
\r
130 // comment.setValues(commentFields);
\r
132 finishComment(aRequest, aSession, comment);
\r
134 String id = comment.insert();
\r
136 afterDuplicateCommentPosting(aRequest, aSession, aResponse, comment);
\r
137 logger.info("Dupe comment rejected");
\r
138 aSession.terminate();
\r
141 i = aRequest.getUploadedFiles().iterator();
\r
142 while (i.hasNext()) {
\r
143 UploadedFile file = (UploadedFile) i.next();
\r
144 processMediaFile(aRequest, aSession, comment, file);
\r
147 afterCommentPosting(aRequest, aSession, aResponse, comment);
\r
148 MirGlobal.abuse().checkComment(comment, aRequest, null);
\r
149 MirGlobal.localizer().openPostings().afterCommentPosting(comment);
\r
150 logger.info("Comment posted");
\r
151 aSession.terminate();
\r
155 catch (Throwable t) {
\r
156 ExceptionFunctions.traceCauseException(t).printStackTrace();
\r
158 throw new SessionFailure("MirBasicCommentPostingSessionHandler.subsequentRequest: " + t.getMessage(), t);
\r
162 public void initializeCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionFailure, SessionExc {
\r
163 String articleId = aRequest.getParameter("to_media");
\r
164 if (articleId==null)
\r
165 articleId = aRequest.getParameter("aid");
\r
167 if (articleId==null)
\r
168 throw new SessionExc("initializeCommentPosting: article id not set!");
\r
170 aSession.setAttribute("to_media", articleId);
\r
171 processCommentPosting(aRequest, aSession, aResponse);
\r
174 public void returnValidationErrors(Request aRequest, Session aSession, Response aResponse, List aValidationErrors) throws SessionFailure, SessionExc {
\r
175 aResponse.setResponseValue("errors", aValidationErrors);
\r
176 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
\r
179 public void processCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
180 if (MirGlobal.abuse().getOpenPostingPassword()) {
\r
181 String password = generateOnetimePassword();
\r
182 aSession.setAttribute("password", password);
\r
183 aResponse.setResponseValue("password", password);
\r
184 aResponse.setResponseValue("passwd", password);
\r
187 aResponse.setResponseValue("password", null);
\r
190 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
\r
193 public void processMediaFile(Request aRequest, Session aSession, EntityComment aComment, UploadedFile aFile) throws SessionExc, SessionFailure {
\r
195 Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, new HashMap());
\r
196 finishMedia(aRequest, aSession, aFile, mediaItem);
\r
197 mediaItem.update();
\r
198 commentToMedia.addMedia(aComment.getId(), mediaItem.getId());
\r
200 catch (Throwable t) {
\r
201 throw new SessionFailure(t);
\r
205 public void finishMedia(Request aRequest, Session aSession, UploadedFile aFile, Entity aMedia) throws SessionExc, SessionFailure {
\r
208 public void finishComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
\r
209 if (aSession.getAttribute("to_media") == null)
\r
210 throw new SessionExc("missing to_media");
\r
212 aComment.setValueForProperty("is_published", "1");
\r
213 aComment.setValueForProperty("to_comment_status", "1");
\r
214 aComment.setValueForProperty("is_html","0");
\r
215 aComment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media"));
\r
218 public void addMedia(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
\r
221 public void afterCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) {
\r
222 DatabaseContent.getInstance().setUnproduced("id=" + aComment.getValue("to_media"));
\r
223 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
\r
226 public void afterDuplicateCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) {
\r
227 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate"));
\r
230 public class ValidationError {
\r
231 private String field;
\r
232 private String message;
\r
233 private List parameters;
\r
235 public ValidationError(String aField, String aMessage) {
\r
236 this (aField, aMessage, new String[] {});
\r
239 public ValidationError(String aField, String aMessage, Object aParameter) {
\r
240 this (aField, aMessage, new Object[] {aParameter});
\r
243 public ValidationError(String aField, String aMessage, Object[] aParameters) {
\r
245 message = aMessage;
\r
246 parameters = Arrays.asList(aParameters);
\r
249 public String getMessage() {
\r
253 public String getField() {
\r
257 public List getParameters() {
\r