use of eu.etaxonomy.cdm.model.reference.CdmLinkSource in project cdmlib by cybertaxonomy.
the class DescriptiveDataSetService method removeDescriptionFromDataSet.
private void removeDescriptionFromDataSet(DeleteResult result, DescriptiveDataSet dataSet, DescriptionBase<?> description, RemoveDescriptionsFromDescriptiveDataSetConfigurator config) {
if (description == null) {
return;
}
boolean success = dataSet.removeDescription(description);
// remove taxon description with IndividualsAssociation from data set
result.addDeletedObject(description);
if (description instanceof SpecimenDescription) {
@SuppressWarnings({ "unchecked", "cast" }) Set<IndividualsAssociation> // NOTE: don't remove cast as it does not compile on some systems
associations = (Set<IndividualsAssociation>) dataSet.getDescriptions().stream().flatMap(// put all description element in one stream
desc -> desc.getElements().stream()).filter(element -> element instanceof IndividualsAssociation).map(ia -> (IndividualsAssociation) ia).collect(Collectors.toSet());
for (IndividualsAssociation individualsAssociation : associations) {
if (individualsAssociation.getAssociatedSpecimenOrObservation().equals(description.getDescribedSpecimenOrObservation())) {
dataSet.removeDescription(individualsAssociation.getInDescription());
result.addUpdatedObject(individualsAssociation.getInDescription());
}
}
}
if (description instanceof TaxonDescription) {
DeleteResult isDeletable = descriptionService.isDeletable(description.getUuid());
for (CdmBase relatedCdmBase : isDeletable.getRelatedObjects()) {
if (relatedCdmBase instanceof CdmLinkSource) {
CdmLinkSource linkSource = (CdmLinkSource) relatedCdmBase;
if (linkSource.getTarget().equals(this)) {
}
}
}
}
if (!config.isOnlyRemoveDescriptionsFromDataSet()) {
DeleteResult deleteResult = descriptionService.deleteDescription(description);
result.includeResult(deleteResult);
result.addUpdatedObject(dataSet);
} else {
MergeResult<DescriptiveDataSet> mergeResult = dao.merge(dataSet, true);
result.addUpdatedObject(mergeResult.getMergedEntity());
}
result.setStatus(success ? Status.OK : Status.ERROR);
}
use of eu.etaxonomy.cdm.model.reference.CdmLinkSource in project cdmlib by cybertaxonomy.
the class DescriptionServiceImpl method isDeletable.
@Override
public DeleteResult isDeletable(UUID descriptionUuid) {
DeleteResult result = new DeleteResult();
DescriptionBase<?> description = this.load(descriptionUuid);
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(description);
if (references == null || references.isEmpty()) {
return result;
}
for (CdmBase ref : references) {
if (description instanceof TaxonDescription && ref instanceof Taxon && ref.equals(((TaxonDescription) description).getTaxon())) {
continue;
} else if (description instanceof TaxonNameDescription && ref instanceof TaxonName && ref.equals(((TaxonNameDescription) description).getTaxonName())) {
continue;
} else if (description instanceof SpecimenDescription && ref instanceof SpecimenOrObservationBase && ref.equals(((SpecimenDescription) description).getDescribedSpecimenOrObservation())) {
continue;
} else if (ref instanceof DescriptionElementBase) {
continue;
} else if (ref instanceof CdmLinkSource && ((CdmLinkSource) ref).hasNoTarget()) {
// maybe only workaround #9801
continue;
} else {
String message = "The description can't be completely deleted because it is referenced by " + ref.getUserFriendlyTypeName();
result.setAbort();
result.addException(new ReferencedObjectUndeletableException(message));
result.addRelatedObject(ref);
}
}
return result;
}
use of eu.etaxonomy.cdm.model.reference.CdmLinkSource in project cdmlib by cybertaxonomy.
the class DescriptionServiceImpl method deleteDescription.
@Override
@Transactional(readOnly = false)
public DeleteResult deleteDescription(DescriptionBase<?> description) {
DeleteResult deleteResult = new DeleteResult();
if (description == null) {
return deleteResult;
}
// avoid lazy init exception
description = load(description.getId(), Arrays.asList("descriptiveDataSets"));
deleteResult = isDeletable(description.getUuid());
if (deleteResult.getRelatedObjects() != null && deleteResult.getRelatedObjects().size() == 1) {
Iterator<CdmBase> relObjects = deleteResult.getRelatedObjects().iterator();
CdmBase next = relObjects.next();
if (next instanceof CdmLinkSource) {
CdmLinkSource source = (CdmLinkSource) next;
ICdmTarget target = source.getTarget();
}
}
if (deleteResult.isOk()) {
CdmBase.deproxy(description);
if (description instanceof TaxonDescription) {
TaxonDescription taxDescription = (TaxonDescription) description;
Taxon tax = taxDescription.getTaxon();
if (tax != null) {
tax.removeDescription(taxDescription, true);
deleteResult.addUpdatedObject(tax);
}
} else if (description instanceof SpecimenDescription) {
SpecimenDescription specimenDescription = (SpecimenDescription) description;
SpecimenOrObservationBase<?> specimen = specimenDescription.getDescribedSpecimenOrObservation();
if (specimen != null) {
specimen.removeDescription(specimenDescription);
deleteResult.addUpdatedObject(specimen);
}
}
for (DescriptiveDataSet dataset : description.getDescriptiveDataSets()) {
dataset.removeDescription(description);
}
dao.delete(description);
deleteResult.addDeletedObject(description);
deleteResult.setCdmEntity(description);
} else {
logger.info(deleteResult.getExceptions().toString());
}
return deleteResult;
}
Aggregations