use of com.b2international.commons.http.ExtendedLocale 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.commons.http.ExtendedLocale in project snow-owl by b2ihealthcare.
the class SnomedValidationIssueDetailExtension method getAffectedComponentLabels.
@SuppressWarnings("unchecked")
private Map<String, String> getAffectedComponentLabels(BranchContext context, Map<String, Object> ruleParameters, final Set<String> conceptIds) {
boolean useFsn = ruleParameters.containsKey(ValidationConfiguration.USE_FSN) ? (boolean) ruleParameters.get(ValidationConfiguration.USE_FSN) : false;
Set<String> types;
if (useFsn) {
types = Set.of(Concepts.FULLY_SPECIFIED_NAME);
} else {
types = SnomedRequests.prepareGetSynonyms().build().execute(context).stream().map(SnomedConcept::getId).collect(Collectors.toSet());
}
List<ExtendedLocale> locales;
if (ruleParameters.containsKey(ValidationConfiguration.LOCALES)) {
locales = (List<ExtendedLocale>) ruleParameters.get(ValidationConfiguration.LOCALES);
} else {
locales = List.of();
}
final Map<String, String> affectedComponentLabelsByConcept = new HashMap<>();
for (List<String> partition : Iterables.partition(conceptIds, SCROLL_SIZE)) {
SnomedDescriptions descriptions = SnomedRequests.prepareSearchDescription().all().filterByActive(true).filterByConcepts(partition).filterByType(types).build().execute(context);
Map<String, SnomedDescription> preferredDescriptions = SnomedDescriptionUtils.indexBestPreferredByConceptId(context, descriptions, locales);
preferredDescriptions.forEach((id, description) -> affectedComponentLabelsByConcept.put(id, description.getTerm()));
}
return affectedComponentLabelsByConcept;
}
use of com.b2international.commons.http.ExtendedLocale in project snow-owl by b2ihealthcare.
the class SnomedDescriptionSearchRequest method prepareQuery.
@Override
protected Expression prepareQuery(BranchContext context) {
if (containsKey(OptionKey.TERM) && get(OptionKey.TERM, TermFilter.class).getTerm().length() < 2) {
throw new BadRequestException("Description term must be at least 2 characters long.");
}
final ExpressionBuilder queryBuilder = Expressions.builder();
// Add (presumably) most selective filters first
addActiveClause(queryBuilder);
addReleasedClause(queryBuilder);
addLanguageFilter(queryBuilder);
addNamespaceFilter(queryBuilder);
addNamespaceConceptIdFilter(context, queryBuilder);
addActiveMemberOfClause(context, queryBuilder);
addMemberOfClause(context, queryBuilder);
addEclFilter(context, queryBuilder, OptionKey.LANGUAGE_REFSET, ids -> {
return Expressions.builder().should(preferredIn(ids)).should(acceptableIn(ids)).build();
});
addEclFilter(context, queryBuilder, OptionKey.ACCEPTABLE_IN, ids -> acceptableIn(ids));
addEclFilter(context, queryBuilder, OptionKey.PREFERRED_IN, ids -> preferredIn(ids));
// apply locale based filters
addFilter(queryBuilder, OptionKey.LANGUAGE_REFSET_LOCALES, ExtendedLocale.class, locales -> {
final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
return Expressions.builder().should(preferredIn(languageRefSetIds)).should(acceptableIn(languageRefSetIds)).build();
});
addFilter(queryBuilder, OptionKey.ACCEPTABLE_IN_LOCALES, ExtendedLocale.class, locales -> {
final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
return acceptableIn(languageRefSetIds);
});
addFilter(queryBuilder, OptionKey.PREFERRED_IN_LOCALES, ExtendedLocale.class, locales -> {
final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
return preferredIn(languageRefSetIds);
});
addEffectiveTimeClause(queryBuilder);
addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
addEclFilter(context, queryBuilder, OptionKey.CONCEPT, SnomedDescriptionIndexEntry.Expressions::concepts);
addEclFilter(context, queryBuilder, OptionKey.TYPE, SnomedDescriptionIndexEntry.Expressions::types);
addEclFilter(context, queryBuilder, OptionKey.CASE_SIGNIFICANCE, SnomedDescriptionIndexEntry.Expressions::caseSignificances);
if (containsKey(OptionKey.SEMANTIC_TAG)) {
queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.semanticTags(getCollection(OptionKey.SEMANTIC_TAG, String.class)));
}
if (containsKey(OptionKey.SEMANTIC_TAG_REGEX)) {
queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.semanticTagRegex(getString(OptionKey.SEMANTIC_TAG_REGEX)));
}
if (containsKey(OptionKey.TERM_REGEX)) {
final String regex = getString(OptionKey.TERM_REGEX);
queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.matchTermRegex(regex));
}
if (containsKey(OptionKey.TERM)) {
final TermFilter termFilter = get(OptionKey.TERM, TermFilter.class);
queryBuilder.must(toDescriptionTermQuery(termFilter));
}
return queryBuilder.build();
}
use of com.b2international.commons.http.ExtendedLocale in project snow-owl by b2ihealthcare.
the class SnomedDescriptionUtils method getLanguageRefSetIds.
/**
* Extracts the language reference set identifier from the specified list of {@link ExtendedLocale}s.
* <p>
* The identifiers may come from the value itself, if it includes a reference set ID (eg. {@code en-x-12345678901}),
* or from the language tag part, if it is well known (eg. {@code en-US}).
* <p>
* If no element from the input list can be converted, an {@link IllegalArgumentException} is thrown; no exception occurs
* if only some of the {@code ExtendedLocale}s could not be transformed into a language reference set identifier, however.
*
* @param context - the context to use to retrieve language map configuration
* @param locales the extended locale list to process (may not be {@code null})
* @return the converted language reference set identifiers or an empty {@link List}, never <code>null</code>
*/
public static List<String> getLanguageRefSetIds(final BranchContext context, final List<ExtendedLocale> locales) {
if (CompareUtils.isEmpty(locales)) {
return Collections.emptyList();
}
final ListMultimap<String, String> languageMap = getLanguageMapping(context);
final List<String> languageRefSetIds = newArrayList();
final List<ExtendedLocale> unconvertableLocales = new ArrayList<ExtendedLocale>();
for (final ExtendedLocale extendedLocale : locales) {
Collection<String> mappedRefSetIds;
if (!extendedLocale.getLanguageRefSetId().isEmpty()) {
mappedRefSetIds = Collections.singleton(extendedLocale.getLanguageRefSetId());
} else {
mappedRefSetIds = languageMap.get(extendedLocale.getLanguageTag());
}
if (mappedRefSetIds.isEmpty()) {
unconvertableLocales.add(extendedLocale);
} else {
mappedRefSetIds.forEach(mappedRefSetId -> {
if (!languageRefSetIds.contains(mappedRefSetId)) {
languageRefSetIds.add(mappedRefSetId);
}
});
}
}
if (languageRefSetIds.isEmpty() && !unconvertableLocales.isEmpty()) {
throw new BadRequestException("Don't know how to convert extended locale %s to a language reference set identifier.", Iterables.toString(unconvertableLocales));
}
return languageRefSetIds;
}
use of com.b2international.commons.http.ExtendedLocale in project snow-owl by b2ihealthcare.
the class SnomedConceptSearchRequest method executeDescriptionSearch.
private Map<String, Float> executeDescriptionSearch(BranchContext context, TermFilter termFilter) {
final SnomedDescriptionSearchRequestBuilder requestBuilder = SnomedRequests.prepareSearchDescription().all().filterByActive(true).setFields(SnomedDescriptionIndexEntry.Fields.ID, SnomedDescriptionIndexEntry.Fields.CONCEPT_ID).sortBy(SCORE);
if (containsKey(SnomedConceptSearchRequest.OptionKey.LANGUAGE_REFSET)) {
List<ExtendedLocale> extendedLocales = getList(SnomedDescriptionSearchRequest.OptionKey.LANGUAGE_REFSET, ExtendedLocale.class);
requestBuilder.filterByLanguageRefSets(SnomedDescriptionUtils.getLanguageRefSetIds(context, extendedLocales));
}
applyIdFilter(requestBuilder, (rb, ids) -> rb.filterByConcepts(ids));
if (containsKey(OptionKey.DESCRIPTION_TYPE)) {
final String type = getString(OptionKey.DESCRIPTION_TYPE);
requestBuilder.filterByType(type);
}
if (termFilter != null) {
requestBuilder.filterByTerm(termFilter);
}
final Collection<SnomedDescription> items = requestBuilder.build().execute(context).getItems();
final Map<String, Float> conceptMap = newHashMap();
for (SnomedDescription description : items) {
if (!conceptMap.containsKey(description.getConceptId())) {
conceptMap.put(description.getConceptId(), description.getScore());
}
}
return conceptMap;
}
Aggregations