use of com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder in project snow-owl by b2ihealthcare.
the class Rf2TransactionContext method add.
void add(Collection<SnomedComponent> componentChanges, Multimap<Class<? extends SnomedDocument>, String> dependenciesByType) {
final Multimap<Class<? extends SnomedDocument>, SnomedComponent> componentChangesByType = Multimaps.index(componentChanges, this::getDocType);
for (Class<? extends SnomedDocument> type : IMPORT_ORDER) {
final Collection<SnomedComponent> rf2Components = componentChangesByType.get(type);
final Set<String> componentsToLookup = rf2Components.stream().map(IComponent::getId).collect(Collectors.toSet());
// add all dependencies with the same type
componentsToLookup.addAll(dependenciesByType.get(type));
final Map<String, ? extends SnomedDocument> existingComponents = lookup(componentsToLookup, type);
final Map<String, SnomedConceptDocument> existingRefSets;
if (SnomedRefSetMemberIndexEntry.class == type) {
existingRefSets = lookup(rf2Components.stream().map(member -> ((SnomedReferenceSetMember) member).getRefsetId()).collect(Collectors.toSet()), SnomedConceptDocument.class);
} else {
existingRefSets = Collections.emptyMap();
}
final Set<String> newRefSetIds = newHashSet();
// seed missing component before applying row changes
// and check for existing components with the same or greater effective time and skip them
final Collection<SnomedComponent> componentsToImport = newArrayList();
for (SnomedComponent rf2Component : rf2Components) {
SnomedDocument existingObject = existingComponents.get(rf2Component.getId());
if (existingObject == null) {
// new component, add to new components and register row for import
newComponents.put(rf2Component.getId(), createIdOnlyDoc(rf2Component.getId(), type));
componentsToImport.add(rf2Component);
} else if (existingObject instanceof SnomedDocument && rf2Component instanceof SnomedComponent) {
final SnomedComponent rf2Row = (SnomedComponent) rf2Component;
final SnomedDocument existingRow = (SnomedDocument) existingObject;
if (rf2Row.getEffectiveTime() == null || EffectiveTimes.getEffectiveTime(rf2Row.getEffectiveTime()) > existingRow.getEffectiveTime()) {
componentsToImport.add(rf2Component);
}
}
// check and register refset props on concept docs
if (rf2Component instanceof SnomedReferenceSetMember) {
final SnomedReferenceSetMember member = (SnomedReferenceSetMember) rf2Component;
// seed the refset if missing
final String refSetId = member.getRefsetId();
SnomedConceptDocument conceptDocToUpdate = existingRefSets.get(refSetId);
if (conceptDocToUpdate == null || newComponents.containsKey(refSetId)) {
conceptDocToUpdate = (SnomedConceptDocument) newComponents.get(refSetId);
}
if (conceptDocToUpdate.getRefSetType() == null) {
final String referencedComponentType = SnomedComponent.getType(member.getReferencedComponentId());
String mapTargetComponentType = TerminologyRegistry.UNKNOWN_COMPONENT_TYPE;
try {
mapTargetComponentType = SnomedComponent.getType((String) member.getProperties().get(SnomedRf2Headers.FIELD_MAP_TARGET));
} catch (IllegalArgumentException e) {
// ignored
}
final SnomedReferenceSet refSet = new SnomedReferenceSet();
refSet.setType(member.type());
refSet.setReferencedComponentType(referencedComponentType);
refSet.setMapTargetComponentType(mapTargetComponentType);
final SnomedConceptDocument updatedConcept = SnomedConceptDocument.builder(conceptDocToUpdate).refSet(refSet).build();
if (newComponents.containsKey(refSetId)) {
newComponents.put(refSetId, updatedConcept);
newRefSetIds.add(refSetId);
} else {
update(conceptDocToUpdate, updatedConcept);
}
}
}
}
// apply row changes
for (SnomedComponent rf2Component : componentsToImport) {
final String id = rf2Component.getId();
SnomedDocument existingRevision = null;
SnomedDocument.Builder<?, ?> newRevision;
if (newComponents.containsKey(id)) {
newRevision = createDocBuilder(id, type, newComponents.get(id));
} else if (existingComponents.containsKey(id)) {
existingRevision = existingComponents.get(id);
newRevision = createDocBuilder(id, type, existingRevision);
} else {
throw new IllegalStateException(String.format("Current revision is null for %s", id));
}
final SnomedComponentBuilder builder;
if (rf2Component instanceof SnomedCoreComponent) {
builder = prepareCoreComponent(rf2Component);
} else if (rf2Component instanceof SnomedReferenceSetMember) {
builder = prepareMember((SnomedReferenceSetMember) rf2Component);
} else {
throw new UnsupportedOperationException("Unsupported component: " + rf2Component);
}
// apply row changes
builder.init(newRevision, this);
if (existingRevision == null) {
// in this case the component is new, and the default values are okay to use
add(newRevision.build());
} else {
// in this case, recalculate the released flag based on the currently available revision
if (existingRevision.isReleased()) {
update(existingRevision, newRevision.released(existingRevision.isReleased()).build());
} else {
update(existingRevision, newRevision.build());
}
}
}
// make sure we always attach refset properties to identifier concepts
final StagingArea staging = service(StagingArea.class);
for (String newRefSetId : newRefSetIds) {
SnomedConceptDocument newRefSet = (SnomedConceptDocument) newComponents.get(newRefSetId);
SnomedConceptDocument stagedNewRefSet = (SnomedConceptDocument) staging.getNewObject(SnomedConceptDocument.class, newRefSetId);
if (newRefSet != null && stagedNewRefSet != null) {
if (stagedNewRefSet.getRefSetType() == null) {
add(SnomedConceptDocument.builder(stagedNewRefSet).refSetType(newRefSet.getRefSetType()).referencedComponentType(newRefSet.getReferencedComponentType()).mapTargetComponentType(newRefSet.getMapTargetComponentType()).build());
}
}
}
}
}
Aggregations