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);
162 //theList = mainModule.getByWhereClause((String)null, offset);
163 if (theList == null || theList.getCount() == 0 || theList.getCount() > 1) {
164 HTMLTemplateProcessor.process(res, templateListString, theList, out, getLocale(req));
167 deliver(req, res, theList.elementAt(0), templateObjektString);
170 catch (Exception e) {
171 throw new ServletModuleException(e.getMessage());
176 * add(req,res) - generische Addmethode. Wennn die Funktionalitaet
177 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
178 * ueberschreiben werden.
179 * @param req Http-Request, das vom Dispatcher durchgereicht wird
180 * @param res Http-Response, die vom Dispatcher durchgereicht wird
182 public void add(HttpServletRequest req, HttpServletResponse res)
183 throws ServletModuleException {
186 SimpleHash mergeData = new SimpleHash();
187 mergeData.put("new", "1");
188 deliver(req, res, mergeData, templateObjektString);
190 catch (Exception e) {
191 throw new ServletModuleException(e.getMessage());
196 * insert(req,res) - generische Insertmethode, folgt auf add.
197 * Wennn die Funktionalitaet
198 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
199 * ueberschreiben werden.
201 * @param req Http-Request, das vom Dispatcher durchgereicht wird
202 * @param res Http-Response, die vom Dispatcher durchgereicht wird
204 public void insert(HttpServletRequest req, HttpServletResponse res)
205 throws ServletModuleException, ServletModuleUserException {
207 HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
208 logger.debug("--trying to add...");
209 String id = mainModule.add(withValues);
210 logger.debug("--trying to deliver..." + id);
213 catch (Exception e) {
214 throw new ServletModuleException(e.getMessage());
219 * delete(req,res) - generic delete method. Can be overridden in subclasses.
223 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
225 String idParam = req.getParameter("id");
228 throw new ServletModuleException("Invalid call to delete: no id supplied");
230 String confirmParam = req.getParameter("confirm");
231 String cancelParam = req.getParameter("cancel");
232 if (confirmParam == null && cancelParam == null) {
233 SimpleHash mergeData = new SimpleHash();
235 mergeData.put("module", getOperationModuleName());
236 mergeData.put("infoString", getOperationModuleName() + ": " + idParam);
237 mergeData.put("id", idParam);
238 mergeData.put("where", req.getParameter("where"));
239 mergeData.put("order", req.getParameter("order"));
240 mergeData.put("offset", req.getParameter("offset"));
241 deliver(req, res, mergeData, templateConfirmString);
244 if (confirmParam != null && !confirmParam.equals("")) {
245 //theLog.printInfo("delete confirmed!");
246 mainModule.deleteById(idParam);
247 list(req, res); // back to list
250 if (req.getParameter("where") != null)
257 catch (Exception e) {
258 throw new ServletModuleException(e.getMessage());
263 * edit(req,res) - generische Editmethode. Wennn die Funktionalitaet
264 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
265 * ueberschreiben werden.
267 * @param req Http-Request, das vom Dispatcher durchgereicht wird
268 * @param res Http-Response, die vom Dispatcher durchgereicht wird
270 public void edit(HttpServletRequest req, HttpServletResponse res)
271 throws ServletModuleException {
273 String idParam = req.getParameter("id");
274 deliver(req, res, mainModule.getById(idParam), templateObjektString);
276 catch (ModuleException e) {
277 throw new ServletModuleException(e.getMessage());
282 * update(req,res) - generische Updatemethode. Wennn die Funktionalitaet
283 * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
284 * ueberschreiben werden.
286 * @param req Http-Request, das vom Dispatcher durchgereicht wird
287 * @param res Http-Response, die vom Dispatcher durchgereicht wird
290 public void update(HttpServletRequest req, HttpServletResponse res)
291 throws ServletModuleException {
293 String idParam = req.getParameter("id");
294 HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
295 String id = mainModule.set(withValues);
296 //theLog.printInfo("Showing Entity with id: " + id);
298 String whereParam = req.getParameter("where");
299 String orderParam = req.getParameter("order");
300 if ((whereParam != null && !whereParam.equals("")) || (orderParam != null && !orderParam.equals(""))) {
301 //theLog.printDebugInfo("update to list");
309 catch (Exception e) {
310 throw new ServletModuleException(e.getMessage());
315 * deliver liefert das Template mit dem Filenamen templateFilename
316 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
317 * TemplateModelRoot rtm gemischt wurde
319 * @param res Http-Response, die vom Dispatcher durchgereicht wird
320 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
321 * Daten, die ins Template gemerged werden sollen.
322 * @param tmpl Name des Templates
323 * @exception ServletModuleException
325 public void deliver(HttpServletRequest req, HttpServletResponse res,
326 TemplateModelRoot rtm, TemplateModelRoot popups,
327 String templateFilename)
328 throws ServletModuleException {
329 if (rtm == null) rtm = new SimpleHash();
331 PrintWriter out = res.getWriter();
332 HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out,
333 getLocale(req), "bundles.admin");
334 // we default to admin bundles here, which is not exactly beautiful...
335 // but this whole producer stuff is going to be rewritten soon.
336 // ServletModuleOpenIndy overwrites deliver() to use open bundles
340 catch (HTMLParseException e) {
341 throw new ServletModuleException(e.getMessage());
342 } catch (IOException e) {
343 throw new ServletModuleException(e.getMessage());
349 * deliver liefert das Template mit dem Filenamen templateFilename
350 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
351 * TemplateModelRoot rtm gemischt wurde
353 * @param res Http-Response, die vom Dispatcher durchgereicht wird
354 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
355 * Daten, die ins Template gemerged werden sollen.
356 * @param tmpl Name des Templates
357 * @exception ServletModuleException
359 public void deliver(HttpServletRequest req, HttpServletResponse res,
360 TemplateModelRoot rtm, String templateFilename)
361 throws ServletModuleException {
362 deliver(req, res, rtm, null, templateFilename);
366 * deliver liefert das Template mit dem Filenamen templateFilename
367 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
368 * TemplateModelRoot rtm gemischt wurde
370 * @param res Http-Response, die vom Dispatcher durchgereicht wird
371 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
372 * Daten, die ins Template gemerged werden sollen.
373 * @param tmpl Name des Templates
374 * @exception ServletModuleException
376 public void deliver_compressed(HttpServletRequest req, HttpServletResponse res,
377 TemplateModelRoot rtm, String templateFilename)
378 throws ServletModuleException {
379 if (rtm == null) rtm = new SimpleHash();
381 PrintWriter out = new LineFilterWriter(res.getWriter());
382 //PrintWriter out = res.getWriter();
383 HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
386 catch (HTMLParseException e) {
387 throw new ServletModuleException(e.getMessage());
389 catch (IOException e) {
390 throw new ServletModuleException(e.getMessage());
395 * deliver liefert das Template mit dem Filenamen templateFilename
396 * an den HttpServletResponse res aus, nachdem es mit den Daten aus
397 * TemplateModelRoot rtm gemischt wurde
399 * @param out ist der OutputStream, in den die gergten Daten geschickt werden sollen.
400 * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
401 * Daten, die ins Template gemerged werden sollen.
402 * @param tmpl Name des Templates
403 * @exception ServletModuleException
405 private void deliver(HttpServletResponse res, HttpServletRequest req, PrintWriter out,
406 TemplateModelRoot rtm, String templateFilename)
407 throws HTMLParseException {
408 HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
412 * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem
413 * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des
414 * doParameters ausgefuehrt.
416 * @return Name der Default-Action
418 public String defaultAction() {
419 return defaultAction;
423 * Hier kann vor der Datenaufbereitung schon mal ein response geschickt
424 * werden (um das subjektive Antwortverhalten bei langsamen Verbindungen
427 public void predeliver(HttpServletRequest req, HttpServletResponse res) {
432 * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
433 * im HttpRequest vorkommen und liefert sie als HashMap zurueck
435 * @return HashMap mit den Werten
437 public HashMap getIntersectingValues(HttpServletRequest req, StorageObject theStorage)
438 throws ServletModuleException {
439 ArrayList theFieldList;
441 theFieldList = theStorage.getFields();
443 catch (StorageObjectException e) {
444 throw new ServletModuleException("ServletModule.getIntersectingValues: " + e.getMessage());
447 HashMap withValues = new HashMap();
448 String aField, aValue;
450 for (int i = 0; i < theFieldList.size(); i++) {
451 aField = (String) theFieldList.get(i);
452 aValue = req.getParameter(aField);
453 if (aValue != null) withValues.put(aField, aValue);