X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fservlet%2FServletModule.java;h=5ddb42da27fd01ee00ed9095afe16dc830203eb6;hb=20c802e7f2f30c5cac3ece603586af684a8b6ab0;hp=77cc80ef08415e17c2d36258b6f6fb3dc592a874;hpb=cf5b651ad570014385eecf160400900cf91e3015;p=mir.git diff --git a/source/mir/servlet/ServletModule.java b/source/mir/servlet/ServletModule.java index 77cc80ef..5ddb42da 100755 --- a/source/mir/servlet/ServletModule.java +++ b/source/mir/servlet/ServletModule.java @@ -1,458 +1,534 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. - */ - -package mir.servlet; - -import freemarker.template.SimpleHash; -import freemarker.template.TemplateModelRoot; -import freemarker.template.TemplateModel; - -import mir.entity.EntityList; -import mir.log.*; -import mir.misc.*; -import mir.module.AbstractModule; -import mir.module.ModuleException; -import mir.storage.StorageObject; -import mir.storage.StorageObjectException; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Locale; - - -/** - * Abstract class ServletModule provides the base functionality for servlets. - * Deriving a class from ServletModule enables class to insert/edit/update/delete - * and list Entity from a Database via mainModule. - * - * - * Abstrakte Klasse ServletModule stellt die Basisfunktionalitaet der - * abgeleiteten ServletModule zur Verfügung. - * - * @version 28.6.1999 - * @author RK - */ - -public abstract class ServletModule { - - public String defaultAction; - protected LoggerWrapper logger; - - protected AbstractModule mainModule; - protected String templateListString; - protected String templateObjektString; - protected String templateConfirmString; - - /** - * Singelton - Methode muss in den abgeleiteten Klassen ueberschrieben werden. - * @return ServletModule - */ - public static ServletModule getInstance() { - return null; - } - - /** - * get the module name to be used for generic operations like delete. - */ - protected String getOperationModuleName() { - return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length()); - } - - /** - * get the session binded language - */ - public String getLanguage(HttpServletRequest req) { - HttpSession session = req.getSession(false); - String language = (String) session.getAttribute("Language"); - if (language == null) { - language = MirConfig.getProp("StandardLanguage"); - } - return language; - } - - /** - * get the locale either from the session or the accept-language header ot the request - * this supersedes getLanguage for the new i18n - */ - public Locale getLocale(HttpServletRequest req) { - Locale loc = null; - HttpSession session = req.getSession(false); - if (session != null) { - // session can be null in case of logout - loc = (Locale) session.getAttribute("Locale"); - } - // if there is nothing in the session get it fron the accept-language - if (loc == null) { - loc = req.getLocale(); - } - return loc; - } - - public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleException { - try { - aResponse.sendRedirect(MirConfig.getProp("RootUri") + "/Mir?"+aQuery); - } - catch (Throwable t) { - throw new ServletModuleException(t.getMessage()); - } - } - - /** - * list(req,res) - generische Listmethode. Wennn die Funktionalitaet - * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse - * ueberschreiben werden. - * - * @param req Http-Request, das vom Dispatcher durchgereicht wird - * @param res Http-Response, die vom Dispatcher durchgereicht wird - */ - public void list(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException { - try { - EntityList theList; - String offsetParam = req.getParameter("offset"); - int offset = 0; - PrintWriter out = res.getWriter(); - - // hier offsetcode bearbeiten - if (offsetParam != null && !offsetParam.equals("")) { - offset = Integer.parseInt(offsetParam); - } - if (req.getParameter("next") != null) { - offset = Integer.parseInt(req.getParameter("nextoffset")); - } - else { - if (req.getParameter("prev") != null) { - offset = Integer.parseInt(req.getParameter("prevoffset")); - } - } - theList = mainModule.getByWhereClause(null, offset); - //theList = mainModule.getByWhereClause((String)null, offset); - if (theList == null || theList.getCount() == 0 || theList.getCount() > 1) { - HTMLTemplateProcessor.process(res, templateListString, theList, out, getLocale(req)); - } - else { - deliver(req, res, theList.elementAt(0), templateObjektString); - } - } - catch (Exception e) { - throw new ServletModuleException(e.getMessage()); - } - } - - /** - * add(req,res) - generische Addmethode. Wennn die Funktionalitaet - * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse - * ueberschreiben werden. - * @param req Http-Request, das vom Dispatcher durchgereicht wird - * @param res Http-Response, die vom Dispatcher durchgereicht wird - */ - public void add(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException { - - try { - SimpleHash mergeData = new SimpleHash(); - mergeData.put("new", "1"); - deliver(req, res, mergeData, templateObjektString); - } - catch (Exception e) { - throw new ServletModuleException(e.getMessage()); - } - } - - /** - * insert(req,res) - generische Insertmethode, folgt auf add. - * Wennn die Funktionalitaet - * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse - * ueberschreiben werden. - * - * @param req Http-Request, das vom Dispatcher durchgereicht wird - * @param res Http-Response, die vom Dispatcher durchgereicht wird - */ - public void insert(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException, ServletModuleUserException { - try { - HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject()); - logger.debug("--trying to add..."); - String id = mainModule.add(withValues); - logger.debug("--trying to deliver..." + id); - list(req, res); - } - catch (Exception e) { - throw new ServletModuleException(e.getMessage()); - } - } - - /** - * delete(req,res) - generic delete method. Can be overridden in subclasses. - * - */ - - public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException { - try { - String idParam = req.getParameter("id"); - - if (idParam == null) - throw new ServletModuleException("Invalid call to delete: no id supplied"); - - String confirmParam = req.getParameter("confirm"); - String cancelParam = req.getParameter("cancel"); - if (confirmParam == null && cancelParam == null) { - SimpleHash mergeData = new SimpleHash(); - - mergeData.put("module", getOperationModuleName()); - mergeData.put("infoString", getOperationModuleName() + ": " + idParam); - mergeData.put("id", idParam); - mergeData.put("where", req.getParameter("where")); - mergeData.put("order", req.getParameter("order")); - mergeData.put("offset", req.getParameter("offset")); - deliver(req, res, mergeData, templateConfirmString); - } - else { - if (confirmParam != null && !confirmParam.equals("")) { - //theLog.printInfo("delete confirmed!"); - mainModule.deleteById(idParam); - list(req, res); // back to list - } - else { - if (req.getParameter("where") != null) - list(req, res); - else - edit(req, res); - } - } - } - catch (Exception e) { - throw new ServletModuleException(e.getMessage()); - } - } - - /** - * edit(req,res) - generische Editmethode. Wennn die Funktionalitaet - * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse - * ueberschreiben werden. - * - * @param req Http-Request, das vom Dispatcher durchgereicht wird - * @param res Http-Response, die vom Dispatcher durchgereicht wird - */ - public void edit(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException { - try { - String idParam = req.getParameter("id"); - deliver(req, res, mainModule.getById(idParam), templateObjektString); - } - catch (ModuleException e) { - throw new ServletModuleException(e.getMessage()); - } - } - - /** - * update(req,res) - generische Updatemethode. Wennn die Funktionalitaet - * nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse - * ueberschreiben werden. - * - * @param req Http-Request, das vom Dispatcher durchgereicht wird - * @param res Http-Response, die vom Dispatcher durchgereicht wird - */ - - public void update(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException { - try { - String idParam = req.getParameter("id"); - HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject()); - String id = mainModule.set(withValues); - //theLog.printInfo("Showing Entity with id: " + id); - //edit(req,res); - String whereParam = req.getParameter("where"); - String orderParam = req.getParameter("order"); - if ((whereParam != null && !whereParam.equals("")) || (orderParam != null && !orderParam.equals(""))) { - //theLog.printDebugInfo("update to list"); - list(req, res); - } - else { - edit(req, res); - } - //list(req,res); - } - catch (Exception e) { - throw new ServletModuleException(e.getMessage()); - } - } - - /** - * deliver liefert das Template mit dem Filenamen templateFilename - * an den HttpServletResponse res aus, nachdem es mit den Daten aus - * TemplateModelRoot rtm gemischt wurde - * - * @param res Http-Response, die vom Dispatcher durchgereicht wird - * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den - * Daten, die ins Template gemerged werden sollen. - * @param tmpl Name des Templates - * @exception ServletModuleException - */ - public void deliver(HttpServletRequest req, HttpServletResponse res, - TemplateModelRoot rtm, TemplateModelRoot popups, - String templateFilename) - throws ServletModuleException { - if (rtm == null) rtm = new SimpleHash(); - try { - PrintWriter out = res.getWriter(); - HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out, - getLocale(req), "bundles.admin"); - // we default to admin bundles here, which is not exactly beautiful... - // but this whole producer stuff is going to be rewritten soon. - // ServletModuleOpenIndy overwrites deliver() to use open bundles - // (br1) - out.close(); - } - catch (HTMLParseException e) { - throw new ServletModuleException(e.getMessage()); - } catch (IOException e) { - throw new ServletModuleException(e.getMessage()); - } - } - - - /** - * deliver liefert das Template mit dem Filenamen templateFilename - * an den HttpServletResponse res aus, nachdem es mit den Daten aus - * TemplateModelRoot rtm gemischt wurde - * - * @param res Http-Response, die vom Dispatcher durchgereicht wird - * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den - * Daten, die ins Template gemerged werden sollen. - * @param tmpl Name des Templates - * @exception ServletModuleException - */ - public void deliver(HttpServletRequest req, HttpServletResponse res, - TemplateModelRoot rtm, String templateFilename) - throws ServletModuleException { - deliver(req, res, rtm, null, templateFilename); - } - - /** - * deliver liefert das Template mit dem Filenamen templateFilename - * an den HttpServletResponse res aus, nachdem es mit den Daten aus - * TemplateModelRoot rtm gemischt wurde - * - * @param res Http-Response, die vom Dispatcher durchgereicht wird - * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den - * Daten, die ins Template gemerged werden sollen. - * @param tmpl Name des Templates - * @exception ServletModuleException - */ - public void deliver_compressed(HttpServletRequest req, HttpServletResponse res, - TemplateModelRoot rtm, String templateFilename) - throws ServletModuleException { - if (rtm == null) rtm = new SimpleHash(); - try { - PrintWriter out = new LineFilterWriter(res.getWriter()); - //PrintWriter out = res.getWriter(); - HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req)); - out.close(); - } - catch (HTMLParseException e) { - throw new ServletModuleException(e.getMessage()); - } - catch (IOException e) { - throw new ServletModuleException(e.getMessage()); - } - } - - /** - * deliver liefert das Template mit dem Filenamen templateFilename - * an den HttpServletResponse res aus, nachdem es mit den Daten aus - * TemplateModelRoot rtm gemischt wurde - * - * @param out ist der OutputStream, in den die gergten Daten geschickt werden sollen. - * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den - * Daten, die ins Template gemerged werden sollen. - * @param tmpl Name des Templates - * @exception ServletModuleException - */ - private void deliver(HttpServletResponse res, HttpServletRequest req, PrintWriter out, - TemplateModelRoot rtm, String templateFilename) - throws HTMLParseException { - HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req)); - } - - /** - * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem - * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des - * doParameters ausgefuehrt. - * - * @return Name der Default-Action - */ - public String defaultAction() { - return defaultAction; - } - - /** - * Hier kann vor der Datenaufbereitung schon mal ein response geschickt - * werden (um das subjektive Antwortverhalten bei langsamen Verbindungen - * zu verbessern). - */ - public void predeliver(HttpServletRequest req, HttpServletResponse res) { - ; - } - - /** - * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die - * im HttpRequest vorkommen und liefert sie als HashMap zurueck - * - * @return HashMap mit den Werten - */ - public HashMap getIntersectingValues(HttpServletRequest req, StorageObject theStorage) - throws ServletModuleException { - ArrayList theFieldList; - try { - theFieldList = theStorage.getFields(); - } - catch (StorageObjectException e) { - throw new ServletModuleException("ServletModule.getIntersectingValues: " + e.getMessage()); - } - - HashMap withValues = new HashMap(); - String aField, aValue; - - for (int i = 0; i < theFieldList.size(); i++) { - aField = (String) theFieldList.get(i); - aValue = req.getParameter(aField); - if (aValue != null) withValues.put(aField, aValue); - } - return withValues; - } - -} +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ +package mir.servlet; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import mir.config.MirPropertiesConfiguration; +import mir.config.MirPropertiesConfiguration.PropertiesConfigExc; +import mir.entity.adapter.EntityAdapterDefinition; +import mir.entity.adapter.EntityAdapterEngine; +import mir.entity.adapter.EntityAdapterModel; +import mir.log.LoggerWrapper; +import mir.module.AbstractModule; +import mir.storage.StorageObject; +import mir.util.HTTPRequestParser; +import mir.util.URLBuilder; +import mircoders.servlet.ServletHelper; + +/** + * + *

Title:

+ *

Description:

+ *

Copyright: Copyright (c) 2003

+ *

Company:

+ * @author not attributable + * @version 1.0 + */ + +public abstract class ServletModule { + public String defaultAction; + protected LoggerWrapper logger; + protected MirPropertiesConfiguration configuration; + protected Locale fallbackLocale; + + protected AbstractModule mainModule; + protected String definition; + protected EntityAdapterModel model; + + protected String listGenerator; + protected String editGenerator; + protected String deleteConfirmationGenerator; + protected int nrEntitiesPerListPage; + + + public ServletModule(){ + definition = null; + model = null; + + try { + configuration = MirPropertiesConfiguration.instance(); + } + catch (PropertiesConfigExc e) { + throw new RuntimeException("Can't get configuration: " + e.getMessage()); + } + + listGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".ListTemplate"); + editGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".EditTemplate"); + deleteConfirmationGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".DeleteConfirmationTemplate"); + nrEntitiesPerListPage = + configuration.getInt("ServletModule."+getOperationModuleName()+".ListSize", + configuration.getInt("ServletModule.Default.ListSize", 20)); + + fallbackLocale = new Locale(configuration.getString("Mir.Admin.FallbackLanguage", "en"), ""); + } + + + /** + * Singleton instance retrievel method. MUST be overridden in subclasses. + * + * @return ServletModule the single instance of the servletmodule class + */ + public static ServletModule getInstance() { + return null; + } + + /** + * Get the module name + * + * @return + */ + protected String getOperationModuleName() { + return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length()); + } + + /** + * get the locale either from the session or the accept-language header ot the request + * this supersedes getLanguage for the new i18n + */ + public Locale getLocale(HttpServletRequest aRequest) { + Locale loc = null; + HttpSession session = aRequest.getSession(false); + if (session != null) { + // session can be null in case of logout + loc = (Locale) session.getAttribute("locale"); + } + // if there is nothing in the session get it fron the accept-language + if (loc == null) { + loc = aRequest.getLocale(); + } + return loc; + } + + /** + * get the locale either from the session or the accept-language header ot the request + * this supersedes getLanguage for the new i18n + */ + public Locale getFallbackLocale(HttpServletRequest aRequest) { + return fallbackLocale; + } + + /** + * Function to specify the default ordering for lists. May be overridden. + * + * + * @return + */ + public String getDefaultListOrdering() { + + if (mainModule!=null && mainModule.getStorageObject()!=null){ + if (mainModule.getStorageObject().getFields().contains("webdb_create")) + return "webdb_create desc"; + } + + return "id asc"; + } + + /** + * + * @param aResponse + * @param aQuery + * @throws ServletModuleExc + * @throws ServletModuleFailure + */ + public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleExc, ServletModuleFailure { + try { + aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/Mir?"+aQuery)); + } + catch (Throwable t) { + throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t); + } + } + + /** + * Generic list method + * + * @param aRequest + * @param aResponse + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ + + public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc + { + HTTPRequestParser requestParser = new HTTPRequestParser(aRequest); + + String where = requestParser.getParameter("where"); + String order = requestParser.getParameterWithDefault("order", getDefaultListOrdering()); + int offset = requestParser.getIntegerWithDefault("offset", 0); + + returnList(aRequest, aResponse, where, order, offset); + } + + + public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse, + String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc { + + HTTPRequestParser requestParser = new HTTPRequestParser(aRequest); + URLBuilder urlBuilder = new URLBuilder(); + int count; + + try { + Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}); + + List list = + EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset); + + responseData.put("nexturl", null); + responseData.put("prevurl", null); + responseData.put("module", getOperationModuleName()); + + count=mainModule.getSize(aWhereClause); + + urlBuilder.setValue("module", getOperationModuleName()); + urlBuilder.setValue("do", "list"); + urlBuilder.setValue("where", aWhereClause); + urlBuilder.setValue("order", anOrderByClause); + + urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield")); + urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext")); + urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished")); + urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus")); + urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder")); + + responseData.put("searchfield", requestParser.getParameter("searchfield")); + responseData.put("searchtext", requestParser.getParameter("searchtext")); + responseData.put("searchispublished", requestParser.getParameter("searchispublished")); + responseData.put("searchstatus", requestParser.getParameter("searchstatus")); + responseData.put("searchorder", requestParser.getParameter("searchorder")); + + urlBuilder.setValue("offset", anOffset); + responseData.put("offset" , new Integer(anOffset).toString()); + responseData.put("thisurl" , urlBuilder.getQuery()); + + if (count>anOffset+nrEntitiesPerListPage) { + urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage); + responseData.put("nexturl" , urlBuilder.getQuery()); + } + + if (anOffset>0) { + urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0)); + responseData.put("prevurl" , urlBuilder.getQuery()); + } + + responseData.put("entities", list); + responseData.put("from" , Integer.toString(anOffset+1)); + responseData.put("count", Integer.toString(count)); + responseData.put("to", Integer.toString(Math.min(anOffset+nrEntitiesPerListPage, count))); + + ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator); + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc { + try { + editObject(aRequest, aResponse, model.makeEntityAdapter(definition, mainModule.getById(anId)), false, anId); + } + catch (Throwable t) { + throw new ServletModuleFailure(t); + } + } + + public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, Object anObject, boolean anIsNew, String anId) throws ServletModuleExc { + HTTPRequestParser requestParser = new HTTPRequestParser(aRequest); + URLBuilder urlBuilder = new URLBuilder(); + EntityAdapterModel model; + + try { + Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}); + + responseData.put("module", getOperationModuleName()); + responseData.put("entity", anObject); + responseData.put("new", new Boolean(anIsNew)); + + + urlBuilder.setValue("module", getOperationModuleName()); + urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl")); + if (anIsNew) + urlBuilder.setValue("do", "add"); + else { + urlBuilder.setValue("id", anId); + urlBuilder.setValue("do", "edit"); + } + responseData.put("returnurl", requestParser.getParameter("returnurl")); + responseData.put("thisurl", urlBuilder.getQuery()); + + ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator); + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + + /** + * Generic add method + * + * @param aRequest + * @param aResponse + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ + public void add(HttpServletRequest aRequest, HttpServletResponse aResponse) + throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + + Map object = new HashMap(); + + Iterator i = mainModule.getStorageObject().getFields().iterator(); + + while (i.hasNext()) + object.put(i.next(), ""); + + initializeNewObject(object, aRequest, aResponse); + + editObject(aRequest, aResponse, object, true, null); + } + + protected void initializeNewObject(Map aNewObject, HttpServletRequest aRequest, HttpServletResponse aResponse) { + } + + /** + * Method called when the user edits an object. + * + * @param aRequest + * @param aResponse + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ + public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + edit(aRequest, aResponse, aRequest.getParameter("id")); + } + + /** + * Generic edit method + * + * @param aRequest + * @param aResponse + * @param anIdentifier + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ + public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse, String anIdentifier) + throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + try { + editObject(aRequest, aResponse, model.makeEntityAdapter(definition, mainModule.getById(anIdentifier)), false, anIdentifier); + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + /** + * Generic update method + * + * @param aRequest + * @param aResponse + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ + public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) + throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + try { + HTTPRequestParser requestParser = new HTTPRequestParser(aRequest); + + String id = aRequest.getParameter("id"); + Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject()); + mainModule.set(withValues); + + String returnUrl = requestParser.getParameter("returnurl"); + + if (returnUrl!=null) { + redirect(aResponse, returnUrl); + } + else { + edit(aRequest, aResponse, id); + } + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + /** + * Generic insert method + * + * @param aRequest + * @param aResponse + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ + public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse) + throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + try { + HTTPRequestParser requestParser = new HTTPRequestParser(aRequest); + + Map object = getIntersectingValues(aRequest, mainModule.getStorageObject()); + + String id = processInstertedObject(object, aRequest, aResponse); + + String returnUrl = requestParser.getParameter("returnurl"); + + if (returnUrl!=null) { + redirect(aResponse, returnUrl); + } + else { + edit(aRequest, aResponse, id); + } + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + public String processInstertedObject(Map anObject, HttpServletRequest aRequest, HttpServletResponse aResponse) { + try { + return mainModule.add(anObject); + } + catch (Throwable t) { + throw new ServletModuleFailure(t); + } + }; + + /** + * + * @param aRequest + * @param aResponse + */ + public void confirmdelete(HttpServletRequest aRequest, HttpServletResponse aResponse) { + try { + String idParam = aRequest.getParameter("id"); + String confirmParam = aRequest.getParameter("confirm"); + String cancelParam = aRequest.getParameter("cancel"); + + if (confirmParam != null && !confirmParam.equals("")) { + mainModule.deleteById(idParam); + redirect(aResponse, aRequest.getParameter("okurl")); + } + else + redirect(aResponse, aRequest.getParameter("cancelurl")); + } + catch (Throwable t) { + throw new ServletModuleFailure(t); + } + } + + /** + * + * @param aRequest + * @param aResponse + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ + public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) + throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + try { + String idParam = aRequest.getParameter("id"); + + if (idParam == null) + throw new ServletModuleExc("Invalid call to delete: no id supplied"); + + Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}); + + responseData.put("module", getOperationModuleName()); + responseData.put("id", idParam); + responseData.put("cancelurl", aRequest.getParameter("cancelurl")); + responseData.put("okurl", aRequest.getParameter("okurl")); + + ServletHelper.generateResponse(aResponse.getWriter(), responseData, deleteConfirmationGenerator); + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + /** + * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem + * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des + * doParameters ausgefuehrt. + * + * @return Name der Default-Action + */ + public String defaultAction() { + return defaultAction; + } + + /** + * Gets the fields from a httprequest and matches them with the metadata from + * the storage object. Returns the keys that match, with their values. + * + * @return Map with the values + */ + public Map getIntersectingValues(HttpServletRequest aRequest, StorageObject theStorage) + throws ServletModuleExc, ServletModuleFailure { + + try { + HTTPRequestParser parser; + List theFieldList; + + parser = new HTTPRequestParser(aRequest); + + theFieldList = theStorage.getFields(); + + Map withValues = new HashMap(); + String aField, aValue; + + for (int i = 0; i < theFieldList.size(); i++) { + aField = (String) theFieldList.get(i); + + aValue = parser.getParameter(aField); + if (aValue != null) + withValues.put(aField, aValue); + } + return withValues; + } + catch (Throwable e) { + e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE)); + + throw new ServletModuleFailure( "ServletModule.getIntersectingValues: " + e.getMessage(), e); + } + } +} \ No newline at end of file