KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > data > DataObjectHandler


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DataObjectHandler.java,v 1.4 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.data;
21
22 import java.io.*;
23 import javax.servlet.http.*;
24 import org.enhydra.barracuda.core.event.*;
25 import org.enhydra.barracuda.core.forms.*;
26 import org.enhydra.barracuda.contrib.sam.xmlform.*;
27 import org.enhydra.barracuda.contrib.sam.models.*;
28 import org.apache.log4j.*;
29
30 /** Base class for other DataHandlers. DataObjectHandler instatiates the XML form
31  * map specified by it's Config. If the form is already visited, validation is
32  * performed.
33  */

34 public abstract class DataObjectHandler extends DefaultBaseEventListener implements GetConfig {
35
36     protected static Logger logger = Logger.getLogger(DataObjectHandler.class.getName());
37
38     public abstract Config getConfig();
39     protected Config config;
40
41     /** <ul>
42      * <li> get a DataObject based on the OID parameter given in the request
43      * <li> instatiate a XMLFormMap based on a XML file given in the Config
44      * <li> if the form is already visited, try to perform validation
45      * <li> if validation fails, create a ErrorModel
46      * <li> continue processing by calling the Config's ViewEvent
47      */

48     public void handleControlEvent(ControlEventContext context) throws EventException, javax.servlet.ServletException JavaDoc, java.io.IOException JavaDoc {
49         BaseEvent event = context.getEvent();
50         HttpServletRequest request = context.getRequest();
51         HttpSession session = request.getSession();
52
53         config = getConfig();
54
55         if (logger.isDebugEnabled()) logger.debug("handling controlEvent " + event.getClass().getName());
56         DataObject dataObj = null;
57         try {
58             dataObj = config.getDataObject(request.getParameter(config.getOidName()));
59         } catch (DataObjectException ex) {
60             logger.error(ex.getMessage() );
61         }
62
63           controlEventHookBeforeXmlFormMap(context, dataObj);
64
65         XmlFormMap xfm = new XmlFormMap(context, config.getXmlFormName(), config.getFormName(), dataObj);
66         context.putState(config.getMapName(), xfm);
67         if (logger.isDebugEnabled()) logger.debug("putting session attribute " + config.getMapName() + " value: " + xfm);
68
69         if (config.getFormName().equals(request.getParameter("visited"))) {
70             try {
71                 logger.debug("Mapping form");
72                 xfm.map(request).validate(true);
73             } catch (ValidationException ex) {
74                 ErrorModel.create(context, ex);
75                 logger.error(ex.getMessage(), ex);
76                 throw new InterruptDispatchException(config.getViewEvent());
77             }
78         }
79           controlEventHookAfterXmlFormMap(context, dataObj);
80
81         ViewEvent renderEvent = config.getViewEvent();
82         if (logger.isDebugEnabled()) logger.debug("putting " + renderEvent.getClass().getName() + " to event queue");
83         event.setHandled(true);
84         context.getQueue().addEvent(renderEvent);
85     }
86
87     /** hook funtion to be overridden in subclasses, empty implementation. It is called before the XMLFormMap is instantiated */
88     protected void controlEventHookBeforeXmlFormMap(ControlEventContext context, DataObject dataObj) {
89     }
90
91     /** hook funtion to be overridden in subclasses, empty implementation. It is called after the XMLFormMap is instantiated and validated */
92     protected void controlEventHookAfterXmlFormMap(ControlEventContext context, DataObject dataObj) {
93     }
94 }
95
Popular Tags