use of eu.etaxonomy.cdm.model.description.TaxonNameDescription in project cdmlib by cybertaxonomy.
the class TaxonNameTest method testAddAndRemoveDescriptionTaxonNameDescription.
@Test
public void testAddAndRemoveDescriptionTaxonNameDescription() {
TaxonNameDescription description = TaxonNameDescription.NewInstance();
// test if reflection method in addDescription() works
nameBase2.addDescription(description);
assertTrue("The description has not properly been added to the taxonName", nameBase2.getDescriptions().contains(description));
assertEquals("The taxon name has not properly been added to the description", nameBase2, description.getTaxonName());
// test if reflection method in removeDescription() works
nameBase2.removeDescription(description);
assertFalse("The description has not properly been removed from the taxon name", nameBase2.getDescriptions().contains(description));
assertEquals("The taxon name has not properly been removed from the description", null, description.getTaxonName());
}
use of eu.etaxonomy.cdm.model.description.TaxonNameDescription in project cdmlib by cybertaxonomy.
the class StatisticsDaoHibernateImplTest method createDataSet.
private void createDataSet() {
// create NO_OF_CLASSIFICATIONS classifications
classifications = new ArrayList<Classification>();
for (int i = 1; i <= NO_OF_CLASSIFICATIONS; i++) {
Classification classification = Classification.NewInstance("European Abies" + i);
classifications.add(classification);
classificationDao.save(classification);
}
// create all taxa, references and synonyms and attach them to one or
// more classifications
// variables: flags
int remainder = NO_OF_ACCEPTED_TAXA;
Reference sec = ReferenceFactory.newBook();
boolean secondClassificationForTaxonFlag = false;
boolean synonymFlag = false;
boolean tNomRefFlag = false;
boolean sNomRefFlag = false;
boolean tDescrSourceRefFlag = false;
boolean sDescrSourceRefFlag = false;
// variables: counter (pre-loop)
int descriptiveElementsPerTaxon = (NO_OF_DESCRIPTIVE_SOURCE_REFERENCES / NO_OF_ACCEPTED_TAXA) + 1;
int taxaInClass;
int classiCounter = 0, sharedClassification = 0, synonymCounter = 0, nomRefCounter = 0;
// iterate over classifications and add taxa
for (; /* see above */
remainder > 0 && classiCounter < NO_OF_CLASSIFICATIONS; ) /* see below */
{
// compute no of taxa to be created in this classification
if (classiCounter >= NO_OF_CLASSIFICATIONS - 1) {
// last classification
// gets all left taxa
taxaInClass = remainder;
} else {
// take half of left taxa for this class:
taxaInClass = remainder / 2;
}
// iterate over amount of taxa meant to be in this classification
for (int taxonCounter = 1; taxonCounter <= taxaInClass; taxonCounter++) {
// create a String for the Name
RandomStringUtils.randomAlphabetic(10);
String randomName = RandomStringUtils.randomAlphabetic(5) + " " + RandomStringUtils.randomAlphabetic(10);
// create a name for the taxon
IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
name.setNameCache(randomName, true);
// create nomenclatural reference for taxon name (if left)
if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
// we remember this taxon has a nomenclatural reference:
tNomRefFlag = true;
Reference nomRef = ReferenceFactory.newBook();
name.setNomenclaturalReference(nomRef);
referenceDao.save(nomRef);
nomRefCounter++;
}
// create a new sec for every other taxon
if (taxonCounter % 2 != 0) {
sec = createSecReference(classiCounter, taxonCounter);
}
// create the taxon
Taxon taxon = Taxon.NewInstance(name, sec);
if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
tDescrSourceRefFlag = true;
// create a description and 2 description elements with
// references for taxon name
TaxonNameDescription nameDescr = TaxonNameDescription.NewInstance();
CommonTaxonName nameElement = CommonTaxonName.NewInstance("Veilchen" + taxonCounter, Language.GERMAN());
TextData textElement = new TextData();
Reference nameElementRef = ReferenceFactory.newArticle();
Reference textElementRef = ReferenceFactory.newBookSection();
nameElement.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, nameElementRef, "name: ");
textElement.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, textElementRef, "text: ");
nameDescr.addElement(nameElement);
nameDescr.addElement(textElement);
name.addDescription(nameDescr);
// taxon.getName().addDescription(nameDescr);
referenceDao.save(nameElementRef);
referenceDao.save(textElementRef);
descriptionDao.save(nameDescr);
// create descriptions, description sources and their
// references
// for taxon
TaxonDescription taxonDescription = new TaxonDescription();
for (int i = 0; i < descriptiveElementsPerTaxon; i++) {
DescriptionElementBase descriptionElement = new TextData();
DescriptionElementSource descriptionElementSource = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
Reference article = ReferenceFactory.newArticle();
descriptionElementSource.setCitation(article);
descriptionElement.addSource(descriptionElementSource);
taxonDescription.addElement(descriptionElement);
referenceDao.save(article);
descriptionElementDao.save(descriptionElement);
}
descriptionDao.save(taxonDescription);
taxon.addDescription(taxonDescription);
// create a Specimen for taxon with description, descr.
// element and referece
//
// Specimen specimen = Specimen.NewInstance();
// SpecimenDescription specimenDescription =
// SpecimenDescription.NewInstance(specimen);
// DescriptionElementBase descrElement = new TextData();
// Reference specimenRef = ReferenceFactory.newArticle();
// descrElement.addSource(null, null, specimenRef, null);
//
//
// descriptionService.save(specimenDescription);
// taxon.add(specimen);
no_of_descriptive_source_references += descriptiveElementsPerTaxon + 2 + 1;
}
// add taxon to classification
classifications.get(classiCounter).addChildTaxon(taxon, null, null);
// now if there are any left, we create a synonym for the taxon
if (synonymCounter < NO_OF_SYNONYMS) {
synonymFlag = true;
randomName = RandomStringUtils.randomAlphabetic(5) + " " + RandomStringUtils.randomAlphabetic(10);
// name for synonym
name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
name.setNameCache(randomName, true);
// create nomenclatural reference for synonym name (if left)
if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
sNomRefFlag = true;
Reference nomRef = ReferenceFactory.newBook();
name.setNomenclaturalReference(nomRef);
referenceDao.save(nomRef);
nomRefCounter++;
}
if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
sDescrSourceRefFlag = true;
// create a description and 2 description elements with
// references for synonym name
TaxonNameDescription nameDescr = TaxonNameDescription.NewInstance();
CommonTaxonName nameElement = CommonTaxonName.NewInstance("anderes Veilchen" + taxonCounter, Language.GERMAN());
TextData textElement = new TextData();
Reference nameElementRef = ReferenceFactory.newArticle();
Reference textElementRef = ReferenceFactory.newBookSection();
nameElement.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, nameElementRef, "name: ");
textElement.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, textElementRef, "text: ");
nameDescr.addElement(nameElement);
nameDescr.addElement(textElement);
name.addDescription(nameDescr);
// taxon.getName().addDescription(nameDescr);
referenceDao.save(nameElementRef);
referenceDao.save(textElementRef);
descriptionDao.save(nameDescr);
no_of_descriptive_source_references += 2;
}
// create a new reference for every other synonym:
if (taxonCounter % 2 != 0) {
sec = createSecReference(classiCounter, taxonCounter);
}
Synonym synonym = Synonym.NewInstance(name, sec);
taxonDao.save(synonym);
taxon.addSynonym(synonym, SynonymType.SYNONYM_OF());
synonymCounter++;
}
// we add the taxon to the next class in the list too.
if (classiCounter < NO_OF_CLASSIFICATIONS && sharedClassification < NO_OF_SHARED_TAXA) {
classifications.get(classiCounter + 1).addChildTaxon(taxon, null, null);
// we remember that this taxon is attached to 2
// classifications:
secondClassificationForTaxonFlag = true;
sharedClassification++;
classificationDao.saveOrUpdate(classifications.get(classiCounter + 1));
}
taxonDao.save(taxon);
classificationDao.saveOrUpdate(classifications.get(classiCounter));
// count the data created with this taxon:
int c = classiCounter;
if (secondClassificationForTaxonFlag) {
c++;
}
// twice, if it is attached to 2 classifications
for (int i = classiCounter; i <= c; i++) {
// count everything just created for this taxon:
increment(no_of_accepted_taxa_c, i);
increment(no_of_taxon_names_c, i);
if (tNomRefFlag) {
increment(no_of_nomenclatural_references_c, i);
}
if (sNomRefFlag) {
increment(no_of_nomenclatural_references_c, i);
}
if (synonymFlag) {
increment(no_of_synonyms_c, i);
increment(no_of_taxon_names_c, i);
}
if (taxonCounter % 2 != 0) {
increment(no_of_all_references_c, i);
if (synonymFlag) {
increment(no_of_all_references_c, i);
}
}
if (tDescrSourceRefFlag) {
increment(no_of_descriptive_source_references_c, i, descriptiveElementsPerTaxon + 2);
}
if (sDescrSourceRefFlag) {
increment(no_of_descriptive_source_references_c, i, 2);
}
}
// put flags back:
secondClassificationForTaxonFlag = false;
tNomRefFlag = false;
sNomRefFlag = false;
synonymFlag = false;
tDescrSourceRefFlag = false;
sDescrSourceRefFlag = false;
}
// modify variables (post-loop)
classiCounter++;
remainder -= taxaInClass;
}
merge(no_of_accepted_taxa_c, no_of_synonyms_c, no_of_all_taxa_c);
merge(no_of_all_references_c, no_of_nomenclatural_references_c, no_of_all_references_c);
}
use of eu.etaxonomy.cdm.model.description.TaxonNameDescription in project cdmlib by cybertaxonomy.
the class DescriptionServiceImpl method moveDescriptionElementsToDescription.
@Override
@Transactional(readOnly = false)
public UpdateResult moveDescriptionElementsToDescription(Set<UUID> descriptionElementUUIDs, DescriptionBase targetDescription, boolean isCopy, boolean setNameInSource) {
Set<DescriptionElementBase> descriptionElements = new HashSet<DescriptionElementBase>();
for (UUID deUuid : descriptionElementUUIDs) {
DescriptionElementBase element = descriptionElementDao.load(deUuid);
if (element != null) {
descriptionElements.add(element);
}
}
DescriptionBase newTargetDescription;
if (targetDescription.isPersited()) {
newTargetDescription = dao.load(targetDescription.getUuid());
} else {
if (targetDescription instanceof TaxonDescription) {
Taxon taxon = (Taxon) taxonDao.load(((TaxonDescription) targetDescription).getTaxon().getUuid());
newTargetDescription = TaxonDescription.NewInstance(taxon, targetDescription.isImageGallery());
} else if (targetDescription instanceof TaxonNameDescription) {
TaxonName name = nameDao.load(((TaxonNameDescription) targetDescription).getTaxonName().getUuid());
newTargetDescription = TaxonNameDescription.NewInstance(name);
} else {
SpecimenOrObservationBase specimen = occurrenceDao.load(((SpecimenDescription) targetDescription).getDescribedSpecimenOrObservation().getUuid());
newTargetDescription = SpecimenDescription.NewInstance(specimen);
}
newTargetDescription.addSources(targetDescription.getSources());
newTargetDescription.setTitleCache(targetDescription.getTitleCache(), targetDescription.isProtectedTitleCache());
}
return moveDescriptionElementsToDescription(descriptionElements, newTargetDescription, isCopy, setNameInSource);
}
use of eu.etaxonomy.cdm.model.description.TaxonNameDescription in project cdmlib by cybertaxonomy.
the class DescriptionServiceImpl method isDeletable.
@Override
public DeleteResult isDeletable(UUID descriptionUuid) {
DeleteResult result = new DeleteResult();
DescriptionBase<?> description = this.load(descriptionUuid);
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(description);
if (references == null || references.isEmpty()) {
return result;
}
for (CdmBase ref : references) {
if (description instanceof TaxonDescription && ref instanceof Taxon && ref.equals(((TaxonDescription) description).getTaxon())) {
continue;
} else if (description instanceof TaxonNameDescription && ref instanceof TaxonName && ref.equals(((TaxonNameDescription) description).getTaxonName())) {
continue;
} else if (description instanceof SpecimenDescription && ref instanceof SpecimenOrObservationBase && ref.equals(((SpecimenDescription) description).getDescribedSpecimenOrObservation())) {
continue;
} else if (ref instanceof DescriptionElementBase) {
continue;
} else if (ref instanceof CdmLinkSource && ((CdmLinkSource) ref).hasNoTarget()) {
// maybe only workaround #9801
continue;
} else {
String message = "The description can't be completely deleted because it is referenced by " + ref.getUserFriendlyTypeName();
result.setAbort();
result.addException(new ReferencedObjectUndeletableException(message));
result.addRelatedObject(ref);
}
}
return result;
}
use of eu.etaxonomy.cdm.model.description.TaxonNameDescription in project cdmlib by cybertaxonomy.
the class DescriptionServiceImpl method mergeDescriptions.
@Override
@Transactional(readOnly = false)
public UpdateResult mergeDescriptions(Collection<DescriptionBaseDto> descriptions, UUID descriptiveDataSetUuid) {
UpdateResult result = new UpdateResult();
DescriptiveDataSet dataSet = descriptiveDataSetDao.load(descriptiveDataSetUuid);
Set<DescriptionBase> descriptionsOfDataSet = dataSet.getDescriptions();
HashMap<UUID, Set<DescriptionBase>> descriptionSpecimenMap = new HashMap<>();
Set<DescriptionBase> specimenDescriptions;
for (DescriptionBase<?> descriptionBase : descriptionsOfDataSet) {
if (descriptionBase.getDescribedSpecimenOrObservation() != null) {
specimenDescriptions = descriptionSpecimenMap.get(descriptionBase.getDescribedSpecimenOrObservation().getUuid());
if (specimenDescriptions == null) {
specimenDescriptions = new HashSet<>();
}
specimenDescriptions.add(descriptionBase);
descriptionSpecimenMap.put(descriptionBase.getDescribedSpecimenOrObservation().getUuid(), specimenDescriptions);
}
if (descriptionBase instanceof TaxonDescription) {
specimenDescriptions = descriptionSpecimenMap.get(((TaxonDescription) descriptionBase).getTaxon().getUuid());
if (specimenDescriptions == null) {
specimenDescriptions = new HashSet<>();
}
specimenDescriptions.add(descriptionBase);
descriptionSpecimenMap.put(((TaxonDescription) descriptionBase).getTaxon().getUuid(), specimenDescriptions);
}
if (descriptionBase instanceof TaxonNameDescription) {
specimenDescriptions = descriptionSpecimenMap.get(((TaxonNameDescription) descriptionBase).getTaxonName().getUuid());
if (specimenDescriptions == null) {
specimenDescriptions = new HashSet<>();
}
specimenDescriptions.add(descriptionBase);
descriptionSpecimenMap.put(((TaxonNameDescription) descriptionBase).getTaxonName().getUuid(), specimenDescriptions);
}
}
MergeResult<DescriptionBase> mergeResult = null;
for (DescriptionBaseDto descDto : descriptions) {
UUID descriptionUUID = descDto.getDescriptionUuid();
DescriptionBase<?> description = load(descriptionUUID);
UUID describedObjectUuid = null;
if (description instanceof SpecimenDescription) {
describedObjectUuid = descDto.getSpecimenDto().getUuid();
} else if (description instanceof TaxonDescription) {
describedObjectUuid = descDto.getTaxonDto().getUuid();
} else if (description instanceof TaxonNameDescription) {
describedObjectUuid = descDto.getNameDto().getUuid();
}
Set<DescriptionBase> descSpecimen = descriptionSpecimenMap.get(describedObjectUuid);
if (descSpecimen != null) {
// TODO: elements are Dtos now, no cdm entities, needs to get the value and replace or create new description element
Set<DescriptionElementDto> elements = new HashSet<>();
for (Object element : descDto.getElements()) {
elements.add((DescriptionElementDto) element);
}
DescriptionBase<?> desc = null;
for (DescriptionBase<?> tempDesc : descSpecimen) {
if (tempDesc.getUuid().equals(descDto.getDescriptionUuid())) {
desc = tempDesc;
break;
}
}
Set<DescriptionElementBase> removeElements = new HashSet<>();
Set<DescriptionElementBase> descriptionElements = desc.getElements();
for (DescriptionElementBase elementBase : descriptionElements) {
UUID descElementUuid = elementBase.getUuid();
if (descElementUuid != null) {
List<DescriptionElementDto> equalUuidsElements = elements.stream().filter(e -> e != null && e.getElementUuid() != null && e.getElementUuid().equals(descElementUuid)).collect(Collectors.toList());
if (equalUuidsElements.size() == 0 || (equalUuidsElements.size() == 1 && equalUuidsElements.get(0) instanceof QuantitativeDataDto && ((QuantitativeDataDto) equalUuidsElements.get(0)).getValues().isEmpty())) {
removeElements.add(elementBase);
}
}
}
if (!removeElements.isEmpty()) {
for (DescriptionElementBase el : removeElements) {
desc.removeElement(el);
}
}
for (DescriptionElementDto descElement : elements) {
if (descElement == null) {
continue;
}
UUID descElementUuid = descElement.getElementUuid();
if (descElement instanceof CategoricalDataDto && ((CategoricalDataDto) descElement).getStates().isEmpty() || descElement instanceof QuantitativeDataDto && ((QuantitativeDataDto) descElement).getValues().isEmpty()) {
continue;
}
List<DescriptionElementBase> equalUuidsElements = descriptionElements.stream().filter(e -> e.getUuid().equals(descElementUuid)).collect(Collectors.toList());
eu.etaxonomy.cdm.model.description.Feature feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Feature.class, descElement.getFeatureUuid());
if (feature == null) {
feature = DefinedTermBase.getTermByClassAndUUID(eu.etaxonomy.cdm.model.description.Character.class, descElement.getFeatureUuid());
}
if (equalUuidsElements.size() == 0) {
if (descElement instanceof CategoricalDataDto) {
CategoricalData elementBase = CategoricalData.NewInstance(feature);
List<StateDataDto> stateDtos = ((CategoricalDataDto) descElement).getStates();
for (StateDataDto dataDto : stateDtos) {
// create new statedata
State newState = DefinedTermBase.getTermByClassAndUUID(State.class, dataDto.getState().getUuid());
StateData newStateData = StateData.NewInstance(newState);
elementBase.addStateData(newStateData);
}
desc.addElement(elementBase);
}
if (descElement instanceof QuantitativeDataDto) {
QuantitativeData data = QuantitativeData.NewInstance(feature);
if (((QuantitativeDataDto) descElement).getMeasurementUnit() != null) {
MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) descElement).getMeasurementUnit().getUuid());
data.setUnit(unit);
}
Set<StatisticalMeasurementValue> statisticalValues = new HashSet<>();
Set<StatisticalMeasurementValueDto> valueDtos = ((QuantitativeDataDto) descElement).getValues();
data.getStatisticalValues().clear();
for (StatisticalMeasurementValueDto dataDto : valueDtos) {
// create new statedata
StatisticalMeasurementValue newStatisticalMeasurement = StatisticalMeasurementValue.NewInstance(DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, dataDto.getType().getUuid()), dataDto.getValue());
statisticalValues.add(newStatisticalMeasurement);
data.addStatisticalValue(newStatisticalMeasurement);
}
// data.getStatisticalValues().addAll(statisticalValues);
data = StructuredDescriptionAggregation.handleMissingMinOrMax(data, MissingMinimumMode.MinToZero, MissingMaximumMode.MaxToMin);
desc.addElement(data);
}
// create new element
} else {
DescriptionElementBase elementBase = equalUuidsElements.get(0);
if (elementBase.isInstanceOf(CategoricalData.class)) {
CategoricalData data = HibernateProxyHelper.deproxy(elementBase, CategoricalData.class);
List<StateData> states = new ArrayList<>(data.getStateData());
List<StateDataDto> stateDtos = ((CategoricalDataDto) descElement).getStates();
data.getStateData().clear();
if (stateDtos.isEmpty()) {
desc.removeElement(data);
} else {
for (StateDataDto dataDto : stateDtos) {
State newState = DefinedTermBase.getTermByClassAndUUID(State.class, dataDto.getState().getUuid());
StateData newStateData = StateData.NewInstance(newState);
data.addStateData(newStateData);
}
}
} else if (elementBase.isInstanceOf(QuantitativeData.class)) {
QuantitativeData data = HibernateProxyHelper.deproxy(elementBase, QuantitativeData.class);
Set<StatisticalMeasurementValue> statisticalValues = new HashSet<>();
if (((QuantitativeDataDto) descElement).getMeasurementUnit() != null) {
MeasurementUnit unit = DefinedTermBase.getTermByClassAndUUID(MeasurementUnit.class, ((QuantitativeDataDto) descElement).getMeasurementUnit().getUuid());
if (data.getUnit() == null || (data.getUnit() != null && !data.getUnit().equals(unit))) {
data.setUnit(unit);
}
}
Set<StatisticalMeasurementValueDto> valueDtos = ((QuantitativeDataDto) descElement).getValues();
data.getStatisticalValues().clear();
if (valueDtos.isEmpty()) {
desc.removeElement(data);
} else {
for (StatisticalMeasurementValueDto dataDto : valueDtos) {
// create new statedata
StatisticalMeasure statMeasure = DefinedTermBase.getTermByClassAndUUID(StatisticalMeasure.class, dataDto.getType().getUuid());
StatisticalMeasurementValue newStatisticalMeasurement = StatisticalMeasurementValue.NewInstance(statMeasure, dataDto.getValue());
statisticalValues.add(newStatisticalMeasurement);
data.addStatisticalValue(newStatisticalMeasurement);
}
// data.getStatisticalValues().addAll(statisticalValues);
data = StructuredDescriptionAggregation.handleMissingMinOrMax(data, MissingMinimumMode.MinToZero, MissingMaximumMode.MaxToMin);
}
}
}
// remove deleted elements
}
descriptionSpecimenMap.get(describedObjectUuid).add(desc);
description = desc;
}
try {
if (description != null) {
mergeResult = dao.merge(description, true);
result.addUpdatedObject(mergeResult.getMergedEntity());
}
// if (description instanceof SpecimenDescription){
// result.addUpdatedObject(mergeResult.getMergedEntity().getDescribedSpecimenOrObservation());
// }else if (description instanceof TaxonDescription){
// result.addUpdatedObject(((TaxonDescription)mergeResult.getMergedEntity()).getTaxon());
// }else if (description instanceof TaxonNameDescription){
// result.addUpdatedObject(((TaxonNameDescription)mergeResult.getMergedEntity()).getTaxonName());
// }
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
Aggregations