Search in sources :

Example 1 with Builder

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry.Builder 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)

Aggregations

SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)1 SnomedRefSetMemberIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)1 Builder (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry.Builder)1 ModuleIdProvider (com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider)1