2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.io
.IOException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
19 import java
.util
.Optional
;
21 import java
.util
.UUID
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.apache
.lucene
.index
.Term
;
25 import org
.apache
.lucene
.sandbox
.queries
.FuzzyLikeThisQuery
;
26 import org
.apache
.lucene
.search
.BooleanClause
.Occur
;
27 import org
.apache
.lucene
.search
.BooleanQuery
;
28 import org
.apache
.lucene
.search
.BooleanQuery
.Builder
;
29 import org
.apache
.lucene
.search
.TopDocs
;
30 import org
.apache
.lucene
.search
.WildcardQuery
;
31 import org
.hibernate
.criterion
.Criterion
;
32 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
33 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
34 import org
.springframework
.stereotype
.Service
;
35 import org
.springframework
.transaction
.annotation
.Transactional
;
37 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
38 import eu
.etaxonomy
.cdm
.api
.service
.config
.NameDeletionConfigurator
;
39 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
40 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
41 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.AbstractPagerImpl
;
42 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
43 import eu
.etaxonomy
.cdm
.api
.service
.search
.DocumentSearchResult
;
44 import eu
.etaxonomy
.cdm
.api
.service
.search
.ILuceneIndexToolProvider
;
45 import eu
.etaxonomy
.cdm
.api
.service
.search
.ISearchResultBuilder
;
46 import eu
.etaxonomy
.cdm
.api
.service
.search
.LuceneParseException
;
47 import eu
.etaxonomy
.cdm
.api
.service
.search
.LuceneSearch
;
48 import eu
.etaxonomy
.cdm
.api
.service
.search
.QueryFactory
;
49 import eu
.etaxonomy
.cdm
.api
.service
.search
.SearchResult
;
50 import eu
.etaxonomy
.cdm
.api
.service
.search
.SearchResultBuilder
;
51 import eu
.etaxonomy
.cdm
.api
.utility
.TaxonNamePartsFilter
;
52 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
53 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
54 import eu
.etaxonomy
.cdm
.model
.CdmBaseType
;
55 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
56 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
57 import eu
.etaxonomy
.cdm
.model
.common
.ReferencedEntityBase
;
58 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
59 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
.Direction
;
60 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
61 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
62 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
63 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationshipType
;
64 import eu
.etaxonomy
.cdm
.model
.name
.INonViralName
;
65 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
66 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
67 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignation
;
68 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
69 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
70 import eu
.etaxonomy
.cdm
.model
.name
.Registration
;
71 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
72 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
73 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
74 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
75 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
76 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmGenericDao
;
77 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IOrderedTermVocabularyDao
;
78 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IReferencedEntityDao
;
79 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ITermVocabularyDao
;
80 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.IHomotypicalGroupDao
;
81 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.INomenclaturalStatusDao
;
82 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.ITaxonNameDao
;
83 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.ITypeDesignationDao
;
84 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNameParts
;
85 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
86 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
87 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
88 import eu
.etaxonomy
.cdm
.strategy
.cache
.TaggedText
;
89 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
90 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
94 @Transactional(readOnly
= true)
95 public class NameServiceImpl
96 extends IdentifiableServiceBase
<TaxonName
,ITaxonNameDao
>
97 implements INameService
{
98 static private final Logger logger
= Logger
.getLogger(NameServiceImpl
.class);
101 protected ITermVocabularyDao vocabularyDao
;
103 protected IOrderedTermVocabularyDao orderedVocabularyDao
;
105 @Qualifier("refEntDao")
106 protected IReferencedEntityDao
<ReferencedEntityBase
> referencedEntityDao
;
108 private INomenclaturalStatusDao nomStatusDao
;
110 private ITypeDesignationDao typeDesignationDao
;
112 private IHomotypicalGroupDao homotypicalGroupDao
;
114 private ICdmGenericDao genericDao
;
116 private ILuceneIndexToolProvider luceneIndexToolProvider
;
121 public NameServiceImpl(){}
123 //********************* METHODS ****************************************************************//
126 @Transactional(readOnly
= false)
127 public DeleteResult
delete(UUID nameUUID
){
128 NameDeletionConfigurator config
= new NameDeletionConfigurator();
129 DeleteResult result
= delete(nameUUID
, config
);
134 public DeleteResult
delete(TaxonName name
){
135 return delete(name
.getUuid());
139 @Transactional(readOnly
= false)
140 public DeleteResult
delete(TaxonName name
, NameDeletionConfigurator config
) {
141 DeleteResult result
= new DeleteResult();
149 result
= this.isDeletable(name
, config
);
151 result
.addException(e
);
156 //remove references to this name
157 removeNameRelationshipsByDeleteConfig(name
, config
);
159 //remove name from homotypical group
160 HomotypicalGroup homotypicalGroup
= name
.getHomotypicalGroup();
161 if (homotypicalGroup
!= null){
162 homotypicalGroup
.removeTypifiedName(name
, false);
165 //all type designation relationships are removed as they belong to the name
166 deleteTypeDesignation(name
, null);
167 // //type designations
168 // if (! name.getTypeDesignations().isEmpty()){
169 // String message = "Name can't be deleted as it has types. Remove types prior to deletion.";
170 // throw new ReferrencedObjectUndeletableException(message);
175 result
.addDeletedObject(name
);
177 result
.addException(e
);
187 @Transactional(readOnly
= false)
188 public DeleteResult
delete(UUID nameUUID
, NameDeletionConfigurator config
) {
190 TaxonName name
= dao
.load(nameUUID
);
191 return delete(name
, config
);
196 public DeleteResult
deleteTypeDesignation(TaxonName name
, TypeDesignationBase typeDesignation
){
197 if(typeDesignation
!= null && typeDesignation
.isPersited()){
198 typeDesignation
= HibernateProxyHelper
.deproxy(referencedEntityDao
.load(typeDesignation
.getUuid()), TypeDesignationBase
.class);
201 DeleteResult result
= new DeleteResult();
202 if (name
== null && typeDesignation
== null){
205 }else if (name
!= null && typeDesignation
!= null){
206 removeSingleDesignation(name
, typeDesignation
);
207 }else if (name
!= null){
208 @SuppressWarnings("rawtypes")
209 Set
<TypeDesignationBase
> designationSet
= new HashSet
<>(name
.getTypeDesignations());
210 for (TypeDesignationBase
<?
> desig
: designationSet
){
211 desig
= CdmBase
.deproxy(desig
);
212 removeSingleDesignation(name
, desig
);
214 }else if (typeDesignation
!= null){
215 @SuppressWarnings("unchecked")
216 Set
<TaxonName
> nameSet
= new HashSet
<>(typeDesignation
.getTypifiedNames());
217 for (TaxonName singleName
: nameSet
){
218 singleName
= CdmBase
.deproxy(singleName
);
219 removeSingleDesignation(singleName
, typeDesignation
);
222 result
.addDeletedObject(typeDesignation
);
223 result
.addUpdatedObject(name
);
229 @Transactional(readOnly
= false)
230 public DeleteResult
deleteTypeDesignation(UUID nameUuid
, UUID typeDesignationUuid
){
231 TaxonName nameBase
= load(nameUuid
);
232 TypeDesignationBase
<?
> typeDesignation
= HibernateProxyHelper
.deproxy(referencedEntityDao
.load(typeDesignationUuid
), TypeDesignationBase
.class);
233 return deleteTypeDesignation(nameBase
, typeDesignation
);
238 * @param typeDesignation
241 private void removeSingleDesignation(TaxonName name
, TypeDesignationBase typeDesignation
) {
242 name
.removeTypeDesignation(typeDesignation
);
243 if (typeDesignation
.getTypifiedNames().isEmpty()){
244 typeDesignation
.removeType();
245 if (!typeDesignation
.getRegistrations().isEmpty()){
246 for(Object reg
: typeDesignation
.getRegistrations()){
247 if (reg
instanceof Registration
){
248 ((Registration
)reg
).removeTypeDesignation(typeDesignation
);
252 typeDesignationDao
.delete(typeDesignation
);
263 private void removeNameRelationshipsByDeleteConfig(TaxonName name
, NameDeletionConfigurator config
) {
265 if (config
.isRemoveAllNameRelationships()){
266 Set
<NameRelationship
> rels
= getModifiableSet(name
.getNameRelations());
267 for (NameRelationship rel
: rels
){
268 name
.removeNameRelationship(rel
);
271 //relations to this name
272 Set
<NameRelationship
> rels
= getModifiableSet(name
.getRelationsToThisName());
273 for (NameRelationship rel
: rels
){
274 if (config
.isIgnoreHasBasionym() && NameRelationshipType
.BASIONYM().equals(rel
.getType() )){
275 name
.removeNameRelationship(rel
);
276 }else if (config
.isIgnoreHasReplacedSynonym() && NameRelationshipType
.REPLACED_SYNONYM().equals(rel
.getType())){
277 name
.removeNameRelationship(rel
);
280 //relations from this name
281 rels
= getModifiableSet(name
.getRelationsFromThisName());
282 for (NameRelationship rel
: rels
){
283 if (config
.isIgnoreIsBasionymFor() && NameRelationshipType
.BASIONYM().equals(rel
.getType()) ){
284 name
.removeNameRelationship(rel
);
285 }else if (config
.isIgnoreIsReplacedSynonymFor() && NameRelationshipType
.REPLACED_SYNONYM().equals(rel
.getType())){
286 name
.removeNameRelationship(rel
);
291 } catch (Exception e
) {
292 throw new RuntimeException(e
);
300 private Set
<NameRelationship
> getModifiableSet(Set
<NameRelationship
> relations
) {
301 Set
<NameRelationship
> rels
= new HashSet
<NameRelationship
>();
302 for (NameRelationship rel
: relations
){
308 //********************* METHODS ****************************************************************//
312 * TODO candidate for harmonization
313 * new name findByName
317 public List
<TaxonName
> getNamesByNameCache(String nameCache
){
318 boolean includeAuthors
= false;
319 List result
= dao
.findByName(includeAuthors
, nameCache
, MatchMode
.EXACT
, null, null, null, null);
324 * TODO candidate for harmonization
325 * new name saveHomotypicalGroups
330 public List
<TaxonName
> findNamesByTitleCache(String titleCache
, MatchMode matchMode
, List
<String
> propertyPaths
){
331 List result
= dao
.findByTitle(titleCache
, matchMode
, null, null, null, propertyPaths
);
336 * TODO candidate for harmonization
337 * new name saveHomotypicalGroups
342 public List
<TaxonName
> findNamesByNameCache(String nameCache
, MatchMode matchMode
, List
<String
> propertyPaths
){
343 List
<TaxonName
> result
= dao
.findByName(false, nameCache
, matchMode
, null, null, null , propertyPaths
);
348 public Pager
<TaxonNameParts
> findTaxonNameParts(Optional
<String
> genusOrUninomial
,
349 Optional
<String
> infraGenericEpithet
, Optional
<String
> specificEpithet
,
350 Optional
<String
> infraSpecificEpithet
, Rank rank
, Set
<UUID
> excludedNamesUuids
,
351 Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
) {
354 long count
= dao
.countTaxonNameParts(genusOrUninomial
, infraGenericEpithet
, specificEpithet
, infraGenericEpithet
, rank
, excludedNamesUuids
);
356 List
<TaxonNameParts
> results
;
357 if(AbstractPagerImpl
.hasResultsInRange(count
, pageIndex
, pageSize
)){
358 results
= dao
.findTaxonNameParts(genusOrUninomial
, infraGenericEpithet
, specificEpithet
, infraSpecificEpithet
,
359 rank
, excludedNamesUuids
,
360 pageSize
, pageIndex
, orderHints
);
362 results
= new ArrayList
<>();
365 return new DefaultPagerImpl
<TaxonNameParts
>(pageIndex
, count
, pageSize
, results
);
372 public Pager
<TaxonNameParts
> findTaxonNameParts(TaxonNamePartsFilter filter
, String namePartQueryString
,
373 Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
) {
375 return findTaxonNameParts(
376 filter
.uninomialQueryString(namePartQueryString
),
377 filter
.infraGenericEpithet(namePartQueryString
),
378 filter
.specificEpithet(namePartQueryString
),
379 filter
.infraspecificEpithet(namePartQueryString
),
381 filter
.getExludedNamesUuids(),
382 pageSize
, pageIndex
, orderHints
);
386 * TODO candidate for harmonization
387 * new name saveHomotypicalGroups
390 @Transactional(readOnly
= false)
391 public Map
<UUID
, HomotypicalGroup
> saveAllHomotypicalGroups(Collection
<HomotypicalGroup
> homotypicalGroups
){
392 return homotypicalGroupDao
.saveAll(homotypicalGroups
);
396 * TODO candidate for harmonization
397 * new name saveTypeDesignations
400 @Transactional(readOnly
= false)
401 public Map
<UUID
, TypeDesignationBase
> saveTypeDesignationAll(Collection
<TypeDesignationBase
> typeDesignationCollection
){
402 return typeDesignationDao
.saveAll(typeDesignationCollection
);
406 * TODO candidate for harmonization
407 * new name saveReferencedEntities
410 @Transactional(readOnly
= false)
411 public Map
<UUID
, ReferencedEntityBase
> saveReferencedEntitiesAll(Collection
<ReferencedEntityBase
> referencedEntityCollection
){
412 return referencedEntityDao
.saveAll(referencedEntityCollection
);
416 * TODO candidate for harmonization
417 * new name getNomenclaturalStatus
420 public List
<NomenclaturalStatus
> getAllNomenclaturalStatus(int limit
, int start
){
421 return nomStatusDao
.list(limit
, start
);
425 * TODO candidate for harmonization
426 * new name getTypeDesignations
429 public List
<TypeDesignationBase
> getAllTypeDesignations(int limit
, int start
){
430 return typeDesignationDao
.getAllTypeDesignations(limit
, start
);
434 public TypeDesignationBase
loadTypeDesignation(int id
, List
<String
> propertyPaths
){
435 return typeDesignationDao
.load(id
, propertyPaths
);
439 public TypeDesignationBase
loadTypeDesignation(UUID uuid
, List
<String
> propertyPaths
){
440 return typeDesignationDao
.load(uuid
, propertyPaths
);
444 * FIXME Candidate for harmonization
445 * homotypicalGroupService.list
448 public List
<HomotypicalGroup
> getAllHomotypicalGroups(int limit
, int start
){
449 return homotypicalGroupDao
.list(limit
, start
);
453 * FIXME Candidate for harmonization
458 public List
<RelationshipBase
> getAllRelationships(int limit
, int start
){
459 return dao
.getAllRelationships(limit
, start
);
466 protected void setDao(ITaxonNameDao dao
) {
471 public Pager
<HybridRelationship
> getHybridNames(INonViralName name
, HybridRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
472 Integer numberOfResults
= dao
.countHybridNames(name
, type
);
474 List
<HybridRelationship
> results
= new ArrayList
<HybridRelationship
>();
475 if(AbstractPagerImpl
.hasResultsInRange(numberOfResults
.longValue(), pageNumber
, pageSize
)) { // no point checking again
476 results
= dao
.getHybridNames(name
, type
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
479 return new DefaultPagerImpl
<HybridRelationship
>(pageNumber
, numberOfResults
, pageSize
, results
);
483 public List
<NameRelationship
> listNameRelationships(TaxonName name
, Direction direction
, NameRelationshipType type
, Integer pageSize
,
484 Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
486 Integer numberOfResults
= dao
.countNameRelationships(name
, direction
, type
);
488 List
<NameRelationship
> results
= new ArrayList
<NameRelationship
>();
489 if (AbstractPagerImpl
.hasResultsInRange(numberOfResults
.longValue(), pageNumber
, pageSize
)) { // no point checking again
490 results
= dao
.getNameRelationships(name
, direction
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
496 protected LuceneSearch
prepareFindByFuzzyNameSearch(Class
<?
extends CdmBase
> clazz
,
500 List
<Language
> languages
,
501 boolean highlightFragments
) {
502 String similarity
= Float
.toString(accuracy
);
503 String searchSuffix
= "~" + similarity
;
506 Builder finalQueryBuilder
= new Builder();
507 finalQueryBuilder
.setDisableCoord(false);
508 Builder textQueryBuilder
= new Builder();
509 textQueryBuilder
.setDisableCoord(false);
511 LuceneSearch luceneSearch
= new LuceneSearch(luceneIndexToolProvider
, TaxonName
.class);
512 QueryFactory queryFactory
= luceneIndexToolProvider
.newQueryFactoryFor(TaxonName
.class);
514 // SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};
515 // luceneSearch.setSortFields(sortFields);
517 // ---- search criteria
518 luceneSearch
.setCdmTypRestriction(clazz
);
520 FuzzyLikeThisQuery fltq
= new FuzzyLikeThisQuery(maxNoOfResults
, luceneSearch
.getAnalyzer());
521 if(nvn
.getGenusOrUninomial() != null && !nvn
.getGenusOrUninomial().equals("")) {
522 fltq
.addTerms(nvn
.getGenusOrUninomial().toLowerCase(), "genusOrUninomial", accuracy
, 3);
524 //textQuery.add(new RegexQuery (new Term ("genusOrUninomial", "^[a-zA-Z]*")), Occur.MUST_NOT);
525 textQueryBuilder
.add(queryFactory
.newTermQuery("genusOrUninomial", "_null_", false), Occur
.MUST
);
528 if(nvn
.getInfraGenericEpithet() != null && !nvn
.getInfraGenericEpithet().equals("")){
529 fltq
.addTerms(nvn
.getInfraGenericEpithet().toLowerCase(), "infraGenericEpithet", accuracy
, 3);
531 //textQuery.add(new RegexQuery (new Term ("infraGenericEpithet", "^[a-zA-Z]*")), Occur.MUST_NOT);
532 textQueryBuilder
.add(queryFactory
.newTermQuery("infraGenericEpithet", "_null_", false), Occur
.MUST
);
535 if(nvn
.getSpecificEpithet() != null && !nvn
.getSpecificEpithet().equals("")){
536 fltq
.addTerms(nvn
.getSpecificEpithet().toLowerCase(), "specificEpithet", accuracy
, 3);
538 //textQuery.add(new RegexQuery (new Term ("specificEpithet", "^[a-zA-Z]*")), Occur.MUST_NOT);
539 textQueryBuilder
.add(queryFactory
.newTermQuery("specificEpithet", "_null_", false), Occur
.MUST
);
542 if(nvn
.getInfraSpecificEpithet() != null && !nvn
.getInfraSpecificEpithet().equals("")){
543 fltq
.addTerms(nvn
.getInfraSpecificEpithet().toLowerCase(), "infraSpecificEpithet", accuracy
, 3);
545 //textQuery.add(new RegexQuery (new Term ("infraSpecificEpithet", "^[a-zA-Z]*")), Occur.MUST_NOT);
546 textQueryBuilder
.add(queryFactory
.newTermQuery("infraSpecificEpithet", "_null_", false), Occur
.MUST
);
549 if(nvn
.getAuthorshipCache() != null && !nvn
.getAuthorshipCache().equals("")){
550 fltq
.addTerms(nvn
.getAuthorshipCache().toLowerCase(), "authorshipCache", accuracy
, 3);
552 //textQuery.add(new RegexQuery (new Term ("authorshipCache", "^[a-zA-Z]*")), Occur.MUST_NOT);
555 textQueryBuilder
.add(fltq
, Occur
.MUST
);
557 BooleanQuery textQuery
= textQueryBuilder
.build();
558 finalQueryBuilder
.add(textQuery
, Occur
.MUST
);
560 luceneSearch
.setQuery(finalQueryBuilder
.build());
562 if(highlightFragments
){
563 luceneSearch
.setHighlightFields(queryFactory
.getTextFieldNamesAsArray());
568 protected LuceneSearch
prepareFindByFuzzyNameCacheSearch(Class
<?
extends CdmBase
> clazz
,
572 List
<Language
> languages
,
573 boolean highlightFragments
) {
575 LuceneSearch luceneSearch
= new LuceneSearch(luceneIndexToolProvider
, TaxonName
.class);
576 QueryFactory queryFactory
= luceneIndexToolProvider
.newQueryFactoryFor(TaxonName
.class);
578 // SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};
579 // luceneSearch.setSortFields(sortFields);
581 // ---- search criteria
582 luceneSearch
.setCdmTypRestriction(clazz
);
583 FuzzyLikeThisQuery fltq
= new FuzzyLikeThisQuery(maxNoOfResults
, luceneSearch
.getAnalyzer());
585 fltq
.addTerms(name
, "nameCache", accuracy
, 3);
587 BooleanQuery finalQuery
= new BooleanQuery(false);
589 finalQuery
.add(fltq
, Occur
.MUST
);
591 luceneSearch
.setQuery(finalQuery
);
593 if(highlightFragments
){
594 luceneSearch
.setHighlightFields(queryFactory
.getTextFieldNamesAsArray());
599 protected LuceneSearch
prepareFindByExactNameSearch(Class
<?
extends CdmBase
> clazz
,
602 List
<Language
> languages
,
603 boolean highlightFragments
) {
604 Builder textQueryBuilder
= new Builder();
606 LuceneSearch luceneSearch
= new LuceneSearch(luceneIndexToolProvider
, TaxonName
.class);
607 QueryFactory queryFactory
= luceneIndexToolProvider
.newQueryFactoryFor(TaxonName
.class);
609 // SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};
610 // luceneSearch.setSortFields(sortFields);
612 // ---- search criteria
613 luceneSearch
.setCdmTypRestriction(clazz
);
615 if(name
!= null && !name
.equals("")) {
617 textQueryBuilder
.add(new WildcardQuery(new Term("nameCache", name
+ "*")), Occur
.MUST
);
619 textQueryBuilder
.add(queryFactory
.newTermQuery("nameCache", name
, false), Occur
.MUST
);
623 luceneSearch
.setQuery(textQueryBuilder
.build());
625 if(highlightFragments
){
626 luceneSearch
.setHighlightFields(queryFactory
.getTextFieldNamesAsArray());
632 public List
<SearchResult
<TaxonName
>> findByNameFuzzySearch(
635 List
<Language
> languages
,
636 boolean highlightFragments
,
637 List
<String
> propertyPaths
,
638 int maxNoOfResults
) throws IOException
, LuceneParseException
{
640 logger
.info("Name to fuzzy search for : " + name
);
641 // parse the input name
642 NonViralNameParserImpl parser
= new NonViralNameParserImpl();
643 INonViralName nvn
= parser
.parseFullName(name
);
644 if(name
!= null && !name
.equals("") && nvn
== null) {
645 throw new LuceneParseException("Could not parse name " + name
);
647 LuceneSearch luceneSearch
= prepareFindByFuzzyNameSearch(null, nvn
, accuracy
, maxNoOfResults
, languages
, highlightFragments
);
649 // --- execute search
650 TopDocs topDocs
= luceneSearch
.executeSearch(maxNoOfResults
);
653 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
654 idFieldMap
.put(CdmBaseType
.NONVIRALNAME
, "id");
656 // --- initialize taxa, highlight matches ....
657 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
659 @SuppressWarnings("rawtypes")
660 List
<SearchResult
<TaxonName
>> searchResults
= searchResultBuilder
.createResultSet(
661 topDocs
, luceneSearch
.getHighlightFields(), dao
, idFieldMap
, propertyPaths
);
663 return searchResults
;
668 public List
<DocumentSearchResult
> findByNameFuzzySearch(
671 List
<Language
> languages
,
672 boolean highlightFragments
,
673 int maxNoOfResults
) throws IOException
, LuceneParseException
{
675 logger
.info("Name to fuzzy search for : " + name
);
676 // parse the input name
677 NonViralNameParserImpl parser
= new NonViralNameParserImpl();
678 INonViralName nvn
= parser
.parseFullName(name
);
679 if(name
!= null && !name
.equals("") && nvn
== null) {
680 throw new LuceneParseException("Could not parse name " + name
);
682 LuceneSearch luceneSearch
= prepareFindByFuzzyNameSearch(null, nvn
, accuracy
, maxNoOfResults
, languages
, highlightFragments
);
684 // --- execute search
685 TopDocs topDocs
= luceneSearch
.executeSearch(maxNoOfResults
);
687 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
689 // --- initialize taxa, highlight matches ....
690 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
692 @SuppressWarnings("rawtypes")
693 List
<DocumentSearchResult
> searchResults
= searchResultBuilder
.createResultSet(topDocs
, luceneSearch
.getHighlightFields());
695 return searchResults
;
699 public List
<DocumentSearchResult
> findByFuzzyNameCacheSearch(
702 List
<Language
> languages
,
703 boolean highlightFragments
,
704 int maxNoOfResults
) throws IOException
, LuceneParseException
{
706 logger
.info("Name to fuzzy search for : " + name
);
708 LuceneSearch luceneSearch
= prepareFindByFuzzyNameCacheSearch(null, name
, accuracy
, maxNoOfResults
, languages
, highlightFragments
);
710 // --- execute search
711 TopDocs topDocs
= luceneSearch
.executeSearch(maxNoOfResults
);
712 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
714 // --- initialize taxa, highlight matches ....
715 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
717 @SuppressWarnings("rawtypes")
718 List
<DocumentSearchResult
> searchResults
= searchResultBuilder
.createResultSet(topDocs
, luceneSearch
.getHighlightFields());
720 return searchResults
;
724 public List
<DocumentSearchResult
> findByNameExactSearch(
727 List
<Language
> languages
,
728 boolean highlightFragments
,
729 int maxNoOfResults
) throws IOException
, LuceneParseException
{
731 logger
.info("Name to exact search for : " + name
);
733 LuceneSearch luceneSearch
= prepareFindByExactNameSearch(null, name
, wildcard
, languages
, highlightFragments
);
735 // --- execute search
738 TopDocs topDocs
= luceneSearch
.executeSearch(maxNoOfResults
);
740 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
742 // --- initialize taxa, highlight matches ....
743 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
745 @SuppressWarnings("rawtypes")
746 List
<DocumentSearchResult
> searchResults
= searchResultBuilder
.createResultSet(topDocs
, luceneSearch
.getHighlightFields());
748 return searchResults
;
752 public Pager
<NameRelationship
> pageNameRelationships(TaxonName name
, Direction direction
, NameRelationshipType type
, Integer pageSize
,
753 Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
754 List
<NameRelationship
> results
= listNameRelationships(name
, direction
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
755 return new DefaultPagerImpl
<NameRelationship
>(pageNumber
, results
.size(), pageSize
, results
);
759 public List
<NameRelationship
> listFromNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
760 return listNameRelationships(name
, Direction
.relatedFrom
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
764 public Pager
<NameRelationship
> pageFromNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
765 List
<NameRelationship
> results
= listNameRelationships(name
, Direction
.relatedFrom
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
766 return new DefaultPagerImpl
<NameRelationship
>(pageNumber
, results
.size(), pageSize
, results
);
770 public List
<NameRelationship
> listToNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
771 return listNameRelationships(name
, Direction
.relatedTo
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
775 public Pager
<NameRelationship
> pageToNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
776 List
<NameRelationship
> results
= listNameRelationships(name
, Direction
.relatedTo
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
777 return new DefaultPagerImpl
<NameRelationship
>(pageNumber
, results
.size(), pageSize
, results
);
781 public Pager
<TypeDesignationBase
> getTypeDesignations(TaxonName name
, SpecimenTypeDesignationStatus status
,
782 Integer pageSize
, Integer pageNumber
) {
783 return getTypeDesignations(name
, status
, pageSize
, pageNumber
, null);
787 public Pager
<TypeDesignationBase
> getTypeDesignations(TaxonName name
, SpecimenTypeDesignationStatus status
,
788 Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
){
789 long numberOfResults
= dao
.countTypeDesignations(name
, status
);
791 List
<TypeDesignationBase
> results
= new ArrayList
<>();
792 if(AbstractPagerImpl
.hasResultsInRange(numberOfResults
, pageNumber
, pageSize
)) {
793 results
= dao
.getTypeDesignations(name
, null, status
, pageSize
, pageNumber
, propertyPaths
);
796 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
800 * FIXME Candidate for harmonization
804 public Pager
<TaxonName
> searchNames(String uninomial
,String infraGenericEpithet
, String specificEpithet
, String infraspecificEpithet
, Rank rank
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
805 List
<String
> propertyPaths
) {
806 long numberOfResults
= dao
.countNames(uninomial
, infraGenericEpithet
, specificEpithet
, infraspecificEpithet
, rank
);
808 List
<TaxonName
> results
= new ArrayList
<>();
809 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
810 results
= dao
.searchNames(uninomial
, infraGenericEpithet
, specificEpithet
, infraspecificEpithet
, rank
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
813 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
817 public List
<UuidAndTitleCache
> getUuidAndTitleCacheOfNames(Integer limit
, String pattern
) {
818 return dao
.getUuidAndTitleCacheOfNames(limit
, pattern
);
822 public Pager
<TaxonName
> findByName(Class
<TaxonName
> clazz
, String queryString
, MatchMode matchmode
, List
<Criterion
> criteria
,
823 Integer pageSize
,Integer pageNumber
, List
<OrderHint
> orderHints
,List
<String
> propertyPaths
) {
824 Long numberOfResults
= dao
.countByName(clazz
, queryString
, matchmode
, criteria
);
826 List
<TaxonName
> results
= new ArrayList
<>();
827 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
828 results
= dao
.findByName(clazz
, queryString
, matchmode
, criteria
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
831 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
835 public HomotypicalGroup
findHomotypicalGroup(UUID uuid
) {
836 return homotypicalGroupDao
.findByUuid(uuid
);
840 @Transactional(readOnly
= false)
841 public void updateCaches(Class
<?
extends TaxonName
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<TaxonName
> cacheStrategy
, IProgressMonitor monitor
) {
843 clazz
= TaxonName
.class;
845 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
850 public List
<TaggedText
> getTaggedName(UUID uuid
) {
851 TaxonName taxonName
= dao
.load(uuid
);
852 List
<TaggedText
> taggedName
= taxonName
.getTaggedName();
857 public DeleteResult
isDeletable(TaxonName name
, DeleteConfiguratorBase config
){
858 DeleteResult result
= new DeleteResult();
860 NameDeletionConfigurator nameConfig
= null;
861 if (config
instanceof NameDeletionConfigurator
){
862 nameConfig
= (NameDeletionConfigurator
) config
;
864 result
.addException(new Exception("The delete configurator should be of the type NameDeletionConfigurator."));
869 if (!name
.getNameRelations().isEmpty() && !nameConfig
.isRemoveAllNameRelationships()){
870 HomotypicalGroup homotypicalGroup
= HibernateProxyHelper
.deproxy(name
.getHomotypicalGroup(), HomotypicalGroup
.class);
872 if (!nameConfig
.isIgnoreIsBasionymFor() && homotypicalGroup
.getBasionyms().contains(name
)){
873 result
.addException(new Exception( "Name can't be deleted as it is a basionym."));
876 if (!nameConfig
.isIgnoreHasBasionym() && (name
.getBasionyms().size()>0)){
877 result
.addException(new Exception( "Name can't be deleted as it has a basionym."));
880 Set
<NameRelationship
> relationships
= name
.getNameRelations();
881 for (NameRelationship rel
: relationships
){
882 if (!rel
.getType().equals(NameRelationshipType
.BASIONYM())){
883 result
.addException(new Exception("Name can't be deleted as it is used in name relationship(s). Remove name relationships prior to deletion."));
891 if (! name
.getTaxonBases().isEmpty()){
892 result
.addException(new Exception("Name can't be deleted as it is used in concept(s). Remove or change concept prior to deletion."));
896 //hybrid relationships
897 if (name
.isNonViral()){
898 INonViralName nvn
= name
;
899 Set
<HybridRelationship
> parentHybridRelations
= nvn
.getHybridParentRelations();
900 //Hibernate.initialize(parentHybridRelations);
901 if (! parentHybridRelations
.isEmpty()){
902 result
.addException(new Exception("Name can't be deleted as it is a parent in (a) hybrid relationship(s). Remove hybrid relationships prior to deletion."));
906 Set
<CdmBase
> referencingObjects
= genericDao
.getReferencingObjectsForDeletion(name
);
907 for (CdmBase referencingObject
: referencingObjects
){
908 //DerivedUnit?.storedUnder
909 if (referencingObject
.isInstanceOf(DerivedUnit
.class)){
910 String message
= "Name can't be deleted as it is used as derivedUnit#storedUnder by %s. Remove 'stored under' prior to deleting this name";
911 message
= String
.format(message
, CdmBase
.deproxy(referencingObject
, DerivedUnit
.class).getTitleCache());
912 result
.addException(new ReferencedObjectUndeletableException(message
));
913 result
.addRelatedObject(referencingObject
);
916 //DescriptionElementSource#nameUsedInSource
917 else if (referencingObject
.isInstanceOf(DescriptionElementSource
.class)){
918 String message
= "Name can't be deleted as it is used as descriptionElementSource#nameUsedInSource";
919 result
.addException(new ReferencedObjectUndeletableException(message
));
920 result
.addRelatedObject(referencingObject
);
923 //NameTypeDesignation#typeName
924 else if (referencingObject
.isInstanceOf(NameTypeDesignation
.class)){
925 NameTypeDesignation typeDesignation
= HibernateProxyHelper
.deproxy(referencingObject
, NameTypeDesignation
.class);
927 if (typeDesignation
.getTypeName().equals(name
)){
928 String message
= "Name can't be deleted as it is used as a name type in a NameTypeDesignation";
929 result
.addException(new ReferencedObjectUndeletableException(message
));
930 result
.addRelatedObject(referencingObject
);
934 //DeterminationEvent#taxonName
935 else if (referencingObject
.isInstanceOf(DeterminationEvent
.class)){
936 String message
= "Name can't be deleted as it is used as a determination event";
937 result
.addException(new ReferencedObjectUndeletableException(message
));
938 result
.addRelatedObject(referencingObject
);
943 //TODO inline references
946 if (!nameConfig
.isIgnoreIsReplacedSynonymFor() && name
.isReplacedSynonym()){
947 String message
= "Name can't be deleted as it is a replaced synonym.";
948 result
.addException(new Exception(message
));
951 if (!nameConfig
.isIgnoreHasReplacedSynonym() && (name
.getReplacedSynonyms().size()>0)){
952 String message
= "Name can't be deleted as it has a replaced synonym.";
953 result
.addException(new Exception(message
));
962 public DeleteResult
isDeletable(UUID nameUUID
, DeleteConfiguratorBase config
){
963 TaxonName name
= this.load(nameUUID
);
964 return isDeletable(name
, config
);
968 @Transactional(readOnly
= true)
969 public UpdateResult
setAsGroupsBasionym(UUID nameUuid
) {
970 TaxonName name
= dao
.load(nameUuid
);
971 UpdateResult result
= new UpdateResult();
972 name
.setAsGroupsBasionym();
973 result
.addUpdatedObject(name
);
979 public List
<HashMap
<String
,String
>> getNameRecords(){
980 return dao
.getNameRecords();