tiny fixes here and there
[mir.git] / source / mircoders / localizer / basic / MirBasicArticlePostingHandler.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
31 package mircoders.localizer.basic;
32
33 import mir.entity.Entity;
34 import mir.misc.StringUtil;
35 import mir.session.Request;
36 import mir.session.Response;
37 import mir.session.Session;
38 import mir.session.SessionExc;
39 import mir.session.SessionFailure;
40 import mir.session.ValidationHelper;
41 import mir.util.EntityUtility;
42 import mircoders.entity.EntityContent;
43 import mircoders.global.MirGlobal;
44 import mircoders.media.MediaUploadProcessor;
45 import mircoders.module.ModuleArticleType;
46 import mircoders.module.ModuleMediafolder;
47 import mircoders.storage.DatabaseContent;
48 import mircoders.storage.DatabaseContentToMedia;
49 import mircoders.storage.DatabaseContentToTopics;
50
51 import java.util.GregorianCalendar;
52 import java.util.HashMap;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Map;
56
57 /**
58  * Extensible handler for open article postings
59  */
60
61 public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler {
62   protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance();
63   protected DatabaseContent contentDatabase = DatabaseContent.getInstance();
64
65   public MirBasicArticlePostingHandler() {
66     this(false);
67   }
68
69   public MirBasicArticlePostingHandler(boolean aPersistentUploadedFiles) {
70     super(aPersistentUploadedFiles);
71
72     setResponseGenerators(
73       configuration.getString("Localizer.OpenSession.article.EditTemplate"),
74       configuration.getString("Localizer.OpenSession.article.DupeTemplate"),
75       configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate"),
76       configuration.getString("Localizer.OpenSession.article.DoneTemplate"));
77   }
78   /**
79    * {@inheritDoc}
80    */
81   protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
82     super.initializeResponseData(aRequest, aSession, aResponse);
83
84     Iterator i = DatabaseContent.getInstance().getFieldNames().iterator();
85     while (i.hasNext()) {
86       String field = (String) i.next();
87       aResponse.setResponseValue(field, aRequest.getParameter(field));
88     }
89     aResponse.setResponseValue("to_topic", aRequest.getParameters("to_topic"));
90   }
91
92   /**
93    * {@inheritDoc}
94    */
95   public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
96     super.validate(aResults, aRequest, aSession);
97
98     ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
99     ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
100     ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
101     ValidationHelper.testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults);
102   }
103
104   /**
105    * Called just before the article is stored in the database.
106    * The last opportunity to alter fields.
107    */
108   public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {
109     try {
110       anArticle.setFieldValue("is_published", "1");
111       anArticle.setFieldValue("is_produced", "0");
112       anArticle.setFieldValue("date", StringUtil.date2webdbDate(new GregorianCalendar()));
113       anArticle.setFieldValue("is_html", "0");
114       anArticle.setFieldValue("publish_path", StringUtil.webdbDate2path(anArticle. getFieldValue("date")));
115
116       ModuleArticleType module = new ModuleArticleType();
117       anArticle.setFieldValue("to_article_type", module.articleTypeIdForName(configuration.getString("Localizer.OpenSession.article.DefaultArticleType")));
118       anArticle.setFieldValue("to_publisher", "1");
119     }
120     catch (Throwable t) {
121       throw new SessionFailure(t);
122     }
123   }
124
125   /**
126    * Extracts topics from the request and associated the
127    * article to them.
128    */
129   public void setArticleTopics(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {
130     // topics:
131     List topics = aRequest.getParameters("to_topic");
132     if (topics.size() > 0) {
133       try {
134         DatabaseContentToTopics.getInstance().setTopics(anArticle.getId(), topics);
135       }
136       catch (Throwable e) {
137         logger.error("setting content_x_topic failed");
138         throw new SessionFailure("MirBasicArticlePostingHandler: can't set topics: " + e.toString(), e);
139       }
140     }
141   }
142
143   public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
144     try {
145       String id;
146       Map values = getIntersectingValues(aRequest, DatabaseContent.getInstance());
147
148       EntityContent article = (EntityContent) contentDatabase.createNewEntity();
149       article.setFieldValues(values);
150
151       finalizeArticle(aRequest, aSession, article);
152       id = article.insert();
153       if (id == null) {
154         logger.info("Duplicate article rejected");
155         throw new DuplicatePostingExc("Duplicate article rejected");
156       }
157       aSession.setAttribute("content", article);
158
159       setArticleTopics(aRequest, aSession, article);
160     }
161     catch (Throwable t) {
162       throw new SessionFailure(t);
163     }
164   }
165
166   public void processAttachmentError(Request aRequest, Session aSession, Attachment aFile, Throwable anError) {
167     EntityUtility.appendLineToField( ((EntityContent) aSession.getAttribute("content")), "comment",
168         "error with attachment: " + anError.toString());
169    ((EntityContent) aSession.getAttribute("content")).update();
170   }
171
172   public void processAttachment(Request aRequest, Session aSession, Attachment aFile) throws SessionExc, SessionFailure {
173     try {
174       Map values = new HashMap();
175       values.put("creator", aRequest.getParameter("creator"));
176       values.putAll(aFile.getAllAttributes());
177       values.put("creator", aRequest.getParameter("creator"));
178       values.put("to_publisher", "0");
179       values.put("is_published", "1");
180       values.put("is_produced", "1");
181       ModuleMediafolder module = new ModuleMediafolder();
182       values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.article.DefaultMediaFolder")));
183
184       Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
185       mediaItem.update();
186       contentToMedia.addMedia(((EntityContent) aSession.getAttribute("content")).getId(), mediaItem.getId());
187     }
188     catch (Throwable t) {
189       throw new SessionFailure(t);
190     }
191   }
192
193   public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
194     EntityContent article = (EntityContent) aSession.getAttribute("content");
195
196     MirGlobal.abuse().checkArticle(article, aRequest, null);
197     try {
198       MirGlobal.localizer().openPostings().afterArticlePosting(article);
199     }
200     catch (Throwable t) {
201       throw new SessionFailure(t);
202     }
203     logger.info("article posted");
204   }
205
206 }