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