00001 import java.sql.SQLException; 00002 import java.sql.PreparedStatement; 00003 00004 public class PatientEditor extends Widget 00005 { 00006 public static final int SAVE = 1; 00007 public static final int CANCEL = 2; 00008 00009 public int patient_id; 00010 public PolicySelect policy; 00011 public TextBox name; 00012 public TextBox address; 00013 public TextBox phone; 00014 00015 public ActionButton action; 00016 public boolean done = false; 00017 00018 public PatientEditor(int patient_id, String prefix, Form form) 00019 { 00020 super(prefix, form); 00021 this.patient_id = patient_id; 00022 action = new ActionButton(n("action"), form); 00023 name = new TextBox(0,20,n("name"), form); 00024 phone = new TextBox(0,20,n("phone"), form); 00025 address = new TextBox(2,20,n("address"), form); 00026 policy = new PolicySelect(n("policy"), form); 00027 addChild(action); 00028 addChild(name); 00029 addChild(phone); 00030 addChild(address); 00031 addChild(policy); 00032 } 00033 00034 public void loadValues() 00035 { 00036 super.loadValues(); 00037 00038 if (action.action == SAVE) 00039 { 00040 if (save()) done = true; 00041 } 00042 else if (action.action == CANCEL) 00043 { 00044 done = true; 00045 } 00046 } 00047 00048 public void loadDefaults() 00049 { 00050 super.loadDefaults(); 00051 Query q = new Query(form.connectDb(), form.pw); 00052 try 00053 { 00054 q.query("SELECT name, phone, address, insurance_policy_id FROM patients WHERE patient_id = " + patient_id); 00055 if (q.r == null || !q.r.next()) return; 00056 name.text = q.r.getString(1); 00057 phone.text = q.r.getString(2); 00058 address.text = q.r.getString(3); 00059 policy.id = new Integer(q.r.getInt(4)); 00060 } 00061 catch (SQLException e) 00062 { 00063 p(e); 00064 } 00065 finally 00066 { 00067 q.close(); 00068 } 00069 } 00070 00071 protected boolean save() 00072 { 00073 form.connectDb(); 00074 Query q = new Query(form.conn, form.pw); 00075 try 00076 { 00077 PreparedStatement s; 00078 if (patient_id > 0) 00079 { 00080 s = form.conn.prepareStatement("\n"+ 00081 " UPDATE patients SET name = ?, phone = ?, \n"+ 00082 " address = ?, insurance_policy_id = ?\n"+ 00083 " WHERE patient_id = " + patient_id); 00084 } 00085 else 00086 { 00087 s = form.conn.prepareStatement("\n"+ 00088 " INSERT INTO patients (name, phone, address, insurance_policy_id)\n"+ 00089 " VALUES (?,?,?,?)"); 00090 } 00091 try 00092 { 00093 s.setString(1, name.text); 00094 s.setString(2, phone.text); 00095 s.setString(3, address.text); 00096 s.setInt(4, policy.id.intValue()); 00097 s.executeUpdate(); 00098 } 00099 finally 00100 { 00101 s.close(); 00102 } 00103 00104 if (patient_id <= 0) 00105 { 00106 q.query("SELECT currval('patients_patient_id_seq')"); 00107 if (q != null && !q.r.next()) 00108 patient_id = q.r.getInt(1); 00109 } 00110 } 00111 catch (SQLException e) 00112 { 00113 p(e); 00114 } 00115 catch (Throwable e) 00116 { 00117 Query.rethrow(e); 00118 } 00119 finally 00120 { 00121 q.close(); 00122 } 00123 return true; 00124 } 00125 00126 public void display(boolean hidden) 00127 { 00128 super.display(hidden); 00129 00130 if (hidden || modalChild != null) return; 00131 00132 p( 00133 "<table>\n"+ 00134 "<tr>\n"+ 00135 " <td valign=top>Name:</td>\n"+ 00136 " <td>"); name.display(); p("</td>\n"+ 00137 "</tr>\n"+ 00138 "<tr>\n"+ 00139 " <td valign=top>Phone:</td>\n"+ 00140 " <td>"); phone.display(); p("</td>\n"+ 00141 "</tr>\n"+ 00142 "<tr>\n"+ 00143 " <td valign=top>Address:</td>\n"+ 00144 " <td>"); address.display(); p("</td>\n"+ 00145 "</tr>\n"+ 00146 "<tr>\n"+ 00147 " <td valign=top>Insurance Policy:</td>\n"+ 00148 " <td>"); policy.display(); p("</td>\n"+ 00149 "</tr>\n"+ 00150 "<tr>\n"+ 00151 " <td> </td>\n"+ 00152 " <td>\n"+ 00153 " "); action.display("Save Changes", SAVE); action.display("Cancel", CANCEL); 00154 p("\n"+ 00155 " </td>\n"+ 00156 "</td>\n"+ 00157 "</table>\n"+ 00158 ""); 00159 } 00160 };