bcb42ff8459094b4c417e457e93dd9436244ba40
[mir.git] / source / mircoders / localizer / basic / MirBasicArticlePostingHandler.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
29  */\r
30 \r
31 package mircoders.localizer.basic;\r
32 \r
33 import java.util.GregorianCalendar;\r
34 import java.util.HashMap;\r
35 import java.util.Iterator;\r
36 import java.util.List;\r
37 import java.util.Map;\r
38 \r
39 import mir.entity.Entity;\r
40 import mir.misc.StringUtil;\r
41 import mir.session.Request;\r
42 import mir.session.Response;\r
43 import mir.session.Session;\r
44 import mir.session.SessionExc;\r
45 import mir.session.SessionFailure;\r
46 import mir.session.UploadedFile;\r
47 import mir.session.ValidationError;\r
48 import mir.session.ValidationHelper;\r
49 import mir.util.ExceptionFunctions;\r
50 import mircoders.entity.EntityContent;\r
51 import mircoders.global.MirGlobal;\r
52 import mircoders.media.MediaUploadProcessor;\r
53 import mircoders.module.ModuleContent;\r
54 import mircoders.module.*;\r
55 import mircoders.storage.*;\r
56 import mircoders.storage.DatabaseContent;\r
57 import mircoders.storage.DatabaseContentToMedia;\r
58 import mircoders.storage.DatabaseContentToTopics;\r
59 \r
60 /**\r
61  *\r
62  * <p>Title: Experimental session handler for article postings </p>\r
63  * <p>Description: </p>\r
64  * <p>Copyright: Copyright (c) 2003</p>\r
65  * <p>Company: </p>\r
66  * @author Zapata\r
67  * @version 1.0\r
68  */\r
69 \r
70 public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler {\r
71   protected ModuleContent contentModule = new ModuleContent(DatabaseContent.getInstance());\r
72   protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance();\r
73   protected DatabaseContent contentDatabase = DatabaseContent.getInstance();\r
74 \r
75   public MirBasicArticlePostingHandler() {\r
76     super();\r
77 \r
78     setResponseGenerators(\r
79       configuration.getString("Localizer.OpenSession.article.EditTemplate"),\r
80       configuration.getString("Localizer.OpenSession.article.DupeTemplate"),\r
81       configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate"),\r
82       configuration.getString("Localizer.OpenSession.article.DoneTemplate"));\r
83   }\r
84 \r
85   protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
86     super.initializeResponseData(aRequest, aSession, aResponse);\r
87 \r
88     Iterator i = DatabaseContent.getInstance().getFields().iterator();\r
89     while (i.hasNext()) {\r
90       String field = (String) i.next();\r
91       aResponse.setResponseValue(field, aRequest.getParameter(field));\r
92     }\r
93     aResponse.setResponseValue("to_topic", aRequest.getParameters("to_topic"));\r
94   }\r
95 \r
96   public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
97     super.validate(aResults, aRequest, aSession);\r
98 \r
99     ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);\r
100     ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);\r
101     ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);\r
102     ValidationHelper.testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults);\r
103   }\r
104 \r
105   public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {\r
106     try {\r
107       anArticle.setValueForProperty("is_published", "1");\r
108       anArticle.setValueForProperty("is_produced", "0");\r
109       anArticle.setValueForProperty("date",\r
110                                     StringUtil.date2webdbDate(new GregorianCalendar()));\r
111       anArticle.setValueForProperty("is_html", "0");\r
112       anArticle.setValueForProperty("publish_path",\r
113                                     StringUtil.webdbDate2path(anArticle.\r
114           getValue("date")));\r
115       ModuleArticleType module = new ModuleArticleType(DatabaseArticleType.\r
116           getInstance());\r
117 \r
118       anArticle.setValueForProperty("to_article_type",\r
119                                     module.articleTypeIdForName(configuration.\r
120           getString("Localizer.OpenSession.article.DefaultArticleType")));\r
121       anArticle.setValueForProperty("to_publisher", "1");\r
122     }\r
123     catch (Throwable t) {\r
124       throw new SessionFailure(t);\r
125     }\r
126   }\r
127 \r
128   public void setArticleTopics(Request aRequest, Session aSession, EntityContent aContent) throws SessionExc, SessionFailure {\r
129     // topics:\r
130     List topics = aRequest.getParameters("to_topic");\r
131     if (topics.size() > 0) {\r
132       try {\r
133         DatabaseContentToTopics.getInstance().setTopics(aContent.getId(), topics);\r
134       }\r
135       catch (Throwable e) {\r
136         logger.error("setting content_x_topic failed");\r
137         throw new SessionFailure("MirBasicArticlePostingHandler: can't set topics: " + e.toString(), e);\r
138       }\r
139     }\r
140   }\r
141 \r
142   public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
143     try {\r
144       String id;\r
145       Map values = getIntersectingValues(aRequest, DatabaseContent.getInstance());\r
146 \r
147       EntityContent article = (EntityContent) contentModule.createNew();\r
148       article.setValues(values);\r
149 \r
150       finalizeArticle(aRequest, aSession, article);\r
151       id = article.insert();\r
152       if (id == null) {\r
153         logger.info("Duplicate article rejected");\r
154         throw new DuplicatePostingExc("Duplicate article rejected");\r
155       }\r
156       aSession.setAttribute("content", article);\r
157 \r
158 \r
159       setArticleTopics(aRequest, aSession, article);\r
160 \r
161     }\r
162     catch (Throwable t) {\r
163       throw new SessionFailure(t);\r
164     }\r
165   }\r
166 \r
167   public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {\r
168     try {\r
169       Map values = new HashMap();\r
170       values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));\r
171       values.put("creator", aRequest.getParameter("creator"));\r
172       values.put("to_publisher", "0");\r
173       values.put("is_published", "1");\r
174       values.put("is_produced", "1");\r
175       Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);\r
176       mediaItem.update();\r
177       contentToMedia.addMedia(((EntityContent) aSession.getAttribute("content")).getId(), mediaItem.getId());\r
178     }\r
179     catch (Throwable t) {\r
180       throw new SessionFailure(t);\r
181     }\r
182   }\r
183 \r
184   public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
185     EntityContent article = (EntityContent) aSession.getAttribute("content");\r
186 \r
187     MirGlobal.abuse().checkArticle(article, aRequest, null);\r
188     try {\r
189       MirGlobal.localizer().openPostings().afterContentPosting(article);\r
190     }\r
191     catch (Throwable t) {\r
192       throw new SessionFailure(t);\r
193     }\r
194     logger.info("article posted");\r
195   };\r
196 \r
197 }
198