Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Extract anonymous Work class into SaveBatchWork
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Nov 15, 2013
1 parent 2af33c6 commit d6d0ce8
Showing 1 changed file with 155 additions and 127 deletions.
Expand Up @@ -30,6 +30,10 @@
import javax.annotation.Nonnull;
import javax.persistence.EntityManager;

import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -624,133 +628,17 @@ protected Boolean work() throws Exception {

for (final List<TextFlowTarget> batch : batches) {
try {
changed |= new Work<Boolean>() {
@Override
protected Boolean work() throws Exception {
boolean changed = false;

for (TextFlowTarget incomingTarget : batch) {
String resId = incomingTarget.getResId();
String sourceHash = incomingTarget.getSourceHash();
HTextFlow textFlow =
textFlowDAO.getById(document, resId);
if (textFlow == null) {
// return warning for unknown resId to caller
String warning =
"Could not find TextFlow for TextFlowTarget "
+ resId + " with contents: "
+ incomingTarget.getContents();
warnings.add(warning);
log.warn(
"skipping TextFlowTarget with unknown resId: {}",
resId);
} else if (sourceHash != null && !sourceHash.equals(textFlow.getContentHash())) {
String warning = MessageFormat.format(
"TextFlowTarget {0} may be obsolete; "
+ "associated source hash: {1}; "
+ "expected hash is {2} for source: {3}",
resId,
sourceHash,
textFlow.getContentHash(),
textFlow.getContents());
warnings.add(warning);
log.warn(
"skipping TextFlowTarget {} with unknown sourceHash: {}",
resId, sourceHash);
} else {
HProjectIteration iterationReloaded =
projectIterationDAO
.findById(hProjectIteration
.getId());
String validationMessage =
validateTranslations(
incomingTarget.getState(),
iterationReloaded,
incomingTarget.getResId(),
textFlow.getContents(),
incomingTarget.getContents());

if (!StringUtils.isEmpty(validationMessage)) {
warnings.add(validationMessage);
log.warn(validationMessage);
continue;
}

int nPlurals = getNumPlurals(hLocale, textFlow);
HTextFlowTarget hTarget =
textFlowTargetDAO.getTextFlowTarget(
textFlow, hLocale);

if (mergeType == MergeType.IMPORT) {
removedTargets.remove(hTarget);
}

TranslationMergeServiceFactory.MergeContext mergeContext =
new TranslationMergeServiceFactory.MergeContext(
mergeType, textFlow, hLocale,
hTarget, nPlurals);
TranslationMergeService mergeService =
translationMergeServiceFactory
.getMergeService(mergeContext);

boolean targetChanged =
mergeService.merge(incomingTarget,
hTarget, extensions);
if (hTarget == null) {
// in case hTarget was null, we need to
// retrieve it after merge
hTarget =
textFlow.getTargets().get(
hLocale.getId());
}
targetChanged |=
adjustContentsAndState(hTarget,
nPlurals, warnings);
// update translation information if applicable
if (targetChanged) {
hTarget.setVersionNum(hTarget
.getVersionNum() + 1);

changed = true;
Long actorId;
if (incomingTarget.getTranslator() != null) {
String email =
incomingTarget.getTranslator()
.getEmail();
HPerson hPerson =
personDAO.findByEmail(email);
if (hPerson == null) {
hPerson = new HPerson();
hPerson.setEmail(email);
hPerson.setName(incomingTarget
.getTranslator().getName());
personDAO.makePersistent(hPerson);
}
hTarget.setTranslator(hPerson);
hTarget.setLastModifiedBy(hPerson);
actorId = hPerson.getId();
} else {
hTarget.setTranslator(null);
hTarget.setLastModifiedBy(null);
actorId = null;
}
textFlowTargetDAO.makePersistent(hTarget);
signalPostTranslateEvent(actorId, hTarget);
}
}

personDAO.flush();
textFlowTargetDAO.flush();
personDAO.clear();
textFlowTargetDAO.clear();
if (handleOp.isPresent()) {
handleOp.get().increaseProgress(1);
}
}

return changed;
}
}.workInTransaction();
SaveBatchWork work = new SaveBatchWork();
work.setExtensions(extensions);
work.setWarnings(warnings);
work.setLocale(hLocale);
work.setDocument(document);
work.setMergeType(mergeType);
work.setRemovedTargets(removedTargets);
work.setHandleOp(handleOp);
work.setProjectIterationId(hProjectIteration.getId());
work.setBatch(batch);
changed |= work.workInTransaction();
} catch (Exception e) {
throw new ZanataServiceException("Error during translation.",
500, e);
Expand Down Expand Up @@ -806,6 +694,146 @@ private int getNumPlurals(HLocale hLocale, HTextFlow textFlow) {
return nPlurals;
}

@Getter
@Setter
private final class SaveBatchWork extends Work<Boolean> {
private Set<String> extensions;
private List<String> warnings;
private HLocale locale;
private HDocument document;
private MergeType mergeType;
private Collection<HTextFlowTarget> removedTargets;
private Optional<AsyncTaskHandle> handleOp;
private Long projectIterationId;
private List<TextFlowTarget> batch;

@Override
protected Boolean work() throws Exception {
boolean changed = false;

for (TextFlowTarget incomingTarget : batch) {
String resId = incomingTarget.getResId();
String sourceHash = incomingTarget.getSourceHash();
HTextFlow textFlow =
textFlowDAO.getById(document, resId);
if (textFlow == null) {
// return warning for unknown resId to caller
String warning =
"Could not find TextFlow for TextFlowTarget "
+ resId + " with contents: "
+ incomingTarget.getContents();
warnings.add(warning);
log.warn(
"skipping TextFlowTarget with unknown resId: {}",
resId);
} else if (sourceHash != null && !sourceHash.equals(textFlow.getContentHash())) {
String warning = MessageFormat.format(
"TextFlowTarget {0} may be obsolete; "
+ "associated source hash: {1}; "
+ "expected hash is {2} for source: {3}",
resId,
sourceHash,
textFlow.getContentHash(),
textFlow.getContents());
warnings.add(warning);
log.warn(
"skipping TextFlowTarget {} with unknown sourceHash: {}",
resId, sourceHash);
} else {
// we need a fresh object in this session,
// so that it can lazily load associated objects
HProjectIteration iterationReloaded =
projectIterationDAO.findById(projectIterationId);
String validationMessage =
validateTranslations(
incomingTarget.getState(),
iterationReloaded,
incomingTarget.getResId(),
textFlow.getContents(),
incomingTarget.getContents());

if (!StringUtils.isEmpty(validationMessage)) {
warnings.add(validationMessage);
log.warn(validationMessage);
continue;
}

int nPlurals = getNumPlurals(locale, textFlow);
HTextFlowTarget hTarget =
textFlowTargetDAO.getTextFlowTarget(
textFlow, locale);

if (mergeType == MergeType.IMPORT) {
removedTargets.remove(hTarget);
}

TranslationMergeServiceFactory.MergeContext mergeContext =
new TranslationMergeServiceFactory.MergeContext(
mergeType, textFlow, locale,
hTarget, nPlurals);
TranslationMergeService mergeService =
translationMergeServiceFactory
.getMergeService(mergeContext);

boolean targetChanged =
mergeService.merge(incomingTarget,
hTarget, extensions);
if (hTarget == null) {
// in case hTarget was null, we need to
// retrieve it after merge
hTarget =
textFlow.getTargets().get(
locale.getId());
}
targetChanged |=
adjustContentsAndState(hTarget,
nPlurals, warnings);
// update translation information if applicable
if (targetChanged) {
hTarget.setVersionNum(hTarget
.getVersionNum() + 1);

changed = true;
Long actorId;
if (incomingTarget.getTranslator() != null) {
String email =
incomingTarget.getTranslator()
.getEmail();
HPerson hPerson =
personDAO.findByEmail(email);
if (hPerson == null) {
hPerson = new HPerson();
hPerson.setEmail(email);
hPerson.setName(incomingTarget
.getTranslator().getName());
personDAO.makePersistent(hPerson);
}
hTarget.setTranslator(hPerson);
hTarget.setLastModifiedBy(hPerson);
actorId = hPerson.getId();
} else {
hTarget.setTranslator(null);
hTarget.setLastModifiedBy(null);
actorId = null;
}
textFlowTargetDAO.makePersistent(hTarget);
signalPostTranslateEvent(actorId, hTarget);
}
}

personDAO.flush();
textFlowTargetDAO.flush();
personDAO.clear();
textFlowTargetDAO.clear();
if (handleOp.isPresent()) {
handleOp.get().increaseProgress(1);
}
}

return changed;
}
}

public static class TranslationResultImpl implements TranslationResult {
private HTextFlowTarget translatedTextFlowTarget;
private boolean isSuccess;
Expand Down

0 comments on commit d6d0ce8

Please sign in to comment.