Initial revision
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
diff --git a/source/mircoders/servlet/ServletModuleOpenIndy.java b/source/mircoders/servlet/ServletModuleOpenIndy.java
new file mode 100755 (executable)
index 0000000..6452a7e
--- /dev/null
@@ -0,0 +1,221 @@
+package mircoders.servlet;
+
+import java.io.*;
+import java.sql.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import freemarker.template.*;
+import com.oreilly.servlet.multipart.*;
+import com.oreilly.servlet.*;
+
+import webdb.servlet.*;
+import webdb.module.*;
+import webdb.misc.*;
+import webdb.entity.*;
+import webdb.storage.*;
+
+import mir.entity.*;
+import mir.storage.*;
+import mir.module.*;
+import mir.producer.*;
+
+/*
+ *  ServletModuleOpenIndy -
+ *   is the open-access-servlet, which is responsible for
+ *    adding comments to articles &
+ *    open-postings to the newswire
+ *
+ * @author RK
+ */
+
+public class ServletModuleOpenIndy extends ServletModule
+{
+
+       private String          commentFormTemplate, commentFormDoneTemplate;
+       private String          postingFormTemplate, postingFormDoneTemplate;
+       private ModuleContent   contentModule;
+       private ModuleImages     imageModule;
+
+       // Singelton / Kontruktor
+       private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
+       public static ServletModule getInstance() { return instance; }
+
+       private ServletModuleOpenIndy() {
+               try {
+                       theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.OpenIndy.Logfile"));
+                       commentFormTemplate = Configuration.getProperty("ServletModule.OpenIndy.CommentTemplate");
+                       commentFormDoneTemplate = Configuration.getProperty("ServletModule.OpenIndy.CommentDoneTemplate");
+                       postingFormTemplate = Configuration.getProperty("ServletModule.OpenIndy.PostingTemplate");
+                       postingFormDoneTemplate = Configuration.getProperty("ServletModule.OpenIndy.PostingDoneTemplate");
+
+                       mainModule = new ModuleComment(DatabaseComment.getInstance());
+                       contentModule = new ModuleContent(DatabaseContent.getInstance());
+                       imageModule = new ModuleImages(DatabaseImages.getInstance());
+      defaultAction="addposting";
+               }
+               catch (StorageObjectException e) {
+                               theLog.printError("servletmoduleopenindy could not be initialized");
+               }
+       }
+
+
+       /**
+        *  Method for making a comment
+        */
+
+       public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
+       {
+               String aid = req.getParameter("aid"); // the article id the comment will belong to
+               if (aid!=null && !aid.equals(""))
+               {
+                       SimpleHash mergeData = new SimpleHash();
+                       // ok, article
+                       mergeData.put("aid", aid);
+                       deliver(req, res, mergeData, commentFormTemplate);
+               }
+               else throw new ServletModuleException("aid not set!");
+       }
+
+       /**
+        *  Method for inserting a comment into the Database and delivering
+        *  the commentDone Page
+        */
+
+       public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
+       {
+               String aid = req.getParameter("to_media"); // the article id the comment will belong to
+               if (aid!=null && !aid.equals(""))
+               {
+                       // ok, collecting data from form
+                       try {
+                               HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
+                               withValues.put("is_published","1");
+
+                               // inserting into database
+                               String id = mainModule.add(withValues);
+
+                               // producing new page
+                               new ProducerContent().handle(null, null, true, false, aid);
+
+                               // sync the server
+                               int exitValue = Helper.rsync();
+
+                               // redirecting to url
+                               // should implement back to article
+                               SimpleHash mergeData = new SimpleHash();
+                               deliver(req, res, mergeData, commentFormDoneTemplate);
+                       }
+                       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
+                       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
+
+               }
+               else throw new ServletModuleException("aid not set!");
+
+       }
+
+       /**
+        *  Method for delivering the form-Page for open posting
+        */
+
+       public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
+       {
+               SimpleHash mergeData = new SimpleHash();
+               /** @todo popups missing */
+               deliver(req, res, mergeData, postingFormTemplate);
+       }
+
+       /**
+        *  Method for inserting an open posting into the Database and delivering
+        *  the postingDone Page
+        */
+
+       public void insposting(HttpServletRequest req, HttpServletResponse res)
+               throws ServletModuleException
+       {
+               SimpleHash mergeData = new SimpleHash();
+
+               try {
+
+                       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
+                       HashMap withValues = mp.getParameters();
+                       byte[] mediaData=mp.getMedia();
+                       String fileName=mp.getFilename();
+
+                       // if op contains imagedata
+                       String mediaId=null;
+                       if (mediaData!=null && fileName!=null) {
+                               HashMap mediaValues = new HashMap();
+                               mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
+                               mediaValues.put("to_publisher", "1"); // op user
+                               mediaValues.put("to_media_folder", "7"); // op media_folder
+                               mediaValues.put("is_produced", "0");
+                               mediaValues.put("is_published","1");
+
+                               String mediaTitle=(String)withValues.get("media_title");
+                               if (mediaTitle==null)
+                                       mediaTitle = (String)withValues.get("title");
+                               mediaValues.put("title",mediaTitle);
+
+                               if (fileName.toLowerCase().endsWith("rm")) {
+                                       // this is video !!
+                                       //theLog.printDebugInfo("--GOT VIDEO");
+                                       EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
+                                       entVideo.setValues(mediaValues);
+                                       mediaId = entVideo.insert();
+                                       entVideo.setVideoData(mediaData);
+                               }
+                               else if (fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".gif")) {
+                                       // this is image !!
+                                       mediaId = imageModule.add(mediaValues);
+                                       EntityImage entImage = (EntityImage)imageModule.getById(mediaId);
+
+                                       int fileType = -1;
+                                       if (fileName.toLowerCase().endsWith(".jpg")) fileType=0;
+                                       if (fileName.toLowerCase().endsWith(".gif")) fileType=1;
+                                       if (fileType>=0) {
+                                               entImage.setImage(mediaData, fileType);
+                                               withValues.put("to_media",mediaId);
+                                       }
+                                       else
+                                               theLog.printDebugInfo("Wrong file uploaded!" + fileName);
+                               }
+                       }
+
+                       withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
+                       withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
+                       withValues.put("is_produced", "0");
+                       // op-articles are immediatly published
+                       withValues.put("is_published","1");
+                       // owner is openposting user
+                       withValues.put("to_publisher","1");
+      if (withValues.get("creator").toString().equals(""))
+                               withValues.put("creator","Anonym");
+
+                       // inserting  content into database
+                       String id = contentModule.add(withValues);
+
+
+                       // producing new page
+                       if(mediaId!=null){
+                               new ProducerImages().handle(null, null, false, false, mediaId);
+                       }
+                       // producing openpostinglist
+                       new ProducerOpenPosting().handle(null,null,false,false);
+                       // producing new page
+                       new ProducerContent().handle(null, null, false, false,id);
+
+                       // sync the server
+                       int exitValue = Helper.rsync();
+
+               }
+               catch (IOException e) { throw new ServletModuleException(e.toString());}
+               catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
+               catch (ModuleException e) { throw new ServletModuleException(e.toString());}
+
+               deliver(req, res, mergeData, postingFormDoneTemplate);
+       }
+
+}
+