Search in sources :

Example 16 with StatisticalMeasure

use of eu.etaxonomy.cdm.model.description.StatisticalMeasure 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;
}
Also used : ArrayList(java.util.ArrayList) TermNode(eu.etaxonomy.cdm.model.term.TermNode) StatisticalMeasure(eu.etaxonomy.cdm.model.description.StatisticalMeasure) DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) TermVocabularyDto(eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto) CharacterDto(eu.etaxonomy.cdm.persistence.dto.CharacterDto) UUID(java.util.UUID) HashSet(java.util.HashSet) MeasurementUnit(eu.etaxonomy.cdm.model.description.MeasurementUnit) Character(eu.etaxonomy.cdm.model.description.Character) TermVocabulary(eu.etaxonomy.cdm.model.term.TermVocabulary) Representation(eu.etaxonomy.cdm.model.term.Representation) TermDto(eu.etaxonomy.cdm.persistence.dto.TermDto) CharacterNodeDto(eu.etaxonomy.cdm.persistence.dto.CharacterNodeDto) DefinedTerm(eu.etaxonomy.cdm.model.term.DefinedTerm)

Aggregations

StatisticalMeasure (eu.etaxonomy.cdm.model.description.StatisticalMeasure)16 MeasurementUnit (eu.etaxonomy.cdm.model.description.MeasurementUnit)11 HashSet (java.util.HashSet)10 DefinedTerm (eu.etaxonomy.cdm.model.term.DefinedTerm)7 TermVocabulary (eu.etaxonomy.cdm.model.term.TermVocabulary)7 StatisticalMeasurementValue (eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue)6 ArrayList (java.util.ArrayList)6 UUID (java.util.UUID)6 State (eu.etaxonomy.cdm.model.description.State)5 Set (java.util.Set)5 QuantitativeData (eu.etaxonomy.cdm.model.description.QuantitativeData)4 TermDto (eu.etaxonomy.cdm.persistence.dto.TermDto)4 HashMap (java.util.HashMap)4 Feature (eu.etaxonomy.cdm.model.description.Feature)3 Media (eu.etaxonomy.cdm.model.media.Media)3 DefinedTermBase (eu.etaxonomy.cdm.model.term.DefinedTermBase)3 CategoricalDataDto (eu.etaxonomy.cdm.api.service.dto.CategoricalDataDto)2 DescriptionBaseDto (eu.etaxonomy.cdm.api.service.dto.DescriptionBaseDto)2 DescriptionElementDto (eu.etaxonomy.cdm.api.service.dto.DescriptionElementDto)2 QuantitativeDataDto (eu.etaxonomy.cdm.api.service.dto.QuantitativeDataDto)2