use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class ClassificationSaveRequest method execute.
@Override
public String execute(final RepositoryContext context) {
final Request<RepositoryContext, ClassificationTask> classificationRequest = ClassificationRequests.prepareGetClassification(classificationId).build();
final ClassificationTask classification = classificationRequest.execute(context);
final String branchPath = classification.getBranch();
final Request<RepositoryContext, Branch> branchRequest = RepositoryRequests.branching().prepareGet(branchPath).build();
final Branch branch = branchRequest.execute(context);
if (!SAVEABLE_STATUSES.contains(classification.getStatus())) {
throw new BadRequestException("Classification '%s' is not in the expected state to start saving changes.", classificationId);
}
if (classification.getTimestamp() < branch.headTimestamp()) {
throw new BadRequestException("Classification '%s' on branch '%s' is stale (classification timestamp: %s, head timestamp: %s).", classificationId, branchPath, classification.getTimestamp(), branch.headTimestamp());
}
final String user = !Strings.isNullOrEmpty(userId) ? userId : context.service(User.class).getUsername();
final AsyncRequest<?> saveRequest = new SaveJobRequestBuilder().setClassificationId(classificationId).setUserId(user).setParentLockContext(parentLockContext).setCommitComment(commitComment).setModuleId(moduleId).setNamespace(namespace).setAssignerType(assignerType).setFixEquivalences(fixEquivalences).setHandleConcreteDomains(handleConcreteDomains).build(branchPath);
return JobRequests.prepareSchedule().setUser(userId).setRequest(saveRequest).setDescription(String.format("Saving classification changes on %s", branch.path())).buildAsync().get(context, SCHEDULE_TIMEOUT_MILLIS);
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedReferenceSetRestService method getRefSetTypes.
private Collection<SnomedRefSetType> getRefSetTypes(String[] refSetTypes) {
if (refSetTypes == null) {
return null;
}
final Set<String> unresolvedRefSetTypes = Sets.newTreeSet();
final Set<SnomedRefSetType> resolvedRefSetTypes = newHashSetWithExpectedSize(refSetTypes.length);
for (String refSetTypeString : refSetTypes) {
final SnomedRefSetType refSetType = SnomedRefSetType.get(refSetTypeString.toUpperCase());
if (refSetType != null) {
resolvedRefSetTypes.add(refSetType);
} else {
unresolvedRefSetTypes.add(refSetTypeString);
}
}
if (!unresolvedRefSetTypes.isEmpty()) {
throw new BadRequestException("Unknown reference set types: '%s'", unresolvedRefSetTypes).withDeveloperMessage("Available reference set types are: " + SnomedRefSetType.VALUES);
}
return resolvedRefSetTypes;
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedEclLabelerRequest method execute.
@Override
public LabeledEclExpressions execute(BranchContext context) {
final EclSerializer eclSerializer = context.service(EclSerializer.class);
final EclParser eclParser = context.service(EclParser.class);
final Set<String> conceptIdsToLabel = Sets.newHashSetWithExpectedSize(expressions.size());
final Map<String, ExpressionConstraint> queries = Maps.newHashMapWithExpectedSize(expressions.size());
final LinkedHashMap<String, Object> errors = Maps.newLinkedHashMap();
for (String expression : expressions) {
if (Strings.isNullOrEmpty(expression)) {
continue;
}
try {
ExpressionConstraint query = queries.computeIfAbsent(expression, (key) -> eclParser.parse(key));
conceptIdsToLabel.addAll(collect(query));
} catch (ApiException e) {
if (e instanceof SyntaxException) {
errors.put(expression, List.copyOf(((SyntaxException) e).getAdditionalInfo().values()));
} else if (e instanceof BadRequestException) {
errors.put(expression, e.getMessage());
} else {
throw e;
}
}
}
if (!errors.isEmpty()) {
BadRequestException badRequestException = new BadRequestException("One or more ECL syntax errors");
badRequestException.withAdditionalInfo("erroneousExpressions", errors);
throw badRequestException;
}
// fetch all concept labels
final Map<String, String> labels = SnomedRequests.prepareSearchConcept().filterByIds(conceptIdsToLabel).setLimit(conceptIdsToLabel.size()).setExpand(descriptionType.toLowerCase() + "()").setLocales(locales()).build().execute(context).stream().collect(Collectors.toMap(SnomedConcept::getId, this::extractLabel));
// expand all queries with labels
List<String> results = expressions.stream().map(expression -> {
if (Strings.isNullOrEmpty(expression)) {
return expression;
} else {
ExpressionConstraint query = queries.get(expression);
expand(query, labels);
return eclSerializer.serialize(query);
}
}).collect(Collectors.toList());
return new LabeledEclExpressions(results);
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedDSVExportRequest method getRefSetExporter.
private IRefSetDSVExporter getRefSetExporter(BranchContext context) {
final SnomedReferenceSet refSet = SnomedRequests.prepareGetReferenceSet(refSetId).build().execute(context);
IRefSetDSVExporter exporter = null;
if (SnomedRefSetType.SIMPLE.equals(refSet.getType())) {
exporter = new SnomedSimpleTypeRefSetDSVExporter(context, toExportModel(context));
} else if (SnomedRefSetUtil.isMapping(refSet.getType())) {
exporter = new MapTypeRefSetDSVExporter(context, toExportModel(context));
} else {
throw new BadRequestException("Unsupported reference set '%s' with type '%s' in DSV export", refSetId, refSet.getType());
}
return exporter;
}
use of com.b2international.commons.exceptions.BadRequestException 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();
}
}
Aggregations