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