Search in sources :

Example 26 with FieldValidationException

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;
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) Date(java.util.Date) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateUserValidationError(org.summerb.microservices.users.api.validation.DuplicateUserValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 27 with FieldValidationException

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());
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 28 with FieldValidationException

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());
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateUserValidationError(org.summerb.microservices.users.api.validation.DuplicateUserValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 29 with FieldValidationException

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();
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) ValidationError(org.summerb.approaches.validation.ValidationError)

Aggregations

FieldValidationException (org.summerb.approaches.validation.FieldValidationException)29 User (org.summerb.microservices.users.api.dto.User)11 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)11 Transactional (org.springframework.transaction.annotation.Transactional)9 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)8 ValidationContext (org.summerb.approaches.validation.ValidationContext)5 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ValidationErrorsVm (org.summerb.approaches.springmvc.model.ValidationErrorsVm)3 ValidationError (org.summerb.approaches.validation.ValidationError)3 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)3 InvalidPasswordException (org.summerb.microservices.users.api.exceptions.InvalidPasswordException)3 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 NotAuthorizedResult (org.summerb.approaches.security.api.dto.NotAuthorizedResult)2 NotAuthorizedException (org.summerb.approaches.security.api.exceptions.NotAuthorizedException)2 UserDetailsImpl (org.summerb.approaches.springmvc.security.dto.UserDetailsImpl)2 UserStatus (org.summerb.approaches.springmvc.security.dto.UserStatus)2 DuplicateUserValidationError (org.summerb.microservices.users.api.validation.DuplicateUserValidationError)2 GenericException (org.summerb.utils.exceptions.GenericException)2 ExceptionInfo (org.summerb.utils.exceptions.dto.ExceptionInfo)2