Search in sources :

Example 1 with SnomedReferenceSet

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet 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 SnomedReferenceSet

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet in project snow-owl by b2ihealthcare.

the class Rf2TransactionContext method add.

void add(Collection<SnomedComponent> componentChanges, Multimap<Class<? extends SnomedDocument>, String> dependenciesByType) {
    final Multimap<Class<? extends SnomedDocument>, SnomedComponent> componentChangesByType = Multimaps.index(componentChanges, this::getDocType);
    for (Class<? extends SnomedDocument> type : IMPORT_ORDER) {
        final Collection<SnomedComponent> rf2Components = componentChangesByType.get(type);
        final Set<String> componentsToLookup = rf2Components.stream().map(IComponent::getId).collect(Collectors.toSet());
        // add all dependencies with the same type
        componentsToLookup.addAll(dependenciesByType.get(type));
        final Map<String, ? extends SnomedDocument> existingComponents = lookup(componentsToLookup, type);
        final Map<String, SnomedConceptDocument> existingRefSets;
        if (SnomedRefSetMemberIndexEntry.class == type) {
            existingRefSets = lookup(rf2Components.stream().map(member -> ((SnomedReferenceSetMember) member).getRefsetId()).collect(Collectors.toSet()), SnomedConceptDocument.class);
        } else {
            existingRefSets = Collections.emptyMap();
        }
        final Set<String> newRefSetIds = newHashSet();
        // seed missing component before applying row changes
        // and check for existing components with the same or greater effective time and skip them
        final Collection<SnomedComponent> componentsToImport = newArrayList();
        for (SnomedComponent rf2Component : rf2Components) {
            SnomedDocument existingObject = existingComponents.get(rf2Component.getId());
            if (existingObject == null) {
                // new component, add to new components and register row for import
                newComponents.put(rf2Component.getId(), createIdOnlyDoc(rf2Component.getId(), type));
                componentsToImport.add(rf2Component);
            } else if (existingObject instanceof SnomedDocument && rf2Component instanceof SnomedComponent) {
                final SnomedComponent rf2Row = (SnomedComponent) rf2Component;
                final SnomedDocument existingRow = (SnomedDocument) existingObject;
                if (rf2Row.getEffectiveTime() == null || EffectiveTimes.getEffectiveTime(rf2Row.getEffectiveTime()) > existingRow.getEffectiveTime()) {
                    componentsToImport.add(rf2Component);
                }
            }
            // check and register refset props on concept docs
            if (rf2Component instanceof SnomedReferenceSetMember) {
                final SnomedReferenceSetMember member = (SnomedReferenceSetMember) rf2Component;
                // seed the refset if missing
                final String refSetId = member.getRefsetId();
                SnomedConceptDocument conceptDocToUpdate = existingRefSets.get(refSetId);
                if (conceptDocToUpdate == null || newComponents.containsKey(refSetId)) {
                    conceptDocToUpdate = (SnomedConceptDocument) newComponents.get(refSetId);
                }
                if (conceptDocToUpdate.getRefSetType() == null) {
                    final String referencedComponentType = SnomedComponent.getType(member.getReferencedComponentId());
                    String mapTargetComponentType = TerminologyRegistry.UNKNOWN_COMPONENT_TYPE;
                    try {
                        mapTargetComponentType = SnomedComponent.getType((String) member.getProperties().get(SnomedRf2Headers.FIELD_MAP_TARGET));
                    } catch (IllegalArgumentException e) {
                    // ignored
                    }
                    final SnomedReferenceSet refSet = new SnomedReferenceSet();
                    refSet.setType(member.type());
                    refSet.setReferencedComponentType(referencedComponentType);
                    refSet.setMapTargetComponentType(mapTargetComponentType);
                    final SnomedConceptDocument updatedConcept = SnomedConceptDocument.builder(conceptDocToUpdate).refSet(refSet).build();
                    if (newComponents.containsKey(refSetId)) {
                        newComponents.put(refSetId, updatedConcept);
                        newRefSetIds.add(refSetId);
                    } else {
                        update(conceptDocToUpdate, updatedConcept);
                    }
                }
            }
        }
        // apply row changes
        for (SnomedComponent rf2Component : componentsToImport) {
            final String id = rf2Component.getId();
            SnomedDocument existingRevision = null;
            SnomedDocument.Builder<?, ?> newRevision;
            if (newComponents.containsKey(id)) {
                newRevision = createDocBuilder(id, type, newComponents.get(id));
            } else if (existingComponents.containsKey(id)) {
                existingRevision = existingComponents.get(id);
                newRevision = createDocBuilder(id, type, existingRevision);
            } else {
                throw new IllegalStateException(String.format("Current revision is null for %s", id));
            }
            final SnomedComponentBuilder builder;
            if (rf2Component instanceof SnomedCoreComponent) {
                builder = prepareCoreComponent(rf2Component);
            } else if (rf2Component instanceof SnomedReferenceSetMember) {
                builder = prepareMember((SnomedReferenceSetMember) rf2Component);
            } else {
                throw new UnsupportedOperationException("Unsupported component: " + rf2Component);
            }
            // apply row changes
            builder.init(newRevision, this);
            if (existingRevision == null) {
                // in this case the component is new, and the default values are okay to use
                add(newRevision.build());
            } else {
                // in this case, recalculate the released flag based on the currently available revision
                if (existingRevision.isReleased()) {
                    update(existingRevision, newRevision.released(existingRevision.isReleased()).build());
                } else {
                    update(existingRevision, newRevision.build());
                }
            }
        }
        // make sure we always attach refset properties to identifier concepts
        final StagingArea staging = service(StagingArea.class);
        for (String newRefSetId : newRefSetIds) {
            SnomedConceptDocument newRefSet = (SnomedConceptDocument) newComponents.get(newRefSetId);
            SnomedConceptDocument stagedNewRefSet = (SnomedConceptDocument) staging.getNewObject(SnomedConceptDocument.class, newRefSetId);
            if (newRefSet != null && stagedNewRefSet != null) {
                if (stagedNewRefSet.getRefSetType() == null) {
                    add(SnomedConceptDocument.builder(stagedNewRefSet).refSetType(newRefSet.getRefSetType()).referencedComponentType(newRefSet.getReferencedComponentType()).mapTargetComponentType(newRefSet.getMapTargetComponentType()).build());
                }
            }
        }
    }
}
Also used : EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) SnomedComponentBuilder(com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) ImmutableList(com.google.common.collect.ImmutableList) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ComponentNotFoundException(com.b2international.snowowl.core.exceptions.ComponentNotFoundException) ISnomedIdentifierService(com.b2international.snowowl.snomed.cis.ISnomedIdentifierService) SnomedComponents(com.b2international.snowowl.snomed.core.store.SnomedComponents) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Commit(com.b2international.index.revision.Commit) SnomedMemberBuilder(com.b2international.snowowl.snomed.core.store.SnomedMemberBuilder) Collectors(java.util.stream.Collectors) TerminologyRegistry(com.b2international.snowowl.core.terminology.TerminologyRegistry) RepositoryTransactionContext(com.b2international.snowowl.core.repository.RepositoryTransactionContext) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) LocalDate(java.time.LocalDate) StagingArea(com.b2international.index.revision.StagingArea) DelegatingTransactionContext(com.b2international.snowowl.core.domain.DelegatingTransactionContext) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) StagingArea(com.b2international.index.revision.StagingArea) SnomedComponentBuilder(com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet)

Example 3 with SnomedReferenceSet

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet in project snow-owl by b2ihealthcare.

the class SnomedRefSetCreateRequest method execute.

@Override
public String execute(TransactionContext context) {
    RefSetSupport.checkType(type, referencedComponentType);
    final SnomedConceptDocument concept;
    if (Strings.isNullOrEmpty(identifierId)) {
        throw new BadRequestException("Reference set identifier ID may not be null or empty.");
    } else {
        try {
            concept = context.lookup(identifierId, SnomedConceptDocument.class);
            if (concept.getRefSetType() != null) {
                throw new BadRequestException("Identifier concept %s has been already registered as refset", identifierId);
            }
        } catch (ComponentNotFoundException e) {
            throw e.toBadRequestException();
        }
    }
    final SnomedConceptDocument.Builder updatedConcept = SnomedConceptDocument.builder(concept);
    final SnomedReferenceSet refSet = new SnomedReferenceSet();
    refSet.setType(type);
    refSet.setReferencedComponentType(referencedComponentType);
    if (SnomedRefSetUtil.isMapping(type)) {
        refSet.setMapTargetComponentType(mapTargetComponentType);
    }
    updatedConcept.refSet(refSet);
    context.update(concept, updatedConcept.build());
    return identifierId;
}
Also used : SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) ComponentNotFoundException(com.b2international.snowowl.core.exceptions.ComponentNotFoundException) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 4 with SnomedReferenceSet

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet in project snow-owl by b2ihealthcare.

the class SnomedReferenceSetConverter method toResource.

@Override
public SnomedReferenceSet toResource(SnomedConceptDocument entry) {
    final SnomedReferenceSet refset = new SnomedReferenceSet();
    refset.setId(entry.getId());
    refset.setEffectiveTime(EffectiveTimes.toDate(entry.getEffectiveTime()));
    refset.setActive(entry.isActive());
    refset.setReleased(entry.isReleased());
    refset.setModuleId(entry.getModuleId());
    refset.setIconId(entry.getIconId());
    refset.setScore(entry.getScore());
    refset.setReferencedComponentType(entry.getReferencedComponentType());
    refset.setMapTargetComponentType(entry.getMapTargetComponentType());
    refset.setType(entry.getRefSetType());
    return refset;
}
Also used : SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet)

Example 5 with SnomedReferenceSet

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet in project snow-owl by b2ihealthcare.

the class SnomedReferenceSetConverter method expandMembers.

private void expandMembers(List<SnomedReferenceSet> results) {
    if (expand().containsKey("members")) {
        Options expandOptions = expand().get("members", Options.class);
        for (SnomedReferenceSet refSet : results) {
            SnomedRefSetMemberSearchRequestBuilder req = SnomedRequests.prepareSearchMember().filterByRefSet(refSet.getId()).setLocales(locales()).setExpand(expandOptions.get("expand", Options.class));
            if (expandOptions.containsKey("limit")) {
                req.setLimit(expandOptions.get("limit", Integer.class));
            }
            refSet.setMembers(req.build().execute(context()));
        }
    }
}
Also used : Options(com.b2international.commons.options.Options) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) SnomedRefSetMemberSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRefSetMemberSearchRequestBuilder)

Aggregations

SnomedReferenceSet (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet)9 ComponentNotFoundException (com.b2international.snowowl.core.exceptions.ComponentNotFoundException)4 BadRequestException (com.b2international.commons.exceptions.BadRequestException)3 EffectiveTimes (com.b2international.snowowl.core.date.EffectiveTimes)2 com.b2international.snowowl.snomed.datastore.index.entry (com.b2international.snowowl.snomed.datastore.index.entry)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 LocalDate (java.time.LocalDate)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ApiException (com.b2international.commons.exceptions.ApiException)1 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)1 Options (com.b2international.commons.options.Options)1 Query (com.b2international.index.query.Query)1 Commit (com.b2international.index.revision.Commit)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 StagingArea (com.b2international.index.revision.StagingArea)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1