1
votes

How do I fetch child user stories with Java client using Rally API?

Using Chrome's Postman client with URL https://us1.rallydev.com/slm/webservice/v2.0/HierarchicalRequirement/ObjectId/Children, I am able to fetch the children user stories.

But when I try with a Java client, like this:

QueryRequest request = new QueryRequest("/HierarchicalRequirement/ObjectId/Children");

it doesn't work.

Any pointer would be helpful.

1

1 Answers

0
votes

It will take fetching "Children" collection on user stories and then hydrating it in a separate request. Here is an example based on latest version of Rally toolkit for Java:

public class GetChildStories {

    public static void main(String[] args) throws Exception {

        String host = "https://rally1.rallydev.com";
        String apiKey = "_abc123";
                String applicationName = "Find Child Stories of Epics filtered by Tag";
                String workspaceRef = "/workspace/12352608129";

                RallyRestApi restApi = null;
        try {
            restApi = new RallyRestApi(new URI(host),apiKey);
            QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement");
            storyRequest.setWorkspace(workspaceRef);
            restApi.setApplicationName(applicationName);  
            storyRequest.setFetch(new Fetch(new String[] {"Name", "FormattedID", "Tags", "Children"}));
            storyRequest.setLimit(1000);
            storyRequest.setScopedDown(false);
            storyRequest.setScopedUp(false);

            storyRequest.setQueryFilter((new QueryFilter("Tags.Name", "contains", "\"tag1\"")).and(new QueryFilter("DirectChildrenCount", ">", "0")));

            QueryResponse storyQueryResponse = restApi.query(storyRequest);
            System.out.println("Successful: " + storyQueryResponse.wasSuccessful());
            System.out.println("Size: " + storyQueryResponse.getTotalResultCount());

            for (int i=0; i<storyQueryResponse.getTotalResultCount();i++){
                JsonObject storyJsonObject = storyQueryResponse.getResults().get(i).getAsJsonObject();
                System.out.println("Name: " + storyJsonObject.get("Name") + " FormattedID: " + storyJsonObject.get("FormattedID"));
                QueryRequest childrenRequest = new QueryRequest(storyJsonObject.getAsJsonObject("Children"));
                childrenRequest.setFetch(new Fetch("Name","FormattedID"));
                int numberOfChildren = storyJsonObject.get("DirectChildrenCount").getAsInt();
                System.out.println(numberOfChildren);
                //load the collection
                JsonArray children = restApi.query(childrenRequest).getResults();
                for (int j=0;j<numberOfChildren;j++){
                    System.out.println("Name: " + children.get(j).getAsJsonObject().get("Name") + children.get(j).getAsJsonObject().get("FormattedID").getAsString());
                System.out.println("Name: " + children.get(0).getAsJsonObject().get("Name") + children.get(0).getAsJsonObject().get("FormattedID").getAsString());
                }
            }


        } finally {
            if (restApi != null) {
                restApi.close();
            }
        }
    }

}