← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16920: support for deleting associations in collection in web-api, use by sending DELETE request to /api...

 

------------------------------------------------------------
revno: 16920
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-10-02 11:12:18 +0700
message:
  support for deleting associations in collection in web-api, use by sending DELETE 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-01 08:39:12 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2014-10-02 04:12:18 +0000
@@ -54,6 +54,7 @@
 import org.hisp.dhis.node.types.ComplexNode;
 import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.node.types.SimpleNode;
+import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.user.CurrentUserService;
@@ -79,6 +80,7 @@
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -248,13 +250,6 @@
         return rootNode;
     }
 
-    @RequestMapping( value = "/{uid}/{property}", method = RequestMethod.GET )
-    public @ResponseBody RootNode getObjectProperty( @PathVariable( "uid" ) String uid, @PathVariable( "property" ) String property,
-        @RequestParam Map<String, String> parameters, HttpServletRequest request, HttpServletResponse response ) throws Exception
-    {
-        return getObjectInternal( uid, parameters, Lists.<String>newArrayList(), Lists.newArrayList( property ) );
-    }
-
     @RequestMapping( value = "/{uid}", method = RequestMethod.GET )
     public @ResponseBody RootNode getObject( @PathVariable( "uid" ) String uid, @RequestParam Map<String, String> parameters,
         HttpServletRequest request, HttpServletResponse response ) throws Exception
@@ -270,6 +265,13 @@
         return getObjectInternal( uid, parameters, filters, fields );
     }
 
+    @RequestMapping( value = "/{uid}/{property}", method = RequestMethod.GET )
+    public @ResponseBody RootNode getObjectProperty( @PathVariable( "uid" ) String uid, @PathVariable( "property" ) String property,
+        @RequestParam Map<String, String> parameters, HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        return getObjectInternal( uid, parameters, Lists.<String>newArrayList(), Lists.newArrayList( property ) );
+    }
+
     private RootNode getObjectInternal( String uid, Map<String, String> parameters,
         List<String> filters, List<String> fields ) throws Exception
     {
@@ -391,7 +393,7 @@
 
         if ( objects.isEmpty() )
         {
-            ContextUtils.conflictResponse( response, getEntityName() + " does not exist: " + uid );
+            ContextUtils.notFoundResponse( response, getEntityName() + " does not exist: " + uid );
             return;
         }
 
@@ -422,7 +424,7 @@
 
         if ( objects.isEmpty() )
         {
-            ContextUtils.conflictResponse( response, getEntityName() + " does not exist: " + uid );
+            ContextUtils.notFoundResponse( response, getEntityName() + " does not exist: " + uid );
             return;
         }
 
@@ -457,7 +459,7 @@
 
         if ( objects.isEmpty() )
         {
-            ContextUtils.conflictResponse( response, getEntityName() + " does not exist: " + uid );
+            ContextUtils.notFoundResponse( response, getEntityName() + " does not exist: " + uid );
             return;
         }
 
@@ -469,6 +471,69 @@
         manager.delete( objects.get( 0 ) );
     }
 
+    @RequestMapping( value = "/{uid}/{property}/{itemId}", method = RequestMethod.DELETE )
+    @SuppressWarnings( "unchecked" )
+    public void deleteCollectionItem(
+        @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 deletes 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 ) );
+
+        Iterator<IdentifiableObject> iterator = identifiableObjects.iterator();
+        IdentifiableObject candidate = null;
+
+        while ( iterator.hasNext() )
+        {
+            candidate = iterator.next();
+
+            if ( candidate.getUid() != null && candidate.getUid().equals( pvItemId ) )
+            {
+                iterator.remove();
+                break;
+            }
+
+            candidate = null;
+        }
+
+        if ( candidate == null )
+        {
+            ContextUtils.notFoundResponse( response, "Collection " + pvProperty + " does not have an item with ID: " + pvItemId );
+        }
+
+        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 ) );
+    }
+
     //--------------------------------------------------------------------------
     // Hooks
     //--------------------------------------------------------------------------