Search in sources :

Example 1 with DuplicateUserValidationError

use of org.summerb.microservices.users.api.validation.DuplicateUserValidationError 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 2 with DuplicateUserValidationError

use of org.summerb.microservices.users.api.validation.DuplicateUserValidationError 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)

Aggregations

DuplicateKeyException (org.springframework.dao.DuplicateKeyException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)2 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)2 DuplicateUserValidationError (org.summerb.microservices.users.api.validation.DuplicateUserValidationError)2 Date (java.util.Date)1 User (org.summerb.microservices.users.api.dto.User)1 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)1