article type for new articles using the open session interface now configurable
[mir.git] / source / mircoders / localizer / basic / MirBasicArticlePostingHandler.java
index fa13298..8b55a30 100755 (executable)
-/*
- * Copyright (C) 2001, 2002 The Mir-coders group
- *
- * This file is part of Mir.
- *
- * Mir is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Mir is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Mir; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with  any library licensed under the Apache Software License,
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
- * (or with modified versions of the above that use the same license as the above),
- * and distribute linked combinations including the two.  You must obey the
- * GNU General Public License in all respects for all of the code used other than
- * the above mentioned libraries.  If you modify this file, you may extend this
- * exception to your version of the file, but you are not obligated to do so.
- * If you do not wish to do so, delete this exception statement from your version.
- */
-
-package mircoders.localizer.basic;
-
-import java.util.GregorianCalendar;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import mir.entity.Entity;
-import mir.misc.StringUtil;
-import mir.session.Request;
-import mir.session.Response;
-import mir.session.Session;
-import mir.session.SessionExc;
-import mir.session.SessionFailure;
-import mir.session.UploadedFile;
-import mir.util.ExceptionFunctions;
-import mircoders.entity.EntityContent;
-import mircoders.global.MirGlobal;
-import mircoders.media.MediaUploadProcessor;
-import mircoders.module.ModuleContent;
-import mircoders.module.ModuleMediaType;
-import mircoders.storage.DatabaseContent;
-import mircoders.storage.DatabaseContentToMedia;
-import mircoders.storage.DatabaseContentToTopics;
-
-/**
- *
- * <p>Title: Experimental session handler for article postings </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author Zapata
- * @version 1.0
- */
-
-public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler {
-  protected ModuleContent contentModule = new ModuleContent(DatabaseContent.getInstance());
-  protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance();
-  protected DatabaseContent contentDatabase = DatabaseContent.getInstance();
-
-  public MirBasicArticlePostingHandler() {
-    super();
-
-    setResponseGenerators(
-      configuration.getString("Localizer.OpenSession.article.EditTemplate"),
-      configuration.getString("Localizer.OpenSession.article.DupeTemplate"),
-      configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate"),
-      configuration.getString("Localizer.OpenSession.article.DoneTemplate"));
-  }
-
-  protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
-    super.initializeResponseData(aRequest, aSession, aResponse);
-
-    Iterator i = DatabaseContent.getInstance().getFields().iterator();
-    while (i.hasNext()) {
-      String field = (String) i.next();
-      aResponse.setResponseValue(field, aRequest.getParameter(field));
-    }
-    aResponse.setResponseValue("to_topic", aRequest.getParameters("to_topic"));
-  }
-
-  public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
-    super.validate(aResults, aRequest, aSession);
-
-    testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
-    testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
-    testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
-    testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults);
-  }
-
-  public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {
-    anArticle.setValueForProperty("is_published", "1");
-    anArticle.setValueForProperty("is_produced", "0");
-    anArticle.setValueForProperty("date", StringUtil.date2webdbDate(new GregorianCalendar()));
-    anArticle.setValueForProperty("is_html","0");
-    anArticle.setValueForProperty("publish_path", StringUtil.webdbDate2path(anArticle.getValue("date")));
-    anArticle.setValueForProperty("to_article_type", "1");
-    anArticle.setValueForProperty("to_publisher", "1");
-  }
-
-  public void setArticleTopics(Request aRequest, Session aSession, EntityContent aContent) throws SessionExc, SessionFailure {
-    // topics:
-    List topics = aRequest.getParameters("to_topic");
-    if (topics.size() > 0) {
-      try {
-        DatabaseContentToTopics.getInstance().setTopics(aContent.getId(), topics);
-      }
-      catch (Throwable e) {
-        logger.error("setting content_x_topic failed");
-        throw new SessionFailure("MirBasicArticlePostingHandler: can't set topics: " + e.toString(), e);
-      }
-    }
-  }
-
-  public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
-    try {
-      String id;
-      Map values = getIntersectingValues(aRequest, DatabaseContent.getInstance());
-
-      EntityContent article = (EntityContent) contentModule.createNew();
-      article.setValues(values);
-
-      finalizeArticle(aRequest, aSession, article);
-      id = article.insert();
-      if (id == null) {
-        logger.info("Duplicate article rejected");
-        throw new DuplicatePostingExc("Duplicate article rejected");
-      }
-      aSession.setAttribute("content", article);
-
-
-      setArticleTopics(aRequest, aSession, article);
-
-    }
-    catch (Throwable t) {
-      throw new SessionFailure(t);
-    }
-  }
-
-  public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
-    try {
-      Map values = new HashMap();
-      values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));
-      values.put("creator", aRequest.getParameter("creator"));
-      values.put("to_publisher", "0");
-      values.put("is_published", "1");
-      values.put("is_produced", "1");
-      Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
-      mediaItem.update();
-      contentToMedia.addMedia(((EntityContent) aSession.getAttribute("content")).getId(), mediaItem.getId());
-    }
-    catch (Throwable t) {
-      throw new SessionFailure(t);
-    }
-  }
-
-  public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
-    EntityContent article = (EntityContent) aSession.getAttribute("content");
-
-    MirGlobal.abuse().checkArticle(article, aRequest, null);
-    try {
-      MirGlobal.localizer().openPostings().afterContentPosting(article);
-    }
-    catch (Throwable t) {
-      throw new SessionFailure(t);
-    }
-    logger.info("article posted");
-  };
-
+/*\r
+ * Copyright (C) 2001, 2002 The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with  any library licensed under the Apache Software License,\r
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
+ * (or with modified versions of the above that use the same license as the above),\r
+ * and distribute linked combinations including the two.  You must obey the\r
+ * GNU General Public License in all respects for all of the code used other than\r
+ * the above mentioned libraries.  If you modify this file, you may extend this\r
+ * exception to your version of the file, but you are not obligated to do so.\r
+ * If you do not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mircoders.localizer.basic;\r
+\r
+import java.util.GregorianCalendar;\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import mir.entity.Entity;\r
+import mir.misc.StringUtil;\r
+import mir.session.Request;\r
+import mir.session.Response;\r
+import mir.session.Session;\r
+import mir.session.SessionExc;\r
+import mir.session.SessionFailure;\r
+import mir.session.UploadedFile;\r
+import mir.util.ExceptionFunctions;\r
+import mircoders.entity.EntityContent;\r
+import mircoders.global.MirGlobal;\r
+import mircoders.media.MediaUploadProcessor;\r
+import mircoders.module.ModuleContent;\r
+import mircoders.module.*;\r
+import mircoders.storage.*;\r
+import mircoders.storage.DatabaseContent;\r
+import mircoders.storage.DatabaseContentToMedia;\r
+import mircoders.storage.DatabaseContentToTopics;\r
+\r
+/**\r
+ *\r
+ * <p>Title: Experimental session handler for article postings </p>\r
+ * <p>Description: </p>\r
+ * <p>Copyright: Copyright (c) 2003</p>\r
+ * <p>Company: </p>\r
+ * @author Zapata\r
+ * @version 1.0\r
+ */\r
+\r
+public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler {\r
+  protected ModuleContent contentModule = new ModuleContent(DatabaseContent.getInstance());\r
+  protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance();\r
+  protected DatabaseContent contentDatabase = DatabaseContent.getInstance();\r
+\r
+  public MirBasicArticlePostingHandler() {\r
+    super();\r
+\r
+    setResponseGenerators(\r
+      configuration.getString("Localizer.OpenSession.article.EditTemplate"),\r
+      configuration.getString("Localizer.OpenSession.article.DupeTemplate"),\r
+      configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate"),\r
+      configuration.getString("Localizer.OpenSession.article.DoneTemplate"));\r
+  }\r
+\r
+  protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
+    super.initializeResponseData(aRequest, aSession, aResponse);\r
+\r
+    Iterator i = DatabaseContent.getInstance().getFields().iterator();\r
+    while (i.hasNext()) {\r
+      String field = (String) i.next();\r
+      aResponse.setResponseValue(field, aRequest.getParameter(field));\r
+    }\r
+    aResponse.setResponseValue("to_topic", aRequest.getParameters("to_topic"));\r
+  }\r
+\r
+  public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
+    super.validate(aResults, aRequest, aSession);\r
+\r
+    testFieldEntered(aRequest, "title", "validationerror.missing", aResults);\r
+    testFieldEntered(aRequest, "description", "validationerror.missing", aResults);\r
+    testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);\r
+    testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults);\r
+  }\r
+\r
+  public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {\r
+    try {\r
+      anArticle.setValueForProperty("is_published", "1");\r
+      anArticle.setValueForProperty("is_produced", "0");\r
+      anArticle.setValueForProperty("date",\r
+                                    StringUtil.date2webdbDate(new GregorianCalendar()));\r
+      anArticle.setValueForProperty("is_html", "0");\r
+      anArticle.setValueForProperty("publish_path",\r
+                                    StringUtil.webdbDate2path(anArticle.\r
+          getValue("date")));\r
+      ModuleArticleType module = new ModuleArticleType(DatabaseArticleType.\r
+          getInstance());\r
+\r
+      anArticle.setValueForProperty("to_article_type",\r
+                                    module.articleTypeIdForName(configuration.\r
+          getString("Localizer.OpenSession.article.DefaultArticleType")));\r
+      anArticle.setValueForProperty("to_publisher", "1");\r
+    }\r
+    catch (Throwable t) {\r
+      throw new SessionFailure(t);\r
+    }\r
+  }\r
+\r
+  public void setArticleTopics(Request aRequest, Session aSession, EntityContent aContent) throws SessionExc, SessionFailure {\r
+    // topics:\r
+    List topics = aRequest.getParameters("to_topic");\r
+    if (topics.size() > 0) {\r
+      try {\r
+        DatabaseContentToTopics.getInstance().setTopics(aContent.getId(), topics);\r
+      }\r
+      catch (Throwable e) {\r
+        logger.error("setting content_x_topic failed");\r
+        throw new SessionFailure("MirBasicArticlePostingHandler: can't set topics: " + e.toString(), e);\r
+      }\r
+    }\r
+  }\r
+\r
+  public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
+    try {\r
+      String id;\r
+      Map values = getIntersectingValues(aRequest, DatabaseContent.getInstance());\r
+\r
+      EntityContent article = (EntityContent) contentModule.createNew();\r
+      article.setValues(values);\r
+\r
+      finalizeArticle(aRequest, aSession, article);\r
+      id = article.insert();\r
+      if (id == null) {\r
+        logger.info("Duplicate article rejected");\r
+        throw new DuplicatePostingExc("Duplicate article rejected");\r
+      }\r
+      aSession.setAttribute("content", article);\r
+\r
+\r
+      setArticleTopics(aRequest, aSession, article);\r
+\r
+    }\r
+    catch (Throwable t) {\r
+      throw new SessionFailure(t);\r
+    }\r
+  }\r
+\r
+  public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {\r
+    try {\r
+      Map values = new HashMap();\r
+      values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));\r
+      values.put("creator", aRequest.getParameter("creator"));\r
+      values.put("to_publisher", "0");\r
+      values.put("is_published", "1");\r
+      values.put("is_produced", "1");\r
+      Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);\r
+      mediaItem.update();\r
+      contentToMedia.addMedia(((EntityContent) aSession.getAttribute("content")).getId(), mediaItem.getId());\r
+    }\r
+    catch (Throwable t) {\r
+      throw new SessionFailure(t);\r
+    }\r
+  }\r
+\r
+  public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
+    EntityContent article = (EntityContent) aSession.getAttribute("content");\r
+\r
+    MirGlobal.abuse().checkArticle(article, aRequest, null);\r
+    try {\r
+      MirGlobal.localizer().openPostings().afterContentPosting(article);\r
+    }\r
+    catch (Throwable t) {\r
+      throw new SessionFailure(t);\r
+    }\r
+    logger.info("article posted");\r
+  };\r
+\r
 }
\ No newline at end of file