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