001: package simpleorm.simplewebapp.dbute;
002:
003: import simpleorm.simplewebapp.core.*;
004: import simpleorm.simplewebapp.scalarFields.WFieldString;
005: import simpleorm.simplewebapp.scalarFields.WFieldInteger;
006: import simpleorm.simplewebapp.scalarFields.WFieldBoolean;
007: import simpleorm.core.*;
008:
009: /**
010: * Does basic crud operations based on meta data.
011: * A transaction must have been begun.
012: */
013: public abstract class WGenericCrudPagelet extends WPagelet {
014:
015: public WButton update = addNewButton("Update");
016: public WButton delete = addNewButton("Delete");
017: public WButton add = addNewButton("Add");
018:
019: SRecordMeta meta;
020: WField key;
021:
022: WFieldGroup identityGroup = addGroup(this , new WFieldGroup(
023: "identity"));
024: WFieldGroup bodyGroup = addGroup(this , new WFieldGroup("body"));
025:
026: public WGenericCrudPagelet(WPage wpage, SRecordMeta meta) {
027: super (wpage, "crud");
028: this .meta = meta;
029: }
030:
031: /**
032: * Very auto way to generate all Crud WPageStructure.
033: */
034: public void generateCrudFields() {
035: for (Object fname : meta.getFieldNames()) {
036: SFieldMeta sfield = meta.getField((String) fname);
037: WField wfield = addGeneratedCrudField(sfield);
038: }
039: }
040:
041: public WField addGeneratedCrudField(SFieldMeta sfield) {
042: WFieldGroup grp = sfield.getBoolean(SCon.SPRIMARY_KEY) ? identityGroup
043: : bodyGroup;
044: WField wfield = addField(grp, generateField(sfield));
045: String[] options = (String[]) sfield
046: .getProperty(WRecordInstance.SELECT_OPTIONS);
047: if (options != null) {
048: wfield.getOptions().add("");
049: for (String opt : options)
050: wfield.getOptions().add(opt);
051: wfield.setWidget(WField.SELECT);
052: }
053: return wfield;
054: }
055:
056: /** Create a new SimpleWebApp WField that corresponds to the given SimpleORM SFieldMeta.
057: * Copies meta attributes across such as required and size.
058: */
059: public WField generateField(SFieldMeta sfield) {
060: String name = sfield.getString(SCon.SFIELD_NAME);
061: WField wfield;
062: if (sfield instanceof SFieldString) {
063: wfield = new WFieldString(name);
064: int size = (Integer) sfield.getProperty(SCon.SBYTE_SIZE);
065: wfield.setMaxLength(size);
066: wfield.setDisplayLength(size < 60 ? size : 60);
067: } else if (sfield instanceof SFieldInteger)
068: wfield = new WFieldInteger(name);
069: else if (sfield instanceof SFieldBoolean) {
070: wfield = new WFieldBoolean(name);
071: } else
072: throw new WException("Cann't create WField for " + sfield);
073: wfield.setDataField(sfield);
074: if (sfield.getBoolean(SCon.SPRIMARY_KEY)) {
075: if (key != null)
076: throw new WException("Only one key supported for now "
077: + key + sfield);
078: else
079: key = wfield;
080: wfield.setReadOnly(true);
081: }
082: if (sfield.getBoolean(SCon.SMANDATORY))
083: wfield.setRequired(true);
084: return wfield;
085: }
086:
087: protected @Override
088: void onPreValidate() throws Exception {
089: String theKey = key.getText();
090: if (theKey == null) {
091: update.setDisabled(true);
092: delete.setDisabled(true);
093: } else
094: add.setDisabled(true);
095: }
096:
097: protected @Override
098: void onNotSubmitted() {
099: //SRecordInstance rec = meta.findOrCreate();
100: Object theKey = key.getValue();
101: if (theKey != null) {
102: SRecordInstance rec = meta.findOrCreate(theKey);
103: for (WField wfield : getAllFieldValues()) {
104: SFieldMeta sfield = (SFieldMeta) wfield.getDataField(); //WUser.meta.getFieldStrict(wfield.getDataName());
105: Object value = rec.getObject(sfield);
106: if (logger().isDebug())
107: logger().debug(
108: "onNotSubmitted " + sfield + " == "
109: + value);
110: wfield.setValue(value);
111: }
112: }
113: }
114:
115: protected @Override
116: void onWasSubmitted() {
117: Object theKey = key.getValue();
118: SRecordInstance rec;
119: if (theKey != null)
120: rec = meta.findOrCreate(theKey);
121: else if (getSubmitButton() == add) {
122: rec = meta.createWithGeneratedKey();
123: } else
124: throw new WException("Bad button with null key "
125: + getSubmitButton());
126:
127: if (getSubmitButton() == update || getSubmitButton() == add) {
128: for (WField wfield : getAllFieldValues()) {
129: if (wfield != key) {
130: SFieldMeta sfield = (SFieldMeta) wfield
131: .getDataField(); //WUser.meta.getFieldStrict(wfield.getDataName());
132: Object value = wfield.getValue();
133: if (logger().isDebug())
134: logger().debug(
135: "onWasSubmitted " + sfield + " := "
136: + value);
137: try {
138: rec.setObject(sfield, value);
139: } catch (WValidationException ve) {
140: ve.setField(wfield);
141: throw ve;
142: }
143: }
144: }
145: } else if (getSubmitButton() == delete) {
146: rec.deleteRecord();
147: nullAllFields();
148: } else
149: throw new WException("Bad button " + getSubmitButton());
150: SConnection.commit(); // Ensure subsequent Selects do not access deleted record.
151: SConnection.begin();
152: getPageStructure().setActualRedirectUrl(
153: getPotentialSuccessUrl());
154: }
155:
156: protected @Override
157: void onMaybeErroneous() {
158: if (isErroneous()) { // && SConnection.getConnection().hasBegun() -- must be in a transaction
159: SConnection.rollback(); // ensure that any partial updates are ignored
160: SConnection.begin();
161: }
162: }
163:
164: //////////////////
165:
166: public WButton getUpdate() {
167: return update;
168: }
169:
170: public WButton getDelete() {
171: return delete;
172: }
173:
174: public WButton getAdd() {
175: return add;
176: }
177:
178: public SRecordMeta getMeta() {
179: return meta;
180: }
181:
182: public WField getKey() {
183: return key;
184: }
185:
186: public WFieldGroup getBodyGroup() {
187: return bodyGroup;
188: }
189:
190: }
|