merge of localization branch into HEAD. mh and zap
[mir.git] / source / mircoders / localizer / basic / MirBasicAdminInterfaceLocalizer.java
1 package mircoders.localizer.basic;
2
3 import java.util.*;
4 import mir.entity.adapter.*;
5 import mir.storage.*;
6 import mir.entity.*;
7 import mircoders.localizer.*;
8 import mircoders.entity.*;
9 import mircoders.storage.*;
10
11
12 public class MirBasicAdminInterfaceLocalizer implements MirAdminInterfaceLocalizer {
13   private Map simpleCommentOperations;
14   private Map simpleArticleOperations;
15
16   public MirBasicAdminInterfaceLocalizer() throws MirLocalizerFailure, MirLocalizerException {
17     simpleCommentOperations = new HashMap();
18     simpleArticleOperations = new HashMap();
19
20     buildSimpleCommentOperations(simpleCommentOperations);
21     buildSimpleArticleOperations(simpleArticleOperations);
22   }
23
24   public Map simpleCommentOperations() {
25     return simpleCommentOperations;
26   };
27
28   public Map simpleArticleOperations() {
29     return simpleArticleOperations;
30   };
31
32   public void buildSimpleCommentOperations(Map anOperations) throws MirLocalizerFailure, MirLocalizerException {
33     anOperations.put("hide", new HideCommentOperation());
34     anOperations.put("unhide", new UnhideCommentOperation());
35   };
36
37   public void buildSimpleArticleOperations(Map anOperations)  throws MirLocalizerFailure, MirLocalizerException {
38     anOperations.put("hide", new HideArticleOperation());
39     anOperations.put("unhide", new UnhideArticleOperation());
40   };
41
42   protected abstract static class EntityModifyingOperation implements MirSimpleEntityOperation {
43     public boolean isAvailable(EntityAdapter anEntity) {
44       try {
45
46         Entity entity = anEntity.getEntity();
47         return (entity instanceof EntityComment) && isAvailable((EntityComment) entity);
48       }
49       catch (Throwable t) {
50         return false;
51       }
52     };
53
54     public void perform(EntityAdapter anEntity) {
55       Entity entity = anEntity.getEntity();
56       try {
57         performModification(entity);
58         entity.update();
59       }
60       catch (Throwable t) {
61       }
62     };
63
64     protected abstract boolean isAvailable(Entity anEntity) throws StorageObjectException ;
65     protected abstract void performModification(Entity anEntity) throws StorageObjectException ;
66   }
67
68   public static abstract class CommentModifyingOperation extends EntityModifyingOperation {
69     protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
70       return anEntity instanceof EntityComment && isAvailable((EntityComment) anEntity);
71     }
72
73     protected void performModification(Entity anEntity) throws StorageObjectException {
74       performModification((EntityComment) anEntity);
75       DatabaseContent.getInstance().setUnproduced("id="+anEntity.getValue("to_media"));
76     };
77
78     protected abstract boolean isAvailable(EntityComment aComment) throws StorageObjectException ;
79     protected abstract void performModification(EntityComment aComment) throws StorageObjectException ;
80   }
81
82   public static abstract class ArticleModifyingOperation extends EntityModifyingOperation {
83     protected boolean isAvailable(Entity anEntity) throws StorageObjectException {
84       return anEntity instanceof EntityContent && isAvailable((EntityComment) anEntity);
85     }
86
87     protected void performModification(Entity anEntity) throws StorageObjectException {
88       performModification((EntityContent) anEntity);
89       anEntity.setValueForProperty("is_produced", "0");
90     };
91
92     protected abstract boolean isAvailable(EntityContent anArticle) throws StorageObjectException ;
93     protected abstract void performModification(EntityContent anArticle) throws StorageObjectException ;
94   }
95
96   private static class HideCommentOperation extends CommentModifyingOperation {
97     protected boolean isAvailable(EntityComment aComment) {
98       return aComment.getValue("is_published").equals("1");
99     }
100     protected void performModification(EntityComment aComment) throws StorageObjectException {
101       aComment.setValueForProperty("is_published", "0");
102     }
103   }
104
105   private static class UnhideCommentOperation extends CommentModifyingOperation {
106     protected boolean isAvailable(EntityComment aComment) {
107       return aComment.getValue("is_published").equals("0");
108     }
109     protected void performModification(EntityComment aComment) throws StorageObjectException {
110       aComment.setValueForProperty("is_published", "1");
111     }
112   }
113
114   private static class HideArticleOperation extends ArticleModifyingOperation {
115     protected boolean isAvailable(EntityContent anArticle) {
116       return anArticle.getValue("is_published").equals("1");
117     }
118     protected void performModification(EntityContent anArticle) throws StorageObjectException {
119       anArticle.setValueForProperty("is_published", "0");
120     }
121   }
122
123   private static class UnhideArticleOperation extends ArticleModifyingOperation {
124     protected boolean isAvailable(EntityContent anArticle) {
125       return anArticle.getValue("is_published").equals("0");
126     }
127     protected void performModification(EntityContent anArticle) throws StorageObjectException {
128       anArticle.setValueForProperty("is_published", "1");
129     }
130   }
131 }