← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5254: Improved validation of coordinates. Both UI and service filtering now take into account min and m...

 

------------------------------------------------------------
revno: 5254
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-12-01 13:29:14 +0100
message:
  Improved validation of coordinates. Both UI and service filtering now take into account min and max values for longitude and latitude.
modified:
  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/ValidationUtilsTest.java
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/validationRules.js


--
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-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	2011-10-20 18:51:07 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java	2011-12-01 12:29:14 +0000
@@ -41,7 +41,11 @@
  */
 public class ValidationUtils
 {
-    private static Pattern COORDINATE_PATTERN = Pattern.compile( "\\[([+-]?\\d+\\.?\\d*),([+-]?\\d+\\.?\\d*)\\]" );
+    private static Pattern POINT_PATTERN = Pattern.compile( "\\[(.+),\\s?(.+)\\]" );
+    private static int LONG_MAX = 180;
+    private static int LONG_MIN = -180;
+    private static int LAT_MAX = 90;
+    private static int LAT_MIN = -90;
     
     /**
      * Validates whether an email string is valid.
@@ -106,7 +110,32 @@
      */
     public static boolean coordinateIsValid( String coordinate )
     {
-        return coordinate != null ? COORDINATE_PATTERN.matcher( coordinate ).matches() : false;
+        if ( coordinate == null || coordinate.trim().isEmpty() )
+        {
+            return false;
+        }
+        
+        Matcher matcher = POINT_PATTERN.matcher( coordinate );
+        
+        if ( !matcher.find() )
+        {
+            return false;
+        }
+        
+        double longitude = 0.0;
+        double latitude = 0.0;
+        
+        try
+        {
+            longitude = Double.parseDouble( matcher.group( 1 ) );
+            latitude = Double.parseDouble( matcher.group( 2 ) );
+        }
+        catch ( NumberFormatException ex )
+        {
+            return false;
+        }
+        
+        return longitude >= LONG_MIN && longitude <= LONG_MAX && latitude >= LAT_MIN && latitude <= LAT_MAX;
     }
     
     /**
@@ -124,7 +153,7 @@
             return null;
         }
         
-        Matcher matcher = COORDINATE_PATTERN.matcher( coordinate );
+        Matcher matcher = POINT_PATTERN.matcher( coordinate );
         
         return matcher.find() ? matcher.group( 1 ) : null;
     }
@@ -144,7 +173,7 @@
             return null;
         }
         
-        Matcher matcher = COORDINATE_PATTERN.matcher( coordinate );
+        Matcher matcher = POINT_PATTERN.matcher( coordinate );
         
         return matcher.find() ? matcher.group( 2 ) : null;
     }

=== 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	2011-11-14 14:48:51 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java	2011-12-01 12:29:14 +0000
@@ -48,8 +48,8 @@
         assertTrue( coordinateIsValid( "[+37.99034,-28.94221]" ) );
         assertTrue( coordinateIsValid( "[37.99034,-28.94221]" ) );
         assertTrue( coordinateIsValid( "[+37.99034,28.94221]" ) );
-        assertTrue( coordinateIsValid( "[37.99034,28.94221]" ) );
-        assertTrue( coordinateIsValid( "[+37,-28.94221]" ) );
+        assertTrue( coordinateIsValid( "[170.99034,78.94221]" ) );
+        assertTrue( coordinateIsValid( "[-167,-28.94221]" ) );
         assertTrue( coordinateIsValid( "[37.99034,28]" ) );
         
         assertFalse( coordinateIsValid( "23.34343,56.3232" ) );
@@ -60,6 +60,11 @@
         assertFalse( coordinateIsValid( "[++37,-28.94221]" ) );
         assertFalse( coordinateIsValid( "S-0.27726 E37.08472" ) );
         assertFalse( coordinateIsValid( null ) );
+                
+        assertFalse( coordinateIsValid( "-185.12345,45.45423" ) );
+        assertFalse( coordinateIsValid( "192.56789,-45.34332" ) );
+        assertFalse( coordinateIsValid( "140.34,92.23323" ) );
+        assertFalse( coordinateIsValid( "123.34,-94.23323" ) );
     }
 
     @Test

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/validationRules.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/validationRules.js	2011-10-06 18:31:41 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/validationRules.js	2011-12-01 12:29:14 +0000
@@ -77,11 +77,15 @@
         "openingDate" : {
             "required" : true
         },
+        "longitude" : {
+        	"number" : true,
+        	"min": -180,
+        	"max": 180
+        },
         "latitude" : {
-        	"number" : true
-        },
-        "longitude" : {
-        	"number" : true
+        	"number" : true,
+        	"min": -90,
+        	"max": 90
         },
         "url" : {
             "url" : true,