Author: tv
Date: Tue Jun 24 13:57:54 2008
New Revision: 671342

URL: http://svn.apache.org/viewvc?rev=671342&view=rev
Log:
Port range validators from Turbine 2.3

Added:
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/CompareCallback.java
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DateRangeValidator.java
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FieldReference.java
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/IntegerRangeValidator.java
Modified:
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/Field.java
    
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/FileItemField.java

Modified: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/Field.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/Field.java?rev=671342&r1=671341&r2=671342&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/Field.java
 (original)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/Field.java
 Tue Jun 24 13:57:54 2008
@@ -124,6 +124,9 @@
     /** Has the field passed the validation test? */
     protected boolean validFlag;
 
+    /** Has the field been validated? */
+    protected boolean validated;
+
     /** Does the field require a value? */
     protected boolean required;
 
@@ -316,6 +319,7 @@
     {
         this.parser = pp;
         validFlag = true;
+        validated = false;
 
         this.locale = pp.getLocale();
 
@@ -329,14 +333,14 @@
             {
                 setFlag = true;
             }
-            validate();
+            // validate();
         }
         else if (pp.containsKey(getValueIfAbsent()) &&
                 pp.getString(getValueIfAbsent()) != null)
         {
             pp.add(getKey(), pp.getString(getValueIfAbsent()));
             setFlag = true;
-            validate();
+            // validate();
         }
 
         initialized = true;
@@ -357,6 +361,7 @@
         if (!initialized)
         {
             validFlag = true;
+            validated = false;
         }
         retrievable = obj;
         return this;
@@ -472,6 +477,7 @@
         initialized = false;
         setFlag = false;
         validFlag = false;
+        validated = false;
         required = false;
         message = null;
         retrievable = null;
@@ -517,8 +523,20 @@
     }
 
     /**
-     * Flag set to true, if the test value has been set to
-     * anything other than an empty value.
+     * Flag to determine whether the field has been validated.
+     *
+     * @return value of validated.
+     */
+    public boolean isValidated()
+    {
+        return validated;
+    }
+
+    /**
+     * Flag set to true, if the test value has been set by the parser (even to
+     * an empty value, so don't used this to determine if the field contains a
+     * non-empty value).  Validation will only be executed for fields that have
+     * been set in this manner.
      *
      * @return a <code>boolean</code> value
      */
@@ -580,7 +598,7 @@
     /**
      * Compares request data with constraints and sets the valid flag.
      */
-    protected boolean validate()
+    public boolean validate()
     {
         log.debug(name + ": validate()");
 
@@ -590,10 +608,13 @@
 
             if (isDebugEnabled)
             {
-                log.debug(name + ": Multi-Valued");
-                for (int i = 0; i < stringValues.length; i++)
+                log.debug(name + ": Multi-Valued, Value is " + stringValue);
+                if (stringValues != null)
                 {
-                    log.debug(name + ": " + i + ". Wert: " + stringValues[i]);
+                    for (int i = 0; i < stringValues.length; i++)
+                    {
+                        log.debug(name + ": " + i + ". Value: " + 
stringValues[i]);
+                    }
                 }
             }
 
@@ -601,7 +622,7 @@
             {
                 // set the test value as a String[] which might be replaced by
                 // the correct type if the input is valid.
-                setTestValue(parser.getStrings(getKey()));
+                setTestValue(stringValues);
 
                 try
                 {
@@ -650,6 +671,8 @@
                 doSetValue();
             }
         }
+        
+        validated = true;
 
         return validFlag;
     }
@@ -869,7 +892,7 @@
             throw new IntakeException(
                     "Attempted to assign an invalid input.");
         }
-        if (isSet())
+        if (isSet() && null != getTestValue())
         {
             valArray[0] = getTestValue();
             if (isDebugEnabled)
@@ -902,9 +925,9 @@
             {
                 log.debug(name + ": has a null setter for the mapToProperty"
                         + " Attribute, although all Fields should be mapped"
-                        + " to " + mapToObject + ". If this is unwanted, You"
-                        + " should doublecheck the mapToProperty Attribute, 
and"
-                        + " consult the logs. The Turbine Intake Serice will"
+                        + " to " + mapToObject + ". If this is unwanted, you"
+                        + " should double check the mapToProperty Attribute, 
and"
+                        + " consult the logs. The Turbine Intake Service will"
                         + " have logged a detailed Message with the error.");
             }
         }

Modified: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/FileItemField.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/FileItemField.java?rev=671342&r1=671341&r2=671342&view=diff
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/FileItemField.java
 (original)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/model/FileItemField.java
 Tue Jun 24 13:57:54 2008
@@ -141,7 +141,7 @@
      *
      * @return the valid flag
      */
-    protected boolean validate()
+    public boolean validate()
     {
         ParameterParser pp = (ParameterParser) super.parser;
         if (isMultiValued)

Added: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/CompareCallback.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/CompareCallback.java?rev=671342&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/CompareCallback.java
 (added)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/CompareCallback.java
 Tue Jun 24 13:57:54 2008
@@ -0,0 +1,41 @@
+package org.apache.fulcrum.intake.validator;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Interface to define the compare operation betwen two field values
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Vandahl</a>
+ * @version $Id: DateStringValidator.java 534527 2007-05-02 16:10:59Z tv $
+ */
+public interface CompareCallback
+{
+    /**
+     * Compare the given values using the compare operation provided
+     * 
+     * @param compare type of compare operation
+     * @param thisValue value of this field
+     * @param refValue value of the reference field
+     * 
+     * @return the result of the comparison
+     */
+    public boolean compareValues(int compare, Object thisValue, Object 
refValue)
+        throws ClassCastException;
+}

Added: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DateRangeValidator.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DateRangeValidator.java?rev=671342&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DateRangeValidator.java
 (added)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/DateRangeValidator.java
 Tue Jun 24 13:57:54 2008
@@ -0,0 +1,243 @@
+package org.apache.fulcrum.intake.validator;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.fulcrum.intake.IntakeException;
+import org.apache.fulcrum.intake.model.Field;
+import org.apache.fulcrum.intake.model.Group;
+
+/**
+ * Validates a DateString field in dependency on another DateString field.
+ *
+ * <table>
+ * <tr>
+ *   <th>Name</th><th>Valid Values</th><th>Default Value</th>
+ * </tr>
+ * <tr>
+ *   <td>less-than</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>greater-than</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>less-than-or-equal</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>greater-than-or-equal</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * </table>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Vandahl</a>
+ * @version $Id: DateStringValidator.java 534527 2007-05-02 16:10:59Z tv $
+ */
+public class DateRangeValidator
+        extends DateStringValidator
+{
+    /** List of FieldReferences for multiple comparisons */
+    List fieldReferences; 
+
+    /** Callback for the actual compare operation */
+    CompareCallback compareCallback; 
+
+    public DateRangeValidator(final Map paramMap)
+            throws IntakeException
+    {
+        init(paramMap);
+    }
+
+    /**
+     *  Default constructor
+     */
+    public DateRangeValidator()
+    {
+        super();
+    }
+
+    /**
+     * Constructor to use when initialising Object
+     *
+     * @param paramMap
+     * @throws InvalidMaskException
+     */
+    public void init(final Map paramMap)
+            throws InvalidMaskException
+    {
+        super.init(paramMap);
+        
+        compareCallback = new CompareCallback()
+            {
+                /**
+                 * Compare the given values using the compare operation 
provided
+                 * 
+                 * @param compare type of compare operation
+                 * @param thisValue value of this field
+                 * @param refValue value of the reference field
+                 * 
+                 * @return the result of the comparison
+                 */
+                public boolean compareValues(int compare, Object thisValue, 
Object refValue)
+                    throws ClassCastException
+                {
+                    boolean result = true;
+                    
+                    Date thisDate = (Date)thisValue;
+                    Date otherDate = (Date)refValue;
+                    
+                    switch (compare)
+                    {
+                        case FieldReference.COMPARE_LT:
+                            result = thisDate.before(otherDate);
+                            break;
+                            
+                        case FieldReference.COMPARE_LTE:
+                            result = !thisDate.after(otherDate);
+                            break;
+                            
+                        case FieldReference.COMPARE_GT:
+                            result = thisDate.after(otherDate);
+                            break;
+                            
+                        case FieldReference.COMPARE_GTE:
+                            result = !thisDate.before(otherDate);
+                            break;
+                    }
+                    
+                    return result;
+                }
+            };
+        
+        fieldReferences = new ArrayList(10);
+
+        for (Iterator i = paramMap.entrySet().iterator(); i.hasNext();)
+        {
+            Map.Entry entry = (Map.Entry)i.next();
+            String key = (String)entry.getKey();
+            Constraint constraint = (Constraint)entry.getValue();
+
+            int compare = FieldReference.getCompareType(key);
+            
+            if (compare != 0)
+            {
+                // found matching constraint
+                FieldReference fieldref = new FieldReference();
+                fieldref.setCompare(compare);
+                fieldref.setFieldName(constraint.getValue());
+                fieldref.setMessage(constraint.getMessage());
+                
+                fieldReferences.add(fieldref);
+            }
+        }
+        
+        if (fieldReferences.isEmpty())
+        {
+            log.warn("No reference field rules have been found.");
+        }
+    }
+    
+    /**
+     * Determine whether a testValue meets the criteria specified
+     * in the constraints defined for this validator
+     *
+     * @param testField a <code>Field</code> to be tested
+     * @exception ValidationException containing an error message if the
+     * testValue did not pass the validation tests.
+     */
+    public void assertValidity(final Field testField)
+        throws ValidationException
+    {
+        super.assertValidity(testField);
+        
+        Group thisGroup = testField.getGroup();
+
+        if (testField.isMultiValued())
+        {
+            String[] stringValues = (String[])testField.getTestValue();
+
+            for (int i = 0; i < stringValues.length; i++)
+            {
+                assertValidity(stringValues[i], thisGroup);
+            }
+        }
+        else
+        {
+            String testValue = (String)testField.getTestValue();
+        
+            assertValidity(testValue, thisGroup);
+        }
+    }
+
+    /**
+     * Determine whether a testValue meets the criteria specified
+     * in the constraints defined for this validator
+     *
+     * @param testValue a <code>String</code> to be tested
+     * @param group the group this field belongs to
+     * 
+     * @exception ValidationException containing an error message if the
+     * testValue did not pass the validation tests.
+     */
+    public void assertValidity(final String testValue, final Group group)
+        throws ValidationException
+    {
+        if (required || StringUtils.isNotEmpty(testValue))
+        {
+            Date testDate = null;
+            
+            try
+            {
+                testDate = parse(testValue);
+            }
+            catch (ParseException e)
+            {
+                // This should not happen because we succeded with this 
before, 
+                // but we need to catch the exception anyway
+                errorMessage = getDateFormatMessage();
+                throw new ValidationException(errorMessage);
+            }
+            
+            try
+            {
+                FieldReference.checkReferences(fieldReferences, 
compareCallback, 
+                        testDate, group);
+            }
+            catch (ValidationException e)
+            {
+                errorMessage = e.getMessage();
+                throw e;
+            }
+        }
+    }
+}

Added: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FieldReference.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FieldReference.java?rev=671342&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FieldReference.java
 (added)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/FieldReference.java
 Tue Jun 24 13:57:54 2008
@@ -0,0 +1,251 @@
+package org.apache.fulcrum.intake.validator;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.fulcrum.intake.IntakeException;
+import org.apache.fulcrum.intake.model.Field;
+import org.apache.fulcrum.intake.model.Group;
+
+/**
+ * Helper Class to manage relations between fields. The following
+ * comparisons are supported:
+ * 
+ * <table>
+ * <tr>
+ *   <th>Name</th><th>Valid Values</th><th>Default Value</th>
+ * </tr>
+ * <tr>
+ *   <td>less-than</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>greater-than</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>less-than-or-equal</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>greater-than-or-equal</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * </table>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Vandahl</a>
+ * @version $Id: DateStringValidator.java 534527 2007-05-02 16:10:59Z tv $
+ */
+public class FieldReference
+{
+    /** a local logger */
+    protected static final Log log = LogFactory.getLog(FieldReference.class);
+    
+    /** Rule name for "&lt;" comparison */
+    public static final String RANGE_LT = "less-than";
+
+    /** Rule name for "&gt;" comparison */
+    public static final String RANGE_GT = "greater-than";
+
+    /** Rule name for "&lt;=" comparison */
+    public static final String RANGE_LTE = "less-than-or-equal";
+
+    /** Rule name for "&gt;=" comparison */
+    public static final String RANGE_GTE = "greater-than-or-equal";
+
+    /** Integer value for "&lt;" comparison */
+    public static final int COMPARE_LT = 1;
+
+    /** Integer value for "&gt;" comparison */
+    public static final int COMPARE_GT = 2;
+
+    /** Integer value for "&lt;=" comparison */
+    public static final int COMPARE_LTE = 3;
+
+    /** Integer value for "&gt;=" comparison */
+    public static final int COMPARE_GTE = 4;
+
+    /** Numeric comparison */
+    private int compare = 0;
+    
+    /** Name of referenced field */
+    private String fieldName = null;
+
+    /** Error message */
+    private String message = null;
+    
+    /**
+     *  Constructor
+     */
+    public FieldReference()
+    {
+        // do nothing
+    }
+
+    /**
+     * @return the comparison type
+     */
+    public int getCompare()
+    {
+        return compare;
+    }
+
+    /**
+     * @param compare the comparison type to set
+     */
+    public void setCompare(int compare)
+    {
+        this.compare = compare;
+    }
+
+    /**
+     * @return the field name
+     */
+    public String getFieldName()
+    {
+        return fieldName;
+    }
+
+    /**
+     * @param fieldName the field name to set
+     */
+    public void setFieldName(String fieldName)
+    {
+        this.fieldName = fieldName;
+    }
+
+    /**
+     * @return the message
+     */
+    public String getMessage()
+    {
+        return message;
+    }
+
+    /**
+     * @param message the message to set
+     */
+    public void setMessage(String message)
+    {
+        this.message = message;
+    }
+    
+    /**
+     * Map the comparison strings to their numeric counterparts
+     * 
+     * @param key the 
+     * @return
+     */
+    public static int getCompareType(String key)
+    {
+        int compareType = 0;
+        
+        if (key.equals(RANGE_LT))
+        {
+            compareType = COMPARE_LT;
+        }
+        else if (key.equals(RANGE_LTE))
+        {
+            compareType = COMPARE_LTE;
+        }
+        else if (key.equals(RANGE_GT))
+        {
+            compareType = COMPARE_GT;
+        }
+        else if (key.equals(RANGE_GTE))
+        {
+            compareType = COMPARE_GTE;
+        }
+        
+        return compareType;
+    }
+    
+    /**
+     * Check the parsed value against the referenced fields
+     * 
+     * @param fieldReferences List of field references to check
+     * @param compareCallback Callback to the actual compare operation
+     * @param value the parsed value of the related field
+     * @param group the group the related field belongs to
+     * 
+     * @throws ValidationException
+     */
+    public static void checkReferences(List fieldReferences, CompareCallback 
compareCallback, 
+            Object value, Group group)
+        throws ValidationException
+    {
+        for (Iterator i = fieldReferences.iterator(); i.hasNext();)
+        {
+            FieldReference ref = (FieldReference)i.next();
+            boolean comp_true = true;
+
+            try
+            {
+                Field refField = group.get(ref.getFieldName());
+                
+                if (refField.isSet())
+                {
+                    /*
+                     * Fields are processed in sequence so that our
+                     * reference field might have been set but not
+                     * yet validated. We check this here.
+                     */
+                    if (!refField.isValidated())
+                    {
+                        refField.validate();
+                    }
+                    
+                    if (refField.isValid())
+                    {
+                        try
+                        {
+                            comp_true = 
compareCallback.compareValues(ref.getCompare(), 
+                                    value, 
+                                    refField.getValue());
+                        }
+                        catch (ClassCastException e)
+                        {
+                            throw new IntakeException("Type mismatch comparing 
" +
+                                    value + " with " + refField.getValue(), e);
+                        }
+                    }
+                }
+            }
+            catch (IntakeException e)
+            {
+                log.error("Validate operation failed.", e);
+                throw new ValidationException(ref.getMessage());
+            }
+
+            if (comp_true == false)
+            {
+                throw new ValidationException(ref.getMessage());
+            }
+        }
+    }
+}

Added: 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/IntegerRangeValidator.java
URL: 
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/IntegerRangeValidator.java?rev=671342&view=auto
==============================================================================
--- 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/IntegerRangeValidator.java
 (added)
+++ 
turbine/fulcrum/trunk/intake/src/java/org/apache/fulcrum/intake/validator/IntegerRangeValidator.java
 Tue Jun 24 13:57:54 2008
@@ -0,0 +1,229 @@
+package org.apache.fulcrum.intake.validator;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.fulcrum.intake.IntakeException;
+import org.apache.fulcrum.intake.model.Field;
+import org.apache.fulcrum.intake.model.Group;
+
+/**
+ * Validates an int field in dependency on another int field.
+ *
+ * <table>
+ * <tr>
+ *   <th>Name</th><th>Valid Values</th><th>Default Value</th>
+ * </tr>
+ * <tr>
+ *   <td>less-than</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>greater-than</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>less-than-or-equal</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * <tr>
+ *   <td>greater-than-or-equal</td>
+ *   <td>&lt;name of other field&gt;</td>
+ *   <td>&nbsp;</td>
+ * </tr>
+ * </table>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Vandahl</a>
+ * @version $Id: DateStringValidator.java 534527 2007-05-02 16:10:59Z tv $
+ */
+public class IntegerRangeValidator
+        extends IntegerValidator
+{
+    /** List of FieldReferences for multiple comparisons */
+    List fieldReferences; 
+
+    /** Callback for the actual compare operation */
+    CompareCallback compareCallback; 
+
+    public IntegerRangeValidator(final Map paramMap)
+            throws IntakeException
+    {
+        init(paramMap);
+    }
+
+    /**
+     *  Default constructor
+     */
+    public IntegerRangeValidator()
+    {
+        super();
+    }
+
+    /**
+     * Constructor to use when initialising Object
+     *
+     * @param paramMap
+     * @throws InvalidMaskException
+     */
+    public void init(final Map paramMap)
+            throws InvalidMaskException
+    {
+        super.init(paramMap);
+        
+        compareCallback = new CompareCallback()
+            {
+                /**
+                 * Compare the given values using the compare operation 
provided
+                 * 
+                 * @param compare type of compare operation
+                 * @param thisValue value of this field
+                 * @param refValue value of the reference field
+                 * 
+                 * @return the result of the comparison
+                 */
+                public boolean compareValues(int compare, Object thisValue, 
Object refValue)
+                    throws ClassCastException
+                {
+                    boolean result = true;
+                    
+                    Integer thisInt = (Integer)thisValue;
+                    Integer otherInt = (Integer)refValue;
+                    
+                    switch (compare)
+                    {
+                        case FieldReference.COMPARE_LT:
+                            result = thisInt.compareTo(otherInt) < 0;
+                            break;
+                            
+                        case FieldReference.COMPARE_LTE:
+                            result = thisInt.compareTo(otherInt) <= 0;
+                            break;
+                            
+                        case FieldReference.COMPARE_GT:
+                            result = thisInt.compareTo(otherInt) > 0;
+                            break;
+                            
+                        case FieldReference.COMPARE_GTE:
+                            result = thisInt.compareTo(otherInt) >= 0;
+                            break;
+                    }
+                    
+                    return result;
+                }
+            };
+        
+        fieldReferences = new ArrayList(10);
+
+        for (Iterator i = paramMap.entrySet().iterator(); i.hasNext();)
+        {
+            Map.Entry entry = (Map.Entry)i.next();
+            String key = (String)entry.getKey();
+            Constraint constraint = (Constraint)entry.getValue();
+
+            int compare = FieldReference.getCompareType(key);
+            
+            if (compare != 0)
+            {
+                // found matching constraint
+                FieldReference fieldref = new FieldReference();
+                fieldref.setCompare(compare);
+                fieldref.setFieldName(constraint.getValue());
+                fieldref.setMessage(constraint.getMessage());
+                
+                fieldReferences.add(fieldref);
+            }
+        }
+        
+        if (fieldReferences.isEmpty())
+        {
+            log.warn("No reference field rules have been found.");
+        }
+    }
+    
+    /**
+     * Determine whether a testValue meets the criteria specified
+     * in the constraints defined for this validator
+     *
+     * @param testField a <code>Field</code> to be tested
+     * @exception ValidationException containing an error message if the
+     * testValue did not pass the validation tests.
+     */
+    public void assertValidity(final Field testField)
+        throws ValidationException
+    {
+        super.assertValidity(testField);
+        
+        Group thisGroup = testField.getGroup();
+
+        if (testField.isMultiValued())
+        {
+            String[] stringValues = (String[])testField.getTestValue();
+
+            for (int i = 0; i < stringValues.length; i++)
+            {
+                assertValidity(stringValues[i], thisGroup);
+            }
+        }
+        else
+        {
+            String testValue = (String)testField.getTestValue();
+        
+            assertValidity(testValue, thisGroup);
+        }
+    }
+
+    /**
+     * Determine whether a testValue meets the criteria specified
+     * in the constraints defined for this validator
+     *
+     * @param testValue a <code>String</code> to be tested
+     * @param group the group this field belongs to
+     * 
+     * @exception ValidationException containing an error message if the
+     * testValue did not pass the validation tests.
+     */
+    public void assertValidity(final String testValue, final Group group)
+        throws ValidationException
+    {
+        if (required || StringUtils.isNotEmpty(testValue))
+        {
+            Integer testInt = new Integer(testValue);
+            
+            try
+            {
+                FieldReference.checkReferences(fieldReferences, 
compareCallback, 
+                        testInt, group);
+            }
+            catch (ValidationException e)
+            {
+                errorMessage = e.getMessage();
+                throw e;
+            }
+        }
+    }
+}


Reply via email to