new admin templates! with many thanks to init...
[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 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.
30  */
31
32 package mircoders.servlet;
33
34 import java.util.List;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.servlet.http.HttpSession;
39
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;
55
56 public class ServletModuleLocalizer extends ServletModule {
57   private static ServletModuleLocalizer instance = new ServletModuleLocalizer();
58   public static ServletModule getInstance() { return instance; }
59
60   private ModuleContent contentModule;
61   private ModuleComment commentModule;
62
63   private ServletModuleLocalizer() {
64     try {
65       contentModule = new ModuleContent(DatabaseContent.getInstance());
66       commentModule = new ModuleComment(DatabaseComment.getInstance());
67
68       logger = new LoggerWrapper("ServletModule.Localizer");
69     }
70     catch (Exception e) {
71       logger.error("ServletModuleLocalizer could not be initialized: " + e.getMessage());
72     }
73   }
74
75   private EntityAdapter getActiveUser(HttpServletRequest aRequest) throws ServletModuleExc {
76     try {
77       HttpSession session = aRequest.getSession(false);
78       return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter
79           ("user", (EntityUsers) session.getAttribute("login.uid"));
80     }
81     catch (Throwable e) {
82       throw new ServletModuleFailure("ServletModuleLocalizer.getActiveUser: " + e.getMessage(), e);
83     }
84   }
85
86   public void performCommentOperation(EntityAdapter aUser, String anId, String anOperation) {
87     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
88     EntityAdapter comment;
89     EntityComment entity;
90
91     try {
92       entity = (EntityComment) commentModule.getById(anId);
93
94       if (entity != null) {
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);
99       }
100       logger.error("Error while performing " + anOperation + " on comment " + anId + ": comment is null");
101     }
102     catch (Throwable e) {
103       logger.error("Error while performing " + anOperation + " on comment " + anId + ": " + e.getMessage());
104     }
105   }
106
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");
111
112     performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
113
114     redirect(aResponse, returnUrlString);
115   }
116
117   public void commentoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
118     String returnUrlString = aRequest.getParameter("returnurl");
119
120     String[] operations = aRequest.getParameterValues("operation");
121
122     for (int i=0; i<operations.length; i++) {
123       if (operations[i].length()>0) {
124         List parts = StringRoutines.splitString(operations[i], ";");
125
126         if (parts.size() != 2) {
127           logger.error("commentoperationbatch: operation string invalid: " +
128                        operations[i]);
129         }
130         else {
131           String commentIdString = (String) parts.get(0);
132           String operationString = (String) parts.get(1);
133
134           performCommentOperation(getActiveUser(aRequest), commentIdString, operationString);
135         }
136       }
137     }
138
139     redirect(aResponse, returnUrlString);
140   }
141
142   public void performArticleOperation(EntityAdapter aUser, String anId, String anOperation) {
143     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation;
144     EntityAdapter article;
145     EntityContent entity;
146
147     try {
148       entity = (EntityContent) contentModule.getById(anId);
149
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);
157       }
158       logger.error("Error while performing " + anOperation + " on article " + anId + ": article is null");
159     }
160     catch (Throwable e) {
161       logger.error("Error while performing " + anOperation + " on article " + anId + ": " + e.getMessage());
162     }
163   }
164
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");
169
170     performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
171     redirect(aResponse, returnUrlString);
172   }
173
174   public void articleoperationbatch(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
175     String returnUrlString = aRequest.getParameter("returnurl");
176
177     String[] operations = aRequest.getParameterValues("operation");
178
179     for (int i=0; i<operations.length; i++) {
180       if (operations[i].length()>0) {
181         List parts = StringRoutines.splitString(operations[i], ";");
182
183         if (parts.size() != 2) {
184           logger.error("articleoperationbatch: operation string invalid: " + operations[i]);
185         }
186         else {
187           String articleIdString = (String) parts.get(0);
188           String operationString = (String) parts.get(1);
189
190           performArticleOperation(getActiveUser(aRequest), articleIdString, operationString);
191         }
192       }
193     }
194
195     redirect(aResponse, returnUrlString);
196   }
197
198 }