All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.braintreegateway.ValidationErrors Maven / Gradle / Ivy

package com.braintreegateway;

import com.braintreegateway.util.NodeWrapper;
import com.braintreegateway.util.StringUtils;
import java.util.*;

/**
 * Represents a collection of (nested) validation errors. Query for validation
 * errors by object and field. For Example:
 * 
 * 
 *  TransactionRequest request = new TransactionRequest().
 *      amount(null).
 *   Result<Transaction> result = gateway.transaction().sale(request);
 *   Assert.assertFalse(result.isSuccess());
 *   ValidationErrors errors = result.getErrors();
 *   Assert.assertEquals(ValidationErrorCode.TRANSACTION_AMOUNT_IS_REQUIRED, errors.forObject("transaction").onField("amount").get(0).getCode());
 * 
* * For more detailed information on {@link ValidationErrors}, see https://developer.paypal.com/braintree/docs/reference/general/validation-errors/overview/java * */ public class ValidationErrors { private List errors; private Map nestedErrors; public ValidationErrors() { errors = new ArrayList(); nestedErrors = new HashMap(); } public ValidationErrors(NodeWrapper node) { this(); populateErrors(node); } public void addError(ValidationError error) { errors.add(error); } public void addErrors(String objectName, ValidationErrors errors) { nestedErrors.put(objectName, errors); } /** * Returns the number of errors on this object and all nested objects. * * @see #size() * @return the number of errors. */ public int deepSize() { int size = errors.size(); for (ValidationErrors nestedError : nestedErrors.values()) { size += nestedError.deepSize(); } return size; } public ValidationErrors forIndex(int index) { return forObject("index_" + index); } /** * Returns a {@link ValidationErrors} representing nested errors for the * given objectName. * * @param objectName * the name of the object with nested validation errors, e.g. * customer or creditCard. * @return a {@link ValidationErrors} object. */ public ValidationErrors forObject(String objectName) { ValidationErrors errorsOnObject = nestedErrors.get(StringUtils.dasherize(objectName)); if (errorsOnObject == null) { return new ValidationErrors(); } else { return errorsOnObject; } } /** * Returns a List of all of the {@link ValidationError} on this object and * all nested objects. * * @return a List of {@link ValidationError} objects. */ public List getAllDeepValidationErrors() { List result = new ArrayList(errors); for (ValidationErrors validationErrors : nestedErrors.values()) { result.addAll(validationErrors.getAllDeepValidationErrors()); } return result; } /** * Returns a List of all of the {@link ValidationError} objects at the * current nesting level. * * @return a List of {@link ValidationError} objects. */ public List getAllValidationErrors() { return Collections.unmodifiableList(new ArrayList(errors)); } /** * Returns a List of {@link ValidationError} objects for the given field. * * @param fieldName * the name of the field with errors, e.g. amount or * expirationDate. * @return a List of {@link ValidationError} objects */ public List onField(String fieldName) { List list = new ArrayList(); for (ValidationError error : errors) { if (error.getAttribute().equals(StringUtils.underscore(fieldName))) { list.add(error); } } return list; } private void populateErrors(NodeWrapper node) { if (node.getElementName().equals("api-error-response")) { node = node.findFirst("errors"); } List errorResponses = node.findAll("*"); for (NodeWrapper errorResponse : errorResponses) { if (!errorResponse.getElementName().equals("errors")) { nestedErrors.put(errorResponse.getElementName(), new ValidationErrors(errorResponse)); } else { populateTopLevelErrors(errorResponse.findAll("error")); } } } private void populateTopLevelErrors(List childErrors) { for (NodeWrapper childError : childErrors) { ValidationErrorCode code = ValidationErrorCode.findByCode(childError.findString("code")); String message = childError.findString("message"); errors.add(new ValidationError(childError.findString("attribute"), code, message)); } } /** * Returns the number of errors on this object at the current nesting level. * * @see #deepSize() * @return the number of errors. */ public int size() { return errors.size(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy