Skip to content

Commit

Permalink
ZNTA-1865 - can merge translation from TM
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jun 12, 2017
1 parent 15be01c commit 716af8e
Show file tree
Hide file tree
Showing 23 changed files with 602 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TransMemoryQuery implements IsSerializable, Serializable {
private Condition project;
private Condition document;
private Condition res;
private List<Long> fromVersionIds;
private Condition includeOwnTranslation = new Condition(true, null);

@SuppressWarnings("unused")
Expand All @@ -67,11 +68,13 @@ public TransMemoryQuery(List<String> queries, SearchType searchType) {
}

public TransMemoryQuery(List<String> queries, SearchType searchType,
Condition project, Condition document, Condition res) {
Condition project, Condition document, Condition res,
List<Long> fromVersionIds) {
this(queries, searchType);
this.project = project;
this.document = document;
this.res = res;
this.fromVersionIds = fromVersionIds;
}

public TransMemoryQuery(String query, SearchType searchType,
Expand Down Expand Up @@ -110,6 +113,10 @@ public SearchType getSearchType() {
return searchType;
}

public List<Long> getFromVersionIds() {
return fromVersionIds;
}

@Override
public String toString() {
return "TransMemoryQuery{" + "searchType=" + searchType + ", queries="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public enum MatchType {
// The optional origin identifiers for this result (i.e. A Trans memory name)
private List<String> origins;
private ArrayList<Long> sourceIdList = new ArrayList<Long>();
private Long fromVersionId;

// for GWT
@SuppressWarnings("unused")
Expand All @@ -65,14 +66,17 @@ private TransMemoryResultItem() {
* @param targetContents
* @param relevanceScore
* @param similarityPercent
* @param fromVersionId
*/
public TransMemoryResultItem(ArrayList<String> sourceContents,
ArrayList<String> targetContents, MatchType matchType,
double relevanceScore, double similarityPercent) {
double relevanceScore, double similarityPercent,
Long fromVersionId) {
super(relevanceScore, similarityPercent);
this.sourceContents = sourceContents;
this.targetContents = targetContents;
this.matchType = matchType;
this.fromVersionId = fromVersionId;
this.origins = new ArrayList<String>();
}

Expand Down Expand Up @@ -134,4 +138,7 @@ public void addSourceId(Long sourceId) {
this.sourceIdList.add(sourceId);
}

public Long getFromVersionId() {
return fromVersionId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2017, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.webtrans.shared.rest.dto;

import java.util.List;

import org.zanata.webtrans.shared.model.ProjectIterationId;
import org.zanata.webtrans.shared.rpc.MergeRule;

public interface HasTMMergeCriteria {
int getThresholdPercent();

MergeRule getDifferentProjectRule();

MergeRule getDifferentDocumentRule();

MergeRule getDifferentContextRule();

MergeRule getImportedMatchRule();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
package org.zanata.webtrans.shared.rest.dto;

import java.util.Collections;
import java.util.List;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.zanata.common.LocaleId;
import org.zanata.webtrans.shared.auth.EditorClientId;
Expand All @@ -30,7 +33,7 @@
/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class TransMemoryMergeRequest {
public class TransMemoryMergeRequest implements HasTMMergeCriteria {
@SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",
justification = "For future implement")
public EditorClientId editorClientId;
Expand All @@ -48,6 +51,7 @@ public class TransMemoryMergeRequest {
public MergeRule differentDocumentRule;
public MergeRule differentContextRule;
public MergeRule importedMatchRule;
private List<ProjectIterationId> fromProjectVersions;

public TransMemoryMergeRequest(
EditorClientId editorClientId,
Expand All @@ -71,23 +75,37 @@ public TransMemoryMergeRequest(
public TransMemoryMergeRequest() {
}

@Override
public int getThresholdPercent() {
return thresholdPercent;
}

@Override
public MergeRule getDifferentProjectRule() {
return differentProjectRule;
}

@Override
public MergeRule getDifferentDocumentRule() {
return differentDocumentRule;
}

@Override
public MergeRule getDifferentContextRule() {
return differentContextRule;
}

@Override
public MergeRule getImportedMatchRule() {
return importedMatchRule;
}

public void setFromProjectVersions(
List<ProjectIterationId> fromProjectVersions) {
this.fromProjectVersions = fromProjectVersions;
}

public List<ProjectIterationId> getFromProjectVersions() {
return fromProjectVersions;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package org.zanata.action;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;

import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.AsyncTaskHandleManager;
import org.zanata.async.handle.MergeTranslationsTaskHandle;
import org.zanata.common.LocaleId;
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.MergeTranslationsService;
import org.zanata.webtrans.shared.model.ProjectIterationId;
import org.zanata.webtrans.shared.rest.dto.TransMemoryMergeRequest;

/**
* Manages tasks to copy translations from one existing version to another.
Expand Down Expand Up @@ -88,6 +97,26 @@ public boolean isRunning(String projectSlug, String versionSlug) {
return handle != null && !handle.isDone();
}

public boolean start(HProjectIteration version, HLocale hLocale,
TransMemoryMergeRequest mergeRequest) {
MergeTranslationTaskKey key =
new MergeTranslationTaskKey(version.getId(),
hLocale.getLocaleId());
AsyncTaskHandle handleByKey =
asyncTaskHandleManager.getHandleByKey(key);
if (handleByKey == null || handleByKey.isCancelled()
|| handleByKey.isDone()) {
MergeTranslationsTaskHandle handle = new MergeTranslationsTaskHandle();

handle.setTriggeredBy(identity.getAccountUsername());
asyncTaskHandleManager.registerTaskHandle(handle, key);
mergeTranslationsServiceImpl.startMergeTranslations(version, hLocale,
mergeRequest, handle);
return true;
}
return false;
}

/**
* Key used for copy version task
*/
Expand Down Expand Up @@ -148,4 +177,32 @@ public Key(final String projectSlug, final String versionSlug) {
this.versionSlug = versionSlug;
}
}


static class MergeTranslationTaskKey implements Serializable {

private static final long serialVersionUID = 5671982177725183233L;
private final Long versionId;
private final LocaleId localeId;

public MergeTranslationTaskKey(Long versionId, LocaleId localeId) {
this.versionId = versionId;
this.localeId = localeId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MergeTranslationTaskKey that = (MergeTranslationTaskKey) o;
return Objects.equals(versionId, that.versionId) &&
Objects.equals(localeId, that.localeId);
}

@Override
public int hashCode() {
return Objects
.hash(versionId, localeId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
*/
public class MergeTranslationsTaskHandle extends AsyncTaskHandle<Void> {

private int totalTranslations;
private long totalTranslations;
private String cancelledBy;
private long cancelledTime;
private String triggeredBy;

public int getTotalTranslations() {
public long getTotalTranslations() {
return this.totalTranslations;
}

public void setTotalTranslations(final int totalTranslations) {
public void setTotalTranslations(final long totalTranslations) {
this.totalTranslations = totalTranslations;
}

Expand Down
35 changes: 35 additions & 0 deletions server/zanata-war/src/main/java/org/zanata/dao/TextFlowDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,41 @@ public int getSourceByMatchedContextCount(Long sourceVersionId,
return count == null ? 0 : count.intValue();
}

public long getUntranslatedOrFuzzyTextFlowCountInVersion(Long targetVersionId, HLocale targetLocale) {
String queryString =
"select count(*) from HTextFlow tf left join " +
"tf.targets tfts WITH index(tfts) =:locale" +
" WHERE (tf.obsolete=0 AND tf.document.projectIteration.id=:targetVersionId ) AND" +
" ( EXISTS ( FROM HTextFlowTarget WHERE ((textFlow=tf AND locale.id=:locale) AND state = :untranslatedState)) OR (:locale not in indices(tf.targets) ))";
Query query = getSession().createQuery(queryString)
.setParameter("locale", targetLocale.getId())
.setParameter("targetVersionId", targetVersionId)
.setParameter("untranslatedState", ContentState.New)
.setCacheable(true)
.setComment("TextFlowDAO.getUntranslatedOrFuzzyTextFlowCountInVersion");
Long count = (Long) query.uniqueResult();
return count == null ? 0 : count;
}

public List<HTextFlow> getUntranslatedOrFuzzyTextFlowsInVersion(
Long targetVersionId,
HLocale targetLocale, int startIndex, int maxCount) {
String queryString =
"select distinct tf from HTextFlow tf left join " +
"tf.targets tfts WITH index(tfts) =:locale" +
" WHERE (tf.obsolete=0 AND tf.document.projectIteration.id=:targetVersionId ) AND" +
" ( EXISTS ( FROM HTextFlowTarget WHERE ((textFlow=tf AND locale.id=:locale) AND state = :untranslatedState)) OR (:locale not in indices(tf.targets) ))";

Query query = getSession().createQuery(queryString)
.setParameter("locale", targetLocale.getId())
.setParameter("targetVersionId", targetVersionId)
.setParameter("untranslatedState", ContentState.New)
.setFirstResult(startIndex).setMaxResults(maxCount)
.setCacheable(true)
.setComment("TextFlowDAO.getUntranslatedOrFuzzyTextFlowsInVersion");
return query.list();
}

/**
* Generate query string for text flows that have matching document id and
* content between the given source and target version
Expand Down
Loading

0 comments on commit 716af8e

Please sign in to comment.