Skip to content

Commit

Permalink
ZNTA-1865 move method and remove from versions list check
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jun 22, 2017
1 parent 75642f6 commit 494e5ea
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 72 deletions.
Expand Up @@ -49,10 +49,6 @@ List<TranslationService.TranslationResult> executeMerge(
Future<List<TranslationService.TranslationResult>> executeMergeAsync(TransMemoryMergeRequest request,
TransMemoryMergeTaskHandle asyncTaskHandle);

void translateInBatch(HasTMMergeCriteria mergeCriteria,
List<HTextFlow> textFlows,
HLocale targetLocale, List<Long> fromProjectVersionIds);

/**
* TM merge for a project version
* @param versionId
Expand Down
Expand Up @@ -211,69 +211,6 @@ public Future<List<TranslationService.TranslationResult>> executeMergeAsync(Tran
return AsyncTaskResult.completed(translationResults);
}

/**
* This method will run in transaction and manages its own transaction.
*
* @param request
* TM merge request
* @param textFlows
* the text flows to be filled
* @param targetLocale
* target locale
* @return translation results
*/
private List<TranslationService.TranslationResult> translateInBatch(
TransMemoryMergeRequest request, List<HTextFlow> textFlows,
HLocale targetLocale) {

if (textFlows.isEmpty()) {
return Collections.emptyList();
}
try {
return transactionUtil.call(() -> {
boolean checkContext =
request.getDifferentContextRule() == MergeRule.REJECT;
boolean checkDocument =
request.getDifferentDocumentRule() == MergeRule.REJECT;
boolean checkProject =
request.getDifferentProjectRule() == MergeRule.REJECT;

List<TransUnitUpdateRequest> updateRequests = Lists.newLinkedList();
for (HTextFlow hTextFlow : textFlows) {
HTextFlowTarget hTextFlowTarget =
hTextFlow.getTargets().get(targetLocale.getId());
Optional<TransMemoryResultItem> tmResult =
translationMemoryServiceImpl.searchBestMatchTransMemory(
hTextFlow, targetLocale.getLocaleId(),
hTextFlow.getDocument().getLocale().getLocaleId(),
checkContext, checkDocument, checkProject,
request.getThresholdPercent(),
Collections.emptyList());
if (tmResult.isPresent()) {
TransUnitUpdateRequest updateRequest =
createRequest(request, targetLocale,
hTextFlow, tmResult.get(), hTextFlowTarget);

if (updateRequest != null) {
updateRequests.add(updateRequest);
textFlowTargetUpdateContextEvent
.fire(new TextFlowTargetUpdateContextEvent(
updateRequest.getTransUnitId(),
request.localeId,
request.editorClientId,
TransUnitUpdated.UpdateType.NonEditorSave));
}
}
}
return translationServiceImpl.translate(
request.localeId, updateRequests);
});
} catch (Exception e) {
log.error("exception during TM merge", e);
return Collections.emptyList();
}
}

@Async
@Override
public Future<Void> startMergeTranslations(Long targetVersionId,
Expand Down Expand Up @@ -304,10 +241,6 @@ public Future<Void> startMergeTranslations(Long targetVersionId,
.contains(targetLocale))
.map(ModelEntityBase::getId).collect(Collectors.toList());

if (fromVersionIds.isEmpty()) {
log.error("Cannot find source versions of {}", fromVersionIds);
return AsyncTaskResult.completed();
}

long mergeTargetCount = textFlowDAO.getUntranslatedOrFuzzyTextFlowCountInVersion(
targetVersion.getId(), targetLocale);
Expand Down Expand Up @@ -347,8 +280,70 @@ public Future<Void> startMergeTranslations(Long targetVersionId,
return AsyncTaskResult.completed();
}

/**
* This method will run in transaction and manages its own transaction.
*
* @param request
* TM merge request
* @param textFlows
* the text flows to be filled
* @param targetLocale
* target locale
* @return translation results
*/
private List<TranslationService.TranslationResult> translateInBatch(
TransMemoryMergeRequest request, List<HTextFlow> textFlows,
HLocale targetLocale) {

if (textFlows.isEmpty()) {
return Collections.emptyList();
}
try {
return transactionUtil.call(() -> {
boolean checkContext =
request.getDifferentContextRule() == MergeRule.REJECT;
boolean checkDocument =
request.getDifferentDocumentRule() == MergeRule.REJECT;
boolean checkProject =
request.getDifferentProjectRule() == MergeRule.REJECT;

List<TransUnitUpdateRequest> updateRequests = Lists.newLinkedList();
for (HTextFlow hTextFlow : textFlows) {
HTextFlowTarget hTextFlowTarget =
hTextFlow.getTargets().get(targetLocale.getId());
Optional<TransMemoryResultItem> tmResult =
translationMemoryServiceImpl.searchBestMatchTransMemory(
hTextFlow, targetLocale.getLocaleId(),
hTextFlow.getDocument().getLocale().getLocaleId(),
checkContext, checkDocument, checkProject,
request.getThresholdPercent(),
Collections.emptyList());
if (tmResult.isPresent()) {
TransUnitUpdateRequest updateRequest =
createRequest(request, targetLocale,
hTextFlow, tmResult.get(), hTextFlowTarget);

if (updateRequest != null) {
updateRequests.add(updateRequest);
textFlowTargetUpdateContextEvent
.fire(new TextFlowTargetUpdateContextEvent(
updateRequest.getTransUnitId(),
request.localeId,
request.editorClientId,
TransUnitUpdated.UpdateType.NonEditorSave));
}
}
}
return translationServiceImpl.translate(
request.localeId, updateRequests);
});
} catch (Exception e) {
log.error("exception during TM merge", e);
return Collections.emptyList();
}
}

// TODO pahuang this duplicated most part of above method
@Override
public void translateInBatch(HasTMMergeCriteria mergeCriteria,
List<HTextFlow> textFlows, HLocale targetLocale,
List<Long> fromProjectVersions) {
Expand Down

0 comments on commit 494e5ea

Please sign in to comment.