restriction of user activity: concept of super-users: the only ones that can add...
[mir.git] / source / mircoders / servlet / ServletModuleLocalizer.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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.servlet;
31
32 import java.util.*;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import javax.servlet.http.HttpSession;
37
38 import mir.entity.adapter.EntityAdapter;
39 import mir.log.LoggerWrapper;
40 import mir.servlet.ServletModule;
41 import mir.servlet.ServletModuleExc;
42 import mir.servlet.ServletModuleFailure;
43 import mir.util.*;
44 import mircoders.entity.EntityComment;
45 import mircoders.entity.EntityContent;
46 import mircoders.entity.EntityUsers;
47 import mircoders.global.MirGlobal;
48 import mircoders.localizer.MirAdminInterfaceLocalizer;
49 import mircoders.module.ModuleComment;
50 import mircoders.module.ModuleContent;
51 import mircoders.storage.DatabaseComment;
52 import mircoders.storage.DatabaseContent;
53
54 public class ServletModuleLocalizer extends ServletModule {
55   private static ServletModuleLocalizer instance = new ServletModuleLocalizer();
56   public static ServletModule getInstance() { return instance; }
57
58   private ModuleContent contentModule;
59   private ModuleComment commentModule;
60   private List administerOperations;
61
62   private ServletModuleLocalizer() {
63     try {
64       logger = new LoggerWrapper("ServletModule.Localizer");
65
66       contentModule = new ModuleContent(DatabaseContent.getInstance());
67       commentModule = new ModuleComment(DatabaseComment.getInstance());
68
69       administerOperations = new Vector();
70
71       String settings[] = configuration.getStringArray("Mir.Localizer.Admin.AdministerOperations");
72
73       if (settings!=null) {
74         for (int i = 0; i < settings.length; i++) {
75           String setting = settings[i].trim();
76
77           if (setting.length() > 0) {
78             List parts = StringRoutines.splitString(setting, ":");
79             if (parts.size() != 2) {
80               logger.error("config error: " + settings[i] + ", 2 parts expected");
81             }
82             else {
83               Map entry = new HashMap();
84               entry.put("name", (String) parts.get(0));
85               entry.put("url", (String) parts.get(1));
86               administerOperations.add(entry);
87             }
88           }
89         }
90       }
91     }
92     catch (Exception e) {
93       logger.error("ServletModuleLocalizer could not be initialized: " + e.getMessage());
94     }
95
96
97   }
98
99   private EntityAdapter getActiveUser(HttpServletRequest aRequest) throws ServletModuleExc {
100     try {
101       HttpSession session = aRequest.getSession(false);
102       return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter
103           ("user", ServletHelper.getUser(aRequest));
104     }
105     catch (Throwable e) {
106       throw new ServletModuleFailure("ServletModuleLocalizer.getActiveUser: " + e.getMessage(), e);
107     }
108   }
109
110   public void performCommentOperation(EntityAdapter aUser, String anId, String anOperation) {
111     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
112     EntityAdapter comment;
113     EntityComment entity;
114
115     try {
116       entity = (EntityComment) commentModule.getById(anId);
117
118       if (entity != null) {
119         comment = MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("comment", entity);
120         operation = MirGlobal.localizer().adminInterface().simpleCommentOperationForName(anOperation);
121         operation.perform(aUser, comment);
122         logger.info("Operation " + anOperation + " successfully performed on comment " + anId);
123       }
124       else {
125         logger.error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");
126       }
127     }
128     catch (Throwable e) {
129       logger.error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage());
130     }
131   }
132
133   public void commentoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
134     String commentIdString = aRequest.getParameter("id");
135     String operationString = aRequest.getParameter("operation");
136     String returnUrlString = aRequest.getParameter("returnurl");
137
138     performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
139
140     redirect(aResponse, returnUrlString);
141   }
142
143   public void commentoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
144     String returnUrlString = aRequest.getParameter("returnurl");
145
146     String[] operations = aRequest.getParameterValues("operation");
147
148     if (operations!=null) {
149       for (int i = 0; i < operations.length; i++) {
150         if (operations[i].length() > 0) {
151           List parts = StringRoutines.splitString(operations[i], ";");
152
153           if (parts.size() != 2) {
154             logger.error("commentoperationbatch: operation string invalid: " +
155                          operations[i]);
156           }
157           else {
158             String commentIdString = (String) parts.get(0);
159             String operationString = (String) parts.get(1);
160
161             performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
162           }
163         }
164       }
165     }
166
167     redirect(aResponse, returnUrlString);
168   }
169
170   public void performArticleOperation(EntityAdapter aUser, String anId, String anOperation) {
171     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
172     EntityAdapter article;
173     EntityContent entity;
174
175     try {
176       entity = (EntityContent) contentModule.getById(anId);
177
178       if (entity != null) {
179         article = MirGlobal.localizer().dataModel().adapterModel().
180             makeEntityAdapter("content", entity);
181         operation = MirGlobal.localizer().adminInterface().
182             simpleArticleOperationForName(anOperation);
183         operation.perform(aUser, article);
184         logger.info("Operation " + anOperation + " successfully performed on article " + anId);
185       }
186       else {
187         logger.error("Error while performing " + anOperation + " on article " + anId + ": article is null");
188       }
189     }
190     catch (Throwable e) {
191       logger.error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());
192     }
193   }
194
195   public void articleoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
196     String articleIdString = aRequest.getParameter("articleid");
197     String operationString = aRequest.getParameter("operation");
198     String returnUrlString = aRequest.getParameter("returnurl");
199
200     performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
201     redirect(aResponse, returnUrlString);
202   }
203
204   public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
205     String returnUrlString = aRequest.getParameter("returnurl");
206
207     String[] operations = aRequest.getParameterValues("operation");
208
209     if (operations!=null) {
210
211       for (int i = 0; i < operations.length; i++) {
212         if (operations[i].length() > 0) {
213           List parts = StringRoutines.splitString(operations[i], ";");
214
215           if (parts.size() != 2) {
216             logger.error("articleoperationbatch: operation string invalid: " + operations[i]);
217           }
218           else {
219             String articleIdString = (String) parts.get(0);
220             String operationString = (String) parts.get(1);
221
222             performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
223           }
224         }
225       }
226     }
227
228     redirect(aResponse, returnUrlString);
229   }
230
231   public List getAdministerOperations() throws ServletModuleExc {
232     return administerOperations;
233   }
234 }