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));
}
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));
}
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));
}
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;
}
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);
}
Aggregations