Search in sources :

Example 1 with ExtendedLocale

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);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Query(com.b2international.index.query.Query) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) LoggerFactory(org.slf4j.LoggerFactory) SnomedReferenceSets(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) SnomedDescriptionUtils(com.b2international.snowowl.snomed.datastore.SnomedDescriptionUtils) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) Map(java.util.Map) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) SnomedLanguageConfig(com.b2international.snowowl.snomed.datastore.config.SnomedLanguageConfig) ZipFile(java.util.zip.ZipFile) Permission(com.b2international.snowowl.core.identity.Permission) ZipEntry(java.util.zip.ZipEntry) ResourceURI(com.b2international.snowowl.core.ResourceURI) Version(com.b2international.snowowl.core.version.Version) Rf2ReleaseType(com.b2international.snowowl.snomed.core.domain.Rf2ReleaseType) ResourceRequests(com.b2international.snowowl.core.request.ResourceRequests) ImmutableSet(com.google.common.collect.ImmutableSet) Rf2ValidationIssueReporter(com.b2international.snowowl.snomed.datastore.request.rf2.validation.Rf2ValidationIssueReporter) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) Set(java.util.Set) IEventBus(com.b2international.snowowl.eventbus.IEventBus) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) List(java.util.List) DatastoreLockContextDescriptions(com.b2international.snowowl.core.internal.locks.DatastoreLockContextDescriptions) LocalDate(java.time.LocalDate) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) DBMaker(org.mapdb.DBMaker) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) BranchContext(com.b2international.snowowl.core.domain.BranchContext) com.b2international.snowowl.snomed.datastore.request.rf2.importer(com.b2international.snowowl.snomed.datastore.request.rf2.importer) Maker(org.mapdb.DBMaker.Maker) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Locks(com.b2international.snowowl.core.locks.Locks) ImportResponse(com.b2international.snowowl.core.request.io.ImportResponse) Stopwatch(com.google.common.base.Stopwatch) Strings(com.google.common.base.Strings) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) Attachment(com.b2international.snowowl.core.attachments.Attachment) ApiException(com.b2international.commons.exceptions.ApiException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ImportDefectAcceptor(com.b2international.snowowl.core.request.io.ImportDefectAcceptor) Logger(org.slf4j.Logger) DateFormats(com.b2international.snowowl.core.date.DateFormats) CsvParser(com.fasterxml.jackson.dataformat.csv.CsvParser) Files(java.nio.file.Files) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) Maps(com.google.common.collect.Maps) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Paths(java.nio.file.Paths) Rf2GlobalValidator(com.b2international.snowowl.snomed.datastore.request.rf2.validation.Rf2GlobalValidator) User(com.b2international.snowowl.core.identity.User) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) DB(org.mapdb.DB) Collections(java.util.Collections) ContentAvailabilityInfoProvider(com.b2international.snowowl.core.repository.ContentAvailabilityInfoProvider) InputStream(java.io.InputStream) ImportDefectBuilder(com.b2international.snowowl.core.request.io.ImportDefectAcceptor.ImportDefectBuilder) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) SnomedReferenceSets(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) SnomedLanguageConfig(com.b2international.snowowl.snomed.datastore.config.SnomedLanguageConfig)

Example 2 with ExtendedLocale

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;
}
Also used : SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedDescriptions(com.b2international.snowowl.snomed.core.domain.SnomedDescriptions) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Example 3 with ExtendedLocale

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();
}
Also used : ExtendedLocale(com.b2international.commons.http.ExtendedLocale) TermFilter(com.b2international.snowowl.core.request.TermFilter) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Expressions(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry.Expressions) Expressions(com.b2international.index.query.Expressions) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 4 with ExtendedLocale

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;
}
Also used : ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 5 with ExtendedLocale

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;
}
Also used : SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) ExtendedLocale(com.b2international.commons.http.ExtendedLocale)

Aggregations

ExtendedLocale (com.b2international.commons.http.ExtendedLocale)6 BadRequestException (com.b2international.commons.exceptions.BadRequestException)4 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)2 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)2 ApiException (com.b2international.commons.exceptions.ApiException)1 Options (com.b2international.commons.options.Options)1 Expressions (com.b2international.index.query.Expressions)1 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)1 Query (com.b2international.index.query.Query)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 EclConceptReference (com.b2international.snomed.ecl.ecl.EclConceptReference)1 ExpressionConstraint (com.b2international.snomed.ecl.ecl.ExpressionConstraint)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 Attachment (com.b2international.snowowl.core.attachments.Attachment)1 AttachmentRegistry (com.b2international.snowowl.core.attachments.AttachmentRegistry)1 InternalAttachmentRegistry (com.b2international.snowowl.core.attachments.InternalAttachmentRegistry)1 AccessControl (com.b2international.snowowl.core.authorization.AccessControl)1 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)1