← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17842: switched to using importer in property updates for now, not ideal but allows for updating propert...

 

------------------------------------------------------------
revno: 17842
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-12-30 17:05:57 +0100
message:
  switched to using importer in property updates for now, not ideal but allows for updating properties with full references, adds utility method in AbstractCrudController called serialize which tries to guess the format you want returned, and then serializes the object
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.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/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2014-12-30 14:09:22 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2014-12-30 16:05:57 +0000
@@ -314,12 +314,6 @@
             throw new UpdateAccessDeniedException( "This property is read-only." );
         }
 
-        if ( !property.isSimple() )
-        {
-            // TODO we need to split out the reference scanner from the importer for this to work with idObjects
-            throw new UpdateAccessDeniedException( "Only simple properties are currently supported for update." );
-        }
-
         T object = deserialize( request );
 
         if ( object == null )
@@ -332,8 +326,8 @@
 
         property.getSetterMethod().invoke( persistedObject, value );
 
-        response.setStatus( HttpServletResponse.SC_NO_CONTENT );
-        manager.update( persistedObject );
+        ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), persistedObject, ImportStrategy.UPDATE );
+        serialize( request, response, summary );
     }
 
     private RootNode getObjectInternal( String uid, Map<String, String> parameters,
@@ -875,6 +869,39 @@
     }
 
     /**
+     * Serializes an object, tries to guess output format using this order.
+     *
+     * @param request  HttpServletRequest from current session
+     * @param response HttpServletResponse from current session
+     * @param object   Object to serialize
+     */
+    protected void serialize( HttpServletRequest request, HttpServletResponse response, Object object ) throws IOException
+    {
+        String type = request.getHeader( "Accept" );
+        type = !StringUtils.isEmpty( type ) ? type : request.getContentType();
+        type = !StringUtils.isEmpty( type ) ? type : MediaType.APPLICATION_JSON_VALUE;
+
+        // allow type to be overridden by path extension
+        if ( request.getPathInfo().endsWith( ".json" ) )
+        {
+            type = MediaType.APPLICATION_JSON_VALUE;
+        }
+        else if ( request.getPathInfo().endsWith( ".xml" ) )
+        {
+            type = MediaType.APPLICATION_XML_VALUE;
+        }
+
+        if ( isCompatibleWith( type, MediaType.APPLICATION_JSON ) )
+        {
+            renderService.toJson( response.getOutputStream(), object );
+        }
+        else if ( isCompatibleWith( type, MediaType.APPLICATION_XML ) )
+        {
+            renderService.toXml( response.getOutputStream(), object );
+        }
+    }
+
+    /**
      * Deserializes a payload from the request, handles JSON/XML payloads
      *
      * @param request HttpServletRequest from current session
@@ -882,11 +909,24 @@
      */
     protected T deserialize( HttpServletRequest request ) throws IOException
     {
-        if ( isJson( request ) )
+        String type = request.getContentType();
+        type = !StringUtils.isEmpty( type ) ? type : MediaType.APPLICATION_JSON_VALUE;
+
+        // allow type to be overridden by path extension
+        if ( request.getPathInfo().endsWith( ".json" ) )
+        {
+            type = MediaType.APPLICATION_JSON_VALUE;
+        }
+        else if ( request.getPathInfo().endsWith( ".xml" ) )
+        {
+            type = MediaType.APPLICATION_XML_VALUE;
+        }
+
+        if ( isCompatibleWith( type, MediaType.APPLICATION_JSON ) )
         {
             return renderService.fromJson( request.getInputStream(), getEntityClass() );
         }
-        else if ( isXml( request ) )
+        else if ( isCompatibleWith( type, MediaType.APPLICATION_XML ) )
         {
             return renderService.fromXml( request.getInputStream(), getEntityClass() );
         }
@@ -902,8 +942,7 @@
      */
     protected boolean isJson( HttpServletRequest request )
     {
-        String contentType = request.getContentType();
-        return !StringUtils.isEmpty( contentType ) && MediaType.parseMediaType( contentType ).isCompatibleWith( MediaType.APPLICATION_JSON );
+        return isCompatibleWith( request.getContentType(), MediaType.APPLICATION_JSON );
     }
 
     /**
@@ -914,8 +953,12 @@
      */
     protected boolean isXml( HttpServletRequest request )
     {
-        String contentType = request.getContentType();
-        return !StringUtils.isEmpty( contentType ) && MediaType.parseMediaType( contentType ).isCompatibleWith( MediaType.APPLICATION_XML );
+        return isCompatibleWith( request.getContentType(), MediaType.APPLICATION_XML );
+    }
+
+    protected boolean isCompatibleWith( String type, MediaType mediaType )
+    {
+        return !StringUtils.isEmpty( type ) && MediaType.parseMediaType( type ).isCompatibleWith( mediaType );
     }
 
     //--------------------------------------------------------------------------