Skip to content

Instantly share code, notes, and snippets.

@pmedcraft
Last active August 12, 2019 16:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pmedcraft/a6843aab00a6ceeb1b91a582febb723c to your computer and use it in GitHub Desktop.
Creates a Project Group in SDL WorldServer using the REST API returning a CreateProjectGroupResponse object with data from the newly generated Project Group
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;
}
}
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");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment