KickJava   Java API By Example, From Geeks To Geeks.

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


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: DataObjectSaveHandler.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.util.*;
23 import javax.servlet.http.*;
24
25 import org.enhydra.barracuda.core.event.*;
26 import org.enhydra.barracuda.core.forms.*;
27 import org.enhydra.barracuda.contrib.sam.xmlform.*;
28 import org.enhydra.barracuda.contrib.sam.models.*;
29 import org.apache.log4j.*;
30
31 public abstract class DataObjectSaveHandler extends DefaultBaseEventListener implements GetConfig {
32
33     protected static Logger logger = Logger.getLogger(DataObjectSaveHandler.class.getName());
34
35     public abstract Config getConfig();
36
37     protected Config config;
38     protected XmlFormMap xfm;
39     protected DataObject dataObj;
40
41     public void handleControlEvent(ControlEventContext context) throws EventException, javax.servlet.ServletException JavaDoc, java.io.IOException JavaDoc {
42         BaseEvent event = context.getEvent();
43         HttpServletRequest request = context.getRequest();
44         HttpSession session = request.getSession();
45
46         config = getConfig();
47
48         if (ErrorModel.exists(context)) return;
49         logger.info("Saving User");
50         xfm = (XmlFormMap) context.getState(config.getMapName());
51         String JavaDoc oid = xfm.getStringVal(config.getOidName());
52         logger.info("oid is " + oid);
53         try {
54             // iterate over all form elements any change underlying dataobject
55
dataObj = config.getDataObject(oid);
56             Map elements = xfm.getElements();
57             Iterator iter = elements.entrySet().iterator();
58             while (iter.hasNext()) {
59                 Map.Entry e = (Map.Entry)iter.next();
60                 String JavaDoc key = (String JavaDoc)e.getKey();
61                 if (logger.isDebugEnabled()) logger.debug("key " + key);
62                 FormElement fe = (FormElement)e.getValue();
63                 Object JavaDoc oldData = fe.getVal();
64                 Object JavaDoc newData = modifyFormData(key, oldData);
65                 logger.debug("old: " + oldData + " new: " + newData);
66                 dataObj.set(key, newData);
67             }
68             dataObj.save(null);
69             logger.debug("oldoid is " + oid);
70             String JavaDoc newoid = (String JavaDoc)dataObj.get(config.getOidName());
71             logger.debug("newoid is " + oid);
72
73
74                 // sam, 2003-08-05 commented out next 4 lines, otherwise save&email fails (project cockpit...)
75
// create a new formmap and store it in event's context for usage during rendering
76
//xfm = new XmlFormMap(context, config.getXmlFormName(), config.getFormName(), dataObj);
77
//logger.debug("oid from formmap:" + xfm.getStringVal(config.getOidName()));
78
//context.putState(config.getMapName(), xfm);
79

80         } catch (DataObjectException ex) {
81             ErrorModel.create(context, ex);
82             logger.error(ex.getMessage(),ex);
83         }
84     }
85
86     /**
87      * gives subclasses a hook to change the form data before it is saved
88      */

89     protected Object JavaDoc modifyFormData(String JavaDoc key, Object JavaDoc value) {
90         return value;
91     }
92 }
93
Popular Tags