Search in sources :

Example 1 with DeleteConfiguratorBase

use of eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase in project cdmlib by cybertaxonomy.

the class NameServiceImpl method isDeletable.

public DeleteResult isDeletable(TaxonName name, DeleteConfiguratorBase config, UUID taxonUuid) {
    DeleteResult result = new DeleteResult();
    NameDeletionConfigurator nameConfig = null;
    if (config instanceof NameDeletionConfigurator) {
        nameConfig = (NameDeletionConfigurator) config;
    } else {
        result.addException(new Exception("The delete configurator should be of the type NameDeletionConfigurator."));
        result.setError();
        return result;
    }
    if (!name.getNameRelations().isEmpty() && !nameConfig.isRemoveAllNameRelationships()) {
        HomotypicalGroup homotypicalGroup = HibernateProxyHelper.deproxy(name.getHomotypicalGroup(), HomotypicalGroup.class);
        if (!nameConfig.isIgnoreIsBasionymFor() && homotypicalGroup.getBasionyms().contains(name)) {
            result.addException(new Exception("Name can't be deleted as it is a basionym."));
            result.setAbort();
        }
        if (!nameConfig.isIgnoreHasBasionym() && (name.getBasionyms().size() > 0)) {
            result.addException(new Exception("Name can't be deleted as it has a basionym."));
            result.setAbort();
        }
        Set<NameRelationship> relationships = name.getNameRelations();
        for (NameRelationship rel : relationships) {
            if (!rel.getType().equals(NameRelationshipType.BASIONYM())) {
                result.addException(new Exception("Name can't be deleted as it is used in name relationship(s). Remove name relationships prior to deletion."));
                result.setAbort();
                break;
            }
        }
    }
    // concepts
    if (!name.getTaxonBases().isEmpty()) {
        boolean isDeletableTaxon = true;
        List<TaxonBase> notDeletedTaxonBases = name.getTaxonBases().stream().filter((taxonBase) -> !taxonBase.getUuid().equals(taxonUuid)).collect(Collectors.toList());
        if (!notDeletedTaxonBases.isEmpty()) {
            result.addException(new Exception("Name can't be deleted as it is used in concept(s). Remove or change concept prior to deletion."));
            result.setAbort();
        }
    }
    // hybrid relationships
    if (name.isNonViral()) {
        INonViralName nvn = name;
        Set<HybridRelationship> parentHybridRelations = nvn.getHybridParentRelations();
        // Hibernate.initialize(parentHybridRelations);
        if (!parentHybridRelations.isEmpty()) {
            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."));
            result.setAbort();
        }
    }
    Set<CdmBase> referencingObjects = genericDao.getReferencingObjectsForDeletion(name);
    for (CdmBase referencingObject : referencingObjects) {
        // DerivedUnit?.storedUnder
        if (referencingObject.isInstanceOf(DerivedUnit.class)) {
            String message = "Name can't be deleted as it is used as derivedUnit#storedUnder by %s. Remove 'stored under' prior to deleting this name";
            message = String.format(message, CdmBase.deproxy(referencingObject, DerivedUnit.class).getTitleCache());
            result.addException(new ReferencedObjectUndeletableException(message));
            result.addRelatedObject(referencingObject);
            result.setAbort();
        } else // DescriptionElementSource#nameUsedInSource
        if (referencingObject.isInstanceOf(DescriptionElementSource.class) && !referencingObject.isInstanceOf(NomenclaturalSource.class)) {
            String message = "Name can't be deleted as it is used as descriptionElementSource#nameUsedInSource";
            result.addException(new ReferencedObjectUndeletableException(message));
            result.addRelatedObject(referencingObject);
            result.setAbort();
        } else // NameTypeDesignation#typeName
        if (referencingObject.isInstanceOf(NameTypeDesignation.class)) {
            NameTypeDesignation typeDesignation = HibernateProxyHelper.deproxy(referencingObject, NameTypeDesignation.class);
            if (typeDesignation.getTypeName().equals(name) && !typeDesignation.getTypifiedNames().isEmpty()) {
                String message = "Name can't be deleted as it is used as a name type in a NameTypeDesignation";
                result.addException(new ReferencedObjectUndeletableException(message));
                result.addRelatedObject(referencingObject);
                result.setAbort();
            }
        } else // DeterminationEvent#taxonName
        if (referencingObject.isInstanceOf(DeterminationEvent.class)) {
            String message = "Name can't be deleted as it is used as a determination event";
            result.addException(new ReferencedObjectUndeletableException(message));
            result.addRelatedObject(referencingObject);
            result.setAbort();
        } else // original spelling
        if (referencingObject.isInstanceOf(NomenclaturalSource.class) && !((NameDeletionConfigurator) config).isIgnoreIsOriginalSpellingFor()) {
            if (((NomenclaturalSource) referencingObject).getNameUsedInSource() != null && ((NomenclaturalSource) referencingObject).getNameUsedInSource().equals(name)) {
                String message = "Name can't be deleted as it is used as original spelling";
                result.addException(new ReferencedObjectUndeletableException(message));
                result.addRelatedObject(referencingObject);
                result.setAbort();
            }
        }
        if (referencingObject.isInstanceOf(NomenclaturalSource.class)) {
            if (((NomenclaturalSource) referencingObject).getNameUsedInSource() != null && ((NomenclaturalSource) referencingObject).getNameUsedInSource().equals(name)) {
                result.addRelatedObject(referencingObject);
            }
        }
    }
    if (!nameConfig.isIgnoreIsReplacedSynonymFor() && name.isReplacedSynonym()) {
        String message = "Name can't be deleted as it is a replaced synonym.";
        result.addException(new Exception(message));
        result.setAbort();
    }
    if (!nameConfig.isIgnoreHasReplacedSynonym() && (name.getReplacedSynonyms().size() > 0)) {
        String message = "Name can't be deleted as it has a replaced synonym.";
        result.addException(new Exception(message));
        result.setAbort();
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Pager(eu.etaxonomy.cdm.api.service.pager.Pager) DocumentSearchResult(eu.etaxonomy.cdm.api.service.search.DocumentSearchResult) Term(org.apache.lucene.index.Term) Builder(org.apache.lucene.search.BooleanQuery.Builder) Restriction(eu.etaxonomy.cdm.persistence.dao.common.Restriction) Autowired(org.springframework.beans.factory.annotation.Autowired) IHomotypicalGroupDao(eu.etaxonomy.cdm.persistence.dao.name.IHomotypicalGroupDao) LuceneParseException(eu.etaxonomy.cdm.api.service.search.LuceneParseException) SpecimenTypeDesignation(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation) SourcedEntityBase(eu.etaxonomy.cdm.model.common.SourcedEntityBase) INonViralName(eu.etaxonomy.cdm.model.name.INonViralName) HybridRelationship(eu.etaxonomy.cdm.model.name.HybridRelationship) Map(java.util.Map) IOriginalSourceDao(eu.etaxonomy.cdm.persistence.dao.reference.IOriginalSourceDao) NameDeletionConfigurator(eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator) CdmBaseType(eu.etaxonomy.cdm.model.CdmBaseType) Occur(org.apache.lucene.search.BooleanClause.Occur) Set(java.util.Set) FuzzyLikeThisQuery(org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TeamOrPersonBase(eu.etaxonomy.cdm.model.agent.TeamOrPersonBase) DerivationEvent(eu.etaxonomy.cdm.model.occurrence.DerivationEvent) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) Person(eu.etaxonomy.cdm.model.agent.Person) HomotypicalGroup(eu.etaxonomy.cdm.model.name.HomotypicalGroup) Team(eu.etaxonomy.cdm.model.agent.Team) ITaxonNameDao(eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao) Reference(eu.etaxonomy.cdm.model.reference.Reference) TaxonNamePartsFilter(eu.etaxonomy.cdm.api.util.TaxonNamePartsFilter) SpecimenTypeDesignationStatus(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus) UuidAndTitleCache(eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache) ArrayList(java.util.ArrayList) DeleteConfiguratorBase(eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase) NameTypeDesignation(eu.etaxonomy.cdm.model.name.NameTypeDesignation) Service(org.springframework.stereotype.Service) NomenclaturalCode(eu.etaxonomy.cdm.model.name.NomenclaturalCode) HybridRelationshipType(eu.etaxonomy.cdm.model.name.HybridRelationshipType) TopDocs(org.apache.lucene.search.TopDocs) Criterion(org.hibernate.criterion.Criterion) AbstractPagerImpl(eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl) IOException(java.io.IOException) IBeanInitializer(eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer) TypeDesignationStatusFilter(eu.etaxonomy.cdm.api.service.dto.TypeDesignationStatusFilter) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) Language(eu.etaxonomy.cdm.model.common.Language) QueryFactory(eu.etaxonomy.cdm.api.service.search.QueryFactory) URI(eu.etaxonomy.cdm.common.URI) Direction(eu.etaxonomy.cdm.model.common.RelationshipBase.Direction) IProgressMonitor(eu.etaxonomy.cdm.common.monitor.IProgressMonitor) ISourcedEntityDao(eu.etaxonomy.cdm.persistence.dao.common.ISourcedEntityDao) DefaultPagerImpl(eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl) TaxonNameParts(eu.etaxonomy.cdm.persistence.dto.TaxonNameParts) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) LuceneSearch(eu.etaxonomy.cdm.api.service.search.LuceneSearch) TypeDesignationStatusBase(eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase) Logger(org.apache.log4j.Logger) MatchStrategyFactory(eu.etaxonomy.cdm.strategy.match.MatchStrategyFactory) NomenclaturalSource(eu.etaxonomy.cdm.model.name.NomenclaturalSource) TaxonBase(eu.etaxonomy.cdm.model.taxon.TaxonBase) ILuceneIndexToolProvider(eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider) NameRelationshipType(eu.etaxonomy.cdm.model.name.NameRelationshipType) ITypeDesignationDao(eu.etaxonomy.cdm.persistence.dao.name.ITypeDesignationDao) ReferencedObjectUndeletableException(eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException) DeterminationEvent(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) DescriptionElementSource(eu.etaxonomy.cdm.model.description.DescriptionElementSource) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) IMatchStrategy(eu.etaxonomy.cdm.strategy.match.IMatchStrategy) Optional(java.util.Optional) ICdmGenericDao(eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao) OrderHint(eu.etaxonomy.cdm.persistence.query.OrderHint) MatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode) IIdentifiableEntityCacheStrategy(eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy) NonViralNameParserImpl(eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) HashMap(java.util.HashMap) HashSet(java.util.HashSet) TaxonNameFactory(eu.etaxonomy.cdm.model.name.TaxonNameFactory) Qualifier(org.springframework.beans.factory.annotation.Qualifier) NameRelationship(eu.etaxonomy.cdm.model.name.NameRelationship) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) Rank(eu.etaxonomy.cdm.model.name.Rank) ISearchResultBuilder(eu.etaxonomy.cdm.api.service.search.ISearchResultBuilder) HibernateProxyHelper(eu.etaxonomy.cdm.hibernate.HibernateProxyHelper) IParsedMatchStrategy(eu.etaxonomy.cdm.strategy.match.IParsedMatchStrategy) SearchResult(eu.etaxonomy.cdm.api.service.search.SearchResult) TypeDesignationBase(eu.etaxonomy.cdm.model.name.TypeDesignationBase) Registration(eu.etaxonomy.cdm.model.name.Registration) TaggedText(eu.etaxonomy.cdm.strategy.cache.TaggedText) MatchException(eu.etaxonomy.cdm.strategy.match.MatchException) INomenclaturalStatusDao(eu.etaxonomy.cdm.persistence.dao.name.INomenclaturalStatusDao) BooleanQuery(org.apache.lucene.search.BooleanQuery) IMatchable(eu.etaxonomy.cdm.strategy.match.IMatchable) SearchResultBuilder(eu.etaxonomy.cdm.api.service.search.SearchResultBuilder) Transactional(org.springframework.transaction.annotation.Transactional) NomenclaturalStatus(eu.etaxonomy.cdm.model.name.NomenclaturalStatus) INonViralName(eu.etaxonomy.cdm.model.name.INonViralName) NameRelationship(eu.etaxonomy.cdm.model.name.NameRelationship) HybridRelationship(eu.etaxonomy.cdm.model.name.HybridRelationship) ReferencedObjectUndeletableException(eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException) DeterminationEvent(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent) LuceneParseException(eu.etaxonomy.cdm.api.service.search.LuceneParseException) IOException(java.io.IOException) ReferencedObjectUndeletableException(eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException) MatchException(eu.etaxonomy.cdm.strategy.match.MatchException) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) NameTypeDesignation(eu.etaxonomy.cdm.model.name.NameTypeDesignation) TaxonBase(eu.etaxonomy.cdm.model.taxon.TaxonBase) HomotypicalGroup(eu.etaxonomy.cdm.model.name.HomotypicalGroup) NomenclaturalSource(eu.etaxonomy.cdm.model.name.NomenclaturalSource) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) NameDeletionConfigurator(eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator)

Aggregations

DeleteConfiguratorBase (eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase)1 NameDeletionConfigurator (eu.etaxonomy.cdm.api.service.config.NameDeletionConfigurator)1 TypeDesignationStatusFilter (eu.etaxonomy.cdm.api.service.dto.TypeDesignationStatusFilter)1 ReferencedObjectUndeletableException (eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException)1 Pager (eu.etaxonomy.cdm.api.service.pager.Pager)1 AbstractPagerImpl (eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl)1 DefaultPagerImpl (eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl)1 DocumentSearchResult (eu.etaxonomy.cdm.api.service.search.DocumentSearchResult)1 ILuceneIndexToolProvider (eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider)1 ISearchResultBuilder (eu.etaxonomy.cdm.api.service.search.ISearchResultBuilder)1 LuceneParseException (eu.etaxonomy.cdm.api.service.search.LuceneParseException)1 LuceneSearch (eu.etaxonomy.cdm.api.service.search.LuceneSearch)1 QueryFactory (eu.etaxonomy.cdm.api.service.search.QueryFactory)1 SearchResult (eu.etaxonomy.cdm.api.service.search.SearchResult)1 SearchResultBuilder (eu.etaxonomy.cdm.api.service.search.SearchResultBuilder)1 TaxonNamePartsFilter (eu.etaxonomy.cdm.api.util.TaxonNamePartsFilter)1 URI (eu.etaxonomy.cdm.common.URI)1 IProgressMonitor (eu.etaxonomy.cdm.common.monitor.IProgressMonitor)1 HibernateProxyHelper (eu.etaxonomy.cdm.hibernate.HibernateProxyHelper)1 CdmBaseType (eu.etaxonomy.cdm.model.CdmBaseType)1