001: package org.apache.turbine.services.intake.validator;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.turbine.services.intake.IntakeException;
029: import org.apache.turbine.services.intake.model.Field;
030: import org.apache.turbine.services.intake.model.Group;
031:
032: /**
033: * Validates an int field in dependency on another int field.
034: *
035: * <table>
036: * <tr>
037: * <th>Name</th><th>Valid Values</th><th>Default Value</th>
038: * </tr>
039: * <tr>
040: * <td>less-than</td>
041: * <td><name of other field></td>
042: * <td> </td>
043: * </tr>
044: * <tr>
045: * <td>greater-than</td>
046: * <td><name of other field></td>
047: * <td> </td>
048: * </tr>
049: * <tr>
050: * <td>less-than-or-equal</td>
051: * <td><name of other field></td>
052: * <td> </td>
053: * </tr>
054: * <tr>
055: * <td>greater-than-or-equal</td>
056: * <td><name of other field></td>
057: * <td> </td>
058: * </tr>
059: * </table>
060: *
061: * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
062: * @version $Id: DateStringValidator.java 534527 2007-05-02 16:10:59Z tv $
063: */
064: public class IntegerRangeValidator extends IntegerValidator {
065: /** List of FieldReferences for multiple comparisons */
066: List fieldReferences;
067:
068: /** Callback for the actual compare operation */
069: CompareCallback compareCallback;
070:
071: public IntegerRangeValidator(final Map paramMap)
072: throws IntakeException {
073: init(paramMap);
074: }
075:
076: /**
077: * Default constructor
078: */
079: public IntegerRangeValidator() {
080: super ();
081: }
082:
083: /**
084: * Constructor to use when initialising Object
085: *
086: * @param paramMap
087: * @throws InvalidMaskException
088: */
089: public void init(final Map paramMap) throws InvalidMaskException {
090: super .init(paramMap);
091:
092: compareCallback = new CompareCallback() {
093: /**
094: * Compare the given values using the compare operation provided
095: *
096: * @param compare type of compare operation
097: * @param thisValue value of this field
098: * @param refValue value of the reference field
099: *
100: * @return the result of the comparison
101: */
102: public boolean compareValues(int compare, Object this Value,
103: Object refValue) throws ClassCastException {
104: boolean result = true;
105:
106: Integer this Int = (Integer) this Value;
107: Integer otherInt = (Integer) refValue;
108:
109: switch (compare) {
110: case FieldReference.COMPARE_LT:
111: result = this Int.compareTo(otherInt) < 0;
112: break;
113:
114: case FieldReference.COMPARE_LTE:
115: result = this Int.compareTo(otherInt) <= 0;
116: break;
117:
118: case FieldReference.COMPARE_GT:
119: result = this Int.compareTo(otherInt) > 0;
120: break;
121:
122: case FieldReference.COMPARE_GTE:
123: result = this Int.compareTo(otherInt) >= 0;
124: break;
125: }
126:
127: return result;
128: }
129: };
130:
131: fieldReferences = new ArrayList(10);
132:
133: for (Iterator i = paramMap.entrySet().iterator(); i.hasNext();) {
134: Map.Entry entry = (Map.Entry) i.next();
135: String key = (String) entry.getKey();
136: Constraint constraint = (Constraint) entry.getValue();
137:
138: int compare = FieldReference.getCompareType(key);
139:
140: if (compare != 0) {
141: // found matching constraint
142: FieldReference fieldref = new FieldReference();
143: fieldref.setCompare(compare);
144: fieldref.setFieldName(constraint.getValue());
145: fieldref.setMessage(constraint.getMessage());
146:
147: fieldReferences.add(fieldref);
148: }
149: }
150:
151: if (fieldReferences.isEmpty()) {
152: log.warn("No reference field rules have been found.");
153: }
154: }
155:
156: /**
157: * Determine whether a testValue meets the criteria specified
158: * in the constraints defined for this validator
159: *
160: * @param testField a <code>Field</code> to be tested
161: * @exception ValidationException containing an error message if the
162: * testValue did not pass the validation tests.
163: */
164: public void assertValidity(final Field testField)
165: throws ValidationException {
166: super .assertValidity(testField);
167:
168: Group this Group = testField.getGroup();
169:
170: if (testField.isMultiValued()) {
171: String[] stringValues = (String[]) testField.getTestValue();
172:
173: for (int i = 0; i < stringValues.length; i++) {
174: assertValidity(stringValues[i], this Group);
175: }
176: } else {
177: String testValue = (String) testField.getTestValue();
178:
179: assertValidity(testValue, this Group);
180: }
181: }
182:
183: /**
184: * Determine whether a testValue meets the criteria specified
185: * in the constraints defined for this validator
186: *
187: * @param testValue a <code>String</code> to be tested
188: * @param group the group this field belongs to
189: *
190: * @exception ValidationException containing an error message if the
191: * testValue did not pass the validation tests.
192: */
193: public void assertValidity(final String testValue, final Group group)
194: throws ValidationException {
195: if (required || StringUtils.isNotEmpty(testValue)) {
196: Integer testInt = new Integer(testValue);
197:
198: try {
199: FieldReference.checkReferences(fieldReferences,
200: compareCallback, testInt, group);
201: } catch (ValidationException e) {
202: errorMessage = e.getMessage();
203: throw e;
204: }
205: }
206: }
207: }
|