Search in sources :

Example 1 with SdkException

use of org.ehrbase.util.exception.SdkException in project openEHR_SDK by ehrbase.

the class WhereBinder method buildLogicalOperator.

private Pair<Condition, List<ParameterValue>> buildLogicalOperator(ConditionLogicalOperatorSymbol symbol, Pair<Condition, List<ParameterValue>> pair1, Pair<Condition, List<ParameterValue>> pair2) {
    final Condition containmentExpression;
    switch(symbol) {
        case OR:
            containmentExpression = pair1.getLeft().or(pair2.getLeft());
            break;
        case AND:
            containmentExpression = pair1.getLeft().and(pair2.getLeft());
            break;
        default:
            throw new SdkException(String.format("Unknown Symbol %s", symbol));
    }
    pair1.getRight().addAll(pair2.getRight());
    return new ImmutablePair<>(containmentExpression, pair1.getRight());
}
Also used : Condition(org.ehrbase.client.aql.condition.Condition) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) SdkException(org.ehrbase.util.exception.SdkException)

Example 2 with SdkException

use of org.ehrbase.util.exception.SdkException 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 3 with SdkException

use of org.ehrbase.util.exception.SdkException in project openEHR_SDK by ehrbase.

the class AbstractRMUnmarshaller method handle.

/**
 * {@inheritDoc} Use {@link RmIntrospectConfig} to find die properties which needs to be set
 */
public void handle(String currentTerm, T rmObject, Map<FlatPathDto, String> currentValues, Context<Map<FlatPathDto, String>> context, Set<String> consumedPaths) {
    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());
                setValue(currentTerm, null, currentValues, s -> {
                    try {
                        propertyDescriptor.getWriteMethod().invoke(rmObject, s);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        throw new SdkException(e.getMessage(), e);
                    }
                }, propertyDescriptor.getPropertyType(), consumedPaths);
            } catch (IntrospectionException e) {
                throw new SdkException(e.getMessage(), e);
            }
        } else {
            for (String propertyName : expandFields) {
                try {
                    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, rmObject.getClass());
                    setValue(currentTerm, propertyName, currentValues, s -> {
                        try {
                            propertyDescriptor.getWriteMethod().invoke(rmObject, s);
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            throw new SdkException(e.getMessage(), e);
                        }
                    }, propertyDescriptor.getPropertyType(), consumedPaths);
                } catch (IntrospectionException e) {
                    throw new SdkException(e.getMessage(), e);
                }
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) SdkException(org.ehrbase.util.exception.SdkException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with SdkException

use of org.ehrbase.util.exception.SdkException in project openEHR_SDK by ehrbase.

the class StructuredHelper method convertStructuredToFlat.

private static Map<String, JsonNode> convertStructuredToFlat(String path, JsonNode jsonNode) {
    if (jsonNode instanceof ValueNode) {
        return Map.of(path, jsonNode);
    } else if (jsonNode instanceof ObjectNode) {
        Map<String, JsonNode> map = new HashMap<>();
        for (Iterator<Map.Entry<String, JsonNode>> it = jsonNode.fields(); it.hasNext(); ) {
            Map.Entry<String, JsonNode> field = it.next();
            final String newPath;
            if (StringUtils.startsWith(field.getKey(), "|") || StringUtils.isBlank(path)) {
                newPath = path + field.getKey();
            } else if (StringUtils.isEmpty(field.getKey())) {
                newPath = path;
            } else {
                newPath = path + PATH_DIVIDER + field.getKey();
            }
            map.putAll(convertStructuredToFlat(newPath, field.getValue()));
        }
        return map;
    } else if (jsonNode instanceof ArrayNode) {
        Map<String, JsonNode> map = new LinkedHashMap<>();
        boolean needsIndex = IntStream.range(0, jsonNode.size()).mapToObj(jsonNode::get).map(n -> {
            if (n instanceof ObjectNode) {
                return StreamSupport.stream(Spliterators.spliteratorUnknownSize(n.fields(), Spliterator.ORDERED), false).map(Map.Entry::getKey).anyMatch(s -> !StringUtils.startsWith(s, "_"));
            }
            return true;
        }).filter(BooleanUtils::isTrue).count() > 1;
        for (int i = 0; i < jsonNode.size(); i++) {
            JsonNode child = jsonNode.get(i);
            final String newPath;
            if (i > 0 && needsIndex) {
                newPath = path + ":" + i;
            } else {
                // To save a look-up if at this path we have a single value or the first value of a multi
                // Node,
                // we can always leave the 0 since the Flat Parser can handle missing 0.
                newPath = path;
            }
            map.putAll(convertStructuredToFlat(newPath, child));
        }
        return map;
    }
    throw new SdkException(String.format("Unknown Structure %s", jsonNode.getClass().getSimpleName()));
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) SdkException(org.ehrbase.util.exception.SdkException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JacksonUtil(org.ehrbase.serialisation.jsonencoding.JacksonUtil) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PATH_DIVIDER(org.ehrbase.webtemplate.parser.OPTParser.PATH_DIVIDER) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) BooleanUtils(org.apache.commons.lang3.BooleanUtils) SdkException(org.ehrbase.util.exception.SdkException) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 5 with SdkException

use of org.ehrbase.util.exception.SdkException in project openEHR_SDK by ehrbase.

the class StructuredHelper method convertStructuredToFlat.

/**
 * Convert Structured JSON into Flat JSON
 *
 * @param structuredString
 * @return
 */
public static String convertStructuredToFlat(String structuredString) {
    try {
        JsonNode jsonNode = OBJECT_MAPPER.readTree(structuredString);
        Map<String, JsonNode> convert = convertStructuredToFlat("", jsonNode);
        return OBJECT_MAPPER.writeValueAsString(convert);
    } catch (JsonProcessingException e) {
        throw new SdkException(e.getMessage(), e);
    }
}
Also used : SdkException(org.ehrbase.util.exception.SdkException) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

SdkException (org.ehrbase.util.exception.SdkException)18 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)5 RMObject (com.nedap.archie.rm.RMObject)4 List (java.util.List)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Locatable (com.nedap.archie.rm.archetyped.Locatable)3 Composition (com.nedap.archie.rm.composition.Composition)3 DvInterval (com.nedap.archie.rm.datavalues.quantity.DvInterval)3 HierObjectId (com.nedap.archie.rm.support.identification.HierObjectId)3 RMAttributeInfo (com.nedap.archie.rminfo.RMAttributeInfo)3 Map (java.util.Map)3 Condition (org.ehrbase.client.aql.condition.Condition)3 CComplexObject (com.nedap.archie.aom.CComplexObject)2 Element (com.nedap.archie.rm.datastructures.Element)2 DvCodedText (com.nedap.archie.rm.datavalues.DvCodedText)2 ClassGraph (io.github.classgraph.ClassGraph)2 ScanResult (io.github.classgraph.ScanResult)2 IntrospectionException (java.beans.IntrospectionException)2