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

Commit

Permalink
Merge branch 'release' into integration/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Nov 29, 2012
2 parents 20a5a55 + 8196537 commit e67a57d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
15 changes: 15 additions & 0 deletions zanata-war/src/main/java/org/zanata/dao/DocumentDAO.java
Expand Up @@ -317,6 +317,21 @@ public HDocument getByProjectIterationAndDocId(final String projectSlug, final S
final HDocument doc = (HDocument) q.uniqueResult();
return doc;
}

public List<HDocument> getByProjectIterationAndDocIdList(final String projectSlug, final String iterationSlug, List<String> docIdList)
{
Session session = getSession();
Query q = session.createQuery("from HDocument d where d.projectIteration.slug = :iterationSlug " +
"and d.projectIteration.project.slug = :projectSlug " +
"and d.docId in (:docIdList) " +
"and d.obsolete = false");
q.setParameter("iterationSlug", iterationSlug)
.setParameter("projectSlug", projectSlug)
.setParameterList("docIdList", docIdList);
q.setCacheable(true);
List<HDocument> docs = q.list();
return docs;
}

public List<HDocument> getAllByProjectIteration(final String projectSlug, final String iterationSlug)
{
Expand Down
Expand Up @@ -25,7 +25,7 @@ public class FilterConstraintToQuery
protected static final String STATE_LIST_NAMED_PARAM = "contentStateList";
protected static final String LOCALE_NAMED_PARAM = "locale";
protected static final String DOC_ID_NAMED_PARAM = "docId";
protected static final String DOC_IDS_LIST_NAMED_PARAM = "docIdList";
protected static final String DOC_IDS_LIST_NAMED_PARAM = "documentIds";
private static final String SEARCH_PLACEHOLDER = ":" + SEARCH_NAMED_PARAM;
private static final String STATE_LIST_PLACEHOLDER = ":" + STATE_LIST_NAMED_PARAM;
private static final String LOCALE_PLACEHOLDER = ":" + LOCALE_NAMED_PARAM;
Expand All @@ -36,18 +36,18 @@ public class FilterConstraintToQuery
private final boolean hasSearch;
private String searchString;
private DocumentId documentId;
private Collection<String> docIdList;
private Collection<Long> documentIds;

private FilterConstraintToQuery(FilterConstraints constraints, DocumentId documentId)
{
this(constraints);
this.documentId = documentId;
}

public FilterConstraintToQuery(FilterConstraints constraints, Collection<String> docIdList)
public FilterConstraintToQuery(FilterConstraints constraints, Collection<Long> documentIds)
{
this(constraints);
this.docIdList = docIdList;
this.documentIds = documentIds;
}

private FilterConstraintToQuery(FilterConstraints constraints)
Expand All @@ -67,11 +67,11 @@ public static FilterConstraintToQuery filterInSingleDocument(FilterConstraints c
return new FilterConstraintToQuery(constraints, documentId);
}

public static FilterConstraintToQuery filterInMultipleDocuments(FilterConstraints constraints, Collection<String> docIdList)
public static FilterConstraintToQuery filterInMultipleDocuments(FilterConstraints constraints, Collection<Long> documentIds)
{
Preconditions.checkNotNull(docIdList);
Preconditions.checkState(!docIdList.isEmpty());
return new FilterConstraintToQuery(constraints, docIdList);
Preconditions.checkNotNull(documentIds);
Preconditions.checkState(!documentIds.isEmpty());
return new FilterConstraintToQuery(constraints, documentIds);
}

public String toHQL()
Expand All @@ -83,7 +83,7 @@ public String toHQL()
}
else
{
docIdCondition = "tf.document.docId in (" + DOC_IDS_LIST_PLACEHOLDER + ")";
docIdCondition = "tf.document.id in (" + DOC_IDS_LIST_PLACEHOLDER + ")";
}
String obsoleteCondition = eq("tf.obsolete", "0").toString();
String searchCondition = buildSearchCondition();
Expand Down Expand Up @@ -167,7 +167,7 @@ public Query setQueryParameters(Query textFlowQuery, HLocale hLocale)
}
else
{
textFlowQuery.setParameterList(DOC_IDS_LIST_NAMED_PARAM, docIdList);
textFlowQuery.setParameterList(DOC_IDS_LIST_NAMED_PARAM, documentIds);
}
textFlowQuery.setParameter(LOCALE_NAMED_PARAM, hLocale.getId());
if (hasSearch)
Expand Down
Expand Up @@ -166,19 +166,20 @@ private List<HTextFlow> findTextFlowsByDocumentPaths(WorkspaceId workspace, List
private List<HTextFlow> findTextFlowsWithDatabaseSearch(String projectSlug, String iterationSlug, List<String> documentPaths, FilterConstraints constraints, HLocale hLocale)
{
boolean hasDocumentPaths = documentPaths != null && !documentPaths.isEmpty();
log.info("document paths: {}", documentPaths); //FIXME reduce log level
List<String> docPaths;
log.debug("document paths: {}", documentPaths);
List<HDocument> documents;
if (hasDocumentPaths)
{
docPaths = documentPaths;
// TODO this won't scale. But looks like at the moment documentPaths is sourced from url in org.zanata.webtrans.client.presenter.SearchResultsPresenter.updateViewAndRun so it should be ok.
documents = documentDAO.getByProjectIterationAndDocIdList(projectSlug, iterationSlug, documentPaths);
}
else
{
List<HDocument> allDocuments = documentDAO.getAllByProjectIteration(projectSlug, iterationSlug);
docPaths = Lists.transform(allDocuments, HDocumentToDocId.FUNCTION);
documents = documentDAO.getAllByProjectIteration(projectSlug, iterationSlug);
}
List<Long> documentIds = Lists.transform(documents, HDocumentToId.FUNCTION);

FilterConstraintToQuery toQuery = FilterConstraintToQuery.filterInMultipleDocuments(constraints, docPaths);
FilterConstraintToQuery toQuery = FilterConstraintToQuery.filterInMultipleDocuments(constraints, documentIds);
String hql = toQuery.toHQL();
log.debug("hql for searching: {}", hql);
org.hibernate.Query query = session.createQuery(hql);
Expand Down Expand Up @@ -441,14 +442,14 @@ private static boolean contentIsValid(Collection<String> contents, FilterConstra
return valid;
}

private static enum HDocumentToDocId implements Function<HDocument, String>
private static enum HDocumentToId implements Function<HDocument, Long>
{
FUNCTION;

@Override
public String apply(HDocument input)
public Long apply(HDocument input)
{
return input.getDocId();
return input.getId();
}
}
}
Expand Up @@ -165,11 +165,11 @@ public void testToHQLWithNoCondition()
@Test
public void testToHQLWithNoConditionForMultipleDocuments()
{
FilterConstraintToQuery constraintToQuery = FilterConstraintToQuery.filterInMultipleDocuments(FilterConstraints.keepAll(), Lists.newArrayList("doc.po"));
FilterConstraintToQuery constraintToQuery = FilterConstraintToQuery.filterInMultipleDocuments(FilterConstraints.keepAll(), Lists.newArrayList(1L));

String result = constraintToQuery.toHQL();

assertThat(result, Matchers.equalToIgnoringCase(QUERY_BEFORE_WHERE + "WHERE (tf.obsolete=0 AND tf.document.docId in (:docIdList)) ORDER BY tf.pos"));
assertThat(result, Matchers.equalToIgnoringCase(QUERY_BEFORE_WHERE + "WHERE (tf.obsolete=0 AND tf.document.id in (:documentIds)) ORDER BY tf.pos"));
}

@Test
Expand Down Expand Up @@ -218,7 +218,7 @@ public void testSetParametersForQuery()
public void testSetParametersForQueryWithMultipleDocuments()
{
FilterConstraints constraints = FilterConstraints.filterBy("file").excludeApproved();
List<String> docIdList = Lists.newArrayList("doc1.po", "doc2.po");
List<Long> docIdList = Lists.newArrayList(1L, 2L);
FilterConstraintToQuery constraintToQuery = FilterConstraintToQuery.filterInMultipleDocuments(constraints, docIdList);

constraintToQuery.setQueryParameters(query, hLocale);
Expand Down

0 comments on commit e67a57d

Please sign in to comment.