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;
}
Aggregations