use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class UserServiceImpl method createUser.
@Override
@Transactional(rollbackFor = Throwable.class)
public User createUser(User userTemplate) throws FieldValidationException {
Assert.notNull(userTemplate);
validateUser(userTemplate);
User userToCreate = new User();
userToCreate.setDisplayName(userTemplate.getDisplayName());
userToCreate.setEmail(userTemplate.getEmail());
userToCreate.setIntegrationData(userTemplate.getIntegrationData());
userToCreate.setIsBlocked(userTemplate.getIsBlocked());
userToCreate.setLocale(userTemplate.getLocale());
userToCreate.setRegisteredAt(userTemplate.getRegisteredAt());
userToCreate.setTimeZone(userTemplate.getTimeZone());
// Patch data
if (userTemplate.getUuid() != null) {
ValidationUtils.isValidNotNullableUuid(userTemplate.getUuid());
userToCreate.setUuid(userTemplate.getUuid());
} else {
userToCreate.setUuid(UUID.randomUUID().toString());
}
if (userToCreate.getRegisteredAt() == 0) {
userToCreate.setRegisteredAt(new Date().getTime());
}
if (userToCreate.getLocale() == null) {
userToCreate.setLocale(LocaleContextHolder.getLocale().toString());
}
if (userToCreate.getTimeZone() == null) {
userToCreate.setTimeZone(Calendar.getInstance().getTimeZone().getID());
}
// Create
try {
userDao.createUser(userToCreate);
eventBus.post(EntityChangedEvent.added(userToCreate));
} catch (DuplicateKeyException dke) {
throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL));
} catch (Throwable t) {
String msg = String.format("Failed to create user with email '%s'", userToCreate.getEmail());
throw new UserServiceUnexpectedException(msg, t);
}
return userToCreate;
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class UserServiceImpl method validateEmail.
private void validateEmail(String email) throws FieldValidationException {
ValidationContext ctx = new ValidationContext();
validateEmail(email, ctx);
if (ctx.getHasErrors()) {
throw new FieldValidationException(ctx.getErrors());
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class UserServiceImpl method updateUser.
@Override
@Transactional(rollbackFor = Throwable.class)
public void updateUser(User user) throws FieldValidationException, UserNotFoundException {
Preconditions.checkArgument(user != null, "User reference required");
Preconditions.checkArgument(StringUtils.hasText(user.getUuid()), "User uuid must be provided");
validateUser(user);
boolean isUpdatedSuccessfully;
try {
isUpdatedSuccessfully = userDao.updateUser(user);
eventBus.post(EntityChangedEvent.updated(user));
} catch (DuplicateKeyException dke) {
throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL));
} catch (Throwable t) {
String msg = String.format("Failed to update user '%s'", user.getUuid());
throw new UserServiceUnexpectedException(msg, t);
}
if (!isUpdatedSuccessfully) {
throw new UserNotFoundException(user.getUuid());
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class ExceptionTranslatorFveImpl method buildUserMessage.
@Override
public String buildUserMessage(Throwable t, Locale locale) {
if (!FieldValidationException.class.equals(t.getClass())) {
return null;
}
FieldValidationException fve = (FieldValidationException) t;
StringBuilder ret = new StringBuilder();
ret.append(I18nUtils.buildMessage(fve, messageSource, locale));
ret.append(": ");
boolean first = true;
for (ValidationError ve : fve.getErrors()) {
if (!first) {
ret.append(", ");
}
ret.append(translateFieldName(ve.getFieldToken(), messageSource, locale));
ret.append(" - ");
ret.append(I18nUtils.buildMessage(ve, messageSource, locale));
first = false;
}
return ret.toString();
}
Aggregations