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
.Arrays
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
20 import java
.util
.Optional
;
22 import java
.util
.UUID
;
24 import org
.apache
.log4j
.Logger
;
25 import org
.apache
.lucene
.index
.Term
;
26 import org
.apache
.lucene
.sandbox
.queries
.FuzzyLikeThisQuery
;
27 import org
.apache
.lucene
.search
.BooleanClause
.Occur
;
28 import org
.apache
.lucene
.search
.BooleanQuery
;
29 import org
.apache
.lucene
.search
.BooleanQuery
.Builder
;
30 import org
.apache
.lucene
.search
.TopDocs
;
31 import org
.apache
.lucene
.search
.WildcardQuery
;
32 import org
.hibernate
.criterion
.Criterion
;
33 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
34 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
35 import org
.springframework
.stereotype
.Service
;
36 import org
.springframework
.transaction
.annotation
.Transactional
;
38 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
39 import eu
.etaxonomy
.cdm
.api
.service
.config
.NameDeletionConfigurator
;
40 import eu
.etaxonomy
.cdm
.api
.service
.dto
.TypeDesignationStatusFilter
;
41 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
42 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
43 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.AbstractPagerImpl
;
44 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
45 import eu
.etaxonomy
.cdm
.api
.service
.search
.DocumentSearchResult
;
46 import eu
.etaxonomy
.cdm
.api
.service
.search
.ILuceneIndexToolProvider
;
47 import eu
.etaxonomy
.cdm
.api
.service
.search
.ISearchResultBuilder
;
48 import eu
.etaxonomy
.cdm
.api
.service
.search
.LuceneParseException
;
49 import eu
.etaxonomy
.cdm
.api
.service
.search
.LuceneSearch
;
50 import eu
.etaxonomy
.cdm
.api
.service
.search
.QueryFactory
;
51 import eu
.etaxonomy
.cdm
.api
.service
.search
.SearchResult
;
52 import eu
.etaxonomy
.cdm
.api
.service
.search
.SearchResultBuilder
;
53 import eu
.etaxonomy
.cdm
.api
.utility
.TaxonNamePartsFilter
;
54 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
55 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
56 import eu
.etaxonomy
.cdm
.model
.CdmBaseType
;
57 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
58 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
59 import eu
.etaxonomy
.cdm
.model
.common
.ReferencedEntityBase
;
60 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
61 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
.Direction
;
62 import eu
.etaxonomy
.cdm
.model
.common
.SourcedEntityBase
;
63 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
64 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
65 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
66 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationshipType
;
67 import eu
.etaxonomy
.cdm
.model
.name
.INonViralName
;
68 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
69 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
70 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignation
;
71 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
72 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
73 import eu
.etaxonomy
.cdm
.model
.name
.Registration
;
74 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
75 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
76 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
77 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
78 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatusBase
;
79 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEvent
;
80 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
81 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
82 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
83 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
84 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmGenericDao
;
85 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IReferencedEntityDao
;
86 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ISourcedEntityDao
;
87 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.Restriction
;
88 import eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
.IBeanInitializer
;
89 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.IHomotypicalGroupDao
;
90 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.INomenclaturalStatusDao
;
91 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.ITaxonNameDao
;
92 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.ITypeDesignationDao
;
93 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.IOrderedTermVocabularyDao
;
94 import eu
.etaxonomy
.cdm
.persistence
.dao
.term
.ITermVocabularyDao
;
95 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNameParts
;
96 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
97 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
98 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
99 import eu
.etaxonomy
.cdm
.strategy
.cache
.TaggedText
;
100 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
101 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
105 @Transactional(readOnly
= true)
106 public class NameServiceImpl
107 extends IdentifiableServiceBase
<TaxonName
,ITaxonNameDao
>
108 implements INameService
{
109 static private final Logger logger
= Logger
.getLogger(NameServiceImpl
.class);
112 protected ITermVocabularyDao vocabularyDao
;
114 protected IOrderedTermVocabularyDao orderedVocabularyDao
;
116 protected IOccurrenceService occurrenceService
;
118 protected ICollectionService collectionService
;
120 @Qualifier("refEntDao")
121 protected IReferencedEntityDao
<ReferencedEntityBase
> referencedEntityDao
;
123 @Qualifier("sourcedEntityDao")
124 protected ISourcedEntityDao
<SourcedEntityBase
<?
>> sourcedEntityDao
;
126 private INomenclaturalStatusDao nomStatusDao
;
128 private ITypeDesignationDao typeDesignationDao
;
130 private IHomotypicalGroupDao homotypicalGroupDao
;
132 private ICdmGenericDao genericDao
;
134 private ILuceneIndexToolProvider luceneIndexToolProvider
;
136 // @Qualifier("defaultBeanInitializer")
137 protected IBeanInitializer defaultBeanInitializer
;
142 public NameServiceImpl(){}
144 //********************* METHODS ****************************************************************//
147 @Transactional(readOnly
= false)
148 public DeleteResult
delete(UUID nameUUID
){
149 NameDeletionConfigurator config
= new NameDeletionConfigurator();
150 DeleteResult result
= delete(nameUUID
, config
);
155 public DeleteResult
delete(TaxonName name
){
156 return delete(name
.getUuid());
160 @Transactional(readOnly
= false)
161 public DeleteResult
delete(TaxonName name
, NameDeletionConfigurator config
) {
162 DeleteResult result
= new DeleteResult();
170 result
= this.isDeletable(name
, config
);
172 result
.addException(e
);
177 //remove references to this name
178 removeNameRelationshipsByDeleteConfig(name
, config
);
180 //remove name from homotypical group
181 HomotypicalGroup homotypicalGroup
= name
.getHomotypicalGroup();
182 if (homotypicalGroup
!= null){
183 homotypicalGroup
.removeTypifiedName(name
, false);
186 //all type designation relationships are removed as they belong to the name
187 deleteTypeDesignation(name
, null);
188 // //type designations
189 // if (! name.getTypeDesignations().isEmpty()){
190 // String message = "Name can't be deleted as it has types. Remove types prior to deletion.";
191 // throw new ReferrencedObjectUndeletableException(message);
196 result
.addDeletedObject(name
);
198 result
.addException(e
);
208 @Transactional(readOnly
= false)
209 public DeleteResult
delete(UUID nameUUID
, NameDeletionConfigurator config
) {
211 TaxonName name
= dao
.load(nameUUID
);
212 return delete(name
, config
);
216 @Transactional(readOnly
= false)
217 public UpdateResult
cloneTypeDesignation(UUID nameUuid
, SpecimenTypeDesignation baseDesignation
,
218 String accessionNumber
, String barcode
, String catalogNumber
,
219 UUID collectionUuid
, SpecimenTypeDesignationStatus typeStatus
){
220 UpdateResult result
= new UpdateResult();
222 DerivedUnit baseSpecimen
= HibernateProxyHelper
.deproxy(occurrenceService
.load(baseDesignation
.getTypeSpecimen().getUuid(), Arrays
.asList("collection")), DerivedUnit
.class);
223 DerivedUnit duplicate
= DerivedUnit
.NewInstance(baseSpecimen
.getRecordBasis());
224 DerivationEvent derivedFrom
= baseSpecimen
.getDerivedFrom();
225 Collection
<FieldUnit
> fieldUnits
= occurrenceService
.findFieldUnits(baseSpecimen
.getUuid(), null);
226 if(fieldUnits
.size()!=1){
227 result
.addException(new Exception("More than one or no field unit found for specimen"));
231 for (SpecimenOrObservationBase original
: derivedFrom
.getOriginals()) {
232 DerivationEvent
.NewSimpleInstance(original
, duplicate
, derivedFrom
.getType());
234 duplicate
.setAccessionNumber(accessionNumber
);
235 duplicate
.setBarcode(barcode
);
236 duplicate
.setCatalogNumber(catalogNumber
);
237 duplicate
.setCollection(collectionService
.load(collectionUuid
));
238 SpecimenTypeDesignation typeDesignation
= SpecimenTypeDesignation
.NewInstance();
239 typeDesignation
.setTypeSpecimen(duplicate
);
240 typeDesignation
.setTypeStatus(typeStatus
);
242 TaxonName name
= load(nameUuid
);
243 name
.getTypeDesignations().add(typeDesignation
);
245 result
.setCdmEntity(typeDesignation
);
246 result
.addUpdatedObject(name
);
252 public DeleteResult
deleteTypeDesignation(TaxonName name
, TypeDesignationBase
<?
> typeDesignation
){
253 if(typeDesignation
!= null && typeDesignation
.isPersited()){
254 typeDesignation
= HibernateProxyHelper
.deproxy(sourcedEntityDao
.load(typeDesignation
.getUuid()), TypeDesignationBase
.class);
257 DeleteResult result
= new DeleteResult();
258 if (name
== null && typeDesignation
== null){
261 }else if (name
!= null && typeDesignation
!= null){
262 removeSingleDesignation(name
, typeDesignation
);
263 }else if (name
!= null){
264 @SuppressWarnings("rawtypes")
265 Set
<TypeDesignationBase
<?
>> designationSet
= new HashSet(name
.getTypeDesignations());
266 for (TypeDesignationBase
<?
> desig
: designationSet
){
267 desig
= CdmBase
.deproxy(desig
);
268 removeSingleDesignation(name
, desig
);
270 }else if (typeDesignation
!= null){
271 @SuppressWarnings("unchecked")
272 Set
<TaxonName
> nameSet
= new HashSet(typeDesignation
.getTypifiedNames());
273 for (TaxonName singleName
: nameSet
){
274 singleName
= CdmBase
.deproxy(singleName
);
275 removeSingleDesignation(singleName
, typeDesignation
);
278 result
.addDeletedObject(typeDesignation
);
279 result
.addUpdatedObject(name
);
285 @Transactional(readOnly
= false)
286 public DeleteResult
deleteTypeDesignation(UUID nameUuid
, UUID typeDesignationUuid
){
287 TaxonName nameBase
= load(nameUuid
);
288 TypeDesignationBase
<?
> typeDesignation
= HibernateProxyHelper
.deproxy(sourcedEntityDao
.load(typeDesignationUuid
), TypeDesignationBase
.class);
289 return deleteTypeDesignation(nameBase
, typeDesignation
);
294 * @param typeDesignation
297 private void removeSingleDesignation(TaxonName name
, TypeDesignationBase
<?
> typeDesignation
) {
299 name
.removeTypeDesignation(typeDesignation
);
300 if (typeDesignation
.getTypifiedNames().isEmpty()){
301 typeDesignation
.removeType();
302 if (!typeDesignation
.getRegistrations().isEmpty()){
303 for(Object reg
: typeDesignation
.getRegistrations()){
304 if (reg
instanceof Registration
){
305 ((Registration
)reg
).removeTypeDesignation(typeDesignation
);
310 typeDesignationDao
.delete(typeDesignation
);
321 private void removeNameRelationshipsByDeleteConfig(TaxonName name
, NameDeletionConfigurator config
) {
323 if (config
.isRemoveAllNameRelationships()){
324 Set
<NameRelationship
> rels
= getModifiableSet(name
.getNameRelations());
325 for (NameRelationship rel
: rels
){
326 name
.removeNameRelationship(rel
);
329 //relations to this name
330 Set
<NameRelationship
> rels
= getModifiableSet(name
.getRelationsToThisName());
331 for (NameRelationship rel
: rels
){
332 if (config
.isIgnoreHasBasionym() && NameRelationshipType
.BASIONYM().equals(rel
.getType() )){
333 name
.removeNameRelationship(rel
);
334 }else if (config
.isIgnoreHasReplacedSynonym() && NameRelationshipType
.REPLACED_SYNONYM().equals(rel
.getType())){
335 name
.removeNameRelationship(rel
);
338 //relations from this name
339 rels
= getModifiableSet(name
.getRelationsFromThisName());
340 for (NameRelationship rel
: rels
){
341 if (config
.isIgnoreIsBasionymFor() && NameRelationshipType
.BASIONYM().equals(rel
.getType()) ){
342 name
.removeNameRelationship(rel
);
343 }else if (config
.isIgnoreIsReplacedSynonymFor() && NameRelationshipType
.REPLACED_SYNONYM().equals(rel
.getType())){
344 name
.removeNameRelationship(rel
);
349 } catch (Exception e
) {
350 throw new RuntimeException(e
);
358 private Set
<NameRelationship
> getModifiableSet(Set
<NameRelationship
> relations
) {
359 Set
<NameRelationship
> rels
= new HashSet
<NameRelationship
>();
360 for (NameRelationship rel
: relations
){
366 //********************* METHODS ****************************************************************//
370 * TODO candidate for harmonization
371 * new name findByName
375 public List
<TaxonName
> getNamesByNameCache(String nameCache
){
376 boolean includeAuthors
= false;
377 List result
= dao
.findByName(includeAuthors
, nameCache
, MatchMode
.EXACT
, null, null, null, null);
382 * TODO candidate for harmonization
383 * new name saveHomotypicalGroups
388 public List
<TaxonName
> findNamesByTitleCache(String titleCache
, MatchMode matchMode
, List
<String
> propertyPaths
){
389 List result
= dao
.findByTitle(titleCache
, matchMode
, null, null, null, propertyPaths
);
394 * TODO candidate for harmonization
395 * new name saveHomotypicalGroups
400 public List
<TaxonName
> findNamesByNameCache(String nameCache
, MatchMode matchMode
, List
<String
> propertyPaths
){
401 List
<TaxonName
> result
= dao
.findByName(false, nameCache
, matchMode
, null, null, null , propertyPaths
);
406 public Pager
<TaxonNameParts
> findTaxonNameParts(Optional
<String
> genusOrUninomial
,
407 Optional
<String
> infraGenericEpithet
, Optional
<String
> specificEpithet
,
408 Optional
<String
> infraSpecificEpithet
, Rank rank
, Set
<UUID
> excludedNamesUuids
,
409 Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
) {
412 long count
= dao
.countTaxonNameParts(genusOrUninomial
, infraGenericEpithet
, specificEpithet
, infraGenericEpithet
, rank
, excludedNamesUuids
);
414 List
<TaxonNameParts
> results
;
415 if(AbstractPagerImpl
.hasResultsInRange(count
, pageIndex
, pageSize
)){
416 results
= dao
.findTaxonNameParts(genusOrUninomial
, infraGenericEpithet
, specificEpithet
, infraSpecificEpithet
,
417 rank
, excludedNamesUuids
,
418 pageSize
, pageIndex
, orderHints
);
420 results
= new ArrayList
<>();
423 return new DefaultPagerImpl
<TaxonNameParts
>(pageIndex
, count
, pageSize
, results
);
430 public Pager
<TaxonNameParts
> findTaxonNameParts(TaxonNamePartsFilter filter
, String namePartQueryString
,
431 Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
) {
433 return findTaxonNameParts(
434 filter
.uninomialQueryString(namePartQueryString
),
435 filter
.infraGenericEpithet(namePartQueryString
),
436 filter
.specificEpithet(namePartQueryString
),
437 filter
.infraspecificEpithet(namePartQueryString
),
439 filter
.getExludedNamesUuids(),
440 pageSize
, pageIndex
, orderHints
);
444 * TODO candidate for harmonization
445 * new name saveHomotypicalGroups
448 @Transactional(readOnly
= false)
449 public Map
<UUID
, HomotypicalGroup
> saveAllHomotypicalGroups(Collection
<HomotypicalGroup
> homotypicalGroups
){
450 return homotypicalGroupDao
.saveAll(homotypicalGroups
);
454 * TODO candidate for harmonization
455 * new name saveTypeDesignations
458 @Transactional(readOnly
= false)
459 public Map
<UUID
, TypeDesignationBase
<?
>> saveTypeDesignationAll(Collection
<TypeDesignationBase
<?
>> typeDesignationCollection
){
460 return typeDesignationDao
.saveAll(typeDesignationCollection
);
464 * TODO candidate for harmonization
465 * new name getNomenclaturalStatus
468 public List
<NomenclaturalStatus
> getAllNomenclaturalStatus(int limit
, int start
){
469 return nomStatusDao
.list(limit
, start
);
473 public NomenclaturalStatus
loadNomenclaturalStatus(UUID uuid
, List
<String
> propertyPaths
){
474 return nomStatusDao
.load(uuid
, propertyPaths
);
478 * TODO candidate for harmonization
479 * new name getTypeDesignations
482 public List
<TypeDesignationBase
<?
>> getAllTypeDesignations(int limit
, int start
){
483 return typeDesignationDao
.getAllTypeDesignations(limit
, start
);
487 public TypeDesignationBase
<?
> loadTypeDesignation(int id
, List
<String
> propertyPaths
){
488 return typeDesignationDao
.load(id
, propertyPaths
);
492 public TypeDesignationBase
<?
> loadTypeDesignation(UUID uuid
, List
<String
> propertyPaths
){
493 return typeDesignationDao
.load(uuid
, propertyPaths
);
497 * FIXME Candidate for harmonization
498 * homotypicalGroupService.list
501 public List
<HomotypicalGroup
> getAllHomotypicalGroups(int limit
, int start
){
502 return homotypicalGroupDao
.list(limit
, start
);
506 * FIXME Candidate for harmonization
511 public List
<RelationshipBase
> getAllRelationships(int limit
, int start
){
512 return dao
.getAllRelationships(limit
, start
);
519 protected void setDao(ITaxonNameDao dao
) {
524 public Pager
<HybridRelationship
> getHybridNames(INonViralName name
, HybridRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
525 Integer numberOfResults
= dao
.countHybridNames(name
, type
);
527 List
<HybridRelationship
> results
= new ArrayList
<HybridRelationship
>();
528 if(AbstractPagerImpl
.hasResultsInRange(numberOfResults
.longValue(), pageNumber
, pageSize
)) { // no point checking again
529 results
= dao
.getHybridNames(name
, type
, pageSize
, pageNumber
,orderHints
,propertyPaths
);
532 return new DefaultPagerImpl
<HybridRelationship
>(pageNumber
, numberOfResults
, pageSize
, results
);
536 public List
<NameRelationship
> listNameRelationships(TaxonName name
, Direction direction
, NameRelationshipType type
, Integer pageSize
,
537 Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
539 Integer numberOfResults
= dao
.countNameRelationships(name
, direction
, type
);
541 List
<NameRelationship
> results
= new ArrayList
<NameRelationship
>();
542 if (AbstractPagerImpl
.hasResultsInRange(numberOfResults
.longValue(), pageNumber
, pageSize
)) { // no point checking again
543 results
= dao
.getNameRelationships(name
, direction
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
549 protected LuceneSearch
prepareFindByFuzzyNameSearch(Class
<?
extends CdmBase
> clazz
,
553 List
<Language
> languages
,
554 boolean highlightFragments
) {
555 String similarity
= Float
.toString(accuracy
);
556 String searchSuffix
= "~" + similarity
;
559 Builder finalQueryBuilder
= new Builder();
560 finalQueryBuilder
.setDisableCoord(false);
561 Builder textQueryBuilder
= new Builder();
562 textQueryBuilder
.setDisableCoord(false);
564 LuceneSearch luceneSearch
= new LuceneSearch(luceneIndexToolProvider
, TaxonName
.class);
565 QueryFactory queryFactory
= luceneIndexToolProvider
.newQueryFactoryFor(TaxonName
.class);
567 // SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};
568 // luceneSearch.setSortFields(sortFields);
570 // ---- search criteria
571 luceneSearch
.setCdmTypRestriction(clazz
);
573 FuzzyLikeThisQuery fltq
= new FuzzyLikeThisQuery(maxNoOfResults
, luceneSearch
.getAnalyzer());
574 if(nvn
.getGenusOrUninomial() != null && !nvn
.getGenusOrUninomial().equals("")) {
575 fltq
.addTerms(nvn
.getGenusOrUninomial().toLowerCase(), "genusOrUninomial", accuracy
, 3);
577 //textQuery.add(new RegexQuery (new Term ("genusOrUninomial", "^[a-zA-Z]*")), Occur.MUST_NOT);
578 textQueryBuilder
.add(queryFactory
.newTermQuery("genusOrUninomial", "_null_", false), Occur
.MUST
);
581 if(nvn
.getInfraGenericEpithet() != null && !nvn
.getInfraGenericEpithet().equals("")){
582 fltq
.addTerms(nvn
.getInfraGenericEpithet().toLowerCase(), "infraGenericEpithet", accuracy
, 3);
584 //textQuery.add(new RegexQuery (new Term ("infraGenericEpithet", "^[a-zA-Z]*")), Occur.MUST_NOT);
585 textQueryBuilder
.add(queryFactory
.newTermQuery("infraGenericEpithet", "_null_", false), Occur
.MUST
);
588 if(nvn
.getSpecificEpithet() != null && !nvn
.getSpecificEpithet().equals("")){
589 fltq
.addTerms(nvn
.getSpecificEpithet().toLowerCase(), "specificEpithet", accuracy
, 3);
591 //textQuery.add(new RegexQuery (new Term ("specificEpithet", "^[a-zA-Z]*")), Occur.MUST_NOT);
592 textQueryBuilder
.add(queryFactory
.newTermQuery("specificEpithet", "_null_", false), Occur
.MUST
);
595 if(nvn
.getInfraSpecificEpithet() != null && !nvn
.getInfraSpecificEpithet().equals("")){
596 fltq
.addTerms(nvn
.getInfraSpecificEpithet().toLowerCase(), "infraSpecificEpithet", accuracy
, 3);
598 //textQuery.add(new RegexQuery (new Term ("infraSpecificEpithet", "^[a-zA-Z]*")), Occur.MUST_NOT);
599 textQueryBuilder
.add(queryFactory
.newTermQuery("infraSpecificEpithet", "_null_", false), Occur
.MUST
);
602 if(nvn
.getAuthorshipCache() != null && !nvn
.getAuthorshipCache().equals("")){
603 fltq
.addTerms(nvn
.getAuthorshipCache().toLowerCase(), "authorshipCache", accuracy
, 3);
605 //textQuery.add(new RegexQuery (new Term ("authorshipCache", "^[a-zA-Z]*")), Occur.MUST_NOT);
608 textQueryBuilder
.add(fltq
, Occur
.MUST
);
610 BooleanQuery textQuery
= textQueryBuilder
.build();
611 finalQueryBuilder
.add(textQuery
, Occur
.MUST
);
613 luceneSearch
.setQuery(finalQueryBuilder
.build());
615 if(highlightFragments
){
616 luceneSearch
.setHighlightFields(queryFactory
.getTextFieldNamesAsArray());
621 protected LuceneSearch
prepareFindByFuzzyNameCacheSearch(Class
<?
extends CdmBase
> clazz
,
625 List
<Language
> languages
,
626 boolean highlightFragments
) {
628 LuceneSearch luceneSearch
= new LuceneSearch(luceneIndexToolProvider
, TaxonName
.class);
629 QueryFactory queryFactory
= luceneIndexToolProvider
.newQueryFactoryFor(TaxonName
.class);
631 // SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};
632 // luceneSearch.setSortFields(sortFields);
634 // ---- search criteria
635 luceneSearch
.setCdmTypRestriction(clazz
);
636 FuzzyLikeThisQuery fltq
= new FuzzyLikeThisQuery(maxNoOfResults
, luceneSearch
.getAnalyzer());
638 fltq
.addTerms(name
, "nameCache", accuracy
, 3);
640 BooleanQuery finalQuery
= new BooleanQuery(false);
642 finalQuery
.add(fltq
, Occur
.MUST
);
644 luceneSearch
.setQuery(finalQuery
);
646 if(highlightFragments
){
647 luceneSearch
.setHighlightFields(queryFactory
.getTextFieldNamesAsArray());
652 protected LuceneSearch
prepareFindByExactNameSearch(Class
<?
extends CdmBase
> clazz
,
655 List
<Language
> languages
,
656 boolean highlightFragments
) {
657 Builder textQueryBuilder
= new Builder();
659 LuceneSearch luceneSearch
= new LuceneSearch(luceneIndexToolProvider
, TaxonName
.class);
660 QueryFactory queryFactory
= luceneIndexToolProvider
.newQueryFactoryFor(TaxonName
.class);
662 // SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};
663 // luceneSearch.setSortFields(sortFields);
665 // ---- search criteria
666 luceneSearch
.setCdmTypRestriction(clazz
);
668 if(name
!= null && !name
.equals("")) {
670 textQueryBuilder
.add(new WildcardQuery(new Term("nameCache", name
+ "*")), Occur
.MUST
);
672 textQueryBuilder
.add(queryFactory
.newTermQuery("nameCache", name
, false), Occur
.MUST
);
676 luceneSearch
.setQuery(textQueryBuilder
.build());
678 if(highlightFragments
){
679 luceneSearch
.setHighlightFields(queryFactory
.getTextFieldNamesAsArray());
685 public List
<SearchResult
<TaxonName
>> findByNameFuzzySearch(
688 List
<Language
> languages
,
689 boolean highlightFragments
,
690 List
<String
> propertyPaths
,
691 int maxNoOfResults
) throws IOException
, LuceneParseException
{
693 logger
.info("Name to fuzzy search for : " + name
);
694 // parse the input name
695 NonViralNameParserImpl parser
= new NonViralNameParserImpl();
696 INonViralName nvn
= parser
.parseFullName(name
);
697 if(name
!= null && !name
.equals("") && nvn
== null) {
698 throw new LuceneParseException("Could not parse name " + name
);
700 LuceneSearch luceneSearch
= prepareFindByFuzzyNameSearch(null, nvn
, accuracy
, maxNoOfResults
, languages
, highlightFragments
);
702 // --- execute search
703 TopDocs topDocs
= luceneSearch
.executeSearch(maxNoOfResults
);
706 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
707 idFieldMap
.put(CdmBaseType
.NONVIRALNAME
, "id");
709 // --- initialize taxa, highlight matches ....
710 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
712 @SuppressWarnings("rawtypes")
713 List
<SearchResult
<TaxonName
>> searchResults
= searchResultBuilder
.createResultSet(
714 topDocs
, luceneSearch
.getHighlightFields(), dao
, idFieldMap
, propertyPaths
);
716 return searchResults
;
721 public List
<DocumentSearchResult
> findByNameFuzzySearch(
724 List
<Language
> languages
,
725 boolean highlightFragments
,
726 int maxNoOfResults
) throws IOException
, LuceneParseException
{
728 logger
.info("Name to fuzzy search for : " + name
);
729 // parse the input name
730 NonViralNameParserImpl parser
= new NonViralNameParserImpl();
731 INonViralName nvn
= parser
.parseFullName(name
);
732 if(name
!= null && !name
.equals("") && nvn
== null) {
733 throw new LuceneParseException("Could not parse name " + name
);
735 LuceneSearch luceneSearch
= prepareFindByFuzzyNameSearch(null, nvn
, accuracy
, maxNoOfResults
, languages
, highlightFragments
);
737 // --- 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 List
<DocumentSearchResult
> findByFuzzyNameCacheSearch(
755 List
<Language
> languages
,
756 boolean highlightFragments
,
757 int maxNoOfResults
) throws IOException
, LuceneParseException
{
759 logger
.info("Name to fuzzy search for : " + name
);
761 LuceneSearch luceneSearch
= prepareFindByFuzzyNameCacheSearch(null, name
, accuracy
, maxNoOfResults
, languages
, highlightFragments
);
763 // --- execute search
764 TopDocs topDocs
= luceneSearch
.executeSearch(maxNoOfResults
);
765 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
767 // --- initialize taxa, highlight matches ....
768 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
770 @SuppressWarnings("rawtypes")
771 List
<DocumentSearchResult
> searchResults
= searchResultBuilder
.createResultSet(topDocs
, luceneSearch
.getHighlightFields());
773 return searchResults
;
777 public List
<DocumentSearchResult
> findByNameExactSearch(
780 List
<Language
> languages
,
781 boolean highlightFragments
,
782 int maxNoOfResults
) throws IOException
, LuceneParseException
{
784 logger
.info("Name to exact search for : " + name
);
786 LuceneSearch luceneSearch
= prepareFindByExactNameSearch(null, name
, wildcard
, languages
, highlightFragments
);
788 // --- execute search
791 TopDocs topDocs
= luceneSearch
.executeSearch(maxNoOfResults
);
793 Map
<CdmBaseType
, String
> idFieldMap
= new HashMap
<CdmBaseType
, String
>();
795 // --- initialize taxa, highlight matches ....
796 ISearchResultBuilder searchResultBuilder
= new SearchResultBuilder(luceneSearch
, luceneSearch
.getQuery());
798 @SuppressWarnings("rawtypes")
799 List
<DocumentSearchResult
> searchResults
= searchResultBuilder
.createResultSet(topDocs
, luceneSearch
.getHighlightFields());
801 return searchResults
;
805 public Pager
<NameRelationship
> pageNameRelationships(TaxonName name
, Direction direction
, NameRelationshipType type
, Integer pageSize
,
806 Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
807 List
<NameRelationship
> results
= listNameRelationships(name
, direction
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
808 return new DefaultPagerImpl
<NameRelationship
>(pageNumber
, results
.size(), pageSize
, results
);
812 public List
<NameRelationship
> listFromNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
813 return listNameRelationships(name
, Direction
.relatedFrom
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
817 public Pager
<NameRelationship
> pageFromNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
818 List
<NameRelationship
> results
= listNameRelationships(name
, Direction
.relatedFrom
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
819 return new DefaultPagerImpl
<NameRelationship
>(pageNumber
, results
.size(), pageSize
, results
);
823 public List
<NameRelationship
> listToNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
824 return listNameRelationships(name
, Direction
.relatedTo
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
828 public Pager
<NameRelationship
> pageToNameRelationships(TaxonName name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
829 List
<NameRelationship
> results
= listNameRelationships(name
, Direction
.relatedTo
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
830 return new DefaultPagerImpl
<NameRelationship
>(pageNumber
, results
.size(), pageSize
, results
);
834 public Pager
<TypeDesignationBase
> getTypeDesignations(TaxonName name
, SpecimenTypeDesignationStatus status
,
835 Integer pageSize
, Integer pageNumber
) {
836 return getTypeDesignations(name
, status
, pageSize
, pageNumber
, null);
840 public Pager
<TypeDesignationBase
> getTypeDesignations(TaxonName name
, SpecimenTypeDesignationStatus status
,
841 Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
){
842 long numberOfResults
= dao
.countTypeDesignations(name
, status
);
844 List
<TypeDesignationBase
> results
= new ArrayList
<>();
845 if(AbstractPagerImpl
.hasResultsInRange(numberOfResults
, pageNumber
, pageSize
)) {
846 results
= dao
.getTypeDesignations(name
, null, status
, pageSize
, pageNumber
, propertyPaths
);
848 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
852 public List
<TypeDesignationBase
> getTypeDesignationsInHomotypicalGroup(UUID nameUuid
, Integer pageSize
,
853 Integer pageNumber
, List
<String
> propertyPaths
){
854 TaxonName name
= load(nameUuid
, Arrays
.asList("nomenclaturalReference.authorship"));
855 Set
<TypeDesignationBase
<?
>> typeDesignations
= name
.getHomotypicalGroup().getTypeDesignations();
856 List
<TypeDesignationBase
> result
= defaultBeanInitializer
.initializeAll(new ArrayList(typeDesignations
), propertyPaths
);
861 * FIXME Candidate for harmonization
865 public Pager
<TaxonName
> searchNames(String uninomial
,String infraGenericEpithet
, String specificEpithet
, String infraspecificEpithet
, Rank rank
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
866 List
<String
> propertyPaths
) {
867 long numberOfResults
= dao
.countNames(uninomial
, infraGenericEpithet
, specificEpithet
, infraspecificEpithet
, rank
);
869 List
<TaxonName
> results
= new ArrayList
<>();
870 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
871 results
= dao
.searchNames(uninomial
, infraGenericEpithet
, specificEpithet
, infraspecificEpithet
, rank
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
874 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
878 public List
<UuidAndTitleCache
> getUuidAndTitleCacheOfNames(Integer limit
, String pattern
) {
879 return dao
.getUuidAndTitleCacheOfNames(limit
, pattern
);
883 public Pager
<TaxonName
> findByName(Class
<TaxonName
> clazz
, String queryString
, MatchMode matchmode
, List
<Criterion
> criteria
,
884 Integer pageSize
,Integer pageNumber
, List
<OrderHint
> orderHints
,List
<String
> propertyPaths
) {
885 Long numberOfResults
= dao
.countByName(clazz
, queryString
, matchmode
, criteria
);
887 List
<TaxonName
> results
= new ArrayList
<>();
888 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
889 results
= dao
.findByName(clazz
, queryString
, matchmode
, criteria
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
892 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
896 public HomotypicalGroup
findHomotypicalGroup(UUID uuid
) {
897 return homotypicalGroupDao
.findByUuid(uuid
);
901 @Transactional(readOnly
= false)
902 public UpdateResult
updateCaches(Class
<?
extends TaxonName
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<TaxonName
> cacheStrategy
, IProgressMonitor monitor
) {
904 clazz
= TaxonName
.class;
906 return super.updateCachesImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
911 public List
<TaggedText
> getTaggedName(UUID uuid
) {
912 TaxonName taxonName
= dao
.load(uuid
);
913 List
<TaggedText
> taggedName
= taxonName
.getTaggedName();
918 public DeleteResult
isDeletable(TaxonName name
, DeleteConfiguratorBase config
){
919 DeleteResult result
= new DeleteResult();
921 NameDeletionConfigurator nameConfig
= null;
922 if (config
instanceof NameDeletionConfigurator
){
923 nameConfig
= (NameDeletionConfigurator
) config
;
925 result
.addException(new Exception("The delete configurator should be of the type NameDeletionConfigurator."));
930 if (!name
.getNameRelations().isEmpty() && !nameConfig
.isRemoveAllNameRelationships()){
931 HomotypicalGroup homotypicalGroup
= HibernateProxyHelper
.deproxy(name
.getHomotypicalGroup(), HomotypicalGroup
.class);
933 if (!nameConfig
.isIgnoreIsBasionymFor() && homotypicalGroup
.getBasionyms().contains(name
)){
934 result
.addException(new Exception( "Name can't be deleted as it is a basionym."));
937 if (!nameConfig
.isIgnoreHasBasionym() && (name
.getBasionyms().size()>0)){
938 result
.addException(new Exception( "Name can't be deleted as it has a basionym."));
941 Set
<NameRelationship
> relationships
= name
.getNameRelations();
942 for (NameRelationship rel
: relationships
){
943 if (!rel
.getType().equals(NameRelationshipType
.BASIONYM())){
944 result
.addException(new Exception("Name can't be deleted as it is used in name relationship(s). Remove name relationships prior to deletion."));
952 if (! name
.getTaxonBases().isEmpty()){
953 result
.addException(new Exception("Name can't be deleted as it is used in concept(s). Remove or change concept prior to deletion."));
957 //hybrid relationships
958 if (name
.isNonViral()){
959 INonViralName nvn
= name
;
960 Set
<HybridRelationship
> parentHybridRelations
= nvn
.getHybridParentRelations();
961 //Hibernate.initialize(parentHybridRelations);
962 if (! parentHybridRelations
.isEmpty()){
963 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."));
967 Set
<CdmBase
> referencingObjects
= genericDao
.getReferencingObjectsForDeletion(name
);
968 for (CdmBase referencingObject
: referencingObjects
){
969 //DerivedUnit?.storedUnder
970 if (referencingObject
.isInstanceOf(DerivedUnit
.class)){
971 String message
= "Name can't be deleted as it is used as derivedUnit#storedUnder by %s. Remove 'stored under' prior to deleting this name";
972 message
= String
.format(message
, CdmBase
.deproxy(referencingObject
, DerivedUnit
.class).getTitleCache());
973 result
.addException(new ReferencedObjectUndeletableException(message
));
974 result
.addRelatedObject(referencingObject
);
977 //DescriptionElementSource#nameUsedInSource
978 else if (referencingObject
.isInstanceOf(DescriptionElementSource
.class)){
979 String message
= "Name can't be deleted as it is used as descriptionElementSource#nameUsedInSource";
980 result
.addException(new ReferencedObjectUndeletableException(message
));
981 result
.addRelatedObject(referencingObject
);
984 //NameTypeDesignation#typeName
985 else if (referencingObject
.isInstanceOf(NameTypeDesignation
.class)){
986 NameTypeDesignation typeDesignation
= HibernateProxyHelper
.deproxy(referencingObject
, NameTypeDesignation
.class);
988 if (typeDesignation
.getTypeName().equals(name
)){
989 String message
= "Name can't be deleted as it is used as a name type in a NameTypeDesignation";
990 result
.addException(new ReferencedObjectUndeletableException(message
));
991 result
.addRelatedObject(referencingObject
);
995 //DeterminationEvent#taxonName
996 else if (referencingObject
.isInstanceOf(DeterminationEvent
.class)){
997 String message
= "Name can't be deleted as it is used as a determination event";
998 result
.addException(new ReferencedObjectUndeletableException(message
));
999 result
.addRelatedObject(referencingObject
);
1004 //TODO inline references
1007 if (!nameConfig
.isIgnoreIsReplacedSynonymFor() && name
.isReplacedSynonym()){
1008 String message
= "Name can't be deleted as it is a replaced synonym.";
1009 result
.addException(new Exception(message
));
1012 if (!nameConfig
.isIgnoreHasReplacedSynonym() && (name
.getReplacedSynonyms().size()>0)){
1013 String message
= "Name can't be deleted as it has a replaced synonym.";
1014 result
.addException(new Exception(message
));
1023 public DeleteResult
isDeletable(UUID nameUUID
, DeleteConfiguratorBase config
){
1024 TaxonName name
= this.load(nameUUID
);
1025 return isDeletable(name
, config
);
1029 @Transactional(readOnly
= true)
1030 public UpdateResult
setAsGroupsBasionym(UUID nameUuid
) {
1031 TaxonName name
= dao
.load(nameUuid
);
1032 UpdateResult result
= new UpdateResult();
1033 name
.setAsGroupsBasionym();
1034 result
.addUpdatedObject(name
);
1040 public List
<HashMap
<String
,String
>> getNameRecords(){
1041 return dao
.getNameRecords();
1046 public List
<TypeDesignationStatusBase
> getTypeDesignationStatusInUse(){
1047 return typeDesignationDao
.getTypeDesignationStatusInUse();
1051 public Collection
<TypeDesignationStatusFilter
> getTypeDesignationStatusFilterTerms(List
<Language
> preferredLanguages
){
1052 List
<TypeDesignationStatusBase
> termList
= typeDesignationDao
.getTypeDesignationStatusInUse();
1053 Map
<String
, TypeDesignationStatusFilter
> filterMap
= new HashMap
<>();
1054 for(TypeDesignationStatusBase term
: termList
){
1055 TypeDesignationStatusFilter filter
= new TypeDesignationStatusFilter(term
, preferredLanguages
, true);
1056 String key
= filter
.getKey();
1057 if(filterMap
.containsKey(key
)){
1058 filterMap
.get(key
).addStatus(term
);
1060 filterMap
.put(key
, filter
);
1063 return filterMap
.values();
1067 public <S
extends TaxonName
> Pager
<S
> page(Class
<S
> clazz
, List
<Restriction
<?
>> restrictions
, Integer pageSize
,
1068 Integer pageIndex
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
1069 return page(clazz
, restrictions
, pageSize
, pageIndex
, orderHints
, propertyPaths
, INCLUDE_UNPUBLISHED
);
1073 public <S
extends TaxonName
> Pager
<S
> page(Class
<S
> clazz
, List
<Restriction
<?
>> restrictions
, Integer pageSize
,
1074 Integer pageIndex
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
, boolean includeUnpublished
) {
1077 long resultSize
= dao
.count(clazz
, restrictions
);
1078 if(AbstractPagerImpl
.hasResultsInRange(resultSize
, pageIndex
, pageSize
)){
1079 records
= dao
.list(clazz
, restrictions
, pageSize
, pageIndex
, orderHints
, propertyPaths
, includeUnpublished
);
1081 records
= new ArrayList
<>();
1083 Pager
<S
> pager
= new DefaultPagerImpl
<>(pageIndex
, resultSize
, pageSize
, records
);