← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9757: FRED-API: check that UUID is in expected format. Allow duplicate names when creating.

 

------------------------------------------------------------
revno: 9757
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-02-07 09:14:21 +0700
message:
  FRED-API: check that UUID is in expected format. Allow duplicate names when creating.
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/domain/Facility.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/FacilityToOrganisationUnitConverter.java


--
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	2013-02-05 06:51:24 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2013-02-07 02:14:21 +0000
@@ -72,6 +72,7 @@
 import javax.validation.Validator;
 import javax.validation.groups.Default;
 import java.beans.PropertyEditorSupport;
+import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -82,15 +83,16 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.UUID;
 
 import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-@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";
@@ -236,13 +238,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 ) Integer 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) Integer limit,
+        @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
         HttpServletRequest request )
     {
         Facilities facilities = new Facilities();
@@ -319,10 +321,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 = organisationUnitService.getOrganisationUnit( id );
@@ -398,10 +400,15 @@
     // 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<String> createFacility( @RequestBody Facility facility ) throws Exception
     {
+        if ( facility.getId() == null )
+        {
+            facility.setId( UUID.randomUUID().toString() );
+        }
+
         Set<ConstraintViolation<Facility>> constraintViolations = validator.validate( facility, Default.class, Create.class );
 
         String json = ValidationUtils.constraintViolationsToJson( constraintViolations );
@@ -421,10 +428,6 @@
             {
                 return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that UID already exists." ), headers, HttpStatus.CONFLICT );
             }
-            else if ( organisationUnitService.getOrganisationUnitByName( organisationUnit.getName() ) != null )
-            {
-                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that name already exists." ), headers, HttpStatus.CONFLICT );
-            }
             else if ( organisationUnit.getCode() != null && organisationUnitService.getOrganisationUnitByCode( organisationUnit.getCode() ) != null )
             {
                 return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that code already exists." ), headers, HttpStatus.CONFLICT );
@@ -465,7 +468,7 @@
         return builder.toString();
     }
 
-    protected void checkUidIdentifier( Facility facility, String id )
+    protected void checkUidIdentifier( Facility facility, String id ) throws IOException
     {
         Identifier identifier = new Identifier();
 
@@ -501,14 +504,30 @@
     @PreAuthorize( "hasRole('F_FRED_UPDATE') or hasRole('ALL')" )
     public ResponseEntity<String> updateFacility( @PathVariable String id, @RequestBody Facility facility, HttpServletRequest request ) throws Exception
     {
+        HttpHeaders headers = new HttpHeaders();
+        headers.add( "Content-Type", MediaType.APPLICATION_JSON_VALUE );
+
+        // getId == null is not legal, but will be catched by bean validation
+        if ( facility.getId() != null )
+        {
+            String uuid = facility.getId();
+
+            try
+            {
+                UUID.fromString( uuid );
+            }
+            catch ( IllegalArgumentException ignored )
+            {
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "Bad id: (does not match expected UUID string format)" ),
+                    headers, HttpStatus.PRECONDITION_FAILED );
+            }
+        }
+
         checkUidIdentifier( facility, id );
         Set<ConstraintViolation<Facility>> constraintViolations = validator.validate( facility, Default.class, Update.class );
 
         String json = ValidationUtils.constraintViolationsToJson( constraintViolations );
 
-        HttpHeaders headers = new HttpHeaders();
-        headers.add( "Content-Type", MediaType.APPLICATION_JSON_VALUE );
-
         if ( constraintViolations.isEmpty() )
         {
             OrganisationUnit organisationUnitUpdate = conversionService.convert( facility, OrganisationUnit.class );

=== 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	2013-02-06 13:03:13 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2013-02-07 02:14:21 +0000
@@ -46,7 +46,6 @@
 public class Facility
 {
     // Internal system identifier
-    @Null(groups = Create.class)
     @NotNull(groups = Update.class)
     @Length(min = 36, max = 36)
     private String id;

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/FacilityToOrganisationUnitConverter.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/FacilityToOrganisationUnitConverter.java	2013-02-04 18:03:10 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/FacilityToOrganisationUnitConverter.java	2013-02-07 02:14:21 +0000
@@ -58,8 +58,6 @@
     public OrganisationUnit convert( Facility facility )
     {
         OrganisationUnit organisationUnit = new OrganisationUnit();
-
-        organisationUnit.setUid( facility.getId() );
         organisationUnit.setName( facility.getName() );
 
         if ( facility.getName() != null && facility.getName().length() > 49 )