use of eu.etaxonomy.cdm.model.common.IdentifiableSource in project cdmlib by cybertaxonomy.
the class OwlImportUtil method addTermProperties.
private static void addTermProperties(DefinedTermBase term, Resource termResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state) {
term.setUuid(UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString()));
// term URI
String uriString = termResource.hasProperty(OwlUtil.propUri) ? termResource.getProperty(OwlUtil.propUri).getString() : null;
if (CdmUtils.isNotBlank(uriString)) {
term.setUri(URI.create(uriString));
}
// symbol
String symbolString = termResource.hasProperty(OwlUtil.propTermSymbol) ? termResource.getProperty(OwlUtil.propTermSymbol).getString() : null;
if (CdmUtils.isNotBlank(symbolString)) {
term.setSymbol(symbolString);
}
// symbol2
String symbol2String = termResource.hasProperty(OwlUtil.propTermSymbol2) ? termResource.getProperty(OwlUtil.propTermSymbol2).getString() : null;
if (CdmUtils.isNotBlank(symbol2String)) {
term.setSymbol2(symbol2String);
}
// idInVocabulary
String idInVocabularyString = termResource.hasProperty(OwlUtil.propTermIdInVocabulary) ? termResource.getProperty(OwlUtil.propTermIdInVocabulary).getString() : null;
if (CdmUtils.isNotBlank(idInVocabularyString)) {
term.setIdInVocabulary(idInVocabularyString);
}
// import representations
Set<Representation> representations = new HashSet<>();
termResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r -> representations.add(OwlImportUtil.createRepresentation(repo, r, model)));
if (representations.isEmpty()) {
logger.error("No representations found for term: " + termResource.getProperty(OwlUtil.propUuid));
}
representations.forEach(rep -> term.addRepresentation(rep));
// import sources
Set<IdentifiableSource> sources = new HashSet<>();
termResource.listProperties(OwlUtil.propTermHasSource).forEachRemaining(sourceStatement -> sources.add(OwlImportUtil.createSource(sourceStatement, repo, model)));
sources.forEach(source -> term.addSource(source));
// add import source
IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(termResource.getURI());
importSource.setCitation(state.getConfig().getSourceReference());
term.addSource(importSource);
}
use of eu.etaxonomy.cdm.model.common.IdentifiableSource in project cdmlib by cybertaxonomy.
the class OwlImportUtil method createMedia.
static Media createMedia(Resource mediaResource, StructureTreeOwlImportState state) {
URI mediaUri = URI.create(mediaResource.getProperty(OwlUtil.propMediaUri).getString());
// create new media
Media media = Media.NewInstance(mediaUri, null, null, null);
media.setUuid(UUID.fromString(mediaResource.getProperty(OwlUtil.propUuid).getString()));
if (mediaResource.hasProperty(OwlUtil.propMediaTitle)) {
// TODO: support multiple language titles
media.putTitle(Language.DEFAULT(), mediaResource.getProperty(OwlUtil.propMediaTitle).getString());
}
IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(mediaResource.getURI());
importSource.setCitation(state.getConfig().getSourceReference());
media.addSource(importSource);
return media;
}
use of eu.etaxonomy.cdm.model.common.IdentifiableSource in project cdmlib by cybertaxonomy.
the class OwlImportUtil method createVocabulary.
static TermVocabulary createVocabulary(Resource vocabularyResource, ICdmRepository repo, Model model, StructureTreeOwlImportState state) {
TermType termType = TermType.getByKey(vocabularyResource.getProperty(OwlUtil.propType).getString());
// create new vocabulary
TermVocabulary vocabulary = TermVocabulary.NewInstance(termType);
vocabulary.setUuid(UUID.fromString(vocabularyResource.getProperty(OwlUtil.propUuid).getString()));
// voc URI
String vocUriString = vocabularyResource.hasProperty(OwlUtil.propUri) ? vocabularyResource.getProperty(OwlUtil.propUri).getString() : null;
if (CdmUtils.isNotBlank(vocUriString)) {
vocabulary.setUri(URI.create(vocUriString));
}
// voc representations
Set<Representation> vocRepresentations = new HashSet<>();
vocabularyResource.listProperties(OwlUtil.propHasRepresentation).forEachRemaining(r -> vocRepresentations.add(OwlImportUtil.createRepresentation(repo, r, model)));
if (vocRepresentations.isEmpty()) {
logger.error("No representations found for vocabulary: " + vocabularyResource.getProperty(OwlUtil.propUuid));
}
vocRepresentations.forEach(rep -> vocabulary.addRepresentation(rep));
IdentifiableSource importSource = IdentifiableSource.NewDataImportInstance(vocabularyResource.getURI());
importSource.setCitation(state.getConfig().getSourceReference());
vocabulary.addSource(importSource);
return vocabulary;
}
use of eu.etaxonomy.cdm.model.common.IdentifiableSource 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.common.IdentifiableSource in project cdmlib by cybertaxonomy.
the class CommonServiceImpl method getReferencingObjectTarget.
private UuidAndTitleCache<CdmBase> getReferencingObjectTarget(CdmBase entity) {
CdmBase targetEntity;
entity = CdmBase.deproxy(entity);
if (entity instanceof SecundumSource) {
targetEntity = ((SecundumSource) entity).getSourcedTaxon();
} else if (entity instanceof NomenclaturalSource) {
targetEntity = ((NomenclaturalSource) entity).getSourcedName();
} else if (entity instanceof DescriptionElementSource) {
DescriptionElementBase element = ((DescriptionElementSource) entity).getSourcedElement();
targetEntity = getTarget(element);
} else if (entity instanceof DescriptionElementBase) {
targetEntity = getTarget((DescriptionElementBase) entity);
} else if (entity instanceof IdentifiableSource) {
IdentifiableSource source = (IdentifiableSource) entity;
targetEntity = originalSourceDao.findIdentifiableBySourceId(IdentifiableEntity.class, source.getId());
} else if (entity instanceof NamedSource) {
NamedSource source = (NamedSource) entity;
SingleSourcedEntityBase singleSourced = originalSourceDao.findSingleSourceBySourceId(SingleSourcedEntityBase.class, source.getId());
if (singleSourced != null) {
targetEntity = singleSourced;
} else {
// TODO
targetEntity = entity;
}
} else if (entity instanceof DescriptionBase) {
targetEntity = getTarget((DescriptionBase<?>) entity);
} else {
targetEntity = entity;
}
targetEntity = CdmBase.deproxy(targetEntity);
if (targetEntity == null) {
targetEntity = entity;
}
String targetLabel = targetEntity instanceof IdentifiableEntity ? ((IdentifiableEntity<?>) targetEntity).getTitleCache() : null;
UuidAndTitleCache<CdmBase> result = new UuidAndTitleCache<>(targetEntity.getClass(), targetEntity.getUuid(), targetEntity.getId(), targetLabel);
return result;
}
Aggregations