iterationSlug));
TermQuery localeQuery =
new TermQuery(new Term(IndexFieldLabels.LOCALE_ID_FIELD,
localeId.getId()));
MultiPhraseQuery documentsQuery = new MultiPhraseQuery();
if (documentPaths != null && !documentPaths.isEmpty()) {
ArrayList<Term> docPathTerms = new ArrayList<Term>();
for (String s : documentPaths) {
docPathTerms
.add(new Term(IndexFieldLabels.DOCUMENT_ID_FIELD, s));
}
documentsQuery.add(docPathTerms.toArray(new Term[docPathTerms
.size()]));
}
List<HTextFlow> resultList = new ArrayList<HTextFlow>();
if (constraints.isSearchInTarget()) {
// Content query for target
String targetAnalyzerName =
TextContainerAnalyzerDiscriminator
.getAnalyzerDefinitionName(localeId.getId());
Analyzer targetAnalyzer =
entityManager.getSearchFactory().getAnalyzer(
targetAnalyzerName);
Query tgtContentPhraseQuery;
QueryParser contentQueryParser =
new MultiFieldQueryParser(Version.LUCENE_29,
IndexFieldLabels.CONTENT_FIELDS, targetAnalyzer);
try {
tgtContentPhraseQuery =
contentQueryParser.parse("\""
+ QueryParser.escape(constraints
.getSearchString()) + "\"");
} catch (ParseException e) {
throw new ZanataServiceException("Failed to parse query", e);
}
// Target Query
BooleanQuery targetQuery = new BooleanQuery();
targetQuery.add(projectQuery, Occur.MUST);
targetQuery.add(iterationQuery, Occur.MUST);
targetQuery.add(tgtContentPhraseQuery, Occur.MUST);
if (documentsQuery.getTermArrays().size() > 0) {
targetQuery.add(documentsQuery, Occur.MUST);
}
targetQuery.add(localeQuery, Occur.MUST);
if (!constraints.getIncludedStates().hasTranslated()) {
TermQuery approvedStateQuery =
new TermQuery(new Term(
IndexFieldLabels.CONTENT_STATE_FIELD,
ContentState.Approved.toString()));
targetQuery.add(approvedStateQuery, Occur.MUST_NOT);
}
if (!constraints.getIncludedStates().hasFuzzy()) {
TermQuery approvedStateQuery =
new TermQuery(new Term(
IndexFieldLabels.CONTENT_STATE_FIELD,
ContentState.NeedReview.toString()));
targetQuery.add(approvedStateQuery, Occur.MUST_NOT);
}
if (!constraints.getIncludedStates().hasNew()) {
TermQuery approvedStateQuery =
new TermQuery(new Term(
IndexFieldLabels.CONTENT_STATE_FIELD,
ContentState.New.toString()));
targetQuery.add(approvedStateQuery, Occur.MUST_NOT);
}
FullTextQuery ftQuery =
entityManager.createFullTextQuery(targetQuery,
HTextFlowTarget.class);
@SuppressWarnings("unchecked")
List<HTextFlowTarget> matchedTargets =
(List<HTextFlowTarget>) ftQuery.getResultList();
log.info("got {} HTextFLowTarget results", matchedTargets.size());
for (HTextFlowTarget htft : matchedTargets) {
// manually check for case sensitive matches
if (!constraints.isCaseSensitive()
|| (contentIsValid(htft.getContents(), constraints))) {
if (!htft.getTextFlow().getDocument().isObsolete()) {
resultList.add(htft.getTextFlow());
}
}
}
}
if (constraints.isSearchInSource()) {
// Source locale
// NB: Assume the first document's locale, or the same target locale
// if there are no documents
// TODO Move source locale to the Project iteration level
LocaleId sourceLocaleId = localeId;
HProjectIteration projectIteration =
projectIterationDAO.getBySlug(projectSlug, iterationSlug);
if (!projectIteration.getDocuments().isEmpty()) {
sourceLocaleId =
projectIteration.getDocuments().values().iterator()
.next().getLocale().getLocaleId();
}
// Content query for source
String sourceAnalyzerName =
TextContainerAnalyzerDiscriminator
.getAnalyzerDefinitionName(sourceLocaleId.getId());
Analyzer sourceAnalyzer =
entityManager.getSearchFactory().getAnalyzer(
sourceAnalyzerName);
Query srcContentPhraseQuery;
QueryParser srcContentQueryParser =
new MultiFieldQueryParser(Version.LUCENE_29,
IndexFieldLabels.CONTENT_FIELDS, sourceAnalyzer);
try {
srcContentPhraseQuery =
srcContentQueryParser.parse("\""
+ QueryParser.escape(constraints
.getSearchString()) + "\"");
} catch (ParseException e) {
throw new ZanataServiceException("Failed to parse query", e);
}
// Source Query
BooleanQuery sourceQuery = new BooleanQuery();
sourceQuery.add(projectQuery, Occur.MUST);
sourceQuery.add(iterationQuery, Occur.MUST);
sourceQuery.add(srcContentPhraseQuery, Occur.MUST);
if (documentsQuery.getTermArrays().size() > 0) {
sourceQuery.add(documentsQuery, Occur.MUST);
}
FullTextQuery ftQuery =
entityManager.createFullTextQuery(sourceQuery,