try {
final String oldUserId = requestJSONObject.optString(Keys.OBJECT_ID);
final JSONObject oldUser = userRepository.get(oldUserId);
if (null == oldUser) {
throw new ServiceException(langPropsService.get("updateFailLabel"));
}
final String userNewEmail = requestJSONObject.optString(User.USER_EMAIL).toLowerCase().trim();
// Check email is whether duplicated
final JSONObject mayBeAnother = userRepository.getByEmail(userNewEmail);
if (null != mayBeAnother && !mayBeAnother.optString(Keys.OBJECT_ID).equals(oldUserId)) {
// Exists someone else has the save email as requested
throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
}
// Update
final String userName = requestJSONObject.optString(User.USER_NAME);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
oldUser.put(User.USER_EMAIL, userNewEmail);
oldUser.put(User.USER_NAME, userName);
oldUser.put(User.USER_PASSWORD, userPassword);
// Unchanges the default role
userRepository.update(oldUserId, oldUser);
transaction.commit();
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Updates a user failed", e);
throw new ServiceException(e);
}
}