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 any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
30 package mircoders.localizer.basic;
\r
32 import java.util.HashMap;
\r
33 import java.util.Iterator;
\r
34 import java.util.List;
\r
35 import java.util.Map;
\r
37 import mir.entity.Entity;
\r
38 import mir.session.Request;
\r
39 import mir.session.Response;
\r
40 import mir.session.Session;
\r
41 import mir.session.SessionExc;
\r
42 import mir.session.SessionFailure;
\r
43 import mir.session.UploadedFile;
\r
44 import mir.session.ValidationHelper;
\r
45 import mircoders.entity.EntityComment;
\r
46 import mircoders.global.MirGlobal;
\r
47 import mircoders.media.MediaUploadProcessor;
\r
48 import mircoders.module.ModuleComment;
\r
49 import mircoders.module.ModuleCommentStatus;
\r
50 import mircoders.module.ModuleMediafolder;
\r
51 import mircoders.storage.DatabaseComment;
\r
52 import mircoders.storage.DatabaseCommentStatus;
\r
53 import mircoders.storage.DatabaseCommentToMedia;
\r
54 import mircoders.storage.DatabaseContent;
\r
55 import mircoders.storage.DatabaseMediafolder;
\r
59 * <p>Title: Experimental session handler for comment postings </p>
\r
60 * <p>Description: </p>
\r
61 * <p>Copyright: Copyright (c) 2003</p>
\r
67 public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler {
\r
68 protected ModuleComment commentModule = new ModuleComment(DatabaseComment.getInstance());
\r
69 protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
\r
72 public MirBasicCommentPostingHandler() {
\r
75 setResponseGenerators(
\r
76 configuration.getString("Localizer.OpenSession.comment.EditTemplate"),
\r
77 configuration.getString("Localizer.OpenSession.comment.DupeTemplate"),
\r
78 configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate"),
\r
79 configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
\r
82 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
83 super.initializeResponseData(aRequest, aSession, aResponse);
\r
85 Iterator i = DatabaseComment.getInstance().getFields().iterator();
\r
86 while (i.hasNext()) {
\r
87 String field = (String) i.next();
\r
88 aResponse.setResponseValue(field, aRequest.getParameter(field));
\r
92 public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
93 super.validate(aResults, aRequest, aSession);
\r
95 ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
\r
96 ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
\r
97 ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
\r
100 protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
101 super.initializeSession(aRequest, aSession);
\r
103 String articleId = aRequest.getParameter("to_media");
\r
104 if (articleId==null)
\r
105 throw new SessionExc("initializeSession: article id not set!");
\r
107 aSession.setAttribute("to_media", articleId);
\r
110 public void finalizeComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
\r
112 aComment.setValueForProperty("is_published", "1");
\r
113 ModuleCommentStatus module = new ModuleCommentStatus(DatabaseCommentStatus.getInstance());
\r
114 aComment.setValueForProperty("to_comment_status", module.commentStatusIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultCommentStatus")));
\r
115 aComment.setValueForProperty("is_html", "0");
\r
116 aComment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media"));
\r
118 catch (Throwable t) {
\r
119 throw new SessionFailure(t);
\r
123 public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
126 Map values = getIntersectingValues(aRequest, DatabaseComment.getInstance());
\r
128 EntityComment comment = (EntityComment) commentModule.createNew();
\r
129 comment.setValues(values);
\r
130 finalizeComment(aRequest, aSession, comment);
\r
131 id = comment.insert();
\r
133 logger.info("Duplicate comment rejected");
\r
134 throw new DuplicateCommentExc("Duplicate comment rejected");
\r
136 aSession.setAttribute("comment", comment);
\r
138 catch (Throwable t) {
\r
139 throw new SessionFailure(t);
\r
143 public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
\r
145 Map values = new HashMap();
\r
146 values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));
\r
147 values.put("creator", aRequest.getParameter("creator"));
\r
148 values.put("to_publisher", "0");
\r
149 values.put("is_published", "1");
\r
150 ModuleMediafolder module = new ModuleMediafolder(DatabaseMediafolder.getInstance());
\r
151 values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultMediaFolder")));
\r
153 Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
\r
154 mediaItem.update();
\r
155 commentToMedia.addMedia(((EntityComment) aSession.getAttribute("comment")).getId(), mediaItem.getId());
\r
157 catch (Throwable t) {
\r
158 throw new SessionFailure(t);
\r
162 public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
163 EntityComment comment = (EntityComment) aSession.getAttribute("comment");
\r
165 MirGlobal.abuse().checkComment(comment, aRequest, null);
\r
167 MirGlobal.localizer().openPostings().afterCommentPosting(comment);
\r
169 catch (Throwable t) {
\r
170 throw new SessionFailure(t);
\r
172 DatabaseContent.getInstance().setUnproduced("id=" + comment.getValue("to_media"));
\r
173 logger.info("Comment posted");
\r
176 protected static class DuplicateCommentExc extends SessionExc {
\r
177 public DuplicateCommentExc(String aMessage) {
\r