whoops
[mir.git] / source / mircoders / localizer / basic / MirBasicAdminInterfaceLocalizer.java
index 74e1b3e..224d980 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two.  You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries.  If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so.  If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
  */
 
 package mircoders.localizer.basic;
 
-import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.Vector;
 
 import mir.entity.Entity;
 import mir.entity.adapter.EntityAdapter;
+import mir.log.LoggerWrapper;
 import mir.misc.StringUtil;
 import mir.storage.StorageObjectFailure;
-import mir.util.StringRoutines;
+import mir.util.DateTimeFunctions;
+import mir.util.EntityUtility;
 import mircoders.entity.EntityComment;
 import mircoders.entity.EntityContent;
+import mircoders.global.MirGlobal;
 import mircoders.localizer.MirAdminInterfaceLocalizer;
 import mircoders.localizer.MirLocalizerExc;
 import mircoders.localizer.MirLocalizerFailure;
@@ -56,9 +61,10 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
   private Vector simpleArticleOperations;
   private Map simpleCommentOperationsMap;
   private Map simpleArticleOperationsMap;
-  private static SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
+  protected static LoggerWrapper logger = new LoggerWrapper("Localizer.AdminInterface");;
 
   public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerExc {
+
     simpleCommentOperations = new Vector();
     simpleArticleOperations = new Vector();
     simpleCommentOperationsMap = new HashMap();
@@ -72,6 +78,10 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
     addSimpleCommentOperation(new ModifyCommentFieldOperation("hide", "is_published", "0"));
   }
 
+  public String getAdminPageGenerator(String aPage, Map aTemplateData, EntityAdapter aUser, String aDefault) {
+    return aDefault;
+  }
+
   public String makePasswordDigest(String aPassword) {
     return aPassword;
   }
@@ -121,9 +131,11 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
 
   protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation {
     private String name;
+    private boolean logOperation;
 
-    protected EntityModifyingOperation(String aName) {
+    protected EntityModifyingOperation(String aName, boolean aLogOperation) {
       name = aName;
+      logOperation = aLogOperation;
     }
 
     public String getName() {
@@ -140,23 +152,52 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
       }
     };
 
-    public void perform(EntityAdapter aUser, EntityAdapter anEntity) {
+    public void perform(EntityAdapter aUser, EntityAdapter anEntity) throws MirLocalizerExc, MirLocalizerFailure {
       Entity entity = anEntity.getEntity();
       try {
         performModification(aUser, entity);
+
+        if (logOperation) {
+          try {
+            StringBuffer line = new StringBuffer();
+
+            line.append(DateTimeFunctions.advancedDateFormat(
+                MirGlobal.config().getString("Mir.DefaultDateTimeFormat"),
+                (new GregorianCalendar()).getTime(),
+                MirGlobal.config().getString("Mir.DefaultTimezone")));
+            line.append(" ");
+            if (aUser != null)
+              line.append(aUser.get("login"));
+            else
+              line.append("unknown");
+
+            line.append(" ");
+            line.append(getName());
+            EntityUtility.appendLineToField(entity, "comment", line.toString());
+          }
+          catch (Throwable t) {
+            logger.error("Error while trying to log an article operation: " + t.toString());
+          }
+        }
+
         entity.update();
       }
       catch (Throwable t) {
+        throw new MirLocalizerFailure(t);
       }
     };
 
     protected abstract boolean isAvailable(Entity anEntity) throws StorageObjectFailure ;
-    protected abstract void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectFailure ;
+    protected abstract void performModification(EntityAdapter aUser, Entity anEntity)  throws MirLocalizerExc, MirLocalizerFailure ;
   }
 
   public static abstract class CommentModifyingOperation extends EntityModifyingOperation {
+    public CommentModifyingOperation(String aName, boolean aLogOperation) {
+      super(aName, aLogOperation);
+    }
+
     public CommentModifyingOperation(String aName) {
-      super(aName);
+      this(aName, true);
     }
 
     protected boolean isAvailable(Entity anEntity) throws StorageObjectFailure {
@@ -165,7 +206,7 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
 
     protected void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectFailure {
       performModification(aUser, (EntityComment) anEntity);
-      DatabaseContent.getInstance().setUnproduced("id="+anEntity.getValue("to_media"));
+      DatabaseContent.getInstance().setUnproduced("id="+anEntity.getFieldValue("to_media"));
     };
 
     protected abstract boolean isAvailable(EntityComment aComment) throws StorageObjectFailure ;
@@ -173,12 +214,8 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
   }
 
   public static abstract class ArticleModifyingOperation extends EntityModifyingOperation {
-    private boolean logOperation;
-
     public ArticleModifyingOperation(String aName, boolean aLogOperation) {
-      super(aName);
-
-      logOperation = aLogOperation;
+      super(aName, aLogOperation);
     }
 
     protected boolean isAvailable(Entity anEntity) throws StorageObjectFailure {
@@ -187,29 +224,7 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
 
     protected void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectFailure {
       performModification(aUser, (EntityContent) anEntity);
-      anEntity.setValueForProperty("is_produced", "0");
-
-      if (logOperation) {
-        StringBuffer comment = new StringBuffer();
-        try {
-          comment.append(StringRoutines.interpretAsString(anEntity.getValue("comment")));
-        }
-        catch (Throwable t) {
-        }
-        if (comment.length()>0 && comment.charAt(comment.length()-1)!='\n') {
-          comment.append('\n');
-        }
-        comment.append(dateFormatter.format((new GregorianCalendar()).getTime()));
-        comment.append(" ");
-        try {
-          comment.append(StringRoutines.interpretAsString(aUser.get("login")));
-        }
-        catch (Throwable t) {
-        }
-        comment.append(" ");
-        comment.append(getName());
-        anEntity.setValueForProperty("comment", comment.toString());
-      }
+      anEntity.setFieldValue("is_produced", "0");
     };
 
     protected abstract boolean isAvailable(EntityContent anArticle) throws StorageObjectFailure ;
@@ -232,10 +247,37 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
     }
 
     protected void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectFailure {
-      aComment.setValueForProperty(field, value);
+      aComment.setFieldValue(field, value);
+    }
+  }
+
+  protected static class SetCommentFieldsOperation extends CommentModifyingOperation {
+    private Map values;
+
+    public SetCommentFieldsOperation(String aName, String aFields[], String aValues[]) throws MirLocalizerExc {
+      super(aName);
+
+      values = new HashMap();
+
+      for (int i=0; i<aFields.length; i++)
+        values.put(aFields[i], aValues[i]);
+    }
+
+    protected boolean isAvailable(EntityComment aComment) {
+      return true;
+    }
+
+    protected void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectFailure {
+      Iterator i = values.entrySet().iterator();
+
+      while (i.hasNext()) {
+        Map.Entry entry = (Map.Entry) i.next();
+        aComment.setFieldValue((String) entry.getKey(), (String) entry.getValue());
+      }
     }
   }
 
+
   protected static class ModifyCommentFieldOperation extends CommentModifyingOperation {
     private String field;
     private String value;
@@ -248,11 +290,11 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
     }
 
     protected boolean isAvailable(EntityComment aComment) {
-      return aComment.getValue(field) == null || !aComment.getValue(field).equals(value);
+      return aComment.getFieldValue(field) == null || !aComment.getFieldValue(field).equals(value);
     }
 
     protected void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectFailure {
-      aComment.setValueForProperty(field, value);
+      aComment.setFieldValue(field, value);
     }
   }
 
@@ -272,7 +314,7 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
     }
 
     protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectFailure {
-      anArticle.setValueForProperty(field, value);
+      anArticle.setFieldValue(field, value);
     }
   }
 
@@ -288,33 +330,36 @@ public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocaliz
     }
 
     protected boolean isAvailable(EntityContent anArticle) {
-      return anArticle.getValue(field) == null || !anArticle.getValue(field).equals(value);
+      return anArticle.getFieldValue(field) == null || !anArticle.getFieldValue(field).equals(value);
     }
 
     protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectFailure {
-      anArticle.setValueForProperty(field, value);
+      anArticle.setFieldValue(field, value);
     }
   }
 
   protected static class ChangeArticleFieldOperation extends ArticleModifyingOperation {
     private String field;
-    private String oldValue;
+    private Set oldValues;
     private String newValue;
 
-    public ChangeArticleFieldOperation(String aName, String aField, String anOldValue, String aNewValue, boolean aLogOperation) {
+    public ChangeArticleFieldOperation(String aName, String aField, String anOldValues[], String aNewValue, boolean aLogOperation) {
       super(aName, aLogOperation);
 
       field = aField;
       newValue = aNewValue;
-      oldValue = anOldValue;
+      oldValues = new HashSet(Arrays.asList(anOldValues));
+    }
+    public ChangeArticleFieldOperation(String aName, String aField, String anOldValue, String aNewValue, boolean aLogOperation) {
+      this(aName, aField, new String[] {anOldValue}, aNewValue, aLogOperation);
     }
 
     protected boolean isAvailable(EntityContent anArticle) {
-      return anArticle.getValue(field) != null && anArticle.getValue(field).equals(oldValue);
+      return anArticle.getFieldValue(field) != null && oldValues.contains(anArticle.getFieldValue(field));
     }
 
     protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectFailure {
-      anArticle.setValueForProperty(field, newValue);
+      anArticle.setFieldValue(field, newValue);
     }
   }
 }