=== 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 parameters, HttpServletRequest request, HttpServletResponse response ) throws Exception - { - return getObjectInternal( uid, parameters, Lists.newArrayList(), Lists.newArrayList( property ) ); - } - @RequestMapping( value = "/{uid}", method = RequestMethod.GET ) public @ResponseBody RootNode getObject( @PathVariable( "uid" ) String uid, @RequestParam Map 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 parameters, HttpServletRequest request, HttpServletResponse response ) throws Exception + { + return getObjectInternal( uid, parameters, Lists.newArrayList(), Lists.newArrayList( property ) ); + } + private RootNode getObjectInternal( String uid, Map parameters, List filters, List 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 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 identifiableObjects = + (Collection) property.getGetterMethod().invoke( objects.get( 0 ) ); + + Iterator 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 //--------------------------------------------------------------------------