adminster operation configuration now supports escape characters
[mir.git] / source / mircoders / servlet / ServletModuleLocalizer.java
index 0f9a895..1967049 100755 (executable)
  */
 package mircoders.servlet;
 
-import java.util.*;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-import mir.entity.adapter.EntityAdapter;
-import mir.log.LoggerWrapper;
-import mir.servlet.ServletModule;
+import mir.servlet.AdminServletModule;
 import mir.servlet.ServletModuleExc;
-import mir.servlet.ServletModuleFailure;
-import mir.util.*;
+import mir.util.StringRoutines;
 import mircoders.entity.EntityComment;
 import mircoders.entity.EntityContent;
-import mircoders.entity.EntityUsers;
 import mircoders.global.MirGlobal;
-import mircoders.localizer.MirAdminInterfaceLocalizer;
 import mircoders.module.ModuleComment;
 import mircoders.module.ModuleContent;
-import mircoders.storage.DatabaseComment;
-import mircoders.storage.DatabaseContent;
 
-public class ServletModuleLocalizer extends ServletModule {
-  private static ServletModuleLocalizer instance = new ServletModuleLocalizer();
-  public static ServletModule getInstance() { return instance; }
+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;
 
-  private ModuleContent contentModule;
-  private ModuleComment commentModule;
-  private List administerOperations;
+public class ServletModuleLocalizer extends AdminServletModule {
+  private final ModuleContent contentModule = new ModuleContent();
+  private final ModuleComment commentModule = new ModuleComment();
+  private final List administerOperations = new ArrayList();
 
-  private ServletModuleLocalizer() {
+  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++) {
           String setting = settings[i].trim();
 
           if (setting.length() > 0) {
-            List parts = StringRoutines.splitString(setting, ":");
+            List parts = StringRoutines.splitStringWithEscape(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,43 +75,35 @@ public class ServletModuleLocalizer extends ServletModule {
       }
     }
     catch (Exception e) {
-      logger.error("ServletModuleLocalizer could not be initialized: " + e.getMessage());
-    }
-
-
-  }
-
-  private EntityAdapter getActiveUser(HttpServletRequest aRequest) throws ServletModuleExc {
-    try {
-      HttpSession session = aRequest.getSession(false);
-      return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter
-          ("user", ServletHelper.getUser(aRequest));
-    }
-    catch (Throwable e) {
-      throw new ServletModuleFailure("ServletModuleLocalizer.getActiveUser: " + e.getMessage(), e);
+      getLogger().error("ServletModuleLocalizer could not be initialized" + e.getMessage(), e);
     }
   }
 
-  public void performCommentOperation(EntityAdapter 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) {
-        comment = MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("comment", entity);
-        operation = MirGlobal.localizer().adminInterface().simpleCommentOperationForName(anOperation);
-        operation.perform(aUser, comment);
-        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);
     }
   }
 
@@ -135,9 +112,26 @@ public class ServletModuleLocalizer extends ServletModule {
     String operationString = aRequest.getParameter("operation");
     String returnUrlString = aRequest.getParameter("returnurl");
 
-    performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
+    performCommentOperation(aRequest, commentIdString, operationString);
 
-    redirect(aResponse, returnUrlString);
+    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");
+
+
+    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 {
@@ -151,44 +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(getActiveUser(aRequest), commentIdString, operationString);
+            performCommentOperation(aRequest, commentIdString, operationString);
           }
         }
       }
     }
 
-    redirect(aResponse, returnUrlString);
+    ServletHelper.redirect(aResponse, returnUrlString);
   }
 
-  public void performArticleOperation(EntityAdapter 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) {
-        article = MirGlobal.localizer().dataModel().adapterModel().
-            makeEntityAdapter("content", entity);
-        operation = MirGlobal.localizer().adminInterface().
-            simpleArticleOperationForName(anOperation);
-        operation.perform(aUser, article);
-        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());
     }
   }
 
@@ -197,8 +218,8 @@ public class ServletModuleLocalizer extends ServletModule {
     String operationString = aRequest.getParameter("operation");
     String returnUrlString = aRequest.getParameter("returnurl");
 
-    performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
-    redirect(aResponse, returnUrlString);
+    performArticleOperation(aRequest, articleIdString, operationString);
+    ServletHelper.redirect(aResponse, returnUrlString);
   }
 
   public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
@@ -213,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(getActiveUser(aRequest), articleIdString, operationString);
+            performArticleOperation(aRequest, articleIdString, operationString);
           }
         }
       }
     }
 
-    redirect(aResponse, returnUrlString);
+    ServletHelper.redirect(aResponse, returnUrlString);
   }
 
   public List getAdministerOperations() throws ServletModuleExc {