Search in sources :

Example 31 with BadRequestException

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);
}
Also used : RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ClassificationTask(com.b2international.snowowl.snomed.reasoner.domain.ClassificationTask) Branch(com.b2international.snowowl.core.branch.Branch) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 32 with BadRequestException

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;
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)

Example 33 with BadRequestException

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);
}
Also used : ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) BadRequestException(com.b2international.commons.exceptions.BadRequestException) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) EObjectWalker(com.b2international.snowowl.core.emf.EObjectWalker) EObjectTreeNode(com.b2international.snowowl.core.emf.EObjectTreeNode) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) Strings(com.google.common.base.Strings) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) NotEmpty(org.hibernate.validator.constraints.NotEmpty) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) SyntaxException(com.b2international.commons.exceptions.SyntaxException) Permission(com.b2international.snowowl.core.identity.Permission) EclConceptReference(com.b2international.snomed.ecl.ecl.EclConceptReference) ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) BranchContext(com.b2international.snowowl.core.domain.BranchContext) NoopTreeVisitor(com.b2international.commons.tree.NoopTreeVisitor) ApiException(com.b2international.commons.exceptions.ApiException) ResourceRequest(com.b2international.snowowl.core.request.ResourceRequest) SyntaxException(com.b2international.commons.exceptions.SyntaxException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ApiException(com.b2international.commons.exceptions.ApiException)

Example 34 with BadRequestException

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;
}
Also used : SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 35 with BadRequestException

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();
    }
}
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

BadRequestException (com.b2international.commons.exceptions.BadRequestException)41 IOException (java.io.IOException)8 Collectors (java.util.stream.Collectors)7 ResourceURI (com.b2international.snowowl.core.ResourceURI)6 BranchContext (com.b2international.snowowl.core.domain.BranchContext)6 Request (com.b2international.snowowl.core.events.Request)6 List (java.util.List)6 Map (java.util.Map)6 Options (com.b2international.commons.options.Options)5 Branch (com.b2international.snowowl.core.branch.Branch)5 Set (java.util.Set)5 CompareUtils (com.b2international.commons.CompareUtils)4 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)4 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)4 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 Collection (java.util.Collection)4 AlreadyExistsException (com.b2international.commons.exceptions.AlreadyExistsException)3 ConflictException (com.b2international.commons.exceptions.ConflictException)3 NotFoundException (com.b2international.commons.exceptions.NotFoundException)3 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)3