Search in sources :

Example 1 with ValueSet

use of org.ehrbase.terminology.client.terminology.ValueSet in project openEHR_SDK by ehrbase.

the class ClassGenerator method addSimpleField.

private void addSimpleField(ClassGeneratorContext context, TypeSpec.Builder classBuilder, String path, WebTemplateNode endNode) {
    Class<?> clazz = extractClass(endNode);
    if (clazz == null) {
        logger.warn("No class for path {} ", path);
        return;
    }
    ValueSet valueSet = buildValueSet(endNode);
    RmClassGeneratorConfig classGeneratorConfig = configMap.get(clazz);
    if (classGeneratorConfig == null && !clazz.getName().contains("java.lang")) {
        logger.debug("No ClassGenerator for {}", clazz);
    }
    boolean expand = classGeneratorConfig != null && classGeneratorConfig.isExpandField();
    if (endNode.getRmType().equals("DV_CODED_TEXT") && !List.of("transition", "language", "setting", "category", "territory", "math_function", "null_flavour").contains(endNode.getId(false))) {
        expand = expand && endNode.getInputs().stream().filter(i -> i.getType().equals("CODED_TEXT")).map(WebTemplateInput::getList).flatMap(List::stream).findAny().isPresent();
    }
    if (!expand) {
        TypeName className = Optional.ofNullable(clazz).map(ClassName::get).orElse(ClassName.get(Object.class));
        if (endNode.isMulti() && !context.nodeDeque.peek().getRmType().equals("ELEMENT")) {
            className = ParameterizedTypeName.get(ClassName.get(List.class), className);
        }
        addField(context, classBuilder, path, endNode, className, valueSet, false);
    } else {
        Map<String, Field> fieldMap = Arrays.stream(FieldUtils.getAllFields(clazz)).filter(f -> !f.isSynthetic()).collect(Collectors.toMap(Field::getName, f -> f));
        Set<String> expandFields = classGeneratorConfig.getExpandFields();
        expandFields.forEach(fieldName -> addField(context, classBuilder, path + "|" + new SnakeCase(fieldName).camelToSnake(), endNode, ClassName.get(fieldMap.get(fieldName).getType()), valueSet, false));
    }
}
Also used : Arrays(java.util.Arrays) Language(org.ehrbase.client.classgenerator.shareddefinition.Language) Composition(com.nedap.archie.rm.composition.Composition) Modifier(javax.lang.model.element.Modifier) MathFunction(org.ehrbase.client.classgenerator.shareddefinition.MathFunction) VersionUid(org.ehrbase.client.openehrclient.VersionUid) ArchieRMInfoLookup(com.nedap.archie.rminfo.ArchieRMInfoLookup) LoggerFactory(org.slf4j.LoggerFactory) ClassName(com.squareup.javapoet.ClassName) StringUtils(org.apache.commons.lang3.StringUtils) Pair(org.apache.commons.lang3.tuple.Pair) Generated(javax.annotation.processing.Generated) Map(java.util.Map) Category(org.ehrbase.client.classgenerator.shareddefinition.Category) Locatable(com.nedap.archie.rm.archetyped.Locatable) Transition(org.ehrbase.client.classgenerator.shareddefinition.Transition) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) RMEntity(org.ehrbase.client.classgenerator.interfaces.RMEntity) Path(org.ehrbase.client.annotations.Path) Entry(com.nedap.archie.rm.composition.Entry) ReflectionHelper(org.ehrbase.util.reflection.ReflectionHelper) IntervalEventEntity(org.ehrbase.client.classgenerator.interfaces.IntervalEventEntity) FilteredWebTemplate(org.ehrbase.webtemplate.model.FilteredWebTemplate) WebTemplateInput(org.ehrbase.webtemplate.model.WebTemplateInput) Set(java.util.Set) Collectors(java.util.stream.Collectors) Template(org.ehrbase.client.annotations.Template) WebTemplateInputValue(org.ehrbase.webtemplate.model.WebTemplateInputValue) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Type(java.lang.reflect.Type) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) TermDefinition(org.ehrbase.terminology.client.terminology.TermDefinition) Territory(org.ehrbase.client.classgenerator.shareddefinition.Territory) FieldSpec(com.squareup.javapoet.FieldSpec) Id(org.ehrbase.client.annotations.Id) HashMap(java.util.HashMap) Deque(java.util.Deque) CompositionEntity(org.ehrbase.client.classgenerator.interfaces.CompositionEntity) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PointEventEntity(org.ehrbase.client.classgenerator.interfaces.PointEventEntity) WebTemplateFilter(org.ehrbase.webtemplate.filter.WebTemplateFilter) SetUtils(org.apache.commons.collections4.SetUtils) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) OptionFor(org.ehrbase.client.annotations.OptionFor) EntryEntity(org.ehrbase.client.classgenerator.interfaces.EntryEntity) Logger(org.slf4j.Logger) MethodSpec(com.squareup.javapoet.MethodSpec) Walker(org.ehrbase.serialisation.walker.Walker) Archetype(org.ehrbase.client.annotations.Archetype) NullFlavour(org.ehrbase.client.classgenerator.shareddefinition.NullFlavour) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) Field(java.lang.reflect.Field) PointEvent(com.nedap.archie.rm.datastructures.PointEvent) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) RmClassGeneratorConfig(org.ehrbase.client.classgenerator.config.RmClassGeneratorConfig) WebTemplate(org.ehrbase.webtemplate.model.WebTemplate) SnakeCase(org.ehrbase.serialisation.util.SnakeCase) FlatPath(org.ehrbase.webtemplate.parser.FlatPath) LocatableEntity(org.ehrbase.client.classgenerator.interfaces.LocatableEntity) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) Entity(org.ehrbase.client.annotations.Entity) Choice(org.ehrbase.client.annotations.Choice) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) IntervalEvent(com.nedap.archie.rm.datastructures.IntervalEvent) Collections(java.util.Collections) Setting(org.ehrbase.client.classgenerator.shareddefinition.Setting) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) SnakeCase(org.ehrbase.serialisation.util.SnakeCase) Field(java.lang.reflect.Field) List(java.util.List) ArrayList(java.util.ArrayList) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) RmClassGeneratorConfig(org.ehrbase.client.classgenerator.config.RmClassGeneratorConfig)

Example 2 with ValueSet

use of org.ehrbase.terminology.client.terminology.ValueSet in project openEHR_SDK by ehrbase.

the class ClassGenerator method build.

private TypeSpec.Builder build(ClassGeneratorContext context, WebTemplateNode next) {
    String className = defaultNamingStrategy.buildClassName(context, next, false, false);
    context.currentFieldNameMap.push(new HashMap<>());
    context.nodeDeque.push(next);
    context.unFilteredNodeDeque.push(next);
    TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className);
    if (StringUtils.isBlank(context.currentMainClass)) {
        context.currentMainClass = className;
    }
    classBuilder.addModifiers(Modifier.PUBLIC);
    classBuilder.addAnnotation(AnnotationSpec.builder(Entity.class).build());
    if (next.isArchetype()) {
        AnnotationSpec archetypeAnnotation = AnnotationSpec.builder(Archetype.class).addMember(Archetype.VALUE, "$S", next.getNodeId()).build();
        classBuilder.addAnnotation(archetypeAnnotation);
    }
    AnnotationSpec generatedAnnotation = buildGeneratedAnnotation();
    classBuilder.addAnnotation(generatedAnnotation);
    classBuilder.addSuperinterface(findRMInterface(next));
    if (next.getChildren().stream().anyMatch(n -> n.getRmType().equals("EVENT"))) {
        WebTemplateNode event = next.getChildren().stream().filter(n -> n.getRmType().equals("EVENT")).findAny().orElseThrow();
        Walker.EventHelper eventHelper = new Walker.EventHelper(event).invoke();
        WebTemplateNode pointEvent = eventHelper.getPointEvent();
        WebTemplateNode intervalEvent = eventHelper.getIntervalEvent();
        next.getChildren().add(intervalEvent);
        next.getChildren().add(pointEvent);
        next.getChildren().remove(event);
    }
    Map<String, List<WebTemplateNode>> choices = next.getChoicesInChildren();
    List<WebTemplateNode> children = next.getChildren().stream().filter(c -> choices.values().stream().flatMap(List::stream).noneMatch(l -> l.equals(c))).collect(Collectors.toList());
    for (WebTemplateNode child : children) {
        Deque<WebTemplateNode> filtersNodes = pushToUnfiltered(context, child);
        String relativPath = context.nodeDeque.peek().buildRelativePath(child);
        if (child.getChildren().isEmpty() && !choices.containsKey(child.getAqlPath())) {
            addSimpleField(context, classBuilder, relativPath, child);
        } else if (!choices.containsKey(child.getAqlPath())) {
            addComplexField(context, classBuilder, relativPath, child);
        }
        if (!CollectionUtils.isEmpty(filtersNodes)) {
            filtersNodes.forEach(n -> context.unFilteredNodeDeque.poll());
        }
    }
    for (List<WebTemplateNode> choice : choices.values()) {
        WebTemplateNode node = choice.get(0);
        WebTemplateNode relativeNode = buildRelativeNode(context, node);
        Deque<WebTemplateNode> filtersNodes = pushToUnfiltered(context, node);
        TypeSpec interfaceSpec;
        TypeName interfaceClassName;
        if (context.currentTypeSpec.containsKey(relativeNode)) {
            interfaceSpec = context.currentTypeSpec.get(relativeNode);
            String interfacePackage = context.currentPackageName + "." + context.currentMainClass.toLowerCase() + DEFINITION_PACKAGE;
            context.classes.put(interfacePackage, interfaceSpec);
            interfaceClassName = ClassName.get(interfacePackage, interfaceSpec.name);
        } else {
            List<Pair<TypeSpec.Builder, WebTemplateNode>> builders = new ArrayList<>();
            for (WebTemplateNode child : choice) {
                TypeSpec.Builder build = build(context, child);
                builders.add(new ImmutablePair<>(build, child));
            }
            TypeSpec.Builder interfaceBuilder = TypeSpec.interfaceBuilder(defaultNamingStrategy.buildClassName(context, choice.get(0), true, false)).addModifiers(Modifier.PUBLIC);
            interfaceBuilder.addAnnotation(buildGeneratedAnnotation());
            Set<FieldSpec> cowmenField = null;
            for (Set<FieldSpec> fields : builders.stream().map(Pair::getLeft).map(s -> s.fieldSpecs).map(HashSet::new).collect(Collectors.toList())) {
                if (cowmenField == null) {
                    cowmenField = fields;
                } else {
                    cowmenField = SetUtils.intersection(cowmenField, fields);
                }
            }
            if (cowmenField == null) {
                cowmenField = Collections.emptySet();
            }
            cowmenField.forEach(f -> {
                interfaceBuilder.addMethod(buildGetter(f, true));
                interfaceBuilder.addMethod(buildSetter(f, true));
            });
            interfaceSpec = interfaceBuilder.build();
            context.currentTypeSpec.put(relativeNode, interfaceSpec);
            String interfacePackage = context.currentPackageName + "." + context.currentMainClass.toLowerCase() + DEFINITION_PACKAGE;
            context.classes.put(interfacePackage, interfaceSpec);
            interfaceClassName = ClassName.get(interfacePackage, interfaceSpec.name);
            TypeName finalInterfaceClassName = interfaceClassName;
            builders.forEach(pair -> {
                TypeSpec.Builder builder = pair.getKey().addSuperinterface(finalInterfaceClassName).addAnnotation(AnnotationSpec.builder(OptionFor.class).addMember(OptionFor.VALUE, "$S", pair.getRight().getRmType()).build());
                context.classes.put(interfacePackage, builder.build());
            });
        }
        if (choice.stream().anyMatch(WebTemplateNode::isMulti)) {
            interfaceClassName = ParameterizedTypeName.get(ClassName.get(List.class), interfaceClassName);
        }
        String relativPath = FlatPath.removeStart(new FlatPath(node.getAqlPath()), new FlatPath(next.getAqlPath())).toString();
        addField(context, classBuilder, relativPath, node, interfaceClassName, new ValueSet(ValueSet.LOCAL, ValueSet.LOCAL, Collections.emptySet()), true);
        if (!CollectionUtils.isEmpty(filtersNodes)) {
            filtersNodes.forEach(n -> context.unFilteredNodeDeque.poll());
        }
    }
    if (next.isArchetype()) {
        context.currentArchetypeName.poll();
    }
    if (children.isEmpty() && choices.isEmpty()) {
        addSimpleField(context, classBuilder, "", next);
    }
    context.currentFieldNameMap.poll();
    context.nodeDeque.poll();
    context.unFilteredNodeDeque.poll();
    return classBuilder;
}
Also used : Arrays(java.util.Arrays) Language(org.ehrbase.client.classgenerator.shareddefinition.Language) Composition(com.nedap.archie.rm.composition.Composition) Modifier(javax.lang.model.element.Modifier) MathFunction(org.ehrbase.client.classgenerator.shareddefinition.MathFunction) VersionUid(org.ehrbase.client.openehrclient.VersionUid) ArchieRMInfoLookup(com.nedap.archie.rminfo.ArchieRMInfoLookup) LoggerFactory(org.slf4j.LoggerFactory) ClassName(com.squareup.javapoet.ClassName) StringUtils(org.apache.commons.lang3.StringUtils) Pair(org.apache.commons.lang3.tuple.Pair) Generated(javax.annotation.processing.Generated) Map(java.util.Map) Category(org.ehrbase.client.classgenerator.shareddefinition.Category) Locatable(com.nedap.archie.rm.archetyped.Locatable) Transition(org.ehrbase.client.classgenerator.shareddefinition.Transition) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) RMEntity(org.ehrbase.client.classgenerator.interfaces.RMEntity) Path(org.ehrbase.client.annotations.Path) Entry(com.nedap.archie.rm.composition.Entry) ReflectionHelper(org.ehrbase.util.reflection.ReflectionHelper) IntervalEventEntity(org.ehrbase.client.classgenerator.interfaces.IntervalEventEntity) FilteredWebTemplate(org.ehrbase.webtemplate.model.FilteredWebTemplate) WebTemplateInput(org.ehrbase.webtemplate.model.WebTemplateInput) Set(java.util.Set) Collectors(java.util.stream.Collectors) Template(org.ehrbase.client.annotations.Template) WebTemplateInputValue(org.ehrbase.webtemplate.model.WebTemplateInputValue) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Type(java.lang.reflect.Type) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) TermDefinition(org.ehrbase.terminology.client.terminology.TermDefinition) Territory(org.ehrbase.client.classgenerator.shareddefinition.Territory) FieldSpec(com.squareup.javapoet.FieldSpec) Id(org.ehrbase.client.annotations.Id) HashMap(java.util.HashMap) Deque(java.util.Deque) CompositionEntity(org.ehrbase.client.classgenerator.interfaces.CompositionEntity) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PointEventEntity(org.ehrbase.client.classgenerator.interfaces.PointEventEntity) WebTemplateFilter(org.ehrbase.webtemplate.filter.WebTemplateFilter) SetUtils(org.apache.commons.collections4.SetUtils) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) OptionFor(org.ehrbase.client.annotations.OptionFor) EntryEntity(org.ehrbase.client.classgenerator.interfaces.EntryEntity) Logger(org.slf4j.Logger) MethodSpec(com.squareup.javapoet.MethodSpec) Walker(org.ehrbase.serialisation.walker.Walker) Archetype(org.ehrbase.client.annotations.Archetype) NullFlavour(org.ehrbase.client.classgenerator.shareddefinition.NullFlavour) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) Field(java.lang.reflect.Field) PointEvent(com.nedap.archie.rm.datastructures.PointEvent) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) RmClassGeneratorConfig(org.ehrbase.client.classgenerator.config.RmClassGeneratorConfig) WebTemplate(org.ehrbase.webtemplate.model.WebTemplate) SnakeCase(org.ehrbase.serialisation.util.SnakeCase) FlatPath(org.ehrbase.webtemplate.parser.FlatPath) LocatableEntity(org.ehrbase.client.classgenerator.interfaces.LocatableEntity) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) Entity(org.ehrbase.client.annotations.Entity) Choice(org.ehrbase.client.annotations.Choice) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) IntervalEvent(com.nedap.archie.rm.datastructures.IntervalEvent) Collections(java.util.Collections) Setting(org.ehrbase.client.classgenerator.shareddefinition.Setting) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) OptionFor(org.ehrbase.client.annotations.OptionFor) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) ArrayList(java.util.ArrayList) Walker(org.ehrbase.serialisation.walker.Walker) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) FieldSpec(com.squareup.javapoet.FieldSpec) List(java.util.List) ArrayList(java.util.ArrayList) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) TypeSpec(com.squareup.javapoet.TypeSpec) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) FlatPath(org.ehrbase.webtemplate.parser.FlatPath)

Example 3 with ValueSet

use of org.ehrbase.terminology.client.terminology.ValueSet in project openEHR_SDK by ehrbase.

the class OPTParser method parseCDOMAINTYPE.

private WebTemplateNode parseCDOMAINTYPE(CDOMAINTYPE cdomaintype, String aqlPath, Map<String, Map<String, TermDefinition>> termDefinitionMap, String rmAttributeName) {
    WebTemplateNode node = buildNode(cdomaintype, rmAttributeName, termDefinitionMap, null);
    node.setAqlPath(aqlPath);
    if (cdomaintype instanceof CDVSTATE) {
        throw new SdkException(String.format("Unexpected class: %s", cdomaintype.getClass().getSimpleName()));
    } else if (cdomaintype instanceof CDVQUANTITY) {
        WebTemplateInput magnitude = new WebTemplateInput();
        magnitude.setSuffix("magnitude");
        magnitude.setType("DECIMAL");
        Optional.of((CDVQUANTITY) cdomaintype).map(CDVQUANTITY::getAssumedValue).map(DVQUANTITY::getMagnitude).map(d -> Double.toString(d)).ifPresent(magnitude::setDefaultValue);
        inputHandler.findDefaultValue(node, "magnitude").ifPresent(magnitude::setDefaultValue);
        node.getInputs().add(magnitude);
        WebTemplateInput unit = new WebTemplateInput();
        unit.setSuffix("unit");
        unit.setType(CODED_TEXT);
        Optional.of((CDVQUANTITY) cdomaintype).map(CDVQUANTITY::getAssumedValue).map(DVQUANTITY::getUnits).ifPresent(unit::setDefaultValue);
        inputHandler.findDefaultValue(node, "units").ifPresent(unit::setDefaultValue);
        node.getInputs().add(unit);
        Arrays.stream(((CDVQUANTITY) cdomaintype).getListArray()).forEach(o -> {
            WebTemplateInputValue value = new WebTemplateInputValue();
            value.setLabel(o.getUnits());
            value.setValue(o.getUnits());
            WebTemplateValidation validation = new WebTemplateValidation();
            boolean addValidation = false;
            if (o.getMagnitude() != null) {
                addValidation = true;
                validation.setRange(inputHandler.extractInterval(o.getMagnitude()));
            }
            if (o.getPrecision() != null) {
                addValidation = true;
                validation.setPrecision(inputHandler.extractInterval(o.getPrecision()));
            }
            if (addValidation) {
                value.setValidation(validation);
            }
            value.getLocalizedLabels().putAll(termDefinitionMap.getOrDefault(o.getUnits(), Collections.emptyMap()).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getValue())));
            unit.getList().add(value);
        });
        if (unit.getList().size() == 1) {
            magnitude.setValidation(unit.getList().get(0).getValidation());
        }
    } else if (cdomaintype instanceof CDVORDINAL) {
        WebTemplateInput code = new WebTemplateInput();
        inputHandler.findDefaultValue(node, "defining_code").ifPresent(code::setDefaultValue);
        code.setType(CODED_TEXT);
        node.getInputs().add(code);
        Optional.of((CDVORDINAL) cdomaintype).map(CDVORDINAL::getAssumedValue).map(DVORDINAL::getSymbol).map(DVCODEDTEXT::getDefiningCode).map(CODEPHRASE::getCodeString).ifPresent(code::setDefaultValue);
        Arrays.stream(((CDVORDINAL) cdomaintype).getListArray()).forEach(o -> {
            WebTemplateInputValue value = new WebTemplateInputValue();
            value.setOrdinal(o.getValue());
            value.setValue(o.getSymbol().getDefiningCode().getCodeString());
            value.getLocalizedLabels().putAll(Optional.ofNullable(termDefinitionMap.get(value.getValue())).map(Map::entrySet).stream().flatMap(Set::stream).collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getValue())));
            value.getLocalizedDescriptions().putAll(Optional.ofNullable(termDefinitionMap.get(value.getValue())).map(Map::entrySet).stream().flatMap(Set::stream).collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getDescription())));
            value.setLabel(value.getLocalizedLabels().get(defaultLanguage));
            if (!value.getLocalizedDescriptions().containsKey(defaultLanguage)) {
                value.getLocalizedDescriptions().put(defaultLanguage, "");
            }
            code.getList().add(value);
        });
    } else if (cdomaintype instanceof CCODEPHRASE) {
        WebTemplateInput code = new WebTemplateInput();
        inputHandler.findDefaultValue(node, "defining_code").ifPresent(code::setDefaultValue);
        code.setSuffix("code");
        node.getInputs().add(code);
        Optional.of((CCODEPHRASE) cdomaintype).map(CCODEPHRASE::getAssumedValue).map(CODEPHRASE::getCodeString).ifPresent(code::setDefaultValue);
        if (cdomaintype instanceof CCODEREFERENCE) {
            code.setTerminology(Optional.of((CCODEREFERENCE) cdomaintype).map(CCODEREFERENCE::getReferenceSetUri).map(s -> StringUtils.removeStart(s, "terminology:")).orElse(null));
        } else {
            code.setTerminology(Optional.of((CCODEPHRASE) cdomaintype).map(CCODEPHRASE::getTerminologyId).map(OBJECTID::getValue).orElse(null));
        }
        if (code.getTerminology().equals(OPENEHR)) {
            ValueSet valueSet = TerminologyProvider.findOpenEhrValueSet(code.getTerminology(), ((CCODEPHRASE) cdomaintype).getCodeListArray(), defaultLanguage);
            Map<String, ValueSet> collect = languages.stream().collect(Collectors.toMap(Function.identity(), l -> TerminologyProvider.findOpenEhrValueSet(code.getTerminology(), ((CCODEPHRASE) cdomaintype).getCodeListArray(), l)));
            valueSet.getTherms().forEach(t -> {
                WebTemplateInputValue value = new WebTemplateInputValue();
                value.setValue(t.getCode());
                value.setLabel(t.getValue());
                value.getLocalizedLabels().putAll(collect.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getTherms().stream().filter(x -> x.getCode().equals(t.getCode())).findAny().map(TermDefinition::getValue).orElse(""))));
                code.getList().add(value);
            });
        } else {
            Arrays.stream(((CCODEPHRASE) cdomaintype).getCodeListArray()).map(o -> StringUtils.isBlank(code.getTerminology()) || "local".equals(code.getTerminology()) ? o : code.getTerminology() + "::" + o).forEach(o -> {
                WebTemplateInputValue value = new WebTemplateInputValue();
                Optional<TermDefinition> termDefinition = Optional.ofNullable(termDefinitionMap.get(o)).map(e -> e.get(defaultLanguage));
                if (termDefinition.isEmpty()) {
                    o = o.replace(code.getTerminology() + "::", "");
                    termDefinition = Optional.ofNullable(termDefinitionMap.get(o)).map(e -> e.get(defaultLanguage));
                }
                if (termDefinition.isPresent()) {
                    value.setValue(termDefinition.get().getCode());
                    if (StringUtils.isNotBlank((code.getTerminology()))) {
                        value.setValue(value.getValue().replace(code.getTerminology() + "::", ""));
                    }
                    value.setLabel(termDefinition.get().getValue());
                    value.getLocalizedLabels().putAll(termDefinitionMap.getOrDefault(o, Collections.emptyMap()).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getValue())));
                    value.getLocalizedDescriptions().putAll(termDefinitionMap.getOrDefault(o, Collections.emptyMap()).entrySet().stream().filter(e -> StringUtils.isNotBlank(e.getValue().getDescription())).collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getDescription())));
                    if (!value.getLocalizedDescriptions().containsKey(defaultLanguage)) {
                        value.getLocalizedDescriptions().put(defaultLanguage, "");
                    }
                    code.getList().add(value);
                }
            });
        }
        if (code.getList().isEmpty()) {
            code.setType("TEXT");
            WebTemplateInput value = inputHandler.buildWebTemplateInput("value", "TEXT");
            value.setTerminology(code.getTerminology());
            node.getInputs().add(value);
        } else {
            code.setType(CODED_TEXT);
        }
    } else {
        throw new SdkException(String.format("Unexpected class: %s", cdomaintype.getClass().getSimpleName()));
    }
    return node;
}
Also used : java.util(java.util) TermDefinition(org.ehrbase.terminology.client.terminology.TermDefinition) org.ehrbase.webtemplate.model(org.ehrbase.webtemplate.model) History(com.nedap.archie.rm.datastructures.History) XmlCursor(org.apache.xmlbeans.XmlCursor) SdkException(org.ehrbase.util.exception.SdkException) ArchieRMInfoLookup(com.nedap.archie.rminfo.ArchieRMInfoLookup) Element(com.nedap.archie.rm.datastructures.Element) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) com.nedap.archie.rm.composition(com.nedap.archie.rm.composition) org.openehr.schemas.v1(org.openehr.schemas.v1) Pair(org.apache.commons.lang3.tuple.Pair) Node(org.w3c.dom.Node) RmConstants(org.ehrbase.util.rmconstants.RmConstants) Locatable(com.nedap.archie.rm.archetyped.Locatable) XmlObject(org.apache.xmlbeans.XmlObject) NodeList(org.w3c.dom.NodeList) XmlTokenSource(org.apache.xmlbeans.XmlTokenSource) TerminologyProvider(org.ehrbase.terminology.client.terminology.TerminologyProvider) RMTypeInfo(com.nedap.archie.rminfo.RMTypeInfo) DvInterval(com.nedap.archie.rm.datavalues.quantity.DvInterval) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) RMAttributeInfo(com.nedap.archie.rminfo.RMAttributeInfo) WebTemplateUtils(org.ehrbase.webtemplate.util.WebTemplateUtils) Event(com.nedap.archie.rm.datastructures.Event) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) SdkException(org.ehrbase.util.exception.SdkException) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet)

Example 4 with ValueSet

use of org.ehrbase.terminology.client.terminology.ValueSet in project openEHR_SDK by ehrbase.

the class ClassGenerator method addComplexField.

private void addComplexField(ClassGeneratorContext context, TypeSpec.Builder classBuilder, String path, WebTemplateNode node) {
    final TypeSpec subSpec;
    WebTemplateNode relativeNode = buildRelativeNode(context, node);
    if (context.currentTypeSpec.containsKey(relativeNode)) {
        subSpec = context.currentTypeSpec.get(relativeNode);
    } else {
        subSpec = build(context, node).build();
        context.currentTypeSpec.put(relativeNode, subSpec);
    }
    String subSpecPackage = context.currentPackageName + "." + context.currentMainClass.toLowerCase() + DEFINITION_PACKAGE;
    context.classes.put(subSpecPackage, subSpec);
    TypeName className = ClassName.get(subSpecPackage, subSpec.name);
    if (node.isMulti()) {
        className = ParameterizedTypeName.get(ClassName.get(List.class), className);
    }
    addField(context, classBuilder, path, node, className, new ValueSet(ValueSet.LOCAL, ValueSet.LOCAL, Collections.emptySet()), false);
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 5 with ValueSet

use of org.ehrbase.terminology.client.terminology.ValueSet in project openEHR_SDK by ehrbase.

the class ClassGenerator method addField.

private void addField(ClassGeneratorContext context, TypeSpec.Builder classBuilder, String path, WebTemplateNode node, TypeName className, ValueSet valueSet, boolean addChoiceAnnotation) {
    if (CodePhrase.class.getName().equals(className.toString()))
        switch(node.getName()) {
            case "language":
                className = ClassName.get(Language.class);
                break;
            case "setting":
                className = ClassName.get(Setting.class);
                break;
            case "category":
                className = ClassName.get(Category.class);
                break;
            case "territory":
                className = ClassName.get(Territory.class);
                break;
            case "math_function":
                className = ClassName.get(MathFunction.class);
                break;
            case "transition":
                className = ClassName.get(Transition.class);
                break;
            case "null_flavour":
                className = ClassName.get(NullFlavour.class);
                break;
            default:
                if (CollectionUtils.isNotEmpty(valueSet.getTherms())) {
                    final TypeSpec enumValueSet = context.currentEnums.computeIfAbsent(valueSet, vs -> buildEnumValueSet(context, node, vs));
                    String enumPackage = context.currentPackageName + "." + context.currentMainClass.toLowerCase() + DEFINITION_PACKAGE;
                    context.classes.put(enumPackage, enumValueSet);
                    className = ClassName.get(enumPackage, enumValueSet.name);
                }
        }
    String fieldName = defaultNamingStrategy.buildFieldName(context, path, node);
    FieldSpec.Builder builder = FieldSpec.builder(className, fieldName).addAnnotation(AnnotationSpec.builder(Path.class).addMember(Path.VALUE, "$S", path).build()).addModifiers(Modifier.PRIVATE).addJavadoc(defaultNamingStrategy.buildFieldJavadoc(context, node));
    if (addChoiceAnnotation) {
        builder.addAnnotation(Choice.class);
    }
    FieldSpec fieldSpec = builder.build();
    classBuilder.addField(fieldSpec);
    classBuilder.addMethod(buildSetter(fieldSpec, false));
    classBuilder.addMethod(buildGetter(fieldSpec, false));
}
Also used : Arrays(java.util.Arrays) Language(org.ehrbase.client.classgenerator.shareddefinition.Language) Composition(com.nedap.archie.rm.composition.Composition) Modifier(javax.lang.model.element.Modifier) MathFunction(org.ehrbase.client.classgenerator.shareddefinition.MathFunction) VersionUid(org.ehrbase.client.openehrclient.VersionUid) ArchieRMInfoLookup(com.nedap.archie.rminfo.ArchieRMInfoLookup) LoggerFactory(org.slf4j.LoggerFactory) ClassName(com.squareup.javapoet.ClassName) StringUtils(org.apache.commons.lang3.StringUtils) Pair(org.apache.commons.lang3.tuple.Pair) Generated(javax.annotation.processing.Generated) Map(java.util.Map) Category(org.ehrbase.client.classgenerator.shareddefinition.Category) Locatable(com.nedap.archie.rm.archetyped.Locatable) Transition(org.ehrbase.client.classgenerator.shareddefinition.Transition) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) RMEntity(org.ehrbase.client.classgenerator.interfaces.RMEntity) Path(org.ehrbase.client.annotations.Path) Entry(com.nedap.archie.rm.composition.Entry) ReflectionHelper(org.ehrbase.util.reflection.ReflectionHelper) IntervalEventEntity(org.ehrbase.client.classgenerator.interfaces.IntervalEventEntity) FilteredWebTemplate(org.ehrbase.webtemplate.model.FilteredWebTemplate) WebTemplateInput(org.ehrbase.webtemplate.model.WebTemplateInput) Set(java.util.Set) Collectors(java.util.stream.Collectors) Template(org.ehrbase.client.annotations.Template) WebTemplateInputValue(org.ehrbase.webtemplate.model.WebTemplateInputValue) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Type(java.lang.reflect.Type) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) TermDefinition(org.ehrbase.terminology.client.terminology.TermDefinition) Territory(org.ehrbase.client.classgenerator.shareddefinition.Territory) FieldSpec(com.squareup.javapoet.FieldSpec) Id(org.ehrbase.client.annotations.Id) HashMap(java.util.HashMap) Deque(java.util.Deque) CompositionEntity(org.ehrbase.client.classgenerator.interfaces.CompositionEntity) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PointEventEntity(org.ehrbase.client.classgenerator.interfaces.PointEventEntity) WebTemplateFilter(org.ehrbase.webtemplate.filter.WebTemplateFilter) SetUtils(org.apache.commons.collections4.SetUtils) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) OptionFor(org.ehrbase.client.annotations.OptionFor) EntryEntity(org.ehrbase.client.classgenerator.interfaces.EntryEntity) Logger(org.slf4j.Logger) MethodSpec(com.squareup.javapoet.MethodSpec) Walker(org.ehrbase.serialisation.walker.Walker) Archetype(org.ehrbase.client.annotations.Archetype) NullFlavour(org.ehrbase.client.classgenerator.shareddefinition.NullFlavour) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) Field(java.lang.reflect.Field) PointEvent(com.nedap.archie.rm.datastructures.PointEvent) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) RmClassGeneratorConfig(org.ehrbase.client.classgenerator.config.RmClassGeneratorConfig) WebTemplate(org.ehrbase.webtemplate.model.WebTemplate) SnakeCase(org.ehrbase.serialisation.util.SnakeCase) FlatPath(org.ehrbase.webtemplate.parser.FlatPath) LocatableEntity(org.ehrbase.client.classgenerator.interfaces.LocatableEntity) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) Entity(org.ehrbase.client.annotations.Entity) Choice(org.ehrbase.client.annotations.Choice) ValueSet(org.ehrbase.terminology.client.terminology.ValueSet) IntervalEvent(com.nedap.archie.rm.datastructures.IntervalEvent) Collections(java.util.Collections) Setting(org.ehrbase.client.classgenerator.shareddefinition.Setting) Path(org.ehrbase.client.annotations.Path) FlatPath(org.ehrbase.webtemplate.parser.FlatPath) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ValueSet (org.ehrbase.terminology.client.terminology.ValueSet)5 Locatable (com.nedap.archie.rm.archetyped.Locatable)4 ArchieRMInfoLookup (com.nedap.archie.rminfo.ArchieRMInfoLookup)4 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)4 TypeName (com.squareup.javapoet.TypeName)4 TypeSpec (com.squareup.javapoet.TypeSpec)4 Collectors (java.util.stream.Collectors)4 StringUtils (org.apache.commons.lang3.StringUtils)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 Pair (org.apache.commons.lang3.tuple.Pair)4 Composition (com.nedap.archie.rm.composition.Composition)3 Entry (com.nedap.archie.rm.composition.Entry)3 IntervalEvent (com.nedap.archie.rm.datastructures.IntervalEvent)3 PointEvent (com.nedap.archie.rm.datastructures.PointEvent)3 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)3 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)3 ClassName (com.squareup.javapoet.ClassName)3 FieldSpec (com.squareup.javapoet.FieldSpec)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 Field (java.lang.reflect.Field)3