use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class RevisionIndexReadRequest method execute.
@Override
public B execute(final BranchContext context) {
final String branchPath = context.path();
RevisionIndex index = context.service(RevisionIndex.class);
if (snapshot) {
return index.read(branchPath, searcher -> {
try {
return next(context.inject().bind(RevisionSearcher.class, searcher).build());
} catch (QueryParseException e) {
throw new IllegalQueryParameterException(e.getMessage());
}
});
} else {
return next(context.inject().bind(RevisionSearcher.class, new RevisionSearcher() {
@Override
public <T> Aggregation<T> aggregate(AggregationBuilder<T> aggregation) throws IOException {
return index.read(branchPath, searcher -> searcher.aggregate(aggregation));
}
@Override
public Searcher searcher() {
return index.read(branchPath, searcher -> searcher.searcher());
}
@Override
public <T> Hits<T> search(Query<T> query) throws IOException {
return index.read(branchPath, searcher -> searcher.search(query));
}
@Override
public <T> Iterable<T> get(Class<T> type, Iterable<String> keys) throws IOException {
return index.read(branchPath, searcher -> searcher.get(type, keys));
}
@Override
public <T> T get(Class<T> type, String key) throws IOException {
return index.read(branchPath, searcher -> searcher.get(type, key));
}
@Override
public String branch() {
return branchPath;
}
}).build());
}
}
use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class SnomedRf2ExportRequest method getIdentifierConcepts.
private List<SnomedConcept> getIdentifierConcepts(final RepositoryContext context, final String currentVersion) {
final Collection<String> refSetsToLoad;
if (refSets == null) {
// Retrieve all reference sets if refSets is null
final Request<BranchContext, SnomedReferenceSets> refSetRequest = SnomedRequests.prepareSearchRefSet().all().build();
final SnomedReferenceSets allReferenceSets = execute(context, currentVersion, refSetRequest);
refSetsToLoad = allReferenceSets.stream().map(r -> r.getId()).collect(Collectors.toSet());
} else {
refSetsToLoad = refSets;
}
final SnomedConceptSearchRequestBuilder refSetRequestBuilder = SnomedRequests.prepareSearchConcept().all().filterByIds(refSetsToLoad).setExpand("pt(),referenceSet()").setLocales(locales());
final Request<BranchContext, SnomedConcepts> request = refSetRequestBuilder.build();
final SnomedConcepts referenceSets = execute(context, currentVersion, request);
// Return only the identifier concepts which have an existing reference set on this branch
return referenceSets.stream().filter(c -> c.getReferenceSet() != null).collect(Collectors.toList());
}
use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class SnomedRf2ImportRequest method updateCodeSystemSettings.
private void updateCodeSystemSettings(final BranchContext context, final ResourceURI codeSystemUri) throws Exception {
SnomedReferenceSets languageReferenceSets = SnomedRequests.prepareSearchRefSet().all().filterByType(SnomedRefSetType.LANGUAGE).filterByActive(true).setFields(SnomedConceptDocument.Fields.ID).sortBy(Sort.fieldAsc(SnomedConceptDocument.Fields.ID)).build().execute(context);
/*
* XXX: The default language in locales is always "en", as there is no
* machine-readable information about what language code each language type
* reference set is associated with.
*/
final List<ExtendedLocale> locales = languageReferenceSets.stream().map(refSet -> new ExtendedLocale("en", "", refSet.getId())).collect(Collectors.toList());
// fetch codesystem again to get the latest settings
CodeSystem currentSnomedCodeSystem = CodeSystemRequests.prepareGetCodeSystem(codeSystemUri.getResourceId()).buildAsync().get(context);
Map<String, SnomedLanguageConfig> mergedLanguagesConfiguration = Maps.newLinkedHashMap();
SnomedDescriptionUtils.getLanguagesConfiguration(context.service(ObjectMapper.class), currentSnomedCodeSystem).forEach(config -> {
mergedLanguagesConfiguration.put(config.getLanguageTag(), config);
});
languageReferenceSets.stream().map(SnomedReferenceSet::getId).filter(SnomedTerminologyComponentConstants.LANG_REFSET_DIALECT_ALIASES::containsKey).forEach(langRefsetId -> {
final String dialect = SnomedTerminologyComponentConstants.LANG_REFSET_DIALECT_ALIASES.get(langRefsetId);
// ignore any aliases that are already defined by using computeIfAbsent
mergedLanguagesConfiguration.computeIfAbsent(dialect, languageTag -> new SnomedLanguageConfig(languageTag, langRefsetId));
});
CodeSystemRequests.prepareUpdateCodeSystem(codeSystemUri.getResourceId()).setSettings(Map.of(CodeSystem.CommonSettings.LOCALES, locales, SnomedTerminologyComponentConstants.CODESYSTEM_LANGUAGE_CONFIG_KEY, mergedLanguagesConfiguration.values())).build(context.service(User.class).getUsername(), String.format("Update '%s' settings based on RF2 import", codeSystemUri.getResourceId())).execute(context.service(IEventBus.class)).getSync(2, TimeUnit.MINUTES);
}
use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class SnomedValidationIssueDetailExtension method extendRelationshipIssueLabels.
private void extendRelationshipIssueLabels(BranchContext context, Collection<ValidationIssue> issues, Map<String, Object> ruleParameters) {
final RevisionSearcher searcher = context.service(RevisionSearcher.class);
final List<ValidationIssue> relationshipIssues = issues.stream().filter(issue -> SnomedRelationship.TYPE == issue.getAffectedComponent().getComponentType()).collect(Collectors.toList());
if (relationshipIssues.isEmpty()) {
return;
}
final Multimap<String, ValidationIssue> issuesByRelationshipId = Multimaps.index(relationshipIssues, issue -> issue.getAffectedComponent().getComponentId());
final Set<String> conceptsToFetch = newHashSet();
final Map<String, String> relationshipFragmentsByRelationshipId = Maps.newHashMap();
searcher.stream(Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.TYPE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID, SnomedRelationshipIndexEntry.Fields.VALUE_TYPE, SnomedRelationshipIndexEntry.Fields.NUMERIC_VALUE, SnomedRelationshipIndexEntry.Fields.STRING_VALUE).where(SnomedRelationshipIndexEntry.Expressions.ids(issuesByRelationshipId.keySet())).limit(SCROLL_SIZE).build()).forEach(hits -> {
for (String[] hit : hits) {
final String id = hit[0];
final String sourceId = hit[1];
final String typeId = hit[2];
final String destinationId = hit[3];
final String valueType = hit[4];
final String numericValue = hit[5];
final String stringValue = hit[6];
String destination = "";
conceptsToFetch.add(sourceId);
conceptsToFetch.add(typeId);
if (!Strings.isNullOrEmpty(destinationId)) {
conceptsToFetch.add(destinationId);
destination = destinationId;
} else {
if (RelationshipValueType.DECIMAL.name().equals(valueType) || RelationshipValueType.INTEGER.name().equals(valueType)) {
destination = DecimalUtils.decode(numericValue).toString();
} else if (RelationshipValueType.STRING.name().equals(valueType)) {
destination = stringValue;
}
}
relationshipFragmentsByRelationshipId.put(id, String.format("%s|%s|%s", sourceId, typeId, destination));
}
});
Map<String, String> affectedComponentLabelsByConcept = getAffectedComponentLabels(context, ruleParameters, conceptsToFetch);
if (!affectedComponentLabelsByConcept.isEmpty()) {
issuesByRelationshipId.values().forEach(issue -> {
final String[] relationshipFragments = relationshipFragmentsByRelationshipId.get(issue.getAffectedComponent().getComponentId()).split("[|]");
final String sourceId = relationshipFragments[0];
final String typeId = relationshipFragments[1];
final String destinationIdOrValue = relationshipFragments[2];
final String sourceTerm = affectedComponentLabelsByConcept.getOrDefault(sourceId, sourceId);
final String typeTerm = affectedComponentLabelsByConcept.getOrDefault(typeId, typeId);
final String destinationTerm = affectedComponentLabelsByConcept.getOrDefault(destinationIdOrValue, destinationIdOrValue);
issue.setAffectedComponentLabels(ImmutableList.of(String.format("%s - %s - %s", sourceTerm, typeTerm, destinationTerm)));
});
}
}
use of com.b2international.snowowl.core.domain.BranchContext 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];
}
Aggregations