use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.
the class AbstractIdentifierServiceTest method whenReleasingReservedIds_ThenTheyShouldBeAvailable.
@Test
public void whenReleasingReservedIds_ThenTheyShouldBeAvailable() {
try {
final Set<String> componentIds = getIdentifierService().reserve(B2I_NAMESPACE, ComponentCategory.CONCEPT, 2);
getIdentifierService().register(componentIds);
getIdentifierService().release(componentIds);
final Collection<SctId> sctIds = getIdentifierService().getSctIds(componentIds).values();
for (final SctId sctId : sctIds) {
assertTrue("Status must be available", IdentifierStatus.AVAILABLE.getSerializedName().equals(sctId.getStatus()));
}
} catch (Exception e) {
fail(String.format("Unexpected exception was thrown: %s.", e.getMessage()));
}
}
use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.
the class CisSnomedIdentifierService method release.
@Override
public Map<String, SctId> release(final Set<String> componentIds) {
LOGGER.debug("Releasing {} 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::isReserved, SctId::isAvailable))));
if (!problemSctIds.isEmpty()) {
throw new SctIdStatusException("Cannot release %s component IDs because they are not assigned, reserved, or already available.", problemSctIds);
}
final Map<String, SctId> assignedOrReservedSctIds = ImmutableMap.copyOf(Maps.filterValues(sctIds, Predicates.or(SctId::isAssigned, SctId::isReserved)));
// if there is no IDs to release, then just return the current sctIds set as a response
if (assignedOrReservedSctIds.isEmpty()) {
return sctIds;
}
HttpPut releaseRequest = null;
String currentNamespace = null;
try {
if (assignedOrReservedSctIds.size() > 1) {
final Multimap<String, String> componentIdsByNamespace = toNamespaceMultimap(assignedOrReservedSctIds.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 release request for namespace {} with size {}.", currentNamespace, bulkIds.size());
releaseRequest = httpPut(String.format("sct/bulk/release?token=%s", getToken()), createBulkReleaseData(currentNamespace, bulkIds));
execute(releaseRequest);
}
}
} else {
final String componentId = Iterables.getOnlyElement(assignedOrReservedSctIds.keySet());
currentNamespace = SnomedIdentifiers.getNamespace(componentId);
releaseRequest = httpPut(String.format("sct/release?token=%s", getToken()), createReleaseData(componentId));
execute(releaseRequest);
}
return ImmutableMap.copyOf(assignedOrReservedSctIds);
} catch (IOException e) {
throw new SnowowlRuntimeException(String.format("Exception while releasing IDs for namespace %s.", currentNamespace), e);
} finally {
release(releaseRequest);
}
}
use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.
the class CisSnomedIdentifierService method readSctIds.
private Map<String, SctId> readSctIds(final Set<String> componentIds) {
if (CompareUtils.isEmpty(componentIds)) {
return Collections.emptyMap();
}
HttpPost bulkRequest = null;
HttpGet singleRequest = null;
try {
if (componentIds.size() > 1) {
LOGGER.debug("Sending bulk component ID get request.");
final ImmutableMap.Builder<String, SctId> resultBuilder = ImmutableMap.builder();
for (final Collection<String> ids : Iterables.partition(componentIds, requestBulkLimit)) {
final String idsAsString = Joiner.on(',').join(ids);
final ObjectNode idsAsJson = mapper.createObjectNode().put("sctids", idsAsString);
bulkRequest = client.httpPost(String.format("sct/bulk/ids/?token=%s", getToken()), idsAsJson);
final String response = execute(bulkRequest);
final SctId[] sctIds = mapper.readValue(response, SctId[].class);
final Map<String, SctId> sctIdMap = Maps.uniqueIndex(Arrays.asList(sctIds), SctId::getSctid);
resultBuilder.putAll(sctIdMap);
}
return resultBuilder.build();
} else {
final String componentId = Iterables.getOnlyElement(componentIds);
LOGGER.debug("Sending component ID {} get request.", componentId);
singleRequest = httpGet(String.format("sct/ids/%s?token=%s", componentId, getToken()));
final String response = execute(singleRequest);
final SctId sctId = mapper.readValue(response, SctId.class);
return ImmutableMap.of(sctId.getSctid(), sctId);
}
} catch (IOException e) {
throw new SnowowlRuntimeException("Exception while getting IDs.", e);
} finally {
release(bulkRequest);
release(singleRequest);
}
}
use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.
the class CisSnomedIdentifierService method generateSctIds.
@Override
public Map<String, SctId> generateSctIds(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("Generating {} component IDs for category {}.", quantity, category.getDisplayName());
HttpPost generateRequest = null;
HttpGet recordsRequest = null;
try {
if (quantity > 1) {
LOGGER.debug("Sending {} ID bulk generation request.", category.getDisplayName());
generateRequest = httpPost(String.format("sct/bulk/generate?token=%s", getToken()), createBulkGenerationData(namespace, category, quantity));
final String response = execute(generateRequest);
final String jobId = mapper.readValue(response, 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 single generation request.", category.getDisplayName());
generateRequest = httpPost(String.format("sct/generate?token=%s", getToken()), createGenerationData(namespace, category));
final String response = execute(generateRequest);
final SctId sctid = mapper.readValue(response, SctId.class);
return readSctIds(Collections.singleton(sctid.getSctid()));
}
} catch (IOException e) {
throw new SnowowlRuntimeException("Caught exception while generating IDs.", e);
} finally {
release(generateRequest);
release(recordsRequest);
}
}
use of com.b2international.snowowl.snomed.cis.domain.SctId in project snow-owl by b2ihealthcare.
the class SnomedDescriptionApiTest method createDescriptionWithReservedId.
@Test
public void createDescriptionWithReservedId() {
ISnomedIdentifierService identifierService = getServiceForClass(ISnomedIdentifierService.class);
String descriptionId = Iterables.getOnlyElement(identifierService.reserve(null, ComponentCategory.DESCRIPTION, 1));
Json requestBody = createDescriptionRequestBody(Concepts.ROOT_CONCEPT).with("id", descriptionId).with("commitComment", "Created new description with reserved identifier");
final String createdDescriptionId = assertCreated(createComponent(branchPath, SnomedComponentType.DESCRIPTION, requestBody));
assertEquals(descriptionId, createdDescriptionId);
SctId descriptionSctId = SnomedRequests.identifiers().prepareGet().setComponentId(descriptionId).buildAsync().execute(getBus()).getSync().first().get();
assertEquals(IdentifierStatus.ASSIGNED.getSerializedName(), descriptionSctId.getStatus());
}
Aggregations