Skip to content

Instantly share code, notes, and snippets.

@pmedcraft
Last active April 15, 2019 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmedcraft/2b7ab18143bc1b301aa5b4630181f0b4 to your computer and use it in GitHub Desktop.
Save pmedcraft/2b7ab18143bc1b301aa5b4630181f0b4 to your computer and use it in GitHub Desktop.
Gets a list of Tasks (TaskDetails objects) from a Project in SDL WorldServer using the REST API
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment