public synchronized void addRoleToUser(User user, String role)
throws AmbariException {
UserEntity userEntity = userDAO.findByPK(user.getUserId());
if (userEntity == null) {
throw new AmbariException("User " + user + " doesn't exist");
}
RoleEntity roleEntity = roleDAO.findByName(role);
if (roleEntity == null) {
LOG.warn("Trying to add user to non-existent role"
+ ", user=" + user.getUserName()
+ ", role=" + role);
throw new AmbariException("Role " + role + " doesn't exist");
}
if (!userEntity.getRoleEntities().contains(roleEntity)) {
userEntity.getRoleEntities().add(roleEntity);
roleEntity.getUserEntities().add(userEntity);
userDAO.merge(userEntity);
roleDAO.merge(roleEntity);
} else {
throw new AmbariException("User " + user + " already owns role " + role);
}
}