mh said it's ok. just a little cleanup...
[mir.git] / source / mircoders / localizer / basic / MirBasicCommentPostingSessionHandler.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 java.util.Arrays;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Vector;
37
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;
53
54 /**
55  *
56  * <p>Title: Experimental session handler for comment postings </p>
57  * <p>Description: </p>
58  * <p>Copyright: Copyright (c) 2003</p>
59  * <p>Company: </p>
60  * @author not attributable
61  * @version 1.0
62  */
63
64 public class MirBasicCommentPostingSessionHandler extends MirBasicPostingSessionHandler {
65   protected ModuleComment commentModule;
66   protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
67
68   public MirBasicCommentPostingSessionHandler() {
69     super();
70
71     commentModule= new ModuleComment(DatabaseComment.getInstance());
72   }
73
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");
78     }
79     else {
80       subsequentRequest(aRequest, aSession, aResponse);
81     }
82   };
83
84   protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
85     super.initializeResponseData(aRequest, aSession, aResponse);
86
87     Iterator i = DatabaseComment.getInstance().getFields().iterator();
88     while (i.hasNext()) {
89       String field = (String) i.next();
90       aResponse.setResponseValue(field, aRequest.getParameter(field));
91     }
92
93   };
94
95   public void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure{
96     super.initialRequest(aRequest, aSession, aResponse);
97
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);
102
103     aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
104   }
105
106   public List validate(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
107     List result = new Vector();
108
109     testFieldEntered(aRequest, "title", "validationerror.missing", result);
110     testFieldEntered(aRequest, "description", "validationerror.missing", result);
111     testFieldEntered(aRequest, "creator", "validationerror.missing", result);
112
113     return result;
114   }
115
116   public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
117     try {
118       initializeResponseData(aRequest, aSession, aResponse);
119
120       List validationErrors = validate(aRequest, aSession);
121
122       if (validationErrors != null && validationErrors.size()>0) {
123         returnValidationErrors(aRequest, aSession, aResponse, validationErrors);
124       }
125       else {
126         EntityComment comment = (EntityComment) commentModule.createNew ();
127         Iterator i = DatabaseComment.getInstance().getFields().iterator();
128 //        comment.setValues(commentFields);
129
130         finishComment(aRequest, aSession, comment);
131
132         String id = comment.insert();
133         if(id==null){
134           afterDuplicateCommentPosting(aRequest, aSession, aResponse, comment);
135           logger.info("Dupe comment rejected");
136           aSession.terminate();
137         }
138         else {
139           i = aRequest.getUploadedFiles().iterator();
140           while (i.hasNext()) {
141             UploadedFile file = (UploadedFile) i.next();
142             processMediaFile(aRequest, aSession, comment, file);
143           }
144
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();
150         }
151       }
152     }
153     catch (Throwable t) {
154       ExceptionFunctions.traceCauseException(t).printStackTrace();
155
156       throw new SessionFailure("MirBasicCommentPostingSessionHandler.subsequentRequest: " + t.getMessage(), t);
157     }
158   }
159
160   public void initializeCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionFailure, SessionExc {
161     String articleId = aRequest.getParameter("to_media");
162     if (articleId==null)
163       articleId = aRequest.getParameter("aid");
164
165     if (articleId==null)
166       throw new SessionExc("initializeCommentPosting: article id not set!");
167
168     aSession.setAttribute("to_media", articleId);
169     processCommentPosting(aRequest, aSession, aResponse);
170   };
171
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"));
175   };
176
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);
183     }
184     else {
185       aResponse.setResponseValue("password", null);
186     }
187
188     aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
189   };
190
191   public void processMediaFile(Request aRequest, Session aSession, EntityComment aComment, UploadedFile aFile) throws SessionExc, SessionFailure {
192     try {
193       Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, new HashMap());
194       finishMedia(aRequest, aSession, aFile, mediaItem);
195       mediaItem.update();
196       commentToMedia.addMedia(aComment.getId(), mediaItem.getId());
197     }
198     catch (Throwable t) {
199       throw new SessionFailure(t);
200     }
201   }
202
203   public void finishMedia(Request aRequest, Session aSession, UploadedFile aFile, Entity aMedia) throws SessionExc, SessionFailure {
204   }
205
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");
209
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"));
214   };
215
216   public void addMedia(Request aRequest, Session aSession, EntityComment aComment)  throws SessionExc, SessionFailure {
217   }
218
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"));
222   };
223
224   public void afterDuplicateCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) {
225     aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate"));
226   };
227
228   public class ValidationError {
229     private String field;
230     private String message;
231     private List parameters;
232
233     public ValidationError(String aField, String aMessage) {
234       this (aField, aMessage, new String[] {});
235     }
236
237     public ValidationError(String aField, String aMessage, Object aParameter) {
238       this (aField, aMessage, new Object[] {aParameter});
239     }
240
241     public ValidationError(String aField, String aMessage, Object[] aParameters) {
242       field = aField;
243       message = aMessage;
244       parameters = Arrays.asList(aParameters);
245     }
246
247     public String getMessage() {
248       return message;
249     }
250
251     public String getField() {
252       return field;
253     }
254
255     public List getParameters() {
256       return parameters;
257     }
258   }
259
260
261
262 }