01: package simpleorm.simplewebapp.eg.token;
02:
03: import simpleorm.simplewebapp.core.*;
04: import simpleorm.simplewebapp.scalarFields.WFieldString;
05: import rsa.edp.simplecrypt.WTokenDatabase;
06: import rsa.edp.simplecrypt.WTokenRecord;
07:
08: public class WTokenCrudPage extends WPage {
09:
10: public final WCrudPagelet pagelet = new WCrudPagelet(this );
11:
12: protected void onInitialize() throws Exception {
13: WTokenDatabase.db.beginTransaction();
14: }
15:
16: protected void onFinalize() throws Exception {
17: WTokenDatabase.db.endTransaction();
18: }
19:
20: public static class WCrudPagelet extends WPagelet {
21:
22: final WButton update = addNewButton("Update");
23: final WButton delete = addNewButton("Delete");
24: final WButton add = addNewButton("Add");
25:
26: WFieldGroup identity = addGroup(this , new WFieldGroup(
27: "user.identity"));
28: final WFieldString serial = addField(identity,
29: new WFieldString("serial")).setRequired(true);
30: final WField custom = addField(identity,
31: new WFieldString("custom")).setRequired(true);
32: WFieldGroup details = addGroup(this , new WFieldGroup(
33: "user.details"));
34: final WField keys = addField(details, new WFieldString("keys"));
35: final WField status = addField(details, new WFieldString(
36: "status", WField.SELECT));
37: final WField comment = addField(details, new WFieldString(
38: "comment").setDisplayLength(60));
39:
40: WTokenRecord row;
41:
42: public WCrudPagelet(WPage wpage) {
43: super (wpage, "crud");
44: status.getOptions().add("enabled");
45: status.getOptions().add("disabled");
46: }
47:
48: /**
49: * Disables buttons before fields are validated.
50: * Needs to retrieve the row here before field validators in order
51: * to be able to set the buttons correctly.
52: * (The messy case is when a user attempts to add a row, enters
53: * an id, but has validation errors.)
54: */
55: protected void onPreValidate() throws Exception {
56: String theKey = serial.getText(); // getValue() not available yet.
57: if (theKey != null)
58: row = (WTokenRecord) WTokenDatabase.db.findRow(theKey);
59: if (row == null) {
60: update.setDisabled(true);
61: delete.setDisabled(true);
62: } else
63: add.setDisabled(true);
64: }
65:
66: public void onNotSubmitted() { // ie. initial display of form
67: // Fetch data here
68: String theId = serial.getValue();
69: if (theId != null) {
70: WBeanUtils.retrieveBeanProperties(row,
71: getAllFieldValues());
72: //fields.name.setValue(row.name);
73: //fields.color.setValue(row.color);
74: //fields.alignment.setValue(row.alignment);
75: //fields.happy.setValue(row.happy);
76: //fields.count.setValue(row.count);
77: //fields.datefield.setValue(row.datefield);
78: }
79: }
80:
81: public void onWasSubmitted() { // ie. after a post back
82: String theId = serial.getValue();
83: if (getSubmitButton() == update || getSubmitButton() == add) {
84: if (row == null)
85: row = WTokenDatabase.db.addRow(theId);
86: WBeanUtils.updateBeanProperties(row,
87: getAllFieldValues());
88: WTokenDatabase.db.unifyKeys(custom.getText(), keys
89: .getText());
90: } else if (getSubmitButton() == delete) {
91: if (row != null)
92: WTokenDatabase.db.removeRow(theId);
93: }
94: }
95:
96: }
97:
98: }
|