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;
}
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());
}
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);
}
}
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);
}
}
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;
}
Aggregations