bc6214b3e0227b7187bb282e1604b674396871f4
[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.List;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import javax.servlet.http.HttpSession;
37
38 import mir.entity.adapter.EntityAdapter;
39 import mir.log.LoggerWrapper;
40 import mir.servlet.ServletModule;
41 import mir.servlet.ServletModuleExc;
42 import mir.servlet.ServletModuleFailure;
43 import mir.util.StringRoutines;
44 import mircoders.entity.EntityComment;
45 import mircoders.entity.EntityContent;
46 import mircoders.entity.EntityUsers;
47 import mircoders.global.MirGlobal;
48 import mircoders.localizer.MirAdminInterfaceLocalizer;
49 import mircoders.module.ModuleComment;
50 import mircoders.module.ModuleContent;
51 import mircoders.storage.DatabaseComment;
52 import mircoders.storage.DatabaseContent;
53
54 public class ServletModuleLocalizer extends ServletModule {
55   private static ServletModuleLocalizer instance = new ServletModuleLocalizer();
56   public static ServletModule getInstance() { return instance; }
57
58   private ModuleContent contentModule;
59   private ModuleComment commentModule;
60
61   private ServletModuleLocalizer() {
62     try {
63       contentModule = new ModuleContent(DatabaseContent.getInstance());
64       commentModule = new ModuleComment(DatabaseComment.getInstance());
65
66       logger = new LoggerWrapper("ServletModule.Localizer");
67     }
68     catch (Exception e) {
69       logger.error("ServletModuleLocalizer could not be initialized: " + e.getMessage());
70     }
71   }
72
73   private EntityAdapter getActiveUser(HttpServletRequest aRequest) throws ServletModuleExc {
74     try {
75       HttpSession session = aRequest.getSession(false);
76       return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter
77           ("user", (EntityUsers) session.getAttribute("login.uid"));
78     }
79     catch (Throwable e) {
80       throw new ServletModuleFailure("ServletModuleLocalizer.getActiveUser: " + e.getMessage(), e);
81     }
82   }
83
84   public void performCommentOperation(EntityAdapter aUser, String anId, String anOperation) {
85     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
86     EntityAdapter comment;
87     EntityComment entity;
88
89     try {
90       entity = (EntityComment) commentModule.getById(anId);
91
92       if (entity != null) {
93         comment = MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("comment", entity);
94         operation = MirGlobal.localizer().adminInterface().simpleCommentOperationForName(anOperation);
95         operation.perform(aUser, comment);
96         logger.info("Operation " + anOperation + " successfully performed on comment " + anId);
97       }
98       logger.error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");
99     }
100     catch (Throwable e) {
101       logger.error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage());
102     }
103   }
104
105   public void commentoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
106     String commentIdString = aRequest.getParameter("id");
107     String operationString = aRequest.getParameter("operation");
108     String returnUrlString = aRequest.getParameter("returnurl");
109
110     performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
111
112     redirect(aResponse, returnUrlString);
113   }
114
115   public void commentoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
116     String returnUrlString = aRequest.getParameter("returnurl");
117
118     String[] operations = aRequest.getParameterValues("operation");
119
120     for (int i=0; i<operations.length; i++) {
121       if (operations[i].length()>0) {
122         List parts = StringRoutines.splitString(operations[i], ";");
123
124         if (parts.size() != 2) {
125           logger.error("commentoperationbatch: operation string invalid: " +
126                        operations[i]);
127         }
128         else {
129           String commentIdString = (String) parts.get(0);
130           String operationString = (String) parts.get(1);
131
132           performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
133         }
134       }
135     }
136
137     redirect(aResponse, returnUrlString);
138   }
139
140   public void performArticleOperation(EntityAdapter aUser, String anId, String anOperation) {
141     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
142     EntityAdapter article;
143     EntityContent entity;
144
145     try {
146       entity = (EntityContent) contentModule.getById(anId);
147
148       if (entity != null) {
149         article = MirGlobal.localizer().dataModel().adapterModel().
150             makeEntityAdapter("content", entity);
151         operation = MirGlobal.localizer().adminInterface().
152             simpleArticleOperationForName(anOperation);
153         operation.perform(aUser, article);
154         logger.info("Operation " + anOperation + " successfully performed on article " + anId);
155       }
156       logger.error("Error while performing " + anOperation + " on article " + anId + ": article is null");
157     }
158     catch (Throwable e) {
159       logger.error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());
160     }
161   }
162
163   public void articleoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
164     String articleIdString = aRequest.getParameter("articleid");
165     String operationString = aRequest.getParameter("operation");
166     String returnUrlString = aRequest.getParameter("returnurl");
167
168     performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
169     redirect(aResponse, returnUrlString);
170   }
171
172   public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
173     String returnUrlString = aRequest.getParameter("returnurl");
174
175     String[] operations = aRequest.getParameterValues("operation");
176
177     for (int i=0; i<operations.length; i++) {
178       if (operations[i].length()>0) {
179         List parts = StringRoutines.splitString(operations[i], ";");
180
181         if (parts.size() != 2) {
182           logger.error("articleoperationbatch: operation string invalid: " + operations[i]);
183         }
184         else {
185           String articleIdString = (String) parts.get(0);
186           String operationString = (String) parts.get(1);
187
188           performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
189         }
190       }
191     }
192
193     redirect(aResponse, returnUrlString);
194   }
195
196 }