dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #36185
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18530: update collection API to support updating collection disregarding which side is the owner
------------------------------------------------------------
revno: 18530
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-03-09 11:57:56 +0530
message:
update collection API to support updating collection disregarding which side is the owner
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 2015-03-06 03:03:52 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java 2015-03-09 06:27:56 +0000
@@ -91,7 +91,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -579,13 +578,13 @@
return;
}
- if ( !getSchema().getPropertyMap().containsKey( pvProperty ) )
+ if ( !getSchema().haveProperty( pvProperty ) )
{
ContextUtils.notFoundResponse( response, "Property " + pvProperty + " does not exist on " + getEntityName() );
return;
}
- Property property = getSchema().getPropertyMap().get( pvProperty );
+ Property property = getSchema().getProperty( pvProperty );
if ( !property.isCollection() || !property.isIdentifiableObject() )
{
@@ -593,41 +592,50 @@
return;
}
- if ( !property.isOwner() )
- {
- ContextUtils.conflictResponse( response, getEntityName() + " is not the owner of this relationship." );
- return;
- }
-
- Collection<IdentifiableObject> identifiableObjects =
- (Collection<IdentifiableObject>) property.getGetterMethod().invoke( objects.get( 0 ) );
-
- IdentifiableObject candidate = manager.getNoAcl( (Class<? extends IdentifiableObject>) property.getItemKlass(), pvItemId );
-
- if ( candidate == null )
+ IdentifiableObject inverseObject = manager.getNoAcl( (Class<? extends IdentifiableObject>) property.getItemKlass(), pvItemId );
+ IdentifiableObject owningObject = objects.get( 0 );
+
+ if ( inverseObject == null )
{
ContextUtils.notFoundResponse( response, "Collection " + pvProperty + " does not have an item with ID: " + pvItemId );
return;
}
+ Collection<IdentifiableObject> collection;
+
+ if ( property.isOwner() )
+ {
+ collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke( owningObject );
+ }
+ else
+ {
+ Schema owningSchema = getSchema( property.getItemKlass() );
+ Property owningProperty = owningSchema.propertyByRole( property.getOwningRole() );
+ collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke( inverseObject );
+
+ IdentifiableObject o = owningObject;
+ owningObject = inverseObject;
+ inverseObject = o;
+ }
+
+ response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+
+ if ( !aclService.canUpdate( currentUserService.getCurrentUser(), owningObject ) )
+ {
+ throw new DeleteAccessDeniedException( "You don't have the proper permissions to update this object." );
+ }
+
// 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 ) )
+ if ( collection.contains( inverseObject ) )
{
response.setStatus( HttpServletResponse.SC_NO_CONTENT );
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." );
- }
-
- response.setStatus( HttpServletResponse.SC_NO_CONTENT );
-
- manager.update( objects.get( 0 ) );
- manager.refresh( candidate );
+ collection.add( inverseObject );
+
+ manager.update( owningObject );
+ manager.refresh( inverseObject );
}
@RequestMapping( value = "/{uid}/{property}/{itemId}", method = RequestMethod.DELETE )
@@ -660,46 +668,42 @@
return;
}
- if ( !property.isOwner() )
- {
- ContextUtils.conflictResponse( response, getEntityName() + " is not the owner of this relationship." );
- return;
- }
-
- 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 )
+ Collection<IdentifiableObject> collection;
+ IdentifiableObject inverseObject = manager.getNoAcl( (Class<? extends IdentifiableObject>) property.getItemKlass(), pvItemId );
+ IdentifiableObject owningObject = objects.get( 0 );
+
+ if ( property.isOwner() )
+ {
+ collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke( owningObject );
+ }
+ else
+ {
+ Schema owningSchema = getSchema( property.getItemKlass() );
+ Property owningProperty = owningSchema.propertyByRole( property.getOwningRole() );
+ collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke( inverseObject );
+
+ IdentifiableObject o = owningObject;
+ owningObject = inverseObject;
+ inverseObject = o;
+ }
+
+ if ( !aclService.canUpdate( currentUserService.getCurrentUser(), owningObject ) )
+ {
+ throw new DeleteAccessDeniedException( "You don't have the proper permissions to delete this object." );
+ }
+
+ if ( !collection.contains( inverseObject ) )
{
ContextUtils.notFoundResponse( response, "Collection " + pvProperty + " does not have an item with ID: " + pvItemId );
return;
}
- if ( !aclService.canUpdate( currentUserService.getCurrentUser(), objects.get( 0 ) ) )
- {
- throw new DeleteAccessDeniedException( "You don't have the proper permissions to delete this object." );
- }
+ collection.remove( inverseObject );
response.setStatus( HttpServletResponse.SC_NO_CONTENT );
- manager.update( objects.get( 0 ) );
- manager.refresh( candidate );
+ manager.update( owningObject );
+ manager.refresh( inverseObject );
}
//--------------------------------------------------------------------------
@@ -830,6 +834,11 @@
return schema;
}
+ protected Schema getSchema( Class<?> klass )
+ {
+ return schemaService.getDynamicSchema( klass );
+ }
+
protected void addAccessProperties( List<T> objects )
{
User user = currentUserService.getCurrentUser();