2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\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
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
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
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
31 package mircoders.localizer.basic;
\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
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.ValidationHelper;
\r
48 import mircoders.entity.EntityContent;
\r
49 import mircoders.global.MirGlobal;
\r
50 import mircoders.media.MediaUploadProcessor;
\r
51 import mircoders.module.ModuleArticleType;
\r
52 import mircoders.module.*;
\r
53 import mircoders.storage.DatabaseArticleType;
\r
54 import mircoders.storage.*;
\r
55 import mircoders.storage.DatabaseContentToMedia;
\r
56 import mircoders.storage.DatabaseContentToTopics;
\r
60 * <p>Title: Experimental session handler for article postings </p>
\r
61 * <p>Description: </p>
\r
62 * <p>Copyright: Copyright (c) 2003</p>
\r
68 public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler {
\r
69 protected ModuleContent contentModule = new ModuleContent(DatabaseContent.getInstance());
\r
70 protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance();
\r
71 protected DatabaseContent contentDatabase = DatabaseContent.getInstance();
\r
73 public MirBasicArticlePostingHandler() {
\r
76 setResponseGenerators(
\r
77 configuration.getString("Localizer.OpenSession.article.EditTemplate"),
\r
78 configuration.getString("Localizer.OpenSession.article.DupeTemplate"),
\r
79 configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate"),
\r
80 configuration.getString("Localizer.OpenSession.article.DoneTemplate"));
\r
83 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
84 super.initializeResponseData(aRequest, aSession, aResponse);
\r
86 Iterator i = DatabaseContent.getInstance().getFields().iterator();
\r
87 while (i.hasNext()) {
\r
88 String field = (String) i.next();
\r
89 aResponse.setResponseValue(field, aRequest.getParameter(field));
\r
91 aResponse.setResponseValue("to_topic", aRequest.getParameters("to_topic"));
\r
94 public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
95 super.validate(aResults, aRequest, aSession);
\r
97 ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
\r
98 ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
\r
99 ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
\r
100 ValidationHelper.testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults);
\r
103 public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {
\r
105 anArticle.setValueForProperty("is_published", "1");
\r
106 anArticle.setValueForProperty("is_produced", "0");
\r
107 anArticle.setValueForProperty("date", StringUtil.date2webdbDate(new GregorianCalendar()));
\r
108 anArticle.setValueForProperty("is_html", "0");
\r
109 anArticle.setValueForProperty("publish_path", StringUtil.webdbDate2path(anArticle. getValue("date")));
\r
111 ModuleArticleType module = new ModuleArticleType(DatabaseArticleType.getInstance());
\r
112 anArticle.setValueForProperty("to_article_type", module.articleTypeIdForName(configuration.getString("Localizer.OpenSession.article.DefaultArticleType")));
\r
113 anArticle.setValueForProperty("to_publisher", "1");
\r
115 catch (Throwable t) {
\r
116 throw new SessionFailure(t);
\r
120 public void setArticleTopics(Request aRequest, Session aSession, EntityContent aContent) throws SessionExc, SessionFailure {
\r
122 List topics = aRequest.getParameters("to_topic");
\r
123 if (topics.size() > 0) {
\r
125 DatabaseContentToTopics.getInstance().setTopics(aContent.getId(), topics);
\r
127 catch (Throwable e) {
\r
128 logger.error("setting content_x_topic failed");
\r
129 throw new SessionFailure("MirBasicArticlePostingHandler: can't set topics: " + e.toString(), e);
\r
134 public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
137 Map values = getIntersectingValues(aRequest, DatabaseContent.getInstance());
\r
139 EntityContent article = (EntityContent) contentModule.createNew();
\r
140 article.setValues(values);
\r
142 finalizeArticle(aRequest, aSession, article);
\r
143 id = article.insert();
\r
145 logger.info("Duplicate article rejected");
\r
146 throw new DuplicatePostingExc("Duplicate article rejected");
\r
148 aSession.setAttribute("content", article);
\r
151 setArticleTopics(aRequest, aSession, article);
\r
154 catch (Throwable t) {
\r
155 throw new SessionFailure(t);
\r
159 public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
\r
161 Map values = new HashMap();
\r
162 values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));
\r
163 values.put("creator", aRequest.getParameter("creator"));
\r
164 values.put("to_publisher", "0");
\r
165 values.put("is_published", "1");
\r
166 values.put("is_produced", "1");
\r
167 ModuleMediafolder module = new ModuleMediafolder(DatabaseMediafolder.getInstance());
\r
168 values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.article.DefaultMediaFolder")));
\r
170 Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
\r
171 mediaItem.update();
\r
172 contentToMedia.addMedia(((EntityContent) aSession.getAttribute("content")).getId(), mediaItem.getId());
\r
174 catch (Throwable t) {
\r
175 throw new SessionFailure(t);
\r
179 public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
180 EntityContent article = (EntityContent) aSession.getAttribute("content");
\r
182 MirGlobal.abuse().checkArticle(article, aRequest, null);
\r
184 MirGlobal.localizer().openPostings().afterContentPosting(article);
\r
186 catch (Throwable t) {
\r
187 throw new SessionFailure(t);
\r
189 logger.info("article posted");
\r