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);
}
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());
}
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());
}
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();
}
}
}
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());
}
Aggregations