Rally Software

 View Only
Expand all | Collapse all

Updating the new teammembership without removing the existing teammembership .

  • 1.  Updating the new teammembership without removing the existing teammembership .

    Posted Oct 11, 2017 07:45 AM

    I have a requirement for updating the teammembership for the existing user.But it is updating the new  teammembership without removing the existing teammembership . For example I have an already created user USER1 with teammebership to PROJ1 and PROJ2. Now I want to update the user to create teammebership for PROJ3 and it should remove the teammebership for  PROJ1 and PROJ2.

     

    Sharing java code, Please provide support .

     

                    teamMembershipProjRef = new ArrayList<String>();

                    teamMembershipProjRef.add(projRef);

                                                   

    if(!(role1.equalsIgnoreCase("Reporter"))){

                                                                      logger.info("The TeamMembership size is....." + teamMembershipProjRef.size());

                                                                      System.out.println(teamMembershipProjRef);

                                                                      String existingTeamMembershipRef = createResponse.getObject().get("TeamMemberships").getAsJsonObject().get("_ref").getAsString();

                                                                                    JsonArray teamMembershipArray = new JsonArray();

                                                                                    JsonObject teamMembershipRefObj;

                                                                                    for(String membershipProjectRef : teamMembershipProjRef){

                                                                                                    teamMembershipRefObj = new JsonObject();

                                                                                                    teamMembershipRefObj.addProperty("_ref", membershipProjectRef);

                                                                                                    teamMembershipArray.add(teamMembershipRefObj);

                                                                                    }

                                                                                    CollectionUpdateRequest teamMembershipUpdateReq = new CollectionUpdateRequest(existingTeamMembershipRef, teamMembershipArray, true);

                                                                                    CollectionUpdateResponse teamMembershipUpdateResp = restApi.updateCollection(teamMembershipUpdateReq);

                                                                                   

                                                                                     if(teamMembershipUpdateResp.wasSuccessful())

                                                                                    {

                                                                                                    System.out.println("Team Membership created....");

                                                                                    }

                                                                                    else {

                                                                                                    System.out.println("Error creating TeamMembership");

                                                                                    }

                                                      }

     

    Thanks in advance…



  • 2.  Re: Updating the new teammembership without removing the existing teammembership .
    Best Answer

    Posted Oct 11, 2017 08:54 AM

    Avi12Apr2017,

     

    I responded with this information on your other post:

     

    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 removed
    adding - 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