diff --git a/core/pom.xml b/core/pom.xml index 9688729..e7b9e9e 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -20,7 +20,7 @@ com.adobe.granite.translation.connector lilt-connector - 2.0-SNAPSHOT + 2.2 ../pom.xml lilt-connector.core diff --git a/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/BootstrapTranslationServiceImpl.java b/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/BootstrapTranslationServiceImpl.java index 992a1be..3ff1e36 100644 --- a/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/BootstrapTranslationServiceImpl.java +++ b/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/BootstrapTranslationServiceImpl.java @@ -281,8 +281,9 @@ public TranslationStatus getTranslationJobStatus(String strTranslationJobID) thr try { int imported = 0; int translated = 0; + int approved = 0; + int rejected = 0; SourceFile[] files = liltApiClient.getFiles(strTranslationJobID); - // loop backwards through the list since the most recent files will be at the end. for (SourceFile file : files) { if (file.labels.contains("status=IMPORTED")) { imported++; @@ -290,30 +291,48 @@ public TranslationStatus getTranslationJobStatus(String strTranslationJobID) thr if (file.labels.contains("status=TRANSLATED")) { translated++; } + if (file.labels.contains("approval=APPROVED")) { + approved++; + } + if (file.labels.contains("approval=REJECTED")) { + rejected++; + } } boolean hasImported = imported > 0; boolean hasTranslated = translated > 0; + boolean hasRejected = rejected > 0; + if (hasImported && hasTranslated && approved >= imported) { + log.warn("lilt: job status for {} is APPROVED", strTranslationJobID); + return TranslationStatus.APPROVED; + } + if (hasImported && hasTranslated && hasRejected) { + log.info("lilt: job status for {} is TRANSLATION_IN_PROGRESS", strTranslationJobID); + return TranslationStatus.TRANSLATION_IN_PROGRESS; + } if (hasImported && hasTranslated) { + log.warn("lilt: job status for {} is TRANSLATED", strTranslationJobID); return TranslationStatus.TRANSLATED; } if (hasImported) { + log.warn("lilt: job status for {} is TRANSLATION_IN_PROGRESS", strTranslationJobID); return TranslationStatus.TRANSLATION_IN_PROGRESS; } } catch (Exception e) { log.warn("error during getTranslationJobStatus {}", e); } + log.warn("lilt: job status for {} is COMMITTED_FOR_TRANSLATION", strTranslationJobID); return TranslationStatus.COMMITTED_FOR_TRANSLATION; } @Override public CommentCollection getTranslationJobCommentCollection(String strTranslationJobID) { - log.trace("BootstrapTranslationServiceImpl.getTranslationJobCommentCollection"); + log.warn("BootstrapTranslationServiceImpl.getTranslationJobCommentCollection"); return null; } @Override public void addTranslationJobComment(String strTranslationJobID, Comment comment) throws TranslationException { - log.trace("BootstrapTranslationServiceImpl.addTranslationJobComment"); + log.warn("BootstrapTranslationServiceImpl.addTranslationJobComment"); throw new TranslationException("This function is not implemented", TranslationException.ErrorCode.SERVICE_NOT_IMPLEMENTED); @@ -323,13 +342,16 @@ public void addTranslationJobComment(String strTranslationJobID, Comment comment public InputStream getTranslatedObject(String strTranslationJobID, TranslationObject translationObj) throws TranslationException { log.trace("BootstrapTranslationServiceImpl.getTranslatedObject"); + log.warn("lilt: strTranslationJobID: {}", strTranslationJobID); String labels = String.format("%s,status=TRANSLATED", strTranslationJobID); String objectPath = String.format("%s.%s", getObjectPath(translationObj), exportFormat); + log.warn("lilt: checking for objectPath {}", objectPath); try { SourceFile[] files = liltApiClient.getFiles(labels); // loop backwards through the list since the most recent files will be at the end. for (int i = files.length - 1; i >= 0; i--) { SourceFile file = files[i]; + log.warn("lilt: comparing {} to {}", file.name, objectPath); if (Objects.equals(file.name, objectPath)) { log.warn("Downloading the translated lilt file {}", file.id); String translation = liltApiClient.downloadFile(file.id); @@ -397,10 +419,35 @@ public String uploadTranslationObject(String strTranslationJobID, TranslationObj @Override public TranslationStatus updateTranslationObjectState(String strTranslationJobID, - TranslationObject translationObject, TranslationState state) throws TranslationException { - log.trace("BootstrapTranslationServiceImpl.updateTranslationObjectState"); - // bootstrapTmsService.setTmsProperty(strTranslationJobID+getObjectPath(translationObject), BootstrapTmsConstants.BOOTSTRAP_TMS_STATUS, state.getStatus().toString()); - return TranslationStatus.TRANSLATED; + TranslationObject translationObject, TranslationState state) throws TranslationException { + log.trace("BootstrapTranslationServiceImpl.updateTranslationObjectState"); + log.warn("lilt: strTranslationJobID: {}", strTranslationJobID); + String labels = String.format("%s,status=TRANSLATED", strTranslationJobID); + String status = state.getStatus().toString(); + String label = String.format("approval=%s", status); + String objectPath = String.format("%s.%s", getObjectPath(translationObject), exportFormat); + log.warn("lilt: checking for objectPath {}", objectPath); + try { + SourceFile[] files = liltApiClient.getFiles(labels); + // loop backwards through the list since the most recent files will be at the end. + for (int i = files.length - 1; i >= 0; i--) { + SourceFile file = files[i]; + log.warn("lilt: comparing {} to {}", file.name, objectPath); + if (Objects.equals(file.name, objectPath)) { + log.warn("Adding label {} to file {}", label, file.id); + liltApiClient.addLabel(file.id, label); + return state.getStatus(); + } + } + } catch (Exception e) { + log.warn("error during getTranslatedObject {}", e); + } + Comment comment = state.getComment(); + if (comment != null) { + log.warn(comment.getMessage()); + addTranslationObjectComment(strTranslationJobID, translationObject, comment); + } + return state.getStatus(); } @Override @@ -408,12 +455,17 @@ public TranslationStatus getTranslationObjectStatus(String strTranslationJobID, throws TranslationException { log.trace("BootstrapTranslationServiceImpl.getTranslationObjectStatus"); try { - String objectPath = String.format("%s.%s", getObjectPath(translationObject), exportFormat); + String path = getObjectPath(translationObject); + String objectPath = String.format("%s.%s", path, exportFormat); + log.warn("lilt: strTranslationJobID"); int imported = 0; int translated = 0; + int approved = 0; + int rejected = 0; + int complete = 0; SourceFile[] files = liltApiClient.getFiles(strTranslationJobID); - // loop backwards through the list since the most recent files will be at the end. for (SourceFile file : files) { + log.warn("lilt: comparing {} to {}", file.name, objectPath); if (!Objects.equals(file.name, objectPath)) { continue; } @@ -423,20 +475,50 @@ public TranslationStatus getTranslationObjectStatus(String strTranslationJobID, if (file.labels.contains("status=TRANSLATED")) { translated++; } + if (file.labels.contains("approval=APPROVED")) { + approved++; + } + if (file.labels.contains("approval=REJECTED")) { + rejected++; + } + if (file.labels.contains("approval=COMPLETE")) { + rejected++; + } } boolean hasImported = imported > 0; boolean hasTranslated = translated > 0; + boolean hasRejected = rejected > 0; + if (hasImported && hasTranslated && complete >= imported) { + log.warn("lilt: status for {} is COMPLETE", objectPath); + return TranslationStatus.COMPLETE; + } + if (hasImported && hasTranslated && approved >= imported) { + log.warn("lilt: status for {} is APPROVED", objectPath); + return TranslationStatus.APPROVED; + } + if (hasImported && hasTranslated && translated > rejected) { + log.warn("lilt: status for {} is APPROVED", objectPath); + return TranslationStatus.TRANSLATED; + } + if (hasImported && hasTranslated && hasRejected) { + log.warn("lilt: status for {} is REJECTED", objectPath); + return TranslationStatus.REJECTED; + } if (hasImported && hasTranslated) { + log.warn("lilt: status for {} is TRANSLATED", objectPath); return TranslationStatus.TRANSLATED; } if (hasImported) { + log.warn("lilt: status for {} is TRANSLATION_IN_PROGRESS", objectPath); return TranslationStatus.TRANSLATION_IN_PROGRESS; } - // if the object has an extension then it is an image or some sort of asset. we should consider those translated. - Optional maybeExt = getFileExtension(objectPath); + // if the object has an extension then it is an image or some sort of asset. we should consider those complete. + Optional maybeExt = getFileExtension(path); if (maybeExt.isPresent()) { - return TranslationStatus.TRANSLATED; - } + log.warn("lilt: asset status for {} is APPROVED", path); + return TranslationStatus.APPROVED; + } + log.warn("lilt: status for {} is COMMITTED_FOR_TRANSLATION", objectPath); } catch (Exception e) { log.warn("error during getTranslationObjectStatus {}", e); } @@ -471,7 +553,7 @@ public TranslationStatus[] getTranslationObjectsStatus(String strTranslationJobI @Override public CommentCollection getTranslationObjectCommentCollection(String strTranslationJobID, TranslationObject translationObject) throws TranslationException { - log.trace("BootstrapTranslationServiceImpl.getTranslationObjectCommentCollection"); + log.warn("BootstrapTranslationServiceImpl.getTranslationObjectCommentCollection"); throw new TranslationException("This function is not implemented", TranslationException.ErrorCode.SERVICE_NOT_IMPLEMENTED); @@ -479,11 +561,21 @@ public CommentCollection getTranslationObjectCommentCollection(String s @Override public void addTranslationObjectComment(String strTranslationJobID, TranslationObject translationObject, - Comment comment) throws TranslationException { - log.trace("BootstrapTranslationServiceImpl.addTranslationObjectComment"); - - throw new TranslationException("This function is not implemented", - TranslationException.ErrorCode.SERVICE_NOT_IMPLEMENTED); + Comment comment) throws TranslationException { + log.warn("BootstrapTranslationServiceImpl.addTranslationObjectComment"); + String annotation = comment.getAnnotationData(); + String author = comment.getAuthorName(); + String fileName = String.format("%s_%s.txt"); + String objectPath = String.format("%s.%s", getObjectPath(translationObject), exportFormat); + String message = comment.getMessage(); + String body = String.format("%s\n\n%s", objectPath, message); + InputStream inputStream = new ByteArrayInputStream(body.getBytes()); + String labels = String.format("%s,comment", strTranslationJobID); + try { + liltApiClient.uploadFile(fileName, labels, inputStream); + } catch (Exception e) { + log.warn("error during addTranslationObjectComment {}", e); + } } @Override @@ -495,9 +587,8 @@ public void updateTranslationJobMetadata(String strTranslationJobID, Translation TranslationException.ErrorCode.SERVICE_NOT_IMPLEMENTED); } - + private String getObjectPath (TranslationObject translationObject){ - if(translationObject.getTranslationObjectSourcePath()!= null && !translationObject.getTranslationObjectSourcePath().isEmpty()){ return translationObject.getTranslationObjectSourcePath(); } @@ -506,19 +597,19 @@ else if(translationObject.getTitle().equals("TAGMETADATA")){ } else if(translationObject.getTitle().equals("ASSETMETADATA")){ return ASSET_METADATA; - } + } else if(translationObject.getTitle().equals("I18NCOMPONENTSTRINGDICT")){ return I18NCOMPONENTSTRINGDICT; } return null; - } + } public void updateDueDate(String strTranslationJobID, Date date) throws TranslationException { log.debug("NEW DUE DATE:{}",date); // bootstrapTmsService.setTmsJobDuedate(strTranslationJobID, date); } - + private static void unzipFileFromStream(ZipInputStream zipInputStream, String targetPath) throws IOException { File dirFile = new File(targetPath + File.separator); if (!dirFile.exists()) { diff --git a/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/LiltApiClient.java b/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/LiltApiClient.java index 2989a35..9bfc117 100644 --- a/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/LiltApiClient.java +++ b/core/src/main/java/com/adobe/granite/translation/connector/bootstrap/core/impl/LiltApiClient.java @@ -6,11 +6,14 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.net.URISyntaxException; +import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; import com.google.gson.Gson; +import org.apache.http.entity.ContentType; import org.apache.http.entity.InputStreamEntity; +import org.apache.http.entity.StringEntity; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; @@ -100,4 +103,24 @@ public String downloadFile(Integer fileId) throws IOException, URISyntaxExceptio .collect(Collectors.joining("\n")); } } + + public void addLabel(Integer fileId, String label) throws IOException, URISyntaxException { + try (CloseableHttpClient httpclient = HttpClients.createDefault()) { + log.warn("addLabel fileId {}", fileId); + String baseUrl = String.format("%s/files/labels", apiUrl); + URIBuilder req = new URIBuilder(baseUrl); + req.setParameter("id", Integer.toString(fileId)); + req.setParameter("key", apiKey); + HttpPost httppost = new HttpPost(req.build()); + HashMap reqBody = new HashMap<>(); + reqBody.put("name", label); + Gson gson = new Gson(); + String jsonBody = gson.toJson(reqBody); + log.warn("addLabel body {}", jsonBody); + HttpEntity body = new StringEntity(jsonBody, ContentType.APPLICATION_JSON); + httppost.setEntity(body); + log.warn("Executing request {}", httppost.getRequestLine()); + httpclient.execute(httppost); + } + } } diff --git a/pom.xml b/pom.xml index 7082be5..fcd02a3 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ lilt-connector Lilt Translation Connector pom - 2.0-SNAPSHOT + 2.2 Lilt Translation Connector diff --git a/ui.apps/pom.xml b/ui.apps/pom.xml index a598e74..377b0c9 100644 --- a/ui.apps/pom.xml +++ b/ui.apps/pom.xml @@ -23,7 +23,7 @@ com.adobe.granite.translation.connector lilt-connector - 2.0-SNAPSHOT + 2.2 ../pom.xml