Search in sources :

Example 1 with ConstantIdStrategy

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

the class IdRequest method execute.

@Override
public R execute(final C context) {
    final IdActionRecorder recorder = new IdActionRecorder(context);
    try {
        final Multimap<ComponentCategory, SnomedComponentCreateRequest> componentCreateRequests = getComponentCreateRequests(next());
        if (!componentCreateRequests.isEmpty()) {
            try {
                for (final ComponentCategory category : componentCreateRequests.keySet()) {
                    final Class<? extends SnomedDocument> documentClass = getDocumentClass(category);
                    final Collection<SnomedComponentCreateRequest> categoryRequests = componentCreateRequests.get(category);
                    final Set<String> coreComponentIds = FluentIterable.from(categoryRequests).filter(SnomedCoreComponentCreateRequest.class).filter(request -> request.getIdGenerationStrategy() instanceof ConstantIdStrategy).transform(request -> ((ConstantIdStrategy) request.getIdGenerationStrategy()).getId()).toSet();
                    final Set<String> refsetMemberIds = FluentIterable.from(categoryRequests).filter(SnomedRefSetMemberCreateRequest.class).transform(request -> request.getId()).toSet();
                    final Set<String> userRequestedIds = Sets.newHashSet(Iterables.concat(coreComponentIds, refsetMemberIds));
                    final Set<String> existingIds = getExistingIds(context, userRequestedIds, documentClass);
                    if (!existingIds.isEmpty()) {
                        // TODO: Report all existing identifiers
                        throw new AlreadyExistsException(category.getDisplayName(), Iterables.getFirst(existingIds, null));
                    } else {
                        if (!coreComponentIds.isEmpty()) {
                            recorder.register(coreComponentIds);
                        }
                    }
                    final Multimap<String, BaseSnomedComponentCreateRequest> requestsByNamespace = HashMultimap.create();
                    // index requests where an explicit namespace identifier is specified (use it directly)
                    categoryRequests.stream().filter(BaseSnomedComponentCreateRequest.class::isInstance).map(BaseSnomedComponentCreateRequest.class::cast).filter(request -> request.getIdGenerationStrategy() instanceof NamespaceIdStrategy).forEach(request -> {
                        requestsByNamespace.put(getNamespaceKey(request), request);
                    });
                    // convert requests that define a namespaceConceptId by extracting the namespaceId from the namespaceConcept's FSN
                    final Multimap<String, BaseSnomedComponentCreateRequest> requestsByNamespaceConceptId = HashMultimap.create();
                    categoryRequests.stream().filter(BaseSnomedComponentCreateRequest.class::isInstance).map(BaseSnomedComponentCreateRequest.class::cast).filter(request -> request.getIdGenerationStrategy() instanceof NamespaceConceptIdStrategy).forEach(request -> {
                        requestsByNamespaceConceptId.put(request.getIdGenerationStrategy().getNamespace(), request);
                    });
                    if (!requestsByNamespaceConceptId.isEmpty()) {
                        final Map<String, String> namespacesByNamespaceConceptId = context.service(NamespaceIdProvider.class).extractNamespaceIds(context, requestsByNamespaceConceptId.keySet(), false);
                        for (String namespaceConceptId : Set.copyOf(requestsByNamespaceConceptId.keySet())) {
                            Collection<BaseSnomedComponentCreateRequest> requests = requestsByNamespaceConceptId.removeAll(namespaceConceptId);
                            if (!namespacesByNamespaceConceptId.containsKey(namespaceConceptId)) {
                                throw new BadRequestException("There is no namespace value associated with the '%s' namespaceConcept's FSN.", namespaceConceptId);
                            }
                            requestsByNamespace.putAll(namespacesByNamespaceConceptId.get(namespaceConceptId), requests);
                        }
                    }
                    for (final String namespace : requestsByNamespace.keySet()) {
                        final String convertedNamespace = SnomedIdentifiers.INT_NAMESPACE.equals(namespace) ? null : namespace;
                        final Collection<BaseSnomedComponentCreateRequest> namespaceRequests = requestsByNamespace.get(namespace);
                        final int count = namespaceRequests.size();
                        final Set<String> uniqueIds = getUniqueIds(context, recorder, category, documentClass, count, convertedNamespace);
                        final Iterator<String> idsToUse = Iterators.consumingIterator(uniqueIds.iterator());
                        for (final BaseSnomedComponentCreateRequest createRequest : namespaceRequests) {
                            createRequest.setIdGenerationStrategy(new ConstantIdStrategy(idsToUse.next()));
                        }
                        checkState(!idsToUse.hasNext(), "More SNOMED CT ids have been requested than used.");
                    }
                }
            } finally {
            // idGenerationSample.stop(registry.timer("idGenerationTime"));
            }
        }
        final R commitInfo = next(context);
        recorder.commit();
        return commitInfo;
    } catch (final Exception e) {
        recorder.rollback();
        throw e;
    }
}
Also used : Query(com.b2international.index.query.Query) AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) IdActionRecorder(com.b2international.snowowl.snomed.cis.action.IdActionRecorder) NamespaceIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceIdStrategy) Map(java.util.Map) BulkRequest(com.b2international.snowowl.core.events.bulk.BulkRequest) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Iterator(java.util.Iterator) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RevisionDocument(com.b2international.snowowl.core.repository.RevisionDocument) Collection(java.util.Collection) Request(com.b2international.snowowl.core.events.Request) Set(java.util.Set) IOException(java.io.IOException) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ConstantIdStrategy(com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy) TransactionalRequest(com.b2international.snowowl.core.request.TransactionalRequest) IdGenerationStrategy(com.b2international.snowowl.snomed.core.domain.IdGenerationStrategy) DelegatingRequest(com.b2international.snowowl.core.events.DelegatingRequest) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) NamespaceConceptIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceConceptIdStrategy) BranchContext(com.b2international.snowowl.core.domain.BranchContext) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) IdActionRecorder(com.b2international.snowowl.snomed.cis.action.IdActionRecorder) AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) IOException(java.io.IOException) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) ConstantIdStrategy(com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy) BadRequestException(com.b2international.commons.exceptions.BadRequestException) NamespaceIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceIdStrategy) NamespaceConceptIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceConceptIdStrategy)

Example 2 with ConstantIdStrategy

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

the class SnomedDescriptionCreateRequest method execute.

@Override
public String execute(TransactionContext context) {
    try {
        final String descriptionId = ((ConstantIdStrategy) getIdGenerationStrategy()).getId();
        final SnomedDescriptionIndexEntry description = SnomedComponents.newDescription().withId(descriptionId).withActive(isActive()).withModuleId(getModuleId()).withCaseSignificanceId(caseSignificanceId).withTerm(term).withType(typeId).withLanguageCode(languageCode).withConcept(conceptId).build(context);
        new SnomedDescriptionAcceptabilityUpdateRequest(description, acceptability, true).execute(context);
        // FIXME: Acceptability updates and member create requests can overlap
        convertMembers(context, descriptionId);
        context.add(description);
        // update inactivation reason members if the new description was immediately set to have an inactivation member
        if (inactivationIndicatorId != null) {
            final SnomedInactivationReasonUpdateRequest inactivationUpdate = new SnomedInactivationReasonUpdateRequest(description, Concepts.REFSET_DESCRIPTION_INACTIVITY_INDICATOR, true);
            inactivationUpdate.setInactivationValueId(inactivationIndicatorId);
            inactivationUpdate.execute(context);
        }
        return description.getId();
    } catch (ComponentNotFoundException e) {
        throw e.toBadRequestException();
    }
}
Also used : ComponentNotFoundException(com.b2international.snowowl.core.exceptions.ComponentNotFoundException) ConstantIdStrategy(com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry)

Example 3 with ConstantIdStrategy

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

the class SnomedRelationshipCreateRequest method execute.

@Override
public String execute(final TransactionContext context) {
    if (Strings.isNullOrEmpty(getSourceId())) {
        throw new BadRequestException("'sourceId' may not be empty");
    }
    if (Strings.isNullOrEmpty(getDestinationId()) && getValue() == null) {
        throw new BadRequestException("'destinationId' or 'value' should be specified");
    }
    if (!Strings.isNullOrEmpty(getDestinationId()) && getValue() != null) {
        throw new BadRequestException("'destinationId' and 'value' can not be set for the same relationship");
    }
    try {
        final String relationshipId = ((ConstantIdStrategy) getIdGenerationStrategy()).getId();
        final SnomedRelationshipIndexEntry relationship = SnomedComponents.newRelationship().withId(relationshipId).withActive(isActive()).withModuleId(getModuleId()).withSourceId(getSourceId()).withTypeId(getTypeId()).withDestinationId(getDestinationId()).withDestinationNegated(isDestinationNegated()).withValue(getValue()).withRelationshipGroup(getRelationshipGroup()).withUnionGroup(getUnionGroup()).withCharacteristicTypeId(getCharacteristicTypeId()).withModifierId(getModifierId()).build(context);
        convertMembers(context, relationshipId);
        context.add(relationship);
        return relationship.getId();
    } catch (final ComponentNotFoundException e) {
        throw e.toBadRequestException();
    }
}
Also used : ComponentNotFoundException(com.b2international.snowowl.core.exceptions.ComponentNotFoundException) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) ConstantIdStrategy(com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Aggregations

ConstantIdStrategy (com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy)3 BadRequestException (com.b2international.commons.exceptions.BadRequestException)2 ComponentNotFoundException (com.b2international.snowowl.core.exceptions.ComponentNotFoundException)2 CompareUtils (com.b2international.commons.CompareUtils)1 AlreadyExistsException (com.b2international.commons.exceptions.AlreadyExistsException)1 NotImplementedException (com.b2international.commons.exceptions.NotImplementedException)1 Query (com.b2international.index.query.Query)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 DelegatingRequest (com.b2international.snowowl.core.events.DelegatingRequest)1 Request (com.b2international.snowowl.core.events.Request)1 BulkRequest (com.b2international.snowowl.core.events.bulk.BulkRequest)1 RevisionDocument (com.b2international.snowowl.core.repository.RevisionDocument)1 TransactionalRequest (com.b2international.snowowl.core.request.TransactionalRequest)1 ComponentCategory (com.b2international.snowowl.core.terminology.ComponentCategory)1 SnomedIdentifiers (com.b2international.snowowl.snomed.cis.SnomedIdentifiers)1 IdActionRecorder (com.b2international.snowowl.snomed.cis.action.IdActionRecorder)1 IdGenerationStrategy (com.b2international.snowowl.snomed.core.domain.IdGenerationStrategy)1 NamespaceConceptIdStrategy (com.b2international.snowowl.snomed.core.domain.NamespaceConceptIdStrategy)1