From 6fcafb295112150a9607e6f936a673d83d7a4fe6 Mon Sep 17 00:00:00 2001 From: zapata Date: Mon, 23 Dec 2002 16:19:38 +0000 Subject: [PATCH] added optional logging for article operations in admin --- .../localizer/MirAdminInterfaceLocalizer.java | 2 +- .../basic/MirBasicAdminInterfaceLocalizer.java | 490 +++++++++++---------- .../mircoders/servlet/ServletModuleLocalizer.java | 27 +- 3 files changed, 278 insertions(+), 241 deletions(-) diff --git a/source/mircoders/localizer/MirAdminInterfaceLocalizer.java b/source/mircoders/localizer/MirAdminInterfaceLocalizer.java index 841e1df8..7c381651 100755 --- a/source/mircoders/localizer/MirAdminInterfaceLocalizer.java +++ b/source/mircoders/localizer/MirAdminInterfaceLocalizer.java @@ -48,6 +48,6 @@ public interface MirAdminInterfaceLocalizer { public interface MirSimpleEntityOperation { public String getName(); public boolean isAvailable(EntityAdapter anEntity); - public void perform(EntityAdapter anEntity); + public void perform(EntityAdapter aUser, EntityAdapter anEntity); } } \ No newline at end of file diff --git a/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java b/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java index 6edb5530..d7abc879 100755 --- a/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java +++ b/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java @@ -1,233 +1,259 @@ -/* - * 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 Vector simpleCommentOperations; - private Vector simpleArticleOperations; - private Map simpleCommentOperationsMap; - private Map simpleArticleOperationsMap; - - public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerExc { - simpleCommentOperations = new Vector(); - simpleArticleOperations = new Vector(); - simpleCommentOperationsMap = new HashMap(); - simpleArticleOperationsMap = new HashMap(); - - addSimpleArticleOperation(new ChangeArticleFieldOperation("newswire", "to_article_type", "0", "1")); - addSimpleArticleOperation(new SetArticleFieldOperation("unhide", "is_published", "1")); - addSimpleArticleOperation(new SetArticleFieldOperation("hide", "is_published", "0")); - - addSimpleCommentOperation(new SetCommentFieldOperation("unhide", "is_published", "1")); - addSimpleCommentOperation(new SetCommentFieldOperation("hide", "is_published", "0")); - } - - public List simpleCommentOperations() { - return simpleCommentOperations; - }; - - public List simpleArticleOperations() { - return simpleArticleOperations; - }; - - public MirSimpleEntityOperation simpleArticleOperationForName(String aName) { - return (MirSimpleEntityOperation) simpleArticleOperationsMap.get(aName); - }; - - public MirSimpleEntityOperation simpleCommentOperationForName(String aName) { - return (MirSimpleEntityOperation) simpleCommentOperationsMap.get(aName); - }; - - public void removeSimpleArticleOperation(String aName) { - simpleArticleOperations.remove(simpleArticleOperationsMap.get(aName)); - simpleArticleOperationsMap.remove(aName); - } - - public void addSimpleArticleOperation(MirSimpleEntityOperation anOperation) { - removeSimpleArticleOperation(anOperation.getName()); - simpleArticleOperationsMap.put(anOperation.getName(), anOperation); - simpleArticleOperations.add(anOperation); - } - - public void removeSimpleCommentOperation(String aName) { - simpleCommentOperations.remove(simpleCommentOperationsMap.get(aName)); - simpleCommentOperationsMap.remove(aName); - } - - public void addSimpleCommentOperation(MirSimpleEntityOperation anOperation) { - removeSimpleCommentOperation(anOperation.getName()); - simpleCommentOperationsMap.put(anOperation.getName(), anOperation); - simpleCommentOperations.add(anOperation); - } - - protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation { - private String name; - - protected EntityModifyingOperation(String aName) { - name = aName; - } - - public String getName() { - return name; - }; - - public boolean isAvailable(EntityAdapter anEntity) { - try { - Entity entity = anEntity.getEntity(); - return isAvailable(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 { - public CommentModifyingOperation(String aName) { - super(aName); - } - - 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 { - public ArticleModifyingOperation(String aName) { - super(aName); - } - - protected boolean isAvailable(Entity anEntity) throws StorageObjectException { - return anEntity instanceof EntityContent && isAvailable((EntityContent) 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 ; - } - - protected static class SetCommentFieldOperation extends CommentModifyingOperation { - private String field; - private String value; - - public SetCommentFieldOperation(String aName, String aField, String aValue) { - super(aName); - - field = aField; - value = aValue; - } - - protected boolean isAvailable(EntityComment aComment) { - return aComment.getValue(field) == null || !aComment.getValue(field).equals(value); - } - - protected void performModification(EntityComment aComment) throws StorageObjectException { - aComment.setValueForProperty(field, value); - } - } - - protected static class SetArticleFieldOperation extends ArticleModifyingOperation { - private String field; - private String value; - - public SetArticleFieldOperation(String aName, String aField, String aValue) { - super(aName); - - field = aField; - value = aValue; - } - - protected boolean isAvailable(EntityContent anArticle) { - return anArticle.getValue(field) == null || !anArticle.getValue(field).equals(value); - } - - protected void performModification(EntityContent anArticle) throws StorageObjectException { - anArticle.setValueForProperty(field, value); - } - } - - protected static class ChangeArticleFieldOperation extends ArticleModifyingOperation { - private String field; - private String oldValue; - private String newValue; - - public ChangeArticleFieldOperation(String aName, String aField, String anOldValue, String aNewValue) { - super(aName); - - field = aField; - newValue = aNewValue; - oldValue = anOldValue; - } - - protected boolean isAvailable(EntityContent anArticle) { - return anArticle.getValue(field) != null && anArticle.getValue(field).equals(oldValue); - } - - protected void performModification(EntityContent anArticle) throws StorageObjectException { - anArticle.setValueForProperty(field, newValue); - } - } +/* + * 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 java.text.*; + +import mir.entity.adapter.*; +import mir.storage.*; +import mir.entity.*; +import mir.util.*; + +import mircoders.localizer.*; +import mircoders.entity.*; +import mircoders.storage.*; + + +public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocalizer { + private Vector simpleCommentOperations; + private Vector simpleArticleOperations; + private Map simpleCommentOperationsMap; + private Map simpleArticleOperationsMap; + private static SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + + public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerExc { + simpleCommentOperations = new Vector(); + simpleArticleOperations = new Vector(); + simpleCommentOperationsMap = new HashMap(); + simpleArticleOperationsMap = new HashMap(); + + addSimpleArticleOperation(new ChangeArticleFieldOperation("newswire", "to_article_type", "0", "1", false)); + addSimpleArticleOperation(new SetArticleFieldOperation("unhide", "is_published", "1", false)); + addSimpleArticleOperation(new SetArticleFieldOperation("hide", "is_published", "0", false)); + + addSimpleCommentOperation(new SetCommentFieldOperation("unhide", "is_published", "1")); + addSimpleCommentOperation(new SetCommentFieldOperation("hide", "is_published", "0")); + } + + public List simpleCommentOperations() { + return simpleCommentOperations; + }; + + public List simpleArticleOperations() { + return simpleArticleOperations; + }; + + public MirSimpleEntityOperation simpleArticleOperationForName(String aName) { + return (MirSimpleEntityOperation) simpleArticleOperationsMap.get(aName); + }; + + public MirSimpleEntityOperation simpleCommentOperationForName(String aName) { + return (MirSimpleEntityOperation) simpleCommentOperationsMap.get(aName); + }; + + public void removeSimpleArticleOperation(String aName) { + simpleArticleOperations.remove(simpleArticleOperationsMap.get(aName)); + simpleArticleOperationsMap.remove(aName); + } + + public void addSimpleArticleOperation(MirSimpleEntityOperation anOperation) { + removeSimpleArticleOperation(anOperation.getName()); + simpleArticleOperationsMap.put(anOperation.getName(), anOperation); + simpleArticleOperations.add(anOperation); + } + + public void removeSimpleCommentOperation(String aName) { + simpleCommentOperations.remove(simpleCommentOperationsMap.get(aName)); + simpleCommentOperationsMap.remove(aName); + } + + public void addSimpleCommentOperation(MirSimpleEntityOperation anOperation) { + removeSimpleCommentOperation(anOperation.getName()); + simpleCommentOperationsMap.put(anOperation.getName(), anOperation); + simpleCommentOperations.add(anOperation); + } + + protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation { + private String name; + + protected EntityModifyingOperation(String aName) { + name = aName; + } + + public String getName() { + return name; + }; + + public boolean isAvailable(EntityAdapter anEntity) { + try { + Entity entity = anEntity.getEntity(); + return isAvailable(entity); + } + catch (Throwable t) { + return false; + } + }; + + public void perform(EntityAdapter aUser, EntityAdapter anEntity) { + Entity entity = anEntity.getEntity(); + try { + performModification(aUser, entity); + entity.update(); + } + catch (Throwable t) { + } + }; + + protected abstract boolean isAvailable(Entity anEntity) throws StorageObjectException ; + protected abstract void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectException ; + } + + public static abstract class CommentModifyingOperation extends EntityModifyingOperation { + public CommentModifyingOperation(String aName) { + super(aName); + } + + protected boolean isAvailable(Entity anEntity) throws StorageObjectException { + return anEntity instanceof EntityComment && isAvailable((EntityComment) anEntity); + } + + protected void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectException { + performModification(aUser, (EntityComment) anEntity); + DatabaseContent.getInstance().setUnproduced("id="+anEntity.getValue("to_media")); + }; + + protected abstract boolean isAvailable(EntityComment aComment) throws StorageObjectException ; + protected abstract void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectException ; + } + + public static abstract class ArticleModifyingOperation extends EntityModifyingOperation { + private boolean logOperation; + + public ArticleModifyingOperation(String aName, boolean aLogOperation) { + super(aName); + + logOperation = aLogOperation; + } + + protected boolean isAvailable(Entity anEntity) throws StorageObjectException { + return anEntity instanceof EntityContent && isAvailable((EntityContent) anEntity); + } + + protected void performModification(EntityAdapter aUser, Entity anEntity) throws StorageObjectException { + performModification(aUser, (EntityContent) anEntity); + anEntity.setValueForProperty("is_produced", "0"); + + if (logOperation) { + StringBuffer comment = new StringBuffer(anEntity.getValue("comment")); + 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()); + } + }; + + protected abstract boolean isAvailable(EntityContent anArticle) throws StorageObjectException ; + protected abstract void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectException ; + } + + protected static class SetCommentFieldOperation extends CommentModifyingOperation { + private String field; + private String value; + + public SetCommentFieldOperation(String aName, String aField, String aValue) { + super(aName); + + field = aField; + value = aValue; + } + + protected boolean isAvailable(EntityComment aComment) { + return aComment.getValue(field) == null || !aComment.getValue(field).equals(value); + } + + protected void performModification(EntityAdapter aUser, EntityComment aComment) throws StorageObjectException { + aComment.setValueForProperty(field, value); + } + } + + protected static class SetArticleFieldOperation extends ArticleModifyingOperation { + private String field; + private String value; + + public SetArticleFieldOperation(String aName, String aField, String aValue, boolean aLogOperation) { + super(aName, aLogOperation); + + field = aField; + value = aValue; + } + + protected boolean isAvailable(EntityContent anArticle) { + return anArticle.getValue(field) == null || !anArticle.getValue(field).equals(value); + } + + protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectException { + anArticle.setValueForProperty(field, value); + } + } + + protected static class ChangeArticleFieldOperation extends ArticleModifyingOperation { + private String field; + private String oldValue; + private String newValue; + + public ChangeArticleFieldOperation(String aName, String aField, String anOldValue, String aNewValue, boolean aLogOperation) { + super(aName, aLogOperation); + + field = aField; + newValue = aNewValue; + oldValue = anOldValue; + } + + protected boolean isAvailable(EntityContent anArticle) { + return anArticle.getValue(field) != null && anArticle.getValue(field).equals(oldValue); + } + + protected void performModification(EntityAdapter aUser, EntityContent anArticle) throws StorageObjectException { + anArticle.setValueForProperty(field, newValue); + } + } } \ No newline at end of file diff --git a/source/mircoders/servlet/ServletModuleLocalizer.java b/source/mircoders/servlet/ServletModuleLocalizer.java index e316a148..a0d57c07 100755 --- a/source/mircoders/servlet/ServletModuleLocalizer.java +++ b/source/mircoders/servlet/ServletModuleLocalizer.java @@ -66,7 +66,18 @@ public class ServletModuleLocalizer extends ServletModule { } } - public void performCommentOperation(String anId, String anOperation) { + private EntityAdapter getActiveUser(HttpServletRequest aRequest) throws ServletModuleException { + try { + HttpSession session = aRequest.getSession(false); + return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter + ("user", (EntityUsers) session.getAttribute("login.uid")); + } + catch (Exception e) { + throw new ServletModuleException("ServletModuleLocalizer.getActiveUser: " + e.getMessage()); + } + } + + public void performCommentOperation(EntityAdapter aUser, String anId, String anOperation) { MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation; EntityAdapter comment; EntityComment entity; @@ -77,7 +88,7 @@ public class ServletModuleLocalizer extends ServletModule { if (entity != null) { comment = MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("comment", entity); operation = MirGlobal.localizer().adminInterface().simpleCommentOperationForName(anOperation); - operation.perform(comment); + operation.perform(aUser, comment); logger.info("Operation " + anOperation + " successfully performed on comment " + anId); } logger.error("Error while performing " + anOperation + " on comment " + anId + ": comment is null"); @@ -92,7 +103,7 @@ public class ServletModuleLocalizer extends ServletModule { String operationString = aRequest.getParameter("operation"); String returnUrlString = aRequest.getParameter("returnurl"); - performCommentOperation(commentIdString, operationString); + performCommentOperation(getActiveUser(aRequest), commentIdString, operationString); redirect(aResponse, returnUrlString); } @@ -114,7 +125,7 @@ public class ServletModuleLocalizer extends ServletModule { String commentIdString = (String) parts.get(0); String operationString = (String) parts.get(1); - performCommentOperation(commentIdString, operationString); + performCommentOperation(getActiveUser(aRequest), commentIdString, operationString); } } } @@ -122,7 +133,7 @@ public class ServletModuleLocalizer extends ServletModule { redirect(aResponse, returnUrlString); } - public void performArticleOperation(String anId, String anOperation) { + public void performArticleOperation(EntityAdapter aUser, String anId, String anOperation) { MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation; EntityAdapter article; EntityContent entity; @@ -135,7 +146,7 @@ public class ServletModuleLocalizer extends ServletModule { makeEntityAdapter("content", entity); operation = MirGlobal.localizer().adminInterface(). simpleArticleOperationForName(anOperation); - operation.perform(article); + operation.perform(aUser, article); logger.info("Operation " + anOperation + " successfully performed on article " + anId); } logger.error("Error while performing " + anOperation + " on article " + anId + ": article is null"); @@ -150,7 +161,7 @@ public class ServletModuleLocalizer extends ServletModule { String operationString = aRequest.getParameter("operation"); String returnUrlString = aRequest.getParameter("returnurl"); - performArticleOperation(articleIdString, operationString); + performArticleOperation(getActiveUser(aRequest), articleIdString, operationString); redirect(aResponse, returnUrlString); } @@ -171,7 +182,7 @@ public class ServletModuleLocalizer extends ServletModule { String articleIdString = (String) parts.get(0); String operationString = (String) parts.get(1); - performArticleOperation(articleIdString, operationString); + performArticleOperation(getActiveUser(aRequest), articleIdString, operationString); } } } -- 2.11.0