Search in sources :

Example 6 with RMObject

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

the class DefaultRestAqlEndpoint method extractValue.

private Object extractValue(String valueAsString, Class<?> aClass) throws com.fasterxml.jackson.core.JsonProcessingException {
    Object object;
    if (StringUtils.isBlank(valueAsString) || "null".equals(valueAsString)) {
        object = null;
    } else if (aClass.isAnnotationPresent(Entity.class)) {
        RMObject locatable = AQL_OBJECT_MAPPER.readValue(valueAsString, RMObject.class);
        object = new Flattener(defaultRestClient.getTemplateProvider()).flatten(locatable, aClass);
        if (locatable instanceof Composition) {
            Flattener.addVersion(object, new VersionUid(((Composition) locatable).getUid().getValue()));
        }
    } else if (EnumValueSet.class.isAssignableFrom(aClass)) {
        RMObject rmObject = AQL_OBJECT_MAPPER.readValue(valueAsString, RMObject.class);
        final String codeString;
        if (CodePhrase.class.isAssignableFrom(rmObject.getClass())) {
            codeString = ((CodePhrase) rmObject).getCodeString();
        } else {
            codeString = ((DvCodedText) rmObject).getDefiningCode().getCodeString();
        }
        object = Arrays.stream(aClass.getEnumConstants()).map(e -> (EnumValueSet) e).filter(e -> e.getCode().equals(codeString)).findAny().orElseThrow(() -> new ClientException(String.format("Unknown code %s for %s", codeString, aClass.getSimpleName())));
    } else {
        object = AQL_OBJECT_MAPPER.readValue(valueAsString, aClass);
    }
    return object;
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) Composition(com.nedap.archie.rm.composition.Composition) VersionUid(org.ehrbase.client.openehrclient.VersionUid) AqlField(org.ehrbase.client.aql.field.AqlField) HashMap(java.util.HashMap) ParameterValue(org.ehrbase.client.aql.parameter.ParameterValue) JsonParser(com.google.gson.JsonParser) StringUtils(org.apache.commons.lang3.StringUtils) EnumValueSet(org.ehrbase.client.classgenerator.EnumValueSet) TemporalAccessor(java.time.temporal.TemporalAccessor) ArrayList(java.util.ArrayList) EntityUtils(org.apache.http.util.EntityUtils) JsonElement(com.google.gson.JsonElement) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) AqlEndpoint(org.ehrbase.client.openehrclient.AqlEndpoint) OBJECT_MAPPER(org.ehrbase.client.openehrclient.defaultrestclient.DefaultRestClient.OBJECT_MAPPER) Map(java.util.Map) Flattener(org.ehrbase.client.flattener.Flattener) QueryResponseData(org.ehrbase.response.openehr.QueryResponseData) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) ClientException(org.ehrbase.client.exception.ClientException) URI(java.net.URI) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) ListSelectAqlField(org.ehrbase.client.aql.field.ListSelectAqlField) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContentType(org.apache.http.entity.ContentType) Version(com.fasterxml.jackson.core.Version) IOException(java.io.IOException) JacksonUtil(org.ehrbase.serialisation.jsonencoding.JacksonUtil) JsonArray(com.google.gson.JsonArray) List(java.util.List) Record(org.ehrbase.client.aql.record.Record) Query(org.ehrbase.client.aql.query.Query) RMObject(com.nedap.archie.rm.RMObject) Entity(org.ehrbase.client.annotations.Entity) HttpResponse(org.apache.http.HttpResponse) RecordImp(org.ehrbase.client.aql.record.RecordImp) Collections(java.util.Collections) Entity(org.ehrbase.client.annotations.Entity) Composition(com.nedap.archie.rm.composition.Composition) VersionUid(org.ehrbase.client.openehrclient.VersionUid) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) Flattener(org.ehrbase.client.flattener.Flattener) JsonObject(com.google.gson.JsonObject) RMObject(com.nedap.archie.rm.RMObject) ClientException(org.ehrbase.client.exception.ClientException) RMObject(com.nedap.archie.rm.RMObject)

Example 7 with RMObject

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

the class CanonicalEhrQuery3IT method testEhrAttributesDrillDown.

@Test
public void testEhrAttributesDrillDown() {
    String rootPath = "e/ehr_status";
    RMObject referenceNode = referenceEhrStatus;
    String[] attributePaths = { "archetype_node_id", "archetype_details", "archetype_details/archetype_id", "archetype_details/archetype_id/value", "archetype_details/template_id", "archetype_details/template_id/value", "subject", "subject/external_ref", "subject/external_ref/id", "subject/external_ref/id/value", "subject/external_ref/id/scheme", "subject/external_ref/namespace", "subject/external_ref/type", "other_details", "other_details/name", "other_details/name/value", "other_details/items[at0001]", "other_details/items[at0001]/archetype_node_id", "other_details/items[at0001]/name", "other_details/items[at0001]/name/value", "other_details/items[at0001]/value", "other_details/items[at0001]/value/id", "other_details/items[at0001]/value/type", "other_details/items[at0001]/value/issuer", "other_details/items[at0001]/value/assigner", "is_queryable", "is_modifiable" };
    for (String attributePath : attributePaths) {
        String aqlSelect = rootPath + "/" + attributePath;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select ");
        stringBuilder.append(aqlSelect);
        stringBuilder.append(" from EHR e[ehr_id/value = $ehr_id]");
        Query<Record1<Map>> query = Query.buildNativeQuery(stringBuilder.toString(), Map.class);
        QueryResponseData result = openEhrClient.aqlEndpoint().executeRaw(query, new ParameterValue("ehr_id", ehrUUID));
        List<Object> objectList = result.getRows().get(0);
        // Mapped object(s) from JSON
        Object actual = valueObject(objectList.get(0));
        if (actual instanceof List) {
            // RMObject(s)
            Object expected = attributeArrayValueAt(referenceNode, attributePath);
            assertThat(toRmObjectList((List<Object>) actual).toArray()).as(aqlSelect).containsExactlyInAnyOrder(((List<?>) expected).toArray());
        } else {
            assertThat(valueObject(objectList.get(0))).as(aqlSelect).isEqualTo(attributeValueAt(referenceNode, attributePath));
        }
    }
}
Also used : ParameterValue(org.ehrbase.client.aql.parameter.ParameterValue) QueryResponseData(org.ehrbase.response.openehr.QueryResponseData) RMObject(com.nedap.archie.rm.RMObject) List(java.util.List) RMObject(com.nedap.archie.rm.RMObject) Record1(org.ehrbase.client.aql.record.Record1)

Example 8 with RMObject

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

the class StdFromCompositionWalker method preHandle.

@Override
protected void preHandle(Context<Map<String, Object>> context) {
    // Handle if at a End-Node
    if (!visitChildren(context.getNodeDeque().peek()) && !context.getFlatHelper().skip(context)) {
        RMObject currentObject = context.getRmObjectDeque().peek();
        StdConfig stdConfig = findStdConfig(currentObject.getClass());
        context.getObjectDeque().peek().putAll(stdConfig.buildChildValues(context.getFlatHelper().buildNamePath(context, true), currentObject, context));
    }
}
Also used : DefaultStdConfig(org.ehrbase.serialisation.flatencoding.std.marshal.config.DefaultStdConfig) StdConfig(org.ehrbase.serialisation.flatencoding.std.marshal.config.StdConfig) RMObject(com.nedap.archie.rm.RMObject)

Example 9 with RMObject

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

the class AbstractsStdConfig method buildChildValues.

@Override
public /**
 * {@inheritDoc}
 */
Map<String, Object> buildChildValues(String currentTerm, T rmObject, Context<Map<String, Object>> context) {
    Map<String, Object> result = new HashMap<>();
    Set<String> expandFields = Optional.ofNullable(configMap.get(rmObject.getClass())).map(RmIntrospectConfig::getNonTemplateFields).orElse(Collections.emptySet());
    if (!expandFields.isEmpty()) {
        if (expandFields.size() == 1 && expandFields.contains("value")) {
            try {
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor("value", rmObject.getClass());
                Object property = propertyDescriptor.getReadMethod().invoke(rmObject);
                result.put(currentTerm, property);
            } catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
                throw new SdkException(e.getMessage(), e);
            }
        } else {
            for (String propertyName : expandFields) {
                try {
                    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, rmObject.getClass());
                    Object property = propertyDescriptor.getReadMethod().invoke(rmObject);
                    result.put(currentTerm + "|" + propertyName, property);
                } catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
                    throw new SdkException(e.getMessage(), e);
                }
            }
        }
    } else {
        result.put(currentTerm, rmObject);
    }
    return result;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) SdkException(org.ehrbase.util.exception.SdkException) IntrospectionException(java.beans.IntrospectionException) RMObject(com.nedap.archie.rm.RMObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with RMObject

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

the class CanonicalUtil method getAttributeValue.

/**
 * resolves an attribute of a RMObject
 * NB. borrowed from CAttribute.java in validation
 *
 * @param obj
 * @param attributePath
 * @return
 */
private static Object getAttributeValue(Object obj, String attributePath) {
    if (obj == null)
        return null;
    Class rmClass = obj.getClass();
    Object value;
    Method getter;
    String getterName;
    if (attributePath.startsWith("is_")) {
        // conventionally, a getter for a boolean uses 'is' as a prefix
        getterName = "is" + new SnakeToCamel(attributePath.substring(3)).convert();
    } else
        getterName = "get" + new SnakeToCamel(attributePath).convert();
    try {
        getter = rmClass.getMethod(getterName);
        value = getter.invoke(obj, null);
    } catch (Exception e) {
        throw new IllegalStateException("unresolved attribute:" + attributePath + " for class:" + rmClass);
    }
    return value;
}
Also used : RMObject(com.nedap.archie.rm.RMObject) Method(java.lang.reflect.Method) SnakeToCamel(org.ehrbase.serialisation.util.SnakeToCamel)

Aggregations

RMObject (com.nedap.archie.rm.RMObject)57 Test (org.junit.Test)19 Composition (com.nedap.archie.rm.composition.Composition)10 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)9 QueryResponseData (org.ehrbase.response.openehr.QueryResponseData)8 Locatable (com.nedap.archie.rm.archetyped.Locatable)7 List (java.util.List)7 WebTemplateNode (org.ehrbase.webtemplate.model.WebTemplateNode)6 Collectors (java.util.stream.Collectors)5 TemplateId (com.nedap.archie.rm.archetyped.TemplateId)4 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Map (java.util.Map)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 TestDataTemplateProvider (org.ehrbase.client.templateprovider.TestDataTemplateProvider)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Archetyped (com.nedap.archie.rm.archetyped.Archetyped)3 Version (com.nedap.archie.rm.changecontrol.Version)3 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)3