00001 import java.util.Vector;
00002 import java.sql.SQLException;
00003 import java.text.SimpleDateFormat;
00004 import java.sql.PreparedStatement;
00005 import java.util.Set;
00006 import java.util.Iterator;
00007 import java.util.Calendar;
00008
00009 public class AppointmentEditor extends Widget
00010 {
00011 public static final int FIND_DOC = 1;
00012 public static final int FIND_PATIENT = 2;
00013 public static final int SAVE = 3;
00014 public static final int CANCEL = 4;
00015
00016 public int appointment_id;
00017 public TextBox begintime;
00018 public TextBox endtime;
00019 public ActionButton action;
00020 public boolean done = false;
00021 public PatientSelect patient;
00022 public DoctorSelect doctor;
00023 public DiagnosisList diagnoses;
00024 public ReferralList referrals;
00025 public ServicesSelect services;
00026
00027
00028 public boolean showErrors = false;
00029 public Vector errors = new Vector();
00030
00031 public AppointmentEditor(int appointment_id, String prefix, Form form)
00032 {
00033 super(prefix, form);
00034 this.appointment_id = appointment_id;
00035 action = new ActionButton(n("action"), form);
00036 begintime = new TextBox(0,20,n("begintime"), form);
00037 endtime = new TextBox(0,20,n("endtime"), form);
00038 diagnoses = new DiagnosisList(n("diagnoses"), form);
00039 referrals = new ReferralList(n("referrals"), form);
00040 services = new ServicesSelect(n("services"), form);
00041 doctor = new DoctorSelect(DoctorDb.PRACTICE_ID, 1, n("doctor"), form);
00042 patient = new PatientSelect(n("patient"), form);
00043 addChild(action);
00044 addChild(begintime);
00045 addChild(endtime);
00046 addChild(diagnoses);
00047 addChild(services);
00048 addChild(doctor);
00049 addChild(patient);
00050 addChild(referrals);
00051 }
00052
00053 public void loadValues()
00054 {
00055 super.loadValues();
00056
00057 if (action.action == SAVE)
00058 {
00059 showErrors = true;
00060 if (save()) done = true;
00061 }
00062 else if (action.action == CANCEL)
00063 {
00064 done = true;
00065 }
00066 }
00067
00068 public void loadDefaults()
00069 {
00070 super.loadDefaults();
00071 Query q = new Query(form.connectDb(), form.pw);
00072 try
00073 {
00074 q.query("SELECT begintime, endtime, doctor_id, patient_id FROM appointments WHERE appointment_id = " + appointment_id);
00075 if (q.r == null || !q.r.next()) return;
00076 begintime.text = q.r.getString(1);
00077 endtime.text = q.r.getString(2);
00078 doctor.id = new Integer(q.r.getInt(3));
00079 patient.id = new Integer(q.r.getInt(4));
00080
00081 q.query("SELECT referral_id, type, doctor_id FROM referrals WHERE appointment_id = " + appointment_id);
00082 if (q != null)
00083 while(q.r.next())
00084 {
00085 Referral r = new Referral();
00086 r.referral_id = q.r.getInt(1);
00087 r.incoming = q.r.getInt(2) == 2;
00088 r.doctor_id = q.r.getInt(3);
00089 referrals.referrals.add(r);
00090 }
00091
00092 q.query("SELECT diagnosis_id, name, description FROM diagnoses WHERE appointment_id = " + appointment_id);
00093 if (q != null)
00094 while(q.r.next())
00095 {
00096 Diagnosis d = new Diagnosis();
00097 d.diagnosis_id = q.r.getInt(1);
00098 d.name = q.r.getString(2);
00099 d.description = q.r.getString(3);
00100 diagnoses.diagnoses.add(d);
00101 }
00102
00103 q.query("SELECT service_id FROM appointment_services WHERE appointment_id = " + appointment_id);
00104 if (q != null)
00105 while(q.r.next())
00106 services.ids.put(new Integer(q.r.getInt(1)), null);
00107 }
00108 catch (SQLException e)
00109 {
00110 p(e);
00111 }
00112 finally
00113 {
00114 q.close();
00115 }
00116 }
00117
00118 protected boolean save()
00119 {
00120 if (doctor.id == null)
00121 errors.add("Doctor not specified");
00122 if (patient.id == null)
00123 errors.add("Patient not specified");
00124 if (begintime.text == null || begintime.text.trim().length() == 0)
00125 errors.add("Begin time not specified");
00126 if (endtime.text == null || endtime.text.trim().length() == 0)
00127 errors.add("End time not specified");
00128
00129 java.sql.Timestamp bt, et;
00130 bt = toTimestamp(begintime.text);
00131 if (bt == null)
00132 errors.add("Unable to parse begin time. (Example of valid format is 2000-01-01 13:30)");
00133 et = toTimestamp(endtime.text);
00134 if (et == null)
00135 errors.add("Unable to parse end time. (Example of valid format is 2000-01-01 13:30)");
00136
00137 if (et != null && bt != null && bt.getTime() >= et.getTime())
00138 {
00139 errors.add("Appointment end time must be later than begin time");
00140 }
00141
00142 if (errors.size() > 0) return false;
00143
00144 form.connectDb();
00145 Query q = new Query(form.conn, form.pw);
00146 try
00147 {
00148 boolean transactionComplete = false;
00149 q.execute("BEGIN");
00150 try
00151 {
00152 PreparedStatement s;
00153 if (appointment_id > 0)
00154 {
00155 s = form.conn.prepareStatement("\n"+
00156 " UPDATE appointments SET begintime = ?, endtime = ?, \n"+
00157 " doctor_id = ?, patient_id = ?\n"+
00158 " WHERE appointment_id = " + appointment_id);
00159 }
00160 else
00161 {
00162 s = form.conn.prepareStatement("\n"+
00163 " INSERT INTO appointments (begintime, endtime, doctor_id, patient_id)\n"+
00164 " VALUES (?,?,?,?)");
00165 }
00166 try
00167 {
00168 s.setTimestamp(1, bt);
00169 s.setTimestamp(2, et);
00170 s.setInt(3, doctor.id.intValue());
00171 s.setInt(4, patient.id.intValue());
00172 s.executeUpdate();
00173 }
00174 finally
00175 {
00176 s.close();
00177 }
00178
00179 if (appointment_id > 0)
00180 {
00181 q.execute
00182 (
00183 "DELETE FROM diagnoses WHERE appointment_id = " + appointment_id + ";\n" +
00184 "DELETE FROM referrals WHERE appointment_id = " + appointment_id + ";\n" +
00185 "DELETE FROM appointment_services WHERE appointment_id = " + appointment_id + ";\n"
00186 );
00187 }
00188 else
00189 {
00190 q.query("SELECT currval('appointments_appointment_id_seq')");
00191 if (q == null || !q.r.next())
00192 {
00193 errors.add("Internal Error: Failed to retrieve appointment_id");
00194 return false;
00195 }
00196 appointment_id = q.r.getInt(1);
00197 }
00198
00199
00200
00201
00202 for(int i = 0; i < referrals.referrals.size(); ++i)
00203 {
00204 Referral r = (Referral) referrals.referrals.get(i);
00205 q.execute(
00206 "INSERT INTO referrals (appointment_id, doctor_id, type)\n" +
00207 "VALUES (" + appointment_id + ", " + r.doctor_id + ", " +
00208 (r.incoming ? "2" : "1") + ")"
00209 );
00210 }
00211
00212 s = form.conn.prepareStatement("INSERT INTO diagnoses (appointment_id, name, description) "
00213 + "VALUES (?,?,?)");
00214 try
00215 {
00216 s.setInt(1, appointment_id);
00217 for(int i = 0; i < diagnoses.diagnoses.size(); ++i)
00218 {
00219 Diagnosis d = (Diagnosis) diagnoses.diagnoses.get(i);
00220 s.setString(2, d.name == null ? "" : d.name);
00221 s.setString(3, d.description == null ? "" : d.description);
00222 s.executeUpdate();
00223 }
00224 }
00225 finally
00226 {
00227 s.close();
00228 }
00229
00230 Set keys = services.ids.keySet();
00231 for(Iterator i = keys.iterator(); i.hasNext();)
00232 {
00233 Integer n = (Integer) i.next();
00234 q.execute(
00235 "INSERT INTO appointment_services (appointment_id, service_id)\n" +
00236 "VALUES (" + appointment_id + ", " + n.intValue() + ")"
00237 );
00238 }
00239
00240 transactionComplete = true;
00241 }
00242 catch (Throwable e)
00243 {
00244 transactionComplete = false;
00245 throw e;
00246 }
00247 finally
00248 {
00249 q.execute(transactionComplete ? "COMMIT" : "ROLLBACK");
00250 }
00251 }
00252 catch (SQLException e)
00253 {
00254 p(e);
00255 }
00256 catch (Throwable e)
00257 {
00258 Query.rethrow(e);
00259 }
00260 finally
00261 {
00262 q.close();
00263 }
00264 return true;
00265 }
00266
00267 public void display(boolean hidden)
00268 {
00269 super.display(hidden);
00270
00271 if (hidden || modalChild != null) return;
00272
00273 if (showErrors)
00274 {
00275 p("<h3>Save Failed</h3>");
00276 if (errors.size() > 0)
00277 {
00278 p("<p><strong>Please correct the following errors:</strong></p>");
00279 p("<ul>\n");
00280 for (int i = 0; i < errors.size(); ++i)
00281 {
00282 String s = (String) errors.get(i);
00283 p(" <li><font color=red>" + s + "</font></li>\n");
00284 }
00285 p("</ul>\n");
00286 }
00287 }
00288
00289 p(
00290 "<table>\n"+
00291 "<tr>\n"+
00292 " <td valign=top>Patient:</td>\n"+
00293 " <td>"); patient.display(); p("</td>\n"+
00294 "</tr>\n"+
00295 "<tr>\n"+
00296 " <td valign=top>Doctor:</td>\n"+
00297 " <td>"); doctor.display(); p("</td>\n"+
00298 "</tr>\n"+
00299 "<tr>\n"+
00300 " <td valign=top>Begin Time:</td>\n"+
00301 " <td>"); begintime.display(); p("</td>\n"+
00302 "</tr>\n"+
00303 "<tr>\n"+
00304 " <td valign=top>End Time:</td>\n"+
00305 " <td>"); endtime.display(); p("</td>\n"+
00306 "</tr>\n"+
00307 "<tr>\n"+
00308 " <td valign=top>Services <i>(select many)</i>:</td>\n"+
00309 " <td>\n"+
00310 " "); services.display(); p("\n"+
00311 " </td>\n"+
00312 "</tr>\n"+
00313 "<tr>\n"+
00314 " <td valign=top>Diagnoses:</td>\n"+
00315 " <td>\n"+
00316 " "); diagnoses.display(); p("\n"+
00317 " </td>\n"+
00318 "</tr>\n"+
00319 "<tr>\n"+
00320 " <td valign=top>Referrals:</td>\n"+
00321 " <td>\n"+
00322 " "); referrals.display(); p("\n"+
00323 " </td>\n"+
00324 "</tr>\n"+
00325 "<tr>\n"+
00326 " <td> </td>\n"+
00327 " <td>\n"+
00328 " "); action.display("Save Changes", SAVE); action.display("Cancel", CANCEL);
00329 p("\n"+
00330 " </td>\n"+
00331 "</td>\n"+
00332 "</table>\n"+
00333 "");
00334 }
00335
00336 java.sql.Timestamp toTimestamp(String s)
00337 {
00338 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm");
00339
00340 java.sql.Timestamp t = null;
00341 java.util.Date d = null;
00342
00343 try
00344 {
00345 d = f.parse(s);
00346 }
00347 catch (java.text.ParseException e) {}
00348
00349 if (d != null)
00350 {
00351 Calendar c = Calendar.getInstance();
00352 c.setTime(d);
00353 c.set(Calendar.SECOND, 0);
00354 t = new java.sql.Timestamp(c.getTimeInMillis());
00355 }
00356 return t;
00357 }
00358 };