Search in sources :

Example 1 with IComponent

use of com.b2international.snowowl.core.domain.IComponent in project snow-owl by b2ihealthcare.

the class SnomedReferenceSetMemberConverter method expandTargetComponent.

private void expandTargetComponent(List<SnomedReferenceSetMember> results) {
    if (expand().containsKey(SnomedReferenceSetMember.Expand.TARGET_COMPONENT)) {
        final Options expandOptions = expand().get(SnomedReferenceSetMember.Expand.TARGET_COMPONENT, Options.class);
        final Multimap<String, SnomedReferenceSetMember> membersByTargetComponent = HashMultimap.create();
        for (SnomedReferenceSetMember member : results) {
            final Map<String, Object> props = member.getProperties();
            if (props.containsKey(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID)) {
                membersByTargetComponent.put(((String) props.get(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID)), member);
            }
        }
        final Multimap<ComponentCategory, String> targetComponentIdsByCategory = Multimaps.index(membersByTargetComponent.keySet(), new Function<String, ComponentCategory>() {

            @Override
            public ComponentCategory apply(String id) {
                return SnomedIdentifiers.getComponentCategory(id);
            }
        });
        for (ComponentCategory category : targetComponentIdsByCategory.keySet()) {
            final Collection<String> targetComponentIds = targetComponentIdsByCategory.get(category);
            final Map<String, ? extends SnomedCoreComponent> componentsById = Maps.uniqueIndex(getComponents(category, targetComponentIds, expandOptions.get("expand", Options.class)), IComponent::getId);
            for (String targetComponentId : targetComponentIds) {
                final SnomedCoreComponent targetComponent = componentsById.get(targetComponentId);
                if (targetComponent != null) {
                    for (SnomedReferenceSetMember member : membersByTargetComponent.get(targetComponentId)) {
                        final Map<String, Object> newProps = newHashMap(member.getProperties());
                        newProps.put(SnomedReferenceSetMember.Expand.TARGET_COMPONENT, targetComponent);
                        ((SnomedReferenceSetMember) member).setProperties(newProps);
                    }
                }
            }
        }
    }
}
Also used : Options(com.b2international.commons.options.Options) SnomedCoreComponent(com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent) IComponent(com.b2international.snowowl.core.domain.IComponent) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)

Example 2 with IComponent

use of com.b2international.snowowl.core.domain.IComponent in project snow-owl by b2ihealthcare.

the class EvaluateQueryRefSetMemberRequest method execute.

@Override
public QueryRefSetMemberEvaluation execute(BranchContext context) {
    // TODO support pre-population???
    final boolean active;
    final String query;
    final String targetReferenceSet;
    if (context instanceof TransactionContext) {
        SnomedRefSetMemberIndexEntry member = ((TransactionContext) context).lookup(memberId, SnomedRefSetMemberIndexEntry.class);
        query = member.getQuery();
        targetReferenceSet = member.getReferencedComponentId();
        active = member.isActive();
    } else {
        final SnomedReferenceSetMember member = SnomedRequests.prepareGetMember(memberId).build().execute(context);
        query = (String) member.getProperties().get(SnomedRf2Headers.FIELD_QUERY);
        targetReferenceSet = member.getReferencedComponent().getId();
        active = member.isActive();
    }
    if (!active) {
        return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, Collections.emptyList());
    }
    if (Strings.isNullOrEmpty(query)) {
        return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, Collections.emptyList());
    }
    // add all matching first
    final Map<String, SnomedConcept> conceptsToAdd = newHashMap();
    // GET matching members of a query
    SnomedRequests.prepareSearchConcept().filterByEcl(query).setLimit(10_000).stream(context).flatMap(SnomedConcepts::stream).forEach(match -> conceptsToAdd.put(match.getId(), match));
    final Collection<SnomedReferenceSetMember> membersToRemove = newHashSet();
    final Collection<SnomedReferenceSetMember> conceptsToActivate = newHashSet();
    // then re-evaluate all current members of the target simple type reference set
    SnomedRequests.prepareSearchMember().filterByRefSet(targetReferenceSet).setLimit(10_000).stream(context).flatMap(SnomedReferenceSetMembers::stream).forEach(member -> {
        final String referencedComponentId = member.getReferencedComponent().getId();
        if (conceptsToAdd.containsKey(referencedComponentId)) {
            if (!member.isActive()) {
                conceptsToAdd.remove(referencedComponentId);
                conceptsToActivate.add(member);
            } else {
                conceptsToAdd.remove(referencedComponentId);
            }
        } else {
            if (member.isActive()) {
                membersToRemove.add(member);
            }
        }
    });
    // fetch all referenced components
    final Set<String> referencedConceptIds = newHashSet();
    referencedConceptIds.addAll(conceptsToAdd.keySet());
    referencedConceptIds.addAll(FluentIterable.from(membersToRemove).transform(SnomedReferenceSetMember::getReferencedComponent).transform(IComponent::getId).toSet());
    referencedConceptIds.addAll(FluentIterable.from(conceptsToActivate).transform(SnomedReferenceSetMember::getReferencedComponent).transform(IComponent::getId).toSet());
    final Map<String, SnomedConcept> concepts;
    if (expand().containsKey("referencedComponent")) {
        final Options expandOptions = expand().getOptions("referencedComponent");
        concepts = Maps.uniqueIndex(SnomedRequests.prepareSearchConcept().filterByIds(referencedConceptIds).setLimit(referencedConceptIds.size()).setExpand(expandOptions.getOptions("expand")).setLocales(locales()).build().execute(context), IComponent::getId);
    } else {
        // initialize with empty SnomedConcept resources
        concepts = newHashMap();
        for (String referencedConceptId : referencedConceptIds) {
            concepts.put(referencedConceptId, new SnomedConcept(referencedConceptId));
        }
    }
    final Collection<MemberChange> changes = newArrayList();
    for (String id : conceptsToAdd.keySet()) {
        changes.add(MemberChangeImpl.added(concepts.get(id)));
    }
    for (SnomedReferenceSetMember memberToRemove : membersToRemove) {
        changes.add(MemberChangeImpl.removed(concepts.get(memberToRemove.getReferencedComponent().getId()), memberToRemove.getId()));
    }
    for (SnomedReferenceSetMember conceptToActivate : conceptsToActivate) {
        changes.add(MemberChangeImpl.changed(concepts.get(conceptToActivate.getReferencedComponent().getId()), conceptToActivate.getId()));
    }
    return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, changes);
}
Also used : Options(com.b2international.commons.options.Options) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext)

Example 3 with IComponent

use of com.b2international.snowowl.core.domain.IComponent 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());
                }
            }
        }
    }
}
Also used : EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) SnomedComponentBuilder(com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) ImmutableList(com.google.common.collect.ImmutableList) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ComponentNotFoundException(com.b2international.snowowl.core.exceptions.ComponentNotFoundException) ISnomedIdentifierService(com.b2international.snowowl.snomed.cis.ISnomedIdentifierService) SnomedComponents(com.b2international.snowowl.snomed.core.store.SnomedComponents) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Commit(com.b2international.index.revision.Commit) SnomedMemberBuilder(com.b2international.snowowl.snomed.core.store.SnomedMemberBuilder) Collectors(java.util.stream.Collectors) TerminologyRegistry(com.b2international.snowowl.core.terminology.TerminologyRegistry) RepositoryTransactionContext(com.b2international.snowowl.core.repository.RepositoryTransactionContext) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) LocalDate(java.time.LocalDate) StagingArea(com.b2international.index.revision.StagingArea) DelegatingTransactionContext(com.b2international.snowowl.core.domain.DelegatingTransactionContext) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) StagingArea(com.b2international.index.revision.StagingArea) SnomedComponentBuilder(com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet)

Aggregations

IComponent (com.b2international.snowowl.core.domain.IComponent)3 Options (com.b2international.commons.options.Options)2 TransactionContext (com.b2international.snowowl.core.domain.TransactionContext)2 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)2 Commit (com.b2international.index.revision.Commit)1 StagingArea (com.b2international.index.revision.StagingArea)1 EffectiveTimes (com.b2international.snowowl.core.date.EffectiveTimes)1 DelegatingTransactionContext (com.b2international.snowowl.core.domain.DelegatingTransactionContext)1 ComponentNotFoundException (com.b2international.snowowl.core.exceptions.ComponentNotFoundException)1 RepositoryTransactionContext (com.b2international.snowowl.core.repository.RepositoryTransactionContext)1 ComponentCategory (com.b2international.snowowl.core.terminology.ComponentCategory)1 TerminologyRegistry (com.b2international.snowowl.core.terminology.TerminologyRegistry)1 ISnomedIdentifierService (com.b2international.snowowl.snomed.cis.ISnomedIdentifierService)1 SnomedIdentifiers (com.b2international.snowowl.snomed.cis.SnomedIdentifiers)1 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)1 com.b2international.snowowl.snomed.core.domain (com.b2international.snowowl.snomed.core.domain)1 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)1 SnomedCoreComponent (com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent)1 SnomedReferenceSet (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet)1 SnomedComponentBuilder (com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder)1