Search in sources :

Example 26 with SpecimenDescription

use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.

the class FieldUnitDefaultCacheStrategyTest method addPlantDescription.

private void addPlantDescription(FieldUnit fieldUnit, String plantDescription) {
    SpecimenDescription description = SpecimenDescription.NewInstance(fieldUnit);
    TextData textData = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
    description.addElement(textData);
}
Also used : SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) TextData(eu.etaxonomy.cdm.model.description.TextData)

Example 27 with SpecimenDescription

use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.

the class TaxonServiceImpl method listMedia.

@Override
public List<Media> listMedia(Taxon taxon, Set<TaxonRelationshipEdge> includeRelationships, Boolean limitToGalleries, Boolean includeTaxonDescriptions, Boolean includeOccurrences, Boolean includeTaxonNameDescriptions, List<String> propertyPath) {
    // TODO let inherit
    boolean includeUnpublished = INCLUDE_UNPUBLISHED;
    if (logger.isTraceEnabled()) {
        logger.trace("listMedia() - START");
    }
    Set<Taxon> taxa = new HashSet<>();
    List<Media> taxonMedia = new ArrayList<>();
    List<Media> nonImageGalleryImages = new ArrayList<>();
    if (limitToGalleries == null) {
        limitToGalleries = false;
    }
    // --- resolve related taxa
    if (includeRelationships != null && !includeRelationships.isEmpty()) {
        if (logger.isTraceEnabled()) {
            logger.trace("listMedia() - resolve related taxa");
        }
        taxa = listRelatedTaxa(taxon, includeRelationships, null, includeUnpublished, null, null, null);
    }
    taxa.add((Taxon) dao.load(taxon.getUuid()));
    if (includeTaxonDescriptions != null && includeTaxonDescriptions) {
        if (logger.isTraceEnabled()) {
            logger.trace("listMedia() - includeTaxonDescriptions");
        }
        List<TaxonDescription> taxonDescriptions = new ArrayList<>();
        // --- TaxonDescriptions
        for (Taxon t : taxa) {
            taxonDescriptions.addAll(descriptionService.listTaxonDescriptions(t, null, null, null, null, propertyPath));
        }
        for (TaxonDescription taxonDescription : taxonDescriptions) {
            if (!limitToGalleries || taxonDescription.isImageGallery()) {
                for (DescriptionElementBase element : taxonDescription.getElements()) {
                    for (Media media : element.getMedia()) {
                        if (taxonDescription.isImageGallery()) {
                            taxonMedia.add(media);
                        } else {
                            nonImageGalleryImages.add(media);
                        }
                    }
                }
            }
        }
        // put images from image gallery first (#3242)
        taxonMedia.addAll(nonImageGalleryImages);
    }
    if (includeOccurrences != null && includeOccurrences) {
        if (logger.isTraceEnabled()) {
            logger.trace("listMedia() - includeOccurrences");
        }
        @SuppressWarnings("rawtypes") Set<SpecimenOrObservationBase> specimensOrObservations = new HashSet<>();
        // --- Specimens
        for (Taxon t : taxa) {
            specimensOrObservations.addAll(occurrenceDao.listByAssociatedTaxon(null, t, null, null, null, null));
        }
        for (SpecimenOrObservationBase<?> occurrence : specimensOrObservations) {
            // direct media removed from specimen #3597
            // taxonMedia.addAll(occurrence.getMedia());
            // SpecimenDescriptions
            Set<SpecimenDescription> specimenDescriptions = occurrence.getSpecimenDescriptions();
            for (DescriptionBase<?> specimenDescription : specimenDescriptions) {
                if (!limitToGalleries || specimenDescription.isImageGallery()) {
                    Set<DescriptionElementBase> elements = specimenDescription.getElements();
                    for (DescriptionElementBase element : elements) {
                        for (Media media : element.getMedia()) {
                            taxonMedia.add(media);
                        }
                    }
                }
            }
            if (occurrence.isInstanceOf(DerivedUnit.class)) {
                DerivedUnit derivedUnit = CdmBase.deproxy(occurrence, DerivedUnit.class);
                // TODO why may collections have media attached? #
                if (derivedUnit.getCollection() != null) {
                    taxonMedia.addAll(derivedUnit.getCollection().getMedia());
                }
            }
            // media in hierarchy
            taxonMedia.addAll(occurrenceService.getMediaInHierarchy(occurrence, null, null, propertyPath).getRecords());
        }
    }
    if (includeTaxonNameDescriptions != null && includeTaxonNameDescriptions) {
        if (logger.isTraceEnabled()) {
            logger.trace("listMedia() - includeTaxonNameDescriptions");
        }
        // --- TaxonNameDescription
        Set<TaxonNameDescription> nameDescriptions = new HashSet<>();
        for (Taxon t : taxa) {
            nameDescriptions.addAll(t.getName().getDescriptions());
        }
        for (TaxonNameDescription nameDescription : nameDescriptions) {
            if (!limitToGalleries || nameDescription.isImageGallery()) {
                Set<DescriptionElementBase> elements = nameDescription.getElements();
                for (DescriptionElementBase element : elements) {
                    for (Media media : element.getMedia()) {
                        taxonMedia.add(media);
                    }
                }
            }
        }
    }
    taxonMedia = deduplicateMedia(taxonMedia);
    if (logger.isTraceEnabled()) {
        logger.trace("listMedia() - initialize");
    }
    beanInitializer.initializeAll(taxonMedia, propertyPath);
    if (logger.isTraceEnabled()) {
        logger.trace("listMedia() - END");
    }
    return taxonMedia;
}
Also used : SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) TaxonNameDescription(eu.etaxonomy.cdm.model.description.TaxonNameDescription) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) Media(eu.etaxonomy.cdm.model.media.Media) ArrayList(java.util.ArrayList) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) HashSet(java.util.HashSet)

Example 28 with SpecimenDescription

use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.

the class OccurrenceServiceImpl method delete.

@Transactional(readOnly = false)
@Override
public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {
    specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
    DeleteResult deleteResult = isDeletable(specimen.getUuid(), config);
    if (!deleteResult.isOk()) {
        return deleteResult;
    }
    if (config.isDeleteChildren()) {
        Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
        // clone to avoid concurrent modification
        // can happen if the child is deleted and deleted its own derivedFrom event
        Set<DerivationEvent> derivationEventsClone = new HashSet<>(derivationEvents);
        for (DerivationEvent derivationEvent : derivationEventsClone) {
            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
            Iterator<DerivedUnit> it = derivatives.iterator();
            Set<DerivedUnit> derivativesToDelete = new HashSet<>();
            while (it.hasNext()) {
                DerivedUnit unit = it.next();
                derivativesToDelete.add(unit);
            }
            for (DerivedUnit unit : derivativesToDelete) {
                DeleteResult derivedDeleteResult = delete(unit, config);
                deleteResult.includeResult(derivedDeleteResult);
            }
        }
    }
    // check related objects
    Set<CdmBase> relatedObjects = deleteResult.getRelatedObjects();
    for (CdmBase relatedObject : relatedObjects) {
        // check for TypeDesignations
        if (relatedObject.isInstanceOf(SpecimenTypeDesignation.class)) {
            SpecimenTypeDesignation designation = HibernateProxyHelper.deproxy(relatedObject, SpecimenTypeDesignation.class);
            designation.setTypeSpecimen(null);
            List<TaxonName> typifiedNames = new ArrayList<>();
            typifiedNames.addAll(designation.getTypifiedNames());
            for (TaxonName taxonName : typifiedNames) {
                taxonName.removeTypeDesignation(designation);
            }
        }
        // delete IndividualsAssociation
        if (relatedObject.isInstanceOf(IndividualsAssociation.class)) {
            IndividualsAssociation association = HibernateProxyHelper.deproxy(relatedObject, IndividualsAssociation.class);
            association.setAssociatedSpecimenOrObservation(null);
            association.getInDescription().removeElement(association);
        }
        // check for "described specimen" (deprecated)
        if (relatedObject.isInstanceOf(TaxonDescription.class)) {
            TaxonDescription description = HibernateProxyHelper.deproxy(relatedObject, TaxonDescription.class);
            description.setDescribedSpecimenOrObservation(null);
        }
        // check for specimen description
        if (relatedObject.isInstanceOf(SpecimenDescription.class)) {
            SpecimenDescription specimenDescription = HibernateProxyHelper.deproxy(relatedObject, SpecimenDescription.class);
            specimenDescription.setDescribedSpecimenOrObservation(null);
            // check if description is a description of the given specimen
            if (specimen.getDescriptions().contains(specimenDescription)) {
                specimen.removeDescription(specimenDescription);
            }
            DeleteResult descriptionDelete = descriptionService.isDeletable(specimenDescription.getUuid(), null);
            if (descriptionDelete.isOk()) {
                deleteResult.includeResult(descriptionService.delete(specimenDescription));
            }
        }
        // check for amplification
        if (relatedObject.isInstanceOf(AmplificationResult.class)) {
            AmplificationResult amplificationResult = HibernateProxyHelper.deproxy(relatedObject, AmplificationResult.class);
            amplificationResult.getDnaSample().removeAmplificationResult(amplificationResult);
        }
        // check for sequence
        if (relatedObject.isInstanceOf(Sequence.class)) {
            Sequence sequence = HibernateProxyHelper.deproxy(relatedObject, Sequence.class);
            sequence.getDnaSample().removeSequence(sequence);
        }
        // check for children and parents (derivation events)
        if (relatedObject.isInstanceOf(DerivationEvent.class)) {
            DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(relatedObject, DerivationEvent.class);
            // parent derivation event (derivedFrom)
            if (derivationEvent.getDerivatives().contains(specimen) && specimen.isInstanceOf(DerivedUnit.class)) {
                derivationEvent.removeDerivative(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
                if (derivationEvent.getDerivatives().isEmpty()) {
                    Set<SpecimenOrObservationBase> originals = new HashSet<>();
                    originals.addAll(derivationEvent.getOriginals());
                    for (SpecimenOrObservationBase specimenOrObservationBase : originals) {
                        specimenOrObservationBase.removeDerivationEvent(derivationEvent);
                        deleteResult.addUpdatedObject(specimenOrObservationBase);
                    }
                    // if derivationEvent has no derivates anymore, delete it
                    deleteResult.includeResult(eventService.delete(derivationEvent));
                }
            } else {
            // child derivation events should not occur since we delete the hierarchy from bottom to top
            }
        }
    }
    if (specimen instanceof FieldUnit) {
        FieldUnit fieldUnit = HibernateProxyHelper.deproxy(specimen, FieldUnit.class);
        GatheringEvent event = fieldUnit.getGatheringEvent();
        fieldUnit.setGatheringEvent(null);
        if (event != null) {
            DeleteResult result = eventService.isDeletable(event.getUuid(), null);
            if (result.isOk()) {
                deleteResult.includeResult(eventService.delete(event));
            }
        }
    }
    deleteResult.includeResult(delete(specimen));
    return deleteResult;
}
Also used : GatheringEvent(eu.etaxonomy.cdm.model.occurrence.GatheringEvent) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) DerivationEvent(eu.etaxonomy.cdm.model.occurrence.DerivationEvent) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) ArrayList(java.util.ArrayList) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) Sequence(eu.etaxonomy.cdm.model.molecular.Sequence) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) IndividualsAssociation(eu.etaxonomy.cdm.model.description.IndividualsAssociation) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) AmplificationResult(eu.etaxonomy.cdm.model.molecular.AmplificationResult) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) SpecimenTypeDesignation(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 29 with SpecimenDescription

use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.

the class PolytomousKeyGenerator method makeKeyTaxa.

private Set<KeyTaxon> makeKeyTaxa(Set<DescriptionBase<?>> descriptions) {
    Map<UUID, KeyTaxon> taxonMap = new HashMap<>();
    for (DescriptionBase<?> db : descriptions) {
        KeyTaxon taxon = new KeyTaxon();
        if (db.isInstanceOf(TaxonDescription.class)) {
            TaxonDescription td = CdmBase.deproxy(db, TaxonDescription.class);
            taxon.uuid = td.getTaxon().getUuid();
            taxon.taxon = td.getTaxon();
        } else if (db.isInstanceOf(SpecimenDescription.class)) {
            SpecimenDescription sd = CdmBase.deproxy(db, SpecimenDescription.class);
            taxon.uuid = sd.getDescribedSpecimenOrObservation().getUuid();
            taxon.specimen = sd.getDescribedSpecimenOrObservation();
        } else {
            throw new RuntimeException("Unhandled entity type " + db.getClass().getName());
        }
        if (taxonMap.get(taxon.uuid) != null) {
            taxon = taxonMap.get(taxon.uuid);
        } else {
            taxonMap.put(taxon.uuid, taxon);
        }
        taxon.addDescription(db);
    }
    createTaxonHierarchy(taxonMap);
    return new HashSet<>(taxonMap.values());
}
Also used : HashMap(java.util.HashMap) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) UUID(java.util.UUID) HashSet(java.util.HashSet)

Example 30 with SpecimenDescription

use of eu.etaxonomy.cdm.model.description.SpecimenDescription in project cdmlib by cybertaxonomy.

the class OccurrenceCacheStrategyBase method getTextData.

private TextData getTextData(SpecimenOrObservationBase<?> occurrence, Feature feature, boolean isImageGallery) {
    if (feature == null) {
        return null;
    }
    TextData textData = null;
    Set<SpecimenDescription> descriptions;
    if (isImageGallery) {
        descriptions = occurrence.getSpecimenDescriptionImageGallery();
    } else {
        descriptions = occurrence.getSpecimenDescriptions(false);
    }
    // no description exists yet for this specimen
    if (descriptions.size() == 0) {
        return null;
    }
    // description already exists
    Set<DescriptionElementBase> existingTextData = new HashSet<>();
    for (SpecimenDescription description : descriptions) {
        // collect all existing text data
        for (DescriptionElementBase element : description.getElements()) {
            if (element.isInstanceOf(TextData.class) && (feature.equals(element.getFeature()) || isImageGallery)) {
                existingTextData.add(element);
            }
        }
    }
    // use existing text data if exactly one exists
    if (existingTextData.size() > 1) {
        throw new IllegalStateException("Specimen facade does not support more than one description text data of type " + feature.getLabel());
    } else if (existingTextData.size() == 1) {
        return CdmBase.deproxy(existingTextData.iterator().next(), TextData.class);
    } else {
        return textData;
    }
}
Also used : SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) TextData(eu.etaxonomy.cdm.model.description.TextData) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase) HashSet(java.util.HashSet)

Aggregations

SpecimenDescription (eu.etaxonomy.cdm.model.description.SpecimenDescription)49 TextData (eu.etaxonomy.cdm.model.description.TextData)21 TaxonDescription (eu.etaxonomy.cdm.model.description.TaxonDescription)20 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)17 Feature (eu.etaxonomy.cdm.model.description.Feature)14 DescriptionElementBase (eu.etaxonomy.cdm.model.description.DescriptionElementBase)13 DescriptiveDataSet (eu.etaxonomy.cdm.model.description.DescriptiveDataSet)13 SpecimenOrObservationBase (eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase)13 Test (org.junit.Test)13 HashSet (java.util.HashSet)12 Media (eu.etaxonomy.cdm.model.media.Media)11 DerivedUnit (eu.etaxonomy.cdm.model.occurrence.DerivedUnit)11 CdmBase (eu.etaxonomy.cdm.model.common.CdmBase)10 UUID (java.util.UUID)10 DescriptionBase (eu.etaxonomy.cdm.model.description.DescriptionBase)9 TaxonNode (eu.etaxonomy.cdm.model.taxon.TaxonNode)9 ArrayList (java.util.ArrayList)9 CategoricalData (eu.etaxonomy.cdm.model.description.CategoricalData)8 QuantitativeData (eu.etaxonomy.cdm.model.description.QuantitativeData)8 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)8