Search in sources :

Example 6 with IBeanValidationResult

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

the class ServiceBeanValidationHelper method createUserQuestionString.

private static String createUserQuestionString(final List<IBeanValidationResult> validationWarnings) {
    final Set<String> messages = new HashSet<String>();
    final StringBuilder builder = new StringBuilder();
    builder.append(THERE_ARE_VALIDATION_WARNINGS.get() + "\n\n");
    for (final IBeanValidationResult validationWarning : validationWarnings) {
        final String messageText = validationWarning.getValidationResult().getWorstFirst().getText();
        if (!messages.contains(messageText)) {
            builder.append(messageText + "\n");
            messages.add(messageText);
        }
    }
    builder.append("\n" + PROCEED_ANYWAY.get());
    return builder.toString();
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) HashSet(java.util.HashSet)

Example 7 with IBeanValidationResult

use of org.jowidgets.cap.common.api.validation.IBeanValidationResult 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)

Example 8 with IBeanValidationResult

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

the class BeanExceptionConverterImpl method convertBeansValidationException.

private IBeanMessage convertBeansValidationException(final String shortMessage, final List<? extends IBeanProxy<?>> processedBeans, final int destinationBeanIndex, final IBeanProxy<?> destinationBean, final BeansValidationException exception) {
    final Map<Object, IBeanValidationResult> validationResults = exception.getValidationResults();
    final IBeanValidationResult validationResult;
    if (KeyType.INDEX.equals(exception.getKeyType())) {
        validationResult = validationResults.get(Integer.valueOf(destinationBeanIndex));
    } else {
        validationResult = validationResults.get(destinationBean.getId());
    }
    if (validationResult != null) {
        return convertBeanValidationException(shortMessage, destinationBean, new BeanValidationException(destinationBean.getId(), validationResult));
    } else {
        return new BeanMessageImpl(BeanMessageType.WARNING, shortMessage, VALIDATION_OF_OTHER_DATASETS_FAILED.get(), exception);
    }
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) BeanValidationException(org.jowidgets.cap.common.api.exception.BeanValidationException)

Example 9 with IBeanValidationResult

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

the class BeanProxyImpl method consolidateBeanValidationResult.

private Map<String, IBeanValidationResult> consolidateBeanValidationResult(final Map<String, IBeanValidationResult> currentResultMap, final ValueHolder<IBeanValidationResult> firstWorstIndependendResultHolder, final Collection<IBeanValidationResult> validationResults) {
    for (final IBeanValidationResult result : validationResults) {
        final String propertyName = result.getPropertyName();
        if (propertyName != null) {
            final IBeanValidationResult currentResult = currentResultMap.get(propertyName);
            final IBeanValidationResult worseResult = getWorseResult(currentResult, result);
            if (worseResult == result) {
                currentResultMap.put(propertyName, result);
            }
        } else {
            final IBeanValidationResult firstWorstIndependendResult = firstWorstIndependendResultHolder.get();
            final IBeanValidationResult worseResult = getWorseResult(firstWorstIndependendResult, result);
            if (worseResult == result) {
                firstWorstIndependendResultHolder.set(result);
            }
        }
    }
    return currentResultMap;
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult)

Example 10 with IBeanValidationResult

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

the class BeanProxyImpl method clearValidationResults.

private void clearValidationResults() {
    final boolean hasResults = independentWorstResult.get() != null || validationResults.size() > 0;
    if (hasResults) {
        if (independentWorstResult.get() != null) {
            for (final Entry<String, IObserverSet<IExternalBeanValidator>> entry : externalValidators.entrySet()) {
                final IBeanValidationResult okResult = BeanValidationResult.create(entry.getKey(), ValidationResult.ok());
                for (final IExternalBeanValidator externalValidator : entry.getValue()) {
                    externalValidator.validate(Collections.singletonList(okResult));
                }
            }
        } else {
            for (final String propertyName : validationResults.keySet()) {
                final IBeanValidationResult okResult = BeanValidationResult.create(propertyName, ValidationResult.ok());
                for (final IExternalBeanValidator externalValidator : getRegisteredExternalValidators(propertyName)) {
                    externalValidator.validate(Collections.singletonList(okResult));
                }
            }
        }
        independentWorstResult.set(null);
        validationResults.clear();
        fireValidationConditionsChanged();
    }
}
Also used : IObserverSet(org.jowidgets.util.collection.IObserverSet) IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) IExternalBeanValidator(org.jowidgets.cap.ui.api.bean.IExternalBeanValidator)

Aggregations

IBeanValidationResult (org.jowidgets.cap.common.api.validation.IBeanValidationResult)12 LinkedList (java.util.LinkedList)3 BeanValidationException (org.jowidgets.cap.common.api.exception.BeanValidationException)2 IExternalBeanValidator (org.jowidgets.cap.ui.api.bean.IExternalBeanValidator)2 IValidationMessage (org.jowidgets.validation.IValidationMessage)2 IValidationResult (org.jowidgets.validation.IValidationResult)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IValidationResultLabel (org.jowidgets.api.widgets.IValidationResultLabel)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 IBeanValidationResultListBuilder (org.jowidgets.cap.common.api.validation.IBeanValidationResultListBuilder)1 ValueHolder (org.jowidgets.util.ValueHolder)1 IObserverSet (org.jowidgets.util.collection.IObserverSet)1