Search in sources :

Example 1 with SpecimenDescription

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

the class SpecimenTest method testBidirectionalSpecimenDescription.

@Test
public void testBidirectionalSpecimenDescription() {
    Assert.assertNotNull("Specimen should exist", specimen);
    SpecimenDescription desc = SpecimenDescription.NewInstance(specimen);
    Assert.assertNotNull("Description should exist.", desc);
    Assert.assertSame("Descriptions specimen should be set correctly", desc.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertTrue("Specimen should contain description", specimen.getDescriptions().contains(desc));
    SpecimenDescription desc2 = SpecimenDescription.NewInstance();
    Assert.assertNotNull("Description should exist.", desc2);
    specimen.addDescription(desc2);
    Assert.assertSame("Description2 specimen should be set correctly", desc2.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertTrue("Specimen should contain description2", specimen.getDescriptions().contains(desc2));
    Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
    SpecimenDescription desc3 = SpecimenDescription.NewInstance();
    Assert.assertNotNull("Description should exist.", desc3);
    desc3.setDescribedSpecimenOrObservation(specimen);
    Assert.assertSame("Description3 specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertTrue("Specimen should contain description3", specimen.getDescriptions().contains(desc3));
    Assert.assertTrue("Specimen should still contain description2", specimen.getDescriptions().contains(desc2));
    Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
    // change specimen of a given description
    DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
    Assert.assertNotNull("Specimen should exist.", specimen2);
    desc3.setDescribedSpecimenOrObservation(specimen2);
    Assert.assertSame("Description3 new specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(), specimen2);
    Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertTrue("Specimen2 should contain description3", specimen2.getDescriptions().contains(desc3));
    Assert.assertEquals("Specimen2 should contain exactly 1 description", 1, specimen2.getDescriptions().size());
    Assert.assertFalse("Specimen should no longer contain description3", specimen.getDescriptions().contains(desc3));
    Assert.assertTrue("Specimen should still contain description2", specimen.getDescriptions().contains(desc2));
    Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
    // remove description which is not contained
    specimen.removeDescription(desc3);
    Assert.assertSame("Nothing should have changed", desc3.getDescribedSpecimenOrObservation(), specimen2);
    Assert.assertSame("Nothing should have changed", desc2.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertSame("Nothing should have changed", desc.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertTrue("Nothing should have changed", specimen2.getDescriptions().contains(desc3));
    Assert.assertEquals("Nothing should have changed", 1, specimen2.getDescriptions().size());
    Assert.assertFalse("Nothing should have changed", specimen.getDescriptions().contains(desc3));
    Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc2));
    Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc));
    // remove description
    specimen.removeDescription(desc2);
    Assert.assertNull("Descriptions2 specimen should not exist anymore", desc2.getDescribedSpecimenOrObservation());
    Assert.assertSame("Description3 specimen should still be set correctly", desc3.getDescribedSpecimenOrObservation(), specimen2);
    Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertTrue("Specimen2 should still contain description3", specimen2.getDescriptions().contains(desc3));
    Assert.assertEquals("Specimen2 should still contain exactly 1 description", 1, specimen2.getDescriptions().size());
    Assert.assertFalse("Specimen should not contain description2 anymore", specimen.getDescriptions().contains(desc2));
    Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
    Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
    // remove description by setting null specimen
    desc3.setDescribedSpecimenOrObservation(null);
    Assert.assertNull("Description3 specimen should not exist anymore", desc3.getDescribedSpecimenOrObservation());
    Assert.assertNull("Descriptions2 specimen should still not exist anymore", desc2.getDescribedSpecimenOrObservation());
    Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(), specimen);
    Assert.assertFalse("Specimen2 should not contain description3 anymore", specimen2.getDescriptions().contains(desc3));
    Assert.assertEquals("Specimen2 should contain no description now", 0, specimen2.getDescriptions().size());
    Assert.assertFalse("Specimen should still no longer contain description2", specimen.getDescriptions().contains(desc2));
    Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
    Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
}
Also used : SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) Test(org.junit.Test)

Example 2 with SpecimenDescription

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

the class OccurrenceDaoHibernateImplTest method testGetMedia.

@Test
public void testGetMedia() {
    MediaSpecimen unit = MediaSpecimen.NewInstance(SpecimenOrObservationType.Media);
    SpecimenDescription desc = SpecimenDescription.NewInstance(unit);
    desc.setImageGallery(true);
    TextData textData = TextData.NewInstance(Feature.IMAGE());
    desc.addElement(textData);
    Media media1 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
    Media media2 = Media.NewInstance(URI.create("https://www.defg.de"), 5, "jpg", "jpg");
    textData.addMedia(media1);
    textData.addMedia(media2);
    Media media3 = Media.NewInstance(URI.create("https://www.hij.de"), 5, "jpg", "jpg");
    unit.setMediaSpecimen(media3);
    dao.save(unit);
    List<Media> media = dao.getMedia(unit, null, null, null);
    Assert.assertEquals(3, media.size());
    // test that paging works (note: the sorting is not a requirement for the method, but with current implementation it works; if implementation is changed the test may need to be adapted
    List<Integer> ids = media.stream().map(m -> m.getId()).collect(Collectors.toList());
    Collections.sort(ids);
    Assert.assertEquals(ids.get(0), (Integer) media.get(0).getId());
    Assert.assertEquals(ids.get(1), (Integer) media.get(1).getId());
    Assert.assertEquals(ids.get(2), (Integer) media.get(2).getId());
    media = dao.getMedia(unit, 2, 1, null);
    Assert.assertEquals(1, media.size());
    Assert.assertEquals(ids.get(2), (Integer) media.get(0).getId());
    // test deduplication
    unit.setMediaSpecimen(media2);
    Assert.assertEquals(2, dao.countMedia(unit));
}
Also used : SpringBeanByType(org.unitils.spring.annotation.SpringBeanByType) MediaSpecimen(eu.etaxonomy.cdm.model.occurrence.MediaSpecimen) BeforeClass(org.junit.BeforeClass) CdmIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmIntegrationTest) TextData(eu.etaxonomy.cdm.model.description.TextData) Test(org.junit.Test) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Media(eu.etaxonomy.cdm.model.media.Media) StringUtils(org.h2.util.StringUtils) List(java.util.List) SpecimenOrObservationType(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType) IOccurrenceDao(eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao) Feature(eu.etaxonomy.cdm.model.description.Feature) Assert(org.junit.Assert) Collections(java.util.Collections) Before(org.junit.Before) URI(eu.etaxonomy.cdm.common.URI) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) MediaSpecimen(eu.etaxonomy.cdm.model.occurrence.MediaSpecimen) TextData(eu.etaxonomy.cdm.model.description.TextData) Media(eu.etaxonomy.cdm.model.media.Media) CdmIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmIntegrationTest) Test(org.junit.Test)

Example 3 with SpecimenDescription

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

the class OccurrenceDaoHibernateImplTest method testCountMedia.

@Test
public void testCountMedia() {
    MediaSpecimen unit = MediaSpecimen.NewInstance(SpecimenOrObservationType.Media);
    SpecimenDescription desc = SpecimenDescription.NewInstance(unit);
    desc.setImageGallery(true);
    TextData textData = TextData.NewInstance(Feature.IMAGE());
    desc.addElement(textData);
    Media media1 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
    Media media2 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
    textData.addMedia(media1);
    textData.addMedia(media2);
    Media media3 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
    unit.setMediaSpecimen(media3);
    dao.save(unit);
    Assert.assertEquals(3, dao.countMedia(unit));
    unit.setMediaSpecimen(media2);
    Assert.assertEquals(2, dao.countMedia(unit));
}
Also used : SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) MediaSpecimen(eu.etaxonomy.cdm.model.occurrence.MediaSpecimen) TextData(eu.etaxonomy.cdm.model.description.TextData) Media(eu.etaxonomy.cdm.model.media.Media) CdmIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmIntegrationTest) Test(org.junit.Test)

Example 4 with SpecimenDescription

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

the class DescriptiveDataSetService method addRowWrapperToDataset.

@Override
@Transactional(readOnly = false)
public UpdateResult addRowWrapperToDataset(Collection<SpecimenRowWrapperDTO> wrappers, UUID datasetUuid, boolean addDatasetSource) {
    UpdateResult result = new UpdateResult();
    DescriptiveDataSet dataSet = load(datasetUuid);
    result.setCdmEntity(dataSet);
    List<UUID> taxonUuids = wrappers.stream().map(wrapper -> wrapper.getTaxonNode().getTaxonUuid()).collect(Collectors.toList());
    List<TaxonBase> taxa = taxonService.load(taxonUuids, Arrays.asList(new String[] { "descriptions" }));
    for (SpecimenRowWrapperDTO wrapper : wrappers) {
        Optional<TaxonBase> findAny = taxa.stream().filter(taxon -> taxon.getUuid().equals(wrapper.getTaxonNode().getTaxonUuid())).findAny();
        if (!findAny.isPresent()) {
            result.addException(new IllegalArgumentException("Could not create wrapper for " + wrapper.getSpecimenDto().getLabel()));
            continue;
        }
        Taxon taxon = (Taxon) findAny.get();
        SpecimenOrObservationBase<?> specimen = occurrenceService.load(wrapper.getSpecimenDto().getUuid());
        TaxonDescription taxonDescription = taxon.getDescriptions().stream().filter(desc -> desc.getTypes().contains(DescriptionType.INDIVIDUALS_ASSOCIATION)).findFirst().orElseGet(() -> {
            TaxonDescription td = TaxonDescription.NewInstance(taxon);
            td.addType(DescriptionType.INDIVIDUALS_ASSOCIATION);
            td.setTitleCache("Specimens used by " + dataSet.getTitleCache() + " for " + getTaxonLabel(taxon), true);
            return td;
        });
        IndividualsAssociation association = null;
        for (DescriptionElementBase el : taxonDescription.getElements()) {
            if (el instanceof IndividualsAssociation) {
                IndividualsAssociation indAss = (IndividualsAssociation) el;
                if (indAss.getAssociatedSpecimenOrObservation().getUuid().equals(specimen.getUuid())) {
                    association = indAss;
                }
            }
        }
        if (association == null) {
            association = IndividualsAssociation.NewInstance(specimen);
            taxonDescription.addElement(association);
            taxonService.saveOrUpdate(taxon);
            result.addUpdatedObject(taxon);
        }
        UUID specimenDescriptionUuid = wrapper.getDescription().getDescriptionUuid();
        DescriptionBaseDto descriptionDto = wrapper.getDescription();
        DescriptionBase<?> specimenDescription = descriptionService.load(specimenDescriptionUuid);
        // if description already exist use the loaded one and add changed data otherwise create a new one and add to specimen
        if (specimenDescription == null) {
            specimenDescription = SpecimenDescription.NewInstance(specimen);
            specimenDescription.setUuid(specimenDescriptionUuid);
            List<DescriptionElementDto> elementDtos = descriptionDto.getElements();
            for (DescriptionElementDto elementDto : elementDtos) {
                if (elementDto instanceof CategoricalDataDto) {
                    eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
                    CategoricalData data = CategoricalData.NewInstance(feature);
                    for (StateDataDto stateDto : ((CategoricalDataDto) elementDto).getStates()) {
                        State state = DefinedTermBase.getTermByClassAndUUID(State.class, stateDto.getState().getUuid());
                        data.addStateData(state);
                        specimenDescription.addElement(data);
                    }
                }
                if (elementDto instanceof QuantitativeDataDto) {
                    eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
                    QuantitativeData data = QuantitativeData.NewInstance(feature);
                    if (((QuantitativeDataDto) elementDto).getMeasurementUnit() != null) {
                        MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) elementDto).getMeasurementUnit().getUuid());
                        data.setUnit(unit);
                    }
                    for (StatisticalMeasurementValueDto stateDto : ((QuantitativeDataDto) elementDto).getValues()) {
                        StatisticalMeasure statMeasure = DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, stateDto.getType().getUuid());
                        StatisticalMeasurementValue value = StatisticalMeasurementValue.NewInstance(statMeasure, stateDto.getValue());
                        data.addStatisticalValue(value);
                        specimenDescription.addElement(data);
                    }
                }
            }
        } else {
            List<DescriptionElementDto> elementDtos = descriptionDto.getElements();
            for (DescriptionElementDto elementDto : elementDtos) {
                if (elementDto instanceof CategoricalDataDto) {
                    eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
                    List<DescriptionElementBase> uniqueElementList = specimenDescription.getElements().stream().filter(element -> element.getUuid().equals(elementDto.getElementUuid())).collect(Collectors.toList());
                    List<State> allStates = new ArrayList<>();
                    CategoricalData element = null;
                    if (uniqueElementList.size() == 1) {
                        element = HibernateProxyHelper.deproxy(uniqueElementList.get(0), CategoricalData.class);
                    } else {
                        element = CategoricalData.NewInstance(feature);
                    }
                    for (StateDataDto stateDto : ((CategoricalDataDto) elementDto).getStates()) {
                        State state = DefinedTermBase.getTermByClassAndUUID(State.class, stateDto.getState().getUuid());
                        allStates.add(state);
                    }
                    element.setStateDataOnly(allStates);
                }
                if (elementDto instanceof QuantitativeDataDto) {
                    eu.etaxonomy.cdm.model.description.Character feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, elementDto.getFeatureUuid());
                    QuantitativeData data = QuantitativeData.NewInstance(feature);
                    if (((QuantitativeDataDto) elementDto).getMeasurementUnit() != null) {
                        MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) elementDto).getMeasurementUnit().getUuid());
                        data.setUnit(unit);
                    }
                    for (StatisticalMeasurementValueDto stateDto : ((QuantitativeDataDto) elementDto).getValues()) {
                        StatisticalMeasure statMeasure = DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, stateDto.getType().getUuid());
                        StatisticalMeasurementValue value = StatisticalMeasurementValue.NewInstance(statMeasure, stateDto.getValue());
                        data.addStatisticalValue(value);
                        specimenDescription.addElement(data);
                    }
                }
            }
        }
        if (addDatasetSource) {
            for (IdentifiableSource source : dataSet.getSources()) {
                try {
                    specimenDescription.addSource(source.clone());
                } catch (CloneNotSupportedException e) {
                // nothing
                }
            }
        }
        // add specimen description to data set
        specimenDescription.addDescriptiveDataSet(dataSet);
        // add taxon description with IndividualsAssociation to the specimen to data set
        taxonDescription.addDescriptiveDataSet(dataSet);
        result.addUpdatedObject(specimen);
        result.addUpdatedObject(specimenDescription);
        result.addUpdatedObject(taxonDescription);
    }
    saveOrUpdate(dataSet);
    return result;
}
Also used : Arrays(java.util.Arrays) TermDto(eu.etaxonomy.cdm.persistence.dto.TermDto) IProgressMonitor(eu.etaxonomy.cdm.common.monitor.IProgressMonitor) StatisticalMeasurementValueDto(eu.etaxonomy.cdm.api.service.dto.StatisticalMeasurementValueDto) Autowired(org.springframework.beans.factory.annotation.Autowired) IdentifiableServiceConfiguratorImpl(eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorImpl) CategoricalData(eu.etaxonomy.cdm.model.description.CategoricalData) DescriptiveSystemRole(eu.etaxonomy.cdm.model.description.DescriptiveSystemRole) TaxonNodeDto(eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto) BigDecimal(java.math.BigDecimal) Logger(org.apache.log4j.Logger) DescriptiveDataSet(eu.etaxonomy.cdm.model.description.DescriptiveDataSet) Classification(eu.etaxonomy.cdm.model.taxon.Classification) QuantitativeDataDto(eu.etaxonomy.cdm.api.service.dto.QuantitativeDataDto) TaxonBase(eu.etaxonomy.cdm.model.taxon.TaxonBase) DescriptionBaseDto(eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto) Map(java.util.Map) Query(org.hibernate.Query) TaxonNodeFilter(eu.etaxonomy.cdm.filter.TaxonNodeFilter) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase) Collection(java.util.Collection) Set(java.util.Set) StateDataDto(eu.etaxonomy.cdm.api.service.dto.StateDataDto) CdmLinkSource(eu.etaxonomy.cdm.model.reference.CdmLinkSource) UUID(java.util.UUID) DescriptiveDataSetBaseDto(eu.etaxonomy.cdm.persistence.dto.DescriptiveDataSetBaseDto) QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) Collectors(java.util.stream.Collectors) PolytomousKeyGeneratorConfigurator(eu.etaxonomy.cdm.strategy.generate.PolytomousKeyGeneratorConfigurator) SpecimenOrObservationDTOFactory(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationDTOFactory) PolytomousKeyGenerator(eu.etaxonomy.cdm.strategy.generate.PolytomousKeyGenerator) List(java.util.List) DeleteDescriptiveDataSetConfigurator(eu.etaxonomy.cdm.api.service.config.DeleteDescriptiveDataSetConfigurator) DescriptionType(eu.etaxonomy.cdm.model.description.DescriptionType) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) Optional(java.util.Optional) DescriptionBase(eu.etaxonomy.cdm.model.description.DescriptionBase) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) IDefinedTermDao(eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao) MergeResult(eu.etaxonomy.cdm.persistence.dto.MergeResult) IIdentifiableEntityCacheStrategy(eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy) TextData(eu.etaxonomy.cdm.model.description.TextData) Session(org.hibernate.Session) StatisticalMeasure(eu.etaxonomy.cdm.model.description.StatisticalMeasure) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) HashMap(java.util.HashMap) Status(eu.etaxonomy.cdm.api.service.UpdateResult.Status) IndividualsAssociation(eu.etaxonomy.cdm.model.description.IndividualsAssociation) RowWrapperDTO(eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO) UuidAndTitleCache(eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache) ArrayList(java.util.ArrayList) IdentifiableSource(eu.etaxonomy.cdm.model.common.IdentifiableSource) HashSet(java.util.HashSet) NamedArea(eu.etaxonomy.cdm.model.location.NamedArea) SpecimenNodeWrapper(eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper) TermTreeDto(eu.etaxonomy.cdm.persistence.dto.TermTreeDto) PolytomousKey(eu.etaxonomy.cdm.model.description.PolytomousKey) Service(org.springframework.stereotype.Service) RemoveDescriptionsFromDescriptiveDataSetConfigurator(eu.etaxonomy.cdm.api.service.config.RemoveDescriptionsFromDescriptiveDataSetConfigurator) State(eu.etaxonomy.cdm.model.description.State) StatisticalMeasurementValue(eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue) IDescriptiveDataSetDao(eu.etaxonomy.cdm.persistence.dao.description.IDescriptiveDataSetDao) DefaultQuantitativeDescriptionBuilder(eu.etaxonomy.cdm.format.description.DefaultQuantitativeDescriptionBuilder) TaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonNode) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) TaxonRowWrapperDTO(eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO) DescriptionElementDto(eu.etaxonomy.cdm.api.service.dto.DescriptionElementDto) HibernateProxyHelper(eu.etaxonomy.cdm.hibernate.HibernateProxyHelper) MeasurementUnit(eu.etaxonomy.cdm.model.description.MeasurementUnit) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) CategoricalDataDto(eu.etaxonomy.cdm.api.service.dto.CategoricalDataDto) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) Language(eu.etaxonomy.cdm.model.common.Language) DefaultCategoricalDescriptionBuilder(eu.etaxonomy.cdm.format.description.DefaultCategoricalDescriptionBuilder) SpecimenRowWrapperDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO) Feature(eu.etaxonomy.cdm.model.description.Feature) DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) StateDataDto(eu.etaxonomy.cdm.api.service.dto.StateDataDto) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) ArrayList(java.util.ArrayList) QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) QuantitativeDataDto(eu.etaxonomy.cdm.api.service.dto.QuantitativeDataDto) StatisticalMeasure(eu.etaxonomy.cdm.model.description.StatisticalMeasure) CategoricalDataDto(eu.etaxonomy.cdm.api.service.dto.CategoricalDataDto) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase) TaxonBase(eu.etaxonomy.cdm.model.taxon.TaxonBase) IdentifiableSource(eu.etaxonomy.cdm.model.common.IdentifiableSource) UUID(java.util.UUID) StatisticalMeasurementValue(eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue) MeasurementUnit(eu.etaxonomy.cdm.model.description.MeasurementUnit) DescriptionBaseDto(eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto) SpecimenRowWrapperDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) DescriptionElementDto(eu.etaxonomy.cdm.api.service.dto.DescriptionElementDto) StatisticalMeasurementValueDto(eu.etaxonomy.cdm.api.service.dto.StatisticalMeasurementValueDto) DescriptiveDataSet(eu.etaxonomy.cdm.model.description.DescriptiveDataSet) IndividualsAssociation(eu.etaxonomy.cdm.model.description.IndividualsAssociation) State(eu.etaxonomy.cdm.model.description.State) CategoricalData(eu.etaxonomy.cdm.model.description.CategoricalData) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with SpecimenDescription

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

the class DescriptiveDataSetService method findSpecimenDescription.

@Override
@Transactional(readOnly = false)
public DescriptionBaseDto findSpecimenDescription(UUID descriptiveDataSetUuid, SpecimenOrObservationBase specimen) {
    DescriptiveDataSetBaseDto dataSet = this.getDescriptiveDataSetDtoByUuid(descriptiveDataSetUuid);
    // SpecimenOrObservationBase specimen = occurrenceService.load(specimenUuid);
    TermTreeDto datasetFeatures = dataSet.getDescriptiveSystem();
    List<DescriptionElementBase> matchingDescriptionElements = new ArrayList<>();
    for (SpecimenDescription specimenDescription : (Set<SpecimenDescription>) specimen.getDescriptions()) {
        specimenDescription = (SpecimenDescription) descriptionService.load(specimenDescription.getUuid());
        // check if description is already added to data set
        if (dataSet.getDescriptionUuids().contains(specimenDescription.getUuid())) {
            return DescriptionBaseDto.fromDescription(specimenDescription);
        }
        // gather specimen description features and check for match with dataset features
        Set<Feature> specimenDescriptionFeatures = new HashSet<>();
        for (DescriptionElementBase specimenDescriptionElement : specimenDescription.getElements()) {
            Feature feature = specimenDescriptionElement.getFeature();
            specimenDescriptionFeatures.add(feature);
            boolean contains = false;
            for (TermDto featureDto : datasetFeatures.getTerms()) {
                if (featureDto.getUuid().equals(feature.getUuid())) {
                    contains = true;
                    break;
                }
            }
            if (contains && RowWrapperDTO.hasData(specimenDescriptionElement)) {
                matchingDescriptionElements.add(specimenDescriptionElement);
            }
        }
    }
    // Create new specimen description if description has not already been added to the dataset
    SpecimenDescription newDesription = SpecimenDescription.NewInstance(specimen);
    // $NON-NLS-2$
    newDesription.setTitleCache("Dataset " + dataSet.getTitleCache() + ": " + newDesription.generateTitle(), true);
    // check for equals description element (same feature and same values)
    Map<Feature, List<DescriptionElementBase>> featureToElementMap = new HashMap<>();
    for (DescriptionElementBase element : matchingDescriptionElements) {
        List<DescriptionElementBase> list = featureToElementMap.get(element.getFeature());
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(element);
        featureToElementMap.put(element.getFeature(), list);
    }
    Set<DescriptionElementBase> descriptionElementsToClone = new HashSet<>();
    for (Feature feature : featureToElementMap.keySet()) {
        List<DescriptionElementBase> elements = featureToElementMap.get(feature);
        // no duplicate description elements found for this feature
        if (elements.size() == 1) {
            descriptionElementsToClone.add(elements.get(0));
        } else // duplicates found -> check if all are equal
        {
            DescriptionElementBase match = null;
            for (DescriptionElementBase descriptionElementBase : elements) {
                if (match == null) {
                    match = descriptionElementBase;
                } else if (!new DescriptionElementCompareWrapper(match).equals(new DescriptionElementCompareWrapper(descriptionElementBase))) {
                    match = null;
                    // String.format(Messages.CharacterMatrix_MULTIPLE_DATA_MESSAGE, feature.getLabel()));
                    break;
                }
            }
            if (match != null) {
                descriptionElementsToClone.add(match);
            }
        }
    }
    // clone matching descriptionElements
    for (DescriptionElementBase descriptionElementBase : descriptionElementsToClone) {
        DescriptionElementBase clone;
        clone = descriptionElementBase.clone(newDesription);
        clone.getSources().forEach(source -> {
            if (descriptionElementBase instanceof CategoricalData) {
                TextData label = new DefaultCategoricalDescriptionBuilder().build((CategoricalData) descriptionElementBase, Arrays.asList(new Language[] { Language.DEFAULT() }));
                source.setOriginalNameString(label.getText(Language.DEFAULT()));
            } else if (descriptionElementBase instanceof QuantitativeData) {
                TextData label = new DefaultQuantitativeDescriptionBuilder().build((QuantitativeData) descriptionElementBase, Arrays.asList(new Language[] { Language.DEFAULT() }));
                source.setOriginalNameString(label.getText(Language.DEFAULT()));
            }
        });
    }
    // }
    return DescriptionBaseDto.fromDescription(newDesription);
}
Also used : DescriptiveDataSet(eu.etaxonomy.cdm.model.description.DescriptiveDataSet) Set(java.util.Set) HashSet(java.util.HashSet) DefaultCategoricalDescriptionBuilder(eu.etaxonomy.cdm.format.description.DefaultCategoricalDescriptionBuilder) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) Feature(eu.etaxonomy.cdm.model.description.Feature) TermDto(eu.etaxonomy.cdm.persistence.dto.TermDto) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase) Language(eu.etaxonomy.cdm.model.common.Language) DefaultQuantitativeDescriptionBuilder(eu.etaxonomy.cdm.format.description.DefaultQuantitativeDescriptionBuilder) CategoricalData(eu.etaxonomy.cdm.model.description.CategoricalData) TextData(eu.etaxonomy.cdm.model.description.TextData) TermTreeDto(eu.etaxonomy.cdm.persistence.dto.TermTreeDto) List(java.util.List) ArrayList(java.util.ArrayList) DescriptiveDataSetBaseDto(eu.etaxonomy.cdm.persistence.dto.DescriptiveDataSetBaseDto) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

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