Rally1

Expand all | Collapse all

Create a Team membership for a user

Jump to Best Answer
  • 1.  Create a Team membership for a user

    Posted 08-28-2017 08:08 AM

    I have a requirement for creating the TeamMembership for some particular project (E.g project5 and project7) in a project hierarchy  for the newly created  user(E.g User1) under a workspace (E.g Workspace2). when some one click on the check box of Team Member then the permission level of that corresponding project become to Editor .

    I have created the Userpermission for the user to Viewer level. I have attached the code that I have implemented. But it is not working.

     

    please suggest.

     

     

    Thanks in advance.



  • 2.  Re: Create a Team membership for a user

    Posted 08-28-2017 07:04 PM

    Hi Avinash,

     

    The problem with your code is that the createRsponse.getObject() method is running a get(“TeamMemberships”) for a JsonObject which has a return type of JsonElement.

     

    https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonObject.html

     

    I suggest you change your object type to JsonElement, example:

     

    JsonElement existingTeamMembershipsObj = (JsonElement)createResponse.getObject().get(“TeamMemberships”);

     

    Then you can work with the returned URI as a JsonElement.

     

    Let me know if that helps,

     

    Sean Davis



  • 3.  Re: Create a Team membership for a user

    Posted 08-29-2017 04:06 AM

    Hi Sean,

    I have applied the logic that you have suggested, still not working.

    jsonElement existingTeamMembership1= (jsonElement )createResp.getObject().get("TeamMembership");

    JsonObject  existingTeamMembership = (JsonObject  )existingTeamMembership1;

    existingTeamMembership .add("Project", projectObject);

    JsonObject  updateTeamMembership = new JsonObject  ();

    updateTeamMembership .add("TeamMemberships" , existingTeamMembership );

     

    Accessing the URI to update the existingTeamMembership object.

     

    can you please help ?



  • 4.  Re: Create a Team membership for a user

    Posted 08-29-2017 04:15 AM

    This is the modified code which I have written.



  • 5.  Re: Create a Team membership for a user
    Best Answer

    Posted 08-29-2017 07:37 AM

    Avi12Apr2017,

     

    I think a different approach will work here.  If you are using the 2.2.1 version of the RallyRestToolkitForJava, you can call the updateCollection() method:

    import com.rallydev.rest.request.CollectionUpdateRequest;
    import com.rallydev.rest.response.CollectionUpdateResponse;

     

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

     

    JsonArray teamMembershipsArray = new JsonArray();
    JsonObject teamMembershipRefObj = new JsonObject();

     

    // projRef is the reference to the desired project i.e. - "/project/12345"

    teamMembershipRefObj.addProperty("_ref", projRef);
    teamMembershipsArray.add(teamMembershipRefObj);

     

    CollectionUpdateRequest teamMembershipsCollectionAddRequest = new CollectionUpdateRequest(teamMembershipsRef, teamMembershipsArray, true);
    CollectionUpdateResponse teamMembershipsCollectionAddResponse = restApi.updateCollection(teamMembershipsCollectionAddRequest);

     

    if(teamMembershipsCollectionAddResponse.wasSuccessful()){
       System.out.println("TeamMembership Updated Successfully");
    }
    else{
       System.out.println("TeamMembership error occured");
    }

     

    I used the information posted here, User Guide · RallyTools/RallyRestToolkitForJava Wiki · GitHub, to construct this code.

     

    I hope that helps!

     

    Michael



  • 6.  Re: Create a Team membership for a user

    Posted 09-20-2017 08:37 AM

    Hi Michael,

     

    I have used the rest API 2.2.1 jar. I am able to access the class  CollectionUpdateRequest class. But it is not providing the updateCollection() method. Instead update() method is available there. Can you please let me know the exact version in which the  updateCollection() method is available?

     

    Thanks.



  • 7.  Re: Create a Team membership for a user

    Posted 09-20-2017 09:03 AM

    Avi12Apr2017,

     

    I am using 2.2.1 as well, pulled from the link I posted above.  So I am not sure why you are not seeing it.

     

    I'll see if I can find any more information.


    Michael



  • 8.  Re: Create a Team membership for a user

    Posted 10-10-2017 07:32 AM

    Hi Micheal,

     

    Now I am able to update the teammemberhip. But It keeps the previous teammemberhip for the project.

    Example : I have a user called USER1 having teammembership for PROJ1 and PROJ2. I want to update the USER1 and give teammemberhip

    to PROJ3. In that case it is not deleting the teammembership  for PROJ1 and PROJ2.

    could you please help.

    Thanks..



  • 9.  Re: Create a Team membership for a user

    Posted 10-11-2017 06:27 AM

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