Tuesday, 16 April 2019

Get a list of Tasks from a Project in SDL WorldServer using the REST API

In this article, I would like to present a method for retrieving a list of Tasks from a Project in SDL WorldServer using the REST API.

The method takes three parameters:
  1. wsBaseUrl
  2. token
  3. projectId
The wsBaseUrl must be the <serverURL>:<portnumber> where your WorldServer instance is running.

The second parameter is a security token, which can be retrieved by using the SDL WorldServer REST API as explained here.

The third parameter is the id of the Project in SDL WorldServer to retrieve the tasks from.

The method returns a List of TaskDetails objects, one for each task returned, containing a subset of the JSON response data.

public class CurrentTaskStep {
private int id;
private String name;
private String displayName;
private String type;
private String typeName;
private List<WorkflowTransition> workflowTransitions;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public List<WorkflowTransition> getWorkflowTransitions() {
return workflowTransitions;
}
public void setWorkflowTransitions(List<WorkflowTransition> workflowTransitions) {
this.workflowTransitions = workflowTransitions;
}
public class WorkflowTransition {
private int id;
private int index;
private String text;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
}
public class TaskDetails {
private int id;
private CurrentTaskStep currentTaskStep;
private String targetAsset;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public CurrentTaskStep getCurrentTaskStep() {
return currentTaskStep;
}
public void setCurrentTaskStep(CurrentTaskStep currentTaskStep) {
this.currentTaskStep = currentTaskStep;
}
public String getTargetAsset() {
return targetAsset;
}
public void setTargetAsset(String targetAsset) {
this.targetAsset = targetAsset;
}
}
public static List<TaskDetails> getTasksFromProject(String wsBaseUrl, String token, int projectId)
throws IOException, URISyntaxException {
Gson gson = new GsonBuilder().create();
URI getUri = new URIBuilder(wsBaseUrl + "/ws-api/v1/tasks")
.addParameter("token", token)
.addParameter("projectId", String.valueOf(projectId))
.build();
List<TaskDetails> tasks = new ArrayList<>();
JsonObject jsonObject = getItems(getUri);
JsonArray items = jsonObject.getAsJsonArray("items");
Iterator<JsonElement> itemIterator = items.iterator();
while (itemIterator.hasNext()) {
JsonObject item = itemIterator.next().getAsJsonObject();
// Get the current task step details
JsonObject jsonCurrentTaskStep = item.getAsJsonObject("currentTaskStep");
CurrentTaskStep currentTaskStep = new CurrentTaskStep();
currentTaskStep.setId(jsonCurrentTaskStep.getAsJsonPrimitive("id").getAsInt());
currentTaskStep.setName(jsonCurrentTaskStep.getAsJsonPrimitive("name").getAsString());
currentTaskStep.setDisplayName(jsonCurrentTaskStep.getAsJsonPrimitive("displayName").getAsString());
currentTaskStep.setType(jsonCurrentTaskStep.getAsJsonPrimitive("type").getAsString());
currentTaskStep.setTypeName(jsonCurrentTaskStep.getAsJsonPrimitive("typeName").getAsString());
// Fetch the transitions
List<CurrentTaskStep.WorkflowTransition> workflowTransitions = new ArrayList<>();
Iterator<JsonElement> transitionsIt = jsonCurrentTaskStep.getAsJsonArray("workflowTransitions").iterator();
while (transitionsIt.hasNext()) {
workflowTransitions.add(gson.fromJson(transitionsIt.next(), CurrentTaskStep.WorkflowTransition.class));
}
currentTaskStep.setWorkflowTransitions(workflowTransitions);
// Bundle everything together
TaskDetails taskDetails = new TaskDetails();
taskDetails.setId(item.getAsJsonPrimitive("id").getAsInt());
taskDetails.setCurrentTaskStep(currentTaskStep);
// Fetch the target asset (if present)
JsonArray targetAssetsArray = item.getAsJsonArray("targetAssets");
if (targetAssetsArray.size() > 0) {
taskDetails.setTargetAsset(targetAssetsArray.get(0).getAsString());
}
// Add to the list
tasks.add(taskDetails);
}
return tasks;
}

No comments:

Post a Comment