1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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());
62
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
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
157 boolean doIt = insertMode;
158
159
160 if (!doIt) {
161 Iterator e = params.iterator();
162
163 while (e.hasNext()) {
164 String param = (String) e.next();
165
166
167 String value = ParseUtil.getParameter(getRequest(), param);
168
169
170 String oldValue = ParseUtil.getParameter(getRequest(),
171 Constants.FIELDNAME_OLDVALUETAG + param);
172
173
174
175 doIt = !value.equals(oldValue);
176
177 if (doIt) {
178 break;
179 }
180 }
181 }
182
183
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
208
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 }