small fixes yet again
[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 Vector simpleCommentOperations;
45   private Vector simpleArticleOperations;
46   private Map simpleCommentOperationsMap;
47   private Map simpleArticleOperationsMap;
48
49   public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerExc {
50     simpleCommentOperations = new Vector();
51     simpleArticleOperations = new Vector();
52     simpleCommentOperationsMap = new HashMap();
53     simpleArticleOperationsMap = new HashMap();
54
55     addSimpleArticleOperation(new ChangeArticleFieldOperation("newswire", "to_article_type", "0", "1"));
56     addSimpleArticleOperation(new SetArticleFieldOperation("unhide", "is_published", "1"));
57     addSimpleArticleOperation(new SetArticleFieldOperation("hide", "is_published", "0"));
58
59     addSimpleCommentOperation(new SetCommentFieldOperation("unhide", "is_published", "1"));
60     addSimpleCommentOperation(new SetCommentFieldOperation("hide", "is_published", "0"));
61   }
62
63   public List simpleCommentOperations() {
64     return simpleCommentOperations;
65   };
66
67   public List simpleArticleOperations() {
68     return simpleArticleOperations;
69   };
70
71   public MirSimpleEntityOperation simpleArticleOperationForName(String aName) {
72     return (MirSimpleEntityOperation) simpleArticleOperationsMap.get(aName);
73   };
74
75   public MirSimpleEntityOperation simpleCommentOperationForName(String aName) {
76     return (MirSimpleEntityOperation) simpleCommentOperationsMap.get(aName);
77   };
78
79   public void removeSimpleArticleOperation(String aName) {
80     simpleArticleOperations.remove(simpleArticleOperationsMap.get(aName));
81     simpleArticleOperationsMap.remove(aName);
82   }
83
84   public void addSimpleArticleOperation(MirSimpleEntityOperation anOperation) {
85     removeSimpleArticleOperation(anOperation.getName());
86     simpleArticleOperationsMap.put(anOperation.getName(), anOperation);
87     simpleArticleOperations.add(anOperation);
88   }
89
90   public void removeSimpleCommentOperation(String aName) {
91     simpleCommentOperations.remove(simpleCommentOperationsMap.get(aName));
92     simpleCommentOperationsMap.remove(aName);
93   }
94
95   public void addSimpleCommentOperation(MirSimpleEntityOperation anOperation) {
96     removeSimpleCommentOperation(anOperation.getName());
97     simpleCommentOperationsMap.put(anOperation.getName(), anOperation);
98     simpleCommentOperations.add(anOperation);
99   }
100
101   protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation {
102     private String name;
103
104     protected EntityModifyingOperation(String aName) {
105       name = aName;
106     }
107
108     public String getName() {
109       return name;
110     };
111
112     public boolean isAvailable(EntityAdapter anEntity) {
113       try {
114         Entity entity = anEntity.getEntity();
115         return isAvailable(entity);
116       }
117       catch (Throwable t) {
118         return false;
119       }
120     };
121
122     public void perform(EntityAdapter anEntity) {
123       Entity entity = anEntity.getEntity();
124       try {
125         performModification(entity);
126         entity.update();
127       }
128       catch (Throwable t) {
129       }
130     };
131
132     protected abstract boolean isAvailable(Entity anEntity) throws StorageObjectException ;
133     protected abstract void performModification(Entity anEntity) throws StorageObjectException ;
134   }
135
136   public static abstract class CommentModifyingOperation extends EntityModifyingOperation {
137     public CommentModifyingOperation(String aName) {
138       super(aName);
139     }
140
141     protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
142       return anEntity instanceof EntityComment && isAvailable((EntityComment) anEntity);
143     }
144
145     protected void performModification(Entity anEntity) throws StorageObjectException {
146       performModification((EntityComment) anEntity);
147       DatabaseContent.getInstance().setUnproduced("id="+anEntity.getValue("to_media"));
148     };
149
150     protected abstract boolean isAvailable(EntityComment aComment) throws StorageObjectException ;
151     protected abstract void performModification(EntityComment aComment) throws StorageObjectException ;
152   }
153
154   public static abstract class ArticleModifyingOperation extends EntityModifyingOperation {
155     public ArticleModifyingOperation(String aName) {
156       super(aName);
157     }
158
159     protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
160       return anEntity instanceof EntityContent && isAvailable((EntityContent) anEntity);
161     }
162
163     protected void performModification(Entity anEntity) throws StorageObjectException {
164       performModification((EntityContent) anEntity);
165       anEntity.setValueForProperty("is_produced", "0");
166     };
167
168     protected abstract boolean isAvailable(EntityContent anArticle) throws StorageObjectException ;
169     protected abstract void performModification(EntityContent anArticle) throws StorageObjectException ;
170   }
171
172   protected static class SetCommentFieldOperation extends CommentModifyingOperation {
173     private String field;
174     private String value;
175
176     public SetCommentFieldOperation(String aName, String aField, String aValue) {
177       super(aName);
178
179       field = aField;
180       value = aValue;
181     }
182
183     protected boolean isAvailable(EntityComment aComment) {
184       return aComment.getValue(field) == null || !aComment.getValue(field).equals(value);
185     }
186
187     protected void performModification(EntityComment aComment) throws StorageObjectException {
188       aComment.setValueForProperty(field, value);
189     }
190   }
191
192   protected static class SetArticleFieldOperation extends ArticleModifyingOperation {
193     private String field;
194     private String value;
195
196     public SetArticleFieldOperation(String aName, String aField, String aValue) {
197       super(aName);
198
199       field = aField;
200       value = aValue;
201     }
202
203     protected boolean isAvailable(EntityContent anArticle) {
204       return anArticle.getValue(field) == null || !anArticle.getValue(field).equals(value);
205     }
206
207     protected void performModification(EntityContent anArticle) throws StorageObjectException {
208       anArticle.setValueForProperty(field, value);
209     }
210   }
211
212   protected static class ChangeArticleFieldOperation extends ArticleModifyingOperation {
213     private String field;
214     private String oldValue;
215     private String newValue;
216
217     public ChangeArticleFieldOperation(String aName, String aField, String anOldValue, String aNewValue) {
218       super(aName);
219
220       field = aField;
221       newValue = aNewValue;
222       oldValue = anOldValue;
223     }
224
225     protected boolean isAvailable(EntityContent anArticle) {
226       return anArticle.getValue(field) != null && anArticle.getValue(field).equals(oldValue);
227     }
228
229     protected void performModification(EntityContent anArticle) throws StorageObjectException {
230       anArticle.setValueForProperty(field, newValue);
231     }
232   }
233 }