merge of localization branch into HEAD. mh and zap
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
index 2b8e463..b244ac9 100755 (executable)
@@ -34,6 +34,8 @@ import mircoders.storage.*;
 import mircoders.module.*;
 import mircoders.producer.*;
 import mircoders.media.MediaRequest;
+import mircoders.global.*;
+import mircoders.localizer.*;
 
 /*
  *  ServletModuleOpenIndy -
@@ -42,9 +44,12 @@ import mircoders.media.MediaRequest;
  *    open-postings to the newswire
  *
  * @author $Author: mh $
- * @version $Revision: 1.38 $ $Date: 2002/07/21 22:50:14 $
+ * @version $Revision: 1.39 $ $Date: 2002/08/25 19:00:11 $
  *
  * $Log: ServletModuleOpenIndy.java,v $
+ * Revision 1.39  2002/08/25 19:00:11  mh
+ * merge of localization branch into HEAD. mh and zap
+ *
  * Revision 1.38  2002/07/21 22:50:14  mh
  * cleanup coding style of getpdf() method
  *
@@ -65,6 +70,7 @@ public class ServletModuleOpenIndy extends ServletModule
   private String        postingFormTemplate, postingFormDoneTemplate,
                         postingFormDupeTemplate;
   private ModuleContent contentModule;
+  private ModuleComment commentModule;
   private ModuleImages  imageModule;
   private ModuleTopics  themenModule;
   private String        directOp ="yes";
@@ -83,13 +89,13 @@ public class ServletModuleOpenIndy extends ServletModule
       postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
       postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate");
       directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
-                       passwdProtection = MirConfig.getProp("PasswdProtection").toLowerCase();
+      passwdProtection = MirConfig.getProp("PasswdProtection").toLowerCase();
       mainModule = new ModuleComment(DatabaseComment.getInstance());
       contentModule = new ModuleContent(DatabaseContent.getInstance());
       themenModule = new ModuleTopics(DatabaseTopics.getInstance());
       imageModule = new ModuleImages(DatabaseImages.getInstance());
       defaultAction="addposting";
-                       
+
     }
     catch (StorageObjectException e) {
         theLog.printError("servletmoduleopenindy could not be initialized");
@@ -104,19 +110,27 @@ public class ServletModuleOpenIndy extends ServletModule
   public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
   {
     String aid = req.getParameter("aid"); // the article id the comment will belong to
+    String language = req.getParameter("language");
+
     if (aid!=null && !aid.equals(""))
     {
-                       SimpleHash mergeData = new SimpleHash();
-
-                       // onetimepasswd
-                       if(passwdProtection.equals("yes")){
-                               String passwd = this.createOneTimePasswd();
-                               System.out.println(passwd);
-                               HttpSession session = req.getSession(false);
-                               session.setAttribute("passwd",passwd);
-                               mergeData.put("passwd", passwd);
-                       }
-                       
+      SimpleHash mergeData = new SimpleHash();
+
+      // onetimepasswd
+      if(passwdProtection.equals("yes")){
+        String passwd = this.createOneTimePasswd();
+        System.out.println(passwd);
+        HttpSession session = req.getSession(false);
+        session.setAttribute("passwd",passwd);
+        mergeData.put("passwd", passwd);
+      }
+
+      if (language!=null) {
+        HttpSession session = req.getSession(false);
+        session.setAttribute("Locale", new Locale(language, ""));
+        session.setAttribute("passwd",language);
+      }
+
       mergeData.put("aid", aid);
       deliver(req, res, mergeData, commentFormTemplate);
     }
@@ -129,7 +143,7 @@ public class ServletModuleOpenIndy extends ServletModule
    */
 
   public void inscomment(HttpServletRequest req, HttpServletResponse res)
-       throws ServletModuleException,ServletModuleUserException
+  throws ServletModuleException,ServletModuleUserException
   {
     String aid = req.getParameter("to_media"); // the article id the comment will belong to
     if (aid!=null && !aid.equals(""))
@@ -137,44 +151,50 @@ public class ServletModuleOpenIndy extends ServletModule
       // ok, collecting data from form
       try {
         HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
-       
+
         //no html in comments(for now)
         for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
             String k=(String)i.next();
             String v=(String)withValues.get(k);
-            
+
             withValues.put(k,StringUtil.removeHTMLTags(v));
         }
         withValues.put("is_published","1");
-                               
-                               //checking the onetimepasswd
-                               if(passwdProtection.equals("yes")){
-                                       HttpSession session = req.getSession(false);
-                                       String sessionPasswd = (String)session.getAttribute("passwd");
-                                       if ( sessionPasswd == null){
-                                               throw new ServletModuleUserException("Lost password");
-                                       }
-                                       String passwd = req.getParameter("passwd");
-                                       if ( passwd == null || (!sessionPasswd.equals(passwd))) {
-                                               throw new ServletModuleUserException("Missing password");
-                                       }
-                                       session.invalidate();
-                               }
-                               
+
+        //checking the onetimepasswd
+        if(passwdProtection.equals("yes")){
+          HttpSession session = req.getSession(false);
+          String sessionPasswd = (String)session.getAttribute("passwd");
+          if ( sessionPasswd == null){
+            throw new ServletModuleUserException("Lost password");
+          }
+          String passwd = req.getParameter("passwd");
+          if ( passwd == null || (!sessionPasswd.equals(passwd))) {
+            throw new ServletModuleUserException("Missing password");
+          }
+          session.invalidate();
+        }
+
         // inserting into database
         String id = mainModule.add(withValues);
         theLog.printDebugInfo("id: "+id);
         //insert was not successfull
         if(id==null){
           deliver(req, res, new SimpleHash(), commentFormDupeTemplate);
+        } else {
+          DatabaseContent.getInstance().setUnproduced("id="+aid);
+
+          try {
+            EntityComment comment = (EntityComment) DatabaseComment.getInstance().selectById(id);
+            MirGlobal.localizer().openPostings().afterCommentPosting(comment);
+          }
+          catch (Throwable t) {
+            throw new ServletModuleException(t.getMessage());
+          }
+          
+          
+          
         }
-        
-        // 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
@@ -196,16 +216,16 @@ public class ServletModuleOpenIndy extends ServletModule
   public void addposting(HttpServletRequest req, HttpServletResponse res)
     throws ServletModuleException {
     SimpleHash mergeData = new SimpleHash();
-               
-               // onetimepasswd
-               if(passwdProtection.equals("yes")){
-                       String passwd = this.createOneTimePasswd();
-                       System.out.println(passwd);
-                       HttpSession session = req.getSession(false);
-                       session.setAttribute("passwd",passwd);
-                       mergeData.put("passwd", passwd);
-               }
-                       
+
+    // onetimepasswd
+    if(passwdProtection.equals("yes")){
+      String passwd = this.createOneTimePasswd();
+      System.out.println(passwd);
+      HttpSession session = req.getSession(false);
+      session.setAttribute("passwd",passwd);
+      mergeData.put("passwd", passwd);
+    }
+
     String maxMedia = MirConfig.getProp("ServletModule.OpenIndy.MaxMediaUploadItems");
     String numOfMedia = req.getParameter("medianum");
     if(numOfMedia==null||numOfMedia.equals("")){
@@ -213,7 +233,7 @@ public class ServletModuleOpenIndy extends ServletModule
     } else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
       numOfMedia = maxMedia;
     }
-    
+
     int mediaNum = Integer.parseInt(numOfMedia);
     SimpleList mediaFields = new SimpleList();
     for(int i =0; i<mediaNum;i++){
@@ -222,19 +242,24 @@ public class ServletModuleOpenIndy extends ServletModule
     }
     mergeData.put("medianum",numOfMedia);
     mergeData.put("mediafields",mediaFields);
-    
-    
+
+
     SimpleHash extraInfo = new SimpleHash();
     try{
       SimpleList popUpData = DatabaseLanguage.getInstance().getPopupData();
       extraInfo.put("languagePopUpData", popUpData );
       extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
+
+      extraInfo.put("topics", themenModule.getTopicsList());
+
     } catch (Exception e) {
       theLog.printError("languagePopUpData or getTopicslist failed "
                         +e.toString());
       throw new ServletModuleException("OpenIndy -- failed getting language or topics: "+e.toString());
     }
-      
+
+
+
     deliver(req, res, mergeData, extraInfo, postingFormTemplate);
   }
 
@@ -248,11 +273,10 @@ public class ServletModuleOpenIndy extends ServletModule
   {
     SimpleHash mergeData = new SimpleHash();
     boolean setMedia=false;
-               boolean setTopic = false;
+    boolean setTopic = false;
 
     try {
       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
-
       EntityList mediaList = null;
       try {
         // new MediaRequest, "1" is the id for the openPosting user
@@ -262,32 +286,32 @@ public class ServletModuleOpenIndy extends ServletModule
       }
           
       HashMap withValues = mp.getParameters();
-                                                       
-                       //checking the onetimepasswd
-                       if(passwdProtection.equals("yes")){
-                               HttpSession session = req.getSession(false);
-                               String sessionPasswd = (String)session.getAttribute("passwd");
-                               if ( sessionPasswd == null){
-                                       throw new ServletModuleUserException("Lost password");
-                               }
-                               String passwd = (String)withValues.get("passwd");
-                               if ( passwd == null || (!sessionPasswd.equals(passwd))) {
-                                       throw new ServletModuleUserException("Missing password");
-                               }
-                               session.invalidate();
-                       }
+
+      //checking the onetimepasswd
+      if(passwdProtection.equals("yes")){
+        HttpSession session = req.getSession(false);
+        String sessionPasswd = (String)session.getAttribute("passwd");
+        if ( sessionPasswd == null){
+          throw new ServletModuleUserException("Lost password");
+        }
+        String passwd = (String)withValues.get("passwd");
+        if ( passwd == null || (!sessionPasswd.equals(passwd))) {
+          throw new ServletModuleUserException("Missing password");
+        }
+        session.invalidate();
+      }
 
       if ((((String)withValues.get("title")).length() == 0) ||
           (((String)withValues.get("description")).length() == 0) ||
           (((String)withValues.get("content_data")).length() == 0))
             throw new ServletModuleUserException("Missing field");
-      
+
       // call the routines that escape html
 
       for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
         String k=(String)i.next();
         String v=(String)withValues.get(k);
-        
+
         if (k.equals("content_data")){
           //this doesn't quite work yet, so for now, all html goes
           //withValues.put(k,StringUtil.approveHTMLTags(v));
@@ -295,7 +319,7 @@ public class ServletModuleOpenIndy extends ServletModule
         } else {
           withValues.put(k,StringUtil.removeHTMLTags(v));
         }
-        
+
       }
 
       withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
@@ -306,11 +330,13 @@ public class ServletModuleOpenIndy extends ServletModule
       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");
+
+      // owner is openposting user
+//      ML: this is not multi-language friendly and this can be done in a template
+//      if (withValues.get("creator").toString().equals(""))
+//        withValues.put("creator","Anonym");
 
       // inserting  content into database
       String cid = contentModule.add(withValues);
@@ -327,8 +353,8 @@ public class ServletModuleOpenIndy extends ServletModule
       }
 
       String[] to_topicsArr = mp.getParameterValues("to_topic");
-      
-                       if (to_topicsArr != null && to_topicsArr.length > 0) {
+
+      if (to_topicsArr != null && to_topicsArr.length > 0) {
         try{
           DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
           setTopic = true;
@@ -345,22 +371,13 @@ public class ServletModuleOpenIndy extends ServletModule
         DatabaseContentToMedia.getInstance().addMedia(cid,mediaEnt.getId());
       }
 
-      // producing openpostinglist
-      new ProducerOpenPosting().handle(null,null,false,false);
-      // producing new page
-      new ProducerContent().handle(null, null, false, false,cid);
-      //if direct op producing startpage
-      if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
-      
-                       //produce the topicPages if set
-                       //should be more intelligent
-                       //if(setTopic==true) new ProducerTopics().handle(null,null);
-                       
-      // sync the server
-      //should be configureable
-      int exitValue = Helper.rsync();
-      theLog.printDebugInfo("rsync: "+exitValue);
-
+      try {
+        MirGlobal.localizer().openPostings().afterContentPosting(
+            (EntityContent)contentModule.getById(cid));
+      }
+      catch (Throwable t) {
+        throw new ServletModuleException(t.getMessage());
+      }
     }
     catch (MirMediaException e) { throw new ServletModuleException("MediaException: "+ e.toString());}
     catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
@@ -439,17 +456,17 @@ public class ServletModuleOpenIndy extends ServletModule
         +", we do not support this mime-type. "
         +"Error One or more files of unrecognized type. Sorry");
   }
-       
-       protected String createOneTimePasswd(){
-               Random r = new Random();
-               int random = r.nextInt();
-               long l = System.currentTimeMillis();
-               l = (l*l*l*l)/random;
-               if(l<0) l = l * -1;
-               String returnString = ""+l;
-               return returnString.substring(5);
-       }
-      
+
+  protected String createOneTimePasswd(){
+    Random r = new Random();
+    int random = r.nextInt();
+    long l = System.currentTimeMillis();
+    l = (l*l*l*l)/random;
+    if(l<0) l = l * -1;
+    String returnString = ""+l;
+    return returnString.substring(5);
+  }
+
 }