@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); }