Search in sources :

Example 1 with BeansValidationException

use of org.jowidgets.cap.common.api.exception.BeansValidationException in project jo-client-platform by jo-source.

the class DefaultServiceExceptionLogger method logBeansValidationException.

protected boolean logBeansValidationException(final Class<?> serviceType, final Throwable original, final ServiceException beanException) {
    if (beanException instanceof BeansValidationException) {
        if (logger.isInfoEnabled()) {
            final BeansValidationException beanValidationException = (BeansValidationException) beanException;
            logger.info("Bean validation failed: " + beanValidationException.getValidationResults());
        }
        return true;
    } else {
        return false;
    }
}
Also used : BeansValidationException(org.jowidgets.cap.common.api.exception.BeansValidationException)

Example 2 with BeansValidationException

use of org.jowidgets.cap.common.api.exception.BeansValidationException in project jo-client-platform by jo-source.

the class ServiceBeanValidationHelper method validate.

public static <BEAN_TYPE> void validate(final IBeanValidator<BEAN_TYPE> beanValidator, final boolean confirmValidationWarnings, final Collection<BEAN_TYPE> beans, final IBeanIdentityResolver<BEAN_TYPE> beanIdentityResolver, final IExecutionCallback executionCallback) {
    final Map<Object, IBeanValidationResult> validationResultMap = new HashMap<Object, IBeanValidationResult>();
    boolean hasError = false;
    final List<IBeanValidationResult> validationWarnings = new LinkedList<IBeanValidationResult>();
    int beanIndex = 0;
    BeansValidationException.KeyType keyType = KeyType.ID;
    for (final BEAN_TYPE bean : beans) {
        CapServiceToolkit.checkCanceled(executionCallback);
        final Object id = beanIdentityResolver.getId(bean);
        final Collection<IBeanValidationResult> validationResults = beanValidator.validate(bean);
        final IBeanValidationResult worstFirst = BeanValidationResultUtil.getWorstFirst(validationResults);
        if (worstFirst != null) {
            if (!worstFirst.getValidationResult().isValid()) {
                hasError = true;
                keyType = addResultsToMap(validationResultMap, id, beanIndex, keyType, worstFirst);
            } else {
                final IValidationMessage validationResult = worstFirst.getValidationResult().getWorstFirst();
                if (MessageType.WARNING.equals(validationResult.getType())) {
                    validationWarnings.add(worstFirst);
                    keyType = addResultsToMap(validationResultMap, id, beanIndex, keyType, worstFirst);
                }
            }
        }
        beanIndex++;
    }
    if (hasError) {
        throw new BeansValidationException(validationResultMap, keyType);
    } else if (confirmValidationWarnings && validationWarnings.size() > 0) {
        final String userQuestion = createUserQuestionString(validationWarnings);
        if (UserQuestionResult.NO.equals(executionCallback.userQuestion(userQuestion))) {
            throw new ServiceCanceledException();
        }
    }
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) IValidationMessage(org.jowidgets.validation.IValidationMessage) HashMap(java.util.HashMap) ServiceCanceledException(org.jowidgets.cap.common.api.exception.ServiceCanceledException) LinkedList(java.util.LinkedList) BeansValidationException(org.jowidgets.cap.common.api.exception.BeansValidationException) KeyType(org.jowidgets.cap.common.api.exception.BeansValidationException.KeyType)

Aggregations

BeansValidationException (org.jowidgets.cap.common.api.exception.BeansValidationException)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 KeyType (org.jowidgets.cap.common.api.exception.BeansValidationException.KeyType)1 ServiceCanceledException (org.jowidgets.cap.common.api.exception.ServiceCanceledException)1 IBeanValidationResult (org.jowidgets.cap.common.api.validation.IBeanValidationResult)1 IValidationMessage (org.jowidgets.validation.IValidationMessage)1