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.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
37 import mir.entity.Entity;
38 import mir.session.Request;
39 import mir.session.Response;
40 import mir.session.Session;
41 import mir.session.SessionExc;
42 import mir.session.SessionFailure;
43 import mir.session.ValidationHelper;
44 import mir.util.EntityUtility;
45 import mircoders.entity.EntityComment;
46 import mircoders.global.MirGlobal;
47 import mircoders.media.MediaUploadProcessor;
48 import mircoders.module.ModuleComment;
49 import mircoders.module.ModuleCommentStatus;
50 import mircoders.module.ModuleMediafolder;
51 import mircoders.storage.DatabaseComment;
52 import mircoders.storage.DatabaseCommentToMedia;
53 import mircoders.storage.DatabaseContent;
56 * Session handler for comment postings
61 public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler {
62 protected DatabaseComment commentDatabase = DatabaseComment.getInstance();
63 protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
66 public MirBasicCommentPostingHandler() {
71 public MirBasicCommentPostingHandler(boolean aPersistentUploadedFiles) {
72 super(aPersistentUploadedFiles);
74 setResponseGenerators(
75 configuration.getString("Localizer.OpenSession.comment.EditTemplate"),
76 configuration.getString("Localizer.OpenSession.comment.DupeTemplate"),
77 configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate"),
78 configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
81 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
82 super.initializeResponseData(aRequest, aSession, aResponse);
84 Iterator i = DatabaseComment.getInstance().getFieldNames().iterator();
86 String field = (String) i.next();
87 aResponse.setResponseValue(field, aRequest.getParameter(field));
91 public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
92 super.validate(aResults, aRequest, aSession);
94 ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
95 ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
96 ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
99 protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
100 super.initializeSession(aRequest, aSession);
102 String articleId = aRequest.getParameter("to_media");
104 throw new SessionExc("initializeSession: article id not set!");
106 aSession.setAttribute("to_media", articleId);
109 public void finalizeComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
111 aComment.setFieldValue("is_published", "1");
112 ModuleCommentStatus module = new ModuleCommentStatus();
113 aComment.setFieldValue("to_comment_status", module.commentStatusIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultCommentStatus")));
114 aComment.setFieldValue("is_html", "0");
115 aComment.setFieldValue("to_media", (String) aSession.getAttribute("to_media"));
117 catch (Throwable t) {
118 throw new SessionFailure(t);
122 public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
125 Map values = getIntersectingValues(aRequest, DatabaseComment.getInstance());
127 EntityComment comment = (EntityComment) commentDatabase.createNewEntity();
128 comment.setFieldValues(values);
129 finalizeComment(aRequest, aSession, comment);
130 id = comment.insert();
132 logger.info("Duplicate comment rejected");
133 throw new DuplicateCommentExc("Duplicate comment rejected");
135 aSession.setAttribute("comment", comment);
137 catch (Throwable t) {
138 throw new SessionFailure(t);
142 public void processAttachment(Request aRequest, Session aSession, Attachment aFile) throws SessionExc, SessionFailure {
144 Map values = new HashMap();
145 values.put("creator", aRequest.getParameter("creator"));
146 values.putAll(aFile.getAllAttributes());
147 values.put("to_publisher", "0");
148 values.put("is_published", "1");
149 ModuleMediafolder module = new ModuleMediafolder();
150 values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultMediaFolder")));
152 Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
154 commentToMedia.addMedia(((EntityComment) aSession.getAttribute("comment")).getId(), mediaItem.getId());
156 catch (Throwable t) {
157 throw new SessionFailure(t);
161 public void processAttachmentError(Request aRequest, Session aSession, Attachment aFile, Throwable anError) {
162 EntityUtility.appendLineToField( ((EntityComment) aSession.getAttribute("comment")), "comment",
163 "error with attachment: " + anError.toString());
165 ((EntityComment) aSession.getAttribute("comment")).update();
168 public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
169 EntityComment comment = (EntityComment) aSession.getAttribute("comment");
171 MirGlobal.abuse().checkComment(comment, aRequest, null);
173 MirGlobal.localizer().openPostings().afterCommentPosting(comment);
175 catch (Throwable t) {
176 throw new SessionFailure(t);
178 DatabaseContent.getInstance().setUnproduced("id=" + comment.getFieldValue("to_media"));
179 logger.info("Comment posted");
182 protected static class DuplicateCommentExc extends SessionExc {
183 public DuplicateCommentExc(String aMessage) {