Search in sources :

Example 1 with SctId

use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method reserveSctIds.

@Override
public Map<String, SctId> reserveSctIds(String namespace, ComponentCategory category, int quantity) {
    checkNotNull(category, "Component category must not be null.");
    checkArgument(quantity > 0, "Number of requested IDs should be non-negative.");
    checkCategory(category);
    LOGGER.debug("Reserving {} component IDs for category {}.", quantity, category.getDisplayName());
    HttpPost reserveRequest = null;
    HttpGet recordsRequest = null;
    try {
        if (quantity > 1) {
            LOGGER.debug("Sending {} ID bulk reservation request.", category.getDisplayName());
            reserveRequest = httpPost(String.format("sct/bulk/reserve?token=%s", getToken()), createBulkReservationData(namespace, category, quantity));
            final String bulkResponse = execute(reserveRequest);
            final String jobId = mapper.readValue(bulkResponse, JsonNode.class).get("id").asText();
            joinBulkJobPolling(jobId, quantity, getToken());
            recordsRequest = httpGet(String.format("bulk/jobs/%s/records?token=%s", jobId, getToken()));
            final String recordsResponse = execute(recordsRequest);
            final JsonNode[] records = mapper.readValue(recordsResponse, JsonNode[].class);
            return readSctIds(getComponentIds(records));
        } else {
            LOGGER.debug("Sending {} ID reservation request.", category.getDisplayName());
            reserveRequest = httpPost(String.format("sct/reserve?token=%s", getToken()), createReservationData(namespace, category));
            final String response = execute(reserveRequest);
            final SctId sctid = mapper.readValue(response, SctId.class);
            return readSctIds(Collections.singleton(sctid.getSctid()));
        }
    } catch (IOException e) {
        throw new SnowowlRuntimeException("Exception while bulk reserving IDs.", e);
    } finally {
        release(reserveRequest);
        release(recordsRequest);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) SctId(com.b2international.snowowl.snomed.cis.domain.SctId)

Example 2 with SctId

use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method publish.

@Override
public Map<String, SctId> publish(final Set<String> componentIds) {
    LOGGER.debug("Publishing {} component IDs.", componentIds.size());
    final Map<String, SctId> sctIds = getSctIds(componentIds);
    HttpPut publishRequest = null;
    String currentNamespace = null;
    try {
        final Map<String, SctId> sctIdsToPublish = ImmutableMap.copyOf(Maps.filterValues(sctIds, Predicates.not(SctId::isPublished)));
        if (!sctIdsToPublish.isEmpty()) {
            if (sctIdsToPublish.size() > 1) {
                final Multimap<String, String> componentIdsByNamespace = toNamespaceMultimap(sctIdsToPublish.keySet());
                for (final Entry<String, Collection<String>> entry : componentIdsByNamespace.asMap().entrySet()) {
                    currentNamespace = entry.getKey();
                    for (final Collection<String> bulkIds : Iterables.partition(entry.getValue(), requestBulkLimit)) {
                        LOGGER.debug("Sending bulk publication request for namespace {} with size {}.", currentNamespace, bulkIds.size());
                        publishRequest = httpPut(String.format("sct/bulk/publish?token=%s", getToken()), createBulkPublishData(currentNamespace, bulkIds));
                        execute(publishRequest);
                    }
                }
            } else {
                final String componentId = Iterables.getOnlyElement(sctIdsToPublish.keySet());
                currentNamespace = SnomedIdentifiers.getNamespace(componentId);
                publishRequest = httpPut(String.format("sct/publish?token=%s", getToken()), createPublishData(componentId));
                execute(publishRequest);
            }
        }
        return ImmutableMap.copyOf(sctIdsToPublish);
    } catch (IOException e) {
        throw new SnowowlRuntimeException(String.format("Exception while publishing IDs for namespace %s.", currentNamespace), e);
    } finally {
        release(publishRequest);
    }
}
Also used : Collection(java.util.Collection) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) SctId(com.b2international.snowowl.snomed.cis.domain.SctId)

Example 3 with SctId

use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method register.

@Override
public Map<String, SctId> register(final Set<String> componentIds) {
    if (CompareUtils.isEmpty(componentIds)) {
        return Collections.emptyMap();
    }
    LOGGER.debug("Registering {} component IDs.", componentIds.size());
    final Map<String, SctId> sctIds = getSctIds(componentIds);
    final Map<String, SctId> availableOrReservedSctIds = ImmutableMap.copyOf(Maps.filterValues(sctIds, Predicates.or(SctId::isAvailable, SctId::isReserved)));
    if (availableOrReservedSctIds.isEmpty()) {
        return Collections.emptyMap();
    }
    HttpPost registerRequest = null;
    String currentNamespace = null;
    try {
        if (availableOrReservedSctIds.size() > 1) {
            final Multimap<String, String> componentIdsByNamespace = toNamespaceMultimap(availableOrReservedSctIds.keySet());
            for (final Entry<String, Collection<String>> entry : componentIdsByNamespace.asMap().entrySet()) {
                currentNamespace = entry.getKey();
                for (final Collection<String> bulkIds : Iterables.partition(entry.getValue(), requestBulkLimit)) {
                    LOGGER.debug("Sending bulk registration request for namespace {} with size {}.", currentNamespace, bulkIds.size());
                    registerRequest = httpPost(String.format("sct/bulk/register?token=%s", getToken()), createBulkRegistrationData(bulkIds));
                    execute(registerRequest);
                }
            }
        } else {
            final String componentId = Iterables.getOnlyElement(availableOrReservedSctIds.keySet());
            currentNamespace = SnomedIdentifiers.getNamespace(componentId);
            registerRequest = httpPost(String.format("sct/register?token=%s", getToken()), createRegistrationData(componentId));
            execute(registerRequest);
        }
        return ImmutableMap.copyOf(availableOrReservedSctIds);
    } catch (IOException e) {
        throw new SnowowlRuntimeException(String.format("Exception while reserving IDs for namespace %s.", currentNamespace), e);
    } finally {
        release(registerRequest);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Collection(java.util.Collection) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) SctId(com.b2international.snowowl.snomed.cis.domain.SctId)

Example 4 with SctId

use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method buildSctId.

private SctId buildSctId(final String componentId, final IdentifierStatus status) {
    final SctId sctId = new SctId();
    sctId.setSctid(componentId);
    sctId.setStatus(status.getSerializedName());
    sctId.setSequence(SnomedIdentifiers.getItemId(componentId));
    sctId.setNamespace(SnomedIdentifiers.getNamespace(componentId));
    sctId.setPartitionId(SnomedIdentifiers.getPartitionId(componentId));
    sctId.setCheckDigit(SnomedIdentifiers.getCheckDigit(componentId));
    // TODO: Other attributes of SctId could also be set here
    return sctId;
}
Also used : SctId(com.b2international.snowowl.snomed.cis.domain.SctId)

Example 5 with SctId

use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method deprecate.

@Override
public Map<String, SctId> deprecate(final Set<String> componentIds) {
    LOGGER.debug("Deprecating {} component IDs.", componentIds.size());
    final Map<String, SctId> sctIds = getSctIds(componentIds);
    final Map<String, SctId> problemSctIds = ImmutableMap.copyOf(Maps.filterValues(sctIds, Predicates.<SctId>not(Predicates.or(SctId::isAssigned, SctId::isPublished, SctId::isDeprecated))));
    if (!problemSctIds.isEmpty()) {
        throw new SctIdStatusException("Cannot deprecate '%s' component IDs because they are not assigned, published, or already deprecated.", problemSctIds);
    }
    final Map<String, SctId> assignedOrPublishedSctIds = ImmutableMap.copyOf(Maps.filterValues(sctIds, Predicates.or(SctId::isAssigned, SctId::isPublished)));
    if (assignedOrPublishedSctIds.isEmpty()) {
        return Collections.emptyMap();
    }
    HttpPut deprecateRequest = null;
    String currentNamespace = null;
    try {
        if (assignedOrPublishedSctIds.size() > 1) {
            final Multimap<String, String> componentIdsByNamespace = toNamespaceMultimap(assignedOrPublishedSctIds.keySet());
            for (final Entry<String, Collection<String>> entry : componentIdsByNamespace.asMap().entrySet()) {
                currentNamespace = entry.getKey();
                for (final Collection<String> bulkIds : Iterables.partition(entry.getValue(), requestBulkLimit)) {
                    LOGGER.debug("Sending bulk deprecation request for namespace {} with size {}.", currentNamespace, bulkIds.size());
                    deprecateRequest = httpPut(String.format("sct/bulk/deprecate?token=%s", getToken()), createBulkDeprecationData(currentNamespace, bulkIds));
                    execute(deprecateRequest);
                }
            }
        } else {
            final String componentId = Iterables.getOnlyElement(assignedOrPublishedSctIds.keySet());
            currentNamespace = SnomedIdentifiers.getNamespace(componentId);
            deprecateRequest = httpPut(String.format("sct/deprecate?token=%s", getToken()), createDeprecationData(componentId));
            execute(deprecateRequest);
        }
        return ImmutableMap.copyOf(assignedOrPublishedSctIds);
    } catch (IOException e) {
        throw new SnowowlRuntimeException(String.format("Exception while deprecating IDs for namespace %s.", currentNamespace), e);
    } finally {
        release(deprecateRequest);
    }
}
Also used : Collection(java.util.Collection) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) SctId(com.b2international.snowowl.snomed.cis.domain.SctId)

Aggregations

SctId (com.b2international.snowowl.snomed.cis.domain.SctId)22 Test (org.junit.Test)10 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)7 IOException (java.io.IOException)7 ISnomedIdentifierService (com.b2international.snowowl.snomed.cis.ISnomedIdentifierService)4 Collection (java.util.Collection)4 HttpPost (org.apache.http.client.methods.HttpPost)4 Json (com.b2international.commons.json.Json)3 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)3 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPut (org.apache.http.client.methods.HttpPut)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 RelationshipValue (com.b2international.snowowl.snomed.core.domain.RelationshipValue)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ImmutableMap (com.google.common.collect.ImmutableMap)1