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