2013-08-22 64 views
0

我試着讀討論從用戶故事通過拉力REST API討論在用戶故事

RallyRestApi restApi = new RallyRestApi(new URI(rallyURL), userName, userPassword); 
    restApi.setApplicationName("QueryExample"); 

    try 
    { 

     QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement"); 
     storyRequest.setFetch(new Fetch("Name", "Owner", "UserName", "EmailAddress", "Discussion")); 
     storyRequest.setLimit(1000); 
     storyRequest.setScopedDown(false); 
     storyRequest.setScopedUp(false); 
     storyRequest.setWorkspace(workspaceRef); 
     storyRequest.setProject(projectRef); 
     storyRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "US4")); 

     QueryResponse storyQueryResponse = restApi.query(storyRequest); 
     JsonObject storyJsonObject = storyQueryResponse.getResults().get(0).getAsJsonObject(); 

     System.out.println("Name: " + storyJsonObject.get("Name")); 
     JsonObject userObject = storyJsonObject.get("Owner").getAsJsonObject().getAsJsonObject(); 
     System.out.println(userObject.get("UserName")); 
     System.out.println(userObject.get("EmailAddress")); 
     System.out.println(userObject.get("Discussion")); 

    } 
    finally 
    { 
     //Release resources 
     restApi.close(); 
    } 

討論爲NULL。

如何通過REST API讀取和編寫用戶故事討論?

WBR,斯拉瓦

回答

0

只要你的故事,你可以遍歷它在ConversationPost,這是討論集合的元素文本屬性如下:

int numberOfPosts = storyJsonObject.get("Discussion").getAsJsonArray().size(); 
      System.out.println(numberOfPosts); 
      if(numberOfPosts>0){ 
       for (int i=0;i<numberOfPosts;i++){ 
        System.out.println(storyJsonObject.get("Discussion").getAsJsonArray().get(i).getAsJsonObject().get("Text")); 
       } 
      } 

而且,這些場需要取:

storyRequest.setFetch(new Fetch("Name","Discussion","ConversationPost","Text")); 

這裏是整個代碼:

import com.google.gson.JsonObject; 
import com.rallydev.rest.RallyRestApi; 
import com.rallydev.rest.request.QueryRequest; 
import com.rallydev.rest.response.QueryResponse; 
import com.rallydev.rest.util.Fetch; 
import com.rallydev.rest.util.QueryFilter; 
import java.io.IOException; 
import java.net.URI; 
import java.net.URISyntaxException; 

public class aRESTstories { 

    public static void main(String[] args) throws URISyntaxException, IOException { 


      String host = "https://rally1.rallydev.com"; 
      String username = "[email protected]"; 
      String password = "1984"; 
     String workspaceRef = "/workspace/1111"; 
      String projectRef = "/project/2222";    

      String applicationName = "RESTExample: find story, print discussions"; 
      String wsapiVersion = "1.43"; 

     RallyRestApi restApi = new RallyRestApi(
       new URI(host), 
       username, 
       password); 
     restApi.setApplicationName(applicationName); 
     restApi.setWsapiVersion(wsapiVersion); 
     System.out.println(restApi.getWsapiVersion()); 

     try{ 
      QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement"); 
      storyRequest.setFetch(new Fetch("Name","Owner","UserName","PlanEstimate","Discussion","ConversationPost","Text")); 
      storyRequest.setLimit(1000); 
      storyRequest.setScopedDown(false); 
      storyRequest.setScopedUp(false); 
      storyRequest.setWorkspace(workspaceRef); 
      storyRequest.setProject(projectRef); 
      storyRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "US359")); 

      QueryResponse storyQueryResponse = restApi.query(storyRequest); 
      JsonObject storyJsonObject = storyQueryResponse.getResults().get(0).getAsJsonObject(); 
      String storyRef = storyJsonObject.get("_ref").toString(); 
      System.out.println("Name: " + storyJsonObject.get("Name")); 

      int numberOfPosts = storyJsonObject.get("Discussion").getAsJsonArray().size(); 
      System.out.println(numberOfPosts); 
      if(numberOfPosts>0){ 
       for (int i=0;i<numberOfPosts;i++){ 
        System.out.println(storyJsonObject.get("Discussion").getAsJsonArray().get(i).getAsJsonObject().get("Text")); 
       } 
      } 

     }catch(Exception e){ 
      System.out.println("Exception occurred...."); 
      e.printStackTrace(); 
     } 
     finally{ 
      restApi.close(); 
     } 
    } 
} 
+0

感謝您的幫助。但我有一個錯誤..我編輯你的答案 –

+0

@VyacheslavYanyuk不編輯帖子來報告他們的問題 - 添加問題作爲評論,以便它可以在答案中解決! – codeling

+0

我沒有看到編輯,並驗證它正在工作。爲了使它適合你,你需要糾正什麼? – nickm