Tuesday, 16 April 2019

Create Project Groups in SDL WorldServer using the REST API

In this article, I would like to present a method for creating a Project Group in SDL WorldServer using the REST API.

The method takes three parameters:
  1. wsBaseUrl
  2. token
  3. projectGroups
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 a List of ProjectGroup objects, each one containing the data required for creating each Project Group in WorldServer. This includes the namedescriptionprojectTypeIdclientId, the list of files for translation (systemFiles) and the list of Locales (target locales). The systemFiles attribute (List of String) must be populated with each asset's internalName returned by the file upload process described here.

The method returns a List of CreateProjectGroupResponse objects, one for each project group created, containing the JSON response data. With this data, it is possible to verify if the call was successful and also retrieve the newly created project group id.

public class CreateProjectGroupResponse {
private String status;
private List<Response> response;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Response> getResponse() {
return response;
}
public void setResponse(List<Response> response) {
this.response = response;
}
public class Response {
private String status;
private int response;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getResponse() {
return response;
}
public void setResponse(int response) {
this.response = response;
}
}
}
public class Locale {
private int id;
private String dueDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDueDate() {
return dueDate;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
}
view raw Locale.java hosted with ❤ by GitHub
public class ProjectGroup {
private String name;
private String description;
private int projectTypeId;
private int clientId;
private List<String> systemFiles;
private List<Locale> locales;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getProjectTypeId() {
return projectTypeId;
}
public void setProjectTypeId(int projectTypeId) {
this.projectTypeId = projectTypeId;
}
public int getClientId() {
return clientId;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
public List<String> getSystemFiles() {
return systemFiles;
}
public void setSystemFiles(List<String> systemFiles) {
this.systemFiles = systemFiles;
}
public List<Locale> getLocales() {
return locales;
}
public void setLocales(List<Locale> locales) {
this.locales = locales;
}
}
public static CreateProjectGroupResponse createProjectGroup(String wsBaseUrl, String token, List<ProjectGroup> projectGroups)
throws IOException, URISyntaxException {
JsonObject jsonObject = doPost(wsBaseUrl, "/ws-api/v1/projectGroups/create", token, projectGroups);
Gson gson = new GsonBuilder().create();
return gson.fromJson(jsonObject, CreateProjectGroupResponse.class);
}
public static JsonObject doPost(String wsBaseUrl, String restPath, String token, Object pojo)
throws IOException, URISyntaxException {
URI postUri = new URIBuilder(wsBaseUrl + restPath)
.addParameter("token", token)
.addParameter("content-type", "application/json")
.build();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(postUri);
httpPost.setEntity(getStringEntity(pojo));
HttpResponse response = httpClient.execute(httpPost);
Gson gson = new GsonBuilder().create();
return gson.fromJson(new InputStreamReader(response.getEntity().getContent()), JsonObject.class);
}
private static StringEntity getStringEntity(Object pojo) {
Gson gson = new GsonBuilder().create();
String jsonString = gson.toJson(pojo);
return new StringEntity(jsonString, ContentType.APPLICATION_JSON);
}
public static void createProjectGroup(Properties config, String token) throws IOException, URISyntaxException, NumberFormatException {
if (!enableProjectCreation(config))
return;
// Upload assets
Collection<File> assetFiles = getIncomingFiles(config);
if (assetFiles == null || assetFiles.isEmpty()) {
System.out.println("There are no incoming files.");
return;
}
List<FileUploadResponse> responses = WSRestUtils.uploadAssets(getWsBaseUrl(config), token, assetFiles);
// Create project group
List<String> internalNames = new ArrayList<>();
for (FileUploadResponse fileUploadResponse : responses) {
internalNames.add(fileUploadResponse.getInternalName());
}
ProjectGroup projectGroup = new ProjectGroup();
projectGroup.setName("Project - " + internalNames.get(0));
projectGroup.setDescription("Project created using the REST API.");
projectGroup.setClientId(getClientId(config));
projectGroup.setProjectTypeId(getProjectTypeId(config));
projectGroup.setLocales(getLocales(config));
projectGroup.setSystemFiles(internalNames);
// Create the project group
CreateProjectGroupResponse response = WSRestUtils
.createProjectGroup(getWsBaseUrl(config), token, Arrays.asList(projectGroup));
if (StringUtils.equalsIgnoreCase(response.getStatus(), "OK")) {
System.out.println("Created project group with id: " + response.getResponse().get(0).getResponse());
// Move the asset files to the processed folder
File processedFolder = new File(getProcessedFolder(config));
for (File file : assetFiles) {
FileUtils.moveFileToDirectory(file, processedFolder, false);
}
System.out.println("Moved files to the processed folder");
}
}

No comments:

Post a Comment