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