View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/AbstractDatabaseEvent.java,v 1.1 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.1 $
4    * $Date: 2005/11/30 20:31:17 $
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  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import org.dbforms.config.Constants;
29  import org.dbforms.config.DbFormsConfig;
30  import org.dbforms.config.Field;
31  import org.dbforms.config.FieldTypes;
32  import org.dbforms.config.FieldValue;
33  import org.dbforms.config.FieldValues;
34  import org.dbforms.config.MultipleValidationException;
35  
36  import org.dbforms.event.eventtype.EventType;
37  
38  import org.dbforms.util.MessageResources;
39  import org.dbforms.util.ParseUtil;
40  import org.dbforms.util.Util;
41  
42  import java.io.UnsupportedEncodingException;
43  
44  import java.sql.Connection;
45  import java.sql.SQLException;
46  
47  import java.util.Iterator;
48  import java.util.Vector;
49  
50  import javax.servlet.ServletContext;
51  import javax.servlet.http.HttpServletRequest;
52  
53  /***
54   * Abstract base class for all web-events related to database operations like
55   * inserts, updates, deletes.
56   * 
57   * @author Joe Peer
58   */
59  public abstract class AbstractDatabaseEvent extends AbstractWebEvent {
60  	private static Log logCat = LogFactory
61  			.getLog(AbstractDatabaseEvent.class.getName()); // logging category for this
62  													// class
63  
64  	/*** key identifier */
65  	private String keyId;
66  
67  	/***
68  	 * Creates a new DatabaseEvent object.
69  	 * 
70  	 * @param tableId
71  	 *            the table id
72  	 * @param keyId
73  	 *            the key id
74  	 * @param request
75  	 *            the request object
76  	 * @param config
77  	 *            the configuration object
78  	 */
79  	public AbstractDatabaseEvent(int tableId, String keyId, HttpServletRequest request,
80  			DbFormsConfig config) {
81  		super(tableId, request, config);
82  		this.keyId = keyId;
83  	}
84  
85  	/***
86  	 * Get the hash table containing the form field names and values taken from
87  	 * the request object. <br>
88  	 * Example of a request parameter:<br>
89  	 * <code>name  = f_0_insroot_6 value = foo-bar </code>
90  	 * 
91  	 * @return the hash map containing the names and values taken from the
92  	 *         request object
93  	 */
94  
95  	// must be public because protected will break cactus testing!
96  	public abstract FieldValues getFieldValues();
97  
98  	/***
99  	 * Get the keyId parameter value
100 	 * 
101 	 * @return keyId parameter value
102 	 */
103 	public String getKeyId() {
104 		return keyId;
105 	}
106 
107 	/***
108 	 * DO the validation of the form with Commons-Validator.
109 	 * 
110 	 * @param formValidatorName
111 	 *            The form name to retreive in validation.xml
112 	 * @param context
113 	 *            The servlet context we are processing
114 	 * 
115 	 * @exception MultipleValidationException
116 	 *                The Vector of errors throwed with this exception
117 	 */
118 	public void doValidation(String formValidatorName, ServletContext context)
119 			throws MultipleValidationException {
120 	}
121 
122 	/***
123 	 * Process this event.
124 	 * 
125 	 * @param con
126 	 *            the jdbc connection object
127 	 * 
128 	 * @throws SQLException
129 	 *             if any data access error occurs
130 	 * @throws MultipleValidationException
131 	 *             if any validation error occurs
132 	 */
133 	public abstract void processEvent(Connection con) throws SQLException,
134 			MultipleValidationException;
135 
136 	/***
137 	 * Get the FieldValues object representing the collection of FieldValue
138 	 * objects builded from the request parameters
139 	 * 
140 	 * @param insertMode
141 	 *            true DOCUMENT ME!
142 	 * 
143 	 * @return the FieldValues object representing the collection of FieldValue
144 	 *         objects builded from the request parameters
145 	 */
146 	protected FieldValues getFieldValues(boolean insertMode) {
147 		FieldValues result = new FieldValues();
148 		String paramStub = (Constants.FIELDNAME_PREFIX
149 				+ getTable().getId()
150 				+ "_"
151 				+ (EventType.EVENT_DATABASE_INSERT.equals(getType()) ? Constants.FIELDNAME_INSERTPREFIX
152 						: "") + keyId + "_");
153 		Vector params = ParseUtil.getParametersStartingWith(getRequest(),
154 				paramStub);
155 
156 		// Always doit in insert or delete mode
157 		boolean doIt = insertMode;
158 
159 		// First check if update is necessary
160 		if (!doIt) {
161 			Iterator e = params.iterator();
162 
163 			while (e.hasNext()) {
164 				String param = (String) e.next();
165 
166 				// value of the named parameter;
167 				String value = ParseUtil.getParameter(getRequest(), param);
168 
169 				// old value of the named parameter;
170 				String oldValue = ParseUtil.getParameter(getRequest(),
171 						Constants.FIELDNAME_OLDVALUETAG + param);
172 
173 				// if they are not equals, set the update flag for this field
174 				// and exit from the loop;
175 				doIt = !value.equals(oldValue);
176 
177 				if (doIt) {
178 					break;
179 				}
180 			}
181 		}
182 
183 		// if update is necessary then do update for all data columns
184 		if (doIt) {
185 			Iterator e = params.iterator();
186 
187 			while (e.hasNext()) {
188 				String param = (String) e.next();
189 
190 				int iiFieldId = Integer.parseInt(param.substring(paramStub
191 						.length()));
192 				Field f = getTable().getField(iiFieldId);
193 
194 				String value = f.getEscaper().unescapeHTML(
195 						ParseUtil.getParameter(getRequest(), param));
196 				FieldValue fv = new FieldValue(f, value);
197 
198 				fv.setOldValue(f.getEscaper().unescapeHTML(
199 						ParseUtil.getParameter(getRequest(),
200 								Constants.FIELDNAME_OLDVALUETAG + param)));
201 				fv.setPattern(ParseUtil.getParameter(getRequest(),
202 						Constants.FIELDNAME_PATTERNTAG + param));
203 				fv.setLocale(MessageResources.getLocale(getRequest()));
204 
205 				if ((f.getType() == FieldTypes.BLOB)
206 						|| (f.getType() == FieldTypes.DISKBLOB)) {
207 					// in case of a BLOB or DISKBLOB save get the FileHolder for
208 					// later use
209 					fv.setFileHolder(ParseUtil.getFileHolder(getRequest(), "f_"
210 							+ getTable().getId()
211 							+ "_"
212 							+ (insertMode ? Constants.FIELDNAME_INSERTPREFIX
213 									: "") + keyId + "_" + iiFieldId));
214 				}
215 
216 				result.put(fv);
217 			}
218 		}
219 
220 		return result;
221 	}
222 
223 	/***
224 	 * Return the key values string from the request object
225 	 * 
226 	 * @return the key values string from the request object
227 	 */
228 	protected String getKeyValues() {
229 		String key = null;
230 
231 		try {
232 			key = ParseUtil.getParameter(getRequest(), "k_"
233 					+ getTable().getId() + "_" + keyId);
234 			key = Util.decode(key, getRequest().getCharacterEncoding());
235 			logCat.info("::getKeyValues - key: " + key);
236 		} catch (UnsupportedEncodingException e) {
237 			logCat.error(e);
238 		}
239 
240 		return key;
241 	}
242 }