← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16921: support for adding associations in collection in web-api, use by sending POST or PUT request to /...

 

------------------------------------------------------------
revno: 16921
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-10-02 12:38:02 +0700
message:
  support for adding associations in collection in web-api, use by sending POST or PUT request to /api/type/id/idCollection/itemId, only owner side can be updated for now.
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-10-02 04:12:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2014-10-02 05:38:02 +0000
@@ -471,6 +471,67 @@
         manager.delete( objects.get( 0 ) );
     }
 
+    //--------------------------------------------------------------------------
+    // Identifiable object collections add, delete
+    //--------------------------------------------------------------------------
+
+    @RequestMapping( value = "/{uid}/{property}/{itemId}", method = { RequestMethod.POST, RequestMethod.PUT } )
+    @SuppressWarnings( "unchecked" )
+    public void addCollectionItem(
+        @PathVariable( "uid" ) String pvUid,
+        @PathVariable( "property" ) String pvProperty,
+        @PathVariable( "itemId" ) String pvItemId, HttpServletResponse response ) throws Exception
+    {
+        List<T> objects = getEntity( pvUid );
+
+        if ( objects.isEmpty() )
+        {
+            ContextUtils.notFoundResponse( response, getEntityName() + " does not exist: " + pvUid );
+        }
+
+        if ( !getSchema().getPropertyMap().containsKey( pvProperty ) )
+        {
+            ContextUtils.notFoundResponse( response, "Property " + pvProperty + " does not exist on " + getEntityName() );
+        }
+
+        Property property = getSchema().getPropertyMap().get( pvProperty );
+
+        if ( !property.isCollection() || !property.isIdentifiableObject() )
+        {
+            ContextUtils.conflictResponse( response, "Only adds within identifiable collection are allowed." );
+        }
+
+        if ( !property.isOwner() )
+        {
+            ContextUtils.conflictResponse( response, getEntityName() + " is not the owner of this relationship." );
+        }
+
+        Collection<IdentifiableObject> identifiableObjects =
+            (Collection<IdentifiableObject>) property.getGetterMethod().invoke( objects.get( 0 ) );
+
+        IdentifiableObject candidate = manager.getNoAcl( (Class<? extends IdentifiableObject>) property.getItemKlass(), pvItemId );
+
+        if ( candidate == null )
+        {
+            ContextUtils.notFoundResponse( response, "Collection " + pvProperty + " does not have an item with ID: " + pvItemId );
+        }
+
+        // if it already contains this object, don't add it. It might be a list and not set, and we don't want duplicates.
+        if ( identifiableObjects.contains( candidate ) )
+        {
+            return; // nothing to do, just return with OK
+        }
+
+        identifiableObjects.add( candidate );
+
+        if ( !aclService.canUpdate( currentUserService.getCurrentUser(), objects.get( 0 ) ) )
+        {
+            throw new DeleteAccessDeniedException( "You don't have the proper permissions to delete this object." );
+        }
+
+        manager.update( objects.get( 0 ) );
+    }
+
     @RequestMapping( value = "/{uid}/{property}/{itemId}", method = RequestMethod.DELETE )
     @SuppressWarnings( "unchecked" )
     public void deleteCollectionItem(
@@ -657,6 +718,21 @@
         }
     }
 
+    private InclusionStrategy.Include getInclusionStrategy( String inclusionStrategy )
+    {
+        if ( inclusionStrategy != null )
+        {
+            Optional<InclusionStrategy.Include> optional = Enums.getIfPresent( InclusionStrategy.Include.class, inclusionStrategy );
+
+            if ( optional.isPresent() )
+            {
+                return optional.get();
+            }
+        }
+
+        return InclusionStrategy.Include.NON_NULL;
+    }
+
     //--------------------------------------------------------------------------
     // Reflection helpers
     //--------------------------------------------------------------------------
@@ -711,19 +787,4 @@
             throw new RuntimeException( ex );
         }
     }
-
-    private InclusionStrategy.Include getInclusionStrategy( String inclusionStrategy )
-    {
-        if ( inclusionStrategy != null )
-        {
-            Optional<InclusionStrategy.Include> optional = Enums.getIfPresent( InclusionStrategy.Include.class, inclusionStrategy );
-
-            if ( optional.isPresent() )
-            {
-                return optional.get();
-            }
-        }
-
-        return InclusionStrategy.Include.NON_NULL;
-    }
 }