use of eu.etaxonomy.cdm.model.description.IndividualsAssociation in project cdmlib by cybertaxonomy.
the class MarkupSpecimenImport method handleMaterialsExamined.
public List<DescriptionElementBase> handleMaterialsExamined(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Feature feature, TaxonDescription defaultDescription) throws XMLStreamException {
List<DescriptionElementBase> result = new ArrayList<DescriptionElementBase>();
// reset current areas
state.removeCurrentAreas();
while (reader.hasNext()) {
XMLEvent next = readNoWhitespace(reader);
if (isMyEndingElement(next, parentEvent)) {
if (result.isEmpty()) {
fireWarningEvent("Materials examined created empty Individual Associations list", parentEvent, 4);
}
state.removeCurrentAreas();
return result;
} else if (isStartingElement(next, SUB_HEADING)) {
// Map<String, Object> inlineMarkup = new HashMap<String, Object>();
String text = getCData(state, reader, next, true);
if (isFeatureHeading(state, next, text)) {
feature = makeHeadingFeature(state, next, text, feature);
} else {
String message = "Unhandled subheading: %s";
fireWarningEvent(String.format(message, text), next, 4);
}
// for (String key : inlineMarkup.keySet()){
// handleInlineMarkup(state, key, inlineMarkup);
// }
} else if (isStartingElement(next, BR) || isEndingElement(next, BR)) {
// do nothing
} else if (isStartingElement(next, GATHERING)) {
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.DerivedUnit);
addCurrentAreas(state, next, facade);
handleGathering(state, reader, next, facade);
SpecimenOrObservationBase<?> specimen;
if (facade.innerDerivedUnit() != null) {
specimen = facade.innerDerivedUnit();
} else {
specimen = facade.innerFieldUnit();
}
IndividualsAssociation individualsAssociation = IndividualsAssociation.NewInstance();
individualsAssociation.addPrimaryTaxonomicSource(state.getConfig().getSourceReference());
individualsAssociation.setAssociatedSpecimenOrObservation(specimen);
result.add(individualsAssociation);
} else if (isStartingElement(next, GATHERING_GROUP)) {
List<DescriptionElementBase> list = getGatheringGroupDescription(state, reader, next);
result.addAll(list);
} else if (next.isCharacters()) {
String text = next.asCharacters().getData().trim();
if (isPunctuation(text)) {
// do nothing
} else {
String message = "Unrecognized text: %s";
fireWarningEvent(String.format(message, text), next, 6);
}
} else {
handleUnexpectedElement(next);
}
}
throw new IllegalStateException("<String> has no closing tag");
}
use of eu.etaxonomy.cdm.model.description.IndividualsAssociation 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.IndividualsAssociation in project cdmlib by cybertaxonomy.
the class DescriptiveDataSetService method removeDescriptionFromDataSet.
private void removeDescriptionFromDataSet(DeleteResult result, DescriptiveDataSet dataSet, DescriptionBase<?> description, RemoveDescriptionsFromDescriptiveDataSetConfigurator config) {
if (description == null) {
return;
}
boolean success = dataSet.removeDescription(description);
// remove taxon description with IndividualsAssociation from data set
result.addDeletedObject(description);
if (description instanceof SpecimenDescription) {
@SuppressWarnings({ "unchecked", "cast" }) Set<IndividualsAssociation> // NOTE: don't remove cast as it does not compile on some systems
associations = (Set<IndividualsAssociation>) dataSet.getDescriptions().stream().flatMap(// put all description element in one stream
desc -> desc.getElements().stream()).filter(element -> element instanceof IndividualsAssociation).map(ia -> (IndividualsAssociation) ia).collect(Collectors.toSet());
for (IndividualsAssociation individualsAssociation : associations) {
if (individualsAssociation.getAssociatedSpecimenOrObservation().equals(description.getDescribedSpecimenOrObservation())) {
dataSet.removeDescription(individualsAssociation.getInDescription());
result.addUpdatedObject(individualsAssociation.getInDescription());
}
}
}
if (description instanceof TaxonDescription) {
DeleteResult isDeletable = descriptionService.isDeletable(description.getUuid());
for (CdmBase relatedCdmBase : isDeletable.getRelatedObjects()) {
if (relatedCdmBase instanceof CdmLinkSource) {
CdmLinkSource linkSource = (CdmLinkSource) relatedCdmBase;
if (linkSource.getTarget().equals(this)) {
}
}
}
}
if (!config.isOnlyRemoveDescriptionsFromDataSet()) {
DeleteResult deleteResult = descriptionService.deleteDescription(description);
result.includeResult(deleteResult);
result.addUpdatedObject(dataSet);
} else {
MergeResult<DescriptiveDataSet> mergeResult = dao.merge(dataSet, true);
result.addUpdatedObject(mergeResult.getMergedEntity());
}
result.setStatus(success ? Status.OK : Status.ERROR);
}
use of eu.etaxonomy.cdm.model.description.IndividualsAssociation in project cdmlib by cybertaxonomy.
the class DescriptiveDataSetServiceTest method createSpecimenDescription.
private SpecimenDescription createSpecimenDescription(DescriptiveDataSet dataSet, UUID taxonUuid, String specLabel) {
Taxon taxon = (Taxon) taxonService.find(taxonUuid);
TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
specimen.setTitleCache(specLabel, true);
IndividualsAssociation individualsAssociation = IndividualsAssociation.NewInstance(specimen);
// TODO this has to be discussed; currently the description with the InidividualsAssociation is
// needed in the dataset for performance reasons
taxonDescription.addElement(individualsAssociation);
dataSet.addDescription(taxonDescription);
SpecimenDescription specDesc = SpecimenDescription.NewInstance(specimen);
dataSet.addDescription(specDesc);
return specDesc;
}
use of eu.etaxonomy.cdm.model.description.IndividualsAssociation in project cdmlib by cybertaxonomy.
the class StructuredDescriptionAggregationTest method createSpecimenDescription.
private SpecimenDescription createSpecimenDescription(DescriptiveDataSet dataSet, UUID taxonUuid, String specLabel, UUID specimenUuid) {
Taxon taxon = (Taxon) taxonService.find(taxonUuid);
DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
specimen.setTitleCache(specLabel, true);
specimen.setUuid(specimenUuid);
TaxonDescription taxonDescription = taxon.getDescriptions(DescriptionType.INDIVIDUALS_ASSOCIATION).stream().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 individualsAssociation = IndividualsAssociation.NewInstance(specimen);
// TODO this has to be discussed; currently the description with the InidividualsAssociation is
// needed in the dataset for performance reasons
taxonDescription.addElement(individualsAssociation);
dataSet.addDescription(taxonDescription);
SpecimenDescription specDesc = SpecimenDescription.NewInstance(specimen);
dataSet.addDescription(specDesc);
return specDesc;
}
Aggregations