-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoBabbyApp.java
553 lines (465 loc) · 22.8 KB
/
GoBabbyApp.java
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
import com.ibm.db2.jcc.t4.h;
import java.sql.* ;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Scanner;
public class GoBabbyApp {
private static String exit = "E";
private static String reviewNotes = "1";
private static String reviewTests = "2";
private static String addNote = "3";
private static String prescribeTest = "4";
private static String goBackApp = "5";
private static String goBackDate = "D";
// We build here our private String we will use as our options
private static int practionerid;
private static boolean present = false;
private static Scanner scan = new Scanner(System.in);
private static String text;
private static ArrayList<Integer> primary = new ArrayList<>();
private static ArrayList<Integer> techid = new ArrayList<>();
// those arraylist will be kept in order to see the status of the practioner towards its appointment
private static String type = "";
private static Date date;
private static java.sql.ResultSet rs;
private static Integer maximumLength = 50;
private static int milli = 1000;
private static int timeinmin = 60;
private static String adate;
private static String ahour;
private static String mname;
private static String mramqid;
private static int pregnancyid;
private static String url = "jdbc:db2://winter2022-comp421.cs.mcgill.ca:50000/cs421";
//REMEMBER to remove your user id and password before submitting your code!!
private static String your_userid = "";
private static String your_password = "";
private static Connection con;
private static Statement statement;
public static void main(String[] args) throws SQLException {
// Unique table names. Either the user supplies a unique identifier as a command line argument, or the program makes one up.
String tableName = "";
int sqlCode = 0; // Variable to hold SQLCODE
String sqlState = "00000"; // Variable to hold SQLSTATE
if (args.length > 0)
tableName += args[0];
else
tableName += "exampletbl";
// Register the driver. You must register the driver before you can use it.
try {
DriverManager.registerDriver(new com.ibm.db2.jcc.DB2Driver());
} catch (Exception cnfe) {
System.out.println("Class not found");
}
// This is the url you must use for DB2.
//Note: This url may not valid now ! Check for the correct year and semester and server name.
//AS AN ALTERNATIVE, you can just set your password in the shell environment in the Unix (as shown below) and read it from there.
//$ export SOCSPASSWD=yoursocspasswd
if (your_userid == null && (your_userid = System.getenv("SOCSUSER")) == null) {
System.err.println("Error!! do not have a password to connect to the database!");
System.exit(1);
}
if (your_password == null && (your_password = System.getenv("SOCSPASSWD")) == null) {
System.err.println("Error!! do not have a password to connect to the database!");
System.exit(1);
}
con = DriverManager.getConnection(url, your_userid, your_password);
statement = con.createStatement();
// Creating a table
while (present == false) {
// we get the practionerid here
practionerid = Start();
try {
// we verify it to be valid
String querySQL = "SELECT COUNT(*) as numpid FROM midwife WHERE practionerid = " + practionerid;
java.sql.ResultSet rsMid = statement.executeQuery(querySQL);
// we verify whether the practionerid is present in the database
while (rsMid.next()) {
int numpid = rsMid.getInt("numpid");
// we get the number of midwife with this practioner id
// it can either return 1 or 0
if (numpid == 0) {
// if it's 0 then the practioner id entered is invalid
present = false;
// we return the error message
System.out.println("ERROR: INVALID PRACTIONER ID");
} else {
present = true;
}
}
} catch (SQLException e) {
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
System.out.println(e);
}
}
try {
// we get the pregnancies for which the practioner is the primary midwife
String querySQL2 = "SELECT pregnancyid, midwife1id, midwife2id FROM assignedat WHERE midwife1id = " + practionerid + " OR midwife2id = " + practionerid;
java.sql.ResultSet rsMidStatus = statement.executeQuery(querySQL2);
while (rsMidStatus.next()) {
// we get the status of the midwife towards all the associated appointments
int pregnancyid = rsMidStatus.getInt("pregnancyid");
int midwife1 = rsMidStatus.getInt("midwife1id");
if (practionerid == midwife1) {
primary.add(pregnancyid);
// if it's the primary midwife it is added here
}
}
} catch (SQLException e) {
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
System.out.println(e);
}
setDate();
// Querying a table
try {
while (true) {
java.sql.ResultSet rsApp = getResultSet();
// we get the value of the appointment
getResultSetApp(rsApp);
//while text not a num loop!
int appNum = getAppointment();
rs = getResultSet();
// we get the result of the avaialable appointments
rsApp = getResultSetAppDetails(rs, appNum);
// we get the value by column Label
adate = rsApp.getString("adate");
ahour = rsApp.getString("ahour");
mname = rsApp.getString("mname");
mramqid = rsApp.getString("mramqid");
pregnancyid = rsApp.getInt("pregnancyid");
if (primary.contains(pregnancyid)) {
type = "P";
} else {
type = "B";
// this describes the status of the midwife given the appointments (Backup or Primary)
}
while (true) {
// we print the values associated with the appointments of the midwife
System.out.print("\n"+adate);
System.out.print(" " + ahour + " " + type);
System.out.print(" " + mname);
System.out.print(" " + mramqid + "\n");
String action = currentAction();
// we get the actions
// and the String associated with those value
if (action.equals(goBackApp)) {
break;
} else if (action.equals(reviewNotes)) {
reviewNotes();
} else if (action.equals(addNote)) {
addANote();
} else if (action.equals(reviewTests)) {
reviewTests();
} else if (action.equals(prescribeTest)) {
addATest();
// we have all of the different options
} else {
System.out.println("The command is not a valid action.");
// error terms
}
}
System.out.println("DONE");
}
} catch (SQLException e) {
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
System.out.println(e);
} catch (ParseException e) {
e.printStackTrace();
}
// Finally but importantly close the statement and connection
statement.close();
con.close();
}
private static int Start() throws SQLException {
// we get the practioner id of the midwife
int practionerid = 0;
while(true) {
// we build a loop until we get the proper format
try {
System.out.println("Please enter your practioner id [E] to exit: \n");
text= scan.nextLine();
practionerid = Integer.parseInt(text);
break;
//we try to get the value of the action
} catch (NumberFormatException e) {
// else we verify if the value entered may be used to change date or exit the application
if (text.equals(exit)) {
finish();
}
// we build the loop to valid the format of the practionerid
System.out.println("ERROR: INVALID PRACTIONER ID : FORMAT INVALID");
}
}
return practionerid;
}
private static String getDate() {
// we get the 1 st date here
System.out.println("Please enter the date for appointment list [E] to exit: \n");
return scan.nextLine();
}
private static int getAppointment() throws SQLException {
int intValue;
while (true) {
// we build an ifinite loop until we get the proper format
// we fetch the appointment number here
System.out.println("Enter the appointment number that you would like to work on.\n\t\t\t [E] to exit [D] to go back to another date:");
text = scan.nextLine();
try {
intValue = Integer.parseInt(text);
//we try to get the value of the action
break;
} catch (NumberFormatException e) {
// else we verify if the value entered may be used to change date or exit the application
if (text.equals(exit)) {
finish();
}
// we consider the options depending on what the client wants
if (text.equals(goBackDate)) {
// we reset the date here
setDate();
rs = getResultSet();
// we get the value for the appointments here
getResultSetApp(rs);
}
}
}
return intValue;
}
private static String currentAction() {
// we get the options and the return the one chosen
System.out.println(" 1. Review notes\n 2. Review tests\n 3. Add a note\n 4. Prescribe a test\n 5. Go back to the appointments\n\n Enter your choice: ");
return scan.nextLine();
}
private static String getAddNote() {
// we get the note we want committed to the database
System.out.println("Please type your observation: \n");
return scan.nextLine();
}
private static void addANote() throws ParseException {
// we add a note and get it
String content = getAddNote();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = dateFormat.parse(adate);
// the date returned for the time stamp is the one of the appointment
LocalTime localtime = LocalTime.now();
// we use our current localtime to create a time stamp
System.out.println(date);
long time = date.getTime() + localtime.getSecond() * milli + localtime.getMinute() * milli * timeinmin + localtime.getHour() * milli * timeinmin * timeinmin;
Timestamp sqlTimestamp = new Timestamp(time);
try {
// we addd the note here
String insertSQL = "INSERT INTO notes VALUES (\'" + adate + "\' , \'" + ahour + "\' , " + pregnancyid + " , \'" + sqlTimestamp + "\', \'" + content + "\' ) ";
System.out.println(insertSQL);
statement.executeUpdate(insertSQL);
} catch (SQLException e) {
int sqlCode = e.getErrorCode(); // Get SQLCODE
String sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
System.out.println(e);
}
}
private static String getAddTest() {
// we enter the name of the test
System.out.println("Please enter the type of test: \n");
return scan.nextLine();
}
private static void addATest() throws SQLException {
// we get the available technicians if a test is needed
fetchTechid();
String content = getAddTest();
try {
// we insert into the test appointment
String insertSQL = "INSERT INTO testapp VALUES (\'" + adate + "\' , \'" + ahour + "\' , " + pregnancyid + " , \'" + content + "\', \'" + adate + "\', \'" + adate + "\' , " + null + " ) ";
System.out.println(insertSQL);
statement.executeUpdate(insertSQL);
// we get a random value among the technician
// we create the labwork and insert into the database
insertSQL = "INSERT INTO labwork VALUES (\'" + adate + "\' , \'" + ahour + "\' , " + pregnancyid + " , \'" + content + "\', \'" + adate + "\', \'" + getTechid() + "\' , " + null + " ) ";
System.out.println(insertSQL);
statement.executeUpdate(insertSQL);
// we consider the type of type of the test appointment we are considering
if (content.toLowerCase().contains("expected") || content.toLowerCase().contains("ultrasound")) {
String method = "last menstrual period";
if (content.toLowerCase().contains("ultrasound")) {
method = "dating ultrasound";
}
// we try to predict of test appointment depending on key works
insertSQL = "INSERT INTO expectedmeeting VALUES (\'" + adate + "\' , \'" + ahour + "\' , " + pregnancyid + " , \'" + content + "\', \'" + adate + "\', \'" + method + "\' , " + null + " ) ";
System.out.println(insertSQL);
statement.executeUpdate(insertSQL);
} else {
// depending on the name of the test we try to predict the sample
// the base case is for blood
String sample = "Blood";
if (content.toLowerCase().contains("hormone")) {
sample = "Saliva";
} else if (content.toLowerCase().contains("dr")) {
sample = "Urine";
}
// this is in the case of a sampling test appointment
insertSQL = "INSERT INTO samplingtestapp VALUES (\'" + adate + "\' , \'" + ahour + "\' , " + pregnancyid + " , \'" + content + "\', \'" + adate + "\', \'" + sample + "\' ) ";
System.out.println(insertSQL);
statement.executeUpdate(insertSQL);
}
} catch (SQLException e) {
int sqlCode = e.getErrorCode(); // Get SQLCODE
String sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
System.out.println(e);
}
}
private static void getResultSetApp(ResultSet rsA) throws SQLException {
int i = 1;
String type = "";
while (rsA.next()) {
// we print the Results when fetching a ResultSet
adate = rsA.getString(1);
ahour = rsA.getString(2);
mname = rsA.getString(3);
mramqid = rsA.getString(4);
pregnancyid = rsA.getInt(5);
if (primary.contains(pregnancyid)) {
type = "P";
} else {
type = "B";
}
// We print the values associated with each appointment
System.out.print(i + ": " + adate);
System.out.print(" " + ahour + " " + type);
System.out.print(" " + mname);
System.out.print(" " + mramqid + "\n");
i++;
}
}
private static com.ibm.db2.jcc.t4.h getResultSetAppDetails(ResultSet rs, int row) throws SQLException {
// we return the proper appointment
int i = 1;
while (rs.next()) {
if (i == row) {
break;
}
i++;
// we return here a particular appointment for a mother depending on the appointment selected
}
return (h) rs;
}
private static ResultSet getResultSet() throws SQLException {
// we return the Result Set for the query depending on a certain pregnancy and the chosen date
String querySQL3 = "SELECT a.adate, a.ahour, m.mname, m.mramqid, a.pregnancyid FROM appointment a, mother m, conceive c WHERE a.midwifeid = " + practionerid
+ " AND a.pregnancyid = c.pregnancyid AND a.adate = \'" + date + "\' AND c.motherramq = m.mramqid ORDER BY a.adate, a.ahour";
return statement.executeQuery(querySQL3);
}
private static int getTechid() throws SQLException {
// we randomly fetch a techid here
int index = (int) (Math.random() * techid.size());
return techid.get(index);
}
private static void fetchTechid() throws SQLException {
// we get all of the technician if we need a test
String querySQL = "SELECT techid from technician";
ResultSet rs = statement.executeQuery(querySQL);
while (rs.next()) {
// we add all the techid to the Arraylist containing them
int tech = rs.getInt("techid");
techid.add(tech);
}
}
//private static void
private static void finish() throws SQLException {
// we have here the code terminating the application
statement.close();
con.close();
System.out.println("SYSTEM EXIT");
System.exit(0);
}
private static void reviewNotes() throws SQLException {
try {
// we fetch here the timestamp and the content from the notes relevant to a certain pregnancy
String querySQL2 = "SELECT ntimestamp, ncontent FROM notes WHERE npregnancyid = " + pregnancyid + " ORDER BY ntimestamp";
java.sql.ResultSet rs2 = statement.executeQuery(querySQL2);
while (rs2.next()) {
// we obtain the contents and the timestamp
String ntimestamp = rs2.getString("ntimestamp");
String ncontent = rs2.getString("ncontent");
if (ncontent.length() > maximumLength) {
// we limit the length of the content of the notes
ncontent = ncontent.substring(0, maximumLength);
}
// we print the content and time here
System.out.println(ntimestamp + " " + ncontent);
}
} catch (SQLException e) {
int sqlCode = e.getErrorCode(); // Get SQLCODE
String sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
System.out.println(e);
}
}
private static void setDate() throws SQLException {
// we get the date here
while(true) {
try {
System.out.println("Date Format: YYYY-MM-DD");
text = getDate();
if (text.equals(exit)) {
finish();
}
date = java.sql.Date.valueOf(text);
break;
} catch (IllegalArgumentException e) {
System.out.println("FORMAT OF DATE IS INVALID");
}
}
}
private static void reviewTests() throws SQLException {
try {
// we get here the proper query to retrieve the information relevant to a test
String querySQL3 = "SELECT tadate, taname, result FROM labwork WHERE tapregnancyid=" + pregnancyid;
java.sql.ResultSet rs2 = statement.executeQuery(querySQL3);
while (rs2.next()) {
// we obtain the values of the tests by columnLabel
String tadate = rs2.getString("tadate");
String taname = rs2.getString("taname");
String result = rs2.getString("result");
if (result == null) {
// if the result is not available yet, we return PENDING
result = "PENDING";
}
if (result.length() > maximumLength) {
// we fix the maximum Length to 50 Characters
result = result.substring(0, maximumLength);
}
// we return the results here
System.out.println(tadate + " [" + taname + "] " + result);
}
} catch (SQLException e) {
int sqlCode = e.getErrorCode(); // Get SQLCODE
String sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
System.out.println(e);
}
}
}