very basic support for localized add/edit article pages
[mir.git] / source / mircoders / servlet / ServletModuleComment.java
index 74089d3..be28e74 100755 (executable)
@@ -32,8 +32,7 @@ package mircoders.servlet;
 
 import mir.entity.adapter.EntityAdapterEngine;
 import mir.entity.adapter.EntityAdapterModel;
-import mir.log.LoggerWrapper;
-import mir.servlet.ServletModule;
+import mir.servlet.AdminServletModule;
 import mir.servlet.ServletModuleExc;
 import mir.servlet.ServletModuleFailure;
 import mir.servlet.ServletModuleUserExc;
@@ -48,29 +47,20 @@ import mircoders.storage.DatabaseComment;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.util.*;
-
-/*
- *  ServletModuleComment - controls navigation for Comments
- *
- *
- *  @author the mir-coders
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * ServletModule for managing Comments
  */
 
-public class ServletModuleComment extends ServletModule
-{
-  private static ServletModuleComment instance = new ServletModuleComment();
-  public static ServletModule getInstance() { return instance; }
-
-  private ServletModuleComment() {
-    logger = new LoggerWrapper("ServletModule.Comment");
-    try {
-      mainModule = new ModuleComment();
-      definition = "comment";
-    }
-    catch (Exception e) {
-      logger.error("servletmodule comment could not be initialized:" + e.getMessage());
-    }
+public class ServletModuleComment extends AdminServletModule {
+  public ServletModuleComment() {
+    mainModule = new ModuleComment();
+    definition = "comment";
   }
 
   public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
@@ -84,22 +74,22 @@ public class ServletModuleComment extends ServletModule
     }
   }
 
-  public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
-  {
+  public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
     String idParam = aRequest.getParameter("id");
 
-    if (idParam == null)
+    if (idParam == null) {
       throw new ServletModuleExc("Invalid call: id not supplied ");
+    }
 
-    editObject(aRequest, aResponse, idParam);
+    editObject(aRequest, aResponse, idParam, null);
   }
 
-  public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc {
+  public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId, String aTemplate) throws ServletModuleExc {
     try {
       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
       EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
-      Map comment;
+      Object comment;
       URLBuilder urlBuilder = new URLBuilder();
 
       urlBuilder.setValue("module", "Comment");
@@ -114,11 +104,13 @@ public class ServletModuleComment extends ServletModule
       else {
         List fields = DatabaseComment.getInstance().getFieldNames();
         responseData.put("new", Boolean.TRUE);
-        comment = new HashMap();
+        Map emptyComment = new HashMap();
         Iterator i = fields.iterator();
         while (i.hasNext()) {
-          comment.put(i.next(), null);
+          emptyComment.put(i.next(), null);
         }
+
+        comment = emptyComment;
       }
       responseData.put("comment", comment);
 
@@ -132,12 +124,13 @@ public class ServletModuleComment extends ServletModule
     }
   }
 
-  public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
-  {
+  public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
     String  mediaIdParam = aRequest.getParameter("mid");
     String  commentId = aRequest.getParameter("commentid");
 
-    if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
+    if (commentId == null || mediaIdParam==null) {
+      throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
+    }
 
     try {
       EntityComment comment = (EntityComment) mainModule.getById(commentId);
@@ -149,17 +142,18 @@ public class ServletModuleComment extends ServletModule
 
     logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
 
-    editObject(aRequest, aResponse, commentId);
+    editObject(aRequest, aResponse, commentId, null);
   }
 
-  public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
-  {
+  public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
     String  commentId = aRequest.getParameter("commentid");
     String  midParam = aRequest.getParameter("mid");
-    if (commentId == null)
+    if (commentId == null) {
       throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
-    if (midParam == null)
+    }
+    if (midParam == null) {
       throw new ServletModuleExc("smod comment :: dettach :: mid missing");
+    }
 
     try {
       EntityComment comment = (EntityComment)mainModule.getById(commentId);
@@ -171,7 +165,7 @@ public class ServletModuleComment extends ServletModule
 
     logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
 
-    editObject(aRequest, aResponse, commentId);
+    editObject(aRequest, aResponse, commentId, null);
   }
 
 
@@ -197,10 +191,10 @@ public class ServletModuleComment extends ServletModule
     String searchStatus = requestParser.getParameter("searchstatus");
     String searchOrder = requestParser.getParameter("searchorder");
 
-    if (searchIsPublished.equals("0")) {
+    if ("0".equals(searchIsPublished)) {
       queryBuilder.appendAndCondition("is_published='f'");
     }
-    else if (searchIsPublished.equals("1")) {
+    else if ("1".equals(searchIsPublished)) {
       queryBuilder.appendAndCondition("is_published='t'");
     }
 
@@ -215,26 +209,27 @@ public class ServletModuleComment extends ServletModule
     }
 
     if (searchOrder.length()>0) {
-      if (searchOrder.equals("datedesc"))
+      if ("datedesc".equals(searchOrder)) {
         queryBuilder.appendDescendingOrder("webdb_create");
-      else if (searchOrder.equals("dateasc"))
+      }
+      else if ("dateasc".equals(searchOrder)) {
         queryBuilder.appendAscendingOrder("webdb_create");
-      else if (searchOrder.equals("articletitle"))
+      }
+      else if ("articletitle".equals(searchOrder)) {
         queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
-      else if (searchOrder.equals("creator"))
+      }
+      else if ("creator".equals(searchOrder)) {
         queryBuilder.appendDescendingOrder("creator");
+      }
     }
 
     returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
   }
 
-  public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
-  {
+  public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
     String articleIdString = aRequest.getParameter("articleid");
-    int articleId;
-
     try {
-      articleId  = Integer.parseInt(articleIdString);
+      int articleId  = Integer.parseInt(articleIdString);
 
       returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
     }
@@ -253,7 +248,8 @@ public class ServletModuleComment extends ServletModule
       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
 
       List commentList =
-         EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset);
+         EntityAdapterEngine.retrieveAdapterList(getModel(), definition, aWhereClause,
+             anOrderByClause, nrEntitiesPerListPage, anOffset);
 
       responseData.put("nexturl", null);
       responseData.put("prevurl", null);
@@ -276,7 +272,7 @@ public class ServletModuleComment extends ServletModule
       responseData.put("searchorder", requestParser.getParameter("searchorder"));
 
       urlBuilder.setValue("offset", anOffset);
-      responseData.put("offset" , new Integer(anOffset).toString());
+      responseData.put("offset" , Integer.toString(anOffset));
       responseData.put("thisurl" , urlBuilder.getQuery());
 
       if (commentList.size()>=nrEntitiesPerListPage) {
@@ -308,19 +304,23 @@ public class ServletModuleComment extends ServletModule
       String returnUrl = requestParser.getParameter("returnurl");
 
       String idParam = aRequest.getParameter("id");
-      if (idParam == null)
+      if (idParam == null) {
         throw new ServletModuleExc("Wrong call: (id) is missing");
+      }
 
       Map withValues = getIntersectingValues(aRequest, DatabaseComment.getInstance());
 
-      if (!withValues.containsKey("is_published"))
+      if (!withValues.containsKey("is_published")) {
         withValues.put("is_published","0");
-      if (!withValues.containsKey("is_html"))
+      }
+      if (!withValues.containsKey("is_html")) {
         withValues.put("is_html","0");
+      }
 
       String webdbCreate = (String) withValues.get("webdb_create");
-      if (webdbCreate==null || webdbCreate.trim().length()==0)
+      if (webdbCreate==null || webdbCreate.trim().length()==0) {
         withValues.remove("webdb_create");
+      }
 
       String id = mainModule.set(withValues);
 
@@ -329,8 +329,9 @@ public class ServletModuleComment extends ServletModule
       if (returnUrl!=null){
         ServletHelper.redirect(aResponse, returnUrl);
       }
-      else
-        editObject(aRequest, aResponse, idParam);
+      else {
+        editObject(aRequest, aResponse, idParam, null);
+      }
     }
     catch (Throwable e) {
       throw new ServletModuleFailure(e);