API Overview API Index Package Overview Direct link to this page
JavaOnTracks 0.1.2
  net.jot.web.forms. JOTForm View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

/*
------------------------------------
JavaOnTracks          Thibaut Colar
tcolar-jot AT colar DOT net
Artistic Licence 2.0
http://www.javaontracks.net
------------------------------------
 */
package net.jot.web.forms;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import java.util.Vector;
import net.jot.logger.JOTLogger;
import net.jot.persistance.JOTModel;
import net.jot.web.JOTFlowRequest;
import net.jot.web.ctrl.JOTController;
import net.jot.web.forms.ui.JOTFormField;
import net.jot.web.view.JOTViewTag;

/**
 * Generic class for handling HTML forms (validate, save the values when validation fail etc...)
 * You probably will want to use one of the subclass rather than this directly: ie: JOTGeneratedForm, JOTDBForm etc...
 * Or you can Subclass this directly for more control
 * Form objects provide easy parsing/validation of web forms
 * @author thibautc
 *
 */
public abstract class JOTForm extends JOTViewTag
{

	public static final String REQUEST_ID = "__JOT__SUBMITTED_FORM";
	private Map requestParams = null;
	;
	/**
	 * Stores the defined form elements (~form fields)
	 */
	private Hashtable elements = new Hashtable();
	/** store validation error messages*/
	private Hashtable errors = null;
	/** store validation success status*/
	private boolean hasValidated = false;
	/**
	End result, can be set by the user to branch to different places.
	by defaut if the form saved ok it's success
	in case of validation failure it would be  JOTController.RESULT_VALIDATION_FAILURE
	 */
	private String result = JOTController.RESULT_SUCCESS;

	/**
	 * Implement this and return true if user is authorized to use this form
	 * @param request
	 * @return
	 */
	public abstract boolean validatePermissions(JOTFlowRequest request);

	/**
	 * You can use this method to set initial form values (Called the first time the form is created)
	 * Ex:     defineField("name", JOTFormConst.TEXTAREA);    get("fname").setValue("toto");
	 */
	public abstract void init(JOTFlowRequest request) throws Exception;

	/**
	 * You need to define this method to you want to validate your form.
	 * The returned a hashtable or error messages (String error_id, String error_text)
	 * The hashtable should be empty/null if no errors occured.
	 * Ex: Hashtable h=new Hashtable(); h.put("error1","Password invalid");
	 */
	public abstract Hashtable validate(JOTFlowRequest request) throws Exception;

	/**
	 * This will be called after a succesful validation
	 * This is where you save your form data .. wherever you want to save it like a database etc ...
	 * Implementation note: if the field is saveAutomatically=false, then we won't save that field
	 * @return
	 */
	public abstract void save(JOTFlowRequest request) throws Exception;

	/**
	 * Override this function if you want to do things before validation runs
	 *
	 */
	public void preValidate()
	{
	}

	/**
	 * Define a field of the form (name/field type)
	 * You should define all the types you want to use / see in the html form
	 * Typically you make calls to this method from within init()
	 * @param name
	 * @param type
	 */
	public void defineField(String name, int type)
	{
		elements.put(name, new JOTFormElement(type));
	}

	/**
	 * Return all the form fields
	 * Hastable of {name:JOTFormElement}
	 */
	public Hashtable getAll()
	{
		return elements;
	}

	/**
	 * Return an element (by name)
	 * @param name
	 * @return
	 */
	public JOTFormElement get(String name)
	{
		JOTFormElement elem = ((JOTFormElement) elements.get(name));
		if (elem != null)
		{
			int type = elem.getType();
			return get(type, name);
		}
		return null;
	}

	/**
	 * Returns an element.
	 * @param type
	 * @param name
	 * @return
	 */
	private JOTFormElement get(int type, String name)
	{
		JOTFormElement el = (JOTFormElement) elements.get(name);
		if (el == null)
		{
			el = new JOTFormElement(type);
			String value = "";
			if (requestParams != null && requestParams.containsKey(name))
			{
				String[] values = (String[]) requestParams.get(name);
				value = values[0];
			} else
			{
				// set to false checkbox, radio etc...
				// browser don't send checbox=off, but instead don't send it at all if off
				/*if(type==JOTFormConst.INPUT_CHECKBOX || type==JOTFormConst.INPUT_RADIO)
				{
				value=JOTFormConst.VALUE_UNCHECKED;
				}*/
			}
			el.setValue(value);
			elements.put(name, el);
		}
		return el;
	}

	/**
	 * if new values are found in the request, this is going to be called to parse the new values
	 * @param request
	 */
	public void reparseForm(JOTFlowRequest request)
	{
		JOTLogger.log(JOTLogger.CAT_FLOW, JOTLogger.TRACE_LEVEL, this, "Reparsing form");
		requestParams = request.getParameterMap();

		Set set = elements.keySet();
		Iterator i = set.iterator();

		while (i.hasNext())
		{
			String key = (String) i.next();
			JOTFormElement el = (JOTFormElement) elements.get(key);
			if (requestParams.containsKey(key))
			{
				String[] values = (String[]) requestParams.get(key);
				String value = "";
				for (int j = 0; j != values.length; j++)
				{
					value += values[j] + ",";
				}
				if (value.endsWith(","))
				{
					value = value.substring(0, value.length() - 1);
				}
				JOTLogger.log(JOTLogger.CAT_FLOW, JOTLogger.TRACE_LEVEL, this, "New Value from request: " + values[0]);
				el.setValue(value);
			} else
			{
				if (el.getType() == JOTFormConst.INPUT_CHECKBOX || el.getType() == JOTFormConst.INPUT_RADIO)
				{
					el.setValue(JOTFormConst.VALUE_UNCHECKED);
				} else if (el.getType() == JOTFormConst.SELECT)
				{
					el.setValue("");
				}
			}
			elements.put(key, el);
		}

	}

	public Hashtable getErrors()
	{
		return errors;
	}

	public void setErrors(Hashtable errors)
	{
		this.errors = errors;
	}

	public boolean hasValidated()
	{
		return hasValidated;
	}

	public void setHasValidated(boolean hasValidated)
	{
		this.hasValidated = hasValidated;
	}

	/**
	Result of the form processing: ie: success, validationFailure etc...
	@return
	 */
	public String getResult()
	{
		return result;
	}

	public void setResult(String result)
	{
		this.result = result;
	}

	/**
	 * Update a model's values using the request params.
	 * Internal use.
	 * @param model
	 * @param items
	 * @param request
	 * @throws java.lang.Exception
	 */
	protected JOTModel updateModelFromRequest(JOTModel model, Vector items, JOTFlowRequest request) throws Exception
	{
		for (int i = 0; i != items.size(); i++)
		{
			if (items.get(i) instanceof JOTFormField)
			{
				JOTFormField field = (JOTFormField) items.get(i);
				String name = field.getName();
				JOTFormElement el = get(name);
				if (el != null && field.isSaveAutomatically())
				{
					Object value = el.getValue();
					Field f = getField(model, name);
					if (model.getMapping(null).getFields().containsKey(name))
					{
						boolean isTransient = f != null && Modifier.isTransient(f.getModifiers());
						if (!isTransient && !name.startsWith("__"))
						{
							String v = (String) value;
							if (f.getType() == String.class)
							{
								value = (String) value;
							} else if (f.getType() == Boolean.class)
							{
								value = new Boolean(v.length() > 0 && !v.equalsIgnoreCase("false"));
							} else if (f.getType() == Integer.class)
							{
								value = new Integer(v);
							} else if (f.getType() == Byte.class)
							{
								value = new Byte(v);
							} else if (f.getType() == Short.class)
							{
								value = new Short(v);
							} else if (f.getType() == Long.class)
							{
								value = new Long(v);
							} else if (f.getType() == Float.class)
							{
								value = new Float(v);
							} else if (f.getType() == Double.class)
							{
								value = new Double(v);
							} else if (f.getType() == BigDecimal.class)
							{
								value = new BigDecimal(v);
							}

							//TBD: timstamp / date /time ?
							f.set(model, value);
						} else
						{
							JOTLogger.debug(this, "Skipping form field: " + name);
						}
					} else
					{
						JOTLogger.log(JOTLogger.CAT_DB, JOTLogger.DEBUG_LEVEL, this, "Form field: '" + name + "' does not exist in DB, ignoring.");
					}
				}
			}
		}
		return model;
	}

	private Field getField(JOTModel model, String fieldName)
	{
		Field f = null;
		try
		{
			f = model.getClass().getField(fieldName);
		} catch (NoSuchFieldException e)
		{
		}
		return f;
	}
}

Generated By: JavaOnTracks Doclet 0.1.5     ©Thibaut Colar