=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.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/controller/FacilityAdvice.java 2013-03-09 15:32:50 +0000 @@ -0,0 +1,93 @@ +package org.hisp.dhis.web.webapi.v1.controller; + +/* + * 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. + */ + +import org.hisp.dhis.common.DeleteNotAllowedException; +import org.hisp.dhis.hierarchy.HierarchyViolationException; +import org.hisp.dhis.web.webapi.v1.exception.DuplicateCodeException; +import org.hisp.dhis.web.webapi.v1.exception.DuplicateUidException; +import org.hisp.dhis.web.webapi.v1.exception.DuplicateUuidException; +import org.hisp.dhis.web.webapi.v1.exception.ETagVerificationException; +import org.hisp.dhis.web.webapi.v1.exception.FacilityNotFoundException; +import org.hisp.dhis.web.webapi.v1.exception.UuidFormatException; +import org.hisp.dhis.web.webapi.v1.utils.MessageUtils; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.HttpStatusCodeException; + +import java.io.IOException; + +/** + * @author Morten Olav Hansen + */ +@ControllerAdvice +public class FacilityAdvice +{ + //-------------------------------------------------------------------------- + // EXCEPTION HANDLERS + //-------------------------------------------------------------------------- + + @ExceptionHandler( { HttpClientErrorException.class, HttpServerErrorException.class } ) + public ResponseEntity statusCodeExceptionHandler( HttpStatusCodeException ex ) throws IOException + { + return new ResponseEntity( MessageUtils.jsonMessage( ex.getStatusText(), + ex.getMessage() ), ex.getStatusCode() ); + } + + @ExceptionHandler( { DeleteNotAllowedException.class, HierarchyViolationException.class } ) + public ResponseEntity handleForbidden( Exception ex ) throws IOException + { + return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.FORBIDDEN.toString(), + ex.getMessage() ), HttpStatus.FORBIDDEN ); + } + + @ExceptionHandler( { ETagVerificationException.class, UuidFormatException.class } ) + public ResponseEntity handlePreconditionFailed( Exception ex ) throws IOException + { + return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.PRECONDITION_FAILED.toString(), + ex.getMessage() ), HttpStatus.PRECONDITION_FAILED ); + } + + @ExceptionHandler( { FacilityNotFoundException.class } ) + public ResponseEntity handleNotFound( Exception ex ) throws IOException + { + return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.NOT_FOUND.toString(), + ex.getMessage() ), HttpStatus.NOT_FOUND ); + } + + @ExceptionHandler( { DuplicateCodeException.class, DuplicateUidException.class, DuplicateUuidException.class } ) + public ResponseEntity handleConflict( Exception ex ) throws IOException + { + return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(), + ex.getMessage() ), HttpStatus.CONFLICT ); + } +} === 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 09:38:51 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2013-03-09 15:32:50 +0000 @@ -30,7 +30,6 @@ import org.apache.commons.lang3.StringEscapeUtils; import org.codehaus.jackson.map.ObjectMapper; import org.hisp.dhis.api.controller.organisationunit.OrganisationUnitLevelController; -import org.hisp.dhis.common.DeleteNotAllowedException; import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.dataset.DataSetService; @@ -49,8 +48,6 @@ import org.hisp.dhis.web.webapi.v1.exception.FacilityNotFoundException; import org.hisp.dhis.web.webapi.v1.exception.UuidFormatException; import org.hisp.dhis.web.webapi.v1.utils.ContextUtils; -import org.hisp.dhis.web.webapi.v1.utils.MessageUtils; -import org.hisp.dhis.web.webapi.v1.utils.ObjectMapperFactoryBean; import org.hisp.dhis.web.webapi.v1.utils.ValidationUtils; import org.hisp.dhis.web.webapi.v1.validation.group.Create; import org.hisp.dhis.web.webapi.v1.validation.group.Update; @@ -65,7 +62,6 @@ import org.springframework.ui.Model; import org.springframework.util.DigestUtils; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -73,8 +69,6 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.client.HttpStatusCodeException; import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolation; @@ -610,7 +604,7 @@ facility = conversionService.convert( organisationUnit, Facility.class ); List organisationUnitLevels = organisationUnitService.getOrganisationUnitLevels(); addHierarchyPropertyToFacility( organisationUnitLevels, facility ); - json = new ObjectMapperFactoryBean().getObject().writeValueAsString( facility ); + json = objectMapper.writeValueAsString( facility ); return new ResponseEntity( json, headers, HttpStatus.OK ); } @@ -641,45 +635,6 @@ } //-------------------------------------------------------------------------- - // EXCEPTION HANDLERS - //-------------------------------------------------------------------------- - - @ExceptionHandler({ HttpClientErrorException.class, HttpServerErrorException.class }) - public ResponseEntity statusCodeExceptionHandler( HttpStatusCodeException ex ) throws IOException - { - return new ResponseEntity( MessageUtils.jsonMessage( ex.getStatusText(), - ex.getMessage() ), ex.getStatusCode() ); - } - - @ExceptionHandler( { DeleteNotAllowedException.class, HierarchyViolationException.class } ) - public ResponseEntity handleForbidden( Exception ex ) throws IOException - { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.FORBIDDEN.toString(), - ex.getMessage() ), HttpStatus.FORBIDDEN ); - } - - @ExceptionHandler( { ETagVerificationException.class, UuidFormatException.class } ) - public ResponseEntity handlePreconditionFailed( Exception ex ) throws IOException - { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.PRECONDITION_FAILED.toString(), - ex.getMessage() ), HttpStatus.PRECONDITION_FAILED ); - } - - @ExceptionHandler( { FacilityNotFoundException.class } ) - public ResponseEntity handleNotFound( Exception ex ) throws IOException - { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.NOT_FOUND.toString(), - ex.getMessage() ), HttpStatus.NOT_FOUND ); - } - - @ExceptionHandler( { DuplicateCodeException.class, DuplicateUidException.class, DuplicateUuidException.class } ) - public ResponseEntity handleConflict( Exception ex ) throws IOException - { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(), - ex.getMessage() ), HttpStatus.CONFLICT ); - } - - //-------------------------------------------------------------------------- // UTILS //-------------------------------------------------------------------------- === 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 2013-03-09 09:38:51 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityServiceController.java 2013-03-09 15:32:50 +0000 @@ -33,7 +33,10 @@ 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.MessageUtils; +import org.hisp.dhis.web.webapi.v1.exception.DuplicateCodeException; +import org.hisp.dhis.web.webapi.v1.exception.DuplicateUidException; +import org.hisp.dhis.web.webapi.v1.exception.DuplicateUuidException; +import org.hisp.dhis.web.webapi.v1.exception.FacilityNotFoundException; import org.hisp.dhis.web.webapi.v1.utils.ValidationUtils; import org.hisp.dhis.web.webapi.v1.validation.group.Create; import org.hisp.dhis.web.webapi.v1.validation.group.Update; @@ -66,9 +69,9 @@ /** * @author Morten Olav Hansen */ -@Controller( value = "facility-service-controller-" + FredController.PREFIX ) -@RequestMapping( FacilityServiceController.RESOURCE_PATH ) -@PreAuthorize( "hasRole('M_dhis-web-api-fred') or hasRole('ALL')" ) +@Controller(value = "facility-service-controller-" + FredController.PREFIX) +@RequestMapping(FacilityServiceController.RESOURCE_PATH) +@PreAuthorize("hasRole('M_dhis-web-api-fred') or hasRole('ALL')") public class FacilityServiceController { public static final String RESOURCE_PATH = "/" + FredController.PREFIX + "/facility-service"; @@ -83,49 +86,49 @@ private ConversionService conversionService; @Autowired - @Qualifier( "objectMapperFactoryBean" ) + @Qualifier("objectMapperFactoryBean") private ObjectMapper objectMapper; //-------------------------------------------------------------------------- // EXTRA WEB METHODS //-------------------------------------------------------------------------- - @RequestMapping( value = "/{id}/activate", method = RequestMethod.POST ) - @PreAuthorize( "hasRole('F_FRED_UPDATE') or hasRole('ALL')" ) - public ResponseEntity activateFacility( @PathVariable String id ) - { - OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id ); - - if ( organisationUnit != null ) - { - organisationUnit.setActive( true ); - organisationUnitService.updateOrganisationUnit( organisationUnit ); - - return new ResponseEntity( HttpStatus.OK ); - } - - return new ResponseEntity( HttpStatus.NOT_FOUND ); - } - - @RequestMapping( value = "/{id}/deactivate", method = RequestMethod.POST ) - @PreAuthorize( "hasRole('F_FRED_UPDATE') or hasRole('ALL')" ) - public ResponseEntity deactivateFacility( @PathVariable String id ) - { - OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id ); - - if ( organisationUnit != null ) - { - organisationUnit.setActive( false ); - organisationUnitService.updateOrganisationUnit( organisationUnit ); - - return new ResponseEntity( HttpStatus.OK ); - } - - return new ResponseEntity( HttpStatus.NOT_FOUND ); - } - - @RequestMapping( value = "/validate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE ) - public ResponseEntity validateFacilityForCreate( @RequestBody Facility facility ) throws IOException + @RequestMapping(value = "/{id}/activate", method = RequestMethod.POST) + @PreAuthorize("hasRole('F_FRED_UPDATE') or hasRole('ALL')") + public ResponseEntity activateFacility( @PathVariable String id ) throws FacilityNotFoundException + { + OrganisationUnit organisationUnit = getOrganisationUnit( id ); + + if ( organisationUnit == null ) + { + throw new FacilityNotFoundException(); + } + + organisationUnit.setActive( true ); + organisationUnitService.updateOrganisationUnit( organisationUnit ); + + return new ResponseEntity( HttpStatus.OK ); + } + + @RequestMapping(value = "/{id}/deactivate", method = RequestMethod.POST) + @PreAuthorize("hasRole('F_FRED_UPDATE') or hasRole('ALL')") + public ResponseEntity deactivateFacility( @PathVariable String id ) throws FacilityNotFoundException + { + OrganisationUnit organisationUnit = getOrganisationUnit( id ); + + if ( organisationUnit == null ) + { + throw new FacilityNotFoundException(); + } + + organisationUnit.setActive( false ); + organisationUnitService.updateOrganisationUnit( organisationUnit ); + + return new ResponseEntity( HttpStatus.OK ); + } + + @RequestMapping(value = "/validate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity validateFacilityForCreate( @RequestBody Facility facility ) throws Exception { Set> constraintViolations = validator.validate( facility, Default.class, Create.class ); @@ -138,20 +141,17 @@ { OrganisationUnit organisationUnit = conversionService.convert( facility, OrganisationUnit.class ); + if ( organisationUnitService.getOrganisationUnit( organisationUnit.getUuid() ) != null ) + { + throw new DuplicateUuidException(); + } if ( organisationUnitService.getOrganisationUnit( organisationUnit.getUid() ) != null ) { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(), - "An object with that ID already exists." ), headers, HttpStatus.CONFLICT ); - } - else if ( organisationUnitService.getOrganisationUnitByName( organisationUnit.getName() ) != null ) - { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(), - "An object with that name already exists." ), headers, HttpStatus.CONFLICT ); + throw new DuplicateUidException(); } else if ( organisationUnit.getCode() != null && organisationUnitService.getOrganisationUnitByCode( organisationUnit.getCode() ) != null ) { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(), - "An object with that code already exists." ), headers, HttpStatus.CONFLICT ); + throw new DuplicateCodeException(); } return new ResponseEntity( json, headers, HttpStatus.OK ); @@ -162,8 +162,8 @@ } } - @RequestMapping( value = "/validate", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE ) - public ResponseEntity validateFacilityForUpdate( @RequestBody Facility facility ) throws IOException + @RequestMapping(value = "/validate", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity validateFacilityForUpdate( @RequestBody Facility facility ) throws Exception { Set> constraintViolations = validator.validate( facility, Default.class, Update.class ); @@ -179,8 +179,7 @@ if ( ou == null ) { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.NOT_FOUND.toString(), - "No object with that identifier exists." ), headers, HttpStatus.NOT_FOUND ); + throw new FacilityNotFoundException(); } else if ( organisationUnit.getCode() != null ) { @@ -188,8 +187,7 @@ if ( ouByCode != null && !ou.getUid().equals( ouByCode.getUid() ) ) { - return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(), - "Another object with the same code already exists." ), headers, HttpStatus.CONFLICT ); + throw new DuplicateCodeException(); } } @@ -240,4 +238,25 @@ return new ResponseEntity( json, headers, HttpStatus.OK ); } + + //-------------------------------------------------------------------------- + // UTILS + //-------------------------------------------------------------------------- + + private OrganisationUnit getOrganisationUnit( String id ) + { + OrganisationUnit organisationUnit; + + if ( id.length() == 11 ) + { + organisationUnit = organisationUnitService.getOrganisationUnit( id ); + } + else + { + organisationUnit = organisationUnitService.getOrganisationUnitByUuid( id ); + } + + return organisationUnit; + } } +