2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.servlet;
34 import java.util.List;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.servlet.http.HttpSession;
40 import mir.entity.adapter.EntityAdapter;
41 import mir.log.LoggerWrapper;
42 import mir.servlet.ServletModule;
43 import mir.servlet.ServletModuleExc;
44 import mir.servlet.ServletModuleFailure;
45 import mir.util.StringRoutines;
46 import mircoders.entity.EntityComment;
47 import mircoders.entity.EntityContent;
48 import mircoders.entity.EntityUsers;
49 import mircoders.global.MirGlobal;
50 import mircoders.localizer.MirAdminInterfaceLocalizer;
51 import mircoders.module.ModuleComment;
52 import mircoders.module.ModuleContent;
53 import mircoders.storage.DatabaseComment;
54 import mircoders.storage.DatabaseContent;
56 public class ServletModuleLocalizer extends ServletModule {
57 private static ServletModuleLocalizer instance = new ServletModuleLocalizer();
58 public static ServletModule getInstance() { return instance; }
60 private ModuleContent contentModule;
61 private ModuleComment commentModule;
63 private ServletModuleLocalizer() {
65 contentModule = new ModuleContent(DatabaseContent.getInstance());
66 commentModule = new ModuleComment(DatabaseComment.getInstance());
68 logger = new LoggerWrapper("ServletModule.Localizer");
71 logger.error("ServletModuleLocalizer could not be initialized: " + e.getMessage());
75 private EntityAdapter getActiveUser(HttpServletRequest aRequest) throws ServletModuleExc {
77 HttpSession session = aRequest.getSession(false);
78 return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter
79 ("user", (EntityUsers) session.getAttribute("login.uid"));
82 throw new ServletModuleFailure("ServletModuleLocalizer.getActiveUser: " + e.getMessage(), e);
86 public void performCommentOperation(EntityAdapter aUser, String anId, String anOperation) {
87 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
88 EntityAdapter comment;
92 entity = (EntityComment) commentModule.getById(anId);
95 comment = MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("comment", entity);
96 operation = MirGlobal.localizer().adminInterface().simpleCommentOperationForName(anOperation);
97 operation.perform(aUser, comment);
98 logger.info("Operation " + anOperation + " successfully performed on comment " + anId);
100 logger.error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");
102 catch (Throwable e) {
103 logger.error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage());
107 public void commentoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
108 String commentIdString = aRequest.getParameter("id");
109 String operationString = aRequest.getParameter("operation");
110 String returnUrlString = aRequest.getParameter("returnurl");
112 performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
114 redirect(aResponse, returnUrlString);
117 public void commentoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
118 String returnUrlString = aRequest.getParameter("returnurl");
120 String[] operations = aRequest.getParameterValues("operation");
122 for (int i=0; i<operations.length; i++) {
123 if (operations[i].length()>0) {
124 List parts = StringRoutines.splitString(operations[i], ";");
126 if (parts.size() != 2) {
127 logger.error("commentoperationbatch: operation string invalid: " +
131 String commentIdString = (String) parts.get(0);
132 String operationString = (String) parts.get(1);
134 performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
139 redirect(aResponse, returnUrlString);
142 public void performArticleOperation(EntityAdapter aUser, String anId, String anOperation) {
143 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
144 EntityAdapter article;
145 EntityContent entity;
148 entity = (EntityContent) contentModule.getById(anId);
150 if (entity != null) {
151 article = MirGlobal.localizer().dataModel().adapterModel().
152 makeEntityAdapter("content", entity);
153 operation = MirGlobal.localizer().adminInterface().
154 simpleArticleOperationForName(anOperation);
155 operation.perform(aUser, article);
156 logger.info("Operation " + anOperation + " successfully performed on article " + anId);
158 logger.error("Error while performing " + anOperation + " on article " + anId + ": article is null");
160 catch (Throwable e) {
161 logger.error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());
165 public void articleoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
166 String articleIdString = aRequest.getParameter("articleid");
167 String operationString = aRequest.getParameter("operation");
168 String returnUrlString = aRequest.getParameter("returnurl");
170 performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
171 redirect(aResponse, returnUrlString);
174 public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
175 String returnUrlString = aRequest.getParameter("returnurl");
177 String[] operations = aRequest.getParameterValues("operation");
179 for (int i=0; i<operations.length; i++) {
180 if (operations[i].length()>0) {
181 List parts = StringRoutines.splitString(operations[i], ";");
183 if (parts.size() != 2) {
184 logger.error("articleoperationbatch: operation string invalid: " + operations[i]);
187 String articleIdString = (String) parts.get(0);
188 String operationString = (String) parts.get(1);
190 performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
195 redirect(aResponse, returnUrlString);