user agent filters now use sub strings instead of regular expressions
[mir.git] / source / mircoders / localizer / basic / MirBasicCommentPostingHandler.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
29  */
30 package mircoders.localizer.basic;
31
32 import mir.entity.Entity;
33 import mir.session.Request;
34 import mir.session.Response;
35 import mir.session.Session;
36 import mir.session.SessionExc;
37 import mir.session.SessionFailure;
38 import mir.session.ValidationHelper;
39 import mir.util.EntityUtility;
40 import mircoders.entity.EntityComment;
41 import mircoders.global.MirGlobal;
42 import mircoders.media.MediaUploadProcessor;
43 import mircoders.module.ModuleCommentStatus;
44 import mircoders.module.ModuleMediafolder;
45 import mircoders.storage.DatabaseComment;
46 import mircoders.storage.DatabaseCommentToMedia;
47 import mircoders.storage.DatabaseContent;
48
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.List;
52 import java.util.Map;
53
54 /**
55  * Session handler for comment postings
56  * 
57  * @author Zapata
58  */
59
60 public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler {
61   protected DatabaseComment commentDatabase = DatabaseComment.getInstance();
62   protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
63
64
65   public MirBasicCommentPostingHandler() {
66     this(false);
67   }
68
69
70   public MirBasicCommentPostingHandler(boolean aPersistentUploadedFiles) {
71     super(aPersistentUploadedFiles);
72
73     setResponseGenerators(
74       configuration.getString("Localizer.OpenSession.comment.EditTemplate"),
75       configuration.getString("Localizer.OpenSession.comment.DupeTemplate"),
76       configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate"),
77       configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
78   }
79
80   protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
81     super.initializeResponseData(aRequest, aSession, aResponse);
82
83     Iterator i = DatabaseComment.getInstance().getFieldNames().iterator();
84     while (i.hasNext()) {
85       String field = (String) i.next();
86       aResponse.setResponseValue(field, aRequest.getParameter(field));
87     }
88   }
89
90   public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
91     super.validate(aResults, aRequest, aSession);
92
93     ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
94     ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
95     ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
96   }
97
98   protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
99     super.initializeSession(aRequest, aSession);
100
101     String articleId = aRequest.getParameter("to_media");
102     if (articleId==null)
103       throw new SessionExc("initializeSession: article id not set!");
104
105     aSession.setAttribute("to_media", articleId);
106   }
107
108   public void finalizeComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
109     try {
110       aComment.setFieldValue("is_published", "1");
111             ModuleCommentStatus module = new ModuleCommentStatus();
112       aComment.setFieldValue("to_comment_status", module.commentStatusIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultCommentStatus")));
113       aComment.setFieldValue("is_html", "0");
114       aComment.setFieldValue("to_media", (String) aSession.getAttribute("to_media"));
115     }
116     catch (Throwable t) {
117       throw new SessionFailure(t);
118     }
119   }
120
121   public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
122     try {
123       String id;
124       Map values = getIntersectingValues(aRequest, DatabaseComment.getInstance());
125
126       EntityComment comment = (EntityComment) commentDatabase.createNewEntity();
127       comment.setFieldValues(values);
128       finalizeComment(aRequest, aSession, comment);
129       id = comment.insert();
130       if (id == null) {
131         logger.info("Duplicate comment rejected");
132         throw new DuplicateCommentExc("Duplicate comment rejected");
133       }
134       aSession.setAttribute("comment", comment);
135     }
136     catch (Throwable t) {
137       throw new SessionFailure(t);
138     }
139   }
140
141   public void processAttachment(Request aRequest, Session aSession, Attachment aFile) throws SessionExc, SessionFailure {
142     try {
143       Map values = new HashMap();
144       values.put("creator", aRequest.getParameter("creator"));
145       values.putAll(aFile.getAllAttributes());
146       values.put("to_publisher", "0");
147       values.put("is_published", "1");
148       ModuleMediafolder module = new ModuleMediafolder();
149       values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultMediaFolder")));
150
151       Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
152       mediaItem.update();
153       commentToMedia.addMedia(((EntityComment) aSession.getAttribute("comment")).getId(), mediaItem.getId());
154     }
155     catch (Throwable t) {
156       throw new SessionFailure(t);
157     }
158   }
159
160   public void processAttachmentError(Request aRequest, Session aSession, Attachment aFile, Throwable anError) {
161     EntityUtility.appendLineToField( ((EntityComment) aSession.getAttribute("comment")), "comment",
162         "error with attachment: " + anError.toString());
163
164     ((EntityComment) aSession.getAttribute("comment")).update();
165   }
166
167   public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
168     EntityComment comment = (EntityComment) aSession.getAttribute("comment");
169
170     MirGlobal.abuse().checkComment(comment, aRequest, null);
171     try {
172       MirGlobal.localizer().openPostings().afterCommentPosting(comment);
173     }
174     catch (Throwable t) {
175       throw new SessionFailure(t);
176     }
177     DatabaseContent.getInstance().setUnproduced("id=" + comment.getFieldValue("to_media"));
178     logger.info("Comment posted");
179   }
180
181   protected static class DuplicateCommentExc extends SessionExc {
182     public DuplicateCommentExc(String aMessage) {
183       super(aMessage);
184     }
185   }
186 }