-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
3482 lines (3083 loc) · 136 KB
/
Copy pathMain.java
File metadata and controls
3482 lines (3083 loc) · 136 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package project;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/*******
* <p> Main class </p>
*
* <p> Description: It represents the main function of the whole application.</p>
* <p> Stores and creates the VBox used to create the help system. </p>
* <p> Manages the GUI and includes the user creation and management </p>
* <p> Collaborators: Role, Admin, User</p>
*
* @author Hassan Khan, Colby Taylor, Xavier Flores, Shashwat Balaji, Avinash Poguluri, Abil Damirbek uulu
*/
public class Main extends Application {
private static List<User> userList = new ArrayList<>();
private Admin adminUser = null;
private User currentUser = null;
private TextArea outputArea = new TextArea();
private VBox optionBox = new VBox(10); // Reusable optionBox to prevent multiple instances
private static DatabaseHelper databaseHelper; // database variable
private Role currRole; // keeps track of the current role of the user
private static List<String> genericQuestions = new ArrayList<>(); // list of generic questions
private static List<String> specificQuestions = new ArrayList<>(); // list of specific questions
private static List<SpecialAccessGroup> specialAccessGroupsList = new ArrayList<>(); // list of special access groups
private static List<String> generalArticleGroupsList = new ArrayList<>();
/**********************************************************************************************
* This is the method that performs the test cases
*
* @param args The standard argument list for a Java Mainline
*
*/
public static void main(String[] args) {
launch(args);
}
/*********
* This is the method that create the VBox from javafx and sets up the starting
* screen of the help system.
*
* @param theStage The pop up stage that handles all of the user interaction
*/
@Override
public void start(Stage theStage) throws Exception { // Method for the first user account created (admin)
theStage.setTitle("ASU Help System");
// Initialize the database helper
databaseHelper = new DatabaseHelper();
try {
// connect to database
databaseHelper.connectToDatabase(); // Connect to the database
// Load users from the database into the userList
userList = databaseHelper.loadUsersFromDatabase();
} catch (Exception e) {
outputArea.appendText("Error loading users from the database: " + e.getMessage() + "\n");
}
// Check if userList is empty
if (userList.isEmpty()) {
Pane firstLogin = new Pane();
VBox formContainer = new VBox(5); // Main container for all components
formContainer.setAlignment(Pos.CENTER);
formContainer.setPadding(new Insets(10, 10, 20, 10)); // Add bottom padding for buffer
Text welcomeText = new Text("Welcome to the ASU Help System");
Button createUserButton = new Button("Create Admin");
formContainer.getChildren().addAll(welcomeText, createUserButton, outputArea);
outputArea.setPrefHeight(200);
outputArea.setEditable(false);
Scene mainScene = new Scene(formContainer, 500, 800);
theStage.setScene(mainScene);
// Create user fields and labels
Label usernameLabel = new Label("Enter Username:");
TextField usernameInput = new TextField();
Label passwordLabel = new Label("Enter Password:");
PasswordField passwordInput = new PasswordField();
Label confirmPasswordLabel = new Label("Confirm Password:");
PasswordField confirmPasswordInput = new PasswordField();
// Button creation
Button submitButton = new Button("Submit");
Button cancelButton = new Button("Cancel");
// Add all components to form container (initially hidden)
formContainer.getChildren().addAll(
usernameLabel, usernameInput,
passwordLabel, passwordInput,
confirmPasswordLabel, confirmPasswordInput,
submitButton, cancelButton
);
// components are initially not visible
usernameLabel.setVisible(false);
usernameInput.setVisible(false);
passwordLabel.setVisible(false);
passwordInput.setVisible(false);
confirmPasswordLabel.setVisible(false);
confirmPasswordInput.setVisible(false);
submitButton.setVisible(false);
cancelButton.setVisible(false);
// Event handler to show admin creation form
createUserButton.setOnAction(event -> {
outputArea.appendText("You are the first user and will be made an Admin.\n");
usernameLabel.setVisible(true);
usernameInput.setVisible(true);
passwordLabel.setVisible(true);
passwordInput.setVisible(true);
confirmPasswordLabel.setVisible(true);
confirmPasswordInput.setVisible(true);
submitButton.setVisible(true);
cancelButton.setVisible(true);
createUserButton.setDisable(true);
});
// Submit button handler for admin creation
submitButton.setOnAction(event -> {
String username = usernameInput.getText();
char[] password = passwordInput.getText().toCharArray();
char[] confirmPassword = confirmPasswordInput.getText().toCharArray();
// create admin
if (Arrays.equals(password, confirmPassword)) {
if (adminUser == null) {
adminUser = new Admin(username, password);
adminUser.addRole(Role.ADMIN);
userList.add(adminUser);
currentUser = adminUser;
outputArea.appendText("Admin account created.\n");
} else {
User newUser = new User(username, password);
newUser.addRole(Role.STUDENT); // Default role for regular users
userList.add(newUser);
currentUser = newUser;
outputArea.appendText("User account created.\n");
}
usernameLabel.setVisible(false);
usernameInput.setVisible(false);
passwordLabel.setVisible(false);
passwordInput.setVisible(false);
confirmPasswordLabel.setVisible(false);
confirmPasswordInput.setVisible(false);
submitButton.setVisible(false);
cancelButton.setVisible(false);
collectUserInfo(); // Collect additional user information
} else {
outputArea.appendText("Passwords do not match. Please try again.\n");
}
});
cancelButton.setOnAction(event -> {
usernameInput.clear();
passwordInput.clear();
confirmPasswordInput.clear();
createUserButton.setDisable(false);
});
theStage.show();
}
else {
// Set up the main container for existing users
Pane existingUserPane = new Pane();
VBox existingUserContainer = new VBox(5); // Main container for all components
existingUserContainer.setAlignment(Pos.CENTER);
existingUserContainer.setPadding(new Insets(10, 10, 20, 10)); // Add bottom padding for buffer
// Welcome text
Text existingUserText = new Text("Welcome back to the ASU Help System");
existingUserContainer.getChildren().add(existingUserText);
// Output area for messages
outputArea.setPrefHeight(200);
outputArea.setEditable(false);
existingUserContainer.getChildren().add(outputArea);
// Set the scene
Scene existingUserScene = new Scene(existingUserContainer, 500, 800);
theStage.setScene(existingUserScene);
showSignInOrCreateAccount();
theStage.show();
}
}
/*********
* This is the method used to collect the rest of the user details when signing up.
* This involves collecting the first name, middle name, preferred name, last name and email
*
*/
private void collectUserInfo() {
outputArea.appendText("Please enter your personal details:\n");
// Create input fields for user details
Label firstNameLabel = new Label("First Name:");
TextField firstNameInput = new TextField();
Label middleNameLabel = new Label("Middle Name:");
TextField middleNameInput = new TextField();
Label lastNameLabel = new Label("Last Name:");
TextField lastNameInput = new TextField();
Label preferredNameLabel = new Label("Preferred Name:");
TextField preferredNameInput = new TextField();
Label emailLabel = new Label("Email:");
TextField emailInput = new TextField();
Button submitDetailsButton = new Button("Submit Details");
Button cancelDetailsButton = new Button("Cancel");
// Create a VBox to hold the user info inputs
VBox userInfoBox = new VBox(10, firstNameLabel, firstNameInput,
middleNameLabel, middleNameInput,
lastNameLabel, lastNameInput,
preferredNameLabel, preferredNameInput,
emailLabel, emailInput,
submitDetailsButton, cancelDetailsButton);
userInfoBox.setAlignment(Pos.CENTER);
((VBox) outputArea.getParent()).getChildren().add(userInfoBox);
// Submit button action for user details
submitDetailsButton.setOnAction(event -> {
String firstName = firstNameInput.getText();
String middleName = middleNameInput.getText();
String lastName = lastNameInput.getText();
String preferredName = preferredNameInput.getText();
String email = emailInput.getText();
// Set the user details in the current user object
if (currentUser != null) {
currentUser.setFirstName(firstName);
currentUser.setMiddleName(middleName);
currentUser.setLastName(lastName);
currentUser.setPreferredName(preferredName);
currentUser.setEmail(email);
}
outputArea.appendText("User details saved.\n");
// Take user to the login screen
((VBox) outputArea.getParent()).getChildren().remove(userInfoBox);
showSignInOrCreateAccount();
/*if (currentUser instanceof Admin) {
((VBox) outputArea.getParent()).getChildren().remove(userInfoBox);
showSignInOrCreateAccount();
}
else {
// Clear the user info box
((VBox) outputArea.getParent()).getChildren().remove(userInfoBox);
//Set<Role> currRole = currentUser.getRoles();
showUserOptions(currRole); // Show the options for the student user
} */
});
// Cancel button action
cancelDetailsButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(userInfoBox);
loginPrompt(); // Show the options for the user
});
}
/*********
* This is the method that acts as the home page for the users.
* Based on what role the user is the permissions will be different
*
* @param role The role of the user used to determine the visible options
*/
private void showUserOptions(Role role) {
outputArea.appendText("What would you like to do? Options:\n");
clearPreviousOptionBox(); // Ensure only one options box is visible
// Clear the optionBox before adding new options
optionBox.getChildren().clear();
// Create buttons that multiple users have
Button signOutButton = new Button("Sign out");
Button quitButton = new Button("Quit");
Button manageStudentsButton = new Button("Manage Students");
Button aritcleButton = new Button("Article settings");
Button generalArticleOptions = new Button("General article group options");
// Add admin options only if the current user is an admin
if (currRole == Role.ADMIN) {
Button printUsersButton = new Button("Print users");
Button deleteUserButton = new Button("Delete user");
Button inviteUserButton = new Button("Invite a user");
Button addOrRemoveRole = new Button("Add or remove a users role");
Button resetUserButton = new Button("Reset a user's password");
// Set button actions
printUsersButton.setOnAction(e -> listUsers());
deleteUserButton.setOnAction(e -> deleteUser());
inviteUserButton.setOnAction(e -> inviteUser());
addOrRemoveRole.setOnAction(e -> addRemoveRole());
resetUserButton.setOnAction(e -> resetUser());
aritcleButton.setOnAction(e -> {
try {
articleOptions();
} catch (Exception e1) {
outputArea.appendText("Error going to article options:\n");
e1.printStackTrace();
}
});
optionBox.getChildren().addAll(
new Label("Select an option:"),
signOutButton,
printUsersButton,
deleteUserButton,
inviteUserButton,
addOrRemoveRole,
resetUserButton,
aritcleButton,
generalArticleOptions,
quitButton
);
}
else if (currRole == Role.INSTRUCTOR) {
// Button declaration
Button specialAccessGroups = new Button("Special access group options");
optionBox.getChildren().addAll(
new Label("Select an option:"),
aritcleButton,
generalArticleOptions,
specialAccessGroups,
manageStudentsButton,
signOutButton,
quitButton
);
aritcleButton.setOnAction(e -> {
try {
articleOptions();
} catch (Exception e1) {
outputArea.appendText("Error going to article options:\n");
e1.printStackTrace();
}
});
generalArticleOptions.setOnAction(e -> {
generalArticleGroupOptions();
});
manageStudentsButton.setOnAction(e -> manageStudents());
specialAccessGroups.setOnAction(e -> {
try {
specialAccessGroupOptions();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
}
else if (currRole == Role.STUDENT) {
// For regular users and instructors, only show sign out and quit options
Button sendGenericMsg = new Button("Send generic message");
Button sendSpecificMsg = new Button("Send specific message");
Button viewArticles = new Button("View articles");
optionBox.getChildren().addAll(
new Label("Select an option:"),
sendGenericMsg,
sendSpecificMsg,
viewArticles,
signOutButton,
quitButton
);
sendGenericMsg.setOnAction( e -> {
sendGenericMessage();
});
sendSpecificMsg.setOnAction( e -> {
sendSpecificMessage();
});
viewArticles.setOnAction(( e-> {
try {
studentListArticles();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}));
}
// Set sign out, quit and manage student button actions
signOutButton.setOnAction(e -> signOut());
//manageStudentsButton.setOnAction(e -> manageStudents());
quitButton.setOnAction(e -> {
// Save the updated user list to the database
try {
databaseHelper.saveUserListToDatabase(userList);
} catch (Exception e1) {
outputArea.appendText("Error saving list to database\n");
e1.printStackTrace();
}
outputArea.appendText("Goodbye!\n");
System.exit(0);
});
optionBox.setAlignment(Pos.CENTER);
((VBox) outputArea.getParent()).getChildren().add(optionBox);
}
/*********
* This is the method used to sign out.
*/
private void signOut() {
outputArea.appendText("You have signed out.\n");
currentUser = null;
showSignInOrCreateAccount();
}
/*********
* This is the method used to show the options when logged out
*/
private void showSignInOrCreateAccount() {
outputArea.appendText("Select an option:\n");
clearPreviousOptionBox(); // Ensure only one options box is visible
// Clear the optionBox before adding new options
optionBox.getChildren().clear();
// Create buttons for login and create account options
Button loginButton = new Button("Login");
Button createAccountButton = new Button("Create Account");
// Set button actions
loginButton.setOnAction(e -> loginPrompt());
createAccountButton.setOnAction(e -> createAccount());
// Add buttons to the option box
optionBox.getChildren().setAll(
new Label("Select an option:"),
loginButton,
createAccountButton
);
optionBox.setAlignment(Pos.CENTER);
((VBox) outputArea.getParent()).getChildren().add(optionBox);
}
/*********
* This is the method used to show the login prompt when clicked on login
*/
private void loginPrompt() {
outputArea.appendText("Enter username and password to log in or enter an invitation code.\n");
Label usernameLabel = new Label("Username:");
TextField usernameInput = new TextField();
Label passwordLabel = new Label("Password:");
PasswordField passwordInput = new PasswordField();
Button loginButton = new Button("Login");
Button invitationButton = new Button("I have an invite code");
Button forgotPasswordButton = new Button("I forgot my password");
Button backButton = new Button("Back");
VBox loginBox = new VBox(10, usernameLabel, usernameInput, passwordLabel, passwordInput,
loginButton, invitationButton, forgotPasswordButton, backButton);
loginBox.setAlignment(Pos.CENTER);
((VBox) outputArea.getParent()).getChildren().add(loginBox);
clearPreviousOptionBox();
loginButton.setOnAction(event -> {
String username = usernameInput.getText();
char[] password = passwordInput.getText().toCharArray();
boolean userFound = false;
for (User user : userList) {
if (user.getUsername().equals(username) && Arrays.equals(password, user.getPassword())) {
outputArea.appendText("Login successful.\n");
currentUser = user;
userFound = true;
((VBox) outputArea.getParent()).getChildren().remove(loginBox);
promptRoleSelection(currentUser);
break;
}
}
if (!userFound) {
outputArea.appendText("User not found or incorrect password.\n");
}
});
// if the invite button is clicked the user will be taken to the invitation screen
invitationButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(loginBox);
inviteLogin();
});
// If the forgot password button is clicked
forgotPasswordButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(loginBox);
resetLogin(); // Redirect to the reset password screen
});
// Back button functionality
backButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(loginBox);
showSignInOrCreateAccount(); // Show the sign-in/create account options again
});
}
/*********
* This is the method that prompts the user for what role they want to choose
* for the current session
*
* @param user The user who is currently signed in
*/
private void promptRoleSelection(User user) {
outputArea.appendText("Select a role for this session:\n");
clearPreviousOptionBox(); // Clear previous UI elements
optionBox.getChildren().clear();
ToggleGroup roleToggleGroup = new ToggleGroup();
// Add radio buttons for each role
for (Role role : user.getRoles()) {
RadioButton roleOption = new RadioButton(role.toString());
roleOption.setToggleGroup(roleToggleGroup);
optionBox.getChildren().add(roleOption);
}
Button submitRoleButton = new Button("Submit");
optionBox.getChildren().add(submitRoleButton);
// Handle role selection
submitRoleButton.setOnAction(e -> {
// retrieve the selected button
RadioButton selectedRadioButton = (RadioButton) roleToggleGroup.getSelectedToggle();
// if admin was selected
if (selectedRadioButton != null && selectedRadioButton.getText().equals("ADMIN")) {
currRole = Role.ADMIN;
showUserOptions(currRole);
}
// if instructor was selected
else if (selectedRadioButton != null && selectedRadioButton.getText().equals("INSTRUCTOR")) {
currRole = Role.INSTRUCTOR;
showUserOptions(currRole);
}
// if student was selected
else if (selectedRadioButton != null && selectedRadioButton.getText().equals("STUDENT")) {
currRole = Role.STUDENT;
showUserOptions(currRole);
}
else {
outputArea.appendText("Please select a role.\n");
}
});
optionBox.setAlignment(Pos.CENTER);
((VBox) outputArea.getParent()).getChildren().add(optionBox);
}
/*********
* This is the method used to show the login for when invite user is clicked.
*/
private void inviteLogin() {
// Inform the user
outputArea.appendText("Enter your invite code.\n");
// Create input fields for the invite code, username, and password
Label inviteLabel = new Label("Invite Code:");
TextField inviteInput = new TextField();
Label usernameLabel = new Label("Username:");
TextField usernameInput = new TextField();
Label passwordLabel = new Label("Password:");
PasswordField passwordInput = new PasswordField();
Label confirmPasswordLabel = new Label("Confirm Password:");
PasswordField confirmPasswordInput = new PasswordField();
// Create buttons
Button submitButton = new Button("Submit");
Button backButton = new Button("Back");
// Layout for the invite login form
VBox inviteBox = new VBox(10, inviteLabel, inviteInput, usernameLabel, usernameInput, passwordLabel,
passwordInput, confirmPasswordLabel, confirmPasswordInput, submitButton, backButton);
inviteBox.setAlignment(Pos.CENTER);
((VBox) outputArea.getParent()).getChildren().add(inviteBox);
// Clear any previous options
clearPreviousOptionBox();
// Event handler for the submit button
submitButton.setOnAction(event -> {
// Get the invite code and validate it
String inviteCode = inviteInput.getText();
// Single role codes
if (inviteCode.equals("STUDENTINVCODE")) {
processInviteCode(usernameInput, passwordInput, confirmPasswordInput, Arrays.asList(Role.STUDENT), inviteBox);
} else if (inviteCode.equals("INSTRUCTORINVCODE")) {
processInviteCode(usernameInput, passwordInput, confirmPasswordInput, Arrays.asList(Role.INSTRUCTOR), inviteBox);
} else if (inviteCode.equals("ADMININVCODE")) {
processInviteCode(usernameInput, passwordInput, confirmPasswordInput, Arrays.asList(Role.ADMIN), inviteBox);
}
// Multiple roles codes
else if (inviteCode.equals("STUDENTINSINVCODE")) {
processInviteCode(usernameInput, passwordInput, confirmPasswordInput, Arrays.asList(Role.STUDENT, Role.INSTRUCTOR), inviteBox);
} else if (inviteCode.equals("STUADINVCODE")) {
processInviteCode(usernameInput, passwordInput, confirmPasswordInput, Arrays.asList(Role.STUDENT, Role.ADMIN), inviteBox);
} else if (inviteCode.equals("ADMININSINVCODE")) {
processInviteCode(usernameInput, passwordInput, confirmPasswordInput, Arrays.asList(Role.INSTRUCTOR, Role.ADMIN), inviteBox);
} else if (inviteCode.equals("ADMININSSTUINVCODE")) {
processInviteCode(usernameInput, passwordInput, confirmPasswordInput, Arrays.asList(Role.STUDENT, Role.INSTRUCTOR, Role.ADMIN), inviteBox);
} else {
outputArea.appendText("Invalid invitation code. Please try again.\n");
}
});
// Event handler for the back button
backButton.setOnAction(event -> {
// Remove the invite login form and show the login page again
((VBox) outputArea.getParent()).getChildren().remove(inviteBox);
loginPrompt(); // Return to the login screen
});
}
/*********
* This is the method used to display the reset login page
*/
private void resetLogin() {
outputArea.appendText("Enter your username and the OTP sent to your email:\n");
// Label and text fields for entering username and OTP
Label usernameLabel = new Label("Username:");
TextField usernameField = new TextField();
Label otpLabel = new Label("OTP:");
TextField otpField = new TextField();
// Button to confirm OTP
Button confirmOtpButton = new Button("Confirm OTP");
Button backButton = new Button("Back");
// VBox layout to arrange the components vertically
VBox resetBox = new VBox(10, usernameLabel, usernameField, otpLabel, otpField, confirmOtpButton, backButton);
resetBox.setAlignment(Pos.CENTER); // Align the components to the center
// Add the resetBox to the existing VBox containing the outputArea
((VBox) outputArea.getParent()).getChildren().add(resetBox);
// Clear any previous option boxes
clearPreviousOptionBox();
// Set the action for when the "Confirm OTP" button is pressed
confirmOtpButton.setOnAction(event -> {
String username = usernameField.getText().trim();
String enteredOtp = otpField.getText().trim();
User user = null; // Initialize user as null
// Iterate through userList to find the user by username
for (User u : userList) { // Assuming userList is a List<User> in your class
if (u.getUsername().equals(username)) {
user = u; // Set user if found
break; // Exit the loop
}
}
// Check if user exists and if the entered OTP matches the user's OTP
if (user != null && user.getOneTimePassword() != null && user.getOneTimePassword().equals(enteredOtp)) {
outputArea.appendText("OTP verified successfully. Please enter your new password:\n");
((VBox) outputArea.getParent()).getChildren().remove(resetBox); // Remove current box
showNewPasswordForm(user); // Show form for new password
} else {
outputArea.appendText("Invalid username or OTP. OTP may have also expired. Please try again.\n");
}
});
// Set the action for when the "Back" button is pressed
backButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(resetBox); // Remove resetBox
loginPrompt(); // Show login prompt again when going back
});
}
/*********
* This is the method used for generating a new password after successful OTP verification
*/
private void showNewPasswordForm(User user) {
outputArea.appendText("Enter your new password:\n");
// Label and text fields for new password
Label newPasswordLabel = new Label("New Password:");
PasswordField newPasswordField = new PasswordField();
Label confirmPasswordLabel = new Label("Confirm Password:");
PasswordField confirmPasswordField = new PasswordField();
Button updatePasswordButton = new Button("Update Password");
Button cancelButton = new Button("Cancel");
// VBox layout for new password input
VBox passwordBox = new VBox(10, newPasswordLabel, newPasswordField, confirmPasswordLabel, confirmPasswordField, updatePasswordButton, cancelButton);
passwordBox.setAlignment(Pos.CENTER);
// Add the passwordBox to the existing VBox containing the outputArea
((VBox) outputArea.getParent()).getChildren().add(passwordBox);
// Set the action for when the "Update Password" button is pressed
updatePasswordButton.setOnAction(event -> {
char[] newPassword = newPasswordField.getText().toCharArray();
char[] confirmPassword = confirmPasswordField.getText().toCharArray();
if (Arrays.equals(newPassword, confirmPassword) && newPassword.length > 0) {
user.setPassword(newPassword); // Update user's password
outputArea.appendText("Password updated successfully. You can now log in with your new password.\n");
((VBox) outputArea.getParent()).getChildren().remove(passwordBox); // Remove passwordBox
loginPrompt(); // Redirect to login
} else {
outputArea.appendText("Passwords do not match or are invalid. Please try again.\n");
}
});
// Set the action for when the "Cancel" button is pressed
cancelButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(passwordBox); // Remove passwordBox
loginPrompt(); // Redirect to login
});
}
/*********
* This is the method used to create a new user given the details from the invite screen
*
* @param usernameInput what the user inputs for username
* @param passwordInput what the user inputs for password
* @param confirmPasswordInput what the user inputs for confirmPasswordInput
* @param roles list of type Role of roles
* @param createBox the VBox used for javafx
*/
private void processInviteCode(TextField usernameInput, PasswordField passwordInput, PasswordField confirmPasswordInput, List<Role> roles, VBox createBox) {
String username = usernameInput.getText();
char[] password = passwordInput.getText().toCharArray();
char[] confirmPassword = confirmPasswordInput.getText().toCharArray();
// Check if passwords match
if (Arrays.equals(password, confirmPassword)) {
// create new user
User newUser = new User(username, password);
// Add all roles to the new user
for (Role role : roles) {
newUser.addRole(role);
}
// Collect additional user information
clearPreviousOptionBox();
collectUserInfo(); // Pass the newUser object to collectUserInfo
userList.add(newUser);
currentUser = newUser;
// Notify user of the roles created
outputArea.appendText("Account was successfully invited.\n");
// After account creation, transition back to the login screen
((VBox) outputArea.getParent()).getChildren().remove(createBox);
} else {
outputArea.appendText("Passwords don't match. Please try again.\n");
}
}
/*********
* This is the method used to create the account
*/
private void createAccount() {
outputArea.appendText("Enter details to create a new account.\n");
// Create input fields for username and password
Label usernameLabel = new Label("Username:");
TextField usernameInput = new TextField();
Label passwordLabel = new Label("Password:");
PasswordField passwordInput = new PasswordField();
Label confirmPasswordLabel = new Label("Confirm Password:");
PasswordField confirmPasswordInput = new PasswordField();
// Create radio buttons for user roles
RadioButton studentRadioButton = new RadioButton("Student");
RadioButton instructorRadioButton = new RadioButton("Instructor");
ToggleGroup roleToggleGroup = new ToggleGroup();
studentRadioButton.setToggleGroup(roleToggleGroup);
instructorRadioButton.setToggleGroup(roleToggleGroup);
studentRadioButton.setSelected(true);
// Create buttons
Button createButton = new Button("Create");
Button backButton = new Button("Back");
// Create a VBox for input fields
VBox createBox = new VBox(10, usernameLabel, usernameInput, passwordLabel, passwordInput,
confirmPasswordLabel, confirmPasswordInput,
studentRadioButton, instructorRadioButton,
createButton, backButton);
createBox.setAlignment(Pos.CENTER);
((VBox) outputArea.getParent()).getChildren().add(createBox);
clearPreviousOptionBox();
createButton.setOnAction(event -> {
String username = usernameInput.getText();
char[] password = passwordInput.getText().toCharArray();
char[] confirmPassword = confirmPasswordInput.getText().toCharArray();
if (Arrays.equals(password, confirmPassword)) {
User newUser = new User(username, password);
if (instructorRadioButton.isSelected()) {
newUser.addRole(Role.INSTRUCTOR);
outputArea.appendText("Instructor account created successfully.\n");
} else {
newUser.addRole(Role.STUDENT);
outputArea.appendText("Student account created successfully.\n");
}
// Collect additional user information
collectUserInfo(); // Pass the newUser object to collectUserInfo
userList.add(newUser);
currentUser = newUser;
((VBox) outputArea.getParent()).getChildren().remove(createBox);
} else {
outputArea.appendText("Passwords do not match. Please try again.\n");
}
});
backButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(createBox);
showSignInOrCreateAccount(); // Show the sign-in/create account options again
});
}
/*********
* This is the method used for admins to invite a user
*/
private void inviteUser() {
// Clear previous output and prepare the invite user view
outputArea.appendText("Invite a new user.\n");
// Label above check boxs
Label title = new Label("Check the roles to assign to the new user:");
// Check boxes for role selection (multiple roles can be selected)
CheckBox studentCheckBox = new CheckBox("Student");
CheckBox instructorCheckBox = new CheckBox("Instructor");
CheckBox adminCheckBox = new CheckBox("Admin");
// Button to invite the user
Button inviteButton = new Button("Invite User");
Button backButton = new Button("Back");
// VBox layout to arrange the components vertically
VBox inviteBox = new VBox(10, title, studentCheckBox, instructorCheckBox, adminCheckBox,
inviteButton, backButton);
inviteBox.setAlignment(Pos.CENTER); // Align the components to the center
// Add the new inviteBox to the existing VBox containing the outputArea
((VBox) outputArea.getParent()).getChildren().add(inviteBox);
// Clear any previous option boxes
clearPreviousOptionBox();
// Set the action for when the "Invite User" button is pressed
inviteButton.setOnAction(event -> {
// Add roles and print messages based on which check boxes are selected
// student + instructor + admin
if (studentCheckBox.isSelected() && instructorCheckBox.isSelected() && adminCheckBox.isSelected()) {
outputArea.appendText("Invite code: ADMININSSTUINVCODE\n");
}
// student + instructor
else if (studentCheckBox.isSelected() && instructorCheckBox.isSelected()) {
outputArea.appendText("Invite code: STUDENTINSINVCODE\n");
}
// student + admin
else if (studentCheckBox.isSelected() && adminCheckBox.isSelected()) {
outputArea.appendText("Invite code: STUADINVCODE\n");
}
// instructor + admin
else if (instructorCheckBox.isSelected() && adminCheckBox.isSelected()) {
outputArea.appendText("Invite code: ADMININSINVCODE\n");
}
// student
else if (studentCheckBox.isSelected()) {
outputArea.appendText("Invite code: STUDENTINVCODE\n");
}
// instructor
else if (instructorCheckBox.isSelected()) {
outputArea.appendText("Invite code: INSTRUCTORINVCODE\n");
}
// admin
else if (adminCheckBox.isSelected()) {
outputArea.appendText("Invite code: ADMININVCODE\n");
}
else {
outputArea.appendText("No role selected. Please select at least one role.\n");
}
// Remove the createBox from the UI after successful account creation
((VBox) outputArea.getParent()).getChildren().remove(inviteBox);
showUserOptions(Role.ADMIN);
});
// Set the action for when the "Back" button is pressed
backButton.setOnAction(event -> {
((VBox) outputArea.getParent()).getChildren().remove(inviteBox); // Remove the inviteBox
showUserOptions(Role.ADMIN); // Show options again when going back();
});
}
/*********
* This is the method used by the admin to reset the password of a given user
*/
private void resetUser() {
outputArea.appendText("Enter the username for password reset:\n");
// Label and text field for entering the username
Label title = new Label("Username:");
TextField usernameField = new TextField();
// Button to generate OTP for password reset
Button generateOTPButton = new Button("Generate OTP");
Button backButton = new Button("Back");
// VBox layout to arrange the components vertically
VBox resetBox = new VBox(10, title, usernameField, generateOTPButton, backButton);
resetBox.setAlignment(Pos.CENTER); // Align the components to the center
// Add the resetBox to the existing VBox containing the outputArea
((VBox) outputArea.getParent()).getChildren().add(resetBox);
// Clear any previous option boxes
clearPreviousOptionBox();
// Set the action for when the "Generate OTP" button is pressed
generateOTPButton.setOnAction(event -> {
String username = usernameField.getText().trim();
if (!username.isEmpty()) {
User[] foundUser = new User[1]; // Use an array to hold the found user
// Find the user by username within the same method
for (User user : userList) { // Assuming userList is a collection of users
if (user.getUsername().equals(username)) {
foundUser[0] = user; // Store the found user
break; // Exit the loop if the user is found
}
}
if (foundUser[0] != null) {
String otp = generateRandomString(7 + (int)(Math.random() * 6)); // Generate OTP
foundUser[0].setOneTimePassword(otp); // Set the OTP for the user
outputArea.appendText("OTP generated and sent to the user's email. It will expire in 5min.\n");
outputArea.appendText("Generated OTP for user " + username + ": " + otp + "\n");
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(() -> {
String otpExpired = generateRandomString(7 + (int)(Math.random() * 6)); // Generate new OTP