Search in sources :

Example 11 with SnomedReferenceSetMember

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

the class MapDBRf2EffectiveTimeSlice method doImport.

@Override
public void doImport(final BranchContext context, final ResourceURI codeSystemUri, final Rf2ImportConfiguration importConfig, final ImmutableSet.Builder<ComponentURI> visitedComponents) throws Exception {
    final Stopwatch w = Stopwatch.createStarted();
    final String importingMessage = isUnpublishedSlice() ? "Importing unpublished components" : String.format("Importing components from %s", getEffectiveTime());
    final String commitMessage = isUnpublishedSlice() ? "Imported unpublished components" : String.format("Imported components from %s", getEffectiveTime());
    final boolean doCreateVersion = !isUnpublishedSlice() && !isSnapshotSlice() && importConfig.isCreateVersions();
    // Collect the type ID for all integer relationship values
    final LongKeyLongMap integerTypeIdsByValueId = PrimitiveMaps.newLongKeyLongOpenHashMap();
    context.log().info(importingMessage);
    try (Rf2TransactionContext tx = new Rf2TransactionContext(context.openTransaction(context, DatastoreLockContextDescriptions.IMPORT), loadOnDemand, importConfig)) {
        final Iterator<LongSet> importPlan = getImportPlan().iterator();
        while (importPlan.hasNext()) {
            LongSet componentsToImportInBatch = importPlan.next();
            LongIterator it = componentsToImportInBatch.iterator();
            final Collection<SnomedComponent> componentsToImport = newArrayListWithExpectedSize(componentsToImportInBatch.size());
            while (it.hasNext()) {
                long componentToImportL = it.next();
                String componentToImport = Long.toString(componentToImportL);
                final SnomedComponent component = getComponent(componentToImport);
                if (component != null) {
                    componentsToImport.add(component);
                    // Record value types across the entire effective time slice
                    if (component instanceof SnomedRelationship) {
                        final SnomedRelationship relationship = (SnomedRelationship) component;
                        final RelationshipValue relationshipValue = relationship.getValueAsObject();
                        if (relationshipValue != null) {
                            if (RelationshipValueType.INTEGER.equals(relationshipValue.type())) {
                                integerTypeIdsByValueId.put(componentToImportL, Long.parseLong(relationship.getTypeId()));
                            }
                        }
                    }
                    // Register container concept as visited component
                    final String conceptId = getConceptId(component);
                    visitedComponents.add(ComponentURI.of(codeSystemUri, SnomedConcept.TYPE, conceptId));
                }
                // add all members of this component to this batch as well
                final Set<String> containerComponents = membersByReferencedComponent.remove(componentToImportL);
                if (containerComponents != null) {
                    for (String containedComponentId : containerComponents) {
                        SnomedReferenceSetMember containedComponent = getComponent(containedComponentId);
                        if (containedComponent != null) {
                            componentsToImport.add(containedComponent);
                            // Register reference set as visited component
                            final String refSetId = containedComponent.getRefsetId();
                            visitedComponents.add(ComponentURI.of(codeSystemUri, SnomedConcept.REFSET_TYPE, refSetId));
                        }
                    }
                }
            }
            tx.add(componentsToImport, getDependencies(componentsToImport));
            tx.commit(commitMessage);
        }
        // Check if any integer values should actually be decimals, indicated by the range constraint on MRCM members
        final LongSet decimalTypeIds = collectAttributesWithRangeConstraint(context, "dec(>#0..)");
        final LongSet decimalValueIds = collectValueTypeChanges(integerTypeIdsByValueId, decimalTypeIds);
        swapValueType(context, decimalValueIds);
        if (doCreateVersion) {
            ResourceRequests.prepareNewVersion().setResource(codeSystemUri).setVersion(getEffectiveTime()).setDescription("").setEffectiveTime(getEffectiveDate()).buildAsync().getRequest().execute(context);
        }
    }
    context.log().info("{} in {}", commitMessage, w);
}
Also used : Stopwatch(com.google.common.base.Stopwatch) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)

Example 12 with SnomedReferenceSetMember

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

the class SnomedImportApiTest method import26OWLExpressionReferenceSetMembers.

@Test
public void import26OWLExpressionReferenceSetMembers() throws Exception {
    SnomedConcept oldRoot = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
    assertTrue(oldRoot.getMembers().getItems().stream().noneMatch(m -> m.getRefsetId().equals(Concepts.REFSET_OWL_AXIOM) || m.getRefsetId().equals(Concepts.REFSET_OWL_ONTOLOGY)));
    importArchive("SnomedCT_Release_INT_20170731_new_owl_expression_members.zip");
    SnomedConcept root = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
    Optional<SnomedReferenceSetMember> axiomMember = root.getMembers().getItems().stream().filter(m -> m.getRefsetId().equals(Concepts.REFSET_OWL_AXIOM)).findFirst();
    assertTrue(axiomMember.isPresent());
    assertEquals("ec2cc6be-a10b-44b1-a2cc-42a3f11d406e", axiomMember.get().getId());
    assertEquals(Concepts.MODULE_SCT_CORE, axiomMember.get().getModuleId());
    assertEquals(Concepts.REFSET_OWL_AXIOM, axiomMember.get().getRefsetId());
    assertEquals(Concepts.ROOT_CONCEPT, axiomMember.get().getReferencedComponent().getId());
    assertEquals(OWL_EXPRESSION, axiomMember.get().getProperties().get(SnomedRf2Headers.FIELD_OWL_EXPRESSION));
    Optional<SnomedReferenceSetMember> ontologyMember = root.getMembers().getItems().stream().filter(m -> m.getRefsetId().equals(Concepts.REFSET_OWL_ONTOLOGY)).findFirst();
    assertTrue(ontologyMember.isPresent());
    assertEquals("f81c24fb-c40a-4b28-9adb-85f748f71395", ontologyMember.get().getId());
    assertEquals(Concepts.MODULE_SCT_CORE, ontologyMember.get().getModuleId());
    assertEquals(Concepts.REFSET_OWL_ONTOLOGY, ontologyMember.get().getRefsetId());
    assertEquals(Concepts.ROOT_CONCEPT, ontologyMember.get().getReferencedComponent().getId());
    assertEquals("Ontology(<http://snomed.info/sct/900000000000207008>)", ontologyMember.get().getProperties().get(SnomedRf2Headers.FIELD_OWL_EXPRESSION));
}
Also used : MethodSorters(org.junit.runners.MethodSorters) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) RemoteJobState(com.b2international.snowowl.core.jobs.RemoteJobState) ValidatableResponse(io.restassured.response.ValidatableResponse) CodeSystemVersionRestRequests.createVersion(com.b2international.snowowl.test.commons.codesystem.CodeSystemVersionRestRequests.createVersion) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RestExtensions.lastPathSegment(com.b2international.snowowl.test.commons.rest.RestExtensions.lastPathSegment) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedComponentType(com.b2international.snowowl.snomed.core.rest.SnomedComponentType) SnomedComponentRestRequests.getComponent(com.b2international.snowowl.snomed.core.rest.SnomedComponentRestRequests.getComponent) Map(java.util.Map) RestExtensions.givenAuthenticatedRequest(com.b2international.snowowl.test.commons.rest.RestExtensions.givenAuthenticatedRequest) StreamSupport(java.util.stream.StreamSupport) NotFoundException(com.b2international.commons.exceptions.NotFoundException) Rf2ReleaseType(com.b2international.snowowl.snomed.core.domain.Rf2ReleaseType) CodeSystemVersionRestRequests.assertGetVersion(com.b2international.snowowl.test.commons.codesystem.CodeSystemVersionRestRequests.assertGetVersion) BranchPathUtils(com.b2international.snowowl.core.branch.BranchPathUtils) SnomedApiTestConstants(com.b2international.snowowl.snomed.core.rest.SnomedApiTestConstants) DateFormats(com.b2international.snowowl.core.date.DateFormats) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) SnomedImportRestRequests.doImport(com.b2international.snowowl.snomed.core.rest.SnomedImportRestRequests.doImport) CodeSystemRestRequests.createCodeSystem(com.b2international.snowowl.test.commons.codesystem.CodeSystemRestRequests.createCodeSystem) TimeUnit(java.util.concurrent.TimeUnit) SnomedImportRestRequests.waitForImportJob(com.b2international.snowowl.snomed.core.rest.SnomedImportRestRequests.waitForImportJob) List(java.util.List) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) IComponent(com.b2international.snowowl.core.domain.IComponent) Optional(java.util.Optional) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) Assert(org.junit.Assert) FixMethodOrder(org.junit.FixMethodOrder) Collections(java.util.Collections) IBranchPath(com.b2international.snowowl.core.api.IBranchPath) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Test(org.junit.Test) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)

Example 13 with SnomedReferenceSetMember

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

the class SnomedImportApiTest method import27MRCMReferenceSetMembers.

@Test
public void import27MRCMReferenceSetMembers() throws Exception {
    SnomedConcept rootConcept = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
    assertTrue(StreamSupport.stream(rootConcept.getMembers().spliterator(), false).noneMatch(m -> {
        return m.getRefsetId().equals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL) || m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_DOMAIN_INTERNATIONAL) || m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_RANGE_INTERNATIONAL) || m.getRefsetId().equals(Concepts.REFSET_MRCM_MODULE_SCOPE);
    }));
    importArchive("SnomedCT_Release_INT_20170731_new_mrcm_members.zip");
    SnomedConcept newRootConcept = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
    Optional<SnomedReferenceSetMember> mrcmDomainMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL)).findFirst();
    assertTrue(mrcmDomainMemberCandidate.isPresent());
    SnomedReferenceSetMember mrcmDomainMember = mrcmDomainMemberCandidate.get();
    assertEquals("28ecaa32-8f0e-4ff8-b6b1-b642e40519d8", mrcmDomainMember.getId());
    assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrcmDomainMember.getModuleId());
    assertEquals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL, mrcmDomainMember.getRefsetId());
    assertEquals(Concepts.ROOT_CONCEPT, mrcmDomainMember.getReferencedComponent().getId());
    Map<String, Object> domainMemberProps = mrcmDomainMember.getProperties();
    assertEquals("domainConstraint", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_CONSTRAINT));
    assertEquals("parentDomain", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_PARENT_DOMAIN));
    assertEquals("proximalPrimitiveConstraint", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_PROXIMAL_PRIMITIVE_CONSTRAINT));
    assertEquals("proximalPrimitiveRefinement", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_PROXIMAL_PRIMITIVE_REFINEMENT));
    assertEquals("domainTemplateForPrecoordination", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_TEMPLATE_FOR_PRECOORDINATION));
    assertEquals("domainTemplateForPostcoordination", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_TEMPLATE_FOR_POSTCOORDINATION));
    assertEquals("guideURL", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_GUIDEURL));
    Optional<SnomedReferenceSetMember> mrcmAttributeDomainMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_DOMAIN_INTERNATIONAL)).findFirst();
    assertTrue(mrcmAttributeDomainMemberCandidate.isPresent());
    SnomedReferenceSetMember mrcmAttributeDomainMember = mrcmAttributeDomainMemberCandidate.get();
    assertEquals("126bf3f1-4f34-439d-ba0a-a832824d072a", mrcmAttributeDomainMember.getId());
    assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrcmAttributeDomainMember.getModuleId());
    assertEquals(Concepts.REFSET_MRCM_ATTRIBUTE_DOMAIN_INTERNATIONAL, mrcmAttributeDomainMember.getRefsetId());
    assertEquals(Concepts.ROOT_CONCEPT, mrcmAttributeDomainMember.getReferencedComponent().getId());
    Map<String, Object> attributeDomainMemberProps = mrcmAttributeDomainMember.getProperties();
    assertEquals(Concepts.ROOT_CONCEPT, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_ID));
    assertEquals(Boolean.TRUE, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_GROUPED));
    assertEquals("attributeCardinality", attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_CARDINALITY));
    assertEquals("attributeInGroupCardinality", attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_IN_GROUP_CARDINALITY));
    assertEquals(Concepts.ROOT_CONCEPT, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID));
    assertEquals(Concepts.ROOT_CONCEPT, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID));
    Optional<SnomedReferenceSetMember> mrcmAttributeRangeMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_RANGE_INTERNATIONAL)).findFirst();
    assertTrue(mrcmAttributeRangeMemberCandidate.isPresent());
    SnomedReferenceSetMember mrcmAttributeRangeMember = mrcmAttributeRangeMemberCandidate.get();
    assertEquals("ae090cc3-2827-4e39-80c6-364435d30c17", mrcmAttributeRangeMember.getId());
    assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrcmAttributeRangeMember.getModuleId());
    assertEquals(Concepts.REFSET_MRCM_ATTRIBUTE_RANGE_INTERNATIONAL, mrcmAttributeRangeMember.getRefsetId());
    assertEquals(Concepts.ROOT_CONCEPT, mrcmAttributeRangeMember.getReferencedComponent().getId());
    Map<String, Object> attributeRangeMemberProps = mrcmAttributeRangeMember.getProperties();
    assertEquals("rangeConstraint", attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT));
    assertEquals("attributeRule", attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_RULE));
    assertEquals(Concepts.ROOT_CONCEPT, attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID));
    assertEquals(Concepts.ROOT_CONCEPT, attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID));
    Optional<SnomedReferenceSetMember> mrcmModuleScopeMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_MODULE_SCOPE)).findFirst();
    assertTrue(mrcmModuleScopeMemberCandidate.isPresent());
    SnomedReferenceSetMember mrmcModuleScopeMember = mrcmModuleScopeMemberCandidate.get();
    assertEquals("52d29f1b-f7a3-4a0f-828c-383c6259c3f5", mrmcModuleScopeMember.getId());
    assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrmcModuleScopeMember.getModuleId());
    assertEquals(Concepts.REFSET_MRCM_MODULE_SCOPE, mrmcModuleScopeMember.getRefsetId());
    assertEquals(Concepts.ROOT_CONCEPT, mrmcModuleScopeMember.getReferencedComponent().getId());
    assertEquals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL, mrmcModuleScopeMember.getProperties().get(SnomedRf2Headers.FIELD_MRCM_RULE_REFSET_ID));
}
Also used : MethodSorters(org.junit.runners.MethodSorters) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) RemoteJobState(com.b2international.snowowl.core.jobs.RemoteJobState) ValidatableResponse(io.restassured.response.ValidatableResponse) CodeSystemVersionRestRequests.createVersion(com.b2international.snowowl.test.commons.codesystem.CodeSystemVersionRestRequests.createVersion) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RestExtensions.lastPathSegment(com.b2international.snowowl.test.commons.rest.RestExtensions.lastPathSegment) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedComponentType(com.b2international.snowowl.snomed.core.rest.SnomedComponentType) SnomedComponentRestRequests.getComponent(com.b2international.snowowl.snomed.core.rest.SnomedComponentRestRequests.getComponent) Map(java.util.Map) RestExtensions.givenAuthenticatedRequest(com.b2international.snowowl.test.commons.rest.RestExtensions.givenAuthenticatedRequest) StreamSupport(java.util.stream.StreamSupport) NotFoundException(com.b2international.commons.exceptions.NotFoundException) Rf2ReleaseType(com.b2international.snowowl.snomed.core.domain.Rf2ReleaseType) CodeSystemVersionRestRequests.assertGetVersion(com.b2international.snowowl.test.commons.codesystem.CodeSystemVersionRestRequests.assertGetVersion) BranchPathUtils(com.b2international.snowowl.core.branch.BranchPathUtils) SnomedApiTestConstants(com.b2international.snowowl.snomed.core.rest.SnomedApiTestConstants) DateFormats(com.b2international.snowowl.core.date.DateFormats) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) SnomedImportRestRequests.doImport(com.b2international.snowowl.snomed.core.rest.SnomedImportRestRequests.doImport) CodeSystemRestRequests.createCodeSystem(com.b2international.snowowl.test.commons.codesystem.CodeSystemRestRequests.createCodeSystem) TimeUnit(java.util.concurrent.TimeUnit) SnomedImportRestRequests.waitForImportJob(com.b2international.snowowl.snomed.core.rest.SnomedImportRestRequests.waitForImportJob) List(java.util.List) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) IComponent(com.b2international.snowowl.core.domain.IComponent) Optional(java.util.Optional) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) Assert(org.junit.Assert) FixMethodOrder(org.junit.FixMethodOrder) Collections(java.util.Collections) IBranchPath(com.b2international.snowowl.core.api.IBranchPath) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Test(org.junit.Test) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)

Example 14 with SnomedReferenceSetMember

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

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

the class BranchCompareRequestTest method prepareBranchWithNewChanges.

private Set<ComponentIdentifier> prepareBranchWithNewChanges(String branchPath) {
    final IBranchPath branch = BranchPathUtils.createPath(branchPath);
    final String newConceptId = createNewConcept(branch);
    final SnomedConcept concept = getComponent(branch, SnomedComponentType.CONCEPT, newConceptId, "descriptions(expand(members())),relationships()").extract().as(SnomedConcept.class);
    final Set<ComponentIdentifier> newIds = newHashSet();
    newIds.add(ComponentIdentifier.of(SnomedConcept.TYPE, concept.getId()));
    for (SnomedDescription description : concept.getDescriptions()) {
        newIds.add(ComponentIdentifier.of(SnomedDescription.TYPE, description.getId()));
        for (SnomedReferenceSetMember member : description.getMembers()) {
            newIds.add(ComponentIdentifier.of(SnomedReferenceSetMember.TYPE, member.getId()));
        }
    }
    for (SnomedRelationship relationship : concept.getRelationships()) {
        newIds.add(ComponentIdentifier.of(SnomedRelationship.TYPE, relationship.getId()));
    }
    return newIds;
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) IBranchPath(com.b2international.snowowl.core.api.IBranchPath)

Aggregations

SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)41 Test (org.junit.Test)17 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)13 Json (com.b2international.commons.json.Json)12 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)11 SnomedReferenceSetMembers (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers)11 Options (com.b2international.commons.options.Options)8 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)8 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)8 List (java.util.List)8 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)7 Map (java.util.Map)7 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)6 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)5 BranchContext (com.b2international.snowowl.core.domain.BranchContext)5 TransactionContext (com.b2international.snowowl.core.domain.TransactionContext)5 ComponentCategory (com.b2international.snowowl.core.terminology.ComponentCategory)5 Collectors (java.util.stream.Collectors)5 BranchPathUtils (com.b2international.snowowl.core.branch.BranchPathUtils)4 EffectiveTimes (com.b2international.snowowl.core.date.EffectiveTimes)4