Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
1283 lines (1147 loc) · 42.7 KB
/
Copy pathUser.java
File metadata and controls
1283 lines (1147 loc) · 42.7 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 application.model;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.sql.Statement;
import java.util.ArrayList;
/**This Class is used to represent the application's users and
* all pertinent information relating to his/her site activity
*
*/
public class User {
private static URL lastHub;
private static String userName;
private static int userID;
private static LifeHub currentHub;
private static ArrayList<LifeHub> userHubs;
private static String currentClass;
private static ArrayList<Contact> userContacts;
private static ArrayList<HubEvent> userEvents;
private static ArrayList<SchoolClass> classes;
private String userLoggedInName;
private String password;
/**This a parameterized constructor for the Task class
*
* @param user - String that represents the user's log-in name
* @param password - String that represents user's password
* @return none
*/
public User(String user, String password) {
this.userLoggedInName = user;
this.password = password;
}
/**This method is used to close any open database connection
*
* @param Connection - conn
* @return none
*/
public static void closeConnection(Connection connection) {
try {
if(!connection.isClosed()) {
System.out.println("Closing DB connection.");
connection.close();
}
} catch (SQLException e) {
System.out.println("Error closing DB: "+e.getMessage());
}
}
/**This method is used to validate user's attempting to login
*
* @param String userName, String password
* @return true for valid login, false for invalid login
*/
public static Boolean validate(String userName, String password)
{
boolean flag = false;
Connection conn;
//Using custom DatabaseConnection class with static method
String login_query = "SELECT * FROM lifeHub.User WHERE username = ? AND password = ?";
try {
conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(login_query);
ps.setString(1,userName);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
//false means an empty result set
if(rs.next() != false) {
System.out.println("User login sucessful for user: "+rs.getString("username")+" user id: "
+rs.getInt("user_id"));
flag = true;
//Set the logged in users static vars
User.setUserName(rs.getString("username"));
User.setUserID(rs.getInt("user_id"));
System.out.println("calling User.getUserName: "+User.getUserName());
System.out.println("calling User.getUserID: "+User.getUserID());
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
return flag;
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect.");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed.");
//e.printStackTrace();
}
System.out.println("Error: Incorrect credentials.");
return flag;
}
/** This is a getter that retrieves the LastHub
*
* @return URL
*/
public static URL getLastHub() {
return lastHub;
}
/** This is a getter that retrieves the user's name
*
* @return userName, String
*/
public static String getUserName() {
return userName;
}
/** This is a getter that retrieves the userID
*
* @return userID, int
*/
public static int getUserID() {
return userID;
}
/** This is a getter that retrieves a LifeHub
*
* @return LifeHub object
*/
public static LifeHub getCurrentHub() {
return currentHub;
}
/** This is a getter that retrieves an ArrayList of LifeHubs
*
* @return ArrayList<LifeHub>
*/
public static ArrayList<LifeHub> getUserHubs() {
return userHubs;
}
/** This is a getter that retrieves the current clas
*
* @return String currentClass
*/
public static String getCurrentClass() {
return currentClass;
}
/** This is a getter that retrieves an ArrayList of Contacts
*
* @return ArrayList of Contact objects
*/
public static ArrayList<Contact> getUserContacts() {
return userContacts;
}
/** This is a getter that retrieves an ArrayList of HubEvents
*
* @return ArrayList of HubEvent
*/
public static ArrayList<HubEvent> getUserEvents() {
return userEvents;
}
/** This is a getter that retrieves an ArrayList of School Classes
*
* @return ArrayList<SchoolClass>
*/
public static ArrayList<SchoolClass> getClasses() {
return classes;
}
/** This is a setter that sets the user's last hub
*
* @param URL lastHub
* @return none
*/
public static void setLastHub(URL lastHub) {
User.lastHub = lastHub;
}
/** This is a setter that sets the user's name
*
* @param String userName
* @return none
*/
public static void setUserName(String userName) {
User.userName = userName;
}
/** This is a setter that sets the user's id
*
* @param int userID
* @return none
*/
public static void setUserID(int userID) {
User.userID = userID;
}
/** This is a setter that sets the user's current hub
*
* @param LifeHub currentHub
* @return none
*/
public static void setCurrentHub(LifeHub currentHub) {
User.currentHub = currentHub;
}
/** This is a setter that sets the user's array list of LifeHubs
*
* @param ArrayList<LifeHub> userHubs
* @return none
*/
public static void setUserHubs(ArrayList<LifeHub> userHubs) {
User.userHubs = userHubs;
}
/** This is a setter that sets the current class
*
* @param String currentClass
* @return none
*/
public static void setCurrentClass(String currentClass) {
User.currentClass = currentClass;
}
/** This is a setter that sets the user's ArrayList of Contact objects
*
* @param ArrayList<Contact> userContacts
* @return none
*/
public static void setUserContacts(ArrayList<Contact> userContacts) {
User.userContacts = userContacts;
}
/** This is a setter that sets the user's ArrayList of HubEvent objects
*
* @param ArrayList<HubEvent> userEvents
* @return none
*/
public static void setUserEvents(ArrayList<HubEvent> userEvents) {
User.userEvents = userEvents;
}
/** This is a setter that sets the user's ArrayList of School Class objects
*
* @param ArrayList<SchoolClass> classes
* @return none
*/
public static void setClasses(ArrayList<SchoolClass> classes) {
User.classes = classes;
}
//////////////////////////////////////////////////////////////////////////////
//These will be used to send things back to the DB
/** This method is used to check the database's contact table for an already
* existing user.
*
* @param Contact contact
* @return boolean
*/
public static boolean checkForContactInTable(Contact contact) {
boolean flag = false;//free and clear to add contact
String check_for_existing_contact = "SELECT * FROM lifeHub.Contact WHERE user_id = ? AND contact_name = ?";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(check_for_existing_contact);
ps.setInt(1, User.getUserID());
ps.setString(2, contact.getName());
ResultSet rs = ps.executeQuery();
//false means an empty result set
if(rs.next() != false) {
System.out.println("contact.getName() EXISTS in Contact Table. Return TRUE");
flag = true;//don't add contact, already exists
}
//close connection
//closeConnection(conn);
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while while checking for existing contact name in Contact Table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed while checking for existing contact name in Contact Table.");
e.printStackTrace();
}
return flag;
}
/** This method is used to insert a contact into the database
* existing user.
*
* @param Contact contact
* @return none
*/
public static void executeInsertIntoContactTable(Contact contact) {
String insert_into_contact_table = "INSERT INTO lifeHub.Contact (user_id,contact_name) "+"VALUES(?,?)";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(insert_into_contact_table);
ps.setInt(1, User.getUserID());
ps.setString(2, contact.getName());
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful insert of new contact!!!");
}
else {
System.out.println("Failed insert of new contact!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while inserting contact name in Contact Table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed inserting contact name in Contact Table");
e.printStackTrace();
}
}
/** This method is used to insert a phone number into the database
* existing user.
*
* @param String contact_name, String phone_number, String type
* @return none
*/
public static void executeInsertIntoPhoneNumberTable(String contact_name, String phone_number, String type) {
System.out.println("Inserting phone number into table: "+contact_name+" "+phone_number+" "+
type);
String insert_into_phone_number_table = "INSERT INTO lifeHub.PhoneNumber (phone_num_id, user_id,contact_name,contact_phone,contact_phone_type) "+"VALUES(?,?,?,?,?)";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(insert_into_phone_number_table);
ps.setInt(1, 0);
ps.setInt(2, User.getUserID());
ps.setString(3, contact_name);
ps.setString(4, phone_number);
ps.setString(5, type);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful insert into PhoneNumber table!!!");
}
else {
System.out.println("Failed insert into PhoneNumber table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while inserting into PhoneNumber table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed inserting into PhoneNumber table");
e.printStackTrace();
}
}
/** This method is used to insert an email into the database
* existing user.
*
* @param String contact_name, String email, String type
* @return none
*/
public static void executeInsertIntoEmailAddressTable(String contact_name, String email, String type) {
System.out.println("Inserting email into table: "+contact_name+" "+email+" "+
type);
String insert_into_email_address_table = "INSERT INTO lifeHub.EmailAddress (email_id,user_id,email_contact,email_detail,email_type) "+"VALUES(?,?,?,?,?)";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(insert_into_email_address_table);
ps.setInt(1, 0);
ps.setInt(2, User.getUserID());
ps.setString(3, contact_name);
ps.setString(4, email);
ps.setString(5, type);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful insert into EmailAddress table!!!");
}
else {
System.out.println("Failed insert into EmailAddress table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while inserting into EmailAddress table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed inserting contact name into EmailAddress table");
e.printStackTrace();
}
}
/** This method is used to insert an email into the database
* existing user.
*
* @param Contact contact
* @return boolean
*/
public static boolean addContact(Contact contact) {
boolean result;
result = User.checkForContactInTable(contact);
if(!result) {
User.executeInsertIntoContactTable(contact);
if (contact.getPhoneList()!=null) {
for (int i = 0; i < contact.getPhoneList().size(); i++)
{
User.executeInsertIntoPhoneNumberTable(contact.getName(), contact.getPhoneList().get(i).getNumber(),
contact.getPhoneList().get(i).getType());
}
}
if (contact.getEmailsList()!=null) {
for (int i=0; i< contact.getEmailsList().size(); i++)
{
User.executeInsertIntoEmailAddressTable(contact.getName(), contact.getEmailsList().get(i).getAddress(),
contact.getEmailsList().get(i).getType());
}
}
}
return !result;
}
/** This method is used to delete a phone number from the database
*
*
* @param String contactName, String phoneNumber, String type
* @return none
*/
public static void executeDeleteFromPhoneNumberTable(String contactName, String phoneNumber, String type) {
System.out.println("Deleting phone number from PhoneNumber table: "+contactName+" "+phoneNumber+" "+
type);
String delete_phone_number = "DELETE FROM lifeHub.PhoneNumber WHERE user_id = ? AND contact_name = ? AND contact_phone = ? AND contact_phone_type = ?";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(delete_phone_number);
ps.setInt(1, User.getUserID());
ps.setString(2, contactName);
ps.setString(3, phoneNumber);
ps.setString(4, type);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful delete from PhoneNumber table!!!");
}
else {
System.out.println("Failed delete PhoneNumber table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while deleting from PhoneNumber table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed while deleting fromPhoneNumber table");
e.printStackTrace();
}
}
/** This method is used to delete an email address from the database
*
*
* @param String contactName, String phoneNumber, String type
* @return none
*/
public static void executeDeleteFromEmailAddressTable(String contactName, String email, String type) {
System.out.println("Deleting email from Email Address table: "+contactName+" "+email+" "+
type);
String delete_email = "DELETE FROM lifeHub.EmailAddress WHERE user_id = ? AND email_contact = ? AND email_detail = ? AND email_type = ?";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(delete_email);
ps.setInt(1, User.getUserID());
ps.setString(2, contactName);
ps.setString(3, email);
ps.setString(4, type);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful delete email from EmailAdress table!!!");
}
else {
System.out.println("Failed to delete email from EmailAdress table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while deleting from PhoneNumber table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed while deleting fromPhoneNumber table");
e.printStackTrace();
}
}
/** This method is used to delete a user Contactfrom the database
*
*
* @param Contact contact
* @return none
*/
public static void executeDeleteFromContactTable(Contact contact) {
System.out.println("Deleting Contact from Contact Table. "+"user: "+User.getUserName()+" contact: "+contact.getName());
String delete_from_contact_table = "DELETE FROM lifeHub.Contact WHERE user_id = ? AND contact_name = ?";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(delete_from_contact_table);
ps.setInt(1, User.getUserID());
ps.setString(2, contact.getName());
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful delete of contact from Contact Table!!!");
}
else {
System.out.println("Failed delete of contact from Contact Table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while deleting contact from Contact Table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed deleting contact from Contact Table");
e.printStackTrace();
}
}
/** This method is used check the database for a user before deleting the contact
*
*
* @param Contact contact
* @return boolean
*/
public static boolean deleteContact(Contact contact) {
boolean result;
result = User.checkForContactInTable(contact);
if(result) {
if (contact.getPhoneList()!=null) {
for (int i = 0; i < contact.getPhoneList().size(); i++)
{
User.executeDeleteFromPhoneNumberTable(contact.getName(), contact.getPhoneList().get(i).getNumber(),
contact.getPhoneList().get(i).getType());
}
}
if (contact.getEmailsList()!=null) {
for (int i=0; i< contact.getEmailsList().size(); i++)
{
User.executeDeleteFromEmailAddressTable(contact.getName(), contact.getEmailsList().get(i).getAddress(),
contact.getEmailsList().get(i).getType());
}
}
User.executeDeleteFromContactTable(contact);
}
return !result;
}
//eventID,eventType,hubName,eventName,imgPath,text
/** This method is used insert a Task into the database
*
*
* @param int eventID, int eventType, String hubName, String eventName, String imgPath,
String text
* @return none
*/
public static void executeInsertIntoTaskTable(int eventID, int eventType, String hubName, String eventName, String imgPath,
String text) {
System.out.println("Inserting task into Task table: "+hubName+" "+eventName+" "+text);
String insert_into_task_table = "INSERT INTO lifeHub.Task (task_id,user_id,event_id,event_type,event_hub,event_name,img_path,text) "+"VALUES(?,?,?,?,?,?,?,?)";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(insert_into_task_table);
ps.setInt(1, 0);
ps.setInt(2, User.getUserID());
ps.setInt(3, eventID);
ps.setInt(4, eventType);
ps.setString(5, hubName);
ps.setString(6, eventName);
ps.setString(7, imgPath);
ps.setString(8, text);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful insert into Task table!!!");
}
else {
System.out.println("Failed insert into Task table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while inserting into Task table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed inserting into Task table");
e.printStackTrace();
}
}
/** This method is used to prep a Task for entry into the database
*
*
* @param Task task, String className
* @return none
*/
public static void addTask(Task task, String className) {
//need user_id event_id=99 event_type event_hub event_name img_path text
User.getUserID(); //user_id
int eventID = 99;
int eventType = User.getCurrentHub().getEventType();
//String eventTypeString = "";
String hubName = User.getCurrentHub().getHubName();
String eventName;
String imgPath = "";
String text = task.getText();
if (User.getCurrentHub().getEventType()==2)//this means it's a Business tasks link to Hub
{
eventName=hubName;
System.out.println("Event Name "+ eventName+". hubName "+hubName);
}
else
{
//send to hub and class
eventName = className;
System.out.println("Event Name "+ eventName+". Class Name "+className);
}
//do insert here
User.executeInsertIntoTaskTable(eventID,eventType,hubName,eventName,imgPath,text);
}
/** This method is used to delete a Task from the database
*
*
* @param int eventType,String hubName,String eventName,String imgPath,String text
* @return none
*/
public static void executeDeleteFromTaskTable(int eventType,String hubName,String eventName,String imgPath,String text) {
System.out.println("Deleting task from Task table: "+User.getUserID()+" "+eventType+" "+hubName+" "+eventName+" "+text);
String delete_from_task_table = "DELETE FROM lifehub.task WHERE user_id = ? AND event_type = ? AND event_hub = ? AND event_name = ? AND text = ?";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(delete_from_task_table);
ps.setInt(1, User.getUserID());
ps.setInt(2, eventType);
ps.setString(3, hubName);
ps.setString(4, eventName);
ps.setString(5, text);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful deletion from Task table!!!");
}
else {
System.out.println("Failed deletion from Task table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while deleting from Task table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed deleting Task table");
e.printStackTrace();
}
}
/** This method is used to prep a Task before deletion from a database
*
*
* @param Task task, String className
* @return none
*/
public static void deleteTask(Task task, String className) {
User.getUserID(); //user_id
int eventType = User.getCurrentHub().getEventType();
String hubName = User.getCurrentHub().getHubName();
String eventName;
String imgPath = "";
String text = task.getText();
if (User.getCurrentHub().getEventType()==2)//this means it's a Business tasks link to Hub
{
eventName=hubName;
System.out.println("Event Name "+ eventName+". hubName "+hubName);
}
else if (User.getCurrentHub().getEventType()==1)
{
//send to hub and class
eventName = className;
System.out.println("Event Name "+ eventName+". Class Name "+className);
}
else
{
//send to hub and class
eventName = className;
System.out.println("Event Name "+ eventName+". Class Name "+className);
}
//do insert here
User.executeDeleteFromTaskTable(eventType,hubName,eventName,imgPath,text);
}
/** This method is used to delete a school class from the database
*
*
* @param int userID, String hubName, String schoolClassName, String schoolClassLocation, String schoolClassProfessor
* @return none
*/
public static void executeDeleteFromSchoolClassTable(int userID, String hubName, String schoolClassName, String schoolClassLocation, String schoolClassProfessor) {
System.out.println("Deleting from SchoolClass table: "+hubName+" "+schoolClassName+" "+schoolClassProfessor);
String delete_from_school_class_table = "DELETE FROM lifeHub.SchoolClass WHERE user_id = ? AND hub_name = ? AND school_class_name = ?"
+ " AND school_class_location = ? AND school_class_professor = ?";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(delete_from_school_class_table);
ps.setInt(1, userID);
ps.setString(2, hubName);
ps.setString(3, schoolClassName);
ps.setString(4, schoolClassLocation);
ps.setString(5, schoolClassProfessor);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful Deletion from SchoolClass table!!!");
}
else {
System.out.println("Failed Deletion SchoolClass table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while deleting from School Class table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed while deleting from School Class table");
e.printStackTrace();
}
}
/** This method is used to prep a School Class for deletion from the database
*
*
* @param SchoolClass thisClass
* @return none
*/
public static void deleteClass(SchoolClass thisClass) {
//userID hubName schoolClassName schoolClassLocation schoolClassProfessor
int userID = User.getUserID();
String hubName = User.getCurrentHub().getHubName();
String schoolClassName = thisClass.getClassName();
String schoolClassLocation = thisClass.getLocation();
String schoolClassProfessor = thisClass.getProfessor();
User.executeDeleteFromSchoolClassTable(userID,hubName,schoolClassName,schoolClassLocation,schoolClassProfessor);
}
/** This method is used to insert a School Class into the database
*
*
* @param int userID, String hubName, String schoolClassName, String schoolClassLocation, String schoolClassProfessor
* @return none
*/
public static void executeInsertIntoSchoolClassTable(int userID, String hubName, String schoolClassName, String schoolClassLocation, String schoolClassProfessor) {
System.out.println("Inserting task into SchoolClass table: "+hubName+" "+schoolClassName+" "+schoolClassProfessor);
String insert_into_task_table = "INSERT INTO lifeHub.SchoolClass (school_class_id,user_id,hub_name,school_class_name,school_class_location,school_class_professor) "+"VALUES(?,?,?,?,?,?)";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(insert_into_task_table);
ps.setInt(1, 0);
ps.setInt(2, User.getUserID());
ps.setString(3, hubName);
ps.setString(4, schoolClassName);
ps.setString(5, schoolClassLocation);
ps.setString(6, schoolClassProfessor);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful insert into SchoolClass table!!!");
}
else {
System.out.println("Failed insert into SchoolClass table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while inserting into School Class table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed inserting into School Class table");
e.printStackTrace();
}
}
/** This method is used to insert a School Class into the database
*
*
* @param SchoolClass thisClass
* @return none
*/
public static void addClass(SchoolClass thisClass) {
int userID = User.getUserID();
String hubName = User.getCurrentHub().getHubName();
String schoolClassName = thisClass.getClassName();
String schoolClassLocation = thisClass.getLocation();
String schoolClassProfessor = thisClass.getProfessor();
User.executeInsertIntoSchoolClassTable(userID,hubName,schoolClassName,schoolClassLocation,schoolClassProfessor);
}
/** This method is used to insert a LifeHub into the database
*
*
* @param int userID, int eventType, String lifeHubName
* @return none
*/
public static void executeInsertIntoLifeHubTable(int userID, int eventType, String lifeHubName) {
System.out.println("Inserting Life Hub Table: "+userID+" "+eventType+" "+lifeHubName);
String insert_into_task_table = "INSERT INTO lifeHub.LifeHub (life_hub_id,user_id,event_type,life_hub_name) "+"VALUES(?,?,?,?)";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(insert_into_task_table);
ps.setInt(1, 0);
ps.setInt(2, userID);
ps.setInt(3, eventType);
ps.setString(4, lifeHubName);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful insert into LifeHub table!!!");
}
else {
System.out.println("Failed insert into LifeHub table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while inserting into LifeHub table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed inserting into LifeHub table");
e.printStackTrace();
}
}
/** This method is used to prepare a LifeHub before entry into the database
*
*
* @param LifeHub hub
* @return none
*/
public static void addHub(LifeHub hub) {
int userID = User.getUserID();
int eventType = hub.getEventType();
String lifeHubName = hub.getHubName();
User.executeInsertIntoLifeHubTable(userID,eventType,lifeHubName);
}
/** This method is used to insert an archived note into the database
*
*
* @param int userID, int eventType, String noteLifeHubName,
String noteSchoolClassName, String noteImagePath, String noteText
* @return none
*/
public static void executeInsertIntoArchivedNoteTable(int userID, int eventType, String noteLifeHubName,
String noteSchoolClassName, String noteImagePath, String noteText) {
System.out.println("Inserting into Archived Note Table: "+userID+" "+eventType+" "+noteText);
String insert_into_task_table = "INSERT INTO lifeHub.ArchivedNotes (archived_notes_id,user_id,event_type,life_hub_name,school_class,img_path,text) "+"VALUES(?,?,?,?,?,?,?)";
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(insert_into_task_table);
ps.setInt(1, 0);
ps.setInt(2, userID);
ps.setInt(3, eventType);
ps.setString(4, noteLifeHubName);
if(noteSchoolClassName == null)
noteSchoolClassName = noteLifeHubName;
ps.setString(5, noteSchoolClassName);
ps.setString(6, noteImagePath);
ps.setString(7, noteText);
int result = ps.executeUpdate();
if(result > 0){
System.out.println("Successful insert into Archived Note Table!!!");
}
else {
System.out.println("Failed insert into Archived Note Table!");
}
//close connection
try {
conn.close();
}catch (SQLException se){
System.out.println("Error closing SQL connection.");
}
}catch(SQLSyntaxErrorException see) {
System.out.println("Error: DB syntax is incorrect while inserting into Archived Note Table");
//see.printStackTrace();
}catch(Exception e) {
System.out.println("Error: DB connection failed inserting into Archived Note Table");
e.printStackTrace();
}
}
/** This method is used to prep an archived note for insert into the database
*
*
You can’t perform that action at this time.
