Search in sources :

Example 31 with SpecimenDescription

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

the class CdmImportBase method getSpecimenDescription.

/**
 * Like {@link #getSpecimenDescription(SpecimenOrObservationBase, boolean, boolean)}
 * Only matches a description if the given reference is a source of the description.<BR>
 * If a new description is created the given reference will be added as a source.
 *
 * @see #getTaxonDescription(Taxon, boolean, boolean)
 */
public SpecimenDescription getSpecimenDescription(SpecimenOrObservationBase specimen, Reference ref, boolean isImageGallery, boolean createNewIfNotExists) {
    SpecimenDescription result = null;
    @SuppressWarnings("unchecked") Set<SpecimenDescription> descriptions = specimen.getDescriptions();
    for (SpecimenDescription description : descriptions) {
        if (description.isImageGallery() == isImageGallery) {
            if (hasCorrespondingSource(ref, description)) {
                result = description;
                break;
            }
        }
    }
    if (result == null && createNewIfNotExists) {
        result = SpecimenDescription.NewInstance(specimen);
        result.setImageGallery(isImageGallery);
        if (ref != null) {
            result.addImportSource(null, null, ref, null);
        }
    }
    return result;
}
Also used : SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription)

Example 32 with SpecimenDescription

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

the class MediaServiceImpl method delete.

@Override
@Transactional(readOnly = false)
public DeleteResult delete(UUID mediaUuid, MediaDeletionConfigurator config) {
    DeleteResult result = new DeleteResult();
    Media media = this.load(mediaUuid);
    if (media == null) {
        return result;
    }
    result = isDeletable(mediaUuid, config);
    if (result.isOk()) {
        Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
        for (CdmBase ref : references) {
            IDescribable<?> updatedObject = null;
            IService<ICdmBase> service = null;
            if (ref instanceof TextData) {
                TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
                DescriptionBase<?> description = HibernateProxyHelper.deproxy(textData.getInDescription(), DescriptionBase.class);
                IDescribable<?> objectToUpdate = null;
                boolean deleteIsMatchingInstance = false;
                if (description instanceof TaxonDescription) {
                    objectToUpdate = ((TaxonDescription) description).getTaxon();
                    deleteIsMatchingInstance = config.getDeleteFrom() instanceof Taxon;
                    service = (IService) taxonService;
                } else if (description instanceof SpecimenDescription) {
                    objectToUpdate = ((SpecimenDescription) description).getDescribedSpecimenOrObservation();
                    deleteIsMatchingInstance = config.getDeleteFrom() instanceof SpecimenOrObservationBase;
                    service = (IService) specimenService;
                } else if (description instanceof TaxonNameDescription) {
                    objectToUpdate = ((TaxonNameDescription) description).getTaxonName();
                    deleteIsMatchingInstance = config.getDeleteFrom() instanceof TaxonName;
                    service = (IService) nameService;
                } else {
                    throw new RuntimeException("Unsupported DescriptionBase class");
                }
                if (objectToUpdate == null) {
                    continue;
                } else if ((config.isDeleteFromDescription() && deleteIsMatchingInstance && config.getDeleteFrom().getId() == objectToUpdate.getId()) || config.isDeleteFromEveryWhere()) {
                    updatedObject = handleDeleteMedia(media, textData, description, (IDescribable) objectToUpdate);
                } else {
                    // this should not be happen, because it is not deletable. see isDeletable
                    result.setAbort();
                }
            // } else if (ref instanceof MediaSpecimen && config.getDeleteFrom().getId() == ref.getId() && config.getDeleteFrom() instanceof MediaSpecimen){
            // MediaSpecimen mediaSpecimen = HibernateProxyHelper.deproxy(ref, MediaSpecimen.class);
            // mediaSpecimen.setMediaSpecimen(null);
            // updatedObject = mediaSpecimen;
            // service = (IService)specimenService;
            } else if (ref instanceof MediaRepresentation) {
                continue;
            } else {
                result.setAbort();
            }
            if (updatedObject != null) {
                // service should always be != null if updatedObject != null
                service.update(updatedObject);
                result.addUpdatedObject((CdmBase) updatedObject);
            }
        }
        if (result.isOk()) {
            dao.delete(media);
            result.addDeletedObject(media);
        }
    }
    return result;
}
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) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) ICdmBase(eu.etaxonomy.cdm.model.common.ICdmBase) TextData(eu.etaxonomy.cdm.model.description.TextData) MediaRepresentation(eu.etaxonomy.cdm.model.media.MediaRepresentation) ICdmBase(eu.etaxonomy.cdm.model.common.ICdmBase) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) Transactional(org.springframework.transaction.annotation.Transactional)

Example 33 with SpecimenDescription

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

the class MediaServiceImpl method isDeletable.

@Override
public DeleteResult isDeletable(UUID mediaUuid, DeleteConfiguratorBase config) {
    DeleteResult result = new DeleteResult();
    Media media = this.load(mediaUuid);
    Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
    MediaDeletionConfigurator mediaConfig = (MediaDeletionConfigurator) config;
    CdmBase deleteFrom = mediaConfig.getDeleteFrom();
    if (mediaConfig.isDeleteFromEveryWhere()) {
        return result;
    }
    for (CdmBase ref : references) {
        String message = null;
        if (ref instanceof MediaRepresentation) {
            continue;
        } else if (ref instanceof TextData) {
            TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
            DescriptionBase<?> description = HibernateProxyHelper.deproxy(textData.getInDescription(), DescriptionBase.class);
            if (description instanceof TaxonDescription) {
                TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
                if (desc.getTaxon() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof Taxon && ((Taxon) deleteFrom).getId() == desc.getTaxon().getId()))) {
                    continue;
                } else {
                    message = "The media can't be deleted from the database because it is referenced by a taxon. (" + desc.getTaxon().getTitleCache() + ")";
                    result.setAbort();
                }
            } else if (description instanceof SpecimenDescription) {
                SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
                if (desc.getDescribedSpecimenOrObservation() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof SpecimenOrObservationBase && ((SpecimenOrObservationBase) deleteFrom).getId() == desc.getDescribedSpecimenOrObservation().getId()))) {
                    continue;
                } else {
                    message = "The media can't be deleted from the database because it is referenced by a specimen or observation. (" + desc.getDescribedSpecimenOrObservation().getTitleCache() + ")";
                    result.setAbort();
                }
            } else if (description instanceof TaxonNameDescription) {
                TaxonNameDescription desc = HibernateProxyHelper.deproxy(description, TaxonNameDescription.class);
                if (desc.getTaxonName() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof TaxonName && ((TaxonName) deleteFrom).getId() == desc.getTaxonName().getId()))) {
                    continue;
                } else {
                    message = "The media can't be deleted from the database because it is referenced by a scientific name. (" + desc.getTaxonName().getTitleCache() + ")";
                    result.setAbort();
                }
            }
        } else if (ref instanceof MediaSpecimen) {
            message = "The media can't be deleted from the database because it is referenced by a mediaspecimen. (" + ((MediaSpecimen) ref).getTitleCache() + ")";
            result.setAbort();
        } else {
            message = "The media can't be completely deleted because it is referenced by another " + ref.getUserFriendlyTypeName();
            result.setAbort();
        }
        if (message != null) {
            result.addException(new ReferencedObjectUndeletableException(message));
            result.addRelatedObject(ref);
        }
    }
    return result;
}
Also used : DescriptionBase(eu.etaxonomy.cdm.model.description.DescriptionBase) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) TaxonNameDescription(eu.etaxonomy.cdm.model.description.TaxonNameDescription) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) ReferencedObjectUndeletableException(eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException) Media(eu.etaxonomy.cdm.model.media.Media) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) MediaRepresentation(eu.etaxonomy.cdm.model.media.MediaRepresentation) TextData(eu.etaxonomy.cdm.model.description.TextData) MediaSpecimen(eu.etaxonomy.cdm.model.occurrence.MediaSpecimen) ICdmBase(eu.etaxonomy.cdm.model.common.ICdmBase) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) MediaDeletionConfigurator(eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator)

Example 34 with SpecimenDescription

use of eu.etaxonomy.cdm.model.description.SpecimenDescription 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;
}
Also used : DescriptiveDataSet(eu.etaxonomy.cdm.model.description.DescriptiveDataSet) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) CdmLinkSource(eu.etaxonomy.cdm.model.reference.CdmLinkSource) ICdmTarget(eu.etaxonomy.cdm.model.reference.ICdmTarget) Transactional(org.springframework.transaction.annotation.Transactional)

Example 35 with SpecimenDescription

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

the class DerivedUnitFacadeTest method testGetFieldObjectImageGalleryBooleanPersisted.

@Test
@DataSet
// TODO generally works has id problems with following tests when running in suite
@Ignore
public void testGetFieldObjectImageGalleryBooleanPersisted() {
    UUID specimenUUID = UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
    DerivedUnit specimen = (DerivedUnit) service.load(specimenUUID);
    assertNotNull("Specimen should exist (persisted)", specimen);
    try {
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
        SpecimenDescription imageGallery = facade.getFieldObjectImageGallery(true);
        assertNotNull("Image gallery should exist", imageGallery);
        assertEquals("UUID should be equal to the persisted uuid", UUID.fromString("8cb772e9-1577-45c6-91ab-dbec1413c060"), imageGallery.getUuid());
        assertEquals("The image gallery should be flagged as such", true, imageGallery.isImageGallery());
        assertEquals("There should be one TextData in image gallery", 1, imageGallery.getElements().size());
        List<Media> media = imageGallery.getElements().iterator().next().getMedia();
        assertEquals("There should be 1 media", 1, media.size());
    } catch (DerivedUnitFacadeNotSupportedException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) Media(eu.etaxonomy.cdm.model.media.Media) UUID(java.util.UUID) Ignore(org.junit.Ignore) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet) DataSet(org.unitils.dbunit.annotation.DataSet)

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