001: package simpleorm.simplewebapp.eg.simple;
002:
003: import simpleorm.simplewebapp.core.*;
004: import simpleorm.simplewebapp.scalarFields.WFieldString;
005: import simpleorm.simplewebapp.scalarFields.WFieldDate;
006: import simpleorm.simplewebapp.scalarFields.WFieldInteger;
007:
008: /**
009: * A simple demonstration of a CRUD page.
010: * The implementation of the logic is very manual, see WUserAutoCrudPage
011: * for a more automated version.<p>
012: *
013: * (This is subtyped by WManualCrudPage and WAutoCrudPage just to
014: * demonstrate two ways of implementing JSPs. This style would not normally be
015: * subtyped at all.)
016: */
017: public class WCrudPage extends WPage {
018:
019: public final WCrudPagelet pagelet = new WCrudPagelet(this );
020:
021: /**
022: * NOT necessary, just here to demonstrate.
023: * This is accessesed as ${wPage.id} in ManualCrudTest.jsp
024: * (Vs the normal ${wPage.fields.id}.)
025: */
026: public WField getId() {
027: return pagelet.id;
028: }
029:
030: protected @Override
031: void onInitialize() throws Exception {
032: WTestDatabase.db.beginTransaction();
033: }
034:
035: protected @Override
036: void onFinalize() throws Exception {
037: WTestDatabase.db.endTransaction();
038: }
039:
040: public static class WCrudPagelet extends WPagelet {
041: // Needs to be static class as newed by WManualListCrudPage
042:
043: final WButton update = addNewButton("Update");
044: final WButton delete = addNewButton("Delete");
045: final WButton add = addNewButton("Add");
046:
047: WFieldGroup identity = addGroup(this , new WFieldGroup(
048: "userIdentity"));
049: final WFieldString id = addField(identity,
050: new WFieldString("id")).setRequired(true);
051: final WField name = addField(identity,
052: new WFieldString("name").setDisplayLength(60))
053: .setRequired(true);
054: final WField database = addField(identity, new WFieldString(
055: "database").setReadOnly(true));
056:
057: WFieldGroup details = addGroup(this , new WFieldGroup(
058: "userDetails"));
059: final WField color = addField(details, new WFieldString(
060: "color", WField.SELECT));
061: final WField alignment = addField(details, new WFieldString(
062: "alignment", WField.RADIO));
063: final WField happy = addField(details, new WFieldString(
064: "happy", WField.CHECKBOX));
065: final WFieldInteger count = addField(details,
066: new WFieldInteger("count")).setRequired(true);
067: final WFieldDate datefield = addField(details, new WFieldDate(
068: "datefield"));
069:
070: public WCrudPagelet(WPage wpage) {
071: // wpage parameter rather than non-static inner class as also used from WManualListCrudPage
072: super (wpage, "crud");
073: //name.setPostedText("Mr Green");
074: color.getOptions().add("Red");
075: color.getOptions().add("Green");
076: color.getOptions().add("Blue");
077: //color.setPostedText("Green");
078: alignment.getOptions().add("Left");
079: alignment.getOptions().add("Middle");
080: alignment.getOptions().add("Right");
081: //alignment.setPostedText("Middle");
082: }
083:
084: /**
085: * Disables buttons before fields are validated.
086: * Needs to retrieve the row here before field validators in order
087: * to be able to set the buttons correctly.
088: * (The messy case is when a user attempts to add a row, enters
089: * an id, but has validation errors.)
090: */
091: protected @Override
092: void onPreValidate() throws Exception {
093: String theKey = id.getText(); // getValue() not available yet.
094: if (theKey == null) {
095: update.setDisabled(true);
096: delete.setDisabled(true);
097: } else
098: add.setDisabled(true);
099: }
100:
101: public @Override
102: void onNotSubmitted() { // ie. initial display of form
103: // Fetch data here
104: String theId = id.getValue();
105: if (theId != null) {
106: WTestRecord row = (WTestRecord) WTestDatabase.db
107: .findRow(theId);
108: WBeanUtils.retrieveBeanProperties(row,
109: getAllFieldValues());
110: //fields.name.setValue(row.name);
111: //fields.color.setValue(row.color);
112: //fields.alignment.setValue(row.alignment);
113: //fields.happy.setValue(row.happy);
114: //fields.count.setValue(row.count);
115: //fields.datefield.setValue(row.datefield);
116: }
117: }
118:
119: public @Override
120: void onWasSubmitted() { // ie. after a post back
121: String theId = id.getValue();
122: if (theId != null) {
123: WTestRecord row = (WTestRecord) WTestDatabase.db
124: .findRow(theId);
125: if (getSubmitButton() == update
126: || getSubmitButton() == add) {
127: int cv = count.getIntValue(0);
128: if (cv % 2 != 1)
129: throw new WValidationException(
130: "Crud.CountMustBeOdd", count)
131: .setParameter("" + cv);
132: if (row == null)
133: row = WTestDatabase.db.addRow(theId);
134: WBeanUtils.updateBeanProperties(row,
135: getAllFieldValues());
136: //row.name = fields.name.getValue();
137: //row.color = fields.color.getValue();
138: //row.count = fields.count.getIntValue(0);
139: //row.datefield = fields.datefield.getDateValue(null);
140: } else if (getSubmitButton() == delete) {
141: if (row != null)
142: WTestDatabase.db.removeRow(row.idx);
143: nullAllFields();
144: }
145: // else throw new WException("Bad button " + getSubmitButton());
146: // no exception, it might be from a different pagelet.
147: //WTestDatabase.db.endTransaction();
148: // WTestDatabase.db.beginTransaction();
149:
150: getPageStructure().setActualRedirectUrl(
151: getPotentialSuccessUrl());
152: }
153: }
154:
155: protected @Override
156: void onPostMaybeSubmitted() throws Exception {
157: database.setValue(WTestDatabase.db.getDatabaseName()); // Hibernate or in Memory
158: }
159: }
160: }
|