// Listeners // --------------------------------------------------------------- public void fetchAssociatedRoleGroups(ActionEvent e) { User selectedUser = (User) dataTable.getRowData(); Collection<UserRole> userRoles = selectedUser.getUserRoles(); List<String> roleNames = new ArrayList<String>(); for (UserRole r : userRoles) { roleNames.add(r.getRole() + "|" + r.getRoleGroup()); } setAssociatedRoleGroupRoleNames(roleNames); usersAssociatedRoleGroupEditable = true; editModeRows.put(dataTable.getRowIndex(), true); }
@Override public void actionSave() { List<User> editItems = new ArrayList<User>(); for (int index = 0; index < dataList.size(); index++) { if (isEditModeRow(index)) { editItems.add(dataList.get(index)); } } Collection<User> foundUserList = new ArrayList<User>(); User existedUser = new User(); for (User editItem : editItems) { String hash = Util.createPasswordHash("MD5", Util.BASE64_ENCODING, null, null, editItem.getPassword()); editItem.setPassword(hash); if (editItem.getId() == null) { existedUser.setUsername(editItem.getUsername()); foundUserList = userDAO.get(existedUser); if (foundUserList.isEmpty()) { try { // id=null stand for new added entry userDAO.persist(editItem); } catch (Exception e) { ToolSet.setErrorMessage(e.getMessage() + " Cause: " + e.getCause()); e.printStackTrace(); return; } } else { String message = "Input user name " + editItem.getUsername() + " exists in database, duplicated is not allowed"; ToolSet.setErrorMessage(message); return; } } else { try { // otherwise, it is existing entry, update it. userDAO.update(editItem); } catch (Exception e) { ToolSet.setErrorMessage(e.getMessage() + " Cause: " + e.getCause()); e.printStackTrace(); return; } } } // reload data. super.actionSave(); usersAssociatedRoleGroupEditable = false; }
@Override public void actionDelete() { // Get selected items. List<User> deleteItems = new ArrayList<User>(); String deletedItemsNames = ""; for (int index = 0; index < dataList.size(); index++) { if (isSelectedRow(index)) { deleteItems.add(dataList.get(index)); } } if (deleteItems.isEmpty()) { ToolSet.setErrorMessage("Select at least one item to delete."); return; } Collection<UserRole> roles; for (User deleteItem : deleteItems) { // Check if there are roles assoicated with the user, before delete // action. roles = deleteItem.getUserRoles(); if (roles.isEmpty()) { try { userDAO.remove(deleteItem); deletedItemsNames = deletedItemsNames + "(" + deleteItem.getUsername() + ");<br>"; } catch (Exception e) { ToolSet.setErrorMessage(e.getMessage() + " Cause: " + e.getCause()); e.printStackTrace(); return; } } else { String message = "Disassociate from role before delete!"; ToolSet.setErrorMessage(message); return; } } if (deletedItemsNames.length() > 0) ToolSet.setErrorMessage("Deleted entries: <br>" + deletedItemsNames); }
public void saveAssociatedUsers(ActionEvent e) { User selectedUser = (User) dataTable.getRowData(); selectedUser = userDAO.get(selectedUser).get(0); UserRole entity; Collection<UserRole> matchedRoles = new ArrayList<UserRole>(); for (String roleGroup : getAssociatedRoleGroupRoleNames()) { entity = new UserRole(); String roleName = roleGroup.split("\\|")[0]; String groupName = roleGroup.split("\\|")[1]; entity.setRole(roleName); entity.setRoleGroup(groupName); matchedRoles.add(roleDAO.get(entity).get(0)); } selectedUser.setUserRoles(matchedRoles); userDAO.update(selectedUser); // Toggle edit mode and reload data. actionSave(); usersAssociatedRoleGroupEditable = false; if (!e.getPhaseId().equals(PhaseId.INVOKE_APPLICATION)) { e.setPhaseId(PhaseId.INVOKE_APPLICATION); e.queue(); return; } }