Skip to content

Commit

Permalink
Ability to edit a user
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdps93 committed Apr 11, 2019
1 parent 5bf6820 commit 5e32d29
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/com/dom/freeman/FileIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -176,6 +175,45 @@ else if (op.equals(FileOperation.REMOVE)) {

return success;
}

public boolean modifyExistingUserInFile(FileOperation op, User toModify) {

boolean success = false;

List<User> 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;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/dom/freeman/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -39,6 +45,9 @@ private void configureContent() {
userTable.setSelectAction(new Runnable() {
@Override
public void run() {

List<String> data = userTable.getTableModel().getRow(userTable.getSelectedRow());

new ActionListDialogBuilder().setTitle("SELECT AN ACTION")
.addAction("View User Summary", new Runnable() {
@Override
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -105,7 +106,7 @@ private Table<String> editUserTable() {
this.oldUser.getDisplayName()
});

return null;
return summary;
}

private String labelMessage() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/users.csv
Original file line number Diff line number Diff line change
@@ -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",""

0 comments on commit 5e32d29

Please sign in to comment.