Search in sources :

Example 26 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class FundDistributionUtils method validateFundDistributionTotal.

public static void validateFundDistributionTotal(List<CompositePoLine> compositePoLines) {
    for (CompositePoLine cPoLine : compositePoLines) {
        if (cPoLine.getCost().getPoLineEstimatedPrice() != null && !cPoLine.getFundDistribution().isEmpty()) {
            Double poLineEstimatedPrice = cPoLine.getCost().getPoLineEstimatedPrice();
            BigDecimal remainingPercent = BigDecimal.valueOf(100);
            for (FundDistribution fundDistribution : cPoLine.getFundDistribution()) {
                FundDistribution.DistributionType dType = fundDistribution.getDistributionType();
                if (dType == FundDistribution.DistributionType.PERCENTAGE) {
                    // convert percent to amount
                    remainingPercent = remainingPercent.subtract(BigDecimal.valueOf(fundDistribution.getValue()));
                } else {
                    Double value = fundDistribution.getValue();
                    BigDecimal percentageValue = BigDecimal.valueOf(value).divide(BigDecimal.valueOf(poLineEstimatedPrice), 15, HALF_EVEN).movePointRight(2);
                    remainingPercent = remainingPercent.subtract(percentageValue);
                }
            }
            BigDecimal epsilon = BigDecimal.valueOf(1e-10);
            if (remainingPercent.abs().compareTo(epsilon) > 0) {
                throw new HttpException(422, INCORRECT_FUND_DISTRIBUTION_TOTAL);
            }
        }
    }
}
Also used : FundDistribution(org.folio.rest.jaxrs.model.FundDistribution) CompositePoLine(org.folio.rest.jaxrs.model.CompositePoLine) HttpException(org.folio.rest.core.exceptions.HttpException) BigDecimal(java.math.BigDecimal)

Example 27 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class ResponseUtil method handleFailure.

public static void handleFailure(Promise<?> promise, Throwable throwable) {
    Throwable cause = Optional.ofNullable(throwable.getCause()).orElse(throwable);
    Errors errors = ExceptionUtil.convertToErrors(throwable);
    int httpCode = extractHttpCode(cause);
    if (logger.isErrorEnabled()) {
        logger.error("Failure : {}", ExceptionUtil.errorAsString(errors));
    }
    promise.fail(new HttpException(httpCode, errors));
}
Also used : Errors(org.folio.rest.jaxrs.model.Errors) HttpException(org.folio.rest.core.exceptions.HttpException)

Example 28 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class PurchaseOrderHelper method checkOrderApprovalPermissions.

/**
 * Checks the value of "isApprovalRequired" in configurations, if the value is set to true, and order is being approved, verifies
 * if the user has required permissions to approve order
 *
 * @param compPO composite purchase order for checking permissions
 */
private void checkOrderApprovalPermissions(CompositePurchaseOrder compPO, JsonObject tenantConfig, RequestContext requestContext) {
    boolean isApprovalRequired = isApprovalRequiredConfiguration(tenantConfig);
    if (isApprovalRequired && compPO.getApproved().equals(Boolean.TRUE)) {
        if (isUserNotHaveApprovePermission(requestContext)) {
            throw new HttpException(HttpStatus.HTTP_FORBIDDEN.toInt(), USER_HAS_NO_APPROVAL_PERMISSIONS);
        }
        compPO.setApprovalDate(new Date());
        compPO.setApprovedById(getCurrentUserId(requestContext));
    }
}
Also used : HttpException(org.folio.rest.core.exceptions.HttpException) Date(java.util.Date)

Example 29 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class HelperUtils method verifyProtectedFieldsChanged.

public static JsonObject verifyProtectedFieldsChanged(List<String> protectedFields, JsonObject objectFromStorage, JsonObject requestObject) {
    Set<String> fields = new HashSet<>();
    JsonPathParser oldObject = new JsonPathParser(objectFromStorage);
    JsonPathParser newObject = new JsonPathParser(requestObject);
    for (String field : protectedFields) {
        if (!Objects.equals(oldObject.getValueAt(field), newObject.getValueAt(field))) {
            fields.add(field);
        }
    }
    if (CollectionUtils.isNotEmpty(fields)) {
        Error error = PROHIBITED_FIELD_CHANGING.toError().withAdditionalProperty(PROTECTED_AND_MODIFIED_FIELDS, fields);
        throw new HttpException(400, error);
    }
    return objectFromStorage;
}
Also used : JsonPathParser(org.folio.rest.tools.parser.JsonPathParser) Error(org.folio.rest.jaxrs.model.Error) HttpException(org.folio.rest.core.exceptions.HttpException) HashSet(java.util.HashSet)

Example 30 with HttpException

use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.

the class HelperUtils method handleEndpointException.

private static void handleEndpointException(HttpMethod operation, String endpoint, Throwable t, CompletableFuture<?> future, Logger logger) {
    Throwable cause = t instanceof CompletionException ? t.getCause() : t;
    int code = cause instanceof HttpException ? ((HttpException) cause).getCode() : 500;
    String message = String.format(EXCEPTION_CALLING_ENDPOINT_MSG, operation, endpoint, cause.getMessage());
    logger.error(message, t);
    future.completeExceptionally(new HttpException(code, message));
}
Also used : CompletionException(java.util.concurrent.CompletionException) HttpException(org.folio.rest.core.exceptions.HttpException)

Aggregations

HttpException (org.folio.rest.core.exceptions.HttpException)54 CompletionException (java.util.concurrent.CompletionException)26 Test (org.junit.jupiter.api.Test)26 Error (org.folio.rest.jaxrs.model.Error)18 CompositePoLine (org.folio.rest.jaxrs.model.CompositePoLine)16 Parameter (org.folio.rest.jaxrs.model.Parameter)15 ArrayList (java.util.ArrayList)14 CompletableFuture (java.util.concurrent.CompletableFuture)14 RequestContext (org.folio.rest.core.models.RequestContext)14 List (java.util.List)13 RequestEntry (org.folio.rest.core.models.RequestEntry)13 CompositePurchaseOrder (org.folio.rest.jaxrs.model.CompositePurchaseOrder)13 JsonObject (io.vertx.core.json.JsonObject)12 FundDistribution (org.folio.rest.jaxrs.model.FundDistribution)12 Map (java.util.Map)11 CollectionUtils (org.apache.commons.collections4.CollectionUtils)10 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 RestClient (org.folio.rest.core.RestClient)10 Location (org.folio.rest.jaxrs.model.Location)10