Search in sources :

Example 16 with SnomedReferenceSetMember

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

the class SnomedCompareRestRequestTest method prepareNewChanges.

private Set<ComponentIdentifier> prepareNewChanges(String conceptId, IBranchPath branchPath) {
    final SnomedConcept concept = getComponent(branchPath, SnomedComponentType.CONCEPT, conceptId, "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)

Example 17 with SnomedReferenceSetMember

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

the class SnomedConceptMapSearchRequestEvaluator method toCollectionResource.

private ConceptMapMappings toCollectionResource(SnomedReferenceSetMembers referenceSetMembers, ResourceURI uri, ServiceProvider context, Options search, SnomedDisplayTermType snomedDisplayTermType) {
    final Set<String> refSetsToFetch = referenceSetMembers.stream().map(SnomedReferenceSetMember::getRefsetId).collect(Collectors.toSet());
    final Map<String, SnomedConcept> refSetsById = SnomedRequests.prepareSearchConcept().all().filterByIds(refSetsToFetch).setLocales(search.getList(OptionKey.LOCALES, ExtendedLocale.class)).setExpand("pt(),referenceSet()").build(uri).execute(context.service(IEventBus.class)).getSync(1, TimeUnit.MINUTES).stream().collect(Collectors.toMap(SnomedConcept::getId, concept -> concept));
    final Map<String, ComponentURI> targetComponentsByRefSetId = getTargetComponentsByRefSetId(context, refSetsById);
    List<ConceptMapMapping> mappings = referenceSetMembers.stream().filter(m -> SnomedConcept.TYPE.equals(m.getReferencedComponent().getComponentType())).map(m -> {
        return toMapping(m, uri, targetComponentsByRefSetId.get(m.getRefsetId()), snomedDisplayTermType, refSetsById);
    }).collect(Collectors.toList());
    if (!mappings.isEmpty()) {
        final Map<String, Concept> conceptsById = Multimaps.index(mappings, mapping -> mapping.getTargetComponentURI().resourceUri()).asMap().entrySet().stream().filter(entry -> !TerminologyRegistry.UNSPECIFIED.equals(entry.getKey().getResourceId())).map(entry -> {
            final Set<String> idsToFetch = entry.getValue().stream().map(map -> map.getTargetComponentURI().identifier()).collect(Collectors.toSet());
            return CodeSystemRequests.prepareSearchConcepts().all().filterByCodeSystemUri(entry.getKey()).filterByIds(idsToFetch).buildAsync().execute(context.service(IEventBus.class)).getSync(5, TimeUnit.MINUTES).stream().collect(Collectors.toMap(Concept::getId, c -> c));
        }).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (concept1, concept2) -> concept1));
        mappings = mappings.stream().map(mapping -> {
            final String mapTargetId = mapping.getTargetComponentURI().identifier();
            if (conceptsById.containsKey(mapTargetId) && !mapping.getTargetComponentURI().isUnspecified()) {
                final Concept concept = conceptsById.get(mapTargetId);
                return mapping.toBuilder().targetTerm(concept.getTerm()).targetIconId(concept.getIconId()).build();
            } else {
                return mapping;
            }
        }).collect(Collectors.toList());
    }
    return new ConceptMapMappings(mappings, referenceSetMembers.getSearchAfter(), referenceSetMembers.getLimit(), referenceSetMembers.getTotal());
}
Also used : CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) SnomedRefSetUtil(com.b2international.snowowl.snomed.datastore.SnomedRefSetUtil) ConceptMapMappingSearchRequestEvaluator(com.b2international.snowowl.core.request.ConceptMapMappingSearchRequestEvaluator) SnomedDisplayTermType(com.b2international.snowowl.snomed.core.SnomedDisplayTermType) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Multimaps(com.google.common.collect.Multimaps) Strings(com.google.common.base.Strings) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Options(com.b2international.commons.options.Options) ConceptMapMappings(com.b2international.snowowl.core.domain.ConceptMapMappings) Map(java.util.Map) ResourceURI(com.b2international.snowowl.core.ResourceURI) SnomedReferenceSetMembers(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers) ConceptMapMapping(com.b2international.snowowl.core.domain.ConceptMapMapping) Collection(java.util.Collection) MappingCorrelation(com.b2international.snowowl.core.request.MappingCorrelation) Set(java.util.Set) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) Builder(com.b2international.snowowl.core.domain.ConceptMapMapping.Builder) IEventBus(com.b2international.snowowl.eventbus.IEventBus) Collectors(java.util.stream.Collectors) TerminologyRegistry(com.b2international.snowowl.core.terminology.TerminologyRegistry) TimeUnit(java.util.concurrent.TimeUnit) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) List(java.util.List) Concept(com.b2international.snowowl.core.domain.Concept) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) OptionsBuilder(com.b2international.commons.options.OptionsBuilder) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Concept(com.b2international.snowowl.core.domain.Concept) ConceptMapMapping(com.b2international.snowowl.core.domain.ConceptMapMapping) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) ConceptMapMappings(com.b2international.snowowl.core.domain.ConceptMapMappings)

Example 18 with SnomedReferenceSetMember

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

the class SnomedAssociationTargetUpdateRequest method doExecute.

@Override
protected void doExecute(TransactionContext context, SnomedComponentDocument componentToUpdate) {
    final List<SnomedReferenceSetMember> existingMembers = newArrayList(SnomedRequests.prepareSearchMember().all().filterByReferencedComponent(componentToUpdate.getId()).filterByRefSet("<" + Concepts.REFSET_ASSOCIATION_TYPE).build().execute(context));
    final Multimap<String, String> newAssociationTargetsToCreate = HashMultimap.create(newAssociationTargets);
    final ModuleIdProvider moduleIdFunction = context.service(ModuleIdProvider.class);
    final Iterator<SnomedReferenceSetMember> memberIterator = existingMembers.iterator();
    while (memberIterator.hasNext()) {
        final SnomedReferenceSetMember existingMember = memberIterator.next();
        // existing historical association member
        final String associationReferenceSetId = existingMember.getRefsetId();
        final String existingTargetId = ((String) existingMember.getProperties().get(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID));
        if (newAssociationTargetsToCreate.remove(associationReferenceSetId, existingTargetId)) {
            // Exact match, just make sure that the member is active and remove it from the working list
            final Builder updatedMember = SnomedRefSetMemberIndexEntry.builder(existingMember);
            final SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
            ensureMemberActive(context, existingMember, updatedMember);
            context.update(oldRevision, updatedMember.build());
            memberIterator.remove();
        }
    }
    /* 
		 * Remaining entries in the multimap have no exact match, but there might be other members with the 
		 * same association type; attempt re-using them, changing the association target in the process 
		 */
    for (final SnomedReferenceSetMember existingMember : existingMembers) {
        final String associationReferenceSetId = existingMember.getRefsetId();
        final Builder updatedMember = SnomedRefSetMemberIndexEntry.builder(existingMember);
        if (newAssociationTargetsToCreate.containsKey(associationReferenceSetId)) {
            // We can re-use the member by changing the target component identifier, and checking that it is active
            final Iterator<String> targetIterator = newAssociationTargetsToCreate.get(associationReferenceSetId).iterator();
            final String newTargetId = targetIterator.next();
            targetIterator.remove();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Changing association member {} with type {} and target component identifier from {} to {}.", existingMember.getId(), associationReferenceSetId, ((String) existingMember.getProperties().get(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID)), newTargetId);
            }
            // Change target component, set status to active if needed, place it in the supplied module
            SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
            updatedMember.field(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID, newTargetId);
            ensureMemberActive(context, existingMember, updatedMember);
            updateModule(context, existingMember, updatedMember, moduleIdFunction.apply(componentToUpdate));
            unsetEffectiveTime(existingMember, updatedMember);
            context.update(oldRevision, updatedMember.build());
        } else {
            // We have no use for this member -- remove or inactivate if already released
            SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
            if (removeOrDeactivate(context, existingMember, updatedMember)) {
                context.update(oldRevision, updatedMember.build());
            }
        }
    }
    /* 
		 * With all existing members processed, the last set of entries in the multimap will need 
		 * to be added as new members; defaultModuleId is only used if there is at least a single
		 * new entry. 
		 */
    for (final Entry<String, String> newAssociationEntry : newAssociationTargetsToCreate.entries()) {
        SnomedComponents.newAssociationMember().withRefSet(newAssociationEntry.getKey()).withTargetComponentId(newAssociationEntry.getValue()).withReferencedComponent(componentToUpdate.getId()).withModuleId(moduleIdFunction.apply(componentToUpdate)).addTo(context);
    }
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) ModuleIdProvider(com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider) Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry.Builder)

Example 19 with SnomedReferenceSetMember

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

the class SnomedInactivationReasonUpdateRequest method doExecute.

@Override
protected void doExecute(TransactionContext context, SnomedComponentDocument componentToUpdate) {
    final List<SnomedReferenceSetMember> existingMembers = newArrayList();
    if (!create) {
        existingMembers.addAll(SnomedRequests.prepareSearchMember().all().filterByReferencedComponent(componentToUpdate.getId()).filterByRefSet(inactivationRefSetId).build().execute(context).getItems());
    }
    boolean firstMemberFound = false;
    // Check if there is at least one existing member
    for (SnomedReferenceSetMember existingMember : existingMembers) {
        final SnomedRefSetMemberIndexEntry.Builder updatedMember = SnomedRefSetMemberIndexEntry.builder(existingMember);
        final SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
        if (firstMemberFound) {
            // If we got through the first iteration, all other members can be removed
            if (removeOrDeactivate(context, existingMember, updatedMember)) {
                context.update(oldRevision, updatedMember.build());
            }
            continue;
        }
        boolean changed = false;
        final String existingValueId = (String) existingMember.getProperties().get(SnomedRf2Headers.FIELD_VALUE_ID);
        if (Objects.equals(existingValueId, inactivationValueId)) {
            // Exact match, just make sure that the member is active
            changed = ensureMemberActive(context, existingMember, updatedMember);
        } else if (!CLEAR.equals(inactivationValueId)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Changing attribute-value member {} with value identifier from {} to {}.", existingMember.getId(), existingValueId, inactivationValueId);
            }
            // Change inactivation value, set status to active if needed, place it in the supplied module
            ensureMemberActive(context, existingMember, updatedMember);
            updatedMember.field(SnomedRf2Headers.FIELD_VALUE_ID, inactivationValueId);
            updateModule(context, existingMember, updatedMember, context.service(ModuleIdProvider.class).apply(componentToUpdate));
            unsetEffectiveTime(existingMember, updatedMember);
            changed = true;
        } else /* if (CLEAR.equals(inactivationValueId) */
        {
            // Inactivation value is set to "no reason given", so remove or inactivate the member
            // If the member needs inactivation, place it in the supplied module
            changed = removeOrDeactivate(context, existingMember, updatedMember);
        }
        // If we get to the end of this loop, the first member has been processed
        if (changed) {
            context.update(oldRevision, updatedMember.build());
        }
        // By the end of this loop, the first member has been processed
        firstMemberFound = true;
    }
    // Add the new member if the intention was not to remove the existing value (which had already happened if so)
    if (!firstMemberFound && !CLEAR.equals(inactivationValueId)) {
        SnomedComponents.newAttributeValueMember().withReferencedComponent(componentToUpdate.getId()).withRefSet(inactivationRefSetId).withModuleId(context.service(ModuleIdProvider.class).apply(componentToUpdate)).withValueId(inactivationValueId).addTo(context);
    }
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) ModuleIdProvider(com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider)

Example 20 with SnomedReferenceSetMember

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

the class QueryRefSetMemberUpdateRequest method execute.

@Override
public Boolean execute(TransactionContext context) {
    // evaluate query member
    final QueryRefSetMemberEvaluation evaluation = SnomedRequests.prepareQueryRefSetMemberEvaluation(memberId).build().execute(context);
    // lookup IDs before applying change to speed up query member update
    final Set<String> referencedComponents = evaluation.getChanges().stream().map(MemberChange::getReferencedComponent).map(SnomedConcept::getId).collect(Collectors.toSet());
    context.lookup(referencedComponents, SnomedConceptDocument.class);
    // apply all change as request on the target reference set
    for (MemberChange change : evaluation.getChanges()) {
        switch(change.getChangeKind()) {
            case ADD:
                SnomedRequests.prepareNewMember().setModuleId(moduleId).setReferencedComponentId(change.getReferencedComponent().getId()).setRefsetId(evaluation.getReferenceSetId()).buildNoContent().execute(context);
                break;
            case REMOVE:
                final SnomedReferenceSetMember member = SnomedRequests.prepareGetMember(change.getMemberId()).build().execute(context);
                if (member.isReleased()) {
                    SnomedRequests.prepareUpdateMember(change.getMemberId()).setSource(Map.of(SnomedRf2Headers.FIELD_ACTIVE, Boolean.FALSE)).build().execute(context);
                } else {
                    SnomedRequests.prepareDeleteMember(change.getMemberId()).build().execute(context);
                }
                break;
            case CHANGE:
                final SnomedReferenceSetMember memberToChange = SnomedRequests.prepareGetMember(change.getMemberId()).build().execute(context);
                if (!memberToChange.isActive()) {
                    SnomedRequests.prepareUpdateMember(change.getMemberId()).setSource(Map.of(SnomedRf2Headers.FIELD_ACTIVE, Boolean.TRUE)).build().execute(context);
                }
                break;
            default:
                throw new UnsupportedOperationException("Not implemented case: " + change.getChangeKind());
        }
    }
    return Boolean.TRUE;
}
Also used : QueryRefSetMemberEvaluation(com.b2international.snowowl.snomed.core.domain.refset.QueryRefSetMemberEvaluation) MemberChange(com.b2international.snowowl.snomed.core.domain.refset.MemberChange) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)

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