use of eu.etaxonomy.cdm.model.description.DescriptiveDataSet 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.DescriptiveDataSet 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.DescriptiveDataSet in project cdmlib by cybertaxonomy.
the class DescriptiveDataSetService method removeDescriptions.
@Override
@Transactional(readOnly = false)
public DeleteResult removeDescriptions(List<UUID> descriptionUuids, UUID descriptiveDataSetUuid, RemoveDescriptionsFromDescriptiveDataSetConfigurator config) {
DeleteResult result = new DeleteResult();
DescriptiveDataSet dataSet = load(descriptiveDataSetUuid);
List<DescriptionBase> descriptions = descriptionService.load(descriptionUuids, null);
if (dataSet == null || descriptions == null) {
result.setError();
} else {
for (DescriptionBase<?> description : descriptions) {
removeDescriptionFromDataSet(result, dataSet, description, config);
}
}
return result;
}
use of eu.etaxonomy.cdm.model.description.DescriptiveDataSet in project cdmlib by cybertaxonomy.
the class DescriptiveDataSetService method delete.
@Override
@Transactional(readOnly = false)
public DeleteResult delete(UUID datasetUuid, DeleteDescriptiveDataSetConfigurator config, IProgressMonitor monitor) {
DescriptiveDataSet dataSet = dao.load(datasetUuid);
monitor.beginTask("Delete Descriptive Dataset", dataSet.getDescriptions().size() + 1);
DeleteResult result = new DeleteResult();
DeleteResult descriptionResult = new DeleteResult();
if (!dataSet.getDescriptions().isEmpty()) {
Set<DescriptionBase> descriptions = new HashSet<>();
;
for (DescriptionBase<?> desc : dataSet.getDescriptions()) {
descriptions.add(desc);
}
monitor.subTask("Delete descriptions");
for (DescriptionBase<?> desc : descriptions) {
dataSet.removeDescription(desc);
if (desc instanceof SpecimenDescription && config.isDeleteAllSpecimenDescriptions()) {
descriptionResult.includeResult(descriptionService.deleteDescription(desc));
} else if (desc instanceof TaxonDescription) {
if (desc.getTypes().contains(DescriptionType.DEFAULT_VALUES_FOR_AGGREGATION) && config.isDeleteAllDefaultDescriptions()) {
descriptionResult.includeResult(descriptionService.deleteDescription(desc));
} else if (desc.getTypes().contains(DescriptionType.SECONDARY_DATA) && config.isDeleteAllLiteratureDescriptions()) {
descriptionResult.includeResult(descriptionService.deleteDescription(desc));
} else if (desc.getTypes().contains(DescriptionType.AGGREGATED_STRUC_DESC) && config.isDeleteAllAggregatedDescriptions()) {
descriptionResult.includeResult(descriptionService.deleteDescription(desc));
}
}
}
}
dao.delete(dataSet);
monitor.worked(1);
monitor.done();
result.includeResult(descriptionResult);
result.setStatus(Status.OK);
result.addDeletedObject(dataSet);
return result;
}
use of eu.etaxonomy.cdm.model.description.DescriptiveDataSet in project cdmlib by cybertaxonomy.
the class DescriptiveDataSetService method generatePolytomousKey.
@Override
@Transactional(readOnly = false)
public UpdateResult generatePolytomousKey(UUID descriptiveDataSetUuid, UUID taxonUuid) {
UpdateResult result = new UpdateResult();
PolytomousKeyGeneratorConfigurator keyConfig = new PolytomousKeyGeneratorConfigurator();
DescriptiveDataSet descriptiveDataSet = load(descriptiveDataSetUuid);
keyConfig.setDataSet(descriptiveDataSet);
PolytomousKey key = new PolytomousKeyGenerator().invoke(keyConfig);
IdentifiableServiceConfiguratorImpl<PolytomousKey> serviceConfig = new IdentifiableServiceConfiguratorImpl<>();
serviceConfig.setTitleSearchString(descriptiveDataSet.getTitleCache());
List<PolytomousKey> list = polytomousKeyService.findByTitle(serviceConfig).getRecords();
if (list != null) {
list.forEach(polytomousKey -> polytomousKeyService.delete(polytomousKey));
}
key.setTitleCache(descriptiveDataSet.getTitleCache(), true);
Taxon taxon = (Taxon) taxonService.load(taxonUuid);
key.addTaxonomicScope(taxon);
polytomousKeyService.saveOrUpdate(key);
result.setCdmEntity(key);
result.addUpdatedObject(taxon);
return result;
}
Aggregations