import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;\r
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;\r
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;\r
+import eu.etaxonomy.cdm.api.service.DeleteResult.DeleteStatus;\r
+import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;\r
import eu.etaxonomy.cdm.api.service.dto.DerivateHierarchyDTO;\r
import eu.etaxonomy.cdm.api.service.dto.DerivateHierarchyDTO.ContigFile;\r
import eu.etaxonomy.cdm.api.service.dto.DerivateHierarchyDTO.MolecularData;\r
+import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;\r
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;\r
import eu.etaxonomy.cdm.api.service.pager.Pager;\r
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;\r
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;\r
import eu.etaxonomy.cdm.model.CdmBaseType;\r
import eu.etaxonomy.cdm.model.agent.AgentBase;\r
+import eu.etaxonomy.cdm.model.common.CdmBase;\r
import eu.etaxonomy.cdm.model.common.DefinedTerm;\r
import eu.etaxonomy.cdm.model.common.DefinedTermBase;\r
import eu.etaxonomy.cdm.model.common.ICdmBase;\r
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;\r
import eu.etaxonomy.cdm.model.description.DescriptionBase;\r
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;\r
+import eu.etaxonomy.cdm.model.description.DescriptionElementSource;\r
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;\r
+import eu.etaxonomy.cdm.model.description.TaxonDescription;\r
import eu.etaxonomy.cdm.model.location.Country;\r
import eu.etaxonomy.cdm.model.location.NamedArea;\r
import eu.etaxonomy.cdm.model.media.Media;\r
import eu.etaxonomy.cdm.model.molecular.DnaSample;\r
import eu.etaxonomy.cdm.model.molecular.Sequence;\r
import eu.etaxonomy.cdm.model.molecular.SingleRead;\r
+import eu.etaxonomy.cdm.model.name.NameTypeDesignation;\r
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;\r
import eu.etaxonomy.cdm.model.name.TaxonNameBase;\r
+import eu.etaxonomy.cdm.model.name.TypeDesignationBase;\r
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;\r
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;\r
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;\r
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;\r
import eu.etaxonomy.cdm.model.taxon.Taxon;\r
import eu.etaxonomy.cdm.model.taxon.TaxonBase;\r
+import eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao;\r
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;\r
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;\r
import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;\r
@Autowired\r
private ILuceneIndexToolProvider luceneIndexToolProvider;\r
\r
+ @Autowired\r
+ private ICdmGenericDao genericDao;\r
+\r
\r
public OccurrenceServiceImpl() {\r
logger.debug("Load OccurrenceService Bean");\r
\r
}\r
\r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#pageByAssociatedTaxon(java.lang.Class, java.util.Set, eu.etaxonomy.cdm.model.taxon.Taxon, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)\r
- */\r
- @SuppressWarnings("unchecked")\r
+\r
@Override\r
public <T extends SpecimenOrObservationBase> Pager<T> pageByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,\r
String taxonUUID, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {\r
UUID uuid = UUID.fromString(taxonUUID);\r
Taxon tax = (Taxon) taxonService.load(uuid);\r
//TODO REMOVE NULL STATEMENT\r
-type=null;\r
+ type=null;\r
return pageByAssociatedTaxon( type,includeRelationships,tax, maxDepth, pageSize, pageNumber, orderHints, propertyPaths );\r
\r
}\r
from.removeDerivationEvent(eventToRemove);\r
saveOrUpdate(from);\r
//add new derivation event to target\r
- to.addDerivationEvent(DerivationEvent.NewSimpleInstance(to, derivate, eventToRemove==null?null:eventToRemove.getType()));\r
+ DerivationEvent derivedFromNewOriginalEvent = DerivationEvent.NewSimpleInstance(to, derivate, eventToRemove==null?null:eventToRemove.getType());\r
+ to.addDerivationEvent(derivedFromNewOriginalEvent);\r
+ derivate.setDerivedFrom(derivedFromNewOriginalEvent);\r
saveOrUpdate(to);\r
return true;\r
}\r
return nonCascadedCdmEntities;\r
}\r
\r
+\r
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#delete(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase, eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator)\r
+ */\r
+ @Override\r
+ public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {\r
+ DeleteResult deleteResult = new DeleteResult();\r
+ //check for derivation events\r
+ Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();\r
+ for (DerivationEvent derivationEvent : derivationEvents) {\r
+ if(!derivationEvent.getDerivatives().isEmpty()){\r
+ deleteResult.setAbort();\r
+ deleteResult.addException(new ReferencedObjectUndeletableException("Derivate with children cannot be deleted."));\r
+ return deleteResult;\r
+ }\r
+ }\r
+ //check for original (parent derivate)\r
+ if(specimen instanceof DerivedUnit){\r
+ DerivationEvent derivedFromEvent = ((DerivedUnit) specimen).getDerivedFrom();\r
+ if(derivedFromEvent!=null){\r
+ derivedFromEvent.removeDerivative((DerivedUnit) specimen);\r
+ }\r
+ }\r
+ //check for IndividualsAssociation (e.g. in TaxonDescriptions)\r
+ Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimen, null, null, null, null);\r
+ if(!associatedTaxa.isEmpty()){\r
+ if(config.isDeleteFromIndividualsAssociation()){\r
+ for (TaxonBase<?> taxonBase : associatedTaxa) {\r
+ if(taxonBase instanceof Taxon){\r
+ Set<TaxonDescription> descriptions = ((Taxon) taxonBase).getDescriptions();\r
+ for (TaxonDescription taxonDescription : descriptions) {\r
+ Set<DescriptionElementBase> elements = taxonDescription.getElements();\r
+ for (DescriptionElementBase descriptionElementBase : elements) {\r
+ if(descriptionElementBase instanceof IndividualsAssociation){\r
+ IndividualsAssociation individualsAssociation = (IndividualsAssociation) descriptionElementBase;\r
+ if(individualsAssociation.getAssociatedSpecimenOrObservation().equals(specimen)){\r
+ individualsAssociation.setAssociatedSpecimenOrObservation(null);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else{\r
+ deleteResult.addRelatedObjects(new HashSet<CdmBase>(associatedTaxa));\r
+ deleteResult.setAbort();\r
+ deleteResult.addException(new ReferencedObjectUndeletableException("Specimen is still associated with taxa."));\r
+ return deleteResult;\r
+ }\r
+ }\r
+ //check for TypeDesignations\r
+ Collection<TaxonBase<?>> typedTaxa = listTypedTaxa(specimen, null, null, null, null);\r
+ if(!typedTaxa.isEmpty()){\r
+ if(config.isdeleteFromTypeDesignation()){\r
+ for (TaxonBase<?> taxonBase : typedTaxa) {\r
+ if(taxonBase.getName()!=null){\r
+ Set<TypeDesignationBase> typeDesignations = taxonBase.getName().getTypeDesignations();\r
+ for (TypeDesignationBase typeDesignationBase : typeDesignations) {\r
+ if(typeDesignationBase instanceof SpecimenTypeDesignation){\r
+ ((SpecimenTypeDesignation) typeDesignationBase).setTypeSpecimen(null);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else{\r
+ deleteResult.addRelatedObjects(new HashSet<CdmBase>(typedTaxa));\r
+ deleteResult.setAbort();\r
+ deleteResult.addException(new ReferencedObjectUndeletableException("Specimen is a type specimen."));\r
+ return deleteResult;\r
+ }\r
+ }\r
+ if(!config.isDeleteChildren()){\r
+ Set<CdmBase> referencingObjects = genericDao.getReferencingObjects(specimen);\r
+ for (CdmBase referencingObject : referencingObjects){\r
+ //DerivedUnit?.storedUnder\r
+ if (referencingObject.isInstanceOf(DerivedUnit.class)){\r
+ String message = "Name can't be deleted as it is used as derivedUnit#storedUnder by %s. Remove 'stored under' prior to deleting this name";\r
+ message = String.format(message, CdmBase.deproxy(referencingObject, DerivedUnit.class).getTitleCache());\r
+ }\r
+ //DescriptionElementSource#nameUsedInSource\r
+ if (referencingObject.isInstanceOf(DescriptionElementSource.class)){\r
+ String message = "Name can't be deleted as it is used as descriptionElementSource#nameUsedInSource";\r
+ }\r
+ //NameTypeDesignation#typeName\r
+ if (referencingObject.isInstanceOf(NameTypeDesignation.class)){\r
+ String message = "Name can't be deleted as it is used as a name type in a NameTypeDesignation";\r
+ }\r
+ }\r
+ deleteResult = delete(specimen);\r
+ }\r
+ else{\r
+ //TODO implement deep delete\r
+ }\r
+ return deleteResult;\r
+ }\r
+\r
/* (non-Javadoc)\r
* @see eu.etaxonomy.cdm.api.service.IOccurrenceService#deleteDerivateHierarchy(eu.etaxonomy.cdm.model.common.ICdmBase)\r
*/\r
@Override\r
- public DeleteResult deleteDerivateHierarchy(ICdmBase from) {\r
+ public DeleteResult deleteDerivateHierarchy(ICdmBase from, SpecimenDeleteConfigurator config) {\r
DeleteResult deleteResult = new DeleteResult();\r
+ if(from instanceof Sequence){\r
+ Sequence sequence = (Sequence)from;\r
+ sequence.getDnaSample().removeSequence(sequence);\r
+ deleteResult.setStatus(DeleteStatus.OK);\r
+ }\r
+ else if(from instanceof SpecimenOrObservationBase<?>) {\r
+ deleteResult = delete((SpecimenOrObservationBase<?>) from, config);\r
+ }\r
return deleteResult;\r
}\r
\r
+ private Set<ICdmBase> collectEntitiesToDelete(ICdmBase entity){\r
+ Set<ICdmBase> entitiesToDelete = new LinkedHashSet<ICdmBase>();\r
+\r
+ if(entity instanceof SpecimenOrObservationBase<?>){\r
+ SpecimenOrObservationBase<?> specimen = (SpecimenOrObservationBase<?>) entity;\r
+ if(entity instanceof DerivedUnit){\r
+ DerivedUnit derivedUnit = (DerivedUnit)entity;\r
+ DerivationEvent derivedFrom = derivedUnit.getDerivedFrom();\r
+ Set<SpecimenOrObservationBase> originals = derivedFrom.getOriginals();\r
+ for (SpecimenOrObservationBase<?> original: originals) {\r
+ original.removeDerivationEvent(derivedFrom);\r
+// saveOrUpdate(original);\r
+ }\r
+ }\r
+ if(entity instanceof DnaSample && ((DnaSample) entity).getRecordBasis()==SpecimenOrObservationType.DnaSample){\r
+ DnaSample dnaSample = (DnaSample)entity;\r
+ for (Sequence sequence : dnaSample.getSequences()) {\r
+ entitiesToDelete.addAll(collectEntitiesToDelete(sequence));\r
+ dnaSample.removeSequence(sequence);\r
+// saveOrUpdate(dnaSample);\r
+ }\r
+ }\r
+ Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();\r
+ for (DerivationEvent derivationEvent : derivationEvents) {\r
+ for (DerivedUnit derivedUnit : derivationEvent.getDerivatives()) {\r
+ entitiesToDelete.addAll(collectEntitiesToDelete(derivedUnit));\r
+ }\r
+ }\r
+ }\r
+ else if(entity instanceof Sequence){\r
+ Sequence sequence = (Sequence)entity;\r
+ for (SingleRead singleRead : sequence.getSingleReads()) {\r
+ entitiesToDelete.addAll(collectEntitiesToDelete(singleRead));\r
+ sequence.removeSingleRead(singleRead);\r
+ }\r
+// sequenceService.saveOrUpdate(sequence);\r
+ }\r
+ entitiesToDelete.add(entity);\r
+ return entitiesToDelete;\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#listAssociatedTaxa(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase)\r
+ */\r
+ @Override\r
+ public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {\r
+ return dao.listAssociatedTaxa(specimen, null, null, null, null);\r
+ }\r
+\r
+ @Override\r
+ public Collection<TaxonBase<?>> listTypedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {\r
+ return dao.listTypedTaxa(specimen, limit, start, orderHints, propertyPaths);\r
+ }\r
+\r
}\r