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

Commit

Permalink
Remove need to pass in a persistent HTextFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Apr 23, 2014
1 parent 64f2b56 commit 475fde4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
4 changes: 3 additions & 1 deletion zanata-model/src/main/java/org/zanata/model/HTextFlow.java
Expand Up @@ -48,6 +48,7 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.google.common.annotations.VisibleForTesting;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down Expand Up @@ -155,7 +156,8 @@ public Long getId() {
return id;
}

protected void setId(Long id) {
@VisibleForTesting
public void setId(Long id) {
this.id = id;
}

Expand Down
54 changes: 29 additions & 25 deletions zanata-war/src/main/java/org/zanata/dao/TextFlowTargetDAO.java
Expand Up @@ -202,54 +202,58 @@ public Optional<HTextFlowTarget> searchBestMatchTransMemory(

StringBuilder queryStr =
new StringBuilder(
"select match "
+ "from HTextFlowTarget match, HTextFlow textFlow "
+ "where "
+ "textFlow.id = :textFlowId "
+ "and textFlow.contentHash = match.textFlow.contentHash "
+ "and match.locale.localeId = :targetLocaleId "
"select match\n"
+ "from HTextFlowTarget match\n"
+ "where match.textFlow.contentHash = :contentHash\n"
+ "and match.locale.localeId = :targetLocaleId\n"
+
// It's fine to reuse translation in Translated
// state even if it came from a reviewable
// project
"and match.state in (:approvedState, :translatedState) "
"and match.state in (:approvedState, :translatedState)\n"
+
// Do not reuse its own translation
"and match.textFlow != textFlow "
"and match.textFlow.id != :textFlowId\n"
+
// Do not reuse matches from obsolete entities
// (iteration, project)
// Obsolete document translations ARE reused
"and match.textFlow.document.projectIteration.status != :obsoleteEntityStatus "
+ "and match.textFlow.document.projectIteration.project.status != :obsoleteEntityStatus ");
"and match.textFlow.document.projectIteration.status != :obsoleteEntityStatus\n"
+ "and match.textFlow.document.projectIteration.project.status != :obsoleteEntityStatus\n"
);
if (checkContext) {
queryStr.append("and match.textFlow.resId = textFlow.resId ");
queryStr.append("and match.textFlow.resId = :resId\n");
}
if (checkDocument) {
queryStr.append("and match.textFlow.document.docId = textFlow.document.docId ");
queryStr.append("and match.textFlow.document.docId = :docId\n");
}
if (checkProject) {
queryStr.append("and match.textFlow.document.projectIteration.project = textFlow.document.projectIteration.project ");
queryStr.append("and match.textFlow.document.projectIteration.project.id = :projectId\n");
}
queryStr.append("order by (case when match.textFlow.resId = textFlow.resId then 0 else 1 end),"
+ "(case when match.textFlow.document.docId = textFlow.document.docId then 0 else 1 end),"
+ "(case when match.textFlow.document.projectIteration.project = textFlow.document.projectIteration.project then 0 else 1 end),"
+ "match.lastChanged desc");
queryStr.append("order by\n"
+ " (case when match.textFlow.resId = :resId then 0 else 1 end),\n"
+ " (case when match.textFlow.document.docId\n"
+ " = :docId then 0 else 1 end),\n"
+ " (case when match.textFlow.document.projectIteration.project.id\n"
+ " = :projectId then 0 else 1 end),\n"
+ " match.lastChanged desc\n");

Query q = getSession().createQuery(queryStr.toString());

q.setParameter("textFlowId", textFlow.getId())
.setParameter("targetLocaleId", targetLocaleId)
.setParameter("approvedState", ContentState.Approved)
.setParameter("translatedState", ContentState.Translated)
.setParameter("obsoleteEntityStatus", EntityStatus.OBSOLETE);
q.setParameter("textFlowId", textFlow.getId());
q.setParameter("contentHash", textFlow.getContentHash());
q.setParameter("resId", textFlow.getResId());
q.setParameter("docId", textFlow.getDocument().getDocId());
q.setParameter("projectId", textFlow.getDocument().getProjectIteration().getProject().getId());
q.setParameter("targetLocaleId", targetLocaleId);
q.setParameter("approvedState", ContentState.Approved);
q.setParameter("translatedState", ContentState.Translated);
q.setParameter("obsoleteEntityStatus", EntityStatus.OBSOLETE);
q.setCacheable(false); // don't try to cache scrollable results
q.setComment("TextFlowTargetDAO.findMatchingTranslations");
q.setMaxResults(1); // Get the first one (should be the best match
// because of the order by clause)
List results = q.list();
return results.isEmpty() ? Optional.<HTextFlowTarget> absent()
: Optional.of((HTextFlowTarget) results.get(0));
return Optional.fromNullable((HTextFlowTarget) q.uniqueResult());
}

/**
Expand Down
Expand Up @@ -76,6 +76,8 @@
import static com.google.common.collect.Collections2.filter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
Expand Down Expand Up @@ -456,7 +458,7 @@ public int hashCode() {

private List<Object[]> getSearchResult(TransMemoryQuery query,
LocaleId sourceLocale, LocaleId targetLocale,
final Integer maxResult, Class<?>... entities)
final @Nullable Integer maxResult, Class<?>... entities)
throws ParseException {
String queryText = null;
String[] multiQueryText = null;
Expand Down Expand Up @@ -522,8 +524,7 @@ private List<Object[]> getSearchResult(TransMemoryQuery query,
ftQuery.setSort(lastChangedSort);

if (maxResult != null) {
return (List<Object[]>) ftQuery.setMaxResults(maxResult)
.getResultList();
ftQuery.setMaxResults(maxResult);
}
return (List<Object[]>) ftQuery.getResultList();
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.List;

import lombok.ToString;
import org.zanata.webtrans.shared.rpc.HasSearchType.SearchType;

import com.google.gwt.user.client.rpc.IsSerializable;
Expand All @@ -33,6 +34,7 @@
* href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
@ToString
public class TransMemoryQuery implements IsSerializable {
private SearchType searchType;
private List<String> queries;
Expand Down Expand Up @@ -144,6 +146,7 @@ public int hashCode() {
return result;
}

@ToString
public static class Condition implements IsSerializable {
private boolean isCheck;
private String value;
Expand Down

0 comments on commit 475fde4

Please sign in to comment.