use of com.b2international.snowowl.snomed.core.domain.NamespaceIdStrategy 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;
}
}
Aggregations