01: package org.jreform.internal;
02:
03: import java.util.Arrays;
04: import java.util.Collections;
05: import java.util.List;
06:
07: import javax.servlet.http.HttpServletRequest;
08:
09: import org.jreform.Criterion;
10: import org.jreform.InputDataType;
11: import org.jreform.MultiInput;
12:
13: /**
14: * @author armandino (at) gmail.com
15: */
16: class MultiInputImpl<T> extends AbstractInputControl<T> implements
17: MultiInput<T> {
18: private static final String[] EMPTY_ARRAY = {};
19:
20: private List<T> values;
21: private String[] valueAttributes;
22:
23: MultiInputImpl(InputDataType<T> type, String name,
24: Criterion<T>... criteria) {
25: super (type, name, criteria);
26: }
27:
28: /**
29: * Returns a list of parsed values or an empty list if none.
30: */
31: @SuppressWarnings("unchecked")
32: public final List<T> getValues() {
33: return values == null ? Collections.EMPTY_LIST : values;
34: }
35:
36: public final void setValues(List<T> value) {
37: this .values = value;
38: }
39:
40: /**
41: * Returns an array of <tt>value</tt> attributes or an empty array if none.
42: */
43: public final String[] getValueAttributes() {
44: return valueAttributes == null ? EMPTY_ARRAY : valueAttributes;
45: }
46:
47: public void setValueAttributes(String[] input) {
48: this .valueAttributes = input;
49: }
50:
51: public final boolean isBlank() {
52: if (valueAttributes != null) {
53: for (String valueAttribute : valueAttributes) {
54: if (valueAttribute != null
55: && !valueAttribute.equals(""))
56: return false;
57: }
58: }
59: return true;
60: }
61:
62: public String getStringValue() {
63: return values.toString();
64: }
65:
66: boolean validate(HttpServletRequest req) {
67: String[] values = req.getParameterValues(getInputName());
68: setValueAttributes(values);
69:
70: MultiInputValidator<T> validator = new MultiInputValidator<T>(
71: this );
72: ValidationResult<List<T>> result = validator
73: .validate(valueAttributes);
74:
75: setValues(result.getParsedValue());
76: setValid(result.isValid());
77: setOnError(result.getErrorMessage());
78:
79: return isValid();
80:
81: }
82:
83: public final String toString() {
84: return getValueAttributes() == null ? "" : Arrays
85: .toString(getValueAttributes());
86: }
87:
88: }
|