use of com.b2international.commons.exceptions.SyntaxException in project snow-owl by b2ihealthcare.
the class SnomedEclLabelerRequest method execute.
@Override
public LabeledEclExpressions execute(BranchContext context) {
final EclSerializer eclSerializer = context.service(EclSerializer.class);
final EclParser eclParser = context.service(EclParser.class);
final Set<String> conceptIdsToLabel = Sets.newHashSetWithExpectedSize(expressions.size());
final Map<String, ExpressionConstraint> queries = Maps.newHashMapWithExpectedSize(expressions.size());
final LinkedHashMap<String, Object> errors = Maps.newLinkedHashMap();
for (String expression : expressions) {
if (Strings.isNullOrEmpty(expression)) {
continue;
}
try {
ExpressionConstraint query = queries.computeIfAbsent(expression, (key) -> eclParser.parse(key));
conceptIdsToLabel.addAll(collect(query));
} catch (ApiException e) {
if (e instanceof SyntaxException) {
errors.put(expression, List.copyOf(((SyntaxException) e).getAdditionalInfo().values()));
} else if (e instanceof BadRequestException) {
errors.put(expression, e.getMessage());
} else {
throw e;
}
}
}
if (!errors.isEmpty()) {
BadRequestException badRequestException = new BadRequestException("One or more ECL syntax errors");
badRequestException.withAdditionalInfo("erroneousExpressions", errors);
throw badRequestException;
}
// fetch all concept labels
final Map<String, String> labels = SnomedRequests.prepareSearchConcept().filterByIds(conceptIdsToLabel).setLimit(conceptIdsToLabel.size()).setExpand(descriptionType.toLowerCase() + "()").setLocales(locales()).build().execute(context).stream().collect(Collectors.toMap(SnomedConcept::getId, this::extractLabel));
// expand all queries with labels
List<String> results = expressions.stream().map(expression -> {
if (Strings.isNullOrEmpty(expression)) {
return expression;
} else {
ExpressionConstraint query = queries.get(expression);
expand(query, labels);
return eclSerializer.serialize(query);
}
}).collect(Collectors.toList());
return new LabeledEclExpressions(results);
}
Aggregations