00001 import java.sql.SQLException; 00002 00003 public class PatientList extends Widget 00004 { 00005 public ActionButton action; 00006 public PatientEditor editor; 00007 public int patient_id = 0; 00008 00009 public static final int EDIT = 1; 00010 public static final int DELETE = 2; 00011 00012 public PatientList(String prefix, Form form) 00013 { 00014 super(prefix, form); 00015 action = new ActionButton(n("action"), form); 00016 addChild(action); 00017 } 00018 00019 public void loadValues() 00020 { 00021 super.loadValues(); 00022 00023 int a = toInt(loadAttribute("patient_id"),0); 00024 boolean editing = a != 0; 00025 00026 for(;;) 00027 if (editing) 00028 { 00029 if (a != 0) patient_id = a; 00030 editor = new PatientEditor(patient_id, n("editor"), form); 00031 addChild(editor); 00032 if (a != 0) editor.loadValues(); else editor.loadDefaults(); 00033 if (editor.done) 00034 patient_id = 0; 00035 else 00036 { 00037 modalChild = editor; 00038 editor.modal = true; 00039 } 00040 return; 00041 } 00042 else 00043 { 00044 switch (action.action) 00045 { 00046 case EDIT: 00047 editing = true; 00048 patient_id = action.param; 00049 break; 00050 case DELETE: 00051 deletePatient(action.param); 00052 return; 00053 default: 00054 return; 00055 } 00056 } 00057 } 00058 00059 public void deletePatient(int id) 00060 { 00061 Query q = new Query(form.connectDb(), form.pw); 00062 try 00063 { 00064 String where1 = " WHERE patient_id = " + id; 00065 String where2 = " WHERE appointment_id IN (SELECT appointment_id FROM appointments" + where1 + ")"; 00066 q.execute( 00067 "DELETE FROM appointment_services" + where2 + ";\n" + 00068 "DELETE FROM diagnoses" + where2 + ";\n" + 00069 "DELETE FROM referrals" + where2 + ";\n" + 00070 "DELETE FROM appointments" + where1 + ";\n" + 00071 "DELETE FROM patients" + where1 + ";\n" 00072 ); 00073 } 00074 finally 00075 { 00076 q.close(); 00077 } 00078 } 00079 00080 public void display(boolean hidden) 00081 { 00082 super.display(hidden); 00083 00084 printAttribute("patient_id", "" + patient_id); 00085 00086 if (hidden || modalChild != null) return; 00087 00088 p("<p align=center>"); 00089 action.display("Create new patient...", EDIT, -1); 00090 p("</p>"); 00091 00092 Query q = new Query(form.connectDb(), form.pw); 00093 try 00094 { 00095 q.query("\n"+ 00096 " SELECT t.name, t.address, t.phone, c.name || ': ' || p.description, patient_id\n"+ 00097 " FROM patients AS t\n"+ 00098 " INNER JOIN insurance_policies AS p USING (insurance_policy_id)\n"+ 00099 " INNER JOIN insurance_companies AS c USING (insurance_company_id)\n"+ 00100 " ORDER BY t.name\n"+ 00101 " "); 00102 00103 if (q.r != null) 00104 { 00105 p("<table border=1>\n"); 00106 p("<tr><td>Name</td><td>Phone</td><td>Address</td><td>Insurance</td><td> </td></tr>\n"); 00107 while(q.r.next()) 00108 { 00109 p("<tr><td>" + q.r.getString(1) + "</td><td>" + q.r.getString(2) 00110 + "</td><td>" + q.r.getString(3) + "</td><td>" + q.r.getString(4) 00111 + "</td><td>"); 00112 00113 action.display("Edit", EDIT, q.r.getInt(5)); 00114 action.display("Delete", DELETE, q.r.getInt(5)); 00115 00116 p("</td></tr>\n"); 00117 } 00118 p("</table>\n"); 00119 } 00120 } 00121 catch(SQLException e) 00122 { 00123 p(e); 00124 } 00125 finally 00126 { 00127 q.close(); 00128 } 00129 } 00130 };