=== 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 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 identifiableObjects = + (Collection) property.getGetterMethod().invoke( objects.get( 0 ) ); + + IdentifiableObject candidate = manager.getNoAcl( (Class) 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 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 optional = Enums.getIfPresent( InclusionStrategy.Include.class, inclusionStrategy ); - - if ( optional.isPresent() ) - { - return optional.get(); - } - } - - return InclusionStrategy.Include.NON_NULL; - } }