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