Revision 8311a989
Added by Katja Luther about 7 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonDaoHibernateImpl.java | ||
---|---|---|
134 | 134 |
public List<TaxonBase> getTaxaByName(boolean doTaxa, boolean doSynonyms, String queryString, MatchMode matchMode, |
135 | 135 |
Integer pageSize, Integer pageNumber) { |
136 | 136 |
|
137 |
return getTaxaByName(doTaxa, doSynonyms, false, false, queryString, null, matchMode, null, null, pageSize, pageNumber, null); |
|
137 |
return getTaxaByName(doTaxa, doSynonyms, false, false, false, queryString, null, matchMode, null, null, pageSize, pageNumber, null);
|
|
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
@Override |
... | ... | |
153 | 153 |
} |
154 | 154 |
|
155 | 155 |
@Override |
156 |
public List<TaxonBase> getTaxaByName(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, |
|
156 |
public List<TaxonBase> getTaxaByName(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, boolean doCommonNames,
|
|
157 | 157 |
boolean includeAuthors, |
158 | 158 |
String queryString, Classification classification, |
159 | 159 |
MatchMode matchMode, Set<NamedArea> namedAreas, NameSearchOrder order, |
... | ... | |
162 | 162 |
boolean doCount = false; |
163 | 163 |
|
164 | 164 |
String searchField = includeAuthors ? "titleCache" : "nameCache"; |
165 |
Query query = prepareTaxaByName(doTaxa, doSynonyms, doMisappliedNames, searchField, queryString, classification, matchMode, namedAreas, order, pageSize, pageNumber, doCount); |
|
165 |
Query query = prepareTaxaByName(doTaxa, doSynonyms, doMisappliedNames, doCommonNames, searchField, queryString, classification, matchMode, namedAreas, order, pageSize, pageNumber, doCount);
|
|
166 | 166 |
|
167 | 167 |
if (query != null){ |
168 | 168 |
@SuppressWarnings("unchecked") |
... | ... | |
182 | 182 |
//new search for the editor, for performance issues the return values are only uuid and titleCache, to avoid the initialisation of all objects |
183 | 183 |
@Override |
184 | 184 |
@SuppressWarnings("unchecked") |
185 |
public List<UuidAndTitleCache<IdentifiableEntity>> getTaxaByNameForEditor(boolean doTaxa, boolean doSynonyms, boolean doNamesWithoutTaxa, boolean doMisappliedNames, String queryString, Classification classification, |
|
185 |
public List<UuidAndTitleCache<IdentifiableEntity>> getTaxaByNameForEditor(boolean doTaxa, boolean doSynonyms, boolean doNamesWithoutTaxa, boolean doMisappliedNames, boolean doCommonNames, String queryString, Classification classification,
|
|
186 | 186 |
MatchMode matchMode, Set<NamedArea> namedAreas) { |
187 | 187 |
// long zstVorher; |
188 | 188 |
// long zstNachher; |
... | ... | |
200 | 200 |
resultObjects.add(new UuidAndTitleCache(TaxonNameBase.class, name.getUuid(), name.getId(), name.getTitleCache())); |
201 | 201 |
} |
202 | 202 |
} |
203 |
if (!doSynonyms && !doTaxa){ |
|
203 |
if (!doSynonyms && !doTaxa && !doCommonNames){
|
|
204 | 204 |
return resultObjects; |
205 | 205 |
} |
206 | 206 |
} |
207 |
Query query = prepareTaxaByNameForEditor(doTaxa, doSynonyms, doMisappliedNames, "nameCache", queryString, classification, matchMode, namedAreas, doCount, order); |
|
207 |
Query query = prepareTaxaByNameForEditor(doTaxa, doSynonyms, doMisappliedNames, doCommonNames, "nameCache", queryString, classification, matchMode, namedAreas, doCount, order);
|
|
208 | 208 |
|
209 | 209 |
|
210 | 210 |
if (query != null){ |
... | ... | |
217 | 217 |
//differentiate taxa and synonyms |
218 | 218 |
// new Boolean(result[3].toString()) is due to the fact that result[3] could be a Boolean ora String |
219 | 219 |
// see FIXME in 'prepareQuery' for more details |
220 |
if (doTaxa && doSynonyms){
|
|
220 |
if (doTaxa || doSynonyms || doCommonNames){
|
|
221 | 221 |
if (result[3].equals("synonym")) { |
222 | 222 |
resultObjects.add( new UuidAndTitleCache(Synonym.class, (UUID) result[0], (Integer) result[1], (String)result[2], new Boolean(result[4].toString()), null)); |
223 | 223 |
} |
224 | 224 |
else { |
225 | 225 |
resultObjects.add( new UuidAndTitleCache(Taxon.class, (UUID) result[0], (Integer) result[1], (String)result[2], new Boolean(result[4].toString()), null)); |
226 | 226 |
} |
227 |
}else if (doTaxa){
|
|
228 |
resultObjects.add( new UuidAndTitleCache(Taxon.class, (UUID) result[0], (Integer) result[1], (String)result[2], new Boolean(result[4].toString()), null)); |
|
227 |
// }else if (doTaxa ){
|
|
228 |
// resultObjects.add( new UuidAndTitleCache(Taxon.class, (UUID) result[0], (Integer) result[1], (String)result[2], new Boolean(result[4].toString()), null));
|
|
229 | 229 |
}else if (doSynonyms){ |
230 | 230 |
resultObjects.add( new UuidAndTitleCache(Synonym.class, (UUID) result[0], (Integer) result[1], (String)result[2], new Boolean(result[4].toString()), null)); |
231 | 231 |
} |
... | ... | |
268 | 268 |
* |
269 | 269 |
* |
270 | 270 |
*/ |
271 |
private Query prepareTaxaByNameForEditor(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, |
|
271 |
private Query prepareTaxaByNameForEditor(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, boolean doCommonNames,
|
|
272 | 272 |
String searchField, String queryString, Classification classification, |
273 | 273 |
MatchMode matchMode, Set<NamedArea> namedAreas, boolean doCount, NameSearchOrder order) { |
274 |
return prepareQuery(doTaxa, doSynonyms, doMisappliedNames, searchField, queryString, |
|
274 |
return prepareQuery(doTaxa, doSynonyms, doMisappliedNames, doCommonNames, searchField, queryString,
|
|
275 | 275 |
classification, matchMode, namedAreas, order, doCount, true); |
276 | 276 |
} |
277 | 277 |
|
... | ... | |
290 | 290 |
* @param clazz |
291 | 291 |
* @return |
292 | 292 |
*/ |
293 |
private Query prepareQuery(boolean doTaxa, boolean doSynonyms, boolean doIncludeMisappliedNames, String searchField, String queryString, |
|
293 |
private Query prepareQuery(boolean doTaxa, boolean doSynonyms, boolean doIncludeMisappliedNames, boolean doCommonNames, String searchField, String queryString,
|
|
294 | 294 |
Classification classification, MatchMode matchMode, Set<NamedArea> namedAreas, NameSearchOrder order, boolean doCount, boolean doNotReturnFullEntities){ |
295 | 295 |
|
296 | 296 |
if (order == null){ |
... | ... | |
304 | 304 |
selectWhat = (doCount ? "count(t)": "t"); |
305 | 305 |
} |
306 | 306 |
|
307 |
/* |
|
308 |
* String what = "select distinct"; |
|
309 |
|
|
310 |
String hql= what + " from Taxon t " + |
|
311 |
"join t.descriptions d "+ |
|
312 |
"join d.descriptionElements e " + |
|
313 |
"join e.feature f " + |
|
314 |
"where f.supportsCommonTaxonName = true and e.name "+matchMode.getMatchOperator()+" :queryString";//and ls.text like 'common%'"; |
|
315 |
|
|
316 |
Query query = getSession().createQuery(hql); |
|
317 |
|
|
318 |
query.setParameter("queryString", matchMode.queryStringFrom(queryString)); |
|
319 |
|
|
320 |
if(pageSize != null && !doCount) { |
|
321 |
query.setMaxResults(pageSize); |
|
322 |
if(pageNumber != null) { |
|
323 |
query.setFirstResult(pageNumber * pageSize); |
|
324 |
} |
|
325 |
} |
|
326 |
return query; |
|
327 |
*/ |
|
328 |
|
|
307 | 329 |
String hql = ""; |
308 | 330 |
Set<NamedArea> areasExpanded = new HashSet<NamedArea>(); |
309 | 331 |
if(namedAreas != null && namedAreas.size() > 0){ |
... | ... | |
323 | 345 |
String taxonSubselect = subSelects[1]; |
324 | 346 |
String synonymSubselect = subSelects[2]; |
325 | 347 |
String misappliedSelect = subSelects[0]; |
348 |
String commonNameSubSelect = subSelects[3]; |
|
326 | 349 |
|
327 | 350 |
|
328 | 351 |
logger.debug("taxonSubselect: " + taxonSubselect != null ? taxonSubselect: "NULL"); |
... | ... | |
331 | 354 |
Query subTaxon = null; |
332 | 355 |
Query subSynonym = null; |
333 | 356 |
Query subMisappliedNames = null; |
357 |
Query subCommonNames = null; |
|
358 |
|
|
334 | 359 |
if(doTaxa){ |
335 | 360 |
// find Taxa |
336 | 361 |
subTaxon = getSession().createQuery(taxonSubselect).setParameter("queryString", hqlQueryString); |
... | ... | |
368 | 393 |
} |
369 | 394 |
} |
370 | 395 |
|
396 |
if(doCommonNames){ |
|
397 |
// find Taxa |
|
398 |
subCommonNames = getSession().createQuery(commonNameSubSelect).setParameter("queryString", hqlQueryString); |
|
399 |
|
|
400 |
if(doAreaRestriction){ |
|
401 |
subCommonNames.setParameterList("namedAreasUuids", namedAreasUuids); |
|
402 |
} |
|
403 |
if(classification != null){ |
|
404 |
subCommonNames.setParameter("classification", classification); |
|
405 |
|
|
406 |
} |
|
407 |
|
|
408 |
|
|
409 |
} |
|
410 |
|
|
371 | 411 |
List<Integer> taxa = new ArrayList<Integer>(); |
372 | 412 |
List<Integer> synonyms = new ArrayList<Integer>(); |
373 | 413 |
if (doSynonyms){ |
... | ... | |
379 | 419 |
if (doIncludeMisappliedNames){ |
380 | 420 |
taxa.addAll(subMisappliedNames.list()); |
381 | 421 |
} |
422 |
if (doCommonNames){ |
|
423 |
taxa.addAll(subCommonNames.list()); |
|
424 |
} |
|
382 | 425 |
|
383 | 426 |
//FIXME : the fourth element of the result should be a boolean, but in the case of a synonym |
384 | 427 |
// (which does require a check) a constant boolean (false) value needs to set. It seems that |
... | ... | |
422 | 465 |
} else{ |
423 | 466 |
hql = "select " + selectWhat + " from %s t"; |
424 | 467 |
} |
425 |
} else if(doTaxa){ |
|
468 |
} else if(doTaxa || doCommonNames){
|
|
426 | 469 |
if (taxa.size()>0){ |
427 | 470 |
hql = "select " + selectWhat; |
428 | 471 |
// in doNotReturnFullEntities mode it is nesscary to also return the type of the matching entities: |
... | ... | |
552 | 595 |
* |
553 | 596 |
* FIXME implement classification restriction & implement test: see {@link TaxonDaoHibernateImplTest#testCountTaxaByName()} |
554 | 597 |
*/ |
555 |
private Query prepareTaxaByName(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, String searchField, String queryString, |
|
598 |
private Query prepareTaxaByName(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, boolean doCommonNames, String searchField, String queryString,
|
|
556 | 599 |
Classification classification, MatchMode matchMode, Set<NamedArea> namedAreas, NameSearchOrder order, Integer pageSize, Integer pageNumber, boolean doCount) { |
557 | 600 |
|
558 |
Query query = prepareQuery(doTaxa, doSynonyms, doMisappliedNames, searchField, queryString, classification, matchMode, namedAreas, order, doCount, false); |
|
601 |
Query query = prepareQuery(doTaxa, doSynonyms, doMisappliedNames, doCommonNames, searchField, queryString, classification, matchMode, namedAreas, order, doCount, false);
|
|
559 | 602 |
|
560 | 603 |
if(pageSize != null && !doCount && query != null) { |
561 | 604 |
query.setMaxResults(pageSize); |
... | ... | |
570 | 613 |
private Query prepareTaxaByCommonName(String queryString, Classification classification, |
571 | 614 |
MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageSize, Integer pageNumber, boolean doCount, boolean doNotReturnFullEntities){ |
572 | 615 |
|
573 |
String what = "select"; |
|
616 |
String what = "select distinct";
|
|
574 | 617 |
if (doNotReturnFullEntities){ |
575 | 618 |
what += " t.uuid, t.id, t.titleCache, \'taxon\', case when t.taxonNodes is empty and t.relationsFromThisTaxon is empty and t.relationsToThisTaxon is empty then true else false end "; |
576 | 619 |
}else { |
... | ... | |
596 | 639 |
} |
597 | 640 |
|
598 | 641 |
@Override |
599 |
public long countTaxaByName(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, |
|
642 |
public long countTaxaByName(boolean doTaxa, boolean doSynonyms, boolean doMisappliedNames, boolean doCommonNames,
|
|
600 | 643 |
boolean doIncludeAuthors, String queryString, Classification classification, |
601 | 644 |
MatchMode matchMode, Set<NamedArea> namedAreas) { |
602 | 645 |
|
... | ... | |
612 | 655 |
*/ |
613 | 656 |
String searchField = doIncludeAuthors ? "titleCache": "nameCache"; |
614 | 657 |
|
615 |
Query query = prepareTaxaByName(doTaxa, doSynonyms, doMisappliedNames, searchField, queryString, classification, matchMode, namedAreas, null, null, null, doCount); |
|
658 |
Query query = prepareTaxaByName(doTaxa, doSynonyms, doMisappliedNames, doCommonNames, searchField, queryString, classification, matchMode, namedAreas, null, null, null, doCount);
|
|
616 | 659 |
if (query != null) { |
617 | 660 |
return (Long)query.uniqueResult(); |
618 | 661 |
}else{ |
... | ... | |
701 | 744 |
public List<TaxonBase> findByNameTitleCache(boolean doTaxa, boolean doSynonyms, String queryString, Classification classification, MatchMode matchMode, Set<NamedArea> namedAreas, NameSearchOrder order, Integer pageNumber, Integer pageSize, List<String> propertyPaths) { |
702 | 745 |
|
703 | 746 |
boolean doCount = false; |
704 |
Query query = prepareTaxaByName(doTaxa, doSynonyms, false, "titleCache", queryString, classification, matchMode, namedAreas, order, pageSize, pageNumber, doCount); |
|
747 |
Query query = prepareTaxaByName(doTaxa, doSynonyms, false, false, "titleCache", queryString, classification, matchMode, namedAreas, order, pageSize, pageNumber, doCount);
|
|
705 | 748 |
if (query != null){ |
706 | 749 |
List<TaxonBase> results = query.list(); |
707 | 750 |
defaultBeanInitializer.initializeAll(results, propertyPaths); |
... | ... | |
1457 | 1500 |
" left join rt.name as n2" + |
1458 | 1501 |
" left join rft.type as rtype"; |
1459 | 1502 |
|
1503 |
String doCommonNamesJoin = "join t.descriptions d "+ |
|
1504 |
"join d.descriptionElements e " + |
|
1505 |
"join e.feature f "; |
|
1506 |
|
|
1507 |
|
|
1460 | 1508 |
String doClassificationWhere = " tn.classification = :classification"; |
1461 | 1509 |
String doClassificationForMisappliedNamesWhere = " tn2 .classification = :classification"; |
1462 | 1510 |
|
1463 | 1511 |
String doAreaRestrictionWhere = " e.area.uuid in (:namedAreasUuids)"; |
1512 |
String doCommonNamesRestrictionWhere = " f.supportsCommonTaxonName = true and e.name "+matchMode.getMatchOperator()+" :queryString"; |
|
1464 | 1513 |
|
1465 | 1514 |
String doSearchFieldWhere = "%s." + searchField + " " + matchMode.getMatchOperator() + " :queryString"; |
1466 | 1515 |
|
... | ... | |
1469 | 1518 |
String taxonSubselect = null; |
1470 | 1519 |
String synonymSubselect = null; |
1471 | 1520 |
String misappliedSelect = null; |
1521 |
String commonNameSubselect = null; |
|
1472 | 1522 |
|
1473 | 1523 |
if(classification != null ){ |
1474 | 1524 |
if (!doIncludeMisappliedNames){ |
... | ... | |
1481 | 1531 |
" WHERE " + doAreaRestrictionWhere + |
1482 | 1532 |
" AND " + doClassificationWhere + |
1483 | 1533 |
" AND " + String.format(doSearchFieldWhere, "sn"); |
1534 |
commonNameSubselect = doCommonNamesJoin + |
|
1535 |
" WHERE " + doAreaRestrictionWhere + " AND " + doClassificationWhere + |
|
1536 |
" AND " + String.format(doSearchFieldWhere, "n") |
|
1537 |
+ "AND " + doCommonNamesRestrictionWhere; |
|
1484 | 1538 |
} else { |
1485 | 1539 |
taxonSubselect = String.format(doTaxonSubSelect, "t" )+ doTaxonNameJoin + |
1486 | 1540 |
" WHERE " + doClassificationWhere + |
... | ... | |
1488 | 1542 |
synonymSubselect = String.format(doTaxonSubSelect, "s" ) + doSynonymNameJoin + |
1489 | 1543 |
" WHERE " + doClassificationWhere + |
1490 | 1544 |
" AND " + String.format(doSearchFieldWhere, "sn"); |
1545 |
commonNameSubselect =String.format(doTaxonSubSelect, "s" )+ doCommonNamesJoin + |
|
1546 |
" WHERE "+ doAreaRestrictionWhere + |
|
1547 |
" AND " + doClassificationWhere + |
|
1548 |
"AND " + doCommonNamesRestrictionWhere; |
|
1491 | 1549 |
} |
1492 | 1550 |
}else{ //misappliedNames included |
1493 | 1551 |
if(doAreaRestriction){ |
1494 |
misappliedSelect = String.format(doAreaRestrictionMisappliedNameSubSelect, "t") + doTaxonNameJoin + doMisappliedNamesJoin + |
|
1552 |
misappliedSelect = String.format(doAreaRestrictionMisappliedNameSubSelect, "t") + doTaxonNameJoin + doMisappliedNamesJoin +
|
|
1495 | 1553 |
" WHERE " + doAreaRestrictionWhere + |
1496 | 1554 |
" AND " + String.format(doSearchFieldWhere, "n") + |
1497 | 1555 |
" AND " + doClassificationForMisappliedNamesWhere + |
... | ... | |
1503 | 1561 |
|
1504 | 1562 |
synonymSubselect = String.format(doAreaRestrictionSubSelect, "s") + doSynonymNameJoin + |
1505 | 1563 |
" WHERE " + doAreaRestrictionWhere + |
1506 |
" AND " + doClassificationWhere + " AND " + String.format(doSearchFieldWhere, "sn");;
|
|
1564 |
" AND " + doClassificationWhere + " AND " + String.format(doSearchFieldWhere, "sn"); |
|
1507 | 1565 |
|
1566 |
commonNameSubselect= String.format(doAreaRestrictionSubSelect, "t")+ doCommonNamesJoin + |
|
1567 |
" WHERE " + doAreaRestrictionWhere + |
|
1568 |
" AND "+ doClassificationWhere + "AND " + doCommonNamesRestrictionWhere; |
|
1508 | 1569 |
} else { |
1509 | 1570 |
misappliedSelect = String.format(doTaxonMisappliedNameSubSelect, "t" ) + doTaxonNameJoin + doMisappliedNamesJoin + |
1510 | 1571 |
" WHERE " + String.format(doSearchFieldWhere, "n") + |
... | ... | |
1519 | 1580 |
" WHERE " + doClassificationWhere + |
1520 | 1581 |
" AND " + String.format(doSearchFieldWhere, "sn"); |
1521 | 1582 |
|
1583 |
commonNameSubselect= String.format(doTaxonSubSelect, "t")+ doCommonNamesJoin + |
|
1584 |
" WHERE " + doClassificationWhere + "AND " + doCommonNamesRestrictionWhere; |
|
1585 |
|
|
1522 | 1586 |
} |
1523 | 1587 |
} |
1524 | 1588 |
} else { |
... | ... | |
1535 | 1599 |
synonymSubselect = String.format(doAreaRestrictionSubSelect, "s") + doSynonymNameJoin + |
1536 | 1600 |
" WHERE " + doAreaRestrictionWhere + |
1537 | 1601 |
" AND " + String.format(doSearchFieldWhere, "sn"); |
1602 |
commonNameSubselect = String.format(doTaxonSubSelect, "t")+ doCommonNamesJoin + |
|
1603 |
" WHERE " + doAreaRestrictionWhere + |
|
1604 |
"AND " + doCommonNamesRestrictionWhere; |
|
1538 | 1605 |
|
1539 | 1606 |
|
1540 | 1607 |
} else { |
1541 | 1608 |
misappliedSelect = String.format(doTaxonMisappliedNameSubSelect, "t" ) + doTaxonNameJoin + doMisappliedNamesJoin + " WHERE " + String.format(doSearchFieldWhere, "n") + " AND " + doRelationshipTypeComparison; |
1542 | 1609 |
taxonSubselect = String.format(doTaxonSubSelect, "t" ) + doTaxonNameJoin + " WHERE " + String.format(doSearchFieldWhere, "n"); |
1543 | 1610 |
synonymSubselect = String.format(doTaxonSubSelect, "s" ) + doSynonymNameJoin + " WHERE " + String.format(doSearchFieldWhere, "sn"); |
1611 |
commonNameSubselect = String.format(doTaxonSubSelect, "t" ) +doCommonNamesJoin + " WHERE "+ doCommonNamesRestrictionWhere; |
|
1544 | 1612 |
|
1545 | 1613 |
} |
1546 | 1614 |
} |
1547 |
String[] result = {misappliedSelect, taxonSubselect, synonymSubselect}; |
|
1615 |
String[] result = {misappliedSelect, taxonSubselect, synonymSubselect, commonNameSubselect};
|
|
1548 | 1616 |
|
1549 | 1617 |
return result; |
1550 | 1618 |
} |
Also available in: Unified diff
fix #6296: include the common name search into the search query of taxa and synonyms