=== 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 2013-03-09 08:34:50 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2013-03-09 08:58:06 +0000 @@ -42,6 +42,7 @@ import org.hisp.dhis.web.webapi.v1.domain.Facilities; import org.hisp.dhis.web.webapi.v1.domain.Facility; import org.hisp.dhis.web.webapi.v1.domain.Identifier; +import org.hisp.dhis.web.webapi.v1.exception.ETagDoesNotMatchException; import org.hisp.dhis.web.webapi.v1.utils.ContextUtils; import org.hisp.dhis.web.webapi.v1.utils.MessageResponseUtils; import org.hisp.dhis.web.webapi.v1.utils.ObjectMapperFactoryBean; @@ -93,9 +94,9 @@ /** * @author Morten Olav Hansen */ -@Controller( value = "facility-controller-" + FredController.PREFIX ) -@RequestMapping( FacilityController.RESOURCE_PATH ) -@PreAuthorize( "hasRole('M_dhis-web-api-fred') or hasRole('ALL')" ) +@Controller(value = "facility-controller-" + FredController.PREFIX) +@RequestMapping(FacilityController.RESOURCE_PATH) +@PreAuthorize("hasRole('M_dhis-web-api-fred') or hasRole('ALL')") public class FacilityController { public static final String RESOURCE_PATH = "/" + FredController.PREFIX + "/facilities"; @@ -244,13 +245,13 @@ return facility; } - @RequestMapping( value = "", method = RequestMethod.GET ) - public String readFacilities( Model model, @RequestParam( required = false ) Boolean active, - @RequestParam( value = "updatedSince", required = false ) Date lastUpdated, - @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties, - @RequestParam( value = "fields", required = false ) String fields, - @RequestParam( value = "limit", required = false, defaultValue = "25" ) String limit, - @RequestParam( value = "offset", required = false, defaultValue = "0" ) Integer offset, + @RequestMapping(value = "", method = RequestMethod.GET) + public String readFacilities( Model model, @RequestParam(required = false) Boolean active, + @RequestParam(value = "updatedSince", required = false) Date lastUpdated, + @RequestParam(value = "allProperties", required = false, defaultValue = "true") Boolean allProperties, + @RequestParam(value = "fields", required = false) String fields, + @RequestParam(value = "limit", required = false, defaultValue = "25") String limit, + @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset, HttpServletRequest request ) { Facilities facilities = new Facilities(); @@ -331,7 +332,7 @@ { Facility facility = conversionService.convert( organisationUnit, Facility.class ); filterFacility( facility, allProperties, fields ); - addHierarchyPropertyToFacility( organisationUnitLevels, organisationUnit, facility ); + addHierarchyPropertyToFacility( organisationUnitLevels, facility ); facilities.getFacilities().add( facility ); } @@ -350,7 +351,7 @@ model.addAttribute( "prevDisabled", true ); } - if ( (offset + limitValue) >= organisationUnitService.getNumberOfOrganisationUnits() ) + if ( (offset + (limitValue == null ? 0 : limitValue) >= organisationUnitService.getNumberOfOrganisationUnits()) ) { model.addAttribute( "nextDisabled", true ); } @@ -358,10 +359,10 @@ return FredController.PREFIX + "/layout"; } - @RequestMapping( value = "/{id}", method = RequestMethod.GET ) + @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String readFacility( Model model, @PathVariable String id, - @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties, - @RequestParam( value = "fields", required = false ) String fields, + @RequestParam(value = "allProperties", required = false, defaultValue = "true") Boolean allProperties, + @RequestParam(value = "fields", required = false) String fields, HttpServletRequest request ) { OrganisationUnit organisationUnit = getOrganisationUnit( id ); @@ -375,7 +376,7 @@ Facility facility = conversionService.convert( organisationUnit, Facility.class ); filterFacility( facility, allProperties, fields ); - addHierarchyPropertyToFacility( organisationUnitLevels, organisationUnit, facility ); + addHierarchyPropertyToFacility( organisationUnitLevels, facility ); setAccessRights( model ); @@ -416,7 +417,7 @@ } } - private void addHierarchyPropertyToFacility( List organisationUnitLevels, OrganisationUnit organisationUnit, Facility facility ) + private void addHierarchyPropertyToFacility( List organisationUnitLevels, Facility facility ) { if ( facility.getProperties() == null ) { @@ -448,8 +449,8 @@ // POST JSON //-------------------------------------------------------------------------- - @RequestMapping( value = "", method = RequestMethod.POST ) - @PreAuthorize( "hasRole('F_FRED_CREATE') or hasRole('ALL')" ) + @RequestMapping(value = "", method = RequestMethod.POST) + @PreAuthorize("hasRole('F_FRED_CREATE') or hasRole('ALL')") public ResponseEntity createFacility( @RequestBody Facility facility ) throws Exception { if ( facility.getUuid() == null ) @@ -510,7 +511,7 @@ facility = conversionService.convert( organisationUnit, Facility.class ); List organisationUnitLevels = organisationUnitService.getOrganisationUnitLevels(); - addHierarchyPropertyToFacility( organisationUnitLevels, organisationUnit, facility ); + addHierarchyPropertyToFacility( organisationUnitLevels, facility ); json = objectMapper.writeValueAsString( facility ); return new ResponseEntity( json, headers, HttpStatus.CREATED ); @@ -525,8 +526,8 @@ // PUT JSON //-------------------------------------------------------------------------- - @RequestMapping( value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE ) - @PreAuthorize( "hasRole('F_FRED_UPDATE') or hasRole('ALL')" ) + @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) + @PreAuthorize("hasRole('F_FRED_UPDATE') or hasRole('ALL')") public ResponseEntity updateFacility( @PathVariable String id, @RequestBody Facility facility, HttpServletRequest request ) throws Exception { HttpHeaders headers = new HttpHeaders(); @@ -569,15 +570,14 @@ { Facility old_facility = conversionService.convert( organisationUnit, Facility.class ); List organisationUnitLevels = organisationUnitService.getOrganisationUnitLevels(); - addHierarchyPropertyToFacility( organisationUnitLevels, organisationUnitUpdate, old_facility ); + addHierarchyPropertyToFacility( organisationUnitLevels, old_facility ); String body = objectMapper.writeValueAsString( old_facility ); String ETag = generateETagHeaderValue( body.getBytes() ); if ( !ETag.equals( request.getHeader( "If-Match" ) ) ) { - return new ResponseEntity( MessageResponseUtils.jsonMessage( HttpStatus.PRECONDITION_FAILED.toString(), - "ETag provided does not match current ETag of facility" ), headers, HttpStatus.PRECONDITION_FAILED ); + throw new ETagDoesNotMatchException(); } } @@ -611,7 +611,7 @@ facility = conversionService.convert( organisationUnit, Facility.class ); List organisationUnitLevels = organisationUnitService.getOrganisationUnitLevels(); - addHierarchyPropertyToFacility( organisationUnitLevels, organisationUnit, facility ); + addHierarchyPropertyToFacility( organisationUnitLevels, facility ); json = new ObjectMapperFactoryBean().getObject().writeValueAsString( facility ); return new ResponseEntity( json, headers, HttpStatus.OK ); @@ -626,8 +626,8 @@ // DELETE JSON //-------------------------------------------------------------------------- - @RequestMapping( value = "/{id}", method = RequestMethod.DELETE ) - @PreAuthorize( "hasRole('F_FRED_DELETE') or hasRole('ALL')" ) + @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) + @PreAuthorize("hasRole('F_FRED_DELETE') or hasRole('ALL')") public ResponseEntity deleteFacility( @PathVariable String id ) throws HierarchyViolationException, IOException { OrganisationUnit organisationUnit = getOrganisationUnit( id ); @@ -647,18 +647,24 @@ // EXCEPTION HANDLERS //-------------------------------------------------------------------------- - @ExceptionHandler( { HttpClientErrorException.class, HttpServerErrorException.class } ) + @ExceptionHandler({ HttpClientErrorException.class, HttpServerErrorException.class }) public ResponseEntity statusCodeExceptionHandler( HttpStatusCodeException ex ) { return new ResponseEntity( ex.getMessage(), ex.getStatusCode() ); } - @ExceptionHandler( { DeleteNotAllowedException.class, HierarchyViolationException.class } ) + @ExceptionHandler({ DeleteNotAllowedException.class, HierarchyViolationException.class }) public ResponseEntity dhisExceptionHandler( Exception ex ) { return new ResponseEntity( ex.getMessage(), HttpStatus.FORBIDDEN ); } + public ResponseEntity etagVerificationFailed( ETagDoesNotMatchException ex ) throws IOException + { + return new ResponseEntity( MessageResponseUtils.jsonMessage( HttpStatus.PRECONDITION_FAILED.toString(), + "ETag provided does not match current ETag of facility" ), HttpStatus.PRECONDITION_FAILED ); + } + //-------------------------------------------------------------------------- // UTILS //-------------------------------------------------------------------------- === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/exception' === added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/exception/ETagDoesNotMatchException.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/exception/ETagDoesNotMatchException.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/exception/ETagDoesNotMatchException.java 2013-03-09 08:58:06 +0000 @@ -0,0 +1,35 @@ +package org.hisp.dhis.web.webapi.v1.exception; + +/* + * Copyright (c) 2004-2013, 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. + */ + +/** + * @author Morten Olav Hansen + */ +public class ETagDoesNotMatchException extends Exception +{ +}