use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.
the class ConcreteDomainChangeConverter method expand.
@Override
public void expand(final List<ConcreteDomainChange> results) {
if (!expand().containsKey(ConcreteDomainChange.Expand.CONCRETE_DOMAIN_MEMBER)) {
return;
}
/*
* Depending on the CD member change search request, we might need to issue
* SNOMED CT searches against multiple branches; find out which ones we have.
*/
final Multimap<String, ConcreteDomainChange> itemsByBranch = getItemsByBranch(results);
// Check if we only need to load inferred CD members in their entirety
final Options expandOptions = expand().getOptions(ConcreteDomainChange.Expand.CONCRETE_DOMAIN_MEMBER);
final boolean inferredOnly = expandOptions.getBoolean("inferredOnly");
final Options cdMemberExpandOptions = expandOptions.getOptions("expand");
final Options referencedComponentOptions = cdMemberExpandOptions.getOptions(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT);
/*
* Remove this option from the member expand options map, so that member search
* does not expand the referenced component again
*/
final boolean needsReferencedComponent = cdMemberExpandOptions.keySet().remove(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT);
for (final String branch : itemsByBranch.keySet()) {
final Collection<ConcreteDomainChange> itemsForCurrentBranch = itemsByBranch.get(branch);
/*
* Expand referenced component on members via a separate search request, as they
* can be different from the referenced component on the "origin" member
*/
if (needsReferencedComponent) {
final List<ReasonerConcreteDomainMember> blankMembers = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(ConcreteDomainChange::getConcreteDomainMember).collect(Collectors.toList());
final Multimap<String, ReasonerConcreteDomainMember> membersByReferencedComponent = Multimaps.index(blankMembers, ReasonerConcreteDomainMember::getReferencedComponentId);
final Multimap<ComponentCategory, String> referencedComponentsByCategory = Multimaps.index(membersByReferencedComponent.keySet(), SnomedIdentifiers::getComponentCategory);
for (final Entry<ComponentCategory, Collection<String>> categoryEntry : referencedComponentsByCategory.asMap().entrySet()) {
expandComponentCategory(branch, categoryEntry.getKey(), categoryEntry.getValue(), referencedComponentOptions, membersByReferencedComponent);
}
}
/*
* Then fetch all the required members (these will have a referenced component
* ID that should no longer be copied on inferred members). Note that the same "origin"
* member might be used for multiple eg. "new" counterparts.
*/
final Set<String> cdMemberUuids = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(c -> c.getConcreteDomainMember().getOriginMemberId()).collect(Collectors.toSet());
final Request<BranchContext, SnomedReferenceSetMembers> cdMemberSearchRequest = SnomedRequests.prepareSearchMember().filterByIds(cdMemberUuids).setLimit(cdMemberUuids.size()).setExpand(cdMemberExpandOptions).setLocales(locales()).build();
final SnomedReferenceSetMembers cdMembers = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(cdMemberSearchRequest)).execute(context());
final Map<String, SnomedReferenceSetMember> cdMembersByUuid = Maps.uniqueIndex(cdMembers, SnomedReferenceSetMember::getId);
/*
* Finally, set the member on the change item, but preserve the properties that
* were already set in "toResource"
*/
for (final ConcreteDomainChange item : itemsForCurrentBranch) {
final ReasonerConcreteDomainMember reasonerMember = item.getConcreteDomainMember();
final String memberUuid = reasonerMember.getOriginMemberId();
switch(item.getChangeNature()) {
case NEW:
{
final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
final Map<String, Object> expandedProperties = expandedMember.getProperties();
// reasonerMember.setCharacteristicTypeId(...) is already set
// reasonerMember.setGroup(...) is already set
// reasonerMember.setReferencedComponent(...) is already set (or expanded)
reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
// reasonerMember.setReleased(...) is already set
reasonerMember.setSerializedValue((String) expandedProperties.get(SnomedRf2Headers.FIELD_VALUE));
reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
}
break;
case UPDATED:
if (!inferredOnly) {
final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
final Map<String, Object> expandedProperties = expandedMember.getProperties();
reasonerMember.setCharacteristicTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID));
reasonerMember.setGroup((Integer) expandedProperties.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
// reasonerMember.setReferencedComponent(...) is already set (or expanded)
reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
// reasonerMember.setReleased(...) is already set
// reasonerMember.setSerializedValue(...) is already set
reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
}
break;
case REDUNDANT:
if (!inferredOnly) {
final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
final Map<String, Object> expandedProperties = expandedMember.getProperties();
reasonerMember.setCharacteristicTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID));
reasonerMember.setGroup((Integer) expandedProperties.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
// reasonerMember.setReferencedComponent(...) is already set (or expanded)
reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
// reasonerMember.setReleased(...) is already set
reasonerMember.setSerializedValue((String) expandedProperties.get(SnomedRf2Headers.FIELD_VALUE));
reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
}
break;
default:
throw new IllegalStateException(String.format("Unexpected CD member change '%s' found with UUID '%s'.", item.getChangeNature(), item.getConcreteDomainMember().getOriginMemberId()));
}
}
}
}
Aggregations