2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
31 package mircoders.localizer.basic;
33 import java.util.GregorianCalendar;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
39 import mir.entity.Entity;
40 import mir.misc.StringUtil;
41 import mir.session.Request;
42 import mir.session.Response;
43 import mir.session.Session;
44 import mir.session.SessionExc;
45 import mir.session.SessionFailure;
46 import mir.session.UploadedFile;
47 import mir.session.ValidationHelper;
48 import mircoders.entity.EntityContent;
49 import mircoders.global.MirGlobal;
50 import mircoders.media.MediaUploadProcessor;
51 import mircoders.module.ModuleArticleType;
52 import mircoders.module.ModuleContent;
53 import mircoders.module.ModuleMediafolder;
54 import mircoders.storage.DatabaseArticleType;
55 import mircoders.storage.DatabaseContent;
56 import mircoders.storage.DatabaseContentToMedia;
57 import mircoders.storage.DatabaseContentToTopics;
58 import mircoders.storage.DatabaseMediafolder;
62 * <p>Title: Experimental session handler for article postings </p>
63 * <p>Description: </p>
64 * <p>Copyright: Copyright (c) 2003</p>
70 public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler {
71 protected ModuleContent contentModule = new ModuleContent(DatabaseContent.getInstance());
72 protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance();
73 protected DatabaseContent contentDatabase = DatabaseContent.getInstance();
75 public MirBasicArticlePostingHandler() {
78 setResponseGenerators(
79 configuration.getString("Localizer.OpenSession.article.EditTemplate"),
80 configuration.getString("Localizer.OpenSession.article.DupeTemplate"),
81 configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate"),
82 configuration.getString("Localizer.OpenSession.article.DoneTemplate"));
85 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
86 super.initializeResponseData(aRequest, aSession, aResponse);
88 Iterator i = DatabaseContent.getInstance().getFields().iterator();
90 String field = (String) i.next();
91 aResponse.setResponseValue(field, aRequest.getParameter(field));
93 aResponse.setResponseValue("to_topic", aRequest.getParameters("to_topic"));
96 public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
97 super.validate(aResults, aRequest, aSession);
99 ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
100 ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
101 ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
102 ValidationHelper.testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults);
105 public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {
107 anArticle.setFieldValue("is_published", "1");
108 anArticle.setFieldValue("is_produced", "0");
109 anArticle.setFieldValue("date", StringUtil.date2webdbDate(new GregorianCalendar()));
110 anArticle.setFieldValue("is_html", "0");
111 anArticle.setFieldValue("publish_path", StringUtil.webdbDate2path(anArticle. getFieldValue("date")));
113 ModuleArticleType module = new ModuleArticleType(DatabaseArticleType.getInstance());
114 anArticle.setFieldValue("to_article_type", module.articleTypeIdForName(configuration.getString("Localizer.OpenSession.article.DefaultArticleType")));
115 anArticle.setFieldValue("to_publisher", "1");
117 catch (Throwable t) {
118 throw new SessionFailure(t);
122 public void setArticleTopics(Request aRequest, Session aSession, EntityContent aContent) throws SessionExc, SessionFailure {
124 List topics = aRequest.getParameters("to_topic");
125 if (topics.size() > 0) {
127 DatabaseContentToTopics.getInstance().setTopics(aContent.getId(), topics);
129 catch (Throwable e) {
130 logger.error("setting content_x_topic failed");
131 throw new SessionFailure("MirBasicArticlePostingHandler: can't set topics: " + e.toString(), e);
136 public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
139 Map values = getIntersectingValues(aRequest, DatabaseContent.getInstance());
141 EntityContent article = (EntityContent) contentModule.createNew();
142 article.setValues(values);
144 finalizeArticle(aRequest, aSession, article);
145 id = article.insert();
147 logger.info("Duplicate article rejected");
148 throw new DuplicatePostingExc("Duplicate article rejected");
150 aSession.setAttribute("content", article);
153 setArticleTopics(aRequest, aSession, article);
156 catch (Throwable t) {
157 throw new SessionFailure(t);
161 public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
163 Map values = new HashMap();
164 values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));
165 values.put("creator", aRequest.getParameter("creator"));
166 values.put("to_publisher", "0");
167 values.put("is_published", "1");
168 values.put("is_produced", "1");
169 ModuleMediafolder module = new ModuleMediafolder(DatabaseMediafolder.getInstance());
170 values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.article.DefaultMediaFolder")));
172 Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
174 contentToMedia.addMedia(((EntityContent) aSession.getAttribute("content")).getId(), mediaItem.getId());
176 catch (Throwable t) {
177 throw new SessionFailure(t);
181 public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
182 EntityContent article = (EntityContent) aSession.getAttribute("content");
184 MirGlobal.abuse().checkArticle(article, aRequest, null);
186 MirGlobal.localizer().openPostings().afterContentPosting(article);
188 catch (Throwable t) {
189 throw new SessionFailure(t);
191 logger.info("article posted");