01: package simpleorm.simplewebapp.eg.database;
02:
03: import simpleorm.simplewebapp.core.*;
04: import simpleorm.simplewebapp.eg.dbute.WUser;
05: import simpleorm.simplewebapp.scalarFields.WFieldString;
06: import simpleorm.simplewebapp.scalarFields.WFieldInteger;
07: import simpleorm.core.*;
08:
09: public class WUserManualCrudPage extends WTestPage {
10:
11: public final WUserManualCrudPagelet pagelet = new WUserManualCrudPagelet(
12: this );
13:
14: /**
15: * Just to demonstrate a getter, not necessary, see .jsp.
16: */
17: public WField getMyId() {
18: return getField("crud_crud_userId");
19: }
20:
21: public static class WUserManualCrudPagelet extends WPagelet {
22:
23: public WButton update = addNewButton("Update");
24: public WButton delete = addNewButton("Delete");
25: public WButton add = addNewButton("Add");
26:
27: WFieldGroup crudGroup = addGroup(this , new WFieldGroup("crud"));
28:
29: final WFieldInteger userId = addField(crudGroup,
30: new WFieldInteger("userId")).setReadOnly(true);
31: final WField name = addField(crudGroup,
32: new WFieldString("name")).setRequired(true);
33: final WField location = addField(crudGroup, new WFieldString(
34: "location"));
35:
36: public WUserManualCrudPagelet(WUserManualCrudPage page) {
37: super (page, "crud");
38: }
39:
40: @Override
41: protected void onPreValidate() {
42: String theKey = userId.getText();
43: if (theKey == null) {
44: update.setDisabled(true);
45: delete.setDisabled(true);
46: } else
47: add.setDisabled(true);
48: }
49:
50: @Override
51: protected void onNotSubmitted() {
52: //SRecordInstance rec = meta.findOrCreate();
53: Integer theKey = userId.getValue();
54: if (theKey != null) {
55: WUser user = (WUser) WUser.meta.findOrCreate(theKey);
56: name.setValue(user.getString(WUser.NAME));
57: location.setValue(user.getString(WUser.LOCATION));
58: }
59: }
60:
61: @Override
62: public void onWasSubmitted() {
63: Integer theKey = userId.getValue();
64: WUser user;
65: if (theKey != null)
66: user = (WUser) WUser.meta.findOrCreate(theKey);
67: else if (getSubmitButton() == add) {
68: user = (WUser) WUser.meta.createWithGeneratedKey();
69: } else
70: throw new WException("Bad button with null key "
71: + getSubmitButton());
72: if (getSubmitButton() == update || getSubmitButton() == add) {
73: user.setString(WUser.NAME, (String) name.getValue());
74: user.setString(WUser.LOCATION, (String) location
75: .getValue());
76: } else if (getSubmitButton() == delete) {
77: user.deleteRecord();
78: nullAllFields();
79: } else
80: throw new WException("Bad button " + getSubmitButton());
81: getPageStructure().setActualRedirectUrl(
82: getPotentialSuccessUrl());
83: }
84:
85: protected @Override
86: void onMaybeErroneous() {
87: if (isErroneous() && SConnection.getConnection().hasBegun()) {
88: SConnection.rollback(); // ensure that any partial updates are ignored
89: SConnection.begin();
90: }
91: }
92: }
93: }
|