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