forgot to make ServletModuleImages use the setImage String.class hack bullshit..
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
index a2ccc2e..53694f2 100755 (executable)
@@ -3,6 +3,8 @@ package mircoders.servlet;
 import java.io.*;
 import java.sql.*;
 import java.util.*;
+import java.net.*;
+import java.lang.reflect.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 
@@ -15,6 +17,7 @@ import mir.module.*;
 import mir.misc.*;
 import mir.entity.*;
 import mir.storage.*;
+import mir.media.*;
 
 import mircoders.entity.*;
 import mircoders.storage.*;
@@ -33,10 +36,12 @@ import mircoders.producer.*;
 public class ServletModuleOpenIndy extends ServletModule
 {
 
-  private String          commentFormTemplate, commentFormDoneTemplate;
+  private String          commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
   private String          postingFormTemplate, postingFormDoneTemplate;
   private ModuleContent   contentModule;
-  private ModuleImages     imageModule;
+  private ModuleImages    imageModule;
+  private ModuleTopics         themenModule;
+  private String          directOp ="yes";
 
   // Singelton / Kontruktor
   private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
@@ -44,14 +49,16 @@ public class ServletModuleOpenIndy extends ServletModule
 
   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");
-
+      theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
+      commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
+      commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
+      commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate");
+      postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
+      postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
+      directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
       mainModule = new ModuleComment(DatabaseComment.getInstance());
       contentModule = new ModuleContent(DatabaseContent.getInstance());
+      themenModule = new ModuleTopics(DatabaseTopics.getInstance());
       imageModule = new ModuleImages(DatabaseImages.getInstance());
       defaultAction="addposting";
     }
@@ -93,19 +100,28 @@ public class ServletModuleOpenIndy extends ServletModule
         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);
+               // Dupe detection
+//             EntityList com = mainModule.getByWhereClause("description='"+req.getParameter("description")+"' AND to_media='"+aid+"' AND creator='"+req.getParameter("creator")+"'", 0);
+//             if (com.getCount() > 0) {
+//             theLog.printDebugInfo("Comment dupe dropped for article "+aid);
+//             deliver(req, res, (TemplateModelRoot) null, commentFormDupeTemplate);
+//             } else {
+
+               // 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();
+               theLog.printDebugInfo("rsync:"+exitValue);
+
+               // 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());}
@@ -122,6 +138,22 @@ public class ServletModuleOpenIndy extends ServletModule
   public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
   {
     SimpleHash mergeData = new SimpleHash();
+    String numOfMedia = req.getParameter("medianum");
+    if(numOfMedia==null||numOfMedia.equals("")){
+      numOfMedia="1";
+    }
+    
+    int mediaNum = Integer.parseInt(numOfMedia);
+    SimpleList mediaFields = new SimpleList();
+    for(int i =0; i<mediaNum;i++){
+      Integer mNum = new Integer(i+1);
+      mediaFields.add(mNum.toString());
+    }
+    mergeData.put("medianum",numOfMedia);
+    mergeData.put("mediafields",mediaFields);
+    mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
+    
+    
     /** @todo popups missing */
     try{
       mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
@@ -143,93 +175,177 @@ public class ServletModuleOpenIndy extends ServletModule
     boolean setMedia=false;
 
     try {
-
       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
+          
       HashMap withValues = mp.getParameters();
-      byte[] mediaData=mp.getMedia();
-      String fileName=mp.getFilename();
-      
-      theLog.printDebugInfo("ContentType: "+mp.getContentType());
-      
-      // 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);
-            setMedia=true;
-          }
-          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");
+      // if op direct article-type == newswire
+      if (directOp.equals("yes")) withValues.put("to_article_type","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);
-      
-      // inserting content and media id in table content_x_media
-      try{
-        DatabaseContentToMedia.getInstance().setMedia(id,mediaId);
-        theLog.printError("setting content_x_topic success");
-      } catch (Exception e) {
-        theLog.printError("setting content_x_topic failed");
-      }
+      String cid = contentModule.add(withValues);
+
+      String[] to_topicsArr = mp.getParameterValues("to_topic"); 
+      if (to_topicsArr != null && to_topicsArr.length > 0) {
+        try{
+          DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
+          theLog.printError("setting content_x_topic success");
+        } catch (Exception e) {
+          theLog.printError("setting content_x_topic failed");
+        } //end try
+      } //end if
+        
+      // if op contains uploaddata
+      String mediaId=null;
+      int i=1;
+      for(Iterator it = mp.requestList.iterator(); it.hasNext();){
+        MpRequest mpReq = (MpRequest)it.next();
+        String fileName = mpReq.getFilename();
+        String contentType = FileUtil.guessContentTypeFromName(fileName);
+        HashMap mediaValues = new HashMap();
 
+        theLog.printError("CONTENT TYPE IS: "+contentType);
 
-      // producing new page
-      if(mediaId!=null){
-        new ProducerImages().handle(null, null, false, false, mediaId);
+        if ((contentType==null) || (contentType=="application/octet-stream")) {
+          throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
+        }
+
+
+        String mediaTitle=(String)withValues.get("media_title"+i);
+        i++;
+
+        if (mediaTitle==null)
+            mediaTitle = (String)withValues.get("title");
+
+        mediaValues.put("title", mediaTitle);
+        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 wc = " mime_type='"+contentType+"'";
+
+        EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
+
+        String mediaTypeId = null;
+        String mediaStorageName = null;
+        String mediaHandlerName = null;
+  
+        if (mediaTypesList.size() > 0) {
+          mediaTypeId = mediaTypesList.elementAt(0).getId();
+          mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
+          mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
+          mediaValues.put("to_media_type",mediaTypeId);
+          
+          String MediaId;
+          try {
+                Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
+                Method m = mediaStorageClass.getMethod("getInstance", null);
+                Database mediaStorage = (Database)m.invoke(null, null);
+                Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
+                mediaEnt.setStorage(mediaStorage);
+                mediaEnt.setValues(mediaValues);
+                mediaId = mediaEnt.insert();
+
+                Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
+                MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
+                mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
+              if(mediaId!=null){
+                //new ProducerImages().handle(null, null, false, false, mediaId);
+              }
+          } catch (Exception e) {
+                theLog.printError("setting uploaded_media failed: "+e.toString());
+          }
+              
+
+          try{
+              DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
+              theLog.printError("setting content_x_media success");
+          } catch (Exception e) {
+              theLog.printError("setting content_x_media failed");
+          }
+
+        } else {
+          theLog.printDebugInfo("Wrong file uploaded!: " + fileName);
+          throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
+        }
+          
+     
+            
+
+          
+
+        //MirUploadedMedia Media = MirUploadedMedia(mpReq, cid); 
+                //mediaValues.put("title",mediaTitle);
+
+        //mediaId = Media.add();
+         /*   // this is video !!
+            //theLog.printDebugInfo("--GOT VIDEO");
+            EntityVideo entVideo = new EntityVideo(DatabaseVideos.getInstance());
+            //notice the discr here..... (imageModule).
+            entVideo.setValues(mediaValues);
+            mediaId = entVideo.insert();
+            entVideo.setVideoData(mediaData);
+          }
+          //try content type first then go by xtension?
+          else if (contentType.equals("image/jpeg") || contentType.equals("image/gif")) {
+            // this is image !!
+            mediaId = imageModule.add(mediaValues);
+            EntityImages entImage = (EntityImages)imageModule.getById(mediaId);
+  
+            int fileType = -1;
+
+            //replace all this with DatabaseContentMedia.getInst().getEn(contentType).set(mediaData) ??? w/ a catch for error, then return form w/ and error
+            if (contentType.equals("image/jpeg")) fileType=0;
+            if (contentType.equals("image/gif")) fileType=1;
+            if (fileType>=0) {
+              entImage.setImage(mediaData, fileType);
+              // inserting content and media id in table content_x_media
+              try{
+                DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
+                theLog.printError("setting content_x_media success");
+              } catch (Exception e) {
+                theLog.printError("setting content_x_media failed");
+              }
+              
+              // producing new page
+              if(mediaId!=null){
+                new ProducerImages().handle(null, null, false, false, mediaId);
+              }
+            } else {
+              theLog.printDebugInfo("Wrong file uploaded!" + fileName);
+            }
+          }
+        }*/
       }
+
       // producing openpostinglist
       new ProducerOpenPosting().handle(null,null,false,false);
       // producing new page
-      new ProducerContent().handle(null, null, false, false,id);
+      new ProducerContent().handle(null, null, false, false,cid);
+      //if direct op producing startpage
+      if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
+      
 
       // sync the server
       //should be configureable
       int exitValue = Helper.rsync();
+      theLog.printDebugInfo("rsync: "+exitValue);
 
     }
-    catch (IOException e) { throw new ServletModuleException(e.toString());}
-    catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
-    catch (ModuleException e) { throw new ServletModuleException(e.toString());}
+    catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
+    catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
+    catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
 
     deliver(req, res, mergeData, postingFormDoneTemplate);
   }