00001 import java.util.Vector;
00002 import java.sql.SQLException;
00003 import java.sql.PreparedStatement;
00004
00005 public class TableEditor extends Widget
00006 {
00007 public static class Table
00008 {
00009 public String name;
00010 public int autoColumn;
00011 public String[] columns;
00012 public String select, update, delete, insert;
00013 public Table(String name, int autoColumn, String[] columns)
00014 {
00015 this.name = name;
00016 this.autoColumn = autoColumn;
00017 this.columns = columns;
00018
00019 delete = "DELETE FROM " + name + " WHERE oid = ?;\n";
00020
00021 StringBuffer select = new StringBuffer("SELECT oid");
00022 StringBuffer insert = new StringBuffer("INSERT INTO " + name + " (");
00023 StringBuffer insertLast = new StringBuffer("VALUES (");
00024 StringBuffer update = new StringBuffer("UPDATE " + name + " SET ");
00025
00026 boolean first = true, firstna = true;
00027 for (int i = 0; i < columns.length; ++i)
00028 {
00029 if (first) first = false; else update.append(", ");
00030 update.append(columns[i]);
00031 update.append(" = ?");
00032
00033 select.append(", ");
00034 select.append(columns[i]);
00035
00036 if (autoColumn != i)
00037 {
00038 if (firstna)
00039 firstna = false;
00040 else
00041 {
00042 insert.append(", ");
00043 insertLast.append(", ");
00044 }
00045 insert.append(columns[i]);
00046 insertLast.append("?");
00047 }
00048 }
00049 insert.append(") ");
00050 insert.append(insertLast);
00051 insert.append(")");
00052 select.append(" FROM " + name);
00053 update.append(" WHERE oid = ?");
00054
00055 this.update = update.toString();
00056 this.select = select.toString();
00057 this.insert = insert.toString();
00058 }
00059 }
00060
00061 static Table[] tables = new Table[10];
00062 static
00063 {
00064 String [] a = {"service_id", "appointment_id"};
00065 tables[0] = new Table("appointment_services", -1, a);
00066
00067 String [] b = {"appointment_id", "begintime", "endtime", "doctor_id", "patient_id"};
00068 tables[1] = new Table("appointments", 0, b);
00069
00070 String [] c = {"diagnosis_id", "appointment_id", "name", "description"};
00071 tables[2] = new Table("diagnoses", 0, c);
00072
00073 String [] d = {"doctor_id", "practice_id", "name"};
00074 tables[3] = new Table("doctors", 0, d);
00075
00076 String [] e = {"insurance_company_id", "name"};
00077 tables[4] = new Table("insurance_companies", 0, e);
00078
00079 String [] f = {"insurance_policy_id", "insurance_company_id", "description"};
00080 tables[5] = new Table("insurance_policies", 0, f);
00081
00082 String [] g = {"patient_id", "insurance_policy_id", "name", "address", "phone"};
00083 tables[6] = new Table("patients", 0, g);
00084
00085 String [] h = {"practice_id", "name", "address", "phone"};
00086 tables[7] = new Table("practices", 0, h);
00087
00088 String [] i = {"referral_id", "appointment_id", "doctor_id", "type"};
00089 tables[8] = new Table("referrals", 0, i);
00090
00091 String [] j = {"service_id", "name", "description", "price"};
00092 tables[9] = new Table("services", 0, j);
00093 }
00094
00095 public static final int CHOOSE = 1;
00096 public static final int EDIT = 2;
00097 public static final int DELETE = 3;
00098 public static final int SAVE = 4;
00099
00100 public int tableIdx = 0;
00101 public int oid = 0;
00102 public ActionButton action;
00103 public TextBox[] fields;
00104 public String message = "";
00105 public TableEditor(String prefix, Form form)
00106 {
00107 super(prefix, form);
00108 action = new ActionButton(n("action"), form);
00109 addChild(action);
00110 }
00111
00112 public void loadValues()
00113 {
00114 super.loadValues();
00115 tableIdx = toInt(loadAttribute("tableIdx"),0);
00116 int o = toInt(loadAttribute("oid"), 0);
00117
00118 if (action.action == EDIT)
00119 oid = action.param;
00120 else
00121 oid = o;
00122
00123 if (oid != 0)
00124 {
00125 fields = new TextBox[tables[tableIdx].columns.length];
00126 int cols = 100 / fields.length;
00127 if (cols > 20) cols = 20;
00128 for(int i = 0; i < fields.length; ++i)
00129 {
00130 TextBox t = new TextBox(0, cols, n("field" + i), form);
00131 if (oid == o) t.loadValues();
00132 fields[i] = t;
00133 addChild(t);
00134 }
00135 }
00136
00137 switch (action.action)
00138 {
00139 case SAVE:
00140 if (saveRow()) oid = 0;
00141 break;
00142 case DELETE:
00143 deleteRow(action.param);
00144 break;
00145 }
00146 }
00147
00148 public boolean saveRow()
00149 {
00150 Table t = tables[tableIdx];
00151 form.connectDb();
00152 try
00153 {
00154 PreparedStatement s = form.conn.prepareStatement(oid == -1 ? t.insert : t.update);
00155 try
00156 {
00157 int f = 0;
00158 for (int i = 0; i < fields.length; ++i)
00159 {
00160 if (oid != -1 || i != t.autoColumn)
00161 {
00162 ++f;
00163 s.setString(f, fields[i].text);
00164 }
00165 }
00166 if (oid != -1)
00167 {
00168 ++f;
00169 s.setInt(f, oid);
00170 }
00171
00172 s.executeUpdate();
00173 }
00174 finally
00175 {
00176 s.close();
00177 }
00178 }
00179 catch (SQLException e)
00180 {
00181 message = "<p><b><font color=red>Unable to save row. Postgres gives this error message:</font></b></p>\n"
00182 + "<blockquote style=\"background-color: #EEEEEE\">" + e.toString() + "</blockquote>\n";
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 form.disconnectDb();
00193 return false;
00194 }
00195 message = "<p><b><font color=blue>Row saved successfully</font></b></p>\n";
00196 return true;
00197 }
00198
00199 public void deleteRow(int row)
00200 {
00201 Table t = tables[tableIdx];
00202 form.connectDb();
00203 try
00204 {
00205 PreparedStatement s = form.conn.prepareStatement(t.delete);
00206 try
00207 {
00208 s.setInt(1,row);
00209 s.executeUpdate();
00210 }
00211 finally
00212 {
00213 s.close();
00214 }
00215 }
00216 catch (SQLException e)
00217 {
00218 message = "<p><b><font color=red>Unable to delete row. Postgres gives this error message:</font></b></p>\n"
00219 + "<blockquote style=\"background-color: #EEEEEE\">" + e.toString() + "</blockquote>\n";
00220 form.disconnectDb();
00221 return;
00222 }
00223 message = "<p><b><font color=blue>Row deleted successfully</font></b></p>\n";
00224 }
00225
00226 public void display(boolean hidden)
00227 {
00228 p(message);
00229
00230 super.display(hidden);
00231 printAttribute("oid", "" + oid);
00232 if (hidden || modalChild != null)
00233 {
00234 printAttribute("tableIdx", "" + tableIdx);
00235 return;
00236 }
00237
00238 p("<select name=" + n("tableIdx") + " rows=1 onchange=\"this.form.submit()\">\n");
00239 for (int i = 0; i < tables.length; ++i)
00240 {
00241 String selected = i == tableIdx ? " selected " : "";
00242 p(" <option value=" + i + selected + ">" + tables[i].name + "</option>\n");
00243 }
00244 p("</select>\n");
00245 action.display("Go", CHOOSE);
00246
00247 if (oid != -1)
00248 {
00249 p("<p align=center>");
00250 action.display("Insert new row...", EDIT, -1);
00251 p("</p>");
00252 }
00253
00254 Table t = tables[tableIdx];
00255 p("<table border=1>\n<tr>\n");
00256 for(int i = 0; i < t.columns.length; ++i)
00257 {
00258 p(" <td><strong>" + t.columns[i] + "</strong></td>\n");
00259 }
00260 p(" <td> </td>\n</tr>\n");
00261
00262 if (oid == -1)
00263 {
00264 p("<tr>\n");
00265 for (int i = 0; i < t.columns.length; ++i)
00266 {
00267 p(" <td>");
00268 if (i == t.autoColumn)
00269 p("<i>auto</i>");
00270 else
00271 fields[i].display();
00272 p("</td>\n");
00273 }
00274 p(" <td>");
00275 action.display("Save", SAVE);
00276 p("</td>\n</tr>\n");
00277 }
00278
00279 Query q = new Query(form.connectDb(), form.pw);
00280 try
00281 {
00282 q.query(t.select);
00283 if (q.r != null)
00284 while(q.r.next())
00285 {
00286 int oid = q.r.getInt(1);
00287
00288 if (oid == this.oid)
00289 {
00290 p("<tr>\n");
00291 for (int i = 0; i < t.columns.length; ++i)
00292 {
00293 p(" <td>");
00294 if (fields[i].text == null)
00295 fields[i].text = q.r.getString(i+2);
00296 fields[i].display();
00297 p("</td>\n");
00298 }
00299 p(" <td>");
00300 action.display("Save", SAVE);
00301 p("</td>\n</tr>\n");
00302 }
00303 else
00304 {
00305 p("<tr>\n");
00306 for (int i = 0; i < t.columns.length; ++i)
00307 p(" <td>" + htmlencode(q.r.getString(i+2)) + "</td>\n");
00308 p(" <td>");
00309 action.display("Edit", EDIT, oid);
00310 action.display("Delete", DELETE, oid);
00311 p("</td>\n</tr>\n");
00312 }
00313 }
00314 }
00315 catch (SQLException e)
00316 {
00317 p(e);
00318 }
00319 finally
00320 {
00321 q.close();
00322 }
00323 p("</table>");
00324 }
00325 };