001: package org.jreform.internal;
002:
003: import java.util.Iterator;
004:
005: import javax.servlet.http.HttpServletRequest;
006:
007: import org.jreform.Group;
008: import org.jreform.Input;
009: import org.jreform.InputControl;
010:
011: /**
012: * An input group maintains its own set of inputs and errors and has its
013: * own validation rules. The rules depend on whether the group
014: * {@link #isRequired()}.<p>
015: *
016: * <li>If required: all inputs must satisfy imposed criteria for
017: * the group to be deemed valid.
018: *
019: * <li>Otherwise: either all inputs in this group are empty (have no values)
020: * for the group to be valid <b>OR</b> all inputs satisfy imposed criteria.
021: *
022: * @author armandino (at) gmail.com
023: */
024: class GroupImpl extends AbstractInputCollection implements Group {
025: private AbstractForm parent;
026: private String name;
027: private boolean isRequired;
028: private boolean isEmpty = true;
029:
030: /**
031: * Create a new input group.
032: * @param parent to which this group will belong.
033: * @param name of the group.
034: * @param isRequired if this group should be treated as valid when empty.
035: */
036: GroupImpl(AbstractForm parent, String name, boolean isRequired) {
037: this .parent = parent;
038: this .name = name;
039: this .isRequired = isRequired;
040: }
041:
042: /**
043: * Adds the input to this input group and its parent form.
044: */
045: public final <T> void add(InputControl<T> input) {
046: ((AbstractInputControl<T>) input).setGroupInput(true);
047: super .add(input);
048: parent.add(input);
049: }
050:
051: public final String getName() {
052: return name;
053: }
054:
055: public final boolean isRequired() {
056: return isRequired;
057: }
058:
059: public final boolean isEmpty() {
060: return isEmpty;
061: }
062:
063: public final void setRequired(boolean isRequired) {
064: this .isRequired = isRequired;
065: }
066:
067: public final boolean validate(HttpServletRequest req) {
068: isEmpty = !containsInputData(req);
069:
070: if (isRequired || !isEmpty) {
071: validateInputs(req);
072: }
073:
074: setValid(getErrors().isEmpty());
075:
076: return isValid();
077: }
078:
079: final void validateInputs(HttpServletRequest req) {
080: Iterator<String> iter = getInputs().keySet().iterator();
081: AbstractInputControl<?> input;
082:
083: while (iter.hasNext()) {
084: input = (AbstractInputControl<?>) getInputs().get(
085: iter.next());
086:
087: if (!input.validate(req))
088: getErrors().add(input.getInputName());
089: }
090: }
091:
092: /*
093: * Returns true if the request contains any data that belong
094: * to this group's inputs.
095: */
096: private boolean containsInputData(HttpServletRequest req) {
097: boolean foundInputData = false;
098: Iterator<String> iter = getInputs().keySet().iterator();
099:
100: while (iter.hasNext() && !foundInputData) {
101: InputControl<?> input = getInputs().get(iter.next());
102:
103: if (input instanceof Input) {
104: String value = req.getParameter(input.getInputName());
105:
106: if (value != null && !value.trim().equals(""))
107: foundInputData = true;
108: } else {
109: String[] values = req.getParameterValues(input
110: .getInputName());
111:
112: if (values != null && values.length > 0)
113: foundInputData = true;
114: }
115: }
116:
117: return foundInputData;
118: }
119:
120: }
|