adminster operation configuration now supports escape characters
[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 mir.servlet.AdminServletModule;
33 import mir.servlet.ServletModuleExc;
34 import mir.util.StringRoutines;
35 import mircoders.entity.EntityComment;
36 import mircoders.entity.EntityContent;
37 import mircoders.global.MirGlobal;
38 import mircoders.module.ModuleComment;
39 import mircoders.module.ModuleContent;
40
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 public class ServletModuleLocalizer extends AdminServletModule {
49   private final ModuleContent contentModule = new ModuleContent();
50   private final ModuleComment commentModule = new ModuleComment();
51   private final List administerOperations = new ArrayList();
52
53   public ServletModuleLocalizer() {
54     try {
55       String settings[] = 
56           getConfiguration().getStringArray("Mir.Localizer.Admin.AdministerOperations");
57
58       if (settings!=null) {
59         for (int i = 0; i < settings.length; i++) {
60           String setting = settings[i].trim();
61
62           if (setting.length() > 0) {
63             List parts = StringRoutines.splitStringWithEscape(setting, ':', '\\');
64             if (parts.size() != 2) {
65               getLogger().error("config error: " + settings[i] + ", 2 parts expected");
66             }
67             else {
68               Map entry = new HashMap();
69               entry.put("name", parts.get(0));
70               entry.put("url", parts.get(1));
71               administerOperations.add(entry);
72             }
73           }
74         }
75       }
76     }
77     catch (Exception e) {
78       getLogger().error("ServletModuleLocalizer could not be initialized" + e.getMessage(), e);
79     }
80   }
81
82   /**
83    * Performs a localizer operation on an article.
84    *
85    * See also
86    * {@link mircoders.localizer.MirAdminInterfaceLocalizer#simpleArticleOperations()}
87    *
88    * @param aRequest       The originating request
89    * @param anId           The id of the article
90    * @param anOperation    The identifier of the operation to perform
91    */
92   public void performCommentOperation(HttpServletRequest aRequest, String anId, String anOperation) {
93     try {
94       EntityComment entity = (EntityComment) commentModule.getById(anId);
95
96       if (entity != null) {
97         MirGlobal.performCommentOperation(ServletHelper.getUser(aRequest), entity, anOperation);
98         getLogger().debug("Operation " + anOperation + " successfully performed on comment " + anId);
99         logAdminUsage(aRequest, "comment."+anId, "operation " + anOperation + " performed");
100       }
101       else {
102         getLogger().error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");
103       }
104     }
105     catch (Throwable e) {
106       getLogger().error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage(), e);
107     }
108   }
109
110   public void commentoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
111     String commentIdString = aRequest.getParameter("id");
112     String operationString = aRequest.getParameter("operation");
113     String returnUrlString = aRequest.getParameter("returnurl");
114
115     performCommentOperation(aRequest, commentIdString, operationString);
116
117     ServletHelper.redirect(aResponse, returnUrlString);
118   }
119
120   public void operation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
121     String type = aRequest.getParameter("objecttype");
122     String id = aRequest.getParameter("id");
123     String operation = aRequest.getParameter("operation");
124     String returnUrl = aRequest.getParameter("returnurl");
125
126
127     if ("comment".equals(type)) {
128       performCommentOperation(aRequest, id, operation);
129     }
130     else {
131       performArticleOperation(aRequest, id, operation);
132     }
133
134     ServletHelper.redirect(aResponse, returnUrl);
135   }
136
137   public void commentoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
138     String returnUrlString = aRequest.getParameter("returnurl");
139
140     String[] operations = aRequest.getParameterValues("operation");
141
142     if (operations!=null) {
143       for (int i = 0; i < operations.length; i++) {
144         if (operations[i].length() > 0) {
145           List parts = StringRoutines.splitString(operations[i], ";");
146
147           if (parts.size() != 2) {
148             getLogger().error("commentoperationbatch: operation string invalid: " +
149                          operations[i]);
150           }
151           else {
152             String commentIdString = (String) parts.get(0);
153             String operationString = (String) parts.get(1);
154
155             performCommentOperation(aRequest, commentIdString, operationString);
156           }
157         }
158       }
159     }
160
161     ServletHelper.redirect(aResponse, returnUrlString);
162   }
163
164   public void operationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
165     String returnUrlString = aRequest.getParameter("returnurl");
166
167     String[] operations = aRequest.getParameterValues("operation");
168
169     if (operations!=null) {
170       for (int i = 0; i < operations.length; i++) {
171         if (operations[i].length() > 0) {
172           List parts = StringRoutines.splitString(operations[i], ";");
173
174           if (parts.size() != 3) {
175             getLogger().error("commentoperationbatch: operation string invalid: " +
176                          operations[i]);
177           }
178           else {
179             String type = (String) parts.get(0);
180             String id = (String) parts.get(1);
181             String operationString = (String) parts.get(2);
182
183             if ("comment".equals(type)) {
184               performCommentOperation(aRequest, id, operationString);
185             }
186             else {
187               performArticleOperation(aRequest, id, operationString);
188             }
189           }
190         }
191       }
192     }
193
194     ServletHelper.redirect(aResponse, returnUrlString);
195   }
196   public void performArticleOperation(HttpServletRequest aRequest, String anId, String anOperation) {
197     EntityContent entity;
198
199     try {
200       entity = (EntityContent) contentModule.getById(anId);
201
202       if (entity != null) {
203         MirGlobal.performArticleOperation(ServletHelper.getUser(aRequest), entity, anOperation);
204         getLogger().info("Operation " + anOperation + " successfully performed on article " + anId);
205         logAdminUsage(aRequest, "article." + anId, "operation " + anOperation + " performed");
206       }
207       else {
208         getLogger().error("Error while performing " + anOperation + " on article " + anId + ": article is null");
209       }
210     }
211     catch (Throwable e) {
212       getLogger().error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());
213     }
214   }
215
216   public void articleoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
217     String articleIdString = aRequest.getParameter("articleid");
218     String operationString = aRequest.getParameter("operation");
219     String returnUrlString = aRequest.getParameter("returnurl");
220
221     performArticleOperation(aRequest, articleIdString, operationString);
222     ServletHelper.redirect(aResponse, returnUrlString);
223   }
224
225   public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
226     String returnUrlString = aRequest.getParameter("returnurl");
227
228     String[] operations = aRequest.getParameterValues("operation");
229
230     if (operations!=null) {
231
232       for (int i = 0; i < operations.length; i++) {
233         if (operations[i].length() > 0) {
234           List parts = StringRoutines.splitString(operations[i], ";");
235
236           if (parts.size() != 2) {
237             getLogger().error("articleoperationbatch: operation string invalid: " + operations[i]);
238           }
239           else {
240             String articleIdString = (String) parts.get(0);
241             String operationString = (String) parts.get(1);
242
243             performArticleOperation(aRequest, articleIdString, operationString);
244           }
245         }
246       }
247     }
248
249     ServletHelper.redirect(aResponse, returnUrlString);
250   }
251
252   public List getAdministerOperations() throws ServletModuleExc {
253     return administerOperations;
254   }
255 }