2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Locale;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession;
42 import mir.config.MirPropertiesConfiguration;
43 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
44 import mir.entity.adapter.EntityAdapterEngine;
45 import mir.entity.adapter.EntityAdapterModel;
46 import mir.log.LoggerWrapper;
47 import mir.module.AbstractModule;
48 import mir.storage.StorageObject;
49 import mir.util.HTTPRequestParser;
50 import mir.util.URLBuilder;
51 import mircoders.global.MirGlobal;
52 import mircoders.servlet.ServletHelper;
57 * <p>Description: </p>
58 * <p>Copyright: Copyright (c) 2003</p>
60 * @author not attributable
64 public abstract class ServletModule {
65 public String defaultAction;
66 protected LoggerWrapper logger;
67 protected MirPropertiesConfiguration configuration;
68 protected Locale fallbackLocale;
70 protected AbstractModule mainModule;
71 protected String definition;
72 protected EntityAdapterModel model;
74 protected String listGenerator;
75 protected String editGenerator;
76 protected String deleteConfirmationGenerator;
77 protected int nrEntitiesPerListPage;
80 public ServletModule(){
85 configuration = MirPropertiesConfiguration.instance();
87 catch (PropertiesConfigExc e) {
88 throw new RuntimeException("Can't get configuration: " + e.getMessage());
91 listGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".ListTemplate");
92 editGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".EditTemplate");
93 deleteConfirmationGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".DeleteConfirmationTemplate");
94 nrEntitiesPerListPage =
95 configuration.getInt("ServletModule."+getOperationModuleName()+".ListSize",
96 configuration.getInt("ServletModule.Default.ListSize", 20));
98 fallbackLocale = new Locale(configuration.getString("Mir.Admin.FallbackLanguage", "en"), "");
102 public void logAdminUsage(HttpServletRequest aRequest, String anObject, String aDescription) {
103 MirGlobal.logAdminUsage(ServletHelper.getUser(aRequest), getOperationModuleName() + ":" + anObject, aDescription);
107 * Singleton instance retrievel method. MUST be overridden in subclasses.
109 * @return ServletModule the single instance of the servletmodule class
111 public static ServletModule getInstance() {
116 * Get the module name
120 protected String getOperationModuleName() {
121 return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length());
125 * get the locale either from the session or the accept-language header ot the request
126 * this supersedes getLanguage for the new i18n
128 public Locale getLocale(HttpServletRequest aRequest) {
130 HttpSession session = aRequest.getSession(false);
131 if (session != null) {
132 // session can be null in case of logout
133 loc = (Locale) session.getAttribute("locale");
135 // if there is nothing in the session get it fron the accept-language
137 loc = aRequest.getLocale();
143 * get the locale either from the session or the accept-language header ot the request
144 * this supersedes getLanguage for the new i18n
146 public Locale getFallbackLocale(HttpServletRequest aRequest) {
147 return fallbackLocale;
151 * Function to specify the default ordering for lists. May be overridden.
156 public String getDefaultListOrdering() {
158 if (mainModule!=null && mainModule.getStorageObject()!=null){
159 if (mainModule.getStorageObject().getFields().contains("webdb_create"))
160 return "webdb_create desc";
170 * @throws ServletModuleExc
171 * @throws ServletModuleFailure
173 public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleExc, ServletModuleFailure {
175 aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery));
177 catch (Throwable t) {
178 throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t);
183 * Generic list method
187 * @throws ServletModuleExc
188 * @throws ServletModuleUserExc
189 * @throws ServletModuleFailure
192 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
194 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
196 String where = requestParser.getParameter("where");
197 String order = requestParser.getParameterWithDefault("order", getDefaultListOrdering());
198 int offset = requestParser.getIntegerWithDefault("offset", 0);
200 returnList(aRequest, aResponse, where, order, offset);
204 public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
205 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
207 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
208 URLBuilder urlBuilder = new URLBuilder();
212 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
215 EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset);
217 responseData.put("nexturl", null);
218 responseData.put("prevurl", null);
219 responseData.put("module", getOperationModuleName());
221 count=mainModule.getSize(aWhereClause);
223 urlBuilder.setValue("module", getOperationModuleName());
224 urlBuilder.setValue("do", "list");
225 urlBuilder.setValue("where", aWhereClause);
226 urlBuilder.setValue("order", anOrderByClause);
228 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
229 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
230 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
231 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
232 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
234 responseData.put("searchfield", requestParser.getParameter("searchfield"));
235 responseData.put("searchtext", requestParser.getParameter("searchtext"));
236 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
237 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
238 responseData.put("searchorder", requestParser.getParameter("searchorder"));
240 urlBuilder.setValue("offset", anOffset);
241 responseData.put("offset" , new Integer(anOffset).toString());
242 responseData.put("thisurl" , urlBuilder.getQuery());
244 if (count>anOffset+nrEntitiesPerListPage) {
245 urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
246 responseData.put("nexturl" , urlBuilder.getQuery());
250 urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
251 responseData.put("prevurl" , urlBuilder.getQuery());
254 responseData.put("entities", list);
255 responseData.put("from" , Integer.toString(anOffset+1));
256 responseData.put("count", Integer.toString(count));
257 responseData.put("to", Integer.toString(Math.min(anOffset+nrEntitiesPerListPage, count)));
259 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
261 catch (Throwable e) {
262 throw new ServletModuleFailure(e);
266 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc {
268 editObject(aRequest, aResponse, model.makeEntityAdapter(definition, mainModule.getById(anId)), false, anId);
270 catch (Throwable t) {
271 throw new ServletModuleFailure(t);
275 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, Object anObject, boolean anIsNew, String anId) throws ServletModuleExc {
276 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
277 URLBuilder urlBuilder = new URLBuilder();
278 EntityAdapterModel model;
281 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
283 responseData.put("module", getOperationModuleName());
284 responseData.put("entity", anObject);
285 responseData.put("new", new Boolean(anIsNew));
288 urlBuilder.setValue("module", getOperationModuleName());
289 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
291 urlBuilder.setValue("do", "add");
293 urlBuilder.setValue("id", anId);
294 urlBuilder.setValue("do", "edit");
296 responseData.put("returnurl", requestParser.getParameter("returnurl"));
297 responseData.put("thisurl", urlBuilder.getQuery());
299 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
301 catch (Throwable e) {
302 throw new ServletModuleFailure(e);
312 * @throws ServletModuleExc
313 * @throws ServletModuleUserExc
314 * @throws ServletModuleFailure
316 public void add(HttpServletRequest aRequest, HttpServletResponse aResponse)
317 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
319 Map object = new HashMap();
321 Iterator i = mainModule.getStorageObject().getFields().iterator();
324 object.put(i.next(), "");
326 initializeNewObject(object, aRequest, aResponse);
328 editObject(aRequest, aResponse, object, true, null);
331 protected void initializeNewObject(Map aNewObject, HttpServletRequest aRequest, HttpServletResponse aResponse) {
335 * Method called when the user edits an object.
339 * @throws ServletModuleExc
340 * @throws ServletModuleUserExc
341 * @throws ServletModuleFailure
343 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
344 edit(aRequest, aResponse, aRequest.getParameter("id"));
348 * Generic edit method
352 * @param anIdentifier
353 * @throws ServletModuleExc
354 * @throws ServletModuleUserExc
355 * @throws ServletModuleFailure
357 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse, String anIdentifier)
358 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
360 editObject(aRequest, aResponse, model.makeEntityAdapter(definition, mainModule.getById(anIdentifier)), false, anIdentifier);
362 catch (Throwable e) {
363 throw new ServletModuleFailure(e);
368 * Generic update method
372 * @throws ServletModuleExc
373 * @throws ServletModuleUserExc
374 * @throws ServletModuleFailure
376 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse)
377 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
379 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
381 String id = aRequest.getParameter("id");
382 Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());
383 mainModule.set(withValues);
385 logAdminUsage(aRequest, id, "object modified");
387 String returnUrl = requestParser.getParameter("returnurl");
389 if (returnUrl!=null) {
390 redirect(aResponse, returnUrl);
393 edit(aRequest, aResponse, id);
396 catch (Throwable e) {
397 throw new ServletModuleFailure(e);
402 * Generic insert method
406 * @throws ServletModuleExc
407 * @throws ServletModuleUserExc
408 * @throws ServletModuleFailure
410 public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse)
411 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
413 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
415 Map object = getIntersectingValues(aRequest, mainModule.getStorageObject());
417 String id = processInstertedObject(object, aRequest, aResponse);
419 logAdminUsage(aRequest, id, "object inserted");
421 String returnUrl = requestParser.getParameter("returnurl");
423 if (returnUrl!=null) {
424 redirect(aResponse, returnUrl);
427 edit(aRequest, aResponse, id);
430 catch (Throwable e) {
431 throw new ServletModuleFailure(e);
435 public String processInstertedObject(Map anObject, HttpServletRequest aRequest, HttpServletResponse aResponse) {
437 return mainModule.add(anObject);
439 catch (Throwable t) {
440 throw new ServletModuleFailure(t);
449 public void confirmdelete(HttpServletRequest aRequest, HttpServletResponse aResponse) {
451 String idParam = aRequest.getParameter("id");
452 String confirmParam = aRequest.getParameter("confirm");
453 String cancelParam = aRequest.getParameter("cancel");
455 if (confirmParam != null && !confirmParam.equals("")) {
456 mainModule.deleteById(idParam);
457 logAdminUsage(aRequest, idParam, "object deleted");
458 redirect(aResponse, aRequest.getParameter("okurl"));
461 redirect(aResponse, aRequest.getParameter("cancelurl"));
463 catch (Throwable t) {
464 throw new ServletModuleFailure(t);
472 * @throws ServletModuleExc
473 * @throws ServletModuleUserExc
474 * @throws ServletModuleFailure
476 public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse)
477 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
479 String idParam = aRequest.getParameter("id");
482 throw new ServletModuleExc("Invalid call to delete: no id supplied");
484 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
486 responseData.put("module", getOperationModuleName());
487 responseData.put("id", idParam);
488 responseData.put("cancelurl", aRequest.getParameter("cancelurl"));
489 responseData.put("okurl", aRequest.getParameter("okurl"));
491 ServletHelper.generateResponse(aResponse.getWriter(), responseData, deleteConfirmationGenerator);
493 catch (Throwable e) {
494 throw new ServletModuleFailure(e);
499 * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem
500 * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des
501 * doParameters ausgefuehrt.
503 * @return Name der Default-Action
505 public String defaultAction() {
506 return defaultAction;
510 * Gets the fields from a httprequest and matches them with the metadata from
511 * the storage object. Returns the keys that match, with their values.
513 * @return Map with the values
515 public Map getIntersectingValues(HttpServletRequest aRequest, StorageObject theStorage)
516 throws ServletModuleExc, ServletModuleFailure {
519 HTTPRequestParser parser;
522 parser = new HTTPRequestParser(aRequest);
524 theFieldList = theStorage.getFields();
526 Map withValues = new HashMap();
527 String aField, aValue;
529 for (int i = 0; i < theFieldList.size(); i++) {
530 aField = (String) theFieldList.get(i);
532 aValue = parser.getParameter(aField);
534 withValues.put(aField, aValue);
538 catch (Throwable e) {
539 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
541 throw new ServletModuleFailure( "ServletModule.getIntersectingValues: " + e.getMessage(), e);