tiny fixes here and there
[mir.git] / source / mircoders / localizer / basic / MirBasicArticlePostingHandler.java
index cfbe93e..c3f42e4 100755 (executable)
-/*\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.session.ValidationHelper;\r
-import mircoders.entity.EntityContent;\r
-import mircoders.global.MirGlobal;\r
-import mircoders.media.MediaUploadProcessor;\r
-import mircoders.module.ModuleArticleType;\r
-import mircoders.module.ModuleContent;\r
-import mircoders.storage.DatabaseArticleType;\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
-    ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);\r
-    ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);\r
-    ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);\r
-    ValidationHelper.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", StringUtil.date2webdbDate(new GregorianCalendar()));\r
-      anArticle.setValueForProperty("is_html", "0");\r
-      anArticle.setValueForProperty("publish_path", StringUtil.webdbDate2path(anArticle. getValue("date")));\r
-      ModuleArticleType module = new ModuleArticleType(DatabaseArticleType.getInstance());\r
-\r
-      anArticle.setValueForProperty("to_article_type", module.articleTypeIdForName(configuration.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
-}\r
+/*
+ * 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 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.ValidationHelper;
+import mir.util.EntityUtility;
+import mircoders.entity.EntityContent;
+import mircoders.global.MirGlobal;
+import mircoders.media.MediaUploadProcessor;
+import mircoders.module.ModuleArticleType;
+import mircoders.module.ModuleMediafolder;
+import mircoders.storage.DatabaseContent;
+import mircoders.storage.DatabaseContentToMedia;
+import mircoders.storage.DatabaseContentToTopics;
+
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Extensible handler for open article postings
+ */
+
+public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler {
+  protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance();
+  protected DatabaseContent contentDatabase = DatabaseContent.getInstance();
+
+  public MirBasicArticlePostingHandler() {
+    this(false);
+  }
+
+  public MirBasicArticlePostingHandler(boolean aPersistentUploadedFiles) {
+    super(aPersistentUploadedFiles);
+
+    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"));
+  }
+  /**
+   * {@inheritDoc}
+   */
+  protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+    super.initializeResponseData(aRequest, aSession, aResponse);
+
+    Iterator i = DatabaseContent.getInstance().getFieldNames().iterator();
+    while (i.hasNext()) {
+      String field = (String) i.next();
+      aResponse.setResponseValue(field, aRequest.getParameter(field));
+    }
+    aResponse.setResponseValue("to_topic", aRequest.getParameters("to_topic"));
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
+    super.validate(aResults, aRequest, aSession);
+
+    ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
+    ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
+    ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
+    ValidationHelper.testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults);
+  }
+
+  /**
+   * Called just before the article is stored in the database.
+   * The last opportunity to alter fields.
+   */
+  public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {
+    try {
+      anArticle.setFieldValue("is_published", "1");
+      anArticle.setFieldValue("is_produced", "0");
+      anArticle.setFieldValue("date", StringUtil.date2webdbDate(new GregorianCalendar()));
+      anArticle.setFieldValue("is_html", "0");
+      anArticle.setFieldValue("publish_path", StringUtil.webdbDate2path(anArticle. getFieldValue("date")));
+
+      ModuleArticleType module = new ModuleArticleType();
+      anArticle.setFieldValue("to_article_type", module.articleTypeIdForName(configuration.getString("Localizer.OpenSession.article.DefaultArticleType")));
+      anArticle.setFieldValue("to_publisher", "1");
+    }
+    catch (Throwable t) {
+      throw new SessionFailure(t);
+    }
+  }
+
+  /**
+   * Extracts topics from the request and associated the
+   * article to them.
+   */
+  public void setArticleTopics(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure {
+    // topics:
+    List topics = aRequest.getParameters("to_topic");
+    if (topics.size() > 0) {
+      try {
+        DatabaseContentToTopics.getInstance().setTopics(anArticle.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) contentDatabase.createNewEntity();
+      article.setFieldValues(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 processAttachmentError(Request aRequest, Session aSession, Attachment aFile, Throwable anError) {
+    EntityUtility.appendLineToField( ((EntityContent) aSession.getAttribute("content")), "comment",
+        "error with attachment: " + anError.toString());
+   ((EntityContent) aSession.getAttribute("content")).update();
+  }
+
+  public void processAttachment(Request aRequest, Session aSession, Attachment aFile) throws SessionExc, SessionFailure {
+    try {
+      Map values = new HashMap();
+      values.put("creator", aRequest.getParameter("creator"));
+      values.putAll(aFile.getAllAttributes());
+      values.put("creator", aRequest.getParameter("creator"));
+      values.put("to_publisher", "0");
+      values.put("is_published", "1");
+      values.put("is_produced", "1");
+      ModuleMediafolder module = new ModuleMediafolder();
+      values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.article.DefaultMediaFolder")));
+
+      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().afterArticlePosting(article);
+    }
+    catch (Throwable t) {
+      throw new SessionFailure(t);
+    }
+    logger.info("article posted");
+  }
+
+}