001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.main.web.person;
016:
017: import java.util.List;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.araneaframework.backend.list.model.ListItemsData;
022: import org.araneaframework.backend.list.model.ListQuery;
023: import org.araneaframework.example.main.TemplateBaseWidget;
024: import org.araneaframework.example.main.business.data.IContractDAO;
025: import org.araneaframework.example.main.business.data.PersonListDAO;
026: import org.araneaframework.example.main.business.model.PersonMO;
027: import org.araneaframework.uilib.form.BeanFormWidget;
028: import org.araneaframework.uilib.form.FormWidget;
029: import org.araneaframework.uilib.form.control.DateControl;
030: import org.araneaframework.uilib.form.control.FloatControl;
031: import org.araneaframework.uilib.form.control.TextControl;
032: import org.araneaframework.uilib.form.formlist.BeanFormListWidget;
033: import org.araneaframework.uilib.form.formlist.FormListUtil;
034: import org.araneaframework.uilib.form.formlist.FormRow;
035: import org.araneaframework.uilib.form.formlist.FormRowHandler;
036: import org.araneaframework.uilib.form.formlist.adapter.ValidOnlyIndividualFormRowHandler;
037: import org.araneaframework.uilib.list.EditableBeanListWidget;
038: import org.araneaframework.uilib.list.dataprovider.BackendListDataProvider;
039: import org.araneaframework.uilib.list.dataprovider.ListDataProvider;
040: import org.araneaframework.uilib.list.dataprovider.MemoryBasedListDataProvider;
041:
042: public abstract class PersonEditableListWidget extends
043: TemplateBaseWidget {
044: protected static final Log log = LogFactory
045: .getLog(PersonEditableListWidget.class);
046: private IContractDAO contractDAO;
047: /* Editable list. */
048: private EditableBeanListWidget list;
049: /* Actual holder of editable list rows (resides inside EditableBeanListWidget).
050: Look inside init() method to see where it comes from. */
051: private BeanFormListWidget formList;
052:
053: protected void init() throws Exception {
054: setViewSelector("person/editableList");
055:
056: /* PersonMO class is already familiar from form examples.
057: FormRowHandler class that will handle the different row operations. */
058: list = new EditableBeanListWidget(buildFormRowHandler(),
059: PersonMO.class);
060: this .formList = list.getFormList();
061: addWidget("list", list);
062: list.setOrderableByDefault(true);
063: list.addField("id", "#Id", false);
064: /* Filtering by fields other than ID is enabled. */
065: list.addField("name", "#First name").like();
066: list.addField("surname", "#Last name").like();
067: list.addField("phone", "#Phone no").like();
068: list.addField("birthdate", "#Birthdate").range();
069: list.addField("salary", "#Salary").range();
070: list.addField("dummy", null, false);
071:
072: /* Set the provider through which list acquires its data. Exactly the same as for ordinary lists. */
073: list.setDataProvider(buildListDataProvider());
074: }
075:
076: protected abstract ListDataProvider buildListDataProvider()
077: throws Exception;
078:
079: protected abstract FormRowHandler buildFormRowHandler()
080: throws Exception;
081:
082: public static class Memory extends PersonEditableListWidget {
083: private static final long serialVersionUID = 1L;
084: private MemoryBasedListDataProvider dataProvider = new DataProvider();
085:
086: protected ListDataProvider buildListDataProvider()
087: throws Exception {
088: return dataProvider;
089: }
090:
091: protected FormRowHandler buildFormRowHandler() throws Exception {
092: /* Implementation of FormRowHandler that also calls dataprovider's
093: * data refresh methods when list editing events occur. */
094: return new PersonEditableRowHandler();
095: }
096:
097: private class DataProvider extends MemoryBasedListDataProvider {
098: private static final long serialVersionUID = 1L;
099:
100: protected DataProvider() {
101: super (PersonMO.class);
102: }
103:
104: public List loadData() throws Exception {
105: return getGeneralDAO().getAll(PersonMO.class);
106: }
107: }
108: }
109:
110: public static class Backend extends PersonEditableListWidget {
111: private static final long serialVersionUID = 1L;
112:
113: protected ListDataProvider buildListDataProvider()
114: throws Exception {
115: return new DataProvider();
116: }
117:
118: protected FormRowHandler buildFormRowHandler() throws Exception {
119: return new PersonEditableRowHandler();
120: }
121:
122: private class DataProvider extends BackendListDataProvider {
123: private static final long serialVersionUID = 1L;
124:
125: protected DataProvider() {
126: super (false);
127: }
128:
129: protected ListItemsData getItemRange(ListQuery query)
130: throws Exception {
131: return ((PersonListDAO) getBeanFactory().getBean(
132: "personListDAO")).getItems(query);
133: }
134: }
135: }
136:
137: /* Row handling functions. As this handler extends ValidOnlyIndividualFormRowHandler class,
138: * its saveRow method does nothing: instead saveValidRow method should be implemented that
139: * saves only these forms (rows) which data passes validation.
140: */
141: public class PersonEditableRowHandler extends
142: ValidOnlyIndividualFormRowHandler {
143: private static final long serialVersionUID = 1L;
144:
145: /* Implementation of the method that must return unique key for each row
146: * in editable list. As we hold database objects (PersonMO-s) in this list,
147: * it is natural to use synthetic ID field for a key.*/
148: public Object getRowKey(Object rowData) {
149: return ((PersonMO) rowData).getId();
150: }
151:
152: // Implementation of method that should save EDITED rows which data passes validation.
153: public void saveValidRow(FormRow editableRow) throws Exception {
154: /* Reads data from form. FormRow.getForm() method returns the widget that is
155: * currently holding row object data -- it is either FormWidget or BeanFormWidget, as
156: * in our case we are using EditableBeanListWidget that holds row data in BeanFormWidgets,
157: * we can cast the return type accordingly. */
158: PersonMO rowData = (PersonMO) ((BeanFormWidget) editableRow
159: .getForm()).writeToBean(new PersonMO());
160: rowData.setId((Long) editableRow.getKey());
161:
162: // Save modified object.
163: getGeneralDAO().edit(rowData);
164:
165: // Set the row closed (for further editing, it must be opened again).
166: editableRow.close();
167: }
168:
169: public void deleteRow(Object key) throws Exception {
170: Long id = (Long) key;
171: contractDAO.removeByPersonId(id);
172: getGeneralDAO().remove(PersonMO.class, id);
173: list.getDataProvider().refreshData();
174: }
175:
176: // Implementation of method that should save ADDED rows which data passes validation.
177: public void addValidRow(FormWidget addForm) throws Exception {
178: PersonMO rowData = (PersonMO) (((BeanFormWidget) addForm)
179: .writeToBean(new PersonMO()));
180: getGeneralDAO().add(rowData);
181: list.getDataProvider().refreshData();
182: // this callback must be made here!
183: formList.resetAddForm();
184: }
185:
186: // Called to initialize each row in editable list.
187: public void initFormRow(FormRow editableRow, Object rowData)
188: throws Exception {
189: // Set initial status of list rows to closed - they cannot be edited before opened.
190: editableRow.close();
191:
192: // Get the rowForm (this is the formwidget holding row object data).
193: BeanFormWidget rowForm = (BeanFormWidget) editableRow
194: .getForm();
195: // See below.
196: addCommonFormFields(rowForm);
197: /* A button that opens row for editing upon receiving onClick event.
198: * Activating button in already opened row saves the row data. */
199: FormListUtil.addEditSaveButtonToRowForm("#", formList,
200: rowForm, getRowKey(rowData));
201: /* A button that deletes this row and its data (calls deleteRow()). */
202: FormListUtil.addDeleteButtonToRowForm("#", formList,
203: rowForm, getRowKey(rowData));
204:
205: rowForm.readFromBean(rowData);
206: }
207:
208: // Called to initialize a blank row meant for adding new records.
209: public void initAddForm(FormWidget addForm) throws Exception {
210: addCommonFormFields((BeanFormWidget) addForm);
211: // Button that saves the content of the new record (calls addValidRow()).
212: FormListUtil.addAddButtonToAddForm("#", formList, addForm);
213: }
214:
215: // Adds PersonMO bean fields to given BeanFormWidget.
216: private void addCommonFormFields(BeanFormWidget form)
217: throws Exception {
218: form.addBeanElement("name", "#First name",
219: new TextControl(), true);
220: form.addBeanElement("surname", "#Last name",
221: new TextControl(), true);
222: form.addBeanElement("phone", "#Phone no",
223: new TextControl(), false);
224: form.addBeanElement("birthdate", "#Birthdate",
225: new DateControl(), false);
226: form.addBeanElement("salary", "#Salary",
227: new FloatControl(), false);
228: }
229: }
230:
231: public void injectContractDAO(IContractDAO contractDAO) {
232: this.contractDAO = contractDAO;
233: }
234: }
|