removed general purpose validation stuff and moved
[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.session.ValidationError;
45 import mir.session.ValidationHelper;
46 import mir.util.ExceptionFunctions;
47 import mircoders.entity.EntityComment;
48 import mircoders.global.MirGlobal;
49 import mircoders.media.MediaUploadProcessor;
50 import mircoders.module.ModuleComment;
51 import mircoders.module.ModuleMediaType;
52 import mircoders.storage.DatabaseComment;
53 import mircoders.storage.DatabaseCommentToMedia;
54 import mircoders.storage.DatabaseContent;
55
56 /**
57  *
58  * <p>Title: Experimental session handler for comment postings </p>
59  * <p>Description: </p>
60  * <p>Copyright: Copyright (c) 2003</p>
61  * <p>Company: </p>
62  * @author Zapata
63  * @version 1.0
64  */
65
66 public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler {
67   protected ModuleComment commentModule = new ModuleComment(DatabaseComment.getInstance());
68   protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
69
70
71   public MirBasicCommentPostingHandler() {
72     super();
73
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"));
79   }
80
81   protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
82     super.initializeResponseData(aRequest, aSession, aResponse);
83
84     Iterator i = DatabaseComment.getInstance().getFields().iterator();
85     while (i.hasNext()) {
86       String field = (String) i.next();
87       aResponse.setResponseValue(field, aRequest.getParameter(field));
88     }
89   }
90
91   public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
92     super.validate(aResults, aRequest, aSession);
93
94     ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
95     ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
96     ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
97   }
98
99   protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
100     super.initializeSession(aRequest, aSession);
101
102     String articleId = aRequest.getParameter("to_media");
103     if (articleId==null)
104       throw new SessionExc("initializeSession: article id not set!");
105
106     aSession.setAttribute("to_media", articleId);
107   };
108
109   public void finalizeComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
110     aComment.setValueForProperty("is_published", "1");
111     aComment.setValueForProperty("to_comment_status", "1");
112     aComment.setValueForProperty("is_html","0");
113     aComment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media"));
114   }
115
116   public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
117     try {
118       String id;
119       Map values = getIntersectingValues(aRequest, DatabaseComment.getInstance());
120
121       EntityComment comment = (EntityComment) commentModule.createNew();
122       comment.setValues(values);
123       finalizeComment(aRequest, aSession, comment);
124       id = comment.insert();
125       if (id == null) {
126         logger.info("Duplicate comment rejected");
127         throw new DuplicateCommentExc("Duplicate comment rejected");
128       }
129       aSession.setAttribute("comment", comment);
130     }
131     catch (Throwable t) {
132       throw new SessionFailure(t);
133     }
134   }
135
136   public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
137     try {
138       Map values = new HashMap();
139       values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));
140       values.put("creator", aRequest.getParameter("creator"));
141       values.put("to_publisher", "0");
142       values.put("is_published", "1");
143       Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
144       mediaItem.update();
145       commentToMedia.addMedia(((EntityComment) aSession.getAttribute("comment")).getId(), mediaItem.getId());
146     }
147     catch (Throwable t) {
148       throw new SessionFailure(t);
149     }
150   }
151
152   public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
153     EntityComment comment = (EntityComment) aSession.getAttribute("comment");
154
155     MirGlobal.abuse().checkComment(comment, aRequest, null);
156     try {
157       MirGlobal.localizer().openPostings().afterCommentPosting(comment);
158     }
159     catch (Throwable t) {
160       throw new SessionFailure(t);
161     }
162     DatabaseContent.getInstance().setUnproduced("id=" + comment.getValue("to_media"));
163     logger.info("Comment posted");
164   };
165
166   protected static class DuplicateCommentExc extends SessionExc {
167     public DuplicateCommentExc(String aMessage) {
168       super(aMessage);
169     }
170   }
171 }