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 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.
34 import freemarker.template.SimpleHash;
35 import freemarker.template.TemplateModelRoot;
36 import freemarker.template.TemplateModel;
38 import mir.entity.EntityList;
41 import mir.module.AbstractModule;
42 import mir.module.ModuleException;
43 import mir.storage.StorageObject;
44 import mir.storage.StorageObjectException;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.http.HttpSession;
49 import java.io.IOException;
50 import java.io.PrintWriter;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.Locale;
57 * Abstract class ServletModule provides the base functionality for servlets.
58 * Deriving a class from ServletModule enables class to insert/edit/update/delete
59 * and list Entity from a Database via mainModule.
62 * Abstrakte Klasse ServletModule stellt die Basisfunktionalitaet der
63 * abgeleiteten ServletModule zur Verfügung.
69 public abstract class ServletModule {
71 public String defaultAction;
72 protected LoggerWrapper logger;
74 protected AbstractModule mainModule;
75 protected String templateListString;
76 protected String templateObjektString;
77 protected String templateConfirmString;
80 * Singelton - Methode muss in den abgeleiteten Klassen ueberschrieben werden.
81 * @return ServletModule
83 public static ServletModule getInstance() {
88 * get the module name to be used for generic operations like delete.
90 protected String getOperationModuleName() {
91 return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length());
95 * get the session binded language
97 public String getLanguage(HttpServletRequest req) {
98 HttpSession session = req.getSession(false);
99 String language = (String) session.getAttribute("Language");
100 if (language == null) {
101 language = MirConfig.getProp("StandardLanguage");
107 * get the locale either from the session or the accept-language header ot the request
108 * this supersedes getLanguage for the new i18n
110 public Locale getLocale(HttpServletRequest req) {
112 HttpSession session = req.getSession(false);
113 if (session != null) {
114 // session can be null in case of logout
115 loc = (Locale) session.getAttribute("Locale");
117 // if there is nothing in the session get it fron the accept-language
119 loc = req.getLocale();
124 public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleException {
126 aResponse.sendRedirect(MirConfig.getProp("RootUri") + "/Mir?"+aQuery);
128 catch (Throwable t) {
129 throw new ServletModuleException(t.getMessage());
134 * list(req,res) - generische Listmethode. Wennn die Funktionalitaet
135 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
136 * ueberschreiben werden.
138 * @param req Http-Request, das vom Dispatcher durchgereicht wird
139 * @param res Http-Response, die vom Dispatcher durchgereicht wird
141 public void list(HttpServletRequest req, HttpServletResponse res)
142 throws ServletModuleException {
145 String offsetParam = req.getParameter("offset");
147 PrintWriter out = res.getWriter();
149 // hier offsetcode bearbeiten
150 if (offsetParam != null && !offsetParam.equals("")) {
151 offset = Integer.parseInt(offsetParam);
153 if (req.getParameter("next") != null) {
154 offset = Integer.parseInt(req.getParameter("nextoffset"));
157 if (req.getParameter("prev") != null) {
158 offset = Integer.parseInt(req.getParameter("prevoffset"));
161 theList = mainModule.getByWhereClause(null, offset);
163 HTMLTemplateProcessor.process(res, templateListString, theList, out, getLocale(req));
165 catch (Exception e) {
166 throw new ServletModuleException(e.getMessage());
171 * add(req,res) - generische Addmethode. Wennn die Funktionalitaet
172 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
173 * ueberschreiben werden.
174 * @param req Http-Request, das vom Dispatcher durchgereicht wird
175 * @param res Http-Response, die vom Dispatcher durchgereicht wird
177 public void add(HttpServletRequest req, HttpServletResponse res)
178 throws ServletModuleException {
181 SimpleHash mergeData = new SimpleHash();
182 mergeData.put("new", "1");
183 deliver(req, res, mergeData, templateObjektString);
185 catch (Exception e) {
186 throw new ServletModuleException(e.getMessage());
191 * insert(req,res) - generische Insertmethode, folgt auf add.
192 * Wennn die Funktionalitaet
193 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
194 * ueberschreiben werden.
196 * @param req Http-Request, das vom Dispatcher durchgereicht wird
197 * @param res Http-Response, die vom Dispatcher durchgereicht wird
199 public void insert(HttpServletRequest req, HttpServletResponse res)
200 throws ServletModuleException, ServletModuleUserException {
202 HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
203 logger.debug("--trying to add...");
204 String id = mainModule.add(withValues);
205 logger.debug("--trying to deliver..." + id);
208 catch (Exception e) {
209 throw new ServletModuleException(e.getMessage());
214 * delete(req,res) - generic delete method. Can be overridden in subclasses.
218 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
220 String idParam = req.getParameter("id");
223 throw new ServletModuleException("Invalid call to delete: no id supplied");
225 String confirmParam = req.getParameter("confirm");
226 String cancelParam = req.getParameter("cancel");
227 if (confirmParam == null && cancelParam == null) {
228 SimpleHash mergeData = new SimpleHash();
230 mergeData.put("module", getOperationModuleName());
231 mergeData.put("infoString", getOperationModuleName() + ": " + idParam);
232 mergeData.put("id", idParam);
233 mergeData.put("where", req.getParameter("where"));
234 mergeData.put("order", req.getParameter("order"));
235 mergeData.put("offset", req.getParameter("offset"));
236 deliver(req, res, mergeData, templateConfirmString);
239 if (confirmParam != null && !confirmParam.equals("")) {
240 //theLog.printInfo("delete confirmed!");
241 mainModule.deleteById(idParam);
242 list(req, res); // back to list
245 if (req.getParameter("where") != null)
252 catch (Exception e) {
253 throw new ServletModuleException(e.getMessage());
258 * edit(req,res) - generische Editmethode. Wennn die Funktionalitaet
259 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
260 * ueberschreiben werden.
262 * @param req Http-Request, das vom Dispatcher durchgereicht wird
263 * @param res Http-Response, die vom Dispatcher durchgereicht wird
265 public void edit(HttpServletRequest req, HttpServletResponse res)
266 throws ServletModuleException {
268 String idParam = req.getParameter("id");
269 deliver(req, res, mainModule.getById(idParam), templateObjektString);
271 catch (ModuleException e) {
272 throw new ServletModuleException(e.getMessage());
277 * update(req,res) - generische Updatemethode. Wennn die Funktionalitaet
278 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
279 * ueberschreiben werden.
281 * @param req Http-Request, das vom Dispatcher durchgereicht wird
282 * @param res Http-Response, die vom Dispatcher durchgereicht wird
285 public void update(HttpServletRequest req, HttpServletResponse res)
286 throws ServletModuleException {
288 String idParam = req.getParameter("id");
289 HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
291 String id = mainModule.set(withValues);
292 String whereParam = req.getParameter("where");
293 String orderParam = req.getParameter("order");
295 if ((whereParam != null && !whereParam.equals("")) || (orderParam != null && !orderParam.equals(""))) {
302 catch (Exception e) {
303 throw new ServletModuleException(e.getMessage());
308 * deliver liefert das Template mit dem Filenamen templateFilename
309 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
310 * TemplateModelRoot rtm gemischt wurde
312 * @param res Http-Response, die vom Dispatcher durchgereicht wird
313 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
314 * Daten, die ins Template gemerged werden sollen.
315 * @param tmpl Name des Templates
316 * @exception ServletModuleException
318 public void deliver(HttpServletRequest req, HttpServletResponse res,
319 TemplateModelRoot rtm, TemplateModelRoot popups,
320 String templateFilename)
321 throws ServletModuleException {
322 if (rtm == null) rtm = new SimpleHash();
324 PrintWriter out = res.getWriter();
325 HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out,
326 getLocale(req), "bundles.admin");
327 // we default to admin bundles here, which is not exactly beautiful...
328 // but this whole producer stuff is going to be rewritten soon.
329 // ServletModuleOpenIndy overwrites deliver() to use open bundles
333 catch (HTMLParseException e) {
334 throw new ServletModuleException(e.getMessage());
335 } catch (IOException e) {
336 throw new ServletModuleException(e.getMessage());
342 * deliver liefert das Template mit dem Filenamen templateFilename
343 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
344 * TemplateModelRoot rtm gemischt wurde
346 * @param res Http-Response, die vom Dispatcher durchgereicht wird
347 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
348 * Daten, die ins Template gemerged werden sollen.
349 * @param tmpl Name des Templates
350 * @exception ServletModuleException
352 public void deliver(HttpServletRequest req, HttpServletResponse res,
353 TemplateModelRoot rtm, String templateFilename)
354 throws ServletModuleException {
355 deliver(req, res, rtm, null, templateFilename);
359 * deliver liefert das Template mit dem Filenamen templateFilename
360 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
361 * TemplateModelRoot rtm gemischt wurde
363 * @param res Http-Response, die vom Dispatcher durchgereicht wird
364 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
365 * Daten, die ins Template gemerged werden sollen.
366 * @param tmpl Name des Templates
367 * @exception ServletModuleException
369 public void deliver_compressed(HttpServletRequest req, HttpServletResponse res,
370 TemplateModelRoot rtm, String templateFilename)
371 throws ServletModuleException {
372 if (rtm == null) rtm = new SimpleHash();
374 PrintWriter out = new LineFilterWriter(res.getWriter());
375 //PrintWriter out = res.getWriter();
376 HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
379 catch (HTMLParseException e) {
380 throw new ServletModuleException(e.getMessage());
382 catch (IOException e) {
383 throw new ServletModuleException(e.getMessage());
388 * deliver liefert das Template mit dem Filenamen templateFilename
389 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
390 * TemplateModelRoot rtm gemischt wurde
392 * @param out ist der OutputStream, in den die gergten Daten geschickt werden sollen.
393 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
394 * Daten, die ins Template gemerged werden sollen.
395 * @param tmpl Name des Templates
396 * @exception ServletModuleException
398 private void deliver(HttpServletResponse res, HttpServletRequest req, PrintWriter out,
399 TemplateModelRoot rtm, String templateFilename)
400 throws HTMLParseException {
401 HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
405 * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem
406 * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des
407 * doParameters ausgefuehrt.
409 * @return Name der Default-Action
411 public String defaultAction() {
412 return defaultAction;
416 * Hier kann vor der Datenaufbereitung schon mal ein response geschickt
417 * werden (um das subjektive Antwortverhalten bei langsamen Verbindungen
420 public void predeliver(HttpServletRequest req, HttpServletResponse res) {
425 * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
426 * im HttpRequest vorkommen und liefert sie als HashMap zurueck
428 * @return HashMap mit den Werten
430 public HashMap getIntersectingValues(HttpServletRequest req, StorageObject theStorage)
431 throws ServletModuleException {
432 ArrayList theFieldList;
434 theFieldList = theStorage.getFields();
436 catch (StorageObjectException e) {
437 throw new ServletModuleException("ServletModule.getIntersectingValues: " + e.getMessage());
440 HashMap withValues = new HashMap();
441 String aField, aValue;
443 for (int i = 0; i < theFieldList.size(); i++) {
444 aField = (String) theFieldList.get(i);
445 aValue = req.getParameter(aField);
446 if (aValue != null) withValues.put(aField, aValue);