Search in sources :

Example 1 with KeyType

use of org.jowidgets.cap.common.api.exception.BeansValidationException.KeyType 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);
        final UserQuestionResult userQuestionResult;
        try {
            userQuestionResult = executionCallback.userQuestion(userQuestion);
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ServiceInterruptedException(e);
        }
        if (UserQuestionResult.NO.equals(userQuestionResult)) {
            throw new ServiceCanceledException();
        }
    }
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) IValidationMessage(org.jowidgets.validation.IValidationMessage) HashMap(java.util.HashMap) ServiceInterruptedException(org.jowidgets.cap.common.api.exception.ServiceInterruptedException) ServiceCanceledException(org.jowidgets.cap.common.api.exception.ServiceCanceledException) ServiceInterruptedException(org.jowidgets.cap.common.api.exception.ServiceInterruptedException) LinkedList(java.util.LinkedList) UserQuestionResult(org.jowidgets.cap.common.api.execution.UserQuestionResult) BeansValidationException(org.jowidgets.cap.common.api.exception.BeansValidationException) KeyType(org.jowidgets.cap.common.api.exception.BeansValidationException.KeyType)

Aggregations

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