dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #20317
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9218: FRED-API: updated Facility-validation to support validation groups, will now validate based on wh...
------------------------------------------------------------
revno: 9218
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-12-08 10:58:59 +0300
message:
FRED-API: updated Facility-validation to support validation groups, will now validate based on whether its an create or update. Updated validate service to support both POST/PUT validation.
added:
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
modified:
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/webapp/WEB-INF/api-fred-velocity/v1/layout.vm
--
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/FacilityServiceController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java 2012-12-07 17:16:42 +0000
+++ 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
@@ -31,6 +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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
@@ -49,21 +51,21 @@
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
*/
-@Controller(value = "facility-service-controller-" + FredController.PREFIX)
-@RequestMapping(FacilityServiceController.RESOURCE_PATH)
+@Controller( value = "facility-service-controller-" + FredController.PREFIX )
+@RequestMapping( FacilityServiceController.RESOURCE_PATH )
public class FacilityServiceController
{
public static final String RESOURCE_PATH = "/" + FredController.PREFIX + "/facility-service";
@Autowired
- @Qualifier("org.hisp.dhis.organisationunit.OrganisationUnitService")
+ @Qualifier( "org.hisp.dhis.organisationunit.OrganisationUnitService" )
private OrganisationUnitService organisationUnitService;
//--------------------------------------------------------------------------
// EXTRA WEB METHODS
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{id}/activate", method = RequestMethod.POST)
+ @RequestMapping( value = "/{id}/activate", method = RequestMethod.POST )
public ResponseEntity<Void> activateFacility( @PathVariable String id )
{
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id );
@@ -79,7 +81,7 @@
return new ResponseEntity<Void>( HttpStatus.NOT_FOUND );
}
- @RequestMapping(value = "/{id}/deactivate", method = RequestMethod.POST)
+ @RequestMapping( value = "/{id}/deactivate", method = RequestMethod.POST )
public ResponseEntity<Void> deactivateFacility( @PathVariable String id )
{
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id );
@@ -96,9 +98,26 @@
}
@RequestMapping( value = "/validate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
- public ResponseEntity<String> validateFacility( @RequestBody Facility facility ) throws IOException
- {
- Set<ConstraintViolation<Facility>> constraintViolations = ValidationUtils.validate( facility );
+ public ResponseEntity<String> validateFacilityForCreate( @RequestBody Facility facility ) throws IOException
+ {
+ Set<ConstraintViolation<Facility>> constraintViolations = ValidationUtils.validate( facility, Create.class );
+
+ String json = ValidationUtils.constraintViolationsToJson( constraintViolations );
+
+ if ( constraintViolations.isEmpty() )
+ {
+ return new ResponseEntity<String>( json, HttpStatus.OK );
+ }
+ else
+ {
+ return new ResponseEntity<String>( json, HttpStatus.UNPROCESSABLE_ENTITY );
+ }
+ }
+
+ @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 );
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-07 16:00: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 07:58:59 +0000
@@ -28,8 +28,11 @@
*/
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 javax.validation.constraints.NotNull;
+import javax.validation.constraints.Null;
import java.util.*;
/**
@@ -38,32 +41,33 @@
public class Facility
{
// Internal system identifier
- @NotNull
- @Length( min = 11, max = 11 )
+ @Length( min = 11, max = 11, groups = { Create.class, Update.class } )
+ @NotNull( groups = { Update.class } )
+ @Null( groups = { Create.class } )
private String id;
// Name of the facility
- @NotNull
- @Length( min = 2, max = 160 )
+ @Length( min = 2, max = 160, groups = { Create.class, Update.class } )
+ @NotNull( groups = { Create.class, Update.class } )
private String name;
// Active = true/false indicates whether the facility is active or not
- @NotNull
+ @NotNull( groups = { Create.class, Update.class } )
private Boolean active;
// URL link to the unique ID API resource for the facility
private String url;
// ISO 8601 timestamp, including timezone, of when the facility was created
- @NotNull
+ @Null( groups = { Create.class, Update.class } )
private Date createdAt;
// ISO 8601 timestamp, including timezone, of when the facility was last updated
- @NotNull
+ @Null( groups = { Create.class, Update.class } )
private Date updatedAt;
// Geo-location represented by latitude and longitude coordinates in that order
- @NotNull
+ @NotNull( groups = { Create.class, Update.class } )
private List<Double> coordinates = new ArrayList<Double>();
// External Facility Identifiers
=== 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-07 16:00: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 07:58:59 +0000
@@ -56,6 +56,11 @@
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>();
=== added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/validationgroups'
=== added 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 1970-01-01 00:00:00 +0000
+++ 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
@@ -0,0 +1,8 @@
+package org.hisp.dhis.web.webapi.v1.utils.validationgroups;
+
+/**
+ * @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/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 1970-01-01 00:00:00 +0000
+++ 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
@@ -0,0 +1,8 @@
+package org.hisp.dhis.web.webapi.v1.utils.validationgroups;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface Update
+{
+}
=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/layout.vm'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/layout.vm 2012-12-07 20:42:02 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/layout.vm 2012-12-08 07:58:59 +0000
@@ -38,7 +38,7 @@
<div class="nav-collapse collapse">
<ul class="nav">
<li #if( $pageName == "home" )class="active"#end><a href="$baseUrl"><span class="icon-home"> </span> Home</a></li>
- <li #if( $pageName == "facilities" )class="active"#end><a href="$baseUrl/facilities"><span class="icon-search"> </span> Facilities</a></li>
+ <li #if( $pageName == "facilities" )class="active"#end><a href="$baseUrl/facilities"><span class="icon-leaf"> </span> Facilities</a></li>
</ul>
</div>
</div>