opensession fixes
[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 java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36
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.UploadedFile;
44 import mir.util.ExceptionFunctions;
45 import mircoders.entity.EntityComment;
46 import mircoders.global.MirGlobal;
47 import mircoders.media.MediaUploadProcessor;
48 import mircoders.module.ModuleComment;
49 import mircoders.module.ModuleMediaType;
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 Zapata
61  * @version 1.0
62  */
63
64 public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler {
65   protected ModuleComment commentModule = new ModuleComment(DatabaseComment.getInstance());
66   protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
67
68   protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
69     super.initializeResponseData(aRequest, aSession, aResponse);
70
71     Iterator i = DatabaseComment.getInstance().getFields().iterator();
72     while (i.hasNext()) {
73       String field = (String) i.next();
74       aResponse.setResponseValue(field, aRequest.getParameter(field));
75     }
76   }
77
78   public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
79     testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
80     testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
81     testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
82   }
83
84   protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
85     super.initializeSession(aRequest, aSession);
86
87     String articleId = aRequest.getParameter("to_media");
88     if (articleId==null)
89       throw new SessionExc("initializeCommentPosting: article id not set!");
90
91     aSession.setAttribute("to_media", articleId);
92   };
93
94   public void finalizeComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
95     aComment.setValueForProperty("is_published", "1");
96     aComment.setValueForProperty("to_comment_status", "1");
97     aComment.setValueForProperty("is_html","0");
98   }
99
100   public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
101     try {
102       String id;
103       Map values = getIntersectingValues(aRequest, DatabaseComment.getInstance());
104
105       EntityComment comment = (EntityComment) commentModule.createNew();
106       comment.setValues(values);
107       comment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media"));
108       finalizeComment(aRequest, aSession, comment);
109       id = comment.insert();
110       if (id == null) {
111         logger.info("Duplicate comment rejected");
112         throw new DuplicateCommentExc("Duplicate comment rejected");
113       }
114       aSession.setAttribute("comment", comment);
115     }
116     catch (Throwable t) {
117       throw new SessionFailure(t);
118     }
119   }
120
121   public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
122     try {
123       Map values = new HashMap();
124       values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));
125       values.put("creator", aRequest.getParameter("creator"));
126       values.put("to_publisher", "0");
127       values.put("is_published", "1");
128       Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
129       mediaItem.update();
130       commentToMedia.addMedia(((EntityComment) aSession.getAttribute("comment")).getId(), mediaItem.getId());
131     }
132     catch (Throwable t) {
133       throw new SessionFailure(t);
134     }
135   }
136
137   public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
138     EntityComment comment = (EntityComment) aSession.getAttribute("comment");
139
140     MirGlobal.abuse().checkComment(comment, aRequest, null);
141     try {
142       MirGlobal.localizer().openPostings().afterCommentPosting(comment);
143     }
144     catch (Throwable t) {
145       throw new SessionFailure(t);
146     }
147     DatabaseContent.getInstance().setUnproduced("id=" + comment.getValue("to_media"));
148     logger.info("Comment posted");
149   };
150
151   protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
152     aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
153   };
154
155   protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
156     aResponse.setResponseValue("errors", anErrors);
157     aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
158   };
159
160   protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
161     aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
162   };
163
164   protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
165     anError.printStackTrace();
166     Throwable rootCause = ExceptionFunctions.traceCauseException(anError);
167
168     if (rootCause instanceof DuplicateCommentExc)
169       aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate"));
170     if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
171       aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType());
172       aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate"));
173     }
174     else
175       super.makeErrorResponse(aRequest, aSession, aResponse, anError);
176   };
177
178   protected static class DuplicateCommentExc extends SessionExc {
179     public DuplicateCommentExc(String aMessage) {
180       super(aMessage);
181     }
182   }
183 }