diff --git a/README.md b/README.md index 52b96a0..bf9817e 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ My intent is to develop a system comprised of four major components: - [x] Associate items with tags by item - [x] Show tags in item summary - [x] Add user -- [ ] Edit user +- [x] Edit user - [ ] Remove user - [ ] Sort user table - [ ] Assign permissions to a user diff --git a/src/main/java/com/dom/freeman/FileIO.java b/src/main/java/com/dom/freeman/FileIO.java index 9613c49..8eb1767 100644 --- a/src/main/java/com/dom/freeman/FileIO.java +++ b/src/main/java/com/dom/freeman/FileIO.java @@ -138,7 +138,6 @@ public boolean modifyExistingItemInFile(Item toModify, FileOperation op) { if (index != -1) { - if (op.equals(FileOperation.EDIT)) items.set(index, toModify); else if (op.equals(FileOperation.REMOVE)) { @@ -176,6 +175,45 @@ else if (op.equals(FileOperation.REMOVE)) { return success; } + + public boolean modifyExistingUserInFile(FileOperation op, User toModify) { + + boolean success = false; + + List users = this.parseUsersFromFile(); + int index = -1; + for (User user : users) { + if (user.getId().equals(toModify.getId())) { + index = users.indexOf(user); + } + } + + if (index != -1) { + + if (op.equals(FileOperation.EDIT)) { + users.set(index, toModify); + } else if (op.equals(FileOperation.REMOVE)) { + users.remove(index); + } + + try { + FileWriter writer = new FileWriter(new File(Paths.get(Global.OBJECTS.getUserPath()).toString()), false); + CSVWriter csv = new CSVWriter(writer); + + for (User user : users) { + csv.writeNext(user.toCsvString()); + } + + csv.close(); + writer.close(); + success = true; + } catch(IOException e) { + success = false; + } + } + + return success; + } public boolean modifyExistingItemTagsInFile(FileOperation op, ItemTag... toModify) { boolean success = false; diff --git a/src/main/java/com/dom/freeman/Utility.java b/src/main/java/com/dom/freeman/Utility.java index fc905b5..9d1f6ba 100644 --- a/src/main/java/com/dom/freeman/Utility.java +++ b/src/main/java/com/dom/freeman/Utility.java @@ -8,6 +8,7 @@ import com.dom.freeman.components.AbstractInventoryTable; import com.dom.freeman.obj.Item; import com.dom.freeman.obj.ItemTag; +import com.dom.freeman.obj.User; public enum Utility { @@ -33,6 +34,14 @@ public Item getItemById(String id) { } return null; } + + public User getUserById(String id) { + for (User user : Global.OBJECTS.getUsers()) { + if (user.getId().equals(id)) + return user; + } + return null; + } public ItemTag getItemTagByName(String name) { for (ItemTag tag : Global.OBJECTS.getItemTags()) { diff --git a/src/main/java/com/dom/freeman/components/users/UserViewPanel.java b/src/main/java/com/dom/freeman/components/users/UserViewPanel.java index 92e26d3..4816793 100644 --- a/src/main/java/com/dom/freeman/components/users/UserViewPanel.java +++ b/src/main/java/com/dom/freeman/components/users/UserViewPanel.java @@ -1,12 +1,18 @@ package com.dom.freeman.components.users; +import java.util.Arrays; +import java.util.List; + import com.dom.freeman.Global; +import com.dom.freeman.Utility; +import com.dom.freeman.components.users.dialog.EditUserDialog; import com.dom.freeman.components.users.tables.UserViewTable; import com.googlecode.lanterna.gui2.Borders; import com.googlecode.lanterna.gui2.Interactable; import com.googlecode.lanterna.gui2.LayoutManager; import com.googlecode.lanterna.gui2.Panel; import com.googlecode.lanterna.gui2.Window; +import com.googlecode.lanterna.gui2.Window.Hint; import com.googlecode.lanterna.gui2.dialogs.ActionListDialogBuilder; public class UserViewPanel extends Panel { @@ -39,6 +45,9 @@ private void configureContent() { userTable.setSelectAction(new Runnable() { @Override public void run() { + + List data = userTable.getTableModel().getRow(userTable.getSelectedRow()); + new ActionListDialogBuilder().setTitle("SELECT AN ACTION") .addAction("View User Summary", new Runnable() { @Override @@ -49,7 +58,10 @@ public void run() { .addAction("Edit User Details", new Runnable() { @Override public void run() { - System.out.println("edit user"); + EditUserDialog editUser = new EditUserDialog("EDIT USER", + Utility.METHODS.getUserById(data.get(userTable.getTableModel().getColumnCount() - 1))); + editUser.setHints(Arrays.asList(Hint.CENTERED)); + editUser.showDialog(parent.getTextGUI()); } }) .addAction("Edit User Permissions", new Runnable() { diff --git a/src/main/java/com/dom/freeman/components/users/dialog/EditUserDialog.java b/src/main/java/com/dom/freeman/components/users/dialog/EditUserDialog.java new file mode 100644 index 0000000..847c906 --- /dev/null +++ b/src/main/java/com/dom/freeman/components/users/dialog/EditUserDialog.java @@ -0,0 +1,101 @@ +package com.dom.freeman.components.users.dialog; + +import java.util.Arrays; + +import com.dom.freeman.FileIO; +import com.dom.freeman.Utility; +import com.dom.freeman.obj.FileOperation; +import com.dom.freeman.obj.User; +import com.googlecode.lanterna.gui2.dialogs.MessageDialogBuilder; +import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton; + +public class EditUserDialog extends AbstractModifyUserDialog { + + private User toEdit; + + public EditUserDialog(String title, User toEdit) { + super(title); + this.toEdit = toEdit; + + this.getFirstNameEntry().setText(this.toEdit.getFirstName()); + this.getLastNameEntry().setText(this.toEdit.getLastName()); + this.getDisplayNameEntry().setText(this.toEdit.getDisplayName()); + } + + @Override + public boolean validateUser() { + + boolean valid = true; + + if (this.getFirstNameEntry().getText().replaceAll(" ", "").isEmpty()) { + new MessageDialogBuilder().setTitle("Create User Validation") + .setText("You did not enter anything for first name. This field cannot be empty.") + .setExtraWindowHints(Arrays.asList(Hint.CENTERED)) + .addButton(MessageDialogButton.OK).build().showDialog(this.getTextGUI()); + valid = false; + } + + if (this.getLastNameEntry().getText().replaceAll(" ", "").isEmpty()) { + new MessageDialogBuilder().setTitle("Create User Validation") + .setText("You did not enter anything for last name. This field cannot be empty.") + .setExtraWindowHints(Arrays.asList(Hint.CENTERED)) + .addButton(MessageDialogButton.OK).build().showDialog(this.getTextGUI()); + valid = false; + } + + if (this.getDisplayNameEntry().getText().replaceAll(" ", "").isEmpty()) { + new MessageDialogBuilder().setTitle("Create User Validation") + .setText("You did not enter anything for display name. This field cannot be empty.") + .setExtraWindowHints(Arrays.asList(Hint.CENTERED)) + .addButton(MessageDialogButton.OK).build().showDialog(this.getTextGUI()); + valid = false; + } + + return valid; + } + + @Override + public void onSave() { + + if (this.validateUser()) { + User user = new User( + this.getFirstNameEntry().getText(), + this.getLastNameEntry().getText(), + this.getDisplayNameEntry().getText(), + this.toEdit.getId()); + + ModifyUserSummaryDialog summary = new ModifyUserSummaryDialog( + "FINAL EDIT USER SUMMARY" , FileOperation.EDIT, user, this.toEdit); + summary.setHints(Arrays.asList(Hint.CENTERED)); + + if (summary.showDialog(this.getTextGUI())) + this.saveUser(user); + } + } + + private void saveUser(User user) { + + boolean write = FileIO.METHODS.modifyExistingUserInFile(FileOperation.EDIT, user); + + if (write) { + new MessageDialogBuilder().setTitle("User Edited Successfully") + .setText("User successfully updated.") + .setExtraWindowHints(Arrays.asList(Hint.CENTERED)) + .addButton(MessageDialogButton.OK).build().showDialog(this.getTextGUI()); + this.close(); + Utility.METHODS.updateInventory(); + Utility.METHODS.refreshViews(); + } else { + new MessageDialogBuilder().setTitle("Warning") + .setText("Some error occurred and the user could not be updated. Please try again.") + .setExtraWindowHints(Arrays.asList(Hint.CENTERED)) + .addButton(MessageDialogButton.OK).build().showDialog(this.getTextGUI()); + } + } + + @Override + public void onCancel() { + this.close(); + } + +} diff --git a/src/main/java/com/dom/freeman/components/users/dialog/ModifyUserSummaryDialog.java b/src/main/java/com/dom/freeman/components/users/dialog/ModifyUserSummaryDialog.java index d3fc34f..35b2241 100644 --- a/src/main/java/com/dom/freeman/components/users/dialog/ModifyUserSummaryDialog.java +++ b/src/main/java/com/dom/freeman/components/users/dialog/ModifyUserSummaryDialog.java @@ -31,6 +31,7 @@ public ModifyUserSummaryDialog(String title, FileOperation op, User newUser) { public ModifyUserSummaryDialog(String title, FileOperation op, User newUser, User oldUser) { super(title); this.op = op; + this.newUser = newUser; this.oldUser = oldUser; this.configureContent(3); } @@ -105,7 +106,7 @@ private Table editUserTable() { this.oldUser.getDisplayName() }); - return null; + return summary; } private String labelMessage() { diff --git a/src/main/resources/users.csv b/src/main/resources/users.csv index a34be13..942ff1b 100644 --- a/src/main/resources/users.csv +++ b/src/main/resources/users.csv @@ -1,3 +1,3 @@ -Donald,Trump,GEOTUS,3d1e48ce-a651-48b6-bd8f-e540fe8db73b, -Richard,Stallman,rms.sexy,bfff8b19-b48f-41d7-b000-942646135f45, -Terry,Davis,extinguisher,0a85af4c-18cb-401f-bace-efcc42de11c7, +"Donald","Trump","GEOTUS","3d1e48ce-a651-48b6-bd8f-e540fe8db73b","" +"Richard","Stallman","rms.sexy","bfff8b19-b48f-41d7-b000-942646135f45","" +"Terry","Davis","temple","0a85af4c-18cb-401f-bace-efcc42de11c7",""