-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThirdNextAppointment.java
420 lines (372 loc) · 17.1 KB
/
ThirdNextAppointment.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
/*
* This program is a standalone application to determine the 3rd next available
* appointment based on data in an Oscar EMR database. It assumes a MySQL
* database and the presence of a mysql-connector-java-*.jar file
* in the classpath.
*
* An example of how to compile and execute the program (after setting
* appropriate dbUrl, dbUser and dbPassword strings in
* ThirdNextAppointment.properties) is:
*
* javac ThirdNextAppointment.java
* java -cp ".:mysql-connector-java-5.1.28.jar" ThirdNextAppointment
*
* The generateReport method is based on code from Oscar's
* src/main/java/oscar/oscarReport/reportByTemplate/ThirdApptTimeReporter.java
* written by Randy Jonasz [mailto:[email protected]]
* Some code is also borrowed from
* http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm
*
* @author rrusk
*/
//package thirdnextappointment;
// Import required packages
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ThirdNextAppointment {
// Application properties file
static final String CONFIG_FILE = "ThirdNextAppointment.properties";
// JDBC driver name
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static Connection getDBConnection(String dbUrl, String user, String pass) {
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException ex) {
//Handle errors for Class.forName
ex.printStackTrace(System.out);
}
// Open a connection
Connection conn = null;
try {
conn = DriverManager.getConnection(dbUrl, user, pass);
} catch (SQLException se) {
//Handle errors for JDBC
System.err.println("ERROR connecting to database. Check the database configuration.");
System.err.println(" The configuration is specified in " + CONFIG_FILE);
System.err.println(" It can be overridding with command-line parameters of the form");
System.err.println(" parmconfig=value.");
System.err.println("The error message was:");
System.err.println(se.getMessage());
//se.printStackTrace(System.out);
}
return conn;
}
static private String getProviderInfo(Connection conn, String provider) {
String providerSQL = "select practitionerNo, ohip_no from provider where provider_no=" + provider;
ResultSet rs;
Statement stmt;
String providerInfo = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(providerSQL);
if (rs.next()) {
String cpsId = rs.getString("practitionerNo");
String mspNo = rs.getString("ohip_no");
providerInfo = "\"cpsid\":\"" + cpsId + "\", \"msp\":\"" + mspNo + "\"";
}
} catch (SQLException se) {
se.printStackTrace(System.out);
System.err.println(providerSQL);
}
return providerInfo;
}
/**
* Wrapper to method from
* src/main/java/oscar/oscarReport/reportByTemplate/ThirdApptTimeReporter.java
* Processes one provider at a time rather than an aggregate over all providers
* @param conn
* @param dateFrom
* @param schedSymbolsStr
* @param provider
* @param apptLength
* @return
*/
static String generateReport(Connection conn, String dateFrom, String schedSymbolsStr, String provider, int apptLength) {
int numDays = -1;
String[] schedSymbols = null;
if (schedSymbolsStr != null) {
schedSymbols = schedSymbolsStr.split(",");
}
if (dateFrom == null || provider == null || schedSymbols == null) {
return "dateFrom and provider must be set and at least one schedule symbol must be set";
}
String scheduleSQL = "select scheduledate.provider_no, scheduletemplate.timecode, scheduledate.sdate from scheduletemplate, scheduledate where scheduletemplate.name=scheduledate.hour and scheduledate.sdate >= '" + dateFrom + "' and scheduledate.provider_no in (" + provider + ") and scheduledate.status = 'A' and (scheduletemplate.provider_no=scheduledate.provider_no or scheduletemplate.provider_no='Public') order by scheduledate.sdate";
String apptSQL = "";//select start_time, end_time from appointment where provider_no = '" + provider_no + "' and status not like '%C%' and appointment_date = '";
String schedDate = "";
ResultSet rs;
ResultSet rs2;
int unbooked;
Statement stmt;
Statement stmt2;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(scheduleSQL);
int duration;
String timecodes, code;
String tmpApptSQL;
String apptTime;
int dayMins = 24 * 60;
int iHours, iMins, apptHour_s, apptMin_s, apptHour_e, apptMin_e;
int codePos;
int latestApptHour, latestApptMin;
int third = 3;
int numAppts = 0;
boolean codeMatch;
while (rs.next() && numAppts < third) {
timecodes = rs.getString("timecode");
duration = dayMins / timecodes.length();
schedDate = rs.getString("sdate");
tmpApptSQL = "select start_time, end_time from appointment where provider_no = '" + rs.getString("provider_no") + "' and status not like '%C%' and appointment_date = '" + schedDate + "' order by start_time asc";
stmt2 = conn.createStatement();
rs2 = stmt2.executeQuery(tmpApptSQL);
codePos = 0;
latestApptHour = latestApptMin = 0;
unbooked = 0;
for (int iTotalMin = 0; iTotalMin < dayMins; iTotalMin += duration) {
code = timecodes.substring(codePos, codePos + 1);
++codePos;
iHours = iTotalMin / 60;
iMins = iTotalMin % 60;
while (rs2.next()) {
apptTime = rs2.getString("start_time");
apptHour_s = Integer.parseInt(apptTime.substring(0, 2));
apptMin_s = Integer.parseInt(apptTime.substring(3, 5));
if (iHours == apptHour_s && iMins == apptMin_s) {
apptTime = rs2.getString("end_time");
apptHour_e = Integer.parseInt(apptTime.substring(0, 2));
apptMin_e = Integer.parseInt(apptTime.substring(3, 5));
if (apptHour_e > latestApptHour || (apptHour_e == latestApptHour && apptMin_e > latestApptMin)) {
latestApptHour = apptHour_e;
latestApptMin = apptMin_e;
}
} else {
rs2.previous();
break;
}
}
codeMatch = false;
for (int schedIdx = 0; schedIdx < schedSymbols.length; ++schedIdx) {
if (code.equals(schedSymbols[schedIdx])) {
codeMatch = true;
//System.out.println("codeMatched " + codeMatch);
if (iHours > latestApptHour || (iHours == latestApptHour && iMins > latestApptMin)) {
unbooked += duration;
if (unbooked >= apptLength) {
unbooked = 0;
++numAppts;
if (numAppts == third) {
break;
}
}
}
}
} //end for schedule symbols
if (numAppts == third) {
break;
}
if (!codeMatch) {
unbooked = 0;
}
} //end for
} //end while
String calcDaysSQL = "select datediff('" + schedDate + "','" + dateFrom + "')";
if (numAppts == third) {
rs = stmt.executeQuery(calcDaysSQL);
if (rs.next()) {
numDays = rs.getInt(1);
}
}
} catch (SQLException se) {
se.printStackTrace(System.out);
} catch (NumberFormatException e) {
e.printStackTrace(System.out);
}
// String sql = scheduleSQL + ";\n " + apptSQL;
// System.out.println("sql: " + sql);
String providerInfo = getProviderInfo(conn, provider);
return makeClinicianJson(numDays, dateFrom, schedDate, provider, providerInfo);
}
static private String makeClinicianJson(int numDays, String requestDate, String thirdDate, String provider, String providerInfo) {
return "{\"clinician\":\"" + provider + "\", " + providerInfo + ", " + "\"3rdnext\":" + numDays + "}";
}
static boolean isValidParameter(String s) {
if (s.contains("=")) {
String parts[] = s.split("=");
if (((parts.length == 2) && (parts[0].length() > 0)) && (parts[1].length() > 0)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Boolean errorOccurred = false;
// Requires all commandline configuration parameters, if any, to be in
// the format parmname=value
// Parameters on the commandline override parameters in the property
// configuration file
StringBuilder sb = new StringBuilder();
for (String arg : args) {
if (isValidParameter(arg)) {
sb.append(arg).append("\n");
} else {
System.out.println("Parameter [" + arg +"] is not a valid property string");
}
}
String commandlineProperties = sb.toString();
// Get properties file with configuration parameters from class path
Properties prop = new Properties();
InputStream input;
input = ThirdNextAppointment.class.getClassLoader().getResourceAsStream(CONFIG_FILE);
if (input == null) {
System.out.println("Unable to find configuration file " + CONFIG_FILE);
return;
}
try {
prop.load(input);
if (!commandlineProperties.isEmpty()) { // override with command line properties if any
prop.load(new StringReader(commandlineProperties));
}
} catch (IOException ex) {
Logger.getLogger(ThirdNextAppointment.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
String dbUrl = prop.getProperty("dburl");
if (dbUrl == null || dbUrl.isEmpty()) {
System.err.println("The database url parameter 'dburl' must be set.");
errorOccurred = true;
}
String dbUser = prop.getProperty("dbuser");
if (dbUser == null || dbUser.isEmpty()) {
System.err.println("The database user parameter 'dbuser' must be set.");
errorOccurred = true;
}
String dbPassword = prop.getProperty("dbpassword");
if (dbPassword == null || dbPassword.isEmpty()) {
System.err.println("The database user password parameter 'dbpassword' must be set.");
errorOccurred = true;
}
String schedSymbols = prop.getProperty("schedsymbols");
if (schedSymbols == null || schedSymbols.isEmpty()) {
System.err.println("The schedule symbols parameter 'schedsymbols' must be set.");
System.err.println(" It should look something like 'schedsymbols=1,2,3'");
errorOccurred = true;
}
String providerNums = prop.getProperty("providernums");
if (providerNums == null || providerNums.isEmpty()) {
System.err.println("The provider number parameter of the service providers must be set.");
System.err.println(" It should look something like 'providernums=101,110'");
errorOccurred = true;
}
Integer apptLength = null;
String apptLengthStr = prop.getProperty("apptlength");
if (apptLengthStr == null || apptLengthStr.isEmpty()) {
System.err.println("The appointment length parameter apptlength must be set.");
System.err.println(" It should look something like 'apptlength=15'");
errorOccurred = true;
} else {
try {
apptLength = Integer.parseInt(prop.getProperty("apptlength"));
} catch (NumberFormatException nf) {
System.err.println("The apptlength parameter is invalid.");
errorOccurred = true;
}
}
// String dateFrom = prop.getProperty("datefrom");
// if (dateFrom == null || dateFrom.isEmpty()) {
// System.err.println("The date from parameter must be set.");
// System.err.println(" It should look something like 'datefrom=2015-06-09'");
// errorOccurred = true;
// }
String clinicName = prop.getProperty("clinic");
if (clinicName == null || clinicName.isEmpty()) {
System.err.println("The clinic name parameter must be set.");
errorOccurred = true;
}
String earliestDate = prop.getProperty("earliest");
if (earliestDate == null || earliestDate.isEmpty()) {
System.err.println("The earliestDate parameter must be set.");
errorOccurred = true;
}
String weekDay = prop.getProperty("weekday");
if (weekDay == null || weekDay.isEmpty()) {
System.err.println("The weekday paramater must be set.");
errorOccurred = true;
}
if (errorOccurred) {
System.err.println("Set unconfigured parameters in '" + CONFIG_FILE + "'");
System.err.println(" or pass them in on the command-line in the format 'parm=value'");
return;
}
Connection conn = null;
try {
//System.out.println("Connecting to database...");
conn = getDBConnection(dbUrl, dbUser, dbPassword);
String[] providers = null;
if (providerNums != null) {
providers = providerNums.split(",");
}
ArrayList<String> weekDays = ReportDate.getDates(earliestDate, weekDay);
String filepath = null;
if (conn != null && providers != null) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
for (String dateFrom: weekDays) {
filepath = "./reports/thirdnextappt_" + clinicName + "_" + dateFrom + ".txt";
File f = new File(filepath);
if (!f.exists()) {
PrintWriter out = new PrintWriter(filepath);
long timestamp = df.parse(dateFrom).getTime() / 1000; // time in seconds rather than milliseconds
out.println("{\"clinic\":\"" + clinicName + "\", \"date\":" + timestamp + ", \"clinicians\":[");
int index = 0;
for (String provider : providers) {
out.print(generateReport(conn, dateFrom, schedSymbols, provider, apptLength));
index++;
if (index < providers.length) {
out.println(", ");
} else {
out.println("");
}
}
out.println("]}");
out.close();
}
}
}
//STEP 6: Clean-up environment
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace(System.out);
} catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace(System.out);
} finally {
//finally block used to close resources
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace(System.out);
}//end finally try
}//end try
//System.out.println("Goodbye!");
}//end main
}//end ThirdNextAppointment