use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class TermNodeServiceImpl method saveCharacterNodeDtoList.
@Override
public UpdateResult saveCharacterNodeDtoList(List<CharacterNodeDto> dtos) {
MergeResult<TermNode> mergeResult;
UpdateResult result = new UpdateResult();
List<UUID> nodeUuids = new ArrayList<>();
dtos.stream().forEach(dto -> nodeUuids.add(dto.getUuid()));
List<TermNode> nodes = dao.list(nodeUuids, null, 0, null, null);
// check all attributes for changes and adapt
for (TermNode<Character> node : nodes) {
for (CharacterNodeDto dto : dtos) {
// TermNodeDto dto = dtoIterator.next();
if (dto.getUuid().equals(node.getUuid())) {
updateFeatureStates(node, dto, true);
updateFeatureStates(node, dto, false);
// if (!dto.getInapplicableIf().equals(node.getInapplicableIf())){
// node.getInapplicableIf().clear();
// node.getInapplicableIf().addAll(dto.getInapplicableIf());
// }
// if (!dto.getOnlyApplicableIf().equals(node.getOnlyApplicableIf())){
// node.getOnlyApplicableIf().clear();
// node.getOnlyApplicableIf().addAll(dto.getOnlyApplicableIf());
// }
Character character = null;
CharacterDto characterDto = (CharacterDto) dto.getTerm();
character = HibernateProxyHelper.deproxy(node.getTerm(), Character.class);
if (characterDto.getRatioTo() != null) {
TermNode ratioToStructure = this.load(characterDto.getRatioTo().getUuid());
character.setRatioToStructure(ratioToStructure);
} else {
character.setRatioToStructure(null);
}
// supportsXXX
// TODO add all other supportsXXX (6 are missing)
character.setSupportsCategoricalData(characterDto.isSupportsCategoricalData());
character.setSupportsQuantitativeData(characterDto.isSupportsQuantitativeData());
// availableForXXX
character.setAvailableForTaxon(characterDto.isAvailableForTaxon());
character.setAvailableForOccurrence(characterDto.isAvailableForOccurrence());
character.setAvailableForTaxonName(characterDto.isAvailableForTaxonName());
// representations
for (Representation rep : dto.getTerm().getRepresentations()) {
Representation oldRep = character.getRepresentation(rep.getLanguage());
if (oldRep == null) {
oldRep = new Representation();
oldRep.setLanguage(rep.getLanguage());
character.addRepresentation(oldRep);
}
oldRep.setLabel(rep.getLabel());
oldRep.setAbbreviatedLabel(rep.getAbbreviatedLabel());
oldRep.setText(rep.getText());
oldRep.setPlural(rep.getPlural());
}
Set<Representation> deleteRepresentations = new HashSet<>();
if (character.getRepresentations().size() > dto.getTerm().getRepresentations().size()) {
for (Representation rep : character.getRepresentations()) {
if (dto.getTerm().getRepresentation(rep.getLanguage()) == null) {
deleteRepresentations.add(rep);
}
}
}
if (!deleteRepresentations.isEmpty()) {
for (Representation rep : deleteRepresentations) {
character.removeRepresentation(rep);
}
}
// structural modifier
if (characterDto.getStructureModifier() != null) {
DefinedTerm structureModifier = (DefinedTerm) termService.load(characterDto.getStructureModifier().getUuid());
character.setStructureModifier(structureModifier);
} else {
character.setStructureModifier(null);
}
// recommended measurement units
character.getRecommendedMeasurementUnits().clear();
List<UUID> uuids = new ArrayList<>();
for (TermDto termDto : characterDto.getRecommendedMeasurementUnits()) {
uuids.add(termDto.getUuid());
}
List<DefinedTermBase> terms;
if (!uuids.isEmpty()) {
terms = termService.load(uuids, null);
Set<MeasurementUnit> measurementUnits = new HashSet<>();
for (DefinedTermBase term : terms) {
if (term instanceof MeasurementUnit) {
measurementUnits.add((MeasurementUnit) term);
}
}
character.getRecommendedMeasurementUnits().addAll(measurementUnits);
}
// statistical measures
character.getRecommendedStatisticalMeasures().clear();
uuids = new ArrayList<>();
for (TermDto termDto : characterDto.getRecommendedStatisticalMeasures()) {
uuids.add(termDto.getUuid());
}
if (!uuids.isEmpty()) {
terms = termService.load(uuids, null);
Set<StatisticalMeasure> statisticalMeasures = new HashSet<>();
for (DefinedTermBase<?> term : terms) {
if (term instanceof StatisticalMeasure) {
statisticalMeasures.add((StatisticalMeasure) term);
}
}
character.getRecommendedStatisticalMeasures().addAll(statisticalMeasures);
}
// recommended mod. vocabularies
character.getRecommendedModifierEnumeration().clear();
uuids = new ArrayList<>();
for (TermVocabularyDto termDto : characterDto.getRecommendedModifierEnumeration()) {
uuids.add(termDto.getUuid());
}
List<TermVocabulary> termVocs;
if (!uuids.isEmpty()) {
termVocs = vocabularyService.load(uuids, null);
for (TermVocabulary voc : termVocs) {
character.addRecommendedModifierEnumeration(voc);
}
}
// supported state vocabularies
character.getSupportedCategoricalEnumerations().clear();
uuids = new ArrayList<>();
for (TermVocabularyDto termDto : characterDto.getSupportedCategoricalEnumerations()) {
uuids.add(termDto.getUuid());
}
if (!uuids.isEmpty()) {
termVocs = vocabularyService.load(uuids, null);
for (TermVocabulary voc : termVocs) {
character.addSupportedCategoricalEnumeration(voc);
}
}
node.setTerm(character);
mergeResult = dao.merge(node, true);
result.addUpdatedObject(mergeResult.getMergedEntity());
}
}
}
return result;
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class TermNodeServiceImpl method moveNode.
@Override
public UpdateResult moveNode(UUID movedNodeUuid, UUID targetNodeUuid, int position) {
UpdateResult result = new UpdateResult();
List<String> propertyPaths = new ArrayList<>();
propertyPaths.add("parent");
propertyPaths.add("parent.children");
propertyPaths.add("children");
TermNode test = load(movedNodeUuid, propertyPaths);
TermNode movedNode = CdmBase.deproxy(load(movedNodeUuid, propertyPaths), TermNode.class);
TermNode<?> targetNode = CdmBase.deproxy(load(targetNodeUuid, propertyPaths));
TermNode<?> parent = CdmBase.deproxy(movedNode.getParent());
parent.removeChild(movedNode);
if (position < 0) {
targetNode.addChild(movedNode);
} else {
targetNode.addChild(movedNode, position);
}
result.addUpdatedObject(targetNode);
result.addUpdatedObject(parent);
result.setCdmEntity(movedNode);
return result;
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class TermNodeServiceImpl method addChildNode.
@Override
public UpdateResult addChildNode(UUID nodeUUID, UUID termChildUuid, int position) {
UpdateResult result = new UpdateResult();
TermNode node = load(nodeUUID);
if (node == null) {
result.setError();
result.addException(new Exception("The parent node does not exist."));
return result;
}
DefinedTermBase child = HibernateProxyHelper.deproxy(termService.load(termChildUuid), DefinedTermBase.class);
if (node != null && node.getGraph() != null && !node.getGraph().isAllowDuplicates() && node.getGraph().getDistinctTerms().contains(child)) {
result.setError();
result.addException(new Exception("This term tree does not allow duplicate terms."));
return result;
}
TermNode childNode;
if (position < 0) {
childNode = node.addChild(child);
} else {
childNode = node.addChild(child, position);
}
save(childNode);
result.addUpdatedObject(node);
result.setCdmEntity(childNode);
return result;
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class DistributionAggregation method makeStatusOrder.
/**
* Sets the priorities for presence and absence terms, the priorities are stored in extensions.
* This method will start a new transaction and commits it after the work is done.
*/
private void makeStatusOrder() {
TransactionStatus txStatus = startTransaction(false);
@SuppressWarnings("rawtypes") TermCollection<PresenceAbsenceTerm, TermNode> stOrder = getConfig().getStatusOrder();
if (stOrder == null) {
stOrder = defaultStatusOrder();
}
if (stOrder.isInstanceOf(TermTree.class)) {
statusOrder = CdmBase.deproxy(stOrder, TermTree.class).asTermList();
} else if (stOrder.isInstanceOf(OrderedTermVocabulary.class)) {
statusOrder = new ArrayList<>(CdmBase.deproxy(stOrder, OrderedTermVocabulary.class).getOrderedTerms());
} else {
throw new RuntimeException("TermCollection type for status order not supported: " + statusOrder.getClass().getSimpleName());
}
commitTransaction(txStatus);
}
use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.
the class TermNodeServiceImplTest method testSaveCharacterNode_representation.
@Test
public void testSaveCharacterNode_representation() {
UUID characterTreeUuid = createAndSaveCharacterTree();
TermTreeDto dto = termTreeService.getTermTreeDtoByUuid(characterTreeUuid);
List<TermNodeDto> children = dto.getRoot().getChildren();
CharacterNodeDto nodeDto = (CharacterNodeDto) children.get(0);
TermDto termDto = nodeDto.getTerm();
if (termDto instanceof CharacterDto) {
CharacterDto characterDto = (CharacterDto) termDto;
Representation rep = characterDto.getPreferredRepresentation(Language.DEFAULT());
if (rep != null) {
rep.setText("Test");
} else {
rep = Representation.NewInstance("Test", "", "", Language.DEFAULT());
characterDto.addRepresentation(rep);
}
List<CharacterNodeDto> dtos = new ArrayList<>();
dtos.add(nodeDto);
termNodeService.saveCharacterNodeDtoList(dtos);
commitAndStartNewTransaction();
@SuppressWarnings("unchecked") TermTree<Feature> characterTree = termTreeService.load(characterTreeUuid);
List<TermNode<Feature>> childNodes = characterTree.getRoot().getChildNodes();
TermNode<Feature> child = childNodes.get(0);
Assert.assertTrue(child.getTerm().getPreferredRepresentation(Language.DEFAULT()).getText().equals("Test"));
} else {
Assert.fail();
}
}
Aggregations