001: package simpleorm.simplewebapp.eg.token;
002:
003: import simpleorm.simplewebapp.core.*;
004: import simpleorm.simplewebapp.eg.dbute.WCreateDB;
005: import simpleorm.simplewebapp.scalarFields.WFieldString;
006:
007: import java.sql.Connection;
008: import java.sql.DatabaseMetaData;
009: import java.sql.ResultSet;
010: import java.sql.PreparedStatement;
011: import java.util.LinkedHashMap;
012:
013: public class WRowCrudPage extends WPage {
014:
015: public final WRowCrudPage.WCrudPagelet pagelet = new WRowCrudPage.WCrudPagelet(
016: this );
017:
018: Connection con;
019:
020: protected void onInitialize() throws Exception {
021: con = WCreateDB.openJdbc();
022: }
023:
024: protected void onFinalize() throws Exception {
025: if (con != null)
026: con.close();
027: }
028:
029: public static class WCrudPagelet extends WPagelet {
030:
031: final WButton update = addNewButton("Update");
032: final WButton delete = addNewButton("Delete");
033: final WButton add = addNewButton("Add");
034:
035: String table_name = "RSA_USER";
036:
037: WFieldGroup identity = addGroup(this , new WFieldGroup(
038: "user.identity"));
039:
040: WFieldGroup details = addGroup(this , new WFieldGroup(
041: "user.details"));
042: LinkedHashMap<String, WField> colFields = new LinkedHashMap();
043: WFieldString keyField = null;
044:
045: LinkedHashMap<String, String> row = new LinkedHashMap(); // fieldname --> value
046:
047: public WCrudPagelet(WPage wpage) {
048: super (wpage, "crud");
049: }
050:
051: Connection getCon() {
052: return ((WRowCrudPage) getPage()).con;
053: }
054:
055: protected void onInitialize() throws Exception {
056: createFields();
057: }
058:
059: /**
060: * Disables buttons before fields are validated.
061: * Needs to retrieve the row here before field validators in order
062: * to be able to set the buttons correctly.
063: * (The messy case is when a user attempts to add a row, enters
064: * an id, but has validation errors.)
065: */
066: protected void onPreValidate() throws Exception {
067: String theKey = keyField.getText(); // getValue() not available yet.
068: if (theKey != null)
069: row = retrieveRow(theKey);
070: if (row == null) {
071: update.setDisabled(true);
072: delete.setDisabled(true);
073: } else
074: add.setDisabled(true);
075: }
076:
077: public void onNotSubmitted() { // ie. initial display of form
078: // Fetch data here
079: String theId = keyField.getValue();
080: if (theId != null) {
081: for (String col : colFields.keySet()) {
082: colFields.get(col).setValue(row.get(col));
083: }
084: }
085: }
086:
087: public void onWasSubmitted() throws Exception { // ie. after a post back
088: String key = keyField.getValue();
089: if (getSubmitButton() == update || getSubmitButton() == add) {
090: boolean add = row == null;
091: if (row == null)
092: row = new LinkedHashMap();
093: for (String colField : colFields.keySet()) {
094: row.put(colField, (String) colFields.get(colField)
095: .getValue());
096: }
097: if (add)
098: addRow(row);
099: else
100: updateRow(key, row);
101: } else if (getSubmitButton() == delete) {
102: if (row != null)
103: deleteRow(key);
104: }
105: }
106:
107: void createFields() throws Exception {
108: /// Read meta data to define WFields dynamically
109: DatabaseMetaData dbmd = getCon().getMetaData();
110: ResultSet cols = dbmd.getColumns(null, // ## allTables.getString("TABLE_CAT"),
111: null, // ##allTables.getString("TABLE_SCHEM"),
112: table_name, null);
113: while (cols.next()) {
114: String colName = cols.getString("COLUMN_NAME");
115: WFieldGroup fgroup = keyField == null ? identity
116: : details;
117: WFieldString fld = new WFieldString(colName);
118: if (keyField == null)
119: keyField = fld;
120: colFields.put(colName, addField(fgroup, fld));
121: }
122: cols.close();
123: if (keyField == null)
124: throw new WValidationException(
125: "No columns, maybe need to initialize database "
126: + table_name, null);
127: }
128:
129: LinkedHashMap retrieveRow(String keyValue) throws Exception {
130: PreparedStatement ps = getCon().prepareStatement(
131: "SELECT * FROM " + table_name + " WHERE \""
132: + keyField.getDataName() + "\" = ?");
133: ps.setString(1, keyValue);
134: ResultSet rs = ps.executeQuery();
135:
136: if (!rs.next())
137: return null;
138:
139: LinkedHashMap row = new LinkedHashMap();
140: for (String colField : colFields.keySet()) {
141: row.put(colField, rs.getString(colField));
142: }
143:
144: if (rs.next())
145: throw new WValidationException(
146: "Primary key not unique " + table_name + " : "
147: + keyValue, keyField);
148:
149: rs.close();
150: ps.close();
151: return row;
152: }
153:
154: void updateRow(String keyValue,
155: LinkedHashMap<String, String> row) throws Exception {
156: String set = "";
157: for (String col1 : row.keySet()) {
158: if (!"".equals(set))
159: set += ", ";
160: set += "\"" + col1 + "\" = ?";
161: }
162: PreparedStatement ps = getCon().prepareStatement(
163: "UPDATE " + table_name + " SET " + set
164: + " WHERE \"" + keyField.getDataName()
165: + "\" = ?");
166: int fld = 1;
167: for (String col2 : row.keySet()) {
168: ps.setString(fld++, row.get(col2));
169: }
170: ps.setString(fld++, keyValue);
171:
172: ps.executeUpdate();
173: ps.close();
174: }
175:
176: void addRow(LinkedHashMap<String, String> row) throws Exception {
177: String insert = "", values = "";
178: for (String col1 : row.keySet()) {
179: if (!"".equals(insert)) {
180: insert += ", ";
181: values += ", ";
182: }
183: insert += "\"" + col1 + "\"";
184: values += "?";
185: }
186: PreparedStatement ps = getCon().prepareStatement(
187: "INSERT INTO " + table_name + " (" + insert
188: + ") VALUES (" + values + ")");
189: int fld = 1;
190: for (String col3 : row.keySet()) {
191: ps.setString(fld++, row.get(col3));
192: }
193: ps.executeUpdate();
194: ps.close();
195: }
196:
197: void deleteRow(String keyValue) throws Exception {
198: PreparedStatement ps = getCon().prepareStatement(
199: "DELETE FROM " + table_name + " WHERE \""
200: + keyField.getDataName() + "\" = ?");
201: ps.setString(1, keyValue);
202:
203: ps.executeUpdate();
204: ps.close();
205: }
206:
207: }
208: }
|