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.Iterator;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.turbine.services.intake.IntakeException;
028: import org.apache.turbine.services.intake.model.Field;
029: import org.apache.turbine.services.intake.model.Group;
030:
031: /**
032: * Helper Class to manage relations between fields. The following
033: * comparisons are supported:
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 FieldReference {
065: /** a local logger */
066: protected static final Log log = LogFactory
067: .getLog(FieldReference.class);
068:
069: /** Rule name for "<" comparison */
070: public static final String RANGE_LT = "less-than";
071:
072: /** Rule name for ">" comparison */
073: public static final String RANGE_GT = "greater-than";
074:
075: /** Rule name for "<=" comparison */
076: public static final String RANGE_LTE = "less-than-or-equal";
077:
078: /** Rule name for ">=" comparison */
079: public static final String RANGE_GTE = "greater-than-or-equal";
080:
081: /** Integer value for "<" comparison */
082: public static final int COMPARE_LT = 1;
083:
084: /** Integer value for ">" comparison */
085: public static final int COMPARE_GT = 2;
086:
087: /** Integer value for "<=" comparison */
088: public static final int COMPARE_LTE = 3;
089:
090: /** Integer value for ">=" comparison */
091: public static final int COMPARE_GTE = 4;
092:
093: /** Numeric comparison */
094: private int compare = 0;
095:
096: /** Name of referenced field */
097: private String fieldName = null;
098:
099: /** Error message */
100: private String message = null;
101:
102: /**
103: * Constructor
104: */
105: public FieldReference() {
106: }
107:
108: /**
109: * @return the comparison type
110: */
111: public int getCompare() {
112: return compare;
113: }
114:
115: /**
116: * @param compare the comparison type to set
117: */
118: public void setCompare(int compare) {
119: this .compare = compare;
120: }
121:
122: /**
123: * @return the field name
124: */
125: public String getFieldName() {
126: return fieldName;
127: }
128:
129: /**
130: * @param fieldName the field name to set
131: */
132: public void setFieldName(String fieldName) {
133: this .fieldName = fieldName;
134: }
135:
136: /**
137: * @return the message
138: */
139: public String getMessage() {
140: return message;
141: }
142:
143: /**
144: * @param message the message to set
145: */
146: public void setMessage(String message) {
147: this .message = message;
148: }
149:
150: /**
151: * Map the comparison strings to their numeric counterparts
152: *
153: * @param key the
154: * @return
155: */
156: public static int getCompareType(String key) {
157: int compareType = 0;
158:
159: if (key.equals(RANGE_LT)) {
160: compareType = COMPARE_LT;
161: } else if (key.equals(RANGE_LTE)) {
162: compareType = COMPARE_LTE;
163: } else if (key.equals(RANGE_GT)) {
164: compareType = COMPARE_GT;
165: } else if (key.equals(RANGE_GTE)) {
166: compareType = COMPARE_GTE;
167: }
168:
169: return compareType;
170: }
171:
172: /**
173: * Check the parsed value against the referenced fields
174: *
175: * @param fieldReferences List of field references to check
176: * @param compareCallback Callback to the actual compare operation
177: * @param value the parsed value of the related field
178: * @param group the group the related field belongs to
179: *
180: * @throws ValidationException
181: */
182: public static void checkReferences(List fieldReferences,
183: CompareCallback compareCallback, Object value, Group group)
184: throws ValidationException {
185: for (Iterator i = fieldReferences.iterator(); i.hasNext();) {
186: FieldReference ref = (FieldReference) i.next();
187: boolean comp_true = true;
188:
189: try {
190: Field refField = group.get(ref.getFieldName());
191:
192: if (refField.isSet()) {
193: /*
194: * Fields are processed in sequence so that our
195: * reference field might have been set but not
196: * yet validated. We check this here.
197: */
198: if (!refField.isValidated()) {
199: refField.validate();
200: }
201:
202: if (refField.isValid()) {
203: try {
204: comp_true = compareCallback.compareValues(
205: ref.getCompare(), value, refField
206: .getValue());
207: } catch (ClassCastException e) {
208: throw new IntakeException(
209: "Type mismatch comparing " + value
210: + " with "
211: + refField.getValue(), e);
212: }
213: }
214: }
215: } catch (IntakeException e) {
216: log.error("Validate operation failed.", e);
217: throw new ValidationException(ref.getMessage());
218: }
219:
220: if (comp_true == false) {
221: throw new ValidationException(ref.getMessage());
222: }
223: }
224: }
225: }
|