Avi12Apr2017,
From the Java Docs for the RallyRestToolkitForJava at CollectionUpdateRequest (Rally Rest Toolkit For Java 2.2.1 API) you can remove Collection entries by passing 'false' as the last parameter in the CollectionUpdateRequest:
public CollectionUpdateRequest(String collectionRef, com.google.gson.JsonArray items, boolean adding)
Create a new update request for the specified collection and values.
- Parameters:
collectionRef
- the ref of the collection to be updated. May be absolute or relative, e.g. "/defect/12345/tags"items
- the items to be added or removedadding
- true if adding, false if removing
There may be a better way to do this, but I just tested the code below and it works to remove all existing Team Memberships. You would then just add the Team Memberships you wanted. The 'userJsonObject' used below has the information about the User in question, so use whatever Object needed to get the Team Memberships _ref for the current User.
// Find the existing Team Memberships to delete
System.out.println("Finding existing Team Memberships");
String memsRef = userJsonObject.get("TeamMemberships").getAsJsonObject().get("_ref").getAsString();
getRequest = new GetRequest(memsRef);
getResponse = restApi.get(getRequest);
JsonArray existingTeamMembershipsArray = new JsonArray();
JsonObject existingTeamMembershipsObject;
for(int j=0; j<getResponse.getObject().get("Results").getAsJsonArray().size(); j++){
existingTeamMembershipsObject = new JsonObject();
existingTeamMembershipsObject.addProperty("_ref", getResponse.getObject().get("Results").getAsJsonArray().get(j).getAsJsonObject().get("_ref").getAsString());
existingTeamMembershipsArray.add(existingTeamMembershipsObject);
}
// Remove the existing Team Memberships
CollectionUpdateRequest teamMembershipsCollectionAddRequest = new CollectionUpdateRequest(memsRef, existingTeamMembershipsArray, false);
CollectionUpdateResponse teamMembershipsCollectionAddResponse = restApi.updateCollection(teamMembershipsCollectionAddRequest);
if(teamMembershipsCollectionAddResponse.wasSuccessful()){
System.out.println("TeamMembership Deleted Successfully");
}
else {
System.out.println("TeamMembership error occured");
}
I hope that helps.
Also, you can post these more technical questions on Stackoverflow at Newest 'rally' Questions - Stack Overflow and you will likely receive a quicker response.
Thank you.
Michael