Search in sources :

Example 1 with ModuleIdProvider

use of com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider 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 2 with ModuleIdProvider

use of com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider in project snow-owl by b2ihealthcare.

the class ComponentInactivationChangeProcessor method processInactivations.

private void processInactivations(StagingArea staging, RevisionSearcher searcher, Set<String> inactivatedConceptIds, Set<String> inactivatedComponentIds) throws IOException {
    // inactivate descriptions of inactivated concepts, take current description changes into account
    ServiceProvider context = (ServiceProvider) staging.getContext();
    ModuleIdProvider moduleIdProvider = context.service(ModuleIdProvider.class);
    if (!inactivatedConceptIds.isEmpty()) {
        final Multimap<String, RevisionDiff> changedMembersByReferencedComponentId = HashMultimap.create();
        staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).forEach(diff -> {
            changedMembersByReferencedComponentId.put(((SnomedRefSetMemberIndexEntry) diff.newRevision).getReferencedComponentId(), diff);
        });
        // Inactivate active descriptions on inactive concepts
        try {
            Query.select(SnomedDescriptionIndexEntry.class).from(SnomedDescriptionIndexEntry.class).fields(SnomedDescriptionIndexEntry.Fields.ID, SnomedDescriptionIndexEntry.Fields.MODULE_ID).where(Expressions.builder().filter(SnomedDescriptionIndexEntry.Expressions.active()).filter(SnomedDescriptionIndexEntry.Expressions.concepts(inactivatedConceptIds)).build()).limit(PAGE_SIZE).build().stream(searcher).forEachOrdered(hits -> {
                try {
                    inactivateDescriptions(searcher, moduleIdProvider, changedMembersByReferencedComponentId, hits);
                } catch (IOException e) {
                    throw new UndeclaredThrowableException(e);
                }
            });
        } catch (UndeclaredThrowableException ute) {
            // Unwrap and throw checked exception from lambda above
            throw (IOException) ute.getCause();
        }
        final Map<ObjectId, RevisionDiff> changedRevisions = staging.getChangedRevisions();
        // Inactivate active relationships on inactive concepts
        Query.select(SnomedRelationshipIndexEntry.class).where(Expressions.builder().filter(SnomedRelationshipIndexEntry.Expressions.active()).should(SnomedRelationshipIndexEntry.Expressions.sourceIds(inactivatedConceptIds)).should(SnomedRelationshipIndexEntry.Expressions.destinationIds(inactivatedConceptIds)).build()).limit(PAGE_SIZE).build().stream(searcher).flatMap(Hits::stream).forEachOrdered(relationship -> inactivateRelationship(inactivatedComponentIds, moduleIdProvider, changedRevisions, relationship));
    }
    if (inactivatedComponentIds.isEmpty()) {
        return;
    }
    // inactivate referring members of all inactivated core component, and all members of inactivated refsets
    final Map<ObjectId, RevisionDiff> changedRevisions = staging.getChangedRevisions();
    Query.select(SnomedRefSetMemberIndexEntry.class).where(Expressions.builder().filter(SnomedRefSetMemberIndexEntry.Expressions.active()).should(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(inactivatedComponentIds)).should(SnomedRefSetMemberIndexEntry.Expressions.refsetIds(inactivatedComponentIds)).setMinimumNumberShouldMatch(1).build()).limit(PAGE_SIZE).build().stream(searcher).flatMap(Hits::stream).forEachOrdered(member -> inactivateReferenceSetMember(moduleIdProvider, changedRevisions, member));
}
Also used : ObjectId(com.b2international.index.revision.ObjectId) ModuleIdProvider(com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) RevisionDiff(com.b2international.index.revision.StagingArea.RevisionDiff) IOException(java.io.IOException)

Example 3 with ModuleIdProvider

use of com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider in project snow-owl by b2ihealthcare.

the class SnomedDescriptionAcceptabilityUpdateRequest method doExecute.

@Override
protected void doExecute(TransactionContext context, SnomedComponentDocument componentToUpdate) {
    final Iterable<SnomedReferenceSetMember> existingMembers = create ? Collections.emptySet() : SnomedRequests.prepareSearchMember().all().filterByReferencedComponent(componentToUpdate.getId()).filterByRefSetType(Collections.singleton(SnomedRefSetType.LANGUAGE)).build().execute(context);
    final Map<String, Acceptability> newLanguageMembersToCreate = newHashMap(newAcceptabilityMap);
    final ModuleIdProvider moduleIdSupplier = context.service(ModuleIdProvider.class);
    // check if there are existing matches
    for (SnomedReferenceSetMember existingMember : existingMembers) {
        final String acceptabilityId = (String) existingMember.getProperties().get(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID);
        final Acceptability acceptability = Acceptability.getByConceptId(acceptabilityId);
        final String languageReferenceSetId = existingMember.getRefsetId();
        if (null == acceptability) {
            continue;
        }
        final SnomedRefSetMemberIndexEntry.Builder updatedMember = SnomedRefSetMemberIndexEntry.builder(existingMember);
        final SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
        if (acceptability.equals(newLanguageMembersToCreate.get(languageReferenceSetId))) {
            // Exact match: make sure that the member is active
            if (ensureMemberActive(context, existingMember, updatedMember)) {
                context.update(oldRevision, updatedMember.build());
            }
            // Remove it from the working list, as we have found a match
            newLanguageMembersToCreate.remove(languageReferenceSetId);
        } else if (newLanguageMembersToCreate.containsKey(languageReferenceSetId)) {
            // Change acceptability, set status to active if required, place it in the supplied module
            final Acceptability newAcceptability = newLanguageMembersToCreate.get(languageReferenceSetId);
            updatedMember.field(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID, newAcceptability.getConceptId());
            ensureMemberActive(context, existingMember, updatedMember);
            updateModule(context, existingMember, updatedMember, moduleIdSupplier.apply(componentToUpdate));
            unsetEffectiveTime(existingMember, updatedMember);
            context.update(oldRevision, updatedMember.build());
            newLanguageMembersToCreate.remove(languageReferenceSetId);
        } else {
            // Not acceptable in this language reference set, remove or inactivate if already released
            if (removeOrDeactivate(context, existingMember, updatedMember)) {
                context.update(oldRevision, updatedMember.build());
            }
        }
    }
    for (final Entry<String, Acceptability> languageMemberEntry : newLanguageMembersToCreate.entrySet()) {
        SnomedComponents.newLanguageMember().withAcceptability(languageMemberEntry.getValue()).withRefSet(languageMemberEntry.getKey()).withModuleId(moduleIdSupplier.apply(componentToUpdate)).withReferencedComponent(componentToUpdate.getId()).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) Acceptability(com.b2international.snowowl.snomed.core.domain.Acceptability)

Example 4 with ModuleIdProvider

use of com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider in project snow-owl by b2ihealthcare.

the class ComponentInactivationChangeProcessor method processReactivations.

private void processReactivations(StagingArea staging, RevisionSearcher searcher, Set<String> reactivatedConceptIds, Set<String> reactivatedComponentIds) throws IOException {
    ServiceProvider context = (ServiceProvider) staging.getContext();
    ModuleIdProvider moduleIdProvider = context.service(ModuleIdProvider.class);
    try {
        Query.select(String.class).from(SnomedDescriptionIndexEntry.class).fields(SnomedDescriptionIndexEntry.Fields.ID).where(Expressions.builder().filter(SnomedDescriptionIndexEntry.Expressions.active()).filter(SnomedDescriptionIndexEntry.Expressions.concepts(reactivatedConceptIds)).filter(SnomedDescriptionIndexEntry.Expressions.activeMemberOf(Concepts.REFSET_DESCRIPTION_INACTIVITY_INDICATOR)).build()).limit(PAGE_SIZE).build().stream(searcher).forEachOrdered(hits -> {
            try {
                reactivateDescriptions(staging, searcher, moduleIdProvider, hits);
            } catch (IOException e) {
                throw new UndeclaredThrowableException(e);
            }
        });
    } catch (UndeclaredThrowableException ute) {
        // Unwrap and throw checked exception from lambda above
        throw (IOException) ute.getCause();
    }
}
Also used : ModuleIdProvider(com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) IOException(java.io.IOException)

Aggregations

ModuleIdProvider (com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider)4 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)2 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)2 SnomedRefSetMemberIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)2 IOException (java.io.IOException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 ObjectId (com.b2international.index.revision.ObjectId)1 RevisionDiff (com.b2international.index.revision.StagingArea.RevisionDiff)1 Acceptability (com.b2international.snowowl.snomed.core.domain.Acceptability)1 Builder (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry.Builder)1