code cleaning, new config
[mir.git] / source / mircoders / servlet / ServletModuleLocalizer.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mircoders.servlet;\r
33 \r
34 import java.util.List;\r
35 \r
36 import javax.servlet.http.HttpServletRequest;\r
37 import javax.servlet.http.HttpServletResponse;\r
38 import javax.servlet.http.HttpSession;\r
39 \r
40 import mir.entity.adapter.EntityAdapter;\r
41 import mir.log.LoggerWrapper;\r
42 import mir.servlet.ServletModule;\r
43 import mir.servlet.ServletModuleException;\r
44 import mir.util.StringRoutines;\r
45 import mircoders.entity.EntityComment;\r
46 import mircoders.entity.EntityContent;\r
47 import mircoders.entity.EntityUsers;\r
48 import mircoders.global.MirGlobal;\r
49 import mircoders.localizer.MirAdminInterfaceLocalizer;\r
50 import mircoders.module.ModuleComment;\r
51 import mircoders.module.ModuleContent;\r
52 import mircoders.storage.DatabaseComment;\r
53 import mircoders.storage.DatabaseContent;\r
54 \r
55 public class ServletModuleLocalizer extends ServletModule {\r
56   private static ServletModuleLocalizer instance = new ServletModuleLocalizer();\r
57   public static ServletModule getInstance() { return instance; }\r
58 \r
59   private ModuleContent contentModule;\r
60   private ModuleComment commentModule;\r
61 \r
62   private ServletModuleLocalizer() {\r
63     try {\r
64       contentModule = new ModuleContent(DatabaseContent.getInstance());\r
65       commentModule = new ModuleComment(DatabaseComment.getInstance());\r
66 \r
67       logger = new LoggerWrapper("ServletModule.Localizer");\r
68     }\r
69     catch (Exception e) {\r
70       logger.error("ServletModuleLocalizer could not be initialized: " + e.getMessage());\r
71     }\r
72   }\r
73 \r
74   private EntityAdapter getActiveUser(HttpServletRequest aRequest) throws ServletModuleException {\r
75     try {\r
76       HttpSession session = aRequest.getSession(false);\r
77       return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter\r
78           ("user", (EntityUsers) session.getAttribute("login.uid"));\r
79     }\r
80     catch (Exception e) {\r
81       throw new ServletModuleException("ServletModuleLocalizer.getActiveUser: " + e.getMessage());\r
82     }\r
83   }\r
84 \r
85   public void performCommentOperation(EntityAdapter aUser, String anId, String anOperation) {\r
86     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;\r
87     EntityAdapter comment;\r
88     EntityComment entity;\r
89 \r
90     try {\r
91       entity = (EntityComment) commentModule.getById(anId);\r
92 \r
93       if (entity != null) {\r
94         comment = MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("comment", entity);\r
95         operation = MirGlobal.localizer().adminInterface().simpleCommentOperationForName(anOperation);\r
96         operation.perform(aUser, comment);\r
97         logger.info("Operation " + anOperation + " successfully performed on comment " + anId);\r
98       }\r
99       logger.error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");\r
100     }\r
101     catch (Throwable e) {\r
102       logger.error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage());\r
103     }\r
104   }\r
105 \r
106   public void commentoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {\r
107     String commentIdString = aRequest.getParameter("id");\r
108     String operationString = aRequest.getParameter("operation");\r
109     String returnUrlString = aRequest.getParameter("returnurl");\r
110 \r
111     performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);\r
112 \r
113     redirect(aResponse, returnUrlString);\r
114   }\r
115 \r
116   public void commentoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {\r
117     String returnUrlString = aRequest.getParameter("returnurl");\r
118 \r
119     String[] operations = aRequest.getParameterValues("operation");\r
120 \r
121     for (int i=0; i<operations.length; i++) {\r
122       if (operations[i].length()>0) {\r
123         List parts = StringRoutines.splitString(operations[i], ";");\r
124 \r
125         if (parts.size() != 2) {\r
126           logger.error("commentoperationbatch: operation string invalid: " +\r
127                        operations[i]);\r
128         }\r
129         else {\r
130           String commentIdString = (String) parts.get(0);\r
131           String operationString = (String) parts.get(1);\r
132 \r
133           performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);\r
134         }\r
135       }\r
136     }\r
137 \r
138     redirect(aResponse, returnUrlString);\r
139   }\r
140 \r
141   public void performArticleOperation(EntityAdapter aUser, String anId, String anOperation) {\r
142     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;\r
143     EntityAdapter article;\r
144     EntityContent entity;\r
145 \r
146     try {\r
147       entity = (EntityContent) contentModule.getById(anId);\r
148 \r
149       if (entity != null) {\r
150         article = MirGlobal.localizer().dataModel().adapterModel().\r
151             makeEntityAdapter("content", entity);\r
152         operation = MirGlobal.localizer().adminInterface().\r
153             simpleArticleOperationForName(anOperation);\r
154         operation.perform(aUser, article);\r
155         logger.info("Operation " + anOperation + " successfully performed on article " + anId);\r
156       }\r
157       logger.error("Error while performing " + anOperation + " on article " + anId + ": article is null");\r
158     }\r
159     catch (Throwable e) {\r
160       logger.error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());\r
161     }\r
162   }\r
163 \r
164   public void articleoperation(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {\r
165     String articleIdString = aRequest.getParameter("articleid");\r
166     String operationString = aRequest.getParameter("operation");\r
167     String returnUrlString = aRequest.getParameter("returnurl");\r
168 \r
169     performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);\r
170     redirect(aResponse, returnUrlString);\r
171   }\r
172 \r
173   public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {\r
174     String returnUrlString = aRequest.getParameter("returnurl");\r
175 \r
176     String[] operations = aRequest.getParameterValues("operation");\r
177 \r
178     for (int i=0; i<operations.length; i++) {\r
179       if (operations[i].length()>0) {\r
180         List parts = StringRoutines.splitString(operations[i], ";");\r
181 \r
182         if (parts.size() != 2) {\r
183           logger.error("articleoperationbatch: operation string invalid: " +\r
184                        operations[i]);\r
185         }\r
186         else {\r
187           String articleIdString = (String) parts.get(0);\r
188           String operationString = (String) parts.get(1);\r
189 \r
190           performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);\r
191         }\r
192       }\r
193     }\r
194 \r
195     redirect(aResponse, returnUrlString);\r
196   }\r
197 \r
198 }