=== 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 14:16:37 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2012-12-07 16:00:59 +0000 @@ -27,6 +27,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.hisp.dhis.common.DeleteNotAllowedException; import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.hierarchy.HierarchyViolationException; @@ -37,11 +38,11 @@ import org.hisp.dhis.web.webapi.v1.utils.GeoUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.hateoas.mvc.ControllerLinkBuilder; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -198,11 +199,9 @@ // EXCEPTION HANDLERS //-------------------------------------------------------------------------- -/* - @ExceptionHandler( Exception.class ) + @ExceptionHandler( { DeleteNotAllowedException.class, HierarchyViolationException.class } ) public ResponseEntity exceptionHandler( Exception ex ) { return new ResponseEntity( ex.getMessage(), HttpStatus.FORBIDDEN ); } -*/ } === 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 13:04:36 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java 2012-12-07 16:00:59 +0000 @@ -27,35 +27,44 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.codehaus.jackson.map.ObjectMapper; import org.hisp.dhis.organisationunit.OrganisationUnit; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import javax.validation.ConstraintViolation; +import java.io.IOException; +import java.util.Set; + /** * @author Morten Olav Hansen */ -@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 activateFacility( @PathVariable String id ) { OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id ); @@ -71,7 +80,7 @@ return new ResponseEntity( HttpStatus.NOT_FOUND ); } - @RequestMapping( value = "/{id}/deactivate", method = RequestMethod.POST ) + @RequestMapping(value = "/{id}/deactivate", method = RequestMethod.POST) public ResponseEntity deactivateFacility( @PathVariable String id ) { OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id ); @@ -86,4 +95,21 @@ return new ResponseEntity( HttpStatus.NOT_FOUND ); } + + @RequestMapping( value = "/validate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE ) + public ResponseEntity validateFacility( @RequestBody Facility facility ) throws IOException + { + Set> constraintViolations = ValidationUtils.validate( facility ); + + String json = ValidationUtils.constraintViolationsToJson( constraintViolations ); + + if ( constraintViolations.isEmpty() ) + { + return new ResponseEntity( json, HttpStatus.OK ); + } + else + { + return new ResponseEntity( json, HttpStatus.NOT_ACCEPTABLE ); + } + } } === 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-06 20:06:23 +0000 +++ 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 @@ -27,6 +27,9 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotNull; import java.util.*; /** @@ -35,24 +38,32 @@ public class Facility { // Internal system identifier + @NotNull + @Length( min = 11, max = 11 ) private String id; // Name of the facility + @NotNull + @Length( min = 2, max = 160 ) private String name; // Active = true/false indicates whether the facility is active or not + @NotNull 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 private Date createdAt; // ISO 8601 timestamp, including timezone, of when the facility was last updated + @NotNull private Date updatedAt; // Geo-location represented by latitude and longitude coordinates in that order + @NotNull private List coordinates = new ArrayList(); // External Facility Identifiers @@ -154,4 +165,20 @@ { this.properties = properties; } + + @Override + public String toString() + { + return "Facility{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", active=" + active + + ", url='" + url + '\'' + + ", createdAt=" + createdAt + + ", updatedAt=" + updatedAt + + ", coordinates=" + coordinates + + ", identifiers=" + identifiers + + ", properties=" + properties + + '}'; + } } === added 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 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/ValidationUtils.java 2012-12-07 16:00:59 +0000 @@ -0,0 +1,70 @@ +package org.hisp.dhis.web.webapi.v1.utils; + +/* + * 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.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; +import java.util.Set; + +/** + * @author Morten Olav Hansen + */ +public class ValidationUtils +{ + private static Validator validator; + + static + { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + validator = factory.getValidator(); + } + + public static Set> validate( T object ) + { + return validator.validate( object ); + } + + public static String constraintViolationsToJson( Set> constraintViolations ) throws IOException + { + Map constraintViolationsMap = new HashMap(); + + for ( ConstraintViolation constraintViolation : constraintViolations ) + { + constraintViolationsMap.put( constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage() ); + } + + return new ObjectMapper().writeValueAsString( constraintViolationsMap ); + } +} === 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 13:04:36 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml 2012-12-07 16:00:59 +0000 @@ -2,11 +2,15 @@ + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd + http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> + +