Search in sources :

Example 1 with IValidationMessage

use of org.jowidgets.validation.IValidationMessage in project jo-client-platform by jo-source.

the class BeanExceptionConverterImpl method convertBeanValidationException.

private IBeanMessage convertBeanValidationException(final String shortMessage, final IBeanProxy<?> bean, final BeanValidationException exception) {
    final IBeanValidationResult firstWorstBeanResult = exception.getValidationResult();
    final String propertyName = firstWorstBeanResult.getPropertyName();
    final Collection<String> propertyLabel = getPropertyLabels(bean, Collections.singleton(propertyName));
    final String labels = StringUtils.concatElementsSeparatedBy(propertyLabel, ',');
    final IValidationMessage worstFirst = firstWorstBeanResult.getValidationResult().getWorstFirst();
    final String message = labels + " - " + worstFirst.getText();
    return new BeanMessageImpl(BeanMessageType.WARNING, shortMessage, message, exception);
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) IValidationMessage(org.jowidgets.validation.IValidationMessage)

Example 2 with IValidationMessage

use of org.jowidgets.validation.IValidationMessage in project jo-client-platform by jo-source.

the class BeanProxyImplTest method testAllValidatorsForInitialValidPerson.

private void testAllValidatorsForInitialValidPerson(final IBeanProxy<IPerson> personProxy) {
    IValidationResult validationResult = personProxy.validate();
    Assert.assertTrue(validationResult.isOk());
    // make name invalid
    final IPerson person = personProxy.getBean();
    person.setName(PersonNameValidator.FORBIDDEN_NAME);
    validationResult = personProxy.validate();
    IValidationMessage worstFirst = validationResult.getWorstFirst();
    Assert.assertEquals(personAttributes.getAttribute(IPerson.NAME_PROPERTY).getCurrentLabel(), worstFirst.getContext());
    Assert.assertEquals(MessageType.ERROR, worstFirst.getType());
    // make name valid again
    personProxy.undoModifications();
    validationResult = personProxy.validate();
    Assert.assertTrue(validationResult.isOk());
    // make name invalid
    person.setName(null);
    validationResult = personProxy.validate();
    worstFirst = validationResult.getWorstFirst();
    Assert.assertEquals(personAttributes.getAttribute(IPerson.NAME_PROPERTY).getCurrentLabel(), worstFirst.getContext());
    Assert.assertEquals(MessageType.INFO_ERROR, worstFirst.getType());
    // make name valid again
    personProxy.undoModifications();
    validationResult = personProxy.validate();
    Assert.assertTrue(validationResult.isOk());
    // make lastname invalid
    person.setLastname(null);
    validationResult = personProxy.validate();
    worstFirst = validationResult.getWorstFirst();
    Assert.assertEquals(personAttributes.getAttribute(IPerson.LAST_NAME_PROPERTY).getCurrentLabel(), worstFirst.getContext());
    Assert.assertEquals(MessageType.INFO_ERROR, worstFirst.getType());
    // make lastname valid again
    personProxy.undoModifications();
    validationResult = personProxy.validate();
    Assert.assertTrue(validationResult.isOk());
    // make name invalid again
    person.setName(PersonNameValidator.FORBIDDEN_NAME);
    validationResult = personProxy.validate();
    worstFirst = validationResult.getWorstFirst();
    Assert.assertEquals(personAttributes.getAttribute(IPerson.NAME_PROPERTY).getCurrentLabel(), worstFirst.getContext());
    Assert.assertEquals(MessageType.ERROR, worstFirst.getType());
    // modify lastname valid
    person.setLastname("Foo");
    // name must still be invalid
    validationResult = personProxy.validate();
    worstFirst = validationResult.getWorstFirst();
    Assert.assertEquals(personAttributes.getAttribute(IPerson.NAME_PROPERTY).getCurrentLabel(), worstFirst.getContext());
    Assert.assertEquals(MessageType.ERROR, worstFirst.getType());
}
Also used : IPerson(org.jowidgets.cap.ui.impl.beans.IPerson) IValidationMessage(org.jowidgets.validation.IValidationMessage) IValidationResult(org.jowidgets.validation.IValidationResult)

Example 3 with IValidationMessage

use of org.jowidgets.validation.IValidationMessage in project jo-client-platform by jo-source.

the class BeanProxyImplTest method testInvalidBeanProxyCreation.

@Test
public void testInvalidBeanProxyCreation() {
    final IBeanProxy<IPerson> invalidPersonProxy = createPersonProxy(invalidPersonDto, personValidators);
    final IPerson invalidPerson = invalidPersonProxy.getBean();
    Assert.assertEquals(INVALID_PERSON_NAME, invalidPerson.getName());
    Assert.assertEquals(INVALID_PERSON_LAST_NAME, invalidPerson.getLastname());
    Assert.assertEquals(INVALID_PERSON_GENDER, invalidPerson.getGender());
    Assert.assertFalse(invalidPersonProxy.isModified(IPerson.NAME_PROPERTY));
    Assert.assertFalse(invalidPersonProxy.isModified(IPerson.LAST_NAME_PROPERTY));
    Assert.assertFalse(invalidPersonProxy.isModified(IPerson.GENDER_PROPERTY));
    final IValidationResult validationResult = invalidPersonProxy.validate();
    Assert.assertFalse(validationResult.isValid());
    final IValidationMessage worstFirst = validationResult.getWorstFirst();
    Assert.assertEquals(personAttributes.getAttribute(IPerson.NAME_PROPERTY).getCurrentLabel(), worstFirst.getContext());
    Assert.assertEquals(MessageType.ERROR, worstFirst.getType());
}
Also used : IPerson(org.jowidgets.cap.ui.impl.beans.IPerson) IValidationMessage(org.jowidgets.validation.IValidationMessage) IValidationResult(org.jowidgets.validation.IValidationResult) Test(org.junit.Test)

Example 4 with IValidationMessage

use of org.jowidgets.validation.IValidationMessage 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 5 with IValidationMessage

use of org.jowidgets.validation.IValidationMessage in project jo-client-platform by jo-source.

the class BeanProxyImplTest method testValidateInvalidPersonAfterValidatorsAdded.

@Test
public void testValidateInvalidPersonAfterValidatorsAdded() {
    final IBeanProxy<IPerson> invalidPersonProxy = createPersonProxy(invalidPersonDto);
    IValidationResult validationResult = invalidPersonProxy.validate();
    Assert.assertTrue(validationResult.isOk());
    // after adding validators, bean must have validation error
    invalidPersonProxy.addBeanPropertyValidators(personValidators);
    validationResult = invalidPersonProxy.validate();
    Assert.assertFalse(validationResult.isValid());
    final IValidationMessage worstFirst = validationResult.getWorstFirst();
    Assert.assertEquals(personAttributes.getAttribute(IPerson.NAME_PROPERTY).getCurrentLabel(), worstFirst.getContext());
    Assert.assertEquals(MessageType.ERROR, worstFirst.getType());
}
Also used : IPerson(org.jowidgets.cap.ui.impl.beans.IPerson) IValidationMessage(org.jowidgets.validation.IValidationMessage) IValidationResult(org.jowidgets.validation.IValidationResult) Test(org.junit.Test)

Aggregations

IValidationMessage (org.jowidgets.validation.IValidationMessage)7 IValidationResult (org.jowidgets.validation.IValidationResult)5 IPerson (org.jowidgets.cap.ui.impl.beans.IPerson)3 IBeanValidationResult (org.jowidgets.cap.common.api.validation.IBeanValidationResult)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 IInputComponentValidationLabelBluePrint (org.jowidgets.api.widgets.blueprint.IInputComponentValidationLabelBluePrint)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 IDecorator (org.jowidgets.util.IDecorator)1