- icons in comment list
[mir.git] / source / mircoders / servlet / ServletModuleLocalizer.java
index 26c3e4b..4944f23 100755 (executable)
  */
 package mircoders.servlet;
 
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Vector;\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.servlet.http.HttpServletResponse;\r
-\r
-import mir.entity.adapter.EntityAdapter;\r
-import mir.log.LoggerWrapper;\r
-import mir.servlet.ServletModule;\r
-import mir.servlet.ServletModuleExc;\r
-import mir.util.StringRoutines;\r
-import mircoders.entity.EntityComment;\r
-import mircoders.entity.EntityContent;\r
-import mircoders.entity.EntityUsers;\r
-import mircoders.global.MirGlobal;\r
-import mircoders.localizer.MirAdminInterfaceLocalizer;\r
-import mircoders.module.ModuleComment;\r
-import mircoders.module.ModuleContent;\r
-import mircoders.storage.DatabaseComment;\r
-import mircoders.storage.DatabaseContent;
-
-public class ServletModuleLocalizer extends ServletModule {
-  private static ServletModuleLocalizer instance = new ServletModuleLocalizer();
-  public static ServletModule getInstance() { return instance; }
-
-  private ModuleContent contentModule;
-  private ModuleComment commentModule;
-  private List administerOperations;
-
-  private ServletModuleLocalizer() {
+import mir.servlet.AdminServletModule;
+import mir.servlet.ServletModuleExc;
+import mir.util.StringRoutines;
+import mircoders.entity.EntityComment;
+import mircoders.entity.EntityContent;
+import mircoders.global.MirGlobal;
+import mircoders.module.ModuleComment;
+import mircoders.module.ModuleContent;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ServletModuleLocalizer extends AdminServletModule {
+  private final ModuleContent contentModule = new ModuleContent();
+  private final ModuleComment commentModule = new ModuleComment();
+  private final List administerOperations = new ArrayList();
+
+  public ServletModuleLocalizer() {
     try {
-      logger = new LoggerWrapper("ServletModule.Localizer");
-
-      contentModule = new ModuleContent(DatabaseContent.getInstance());
-      commentModule = new ModuleComment(DatabaseComment.getInstance());
-
-      administerOperations = new Vector();
-
-      String settings[] = configuration.getStringArray("Mir.Localizer.Admin.AdministerOperations");
+      String settings[] = 
+          getConfiguration().getStringArray("Mir.Localizer.Admin.AdministerOperations");
 
       if (settings!=null) {
         for (int i = 0; i < settings.length; i++) {
@@ -77,12 +62,12 @@ public class ServletModuleLocalizer extends ServletModule {
           if (setting.length() > 0) {
             List parts = StringRoutines.splitString(setting, ":");
             if (parts.size() != 2) {
-              logger.error("config error: " + settings[i] + ", 2 parts expected");
+              getLogger().error("config error: " + settings[i] + ", 2 parts expected");
             }
             else {
               Map entry = new HashMap();
-              entry.put("name", (String) parts.get(0));
-              entry.put("url", (String) parts.get(1));
+              entry.put("name", parts.get(0));
+              entry.put("url", parts.get(1));
               administerOperations.add(entry);
             }
           }
@@ -90,30 +75,35 @@ public class ServletModuleLocalizer extends ServletModule {
       }
     }
     catch (Exception e) {
-      logger.error("ServletModuleLocalizer could not be initialized: " + e.getMessage());
+      getLogger().error("ServletModuleLocalizer could not be initialized" + e.getMessage(), e);
     }
-
-
   }
 
-  public void performCommentOperation(EntityUsers aUser, String anId, String anOperation) {
-    MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
-    EntityAdapter comment;
-    EntityComment entity;
-
+  /**
+   * Performs a localizer operation on an article.
+   *
+   * See also
+   * {@link mircoders.localizer.MirAdminInterfaceLocalizer#simpleArticleOperations()}
+   *
+   * @param aRequest       The originating request
+   * @param anId           The id of the article
+   * @param anOperation    The identifier of the operation to perform
+   */
+  public void performCommentOperation(HttpServletRequest aRequest, String anId, String anOperation) {
     try {
-      entity = (EntityComment) commentModule.getById(anId);
+      EntityComment entity = (EntityComment) commentModule.getById(anId);
 
       if (entity != null) {
-        MirGlobal.performCommentOperation(aUser, entity, anOperation);
-        logger.info("Operation " + anOperation + " successfully performed on comment " + anId);
+        MirGlobal.performCommentOperation(ServletHelper.getUser(aRequest), entity, anOperation);
+        getLogger().debug("Operation " + anOperation + " successfully performed on comment " + anId);
+        logAdminUsage(aRequest, "comment."+anId, "operation " + anOperation + " performed");
       }
       else {
-        logger.error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");
+        getLogger().error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");
       }
     }
     catch (Throwable e) {
-      logger.error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage());
+      getLogger().error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage(), e);
     }
   }
 
@@ -122,9 +112,26 @@ public class ServletModuleLocalizer extends ServletModule {
     String operationString = aRequest.getParameter("operation");
     String returnUrlString = aRequest.getParameter("returnurl");
 
-    performCommentOperation(ServletHelper.getUser(aRequest), commentIdString, operationString);
+    performCommentOperation(aRequest, commentIdString, operationString);
+
+    ServletHelper.redirect(aResponse, returnUrlString);
+  }
+
+  public void operation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
+    String type = aRequest.getParameter("objecttype");
+    String id = aRequest.getParameter("id");
+    String operation = aRequest.getParameter("operation");
+    String returnUrl = aRequest.getParameter("returnurl");
 
-    redirect(aResponse, returnUrlString);
+
+    if ("comment".equals(type)) {
+      performCommentOperation(aRequest, id, operation);
+    }
+    else {
+      performArticleOperation(aRequest, id, operation);
+    }
+
+    ServletHelper.redirect(aResponse, returnUrl);
   }
 
   public void commentoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
@@ -138,40 +145,71 @@ public class ServletModuleLocalizer extends ServletModule {
           List parts = StringRoutines.splitString(operations[i], ";");
 
           if (parts.size() != 2) {
-            logger.error("commentoperationbatch: operation string invalid: " +
+            getLogger().error("commentoperationbatch: operation string invalid: " +
                          operations[i]);
           }
           else {
             String commentIdString = (String) parts.get(0);
             String operationString = (String) parts.get(1);
 
-            performCommentOperation(ServletHelper.getUser(aRequest), commentIdString, operationString);
+            performCommentOperation(aRequest, commentIdString, operationString);
           }
         }
       }
     }
 
-    redirect(aResponse, returnUrlString);
+    ServletHelper.redirect(aResponse, returnUrlString);
   }
 
-  public void performArticleOperation(EntityUsers aUser, String anId, String anOperation) {
-    MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
-    EntityAdapter article;
+  public void operationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
+    String returnUrlString = aRequest.getParameter("returnurl");
+
+    String[] operations = aRequest.getParameterValues("operation");
+
+    if (operations!=null) {
+      for (int i = 0; i < operations.length; i++) {
+        if (operations[i].length() > 0) {
+          List parts = StringRoutines.splitString(operations[i], ";");
+
+          if (parts.size() != 3) {
+            getLogger().error("commentoperationbatch: operation string invalid: " +
+                         operations[i]);
+          }
+          else {
+            String type = (String) parts.get(0);
+            String id = (String) parts.get(1);
+            String operationString = (String) parts.get(2);
+
+            if ("comment".equals(type)) {
+              performCommentOperation(aRequest, id, operationString);
+            }
+            else {
+              performArticleOperation(aRequest, id, operationString);
+            }
+          }
+        }
+      }
+    }
+
+    ServletHelper.redirect(aResponse, returnUrlString);
+  }
+  public void performArticleOperation(HttpServletRequest aRequest, String anId, String anOperation) {
     EntityContent entity;
 
     try {
       entity = (EntityContent) contentModule.getById(anId);
 
       if (entity != null) {
-        MirGlobal.performArticleOperation(aUser, entity, anOperation);
-        logger.info("Operation " + anOperation + " successfully performed on article " + anId);
+        MirGlobal.performArticleOperation(ServletHelper.getUser(aRequest), entity, anOperation);
+        getLogger().info("Operation " + anOperation + " successfully performed on article " + anId);
+        logAdminUsage(aRequest, "article." + anId, "operation " + anOperation + " performed");
       }
       else {
-        logger.error("Error while performing " + anOperation + " on article " + anId + ": article is null");
+        getLogger().error("Error while performing " + anOperation + " on article " + anId + ": article is null");
       }
     }
     catch (Throwable e) {
-      logger.error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());
+      getLogger().error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());
     }
   }
 
@@ -180,8 +218,8 @@ public class ServletModuleLocalizer extends ServletModule {
     String operationString = aRequest.getParameter("operation");
     String returnUrlString = aRequest.getParameter("returnurl");
 
-    performArticleOperation(ServletHelper.getUser(aRequest), articleIdString, operationString);
-    redirect(aResponse, returnUrlString);
+    performArticleOperation(aRequest, articleIdString, operationString);
+    ServletHelper.redirect(aResponse, returnUrlString);
   }
 
   public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
@@ -196,19 +234,19 @@ public class ServletModuleLocalizer extends ServletModule {
           List parts = StringRoutines.splitString(operations[i], ";");
 
           if (parts.size() != 2) {
-            logger.error("articleoperationbatch: operation string invalid: " + operations[i]);
+            getLogger().error("articleoperationbatch: operation string invalid: " + operations[i]);
           }
           else {
             String articleIdString = (String) parts.get(0);
             String operationString = (String) parts.get(1);
 
-            performArticleOperation(ServletHelper.getUser(aRequest), articleIdString, operationString);
+            performArticleOperation(aRequest, articleIdString, operationString);
           }
         }
       }
     }
 
-    redirect(aResponse, returnUrlString);
+    ServletHelper.redirect(aResponse, returnUrlString);
   }
 
   public List getAdministerOperations() throws ServletModuleExc {