KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > DatabaseEvent


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/DatabaseEvent.java,v 1.35 2004/10/20 10:51:29 hkollmann Exp $
3  * $Revision: 1.35 $
4  * $Date: 2004/10/20 10:51:29 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.dbforms.event;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.dbforms.config.Constants;
28 import org.dbforms.config.DbFormsConfig;
29 import org.dbforms.config.Field;
30 import org.dbforms.config.FieldTypes;
31 import org.dbforms.config.FieldValue;
32 import org.dbforms.config.FieldValues;
33 import org.dbforms.config.MultipleValidationException;
34
35 import org.dbforms.event.eventtype.EventType;
36
37 import org.dbforms.util.MessageResources;
38 import org.dbforms.util.ParseUtil;
39 import org.dbforms.util.Util;
40
41 import java.io.UnsupportedEncodingException JavaDoc;
42
43 import java.sql.Connection JavaDoc;
44 import java.sql.SQLException JavaDoc;
45
46 import java.util.Iterator JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import javax.servlet.ServletContext JavaDoc;
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51
52
53
54 /**
55  * Abstract base class for all web-events related to database operations like
56  * inserts, updates, deletes.
57  *
58  * @author Joe Peer
59  */

60 public abstract class DatabaseEvent extends WebEvent {
61    private static Log logCat = LogFactory.getLog(DatabaseEvent.class.getName()); // logging category for this class
62

63    /** key identifier */
64    private String JavaDoc keyId;
65
66    /**
67     * Creates a new DatabaseEvent object.
68     *
69     * @param tableId the table id
70     * @param keyId the key id
71     * @param request the request object
72     * @param config the configuration object
73     */

74    public DatabaseEvent(int tableId, String JavaDoc keyId, HttpServletRequest JavaDoc request,
75       DbFormsConfig config) {
76       super(tableId, request, config);
77       this.keyId = keyId;
78    }
79
80    /**
81     * Get the hash table containing the form field names and values taken from
82     * the request object. <br>
83     * Example of a request parameter:<br>
84     * <code>name = f_0_insroot_6 value = foo-bar </code>
85     *
86     * @return the hash map containing the names and values taken from the
87     * request object
88     */

89
90    // must be public because protected will break cactus testing!
91
public abstract FieldValues getFieldValues();
92
93
94    /**
95     * Get the keyId parameter value
96     *
97     * @return keyId parameter value
98     */

99    public String JavaDoc getKeyId() {
100       return keyId;
101    }
102
103
104    /**
105     * DO the validation of the form with Commons-Validator.
106     *
107     * @param formValidatorName The form name to retreive in validation.xml
108     * @param context The servlet context we are processing
109     *
110     * @exception MultipleValidationException The Vector of errors throwed with
111     * this exception
112     */

113    public void doValidation(String JavaDoc formValidatorName, ServletContext JavaDoc context)
114       throws MultipleValidationException {
115    }
116
117
118    /**
119     * Process this event.
120     *
121     * @param con the jdbc connection object
122     *
123     * @throws SQLException if any data access error occurs
124     * @throws MultipleValidationException if any validation error occurs
125     */

126    public abstract void processEvent(Connection JavaDoc con)
127       throws SQLException JavaDoc, MultipleValidationException;
128
129
130    /**
131     * Get the FieldValues object representing the collection of FieldValue
132     * objects builded from the request parameters
133     *
134     * @param insertMode true DOCUMENT ME!
135     *
136     * @return the FieldValues object representing the collection of FieldValue
137     * objects builded from the request parameters
138     */

139    protected FieldValues getFieldValues(boolean insertMode) {
140       FieldValues result = new FieldValues();
141       String JavaDoc paramStub = (Constants.FIELDNAME_PREFIX + getTable().getId()
142          + "_"
143          + (EventType.EVENT_DATABASE_INSERT.equals(getType())
144          ? Constants.FIELDNAME_INSERTPREFIX : "") + keyId + "_");
145       Vector JavaDoc params = ParseUtil.getParametersStartingWith(getRequest(),
146             paramStub);
147
148       // Always doit in insert or delete mode
149
boolean doIt = insertMode;
150
151       // First check if update is necessary
152
if (!doIt) {
153          Iterator JavaDoc e = params.iterator();
154
155          while (e.hasNext()) {
156             String JavaDoc param = (String JavaDoc) e.next();
157
158             // value of the named parameter;
159
String JavaDoc value = ParseUtil.getParameter(getRequest(), param);
160
161             // old value of the named parameter;
162
String JavaDoc oldValue = ParseUtil.getParameter(getRequest(),
163                   Constants.FIELDNAME_OLDVALUETAG + param);
164
165             // if they are not equals, set the update flag for this field
166
// and exit from the loop;
167
doIt = !value.equals(oldValue);
168
169             if (doIt) {
170                break;
171             }
172          }
173       }
174
175       // if update is necessary then do update for all data columns
176
if (doIt) {
177          Iterator JavaDoc e = params.iterator();
178
179          while (e.hasNext()) {
180             String JavaDoc param = (String JavaDoc) e.next();
181
182             int iiFieldId = Integer.parseInt(param.substring(
183                      paramStub.length()));
184             Field f = getTable().getField(iiFieldId);
185
186             String JavaDoc value = f.getEscaper().unescapeHTML(ParseUtil.getParameter(
187                      getRequest(), param));
188             FieldValue fv = new FieldValue(f, value);
189
190             fv.setOldValue(f.getEscaper().unescapeHTML(ParseUtil.getParameter(
191                      getRequest(), Constants.FIELDNAME_OLDVALUETAG + param)));
192             fv.setPattern(ParseUtil.getParameter(getRequest(),
193                   Constants.FIELDNAME_PATTERNTAG + param));
194             fv.setLocale(MessageResources.getLocale(getRequest()));
195
196             if ((f.getType() == FieldTypes.BLOB)
197                      || (f.getType() == FieldTypes.DISKBLOB)) {
198                // in case of a BLOB or DISKBLOB save get the FileHolder for later use
199
fv.setFileHolder(ParseUtil.getFileHolder(getRequest(),
200                      "f_" + getTable().getId() + "_"
201                      + (insertMode ? Constants.FIELDNAME_INSERTPREFIX : "")
202                      + keyId + "_" + iiFieldId));
203             }
204
205             result.put(fv);
206          }
207       }
208
209       return result;
210    }
211
212
213    /**
214     * Return the key values string from the request object
215     *
216     * @return the key values string from the request object
217     */

218    protected String JavaDoc getKeyValues() {
219       String JavaDoc key = null;
220
221       try {
222          key = ParseUtil.getParameter(getRequest(),
223                "k_" + getTable().getId() + "_" + keyId);
224          key = Util.decode(key, getRequest().getCharacterEncoding());
225          logCat.info("::getKeyValues - key: " + key);
226       } catch (UnsupportedEncodingException JavaDoc e) {
227          logCat.error(e);
228       }
229
230       return key;
231    }
232 }
233
Popular Tags