← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9266: FRED-API: defer ID check so that the correct http response can be sent back. Added MessageRespons...

 

------------------------------------------------------------
revno: 9266
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-12-11 13:59:00 +0300
message:
  FRED-API: defer ID check so that the correct http response can be sent back. Added MessageResponse classes and utils.
added:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/MessageResponse.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageResponseUtils.java
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/ObjectMapperFactoryBean.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	2012-12-11 09:10:56 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2012-12-11 10:59:00 +0000
@@ -38,6 +38,7 @@
 import org.hisp.dhis.user.CurrentUserService;
 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.utils.MessageResponseUtils;
 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;
@@ -133,7 +134,7 @@
         @RequestParam( value = "updatedSince", required = false ) Date lastUpdated )
     {
         Facilities facilities = new Facilities();
-        List<OrganisationUnit> allOrganisationUnits = null;
+        List<OrganisationUnit> allOrganisationUnits;
 
         if ( active == null && lastUpdated == null )
         {
@@ -212,8 +213,16 @@
 
         String json = ValidationUtils.constraintViolationsToJson( constraintViolations );
 
+        HttpHeaders headers = new HttpHeaders();
+        headers.add( "Content-Type", MediaType.APPLICATION_JSON_VALUE );
+
         if ( constraintViolations.isEmpty() )
         {
+            if ( organisationUnitService.getOrganisationUnit( facility.getId() ) != null )
+            {
+                return new ResponseEntity<String>( MessageResponseUtils.jsonMessage( "An object with that ID already exists." ), headers, HttpStatus.CONFLICT );
+            }
+
             OrganisationUnit organisationUnit = conversionService.convert( facility, OrganisationUnit.class );
             organisationUnitService.addOrganisationUnit( organisationUnit );
 
@@ -223,14 +232,13 @@
                 dataSetService.updateDataSet( dataSet );
             }
 
-            HttpHeaders headers = new HttpHeaders();
             headers.setLocation( linkTo( FacilityController.class ).slash( organisationUnit.getUid() ).toUri() );
 
             return new ResponseEntity<String>( json, headers, HttpStatus.CREATED );
         }
         else
         {
-            return new ResponseEntity<String>( json, HttpStatus.UNPROCESSABLE_ENTITY );
+            return new ResponseEntity<String>( json, headers, HttpStatus.UNPROCESSABLE_ENTITY );
         }
     }
 
@@ -249,6 +257,9 @@
 
         String json = ValidationUtils.constraintViolationsToJson( constraintViolations );
 
+        HttpHeaders headers = new HttpHeaders();
+        headers.add( "Content-Type", MediaType.APPLICATION_JSON_VALUE );
+
         if ( constraintViolations.isEmpty() )
         {
             OrganisationUnit ou = organisationUnitService.getOrganisationUnit( facility.getId() );
@@ -265,11 +276,11 @@
 
             organisationUnitService.updateOrganisationUnit( ou );
 
-            return new ResponseEntity<String>( json, HttpStatus.OK );
+            return new ResponseEntity<String>( json, headers, HttpStatus.OK );
         }
         else
         {
-            return new ResponseEntity<String>( json, HttpStatus.UNPROCESSABLE_ENTITY );
+            return new ResponseEntity<String>( json, headers, HttpStatus.UNPROCESSABLE_ENTITY );
         }
     }
 

=== 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-10 12:38:04 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2012-12-11 10:59:00 +0000
@@ -28,10 +28,8 @@
  */
 
 import org.hibernate.validator.constraints.Length;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.ValidIdentifiers;
 import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.ValidProperties;
-import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.ValidUidReference;
 import org.hisp.dhis.web.webapi.v1.validation.group.Create;
 import org.hisp.dhis.web.webapi.v1.validation.group.Update;
 
@@ -45,8 +43,9 @@
 public class Facility
 {
     // Internal system identifier
+    @NotNull( groups = Create.class )
+    @Null( groups = Update.class )
     @Length( min = 11, max = 11 )
-    @ValidUidReference( value = OrganisationUnit.class, groups = Update.class )
     private String id;
 
     // Name of the facility

=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/MessageResponse.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/MessageResponse.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/domain/MessageResponse.java	2012-12-11 10:59:00 +0000
@@ -0,0 +1,71 @@
+package org.hisp.dhis.web.webapi.v1.domain;
+
+/*
+ * 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.annotate.JsonPropertyOrder;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@JsonPropertyOrder( value = { "message", "moreInfo" } )
+public class MessageResponse
+{
+    private String message;
+
+    private String moreInfo;
+
+    public MessageResponse()
+    {
+    }
+
+    public MessageResponse( String message, String moreInfo )
+    {
+        this.message = message;
+        this.moreInfo = moreInfo;
+    }
+
+    public String getMessage()
+    {
+        return message;
+    }
+
+    public void setMessage( String message )
+    {
+        this.message = message;
+    }
+
+    public String getMoreInfo()
+    {
+        return moreInfo;
+    }
+
+    public void setMoreInfo( String moreInfo )
+    {
+        this.moreInfo = moreInfo;
+    }
+}

=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageResponseUtils.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageResponseUtils.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/MessageResponseUtils.java	2012-12-11 10:59:00 +0000
@@ -0,0 +1,65 @@
+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.JsonGenerator;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.hisp.dhis.web.webapi.v1.domain.MessageResponse;
+
+import java.io.IOException;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class MessageResponseUtils
+{
+    private static ObjectMapper objectMapper;
+
+    static
+    {
+        objectMapper = new ObjectMapper();
+        objectMapper.configure( JsonGenerator.Feature.ESCAPE_NON_ASCII, true );
+        objectMapper.setSerializationInclusion( JsonSerialize.Inclusion.NON_NULL );
+    }
+
+    public static String jsonMessage( String message ) throws IOException
+    {
+        return messageToJson( new MessageResponse( message, null ) );
+    }
+
+    public static String jsonMessage( String message, String moreInfo ) throws IOException
+    {
+        return messageToJson( new MessageResponse( message, moreInfo ) );
+    }
+
+    public static String messageToJson( MessageResponse messageResponse ) throws IOException
+    {
+        return objectMapper.writeValueAsString( messageResponse );
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ObjectMapperFactoryBean.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ObjectMapperFactoryBean.java	2012-12-09 13:36:18 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ObjectMapperFactoryBean.java	2012-12-11 10:59:00 +0000
@@ -46,7 +46,7 @@
         ObjectMapper objectMapper = new ObjectMapper();
         objectMapper.configure( JsonGenerator.Feature.ESCAPE_NON_ASCII, true );
         objectMapper.configure( SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false );
-        objectMapper.setSerializationInclusion( JsonSerialize.Inclusion.NON_EMPTY );
+        objectMapper.setSerializationInclusion( JsonSerialize.Inclusion.NON_NULL );
 
         return objectMapper;
     }