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());
}
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;
}
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);
}
}
}
}
}
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()));
}
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);
}
}
Aggregations