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

Commit

Permalink
Add unit test for merge translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Mar 6, 2015
1 parent fd06390 commit f870517
Show file tree
Hide file tree
Showing 11 changed files with 545 additions and 123 deletions.
Expand Up @@ -910,13 +910,6 @@ public String decodeDocId(String docId) {
return UrlUtil.decodeString(docId);
}

@In
private MergeTranslationsManager mergeTranslationsManager;

public void test() {
mergeTranslationsManager.startMergeTranslations("abrt", "test16", "abrt", "test1", true);
}

public void uploadTranslationFile(HLocale hLocale) {
identity.checkPermission("modify-translation", hLocale, getVersion()
.getProject());
Expand Down
39 changes: 22 additions & 17 deletions zanata-war/src/main/java/org/zanata/dao/TextFlowTargetDAO.java
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;
import java.util.List;

import com.google.common.collect.Lists;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
Expand Down Expand Up @@ -490,38 +491,43 @@ public List<HTextFlowTarget[]> getTranslationsByMatchedContext(Long sourceVersio
Long targetVersionId, int offset, int maxResults, Collection<ContentState> states) {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder
.append("select * from HTextFlowTarget tft, HTextFlowTarget tft2 ")
.append("where tft.textFlow.document.version.id = :sourceVersionId ")
.append("and tft2.textFlow.document.version.id = :targetVersionId")
.append("and tft.textFlow.obsolete = false ")
.append("and tft.textFlow.document.obsolete = false ")
.append("and tft.state in :states ")
.append("and tft2.textFlow.obsolete = false ")
.append("and tft2.textFlow.document.obsolete = false ")
.append("and tft.textFlow.contentHash = tft2.textFlow.contentHash ")
.append("and tft.textFlow.document.docId = tft2.textFlow.document.docId");
.append("select tft, tft2 from HTextFlowTarget tft, HTextFlowTarget tft2 ")
.append("where tft.textFlow.document.projectIteration.id = :sourceVersionId ")
.append("and tft2.textFlow.document.projectIteration.id = :targetVersionId ")
.append("and tft.textFlow.obsolete = false ")
.append("and tft.textFlow.document.obsolete = false ")
.append("and tft.state in :states ")
.append("and tft2.textFlow.obsolete = false ")
.append("and tft2.textFlow.document.obsolete = false ")
.append("and tft.textFlow.contentHash = tft2.textFlow.contentHash ")
.append("and tft.textFlow.document.docId = tft2.textFlow.document.docId");

Query query = getSession()
.createQuery(queryBuilder.toString())
.setParameter("sourceVersionId", sourceVersionId)
.setParameter("targetVersionId", targetVersionId)
.setParameter("states", states)
.setParameterList("states", states)
.setMaxResults(maxResults)
.setFirstResult(offset)
.setCacheable(true)
.setComment("TextFlowTargetDAO.getTranslationsByMatchedContext");

List<HTextFlowTarget[]> results = query.list();
List<HTextFlowTarget[]> results = Lists.newArrayList();
for(Object result: query.list()) {
Object[] castResults = (Object[])result;
results.add(new HTextFlowTarget[] { (HTextFlowTarget) castResults[0],
(HTextFlowTarget) castResults[1] });
}
return results;
}

public int getTranslationsByMatchedContextCount(Long sourceVersionId,
Long targetVersionId, Collection<ContentState> states) {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder
.append("select count(*) from HTextFlowTarget tft, HTextFlowTarget tft2 ")
.append("where tft.textFlow.document.version.id = :sourceVersionId ")
.append("and tft2.textFlow.document.version.id = :targetVersionId")
.append("select count(tft.id) from HTextFlowTarget tft, HTextFlowTarget tft2 ")
.append("where tft.textFlow.document.projectIteration.id = :sourceVersionId ")
.append("and tft2.textFlow.document.projectIteration.id = :targetVersionId ")
.append("and tft.textFlow.obsolete = false ")
.append("and tft.textFlow.document.obsolete = false ")
.append("and tft.state in :states ")
Expand All @@ -534,10 +540,9 @@ public int getTranslationsByMatchedContextCount(Long sourceVersionId,
.createQuery(queryBuilder.toString())
.setParameter("sourceVersionId", sourceVersionId)
.setParameter("targetVersionId", targetVersionId)
.setParameter("states", states)
.setParameterList("states", states)
.setCacheable(true)
.setComment("TextFlowTargetDAO.getTranslationsByMatchedContextCount");

Long count = (Long) query.uniqueResult();
return count == null ? 0 : count.intValue();
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
import org.zanata.service.TranslationFinder;
import org.zanata.service.ValidationService;
import org.zanata.service.VersionStateCache;
import org.zanata.util.MessageGenerator;
import org.zanata.webtrans.shared.model.ValidationAction;
import com.google.common.base.Optional;
import com.google.common.base.Stopwatch;
Expand Down Expand Up @@ -179,23 +180,6 @@ protected Integer work() throws Exception {
return numCopied;
}

private String createComment(HTextFlowTarget target) {
String author;
HDocument document = target.getTextFlow().getDocument();
String projectname =
document.getProjectIteration().getProject().getName();
String version = document.getProjectIteration().getSlug();
String documentid = document.getDocId();
if (target.getLastModifiedBy() != null) {
author = ", author " + target.getLastModifiedBy().getName();
} else {
author = "";
}

return "translation auto-copied from project " + projectname
+ ", version " + version + ", document " + documentid + author;
}

private void saveCopyTransMatch(final HTextFlowTarget matchingTarget,
final HTextFlow originalTf, final HCopyTransOptions options,
final boolean requireTranslationReview) {
Expand Down Expand Up @@ -264,12 +248,15 @@ public Boolean get() {
}
hTarget.setContents(matchingTarget.getContents());
hTarget.setState(copyState);
HSimpleComment hcomment = hTarget.getComment();
if (hcomment == null) {
hcomment = new HSimpleComment();
hTarget.setComment(hcomment);
HSimpleComment hComment = hTarget.getComment();
if (hComment == null) {
hComment = new HSimpleComment();
hTarget.setComment(hComment);
}
hcomment.setComment(createComment(matchingTarget));
hComment.setComment(matchingTarget.getComment().getComment());

hTarget.setRevisionComment(MessageGenerator
.getCopyTransMessage(matchingTarget));

// TODO Maybe we should think about registering a Hibernate
// integrator for these updates
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HRawDocument;
import org.zanata.model.HSimpleComment;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.HTextFlowTargetHistory;
Expand All @@ -33,6 +34,7 @@
import org.zanata.service.CopyVersionService;
import org.zanata.service.VersionStateCache;
import org.zanata.util.JPACopier;
import org.zanata.util.MessageGenerator;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -350,6 +352,10 @@ JPACopier.<HTextFlowTarget> copyBean(tft, "textFlow",
"reviewComments", "history");
copy.setTextFlow(newTf);
copy.setTextFlowRevision(newTf.getRevision());
if(tft.getComment() != null) {
copy.setComment(new HSimpleComment(tft.getComment().getComment()));
}
copy.setRevisionComment(MessageGenerator.getCopyVersionMessage(tft));

// copy review comment
copy.setReviewComments(Lists
Expand Down
Expand Up @@ -37,12 +37,13 @@
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.dao.TextFlowTargetDAO;
import org.zanata.model.HProjectIteration;
import org.zanata.model.HTextFlowTarget;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.MergeTranslationsService;

import com.google.common.base.Optional;
import com.google.common.base.Stopwatch;
import org.zanata.service.TranslationStateCache;
import org.zanata.service.VersionStateCache;

/**
* Service provider for merge translations task.
Expand All @@ -65,6 +66,13 @@ public class MergeTranslationsServiceImpl implements MergeTranslationsService {
@In
private ZanataIdentity identity;

@In
private VersionStateCache versionStateCacheImpl;

@In
private TranslationStateCache translationStateCacheImpl;


private final static int TRANSLATION_BATCH_SIZE = 50;

@Override
Expand All @@ -76,13 +84,25 @@ public Future<Void> startMergeTranslations(String sourceProjectSlug,
HProjectIteration sourceVersion =
projectIterationDAO.getBySlug(sourceProjectSlug, sourceVersionSlug);

if (sourceVersion == null) {
log.error("Cannot find source version of {}:{}", sourceProjectSlug,
sourceVersionSlug);
return null;
}

HProjectIteration targetVersion =
projectIterationDAO.getBySlug(targetProjectSlug, targetVersionSlug);

if(!verifyVersions(sourceVersion, targetVersion)) {
if (targetVersion == null) {
log.error("Cannot find target version of {}:{}", targetProjectSlug,
targetVersionSlug);
return null;
}

if(isVersionsEmpty(sourceVersion, targetVersion)) {
return null;
}

Optional<MergeTranslationsTaskHandle> taskHandleOpt =
Optional.fromNullable(handle);

Expand Down Expand Up @@ -113,57 +133,47 @@ public Future<Void> startMergeTranslations(String sourceProjectSlug,
startCount += TRANSLATION_BATCH_SIZE;
}

versionStateCacheImpl.clearVersionStatsCache(targetVersion.getId());
log.info("merge translation end: from {} to {}, {}", sourceProjectSlug
+ ":" + sourceVersionSlug, targetProjectSlug + ":"
+ targetVersionSlug, overallStopwatch);

return AsyncTaskResult.taskResult();
}

private int mergeTranslationBatch(Long sourceVersionId,
protected int mergeTranslationBatch(Long sourceVersionId,
Long targetVersionId, boolean useLatestTranslatedString,
int offset, int batchSize) {
try {
return new MergeTranslationsWork(sourceVersionId, targetVersionId,
offset, batchSize, useLatestTranslatedString,
textFlowTargetDAO).workInTransaction();
textFlowTargetDAO, translationStateCacheImpl).workInTransaction();
} catch (Exception e) {
log.warn("exception during copy text flow target", e);
return 0;
}
}

/**
* Check if sourceVersion or targetVersion exists and there's document in
* both
* Check if sourceVersion or targetVersion has source document.
*
* @param sourceVersion
* @param targetVersion
* @return
*/
private boolean verifyVersions(HProjectIteration sourceVersion,
private boolean isVersionsEmpty(HProjectIteration sourceVersion,
HProjectIteration targetVersion) {
if (sourceVersion == null) {
log.error("Cannot find source version of {}:{}", sourceVersion
.getProject().getSlug(), sourceVersion.getSlug());
return false;
}
if (targetVersion == null) {
log.error("Cannot find target version of {}:{}", targetVersion
.getProject().getSlug(), targetVersion.getSlug());
return false;
}
if(sourceVersion.getDocuments().isEmpty()) {
log.error("No documents in source version {}:{}", sourceVersion
.getProject().getSlug(), sourceVersion.getSlug());
return false;
return true;
}
if(targetVersion.getDocuments().isEmpty()) {
log.error("No documents in target version {}:{}", targetVersion
.getProject().getSlug(), targetVersion.getSlug());
return false;
return true;
}
return true;
return false;
}

private void prepareMergeTranslationsHandle(
Expand Down

0 comments on commit f870517

Please sign in to comment.