Initial revision
[mir.git] / source / mircoders / servlet / ServletModuleComment.java
diff --git a/source/mircoders/servlet/ServletModuleComment.java b/source/mircoders/servlet/ServletModuleComment.java
new file mode 100755 (executable)
index 0000000..3c1c6e4
--- /dev/null
@@ -0,0 +1,126 @@
+package mircoders.servlet;
+
+import java.io.*;
+import java.sql.*;
+import java.util.*;
+import java.net.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import freemarker.template.*;
+
+import webdb.servlet.*;
+import webdb.module.*;
+import webdb.misc.*;
+import webdb.entity.*;
+import webdb.storage.*;
+
+import mir.entity.*;
+import mir.storage.*;
+import mir.module.*;
+
+/*
+ *  ServletModuleComment - controls navigation for Comments
+ *
+ *
+ * @author RK
+ */
+
+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(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.Comment.Logfile"));
+               templateListString = Configuration.getProperty("ServletModule.Comment.ListTemplate");
+               templateObjektString = Configuration.getProperty("ServletModule.Comment.ObjektTemplate");
+               templateConfirmString = Configuration.getProperty("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) {
+
+                                               //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",HTMLTemplateProcessor.makeSimpleList(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(getLanguage(req)+"/"+templateListString, mergeData, res.getWriter());
+                       }
+                       catch (ModuleException e) {throw new ServletModuleException(e.toString());}
+                       catch (IOException e) {throw new ServletModuleException(e.toString());}
+                       catch (Exception e) {throw new ServletModuleException(e.toString());}
+       }
+}