* its associated roles and
* @param loggedInUser
* @return
*/
private User alignWithUserInDB(User loggedInUser) {
User u = null;
try {
u = (User) em.createQuery("select u from User u where u.username = :user_name")
.setParameter("user_name", loggedInUser.getUsername())
.getSingleResult();
} catch (NoResultException e) {
log.info("User "+loggedInUser+ " logged into Plato the first time.");
// no user with this username in database, so lets add him:
u = loggedInUser.clone();
}
u.setFirstName(loggedInUser.getFirstName());
u.setLastName(loggedInUser.getLastName());
u.setEmail(loggedInUser.getEmail());
u.setRoles(loggedInUser.getRoles());
alignRoles(u);
// persist all new roles
for (Role r : u.getRoles()) {
if (r.getId() == 0) {
em.persist(r);
}
}
em.persist(u);