dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #22176
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10639: Impl validation methods for various number types and for data values according to data element va...
------------------------------------------------------------
revno: 10639
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-04-19 17:02:38 +0200
message:
Impl validation methods for various number types and for data values according to data element value type in dhis-support-system
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java
dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java
dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java 2013-02-13 03:57:52 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java 2013-04-19 15:02:38 +0000
@@ -304,8 +304,7 @@
/**
* Returns the value type. If value type is int and the number type exists,
- * the number type is returned, if the type is int and the number type does
- * not exist int is returned.
+ * the number type is returned, otherwise the type is returned.
*/
public String getDetailedNumberType()
{
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java 2013-04-19 14:12:51 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java 2013-04-19 15:02:38 +0000
@@ -38,7 +38,6 @@
/**
* @author Lars Helge Overland
- * @version $Id: MathUtil.java 4712 2008-03-12 10:32:45Z larshelg $
*/
public class MathUtils
{
@@ -48,6 +47,10 @@
private static final double TOLERANCE = 0.01;
private static final Pattern NUMERIC_PATTERN = Pattern.compile( "^(0|-?[1-9]\\d*)(\\.\\d+)?$" );
+ private static final Pattern INT_PATTERN = Pattern.compile( "^(0|-?[1-9]\\d*)$" );
+ private static final Pattern POSITIVE_INT_PATTERN = Pattern.compile( "^[1-9]\\d*$" );
+ private static final Pattern NEGATIVE_INT_PATTERN = Pattern.compile( "^-[1-9]\\d*$" );
+ private static final Pattern ZERO_PATTERN = Pattern.compile( "^0(\\.0*)?$" );
/**
* Validates whether an expression is true or false.
@@ -197,16 +200,63 @@
}
/**
- * Returns true if the provided String argument can be converted to a Double,
- * false if not.
+ * Returns true if the provided string argument is to be considered numeric.
*
* @param value the value.
- * @return true if the provided String argument can be converted to a Double.
+ * @return true if the provided string argument is to be considered numeric.
*/
public static boolean isNumeric( String value )
{
return value != null && NUMERIC_PATTERN.matcher( value ).matches();
}
+
+ /**
+ * Returns true if the provided string argument is to be considered an integer.
+ *
+ * @param value the value.
+ * @return true if the provided string argument is to be considered an integer.
+ */
+ public static boolean isInteger( String value )
+ {
+ return value != null && INT_PATTERN.matcher( value ).matches();
+ }
+
+ /**
+ * Returns true if the provided string argument is to be considered a positive
+ * integer.
+ *
+ * @param value the value.
+ * @return true if the provided string argument is to be considered a positive
+ * integer.
+ */
+ public static boolean isPositiveInteger( String value )
+ {
+ return value != null && POSITIVE_INT_PATTERN.matcher( value ).matches();
+ }
+
+ /**
+ * Returns true if the provided string argument is to be considered a negative
+ * integer.
+ *
+ * @param value the value.
+ * @return true if the provided string argument is to be considered a negative
+ * integer.
+ */
+ public static boolean isNegativeInteger( String value )
+ {
+ return value != null && NEGATIVE_INT_PATTERN.matcher( value ).matches();
+ }
+
+ /**
+ * Returns true if the provided string argument is to be considered a zero.
+ *
+ * @param value the value.
+ * @return true if the provided string argument is to be considered a zero.
+ */
+ public static boolean isZero( String value )
+ {
+ return value != null && ZERO_PATTERN.matcher( value ).matches();
+ }
/**
* Tests whether the two decimal numbers are equal with a tolerance of 0.01.
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2013-04-16 12:07:17 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2013-04-19 15:02:38 +0000
@@ -27,6 +27,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import java.util.Arrays;
+import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -34,6 +36,9 @@
import org.apache.commons.validator.routines.DateValidator;
import org.apache.commons.validator.routines.EmailValidator;
import org.apache.commons.validator.routines.UrlValidator;
+import org.hisp.dhis.dataelement.DataElement;
+
+import static org.hisp.dhis.dataelement.DataElement.*;
/**
* @author Lars Helge Overland
@@ -204,4 +209,67 @@
{
return "[" + longitude + "," + latitude + "]";
}
+
+ /**
+ * Checks if the given data value is valid according to the value type of the
+ * given data element. Returns a string if the valid is invalid, possible
+ * values are:
+ *
+ * <ul>
+ * <li>value_null_or_empty</li>
+ * <li>data_element_or_type_null_or_empty</li>
+ * <li>value_length_greater_than_max_length</li>
+ * <li>value_not_numeric</li>
+ * <li>value_not_integer</li>
+ * <li>value_not_positive_integer</li>
+ * <li>value_not_negative_integer</li>
+ * </ul>
+ *
+ * @param value the data value.
+ * @param dataElement the data element.
+ * @return null if the value is valid, a string if not.
+ */
+ public static String dataValueIsValid( String value, DataElement dataElement )
+ {
+ if ( value == null || value.trim().isEmpty() )
+ {
+ return "value_null_or_empty";
+ }
+
+ if ( dataElement == null || dataElement.getType() == null || dataElement.getType().isEmpty() )
+ {
+ return "data_element_or_type_null_or_empty";
+ }
+
+ List<String> types = Arrays.asList( VALUE_TYPE_STRING, VALUE_TYPE_INT, VALUE_TYPE_NUMBER, VALUE_TYPE_POSITIVE_INT, VALUE_TYPE_NEGATIVE_INT );
+
+ String type = dataElement.getDetailedNumberType();
+
+ if ( types.contains( type ) && value.length() > 255 )
+ {
+ return "value_length_greater_than_max_length";
+ }
+
+ if ( VALUE_TYPE_NUMBER.equals( type ) && !MathUtils.isNumeric( value ) )
+ {
+ return "value_not_numeric";
+ }
+
+ if ( VALUE_TYPE_INT.equals( type ) && !MathUtils.isInteger( value ) )
+ {
+ return "value_not_integer";
+ }
+
+ if ( VALUE_TYPE_POSITIVE_INT.equals( type ) && !MathUtils.isPositiveInteger( value ) )
+ {
+ return "value_not_positive_integer";
+ }
+
+ if ( VALUE_TYPE_NEGATIVE_INT.equals( type ) && !MathUtils.isNegativeInteger( value ) )
+ {
+ return "value_not_negative_integer";
+ }
+
+ return null;
+ }
}
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java 2013-04-19 13:52:46 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java 2013-04-19 15:02:38 +0000
@@ -121,6 +121,71 @@
}
@Test
+ public void testIsInteger()
+ {
+ assertTrue( MathUtils.isInteger( "1" ) );
+ assertTrue( MathUtils.isInteger( "123" ) );
+ assertTrue( MathUtils.isInteger( "-2" ) );
+ assertTrue( MathUtils.isInteger( "0" ) );
+
+ assertFalse( MathUtils.isInteger( "1.1" ) );
+ assertFalse( MathUtils.isInteger( "+4" ) );
+ assertFalse( MathUtils.isInteger( "-0" ) );
+ assertFalse( MathUtils.isInteger( "Hey" ) );
+ assertFalse( MathUtils.isInteger( " 1" ) );
+ assertFalse( MathUtils.isInteger( "1 " ) );
+ assertFalse( MathUtils.isInteger( "1.2345" ) );
+ }
+
+ @Test
+ public void testIsPositiveInteger()
+ {
+ assertTrue( MathUtils.isPositiveInteger( "1" ) );
+ assertTrue( MathUtils.isPositiveInteger( "123" ) );
+
+ assertFalse( MathUtils.isPositiveInteger( "0" ) );
+ assertFalse( MathUtils.isPositiveInteger( "+2" ) );
+ assertFalse( MathUtils.isPositiveInteger( "-2" ) );
+ assertFalse( MathUtils.isPositiveInteger( "-2232" ) );
+ assertFalse( MathUtils.isPositiveInteger( "-2.17" ) );
+ assertFalse( MathUtils.isPositiveInteger( "1.1" ) );
+ assertFalse( MathUtils.isPositiveInteger( "-0" ) );
+ assertFalse( MathUtils.isPositiveInteger( "Hey" ) );
+ assertFalse( MathUtils.isPositiveInteger( "1 " ) );
+ assertFalse( MathUtils.isPositiveInteger( "1.2345" ) );
+ }
+
+ @Test
+ public void testIsNegativeInteger()
+ {
+ assertTrue( MathUtils.isNegativeInteger( "-1" ) );
+ assertTrue( MathUtils.isNegativeInteger( "-123" ) );
+
+ assertFalse( MathUtils.isNegativeInteger( "0" ) );
+ assertFalse( MathUtils.isNegativeInteger( "+2" ) );
+ assertFalse( MathUtils.isNegativeInteger( "2" ) );
+ assertFalse( MathUtils.isNegativeInteger( "2232" ) );
+ assertFalse( MathUtils.isNegativeInteger( "2.17" ) );
+ assertFalse( MathUtils.isNegativeInteger( "1.1" ) );
+ assertFalse( MathUtils.isNegativeInteger( "-0" ) );
+ assertFalse( MathUtils.isNegativeInteger( "Hey" ) );
+ assertFalse( MathUtils.isNegativeInteger( "2 " ) );
+ assertFalse( MathUtils.isNegativeInteger( "6.1345" ) );
+ }
+
+ @Test
+ public void testIsZero()
+ {
+ assertTrue( MathUtils.isZero( "0" ) );
+
+ assertFalse( MathUtils.isZero( "+0" ) );
+ assertFalse( MathUtils.isZero( "-0" ) );
+ assertFalse( MathUtils.isZero( "2232" ) );
+ assertFalse( MathUtils.isZero( "2.17" ) );
+ assertFalse( MathUtils.isZero( "Hey" ) );
+ }
+
+ @Test
public void testGetAverage()
{
assertEquals( 7.5, MathUtils.getAverage( Arrays.asList( 5.0, 5.0, 10.0, 10.0 ) ), 0.01 );
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2012-12-21 12:59:39 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2013-04-19 15:02:38 +0000
@@ -31,12 +31,16 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
import static org.hisp.dhis.system.util.ValidationUtils.coordinateIsValid;
import static org.hisp.dhis.system.util.ValidationUtils.getLatitude;
import static org.hisp.dhis.system.util.ValidationUtils.getLongitude;
import static org.hisp.dhis.system.util.ValidationUtils.passwordIsValid;
import static org.hisp.dhis.system.util.ValidationUtils.emailIsValid;
+import static org.hisp.dhis.system.util.ValidationUtils.dataValueIsValid;
+import org.hisp.dhis.dataelement.DataElement;
import org.junit.Test;
/**
@@ -104,4 +108,32 @@
assertFalse( emailIsValid( "john@doe" ) );
assertTrue( emailIsValid( "john@xxxxxxx" ) );
}
+
+ @Test
+ public void testDataValueIsValid()
+ {
+ DataElement de = new DataElement( "DEA" );
+ de.setType( DataElement.VALUE_TYPE_INT );
+
+ assertNotNull( dataValueIsValid( null, de ) );
+ assertNotNull( dataValueIsValid( "", de ) );
+
+ assertNull( dataValueIsValid( "34", de ) );
+ assertNotNull( dataValueIsValid( "Yes", de ) );
+
+ de.setNumberType( DataElement.VALUE_TYPE_NUMBER );
+
+ assertNull( dataValueIsValid( "3.7", de ) );
+ assertNotNull( dataValueIsValid( "No", de ) );
+
+ de.setNumberType( DataElement.VALUE_TYPE_POSITIVE_INT );
+
+ assertNull( dataValueIsValid( "3", de ) );
+ assertNotNull( dataValueIsValid( "-4", de ) );
+
+ de.setNumberType( DataElement.VALUE_TYPE_NEGATIVE_INT );
+
+ assertNull( dataValueIsValid( "-3", de ) );
+ assertNotNull( dataValueIsValid( "4", de ) );
+ }
}