Search in sources :

Example 1 with SyntaxException

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);
}
Also used : ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) BadRequestException(com.b2international.commons.exceptions.BadRequestException) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) EObjectWalker(com.b2international.snowowl.core.emf.EObjectWalker) EObjectTreeNode(com.b2international.snowowl.core.emf.EObjectTreeNode) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) Strings(com.google.common.base.Strings) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) NotEmpty(org.hibernate.validator.constraints.NotEmpty) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) SyntaxException(com.b2international.commons.exceptions.SyntaxException) Permission(com.b2international.snowowl.core.identity.Permission) EclConceptReference(com.b2international.snomed.ecl.ecl.EclConceptReference) ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) BranchContext(com.b2international.snowowl.core.domain.BranchContext) NoopTreeVisitor(com.b2international.commons.tree.NoopTreeVisitor) ApiException(com.b2international.commons.exceptions.ApiException) ResourceRequest(com.b2international.snowowl.core.request.ResourceRequest) SyntaxException(com.b2international.commons.exceptions.SyntaxException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ApiException(com.b2international.commons.exceptions.ApiException)

Aggregations

ApiException (com.b2international.commons.exceptions.ApiException)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 SyntaxException (com.b2international.commons.exceptions.SyntaxException)1 NoopTreeVisitor (com.b2international.commons.tree.NoopTreeVisitor)1 EclConceptReference (com.b2international.snomed.ecl.ecl.EclConceptReference)1 ExpressionConstraint (com.b2international.snomed.ecl.ecl.ExpressionConstraint)1 AccessControl (com.b2international.snowowl.core.authorization.AccessControl)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 EObjectTreeNode (com.b2international.snowowl.core.emf.EObjectTreeNode)1 EObjectWalker (com.b2international.snowowl.core.emf.EObjectWalker)1 Permission (com.b2international.snowowl.core.identity.Permission)1 ResourceRequest (com.b2international.snowowl.core.request.ResourceRequest)1 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)1 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 Strings (com.google.common.base.Strings)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1