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