Search in sources :

Example 41 with CodePhrase

use of com.nedap.archie.rm.datatypes.CodePhrase in project openEHR_SDK by ehrbase.

the class SimpleTerminologyAccess method addGroup.

/**
 * Adds a group of codes with language dependent names
 *
 * @param groupId not null
 * @param codes   not null
 * @param names   <lang, name>  not null
 */
void addGroup(String groupId, Collection<String> codes, Map<String, String> names) {
    Set<CodePhrase> group = new HashSet<>();
    for (String c : codes) {
        CodePhrase code = new CodePhrase(new TerminologyId(id), c);
        group.add(code);
    }
    groups.put(groupId, group);
    for (Map.Entry<String, String> entry : names.entrySet()) {
        String lang = entry.getKey();
        Map<String, String> nameToId = groupLangNameToId.get(lang);
        if (nameToId == null) {
            nameToId = new HashMap<>();
        }
        String name = entry.getValue();
        nameToId.put(name, groupId);
        groupLangNameToId.put(lang, nameToId);
    }
}
Also used : TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase)

Example 42 with CodePhrase

use of com.nedap.archie.rm.datatypes.CodePhrase in project openEHR_SDK by ehrbase.

the class DvCodedTextAdapter method write.

@Override
public void write(JsonWriter writer, DvCodedText dvalue) throws IOException {
    if (!Optional.ofNullable(dvalue).map(DvCodedText::getDefiningCode).map(CodePhrase::getCodeString).isPresent()) {
        writer.nullValue();
        return;
    }
    TermMappingAdapter termMappingAdapter = new TermMappingAdapter();
    if (adapterType == I_DvTypeAdapter.AdapterType.PG_JSONB) {
        writer.beginObject();
        writer.name(VALUE).value(dvalue.getValue());
        writer.name(I_DvTypeAdapter.TAG_CLASS_RAW_JSON).value(new SnakeCase(DvCodedText.class.getSimpleName()).camelToUpperSnake());
        writer.name("definingCode");
        writer.beginObject();
        writer.name("codeString").value(dvalue.getDefiningCode().getCodeString());
        writer.name("terminologyId");
        writer.beginObject();
        writer.name(VALUE).value(dvalue.getDefiningCode().getTerminologyId().getValue());
        writer.name(I_DvTypeAdapter.TAG_CLASS_RAW_JSON).value(new SnakeCase(TerminologyId.class.getSimpleName()).camelToUpperSnake());
        writer.endObject();
        writer.name(I_DvTypeAdapter.TAG_CLASS_RAW_JSON).value(new SnakeCase(CodePhrase.class.getSimpleName()).camelToUpperSnake());
        writer.endObject();
        termMappingAdapter.write(writer, dvalue.getMappings());
        writer.endObject();
    } else if (adapterType == I_DvTypeAdapter.AdapterType.RAW_JSON) {
        writer.beginObject();
        writer.name(I_DvTypeAdapter.TAG_CLASS_RAW_JSON).value(new ObjectSnakeCase(dvalue).camelToUpperSnake());
        writer.name(VALUE).value(dvalue.getValue());
        CodePhrase codePhrase = dvalue.getDefiningCode();
        writer.name("defining_code").value(gson.toJson(codePhrase));
        writer.name(I_DvTypeAdapter.TAG_CLASS_RAW_JSON).value(new SnakeCase(CodePhrase.class.getSimpleName()).camelToUpperSnake());
        writer.endObject();
    }
}
Also used : TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) SnakeCase(org.ehrbase.serialisation.util.SnakeCase) ObjectSnakeCase(org.ehrbase.serialisation.util.ObjectSnakeCase) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) ObjectSnakeCase(org.ehrbase.serialisation.util.ObjectSnakeCase)

Example 43 with CodePhrase

use of com.nedap.archie.rm.datatypes.CodePhrase in project openEHR_SDK by ehrbase.

the class FeederAuditDetailsAttributesTest method toMap.

@Test
public void toMap() throws Exception {
    String value = IOUtils.toString(CompositionTestDataCanonicalJson.FEEDER_AUDIT_DETAILS.getStream(), UTF_8);
    CanonicalJson cut = new CanonicalJson();
    Composition composition = cut.unmarshal(value, Composition.class);
    assertNotNull(composition);
    assertNotNull(composition.getFeederAudit().getFeederSystemAudit().getOtherDetails());
    // with real data
    FeederAuditDetailsAttributes attributes = new FeederAuditDetailsAttributes(composition.getFeederAudit().getFeederSystemAudit());
    Map<String, Object> map = attributes.toMap();
    assertNotNull(map);
    assertNotNull(map.get("other_details[openEHR-EHR-ITEM_TREE.generic.v1]"));
    // valid fabricated data without brackets
    FeederAuditDetails details = new FeederAuditDetails();
    ItemSingle single = new ItemSingle("test", new DvCodedText("text", new CodePhrase("string")), new Element("node", new DvText("name"), null));
    details.setOtherDetails(single);
    details.setSystemId("system");
    attributes = new FeederAuditDetailsAttributes(details);
    map = attributes.toMap();
    assertNotNull(map.get("other_details[test]"));
    // valid fabricated data with brackets
    details = new FeederAuditDetails();
    single = new ItemSingle("[test]", new DvCodedText("text", new CodePhrase("string")), new Element("node", new DvText("name"), null));
    details.setOtherDetails(single);
    details.setSystemId("system");
    attributes = new FeederAuditDetailsAttributes(details);
    map = attributes.toMap();
    assertNotNull(map.get("other_details[test]"));
    // invalid data with one [
    details = new FeederAuditDetails();
    single = new ItemSingle("[test", new DvCodedText("text", new CodePhrase("string")), new Element("node", new DvText("name"), null));
    details.setOtherDetails(single);
    details.setSystemId("system");
    attributes = new FeederAuditDetailsAttributes(details);
    assertThrows(IllegalArgumentException.class, attributes::toMap);
    // invalid data with one ]
    details = new FeederAuditDetails();
    single = new ItemSingle("test]", new DvCodedText("text", new CodePhrase("string")), new Element("node", new DvText("name"), null));
    details.setOtherDetails(single);
    details.setSystemId("system");
    attributes = new FeederAuditDetailsAttributes(details);
    assertThrows(IllegalArgumentException.class, attributes::toMap);
}
Also used : CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) CompositionTestDataCanonicalJson(org.ehrbase.test_data.composition.CompositionTestDataCanonicalJson) Composition(com.nedap.archie.rm.composition.Composition) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) Element(com.nedap.archie.rm.datastructures.Element) DvText(com.nedap.archie.rm.datavalues.DvText) FeederAuditDetails(com.nedap.archie.rm.archetyped.FeederAuditDetails) ItemSingle(com.nedap.archie.rm.datastructures.ItemSingle) Test(org.junit.Test)

Example 44 with CodePhrase

use of com.nedap.archie.rm.datatypes.CodePhrase in project openEHR_SDK by ehrbase.

the class DtoFromCompositionWalker method writeField.

private void writeField(Field field, Object dto, Object value) {
    try {
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), dto.getClass());
        Object dtoList = value;
        if (EnumValueSet.class.isAssignableFrom(field.getType()) && value != null && CodePhrase.class.isAssignableFrom(value.getClass())) {
            CodePhrase codePhrase = (CodePhrase) value;
            EnumValueSet enumValueSet = Arrays.stream(field.getType().getEnumConstants()).map(o -> (EnumValueSet) o).filter(v -> {
                String terminologyId = Optional.ofNullable(codePhrase.getTerminologyId()).map(ObjectId::getValue).orElse(null);
                return v.getTerminologyId().equals(terminologyId);
            }).filter(v -> v.getCode().equals(codePhrase.getCodeString())).findAny().orElse(null);
            dtoList = enumValueSet;
        }
        if (dtoList instanceof RmPrimitive) {
            dtoList = ((RmPrimitive<?>) dtoList).getValue();
        }
        if (List.class.isAssignableFrom(field.getType())) {
            dtoList = propertyDescriptor.getReadMethod().invoke(dto);
            if (dtoList == null) {
                dtoList = new ArrayList<>();
            }
            ((List) dtoList).add(value);
        }
        propertyDescriptor.getWriteMethod().invoke(dto, dtoList);
    } catch (IllegalAccessException | InvocationTargetException | IntrospectionException | IllegalArgumentException e) {
        throw new ClientException(e.getMessage(), e);
    }
}
Also used : java.util(java.util) SdkException(org.ehrbase.util.exception.SdkException) LoggerFactory(org.slf4j.LoggerFactory) TypeToken(com.google.common.reflect.TypeToken) ObjectId(com.nedap.archie.rm.support.identification.ObjectId) FromCompositionWalker(org.ehrbase.serialisation.walker.FromCompositionWalker) StringUtils(org.apache.commons.lang3.StringUtils) EnumValueSet(org.ehrbase.client.classgenerator.EnumValueSet) Function(java.util.function.Function) RmPrimitive(org.ehrbase.serialisation.walker.RmPrimitive) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) ClientException(org.ehrbase.client.exception.ClientException) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) OptionFor(org.ehrbase.client.annotations.OptionFor) Path(org.ehrbase.client.annotations.Path) Context(org.ehrbase.serialisation.walker.Context) ReflectionHelper(org.ehrbase.util.reflection.ReflectionHelper) Logger(org.slf4j.Logger) CaseUtils(org.apache.commons.text.CaseUtils) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FlatPath(org.ehrbase.webtemplate.parser.FlatPath) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) PropertyDescriptor(java.beans.PropertyDescriptor) RMObject(com.nedap.archie.rm.RMObject) Entity(org.ehrbase.client.annotations.Entity) WebTemplateNode(org.ehrbase.webtemplate.model.WebTemplateNode) PropertyDescriptor(java.beans.PropertyDescriptor) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) ObjectId(com.nedap.archie.rm.support.identification.ObjectId) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EnumValueSet(org.ehrbase.client.classgenerator.EnumValueSet) RmPrimitive(org.ehrbase.serialisation.walker.RmPrimitive) RMObject(com.nedap.archie.rm.RMObject) ClientException(org.ehrbase.client.exception.ClientException)

Example 45 with CodePhrase

use of com.nedap.archie.rm.datatypes.CodePhrase in project openEHR_SDK by ehrbase.

the class DtoToCompositionWalker method handleSingleValue.

private void handleSingleValue(Object value, String childName, Object child, Object parent) {
    if (value == null) {
    // NOP
    } else if (EnumValueSet.class.isAssignableFrom(value.getClass()) && DvCodedText.class.isAssignableFrom(parent.getClass())) {
        EnumValueSet valueSet = (EnumValueSet) value;
        DvCodedText dvCodedText = (DvCodedText) parent;
        dvCodedText.setValue(valueSet.getValue());
        dvCodedText.setDefiningCode(new CodePhrase(new TerminologyId(valueSet.getTerminologyId()), valueSet.getCode()));
    } else if (EnumValueSet.class.isAssignableFrom(value.getClass()) && DvCodedText.class.isAssignableFrom(ARCHIE_RM_INFO_LOOKUP.getAttributeInfo(parent.getClass(), childName).getType())) {
        EnumValueSet valueSet = (EnumValueSet) value;
        DvCodedText dvCodedText = new DvCodedText();
        dvCodedText.setValue(valueSet.getValue());
        dvCodedText.setDefiningCode(new CodePhrase(new TerminologyId(valueSet.getTerminologyId()), valueSet.getCode()));
        RM_OBJECT_CREATOR.set(parent, childName, Collections.singletonList(dvCodedText));
    } else if (EnumValueSet.class.isAssignableFrom(value.getClass())) {
        EnumValueSet valueSet = (EnumValueSet) value;
        CodePhrase codePhrase = new CodePhrase(new TerminologyId(valueSet.getTerminologyId()), valueSet.getCode());
        RM_OBJECT_CREATOR.set(parent, childName, Collections.singletonList(codePhrase));
    } else if (ARCHIE_RM_INFO_LOOKUP.getAttributeInfo(parent.getClass(), childName).getTypeInCollection().isAssignableFrom(value.getClass()) || (ARCHIE_RM_INFO_LOOKUP.getAttributeInfo(parent.getClass(), childName).getTypeInCollection().isAssignableFrom(boolean.class) && value.getClass().isAssignableFrom(Boolean.class))) {
        RMAttributeInfo attributeInfo = ARCHIE_RM_INFO_LOOKUP.getAttributeInfo(parent.getClass(), childName);
        if (attributeInfo.isMultipleValued()) {
            try {
                Object invoke = attributeInfo.getGetMethod().invoke(parent);
                if (Collection.class.isAssignableFrom(invoke.getClass()) && child != null) {
                    ((Collection) invoke).remove(child);
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                logger.warn(e.getMessage(), e);
            }
        }
        RM_OBJECT_CREATOR.addElementToListOrSetSingleValues(parent, childName, Collections.singletonList(value));
    } else {
        logger.warn("Unhandled child {} in {}", childName, parent);
    }
}
Also used : TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) EnumValueSet(org.ehrbase.client.classgenerator.EnumValueSet) RMAttributeInfo(com.nedap.archie.rminfo.RMAttributeInfo) RMObject(com.nedap.archie.rm.RMObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)71 TerminologyId (com.nedap.archie.rm.support.identification.TerminologyId)60 DvCodedText (com.nedap.archie.rm.datavalues.DvCodedText)56 Test (org.junit.jupiter.api.Test)23 Test (org.junit.Test)16 Composition (com.nedap.archie.rm.composition.Composition)12 DvText (com.nedap.archie.rm.datavalues.DvText)12 ArrayList (java.util.ArrayList)8 AbstractRMObjectValidatorTest (org.ehrbase.validation.webtemplate.AbstractRMObjectValidatorTest)7 RMObject (com.nedap.archie.rm.RMObject)5 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)5 FlatPathDto (org.ehrbase.webtemplate.path.flat.FlatPathDto)5 PartyIdentified (com.nedap.archie.rm.generic.PartyIdentified)4 HierObjectId (com.nedap.archie.rm.support.identification.HierObjectId)4 ObjectVersionId (com.nedap.archie.rm.support.identification.ObjectVersionId)4 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 WebTemplate (org.ehrbase.webtemplate.model.WebTemplate)4 WebTemplateNode (org.ehrbase.webtemplate.model.WebTemplateNode)4 Entry (com.nedap.archie.rm.composition.Entry)3