public String prepareUpdateUser(Long id) {
   userListToUpdate = new ArrayList<User>();
   userToUpdate = registrationDao.getEntityManager().find(User.class, id);
   userName = userToUpdate.getUserName();
   firstName = userToUpdate.getFirstName();
   lastName = userToUpdate.getLastName();
   email = userToUpdate.getEmail();
   prepareUpdatedRolePickup();
   prepareUpdatedGroupPickup();
   userListToUpdate.add(userToUpdate);
   allUsersByCreationDate = registrationDao.findAllUsersByRegistrationDate();
   sortPickup();
   return UPDATE_PATH;
 }
 // ###################################################################
 // business methods
 // ###################################################################
 public String saveUserRegistration() {
   if (!roleDefinitions.getTarget().isEmpty() && !groupDefinitions.getTarget().isEmpty()) {
     User newUser = defineBasicUserAttributes();
     Set<Role> userRoles = defineRoles();
     newUser.setRoles(userRoles);
     Set<UserGroup> userGroups = defineGroups();
     newUser.setGroups(userGroups);
     registrationDao.persist(newUser);
     resetFields();
     MessageService.displayFacesMessageInfo(
         "registration_properties", "REGISTRATION_SUCCESS_SUMMARY", "REGISTRATION_SUCCESS_DETAIL");
   }
   allUsersByCreationDate = registrationDao.findAllUsersByRegistrationDate();
   return "/pages/administration/registration/new.jsf";
 }
 private Set<Role> redefineRoles() {
   Set<Role> userRoles = new HashSet<Role>(userToUpdate.getRoles());
   for (String newRole : roleDefinitions.getSource()) {
     Role role = registrationDao.findRoleByRoleName(newRole);
     userRoles.remove(role);
   }
   for (String newRole : roleDefinitions.getTarget()) {
     Role role = registrationDao.findRoleByRoleName(newRole);
     userRoles.add(role);
   }
   return userRoles;
 }
 private Set<UserGroup> redefineGroups() {
   Set<UserGroup> userGroups = new HashSet<UserGroup>(userToUpdate.getGroups());
   for (String newGroup : groupDefinitions.getSource()) {
     UserGroup userGroup = registrationDao.findGroupByGroupName(newGroup);
     userGroups.remove(userGroup);
   }
   for (String newGroup : groupDefinitions.getTarget()) {
     UserGroup userGroup = registrationDao.findGroupByGroupName(newGroup);
     userGroups.add(userGroup);
   }
   return userGroups;
 }
 public String toggleUserActivity(User user) {
   userToUpdate = user;
   userToUpdate.setDisabled(!userToUpdate.getDisabled());
   userToUpdate.setModifiedOn(new Date());
   User modifiedByUser = (User) SessionService.getSessionAttribute("user");
   userToUpdate.setModifiedBy(modifiedByUser.getUserName());
   registrationDao.mergeUser(userToUpdate);
   userToUpdate = null;
   if (user.getDisabled()) {
     MessageService.displayFacesMessageInfo(
         "registration_properties",
         "REGISTRATION_USER_DISABLED_SUMMARY",
         "REGISTRATION_USER_DISABLED_DETAIL");
   } else {
     MessageService.displayFacesMessageInfo(
         "registration_properties",
         "REGISTRATION_USER_ENABLED_SUMMARY",
         "REGISTRATION_USER_ENABLED_DETAIL");
   }
   return UPDATE_PATH;
 }
 public String updateUser(User user) {
   userToUpdate = user;
   userToUpdate.setModifiedOn(new Date());
   User modifiedByUser = (User) SessionService.getSessionAttribute("user");
   userToUpdate.setModifiedBy(modifiedByUser.getUserName());
   Set<Role> userRoles = redefineRoles();
   userToUpdate.setRoles(userRoles);
   Set<UserGroup> userGroups = redefineGroups();
   userToUpdate.setGroups(userGroups);
   registrationDao.mergeUser(userToUpdate);
   sortPickup();
   resetFields();
   MessageService.displayFacesMessageInfo(
       "registration_properties",
       "REGISTRATION_USER_UPDATED_SUMMARY",
       "REGISTRATION_USER_UPDATED_DETAIL");
   return UPDATE_PATH;
 }
 private User defineBasicUserAttributes() {
   Date createdOn = new Date();
   User createdByUser = (User) SessionService.getSessionAttribute("user");
   String createdBy = createdByUser.getUserName();
   User newUser = new User();
   newUser.setCreatedBy(createdBy);
   newUser.setCreatedOn(createdOn);
   newUser.setDisabled(false);
   newUser.setEmail(email);
   newUser.setPassword(PasswordService.createHashedPassword(password));
   newUser.setUserName(userName);
   newUser.setFirstName(firstName);
   newUser.setLastName(lastName);
   return newUser;
 }