content -> media are now ordered by id
[mir.git] / source / mircoders / localizer / basic / MirBasicAdminInterfaceLocalizer.java
index e680a6c..06c3200 100755 (executable)
-/*
- * Copyright (C) 2001, 2002  The Mir-coders group
- *
- * This file is part of Mir.
- *
- * Mir is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Mir is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Mir; if not, write to the Free Software
- * 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.
- */
-
-package mircoders.localizer.basic;
-
-import java.util.*;
-import mir.entity.adapter.*;
-import mir.storage.*;
-import mir.entity.*;
-import mircoders.localizer.*;
-import mircoders.entity.*;
-import mircoders.storage.*;
-
-
-public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocalizer {
-  private Map simpleCommentOperations;
-  private Map simpleArticleOperations;
-
-  public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerException {
-    simpleCommentOperations = new HashMap();
-    simpleArticleOperations = new HashMap();
-
-    buildSimpleCommentOperations(simpleCommentOperations);
-    buildSimpleArticleOperations(simpleArticleOperations);
-  }
-
-  public Map simpleCommentOperations() {
-    return simpleCommentOperations;
-  };
-
-  public Map simpleArticleOperations() {
-    return simpleArticleOperations;
-  };
-
-  public void buildSimpleCommentOperations(Map anOperations) throws MirLocalizerFailure, MirLocalizerException {
-    anOperations.put("hide", new HideCommentOperation());
-    anOperations.put("unhide", new UnhideCommentOperation());
-  };
-
-  public void buildSimpleArticleOperations(Map anOperations)  throws MirLocalizerFailure, MirLocalizerException {
-    anOperations.put("hide", new HideArticleOperation());
-    anOperations.put("unhide", new UnhideArticleOperation());
-  };
-
-  protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation {
-    public boolean isAvailable(EntityAdapter anEntity) {
-      try {
-
-        Entity entity = anEntity.getEntity();
-        return (entity instanceof EntityComment) && isAvailable((EntityComment) entity);
-      }
-      catch (Throwable t) {
-        return false;
-      }
-    };
-
-    public void perform(EntityAdapter anEntity) {
-      Entity entity = anEntity.getEntity();
-      try {
-        performModification(entity);
-        entity.update();
-      }
-      catch (Throwable t) {
-      }
-    };
-
-    protected abstract boolean isAvailable(Entity anEntity) throws StorageObjectException ;
-    protected abstract void performModification(Entity anEntity) throws StorageObjectException ;
-  }
-
-  public static abstract class CommentModifyingOperation extends EntityModifyingOperation {
-    protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
-      return anEntity instanceof EntityComment && isAvailable((EntityComment) anEntity);
-    }
-
-    protected void performModification(Entity anEntity) throws StorageObjectException {
-      performModification((EntityComment) anEntity);
-      DatabaseContent.getInstance().setUnproduced("id="+anEntity.getValue("to_media"));
-    };
-
-    protected abstract boolean isAvailable(EntityComment aComment) throws StorageObjectException ;
-    protected abstract void performModification(EntityComment aComment) throws StorageObjectException ;
-  }
-
-  public static abstract class ArticleModifyingOperation extends EntityModifyingOperation {
-    protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
-      return anEntity instanceof EntityContent && isAvailable((EntityComment) anEntity);
-    }
-
-    protected void performModification(Entity anEntity) throws StorageObjectException {
-      performModification((EntityContent) anEntity);
-      anEntity.setValueForProperty("is_produced", "0");
-    };
-
-    protected abstract boolean isAvailable(EntityContent anArticle) throws StorageObjectException ;
-    protected abstract void performModification(EntityContent anArticle) throws StorageObjectException ;
-  }
-
-  private static class HideCommentOperation extends CommentModifyingOperation {
-    protected boolean isAvailable(EntityComment aComment) {
-      return aComment.getValue("is_published").equals("1");
-    }
-    protected void performModification(EntityComment aComment) throws StorageObjectException {
-      aComment.setValueForProperty("is_published", "0");
-    }
-  }
-
-  private static class UnhideCommentOperation extends CommentModifyingOperation {
-    protected boolean isAvailable(EntityComment aComment) {
-      return aComment.getValue("is_published").equals("0");
-    }
-    protected void performModification(EntityComment aComment) throws StorageObjectException {
-      aComment.setValueForProperty("is_published", "1");
-    }
-  }
-
-  private static class HideArticleOperation extends ArticleModifyingOperation {
-    protected boolean isAvailable(EntityContent anArticle) {
-      return anArticle.getValue("is_published").equals("1");
-    }
-    protected void performModification(EntityContent anArticle) throws StorageObjectException {
-      anArticle.setValueForProperty("is_published", "0");
-    }
-  }
-
-  private static class UnhideArticleOperation extends ArticleModifyingOperation {
-    protected boolean isAvailable(EntityContent anArticle) {
-      return anArticle.getValue("is_published").equals("0");
-    }
-    protected void performModification(EntityContent anArticle) throws StorageObjectException {
-      anArticle.setValueForProperty("is_published", "1");
-    }
-  }
-}
\ No newline at end of file
+/*\r
+ * Copyright (C) 2001, 2002  The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with the com.oreilly.servlet library, any library\r
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
+ * the above that use the same license as the above), and distribute linked\r
+ * combinations including the two.  You must obey the GNU General Public\r
+ * License in all respects for all of the code used other than the above\r
+ * mentioned libraries.  If you modify this file, you may extend this exception\r
+ * to your version of the file, but you are not obligated to do so.  If you do\r
+ * not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mircoders.localizer.basic;\r
+\r
+import java.text.SimpleDateFormat;\r
+import java.util.GregorianCalendar;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Vector;\r
+\r
+import mir.entity.Entity;\r
+import mir.entity.adapter.EntityAdapter;\r
+import mir.storage.StorageObjectFailure;\r
+import mir.misc.StringUtil;\r
+import mir.util.StringRoutines;\r
+import mircoders.entity.EntityComment;\r
+import mircoders.entity.EntityContent;\r
+import mircoders.localizer.MirAdminInterfaceLocalizer;\r
+import mircoders.localizer.MirLocalizerExc;\r
+import mircoders.localizer.MirLocalizerFailure;\r
+import mircoders.storage.DatabaseContent;\r
+\r
+\r
+public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocalizer {\r
+  private Vector simpleCommentOperations;\r
+  private Vector simpleArticleOperations;\r
+  private Map simpleCommentOperationsMap;\r
+  private Map simpleArticleOperationsMap;\r
+  private static SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");\r
+\r
+  public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerExc {\r
+    simpleCommentOperations = new Vector();\r
+    simpleArticleOperations = new Vector();\r
+    simpleCommentOperationsMap = new HashMap();\r
+    simpleArticleOperationsMap = new HashMap();\r
+\r
+    addSimpleArticleOperation(new ChangeArticleFieldOperation("newswire", "to_article_type", "0", "1", false));\r
+    addSimpleArticleOperation(new ModifyArticleFieldOperation("unhide", "is_published", "1", false));\r
+    addSimpleArticleOperation(new ModifyArticleFieldOperation("hide", "is_published", "0", false));\r
+\r
+    addSimpleCommentOperation(new ModifyCommentFieldOperation("unhide", "is_published", "1"));\r
+    addSimpleCommentOperation(new ModifyCommentFieldOperation("hide", "is_published", "0"));\r
+  }\r
+\r
+  public void initializeArticle(Map anArticle) {\r
+    anArticle.put("is_published", "0");\r
+    anArticle.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));\r
+  };\r
+\r
+  public List simpleCommentOperations() {\r
+    return simpleCommentOperations;\r
+  };\r
+\r
+  public List simpleArticleOperations() {\r
+    return simpleArticleOperations;\r
+  };\r
+\r
+  public MirSimpleEntityOperation simpleArticleOperationForName(String aName) {\r
+    return (MirSimpleEntityOperation) simpleArticleOperationsMap.get(aName);\r
+  };\r
+\r
+  public MirSimpleEntityOperation simpleCommentOperationForName(String aName) {\r
+    return (MirSimpleEntityOperation) simpleCommentOperationsMap.get(aName);\r
+  };\r
+\r
+  public void removeSimpleArticleOperation(String aName) {\r
+    simpleArticleOperations.remove(simpleArticleOperationsMap.get(aName));\r
+    simpleArticleOperationsMap.remove(aName);\r
+  }\r
+\r
+  public void addSimpleArticleOperation(MirSimpleEntityOperation anOperation) {\r
+    removeSimpleArticleOperation(anOperation.getName());\r
+    simpleArticleOperationsMap.put(anOperation.getName(), anOperation);\r
+    simpleArticleOperations.add(anOperation);\r
+  }\r
+\r
+  public void removeSimpleCommentOperation(String aName) {\r
+    simpleCommentOperations.remove(simpleCommentOperationsMap.get(aName));\r
+    simpleCommentOperationsMap.remove(aName);\r
+  }\r
+\r
+  public void addSimpleCommentOperation(MirSimpleEntityOperation anOperation) {\r
+    removeSimpleCommentOperation(anOperation.getName());\r
+    simpleCommentOperationsMap.put(anOperation.getName(), anOperation);\r
+    simpleCommentOperations.add(anOperation);\r
+  }\r
+\r
+  protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation {\r
+    private String name;\r
+\r
+    protected EntityModifyingOperation(String aName) {\r
+      name = aName;\r
+    }\r
+\r
+    public String getName() {\r
+      return name;\r
+    };\r
+\r
+    public boolean isAvailable(EntityAdapter anEntity) {\r
+      try {\r
+        Entity entity = anEntity.getEntity();\r
+        return isAvailable(entity);\r
+      }\r
+      catch (Throwable t) {\r
+        return false;\r
+      }\r
+    };\r
+\r
+    public void perform(EntityAdapter aUser, EntityAdapter anEntity) {\r
+      Entity entity = anEntity.getEntity();\r
+      try {\r
+        performModification(aUser, entity);\r
+        entity.update();\r
+      }\r
+      catch (Throwable t) {\r
+      }\r
+    };\r
+\r
+    protected abstract boolean isAvailable(Entity anEntity) throws StorageObjectFailure ;\r
+    protected abstract void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectFailure ;\r
+  }\r
+\r
+  public static abstract class CommentModifyingOperation extends EntityModifyingOperation {\r
+    public CommentModifyingOperation(String aName) {\r
+      super(aName);\r
+    }\r
+\r
+    protected boolean isAvailable(Entity anEntity) throws StorageObjectFailure {\r
+      return anEntity instanceof EntityComment && isAvailable((EntityComment) anEntity);\r
+    }\r
+\r
+    protected void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectFailure {\r
+      performModification(aUser, (EntityComment) anEntity);\r
+      DatabaseContent.getInstance().setUnproduced("id="+anEntity.getValue("to_media"));\r
+    };\r
+\r
+    protected abstract boolean isAvailable(EntityComment aComment) throws StorageObjectFailure ;\r
+    protected abstract void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectFailure ;\r
+  }\r
+\r
+  public static abstract class ArticleModifyingOperation extends EntityModifyingOperation {\r
+    private boolean logOperation;\r
+\r
+    public ArticleModifyingOperation(String aName, boolean aLogOperation) {\r
+      super(aName);\r
+\r
+      logOperation = aLogOperation;\r
+    }\r
+\r
+    protected boolean isAvailable(Entity anEntity) throws StorageObjectFailure {\r
+      return anEntity instanceof EntityContent && isAvailable((EntityContent) anEntity);\r
+    }\r
+\r
+    protected void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectFailure {\r
+      performModification(aUser, (EntityContent) anEntity);\r
+      anEntity.setValueForProperty("is_produced", "0");\r
+\r
+      if (logOperation) {\r
+        StringBuffer comment = new StringBuffer();\r
+        try {\r
+          comment.append(StringRoutines.interpretAsString(anEntity.getValue("comment")));\r
+        }\r
+        catch (Throwable t) {\r
+        }\r
+        if (comment.length()>0 && comment.charAt(comment.length()-1)!='\n') {\r
+          comment.append('\n');\r
+        }\r
+        comment.append(dateFormatter.format((new GregorianCalendar()).getTime()));\r
+        comment.append(" ");\r
+        try {\r
+          comment.append(StringRoutines.interpretAsString(aUser.get("login")));\r
+        }\r
+        catch (Throwable t) {\r
+        }\r
+        comment.append(" ");\r
+        comment.append(getName());\r
+        anEntity.setValueForProperty("comment", comment.toString());\r
+      }\r
+    };\r
+\r
+    protected abstract boolean isAvailable(EntityContent anArticle) throws StorageObjectFailure ;\r
+    protected abstract void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectFailure ;\r
+  }\r
+\r
+  protected static class SetCommentFieldOperation extends CommentModifyingOperation {\r
+    private String field;\r
+    private String value;\r
+\r
+    public SetCommentFieldOperation(String aName, String aField, String aValue) {\r
+      super(aName);\r
+\r
+      field = aField;\r
+      value = aValue;\r
+    }\r
+\r
+    protected boolean isAvailable(EntityComment aComment) {\r
+      return true;\r
+    }\r
+\r
+    protected void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectFailure {\r
+      aComment.setValueForProperty(field, value);\r
+    }\r
+  }\r
+\r
+  protected static class ModifyCommentFieldOperation extends CommentModifyingOperation {\r
+    private String field;\r
+    private String value;\r
+\r
+    public ModifyCommentFieldOperation(String aName, String aField, String aValue) {\r
+      super(aName);\r
+\r
+      field = aField;\r
+      value = aValue;\r
+    }\r
+\r
+    protected boolean isAvailable(EntityComment aComment) {\r
+      return aComment.getValue(field) == null || !aComment.getValue(field).equals(value);\r
+    }\r
+\r
+    protected void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectFailure {\r
+      aComment.setValueForProperty(field, value);\r
+    }\r
+  }\r
+\r
+  protected static class SetArticleFieldOperation extends ArticleModifyingOperation {\r
+    private String field;\r
+    private String value;\r
+\r
+    public SetArticleFieldOperation(String aName, String aField, String aValue, boolean aLogOperation) {\r
+      super(aName, aLogOperation);\r
+\r
+      field = aField;\r
+      value = aValue;\r
+    }\r
+\r
+    protected boolean isAvailable(EntityContent anArticle) {\r
+      return true;\r
+    }\r
+\r
+    protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectFailure {\r
+      anArticle.setValueForProperty(field, value);\r
+    }\r
+  }\r
+\r
+  protected static class ModifyArticleFieldOperation extends ArticleModifyingOperation {\r
+    private String field;\r
+    private String value;\r
+\r
+    public ModifyArticleFieldOperation(String aName, String aField, String aValue, boolean aLogOperation) {\r
+      super(aName, aLogOperation);\r
+\r
+      field = aField;\r
+      value = aValue;\r
+    }\r
+\r
+    protected boolean isAvailable(EntityContent anArticle) {\r
+      return anArticle.getValue(field) == null || !anArticle.getValue(field).equals(value);\r
+    }\r
+\r
+    protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectFailure {\r
+      anArticle.setValueForProperty(field, value);\r
+    }\r
+  }\r
+\r
+  protected static class ChangeArticleFieldOperation extends ArticleModifyingOperation {\r
+    private String field;\r
+    private String oldValue;\r
+    private String newValue;\r
+\r
+    public ChangeArticleFieldOperation(String aName, String aField, String anOldValue, String aNewValue, boolean aLogOperation) {\r
+      super(aName, aLogOperation);\r
+\r
+      field = aField;\r
+      newValue = aNewValue;\r
+      oldValue = anOldValue;\r
+    }\r
+\r
+    protected boolean isAvailable(EntityContent anArticle) {\r
+      return anArticle.getValue(field) != null && anArticle.getValue(field).equals(oldValue);\r
+    }\r
+\r
+    protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectFailure {\r
+      anArticle.setValueForProperty(field, newValue);\r
+    }\r
+  }\r
+}\r