← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9219: FRED-API: re-organised validation. Added new @IdentifiableObjectExists annotation, that will chec...

 

------------------------------------------------------------
revno: 9219
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-12-08 11:31:02 +0300
message:
  FRED-API: re-organised validation. Added new @IdentifiableObjectExists annotation, that will check an Class<? extends IdentifiableObject> against identifiableObjectManager (where uid = value of the field)
removed:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Create.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Update.java
added:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/IdObjectExistsValidator.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/IdentifiableObjectExists.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Create.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Update.java
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml


--
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-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2012-12-07 20:03:51 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2012-12-08 08:31:02 +0000
@@ -66,7 +66,7 @@
     private OrganisationUnitService organisationUnitService;
 
     @Autowired
-    @Qualifier( "fredConversionService" )
+    @Qualifier( "conversionService" )
     private ConversionService conversionService;
 
     //--------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java	2012-12-08 07:58:59 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java	2012-12-08 08:31:02 +0000
@@ -31,8 +31,8 @@
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.web.webapi.v1.domain.Facility;
 import org.hisp.dhis.web.webapi.v1.utils.ValidationUtils;
-import org.hisp.dhis.web.webapi.v1.utils.validationgroups.Create;
-import org.hisp.dhis.web.webapi.v1.utils.validationgroups.Update;
+import org.hisp.dhis.web.webapi.v1.validation.group.Create;
+import org.hisp.dhis.web.webapi.v1.validation.group.Update;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.http.HttpStatus;
@@ -45,6 +45,7 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 
 import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
 import java.io.IOException;
 import java.util.Set;
 
@@ -61,6 +62,9 @@
     @Qualifier( "org.hisp.dhis.organisationunit.OrganisationUnitService" )
     private OrganisationUnitService organisationUnitService;
 
+    @Autowired
+    private Validator validator;
+
     //--------------------------------------------------------------------------
     // EXTRA WEB METHODS
     //--------------------------------------------------------------------------
@@ -100,7 +104,7 @@
     @RequestMapping( value = "/validate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
     public ResponseEntity<String> validateFacilityForCreate( @RequestBody Facility facility ) throws IOException
     {
-        Set<ConstraintViolation<Facility>> constraintViolations = ValidationUtils.validate( facility, Create.class );
+        Set<ConstraintViolation<Facility>> constraintViolations = validator.validate( facility, Create.class );
 
         String json = ValidationUtils.constraintViolationsToJson( constraintViolations );
 
@@ -117,7 +121,7 @@
     @RequestMapping( value = "/validate", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE )
     public ResponseEntity<String> validateFacilityForUpdate( @RequestBody Facility facility ) throws IOException
     {
-        Set<ConstraintViolation<Facility>> constraintViolations = ValidationUtils.validate( facility, Update.class );
+        Set<ConstraintViolation<Facility>> constraintViolations = validator.validate( facility, Update.class );
 
         String json = ValidationUtils.constraintViolationsToJson( constraintViolations );
 

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2012-12-08 07:58:59 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2012-12-08 08:31:02 +0000
@@ -28,8 +28,10 @@
  */
 
 import org.hibernate.validator.constraints.Length;
-import org.hisp.dhis.web.webapi.v1.utils.validationgroups.Create;
-import org.hisp.dhis.web.webapi.v1.utils.validationgroups.Update;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.IdentifiableObjectExists;
+import org.hisp.dhis.web.webapi.v1.validation.group.Create;
+import org.hisp.dhis.web.webapi.v1.validation.group.Update;
 
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Null;
@@ -44,6 +46,7 @@
     @Length( min = 11, max = 11, groups = { Create.class, Update.class } )
     @NotNull( groups = { Update.class } )
     @Null( groups = { Create.class } )
+    @IdentifiableObjectExists( value = OrganisationUnit.class, groups = Update.class )
     private String id;
 
     // Name of the facility

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java	2012-12-08 07:58:59 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java	2012-12-08 08:31:02 +0000
@@ -30,9 +30,6 @@
 import org.codehaus.jackson.map.ObjectMapper;
 
 import javax.validation.ConstraintViolation;
-import javax.validation.Validation;
-import javax.validation.Validator;
-import javax.validation.ValidatorFactory;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
@@ -43,24 +40,6 @@
  */
 public class ValidationUtils
 {
-    private static Validator validator;
-
-    static
-    {
-        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
-        validator = factory.getValidator();
-    }
-
-    public static <T> Set<ConstraintViolation<T>> validate( T object )
-    {
-        return validator.validate( object );
-    }
-
-    public static <T> Set<ConstraintViolation<T>> validate( T object, Class<?>... groups )
-    {
-        return validator.validate( object, groups );
-    }
-
     public static <T> String constraintViolationsToJson( Set<ConstraintViolation<T>> constraintViolations ) throws IOException
     {
         Map<String, String> constraintViolationsMap = new HashMap<String, String>();

=== removed directory 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups'
=== removed file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Create.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Create.java	2012-12-08 07:58:59 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Create.java	1970-01-01 00:00:00 +0000
@@ -1,8 +0,0 @@
-package org.hisp.dhis.web.webapi.v1.utils.validationgroups;
-
-/**
- * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
- */
-public interface Create
-{
-}

=== removed file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Update.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Update.java	2012-12-08 07:58:59 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups/Update.java	1970-01-01 00:00:00 +0000
@@ -1,8 +0,0 @@
-package org.hisp.dhis.web.webapi.v1.utils.validationgroups;
-
-/**
- * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
- */
-public interface Update
-{
-}

=== added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation'
=== added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint'
=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/IdObjectExistsValidator.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/IdObjectExistsValidator.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/IdObjectExistsValidator.java	2012-12-08 08:31:02 +0000
@@ -0,0 +1,63 @@
+package org.hisp.dhis.web.webapi.v1.validation.constraint;
+
+/*
+ * Copyright (c) 2004-2012, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.IdentifiableObjectExists;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class IdObjectExistsValidator implements ConstraintValidator<IdentifiableObjectExists, String>
+{
+    @Autowired
+    @Qualifier( "org.hisp.dhis.common.IdentifiableObjectManager" )
+    private IdentifiableObjectManager identifiableObjectManager;
+
+    private Class<? extends IdentifiableObject> identifiableObjectClass;
+
+    @Override
+    public void initialize( IdentifiableObjectExists constraintAnnotation )
+    {
+        identifiableObjectClass = constraintAnnotation.value();
+    }
+
+    @Override
+    public boolean isValid( String value, ConstraintValidatorContext context )
+    {
+        IdentifiableObject identifiableObject = identifiableObjectManager.get( identifiableObjectClass, value );
+
+        return identifiableObject != null;
+    }
+}

=== added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation'
=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/IdentifiableObjectExists.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/IdentifiableObjectExists.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/IdentifiableObjectExists.java	2012-12-08 08:31:02 +0000
@@ -0,0 +1,56 @@
+package org.hisp.dhis.web.webapi.v1.validation.constraint.annotation;
+
+/*
+ * Copyright (c) 2004-2012, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.web.webapi.v1.validation.constraint.IdObjectExistsValidator;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+
+@Target( { ElementType.METHOD, ElementType.FIELD } )
+@Retention( RetentionPolicy.RUNTIME )
+@Constraint( validatedBy = IdObjectExistsValidator.class )
+public @interface IdentifiableObjectExists
+{
+    String message() default "No object found with that ID.";
+
+    Class<?>[] groups() default { };
+
+    Class<? extends Payload>[] payload() default { };
+
+    Class<? extends IdentifiableObject> value();
+}

=== added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group'
=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Create.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Create.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Create.java	2012-12-08 08:31:02 +0000
@@ -0,0 +1,8 @@
+package org.hisp.dhis.web.webapi.v1.validation.group;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface Create
+{
+}

=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Update.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Update.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/group/Update.java	2012-12-08 08:31:02 +0000
@@ -0,0 +1,8 @@
+package org.hisp.dhis.web.webapi.v1.validation.group;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface Update
+{
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml	2012-12-07 20:03:51 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml	2012-12-08 08:31:02 +0000
@@ -11,7 +11,7 @@
   <context:component-scan base-package="org.hisp.dhis.web.webapi" />
   <context:annotation-config />
 
-  <bean id="fredConversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
+  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
     <property name="converters">
       <set>
         <ref bean="toFacilityConverter" />
@@ -19,7 +19,9 @@
     </property>
   </bean>
 
-  <mvc:annotation-driven conversion-service="fredConversionService" />
+  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
+
+  <mvc:annotation-driven conversion-service="conversionService" validator="validator" />
 
   <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
     <property name="resourceLoaderPath" value="/WEB-INF/api-fred-velocity/" />