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