Search in sources :

Example 1 with PageableCollectionResource

use of com.b2international.snowowl.core.domain.PageableCollectionResource in project snow-owl by b2ihealthcare.

the class SnomedQueryValidationRuleEvaluator method eval.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<?> eval(BranchContext context, ValidationRule rule, Map<String, Object> params) throws Exception {
    checkArgument(type().equals(rule.getType()), "'%s' is not recognizable by this evaluator (accepts: %s)", rule, type());
    SnomedComponentValidationQuery<?, PageableCollectionResource<SnomedComponent>, SnomedComponent> validationQuery = context.service(ObjectMapper.class).<SnomedComponentValidationQuery<?, PageableCollectionResource<SnomedComponent>, SnomedComponent>>readValue(rule.getImplementation(), TYPE_REF);
    SnomedSearchRequestBuilder<?, PageableCollectionResource<SnomedComponent>> req = validationQuery.prepareSearch();
    String extensionModules = params != null && params.containsKey(ValidationConfiguration.MODULES) ? Strings.nullToEmpty((String) params.get(ValidationConfiguration.MODULES)) : "";
    String module = validationQuery.module;
    if (!Strings.isNullOrEmpty(module)) {
        // If there is a module given assume that it must be more specific and provided on purpose
        req.filterByModule(module);
    } else if (Boolean.TRUE.equals(validationQuery.extensionScope) && !Strings.isNullOrEmpty(extensionModules)) {
        req.filterByModule(extensionModules);
    }
    SearchIndexResourceRequest<BranchContext, ?, ? extends SnomedDocument> searchReq = (SearchIndexResourceRequest<BranchContext, ?, ? extends SnomedDocument>) req.build();
    final ExpressionBuilder expressionBuilder = Expressions.builder().filter(searchReq.toRawQuery(context));
    if (params != null && params.containsKey(ValidationConfiguration.IS_UNPUBLISHED_ONLY) && Boolean.TRUE.equals(params.get(ValidationConfiguration.IS_UNPUBLISHED_ONLY))) {
        expressionBuilder.filter(SnomedDocument.Expressions.effectiveTime(EffectiveTimes.UNSET_EFFECTIVE_TIME));
    }
    Expression where = expressionBuilder.build();
    // TODO check if the expression contains only the ID list, then skip scrolling and just report them
    List[] issues = { null };
    Query.select(String.class).from(validationQuery.getDocType()).fields(SnomedDocument.Fields.ID).where(where).limit(RULE_LIMIT).withScores(false).build().stream(context.service(RevisionSearcher.class)).forEachOrdered(page -> {
        if (issues[0] == null) {
            issues[0] = newArrayListWithExpectedSize(page.getTotal());
        }
        for (String affectedComponentId : page) {
            String affectedComponentType = SnomedComponent.getTypeSafe(affectedComponentId);
            if (TerminologyRegistry.UNKNOWN_COMPONENT_TYPE.equals(affectedComponentType)) {
                affectedComponentType = SnomedReferenceSetMember.TYPE;
            }
            issues[0].add(ComponentIdentifier.of(affectedComponentType, affectedComponentId));
        }
    });
    return issues[0] == null ? Collections.emptyList() : issues[0];
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SearchIndexResourceRequest(com.b2international.snowowl.core.request.SearchIndexResourceRequest) Expression(com.b2international.index.query.Expression) BranchContext(com.b2international.snowowl.core.domain.BranchContext) PageableCollectionResource(com.b2international.snowowl.core.domain.PageableCollectionResource) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Expression (com.b2international.index.query.Expression)1 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 PageableCollectionResource (com.b2international.snowowl.core.domain.PageableCollectionResource)1 SearchIndexResourceRequest (com.b2international.snowowl.core.request.SearchIndexResourceRequest)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1