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