merge of localization branch into HEAD. mh and zap
[mir.git] / source / mircoders / servlet / ServletModuleComment.java
index dc5383d..57f225f 100755 (executable)
@@ -6,6 +6,7 @@ import java.util.*;
 import java.net.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
+import org.apache.struts.util.MessageResources;
 
 import freemarker.template.*;
 
@@ -14,9 +15,14 @@ import mir.module.*;
 import mir.misc.*;
 import mir.entity.*;
 import mir.storage.*;
-
+import mir.generator.*;
 import mir.entity.*;
+import mir.entity.adapter.*;
+import mir.util.*;
+
 import mircoders.storage.*;
+import mircoders.global.*;
+import mircoders.localizer.*;
 import mircoders.module.*;
 
 /*
@@ -29,98 +35,205 @@ import mircoders.module.*;
 public class ServletModuleComment extends ServletModule
 {
 
-       private ModuleContent     moduleContent;
-
-       // Singelton / Kontruktor
-       private static ServletModuleComment instance = new ServletModuleComment();
-       public static ServletModule getInstance() { return instance; }
-
-       private ServletModuleComment() {
-               theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Comment.Logfile"));
-               templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
-               templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
-               templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
-               try {
-                       mainModule = new ModuleComment(DatabaseComment.getInstance());
-                       moduleContent = new ModuleContent(DatabaseContent.getInstance());
-               }
-               catch (StorageObjectException e) {
-                       theLog.printError("servletmodule: comment could not be initialized");
-               }
-       }
-
-
-       public void list(HttpServletRequest req, HttpServletResponse res)
-               throws ServletModuleException
-       {
-                       // Parameter auswerten
-                       SimpleHash mergeData = new SimpleHash();
-                       String query_text = req.getParameter("query_text");
-                       mergeData.put("query_text",query_text);
-                       if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
-                       String query_field = req.getParameter("query_field");
-                       mergeData.put("query_field",query_field);
-                       String query_is_published = req.getParameter("query_is_published");
-                       mergeData.put("query_is_published",query_is_published);
-
-                       String offset = req.getParameter("offset");
-                       if (offset==null || offset.equals("")) offset="0";
-                       mergeData.put("offset",offset);
-
-                       // patching order
-                       String order = req.getParameter("order");
-                       if(order!=null) {
-                               mergeData.put("order", order);
-                               mergeData.put("order_encoded", URLEncoder.encode(order));
-                               if (order.equals("webdb_create")) order="webdb_create desc";
-                       }
-
-                       // sql basteln
-                       String whereClause=""; boolean isFirst=true;
-                       if (query_text!=null && !query_text.equalsIgnoreCase("")) {
-                               whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
-                       if (query_is_published != null && !query_is_published.equals("")) {
-                               if (isFirst==false) whereClause+=" and ";
-                               whereClause += "is_published='"+query_is_published+"'";
-                               isFirst=false;
-                       }
-
-                       theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
-
-                       // fetch und ausliefern
-                       try {
-
-                               if (query_text!=null || query_is_published!=null ) {
-                                       EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue());
-                                       if (theList!=null && theList.size()>0) {
-
-                                               //make articleHash for comment
-                                               StringBuffer buf= new StringBuffer("id in (");boolean first=true;
-                                               for(int i=0;i<theList.size();i++) {
-                                                       if (first==false) buf.append(",");
-                                                       first=false;
-                                                       buf.append(theList.elementAt(i).getValue("to_media"));
-                                               }
-                                               buf.append(")");
-                                               SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
-                                               mergeData.put("articleHash", articleHash);
-
-                                               // get comment
-                                               mergeData.put("contentlist",theList);
-                                               mergeData.put("count", (new Integer(theList.getCount())).toString());
-                                               mergeData.put("from", (new Integer(theList.getFrom())).toString());
-                                               mergeData.put("to", (new Integer(theList.getTo())).toString());
-                                               if (theList.hasNextBatch())
-                                                       mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
-                                               if (theList.hasPrevBatch())
-                                                       mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
-                                       }
-                               }
-                               // raus damit
-                               HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
-                       }
-                       catch (ModuleException e) {throw new ServletModuleException(e.toString());}
-                       catch (IOException e) {throw new ServletModuleException(e.toString());}
-                       catch (Exception e) {throw new ServletModuleException(e.toString());}
-       }
+  private ModuleContent     moduleContent;
+
+  // Singelton / Kontruktor
+  private static ServletModuleComment instance = new ServletModuleComment();
+  public static ServletModule getInstance() { return instance; }
+
+  private ServletModuleComment() {
+    theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Comment.Logfile"));
+    templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
+    templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
+    templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
+    try {
+      mainModule = new ModuleComment(DatabaseComment.getInstance());
+      moduleContent = new ModuleContent(DatabaseContent.getInstance());
+    }
+    catch (StorageObjectException e) {
+      theLog.printError("servletmodule: comment could not be initialized");
+    }
+  }
+
+  public void list(HttpServletRequest req, HttpServletResponse res)
+      throws ServletModuleException
+  {
+    // Parameter auswerten
+    SimpleHash mergeData = new SimpleHash();
+    String query_text = req.getParameter("query_text");
+    mergeData.put("query_text",query_text);
+    if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
+    String query_field = req.getParameter("query_field");
+    mergeData.put("query_field",query_field);
+    String query_is_published = req.getParameter("query_is_published");
+    mergeData.put("query_is_published",query_is_published);
+
+    String offset = req.getParameter("offset");
+    if (offset==null || offset.equals("")) offset="0";
+    mergeData.put("offset",offset);
+
+    // patching order
+    String order = req.getParameter("order");
+    if(order!=null) {
+      mergeData.put("order", order);
+      mergeData.put("order_encoded", URLEncoder.encode(order));
+      if (order.equals("webdb_create")) order="webdb_create desc";
+    }
+
+    // sql basteln
+    String whereClause=""; boolean isFirst=true;
+    if (query_text!=null && !query_text.equalsIgnoreCase("")) {
+    whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
+    if (query_is_published != null && !query_is_published.equals("")) {
+      if (isFirst==false) whereClause+=" and ";
+      whereClause += "is_published='"+query_is_published+"'";
+      isFirst=false;
+    }
+
+    theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
+
+    // fetch und ausliefern
+    try {
+
+      if (query_text!=null || query_is_published!=null ) {
+        EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue());
+        if (theList!=null && theList.size()>0) {
+
+          //make articleHash for comment
+          StringBuffer buf= new StringBuffer("id in (");boolean first=true;
+          for(int i=0;i<theList.size();i++) {
+            if (first==false) buf.append(",");
+            first=false;
+            buf.append(theList.elementAt(i).getValue("to_media"));
+          }
+          buf.append(")");
+          SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
+          mergeData.put("articleHash", articleHash);
+
+          // get comment
+          mergeData.put("contentlist",theList);
+          mergeData.put("count", (new Integer(theList.getCount())).toString());
+          mergeData.put("from", (new Integer(theList.getFrom())).toString());
+          mergeData.put("to", (new Integer(theList.getTo())).toString());
+          if (theList.hasNextBatch())
+            mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
+          if (theList.hasPrevBatch())
+            mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
+        }
+      }
+      // raus damit
+      HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
+    }
+    catch (ModuleException e) {throw new ServletModuleException(e.toString());}
+    catch (IOException e) {throw new ServletModuleException(e.toString());}
+    catch (Exception e) {throw new ServletModuleException(e.toString());}
+  }
+
+  public void showArticleCommentList(Object aWriter, int anOffset, int anArticleId, Locale aLocale) throws ServletModuleException {
+    int nrCommentsPerPage = 20;
+
+    Object comments;
+    Map generationData;
+    Generator generator;
+    int totalNrComments;
+    EntityAdapterModel model;
+    MessageResources messages = MessageResources.getMessageResources("bundles.admin");
+
+    try {
+      generator = MirGlobal.localizer().generators().makeGeneratorLibrary().makeGenerator("admin/commentlist2.template");
+      model = MirGlobal.localizer().dataModel().adapterModel();
+
+//    commentList = mainModule.getByWhereClause(whereClause, order, offset);
+
+      generationData = new HashMap();
+      MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
+
+      comments =
+        new CachingRewindableIterator(
+          new EntityIteratorAdapter( "to_media = " + anArticleId,
+               "webdb_create desc",
+               nrCommentsPerPage,
+               model,
+              "comment",
+              nrCommentsPerPage,
+              anOffset
+          )
+        );
+
+      totalNrComments = model.getMappingForName("comment").getStorage().getSize("to_media = " + anArticleId);
+
+      generationData.put( "comments", comments);
+      generationData.put( "offset", new Integer(anOffset));
+      generationData.put( "articleid", new Integer(anArticleId));
+      generationData.put( "lang", new MessageMethodModel(aLocale, messages) );
+      generationData.put( "thisurl", "module=Comment&do=listarticlecomments&offset="+anOffset+"&articleid="+anArticleId);
+
+      if (anOffset>0) {
+        generationData.put( "previousurl", "module=Comment&do=listarticlecomments&offset="+
+                            Math.max( 0, anOffset - nrCommentsPerPage )+"&articleid="+anArticleId);
+        generationData.put("previous", new Integer(Math.max( 0, anOffset - nrCommentsPerPage )));
+      }
+
+      if (anOffset + nrCommentsPerPage < totalNrComments) {
+        generationData.put( "nexturl", "module=Comment&do=listarticlecomments&offset="+
+                            Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )+"&articleid="+anArticleId);
+        generationData.put("next", new Integer(Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )));
+      }
+
+      generator.generate(aWriter, generationData, new PrintWriter(new NullWriter()));
+    }
+    catch (Throwable t) {
+      t.printStackTrace(System.out);
+      throw new ServletModuleException(t.getMessage());
+    }
+  }
+
+  public void listarticlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
+  {
+    String articleIdString = req.getParameter("articleid");
+    String offsetString = req.getParameter("offset");
+    int offset = 0;
+    int articleId = 0;
+
+    try {
+      offset = Integer.parseInt(offsetString);
+    }
+    catch (Throwable t) {
+    }
+
+    try {
+      articleId  = Integer.parseInt(articleIdString);
+
+      showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
+    }
+    catch (ServletModuleException e) {
+      throw e;
+    }
+    catch (Throwable e) {
+      e.printStackTrace(System.out);
+      throw new ServletModuleException(e.getMessage());
+    }
+  }
+
+  public void performarticlecommentsoperation(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
+    String commentIdString = req.getParameter("commentid");
+    String articleIdString = req.getParameter("articleid");
+    String offsetString = req.getParameter("offset");
+    String operation = req.getParameter("operation");
+    int offset = 0;
+    int articleId = 0;
+
+    try {
+      articleId  = Integer.parseInt(articleIdString);
+
+      showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
+    }
+    catch (ServletModuleException e) {
+      throw e;
+    }
+    catch (Throwable e) {
+      e.printStackTrace(System.out);
+      throw new ServletModuleException(e.getMessage());
+    }
+  }
 }