some misc. maintenance
[mir.git] / source / mircoders / localizer / basic / MirBasicAdminInterfaceLocalizer.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.localizer.basic;
33
34 import java.util.*;
35 import mir.entity.adapter.*;
36 import mir.storage.*;
37 import mir.entity.*;
38 import mircoders.localizer.*;
39 import mircoders.entity.*;
40 import mircoders.storage.*;
41
42
43 public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocalizer {
44   private Map simpleCommentOperations;
45   private Map simpleArticleOperations;
46
47   public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerExc {
48     simpleCommentOperations = new HashMap();
49     simpleArticleOperations = new HashMap();
50
51     buildSimpleCommentOperations(simpleCommentOperations);
52     buildSimpleArticleOperations(simpleArticleOperations);
53   }
54
55   public Map simpleCommentOperations() {
56     return simpleCommentOperations;
57   };
58
59   public Map simpleArticleOperations() {
60     return simpleArticleOperations;
61   };
62
63   public void buildSimpleCommentOperations(Map anOperations) throws MirLocalizerFailure, MirLocalizerExc {
64     anOperations.put("hide", new HideCommentOperation());
65     anOperations.put("unhide", new UnhideCommentOperation());
66   };
67
68   public void buildSimpleArticleOperations(Map anOperations)  throws MirLocalizerFailure, MirLocalizerExc {
69     anOperations.put("hide", new HideArticleOperation());
70     anOperations.put("unhide", new UnhideArticleOperation());
71   };
72
73   protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation {
74     public boolean isAvailable(EntityAdapter anEntity) {
75       try {
76
77         Entity entity = anEntity.getEntity();
78         return (entity instanceof EntityComment) && isAvailable((EntityComment) entity);
79       }
80       catch (Throwable t) {
81         return false;
82       }
83     };
84
85     public void perform(EntityAdapter anEntity) {
86       Entity entity = anEntity.getEntity();
87       try {
88         performModification(entity);
89         entity.update();
90       }
91       catch (Throwable t) {
92       }
93     };
94
95     protected abstract boolean isAvailable(Entity anEntity) throws StorageObjectException ;
96     protected abstract void performModification(Entity anEntity) throws StorageObjectException ;
97   }
98
99   public static abstract class CommentModifyingOperation extends EntityModifyingOperation {
100     protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
101       return anEntity instanceof EntityComment && isAvailable((EntityComment) anEntity);
102     }
103
104     protected void performModification(Entity anEntity) throws StorageObjectException {
105       performModification((EntityComment) anEntity);
106       DatabaseContent.getInstance().setUnproduced("id="+anEntity.getValue("to_media"));
107     };
108
109     protected abstract boolean isAvailable(EntityComment aComment) throws StorageObjectException ;
110     protected abstract void performModification(EntityComment aComment) throws StorageObjectException ;
111   }
112
113   public static abstract class ArticleModifyingOperation extends EntityModifyingOperation {
114     protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
115       return anEntity instanceof EntityContent && isAvailable((EntityComment) anEntity);
116     }
117
118     protected void performModification(Entity anEntity) throws StorageObjectException {
119       performModification((EntityContent) anEntity);
120       anEntity.setValueForProperty("is_produced", "0");
121     };
122
123     protected abstract boolean isAvailable(EntityContent anArticle) throws StorageObjectException ;
124     protected abstract void performModification(EntityContent anArticle) throws StorageObjectException ;
125   }
126
127   private static class HideCommentOperation extends CommentModifyingOperation {
128     protected boolean isAvailable(EntityComment aComment) {
129       return aComment.getValue("is_published").equals("1");
130     }
131     protected void performModification(EntityComment aComment) throws StorageObjectException {
132       aComment.setValueForProperty("is_published", "0");
133     }
134   }
135
136   private static class UnhideCommentOperation extends CommentModifyingOperation {
137     protected boolean isAvailable(EntityComment aComment) {
138       return aComment.getValue("is_published").equals("0");
139     }
140     protected void performModification(EntityComment aComment) throws StorageObjectException {
141       aComment.setValueForProperty("is_published", "1");
142     }
143   }
144
145   private static class HideArticleOperation extends ArticleModifyingOperation {
146     protected boolean isAvailable(EntityContent anArticle) {
147       return anArticle.getValue("is_published").equals("1");
148     }
149     protected void performModification(EntityContent anArticle) throws StorageObjectException {
150       anArticle.setValueForProperty("is_published", "0");
151     }
152   }
153
154   private static class UnhideArticleOperation extends ArticleModifyingOperation {
155     protected boolean isAvailable(EntityContent anArticle) {
156       return anArticle.getValue("is_published").equals("0");
157     }
158     protected void performModification(EntityContent anArticle) throws StorageObjectException {
159       anArticle.setValueForProperty("is_published", "1");
160     }
161   }
162 }