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 org.araneaframework.example.main.TemplateBaseWidget;
018: import org.araneaframework.example.main.business.model.PersonMO;
019: import org.araneaframework.uilib.form.BeanFormWidget;
020: import org.araneaframework.uilib.form.control.DateControl;
021: import org.araneaframework.uilib.form.control.FloatControl;
022: import org.araneaframework.uilib.form.control.TextControl;
023:
024: /**
025: * This widget is for adding new or editing existing persons.
026: * Upon successful completion it returns the ID of stored person.
027: *
028: * @author <a href="mailto:rein@araneaframework.org">Rein Raudjärv</a>
029: */
030: public class PersonAddEditWidget extends TemplateBaseWidget {
031: private static final long serialVersionUID = 1L;
032: /* The ID field of the person data model, only has value if object has already
033: been saved to/loaded from database. */
034: private Long personId = null;
035: // Whether the person is being edited or added
036: private boolean editMode;
037:
038: /* The form. Person data (represented by class PersonMO) will be binded to it, thus
039: usage of BeanFormWidget instead of FormWidget. */
040: private BeanFormWidget form;
041:
042: /**
043: * Constructor for adding new person.
044: */
045: public PersonAddEditWidget() {
046: }
047:
048: /**
049: * Constructor for editing existing person with specified Id.
050: * @param personId Person's Id.
051: */
052: public PersonAddEditWidget(Long id) {
053: this .personId = id;
054: editMode = true;
055: }
056:
057: protected void init() throws Exception {
058: // Sets the view selector that will be used for rendering this widget. */
059: setViewSelector("person/personAddEdit");
060: // This viewdata is used in JSP to set component header (different for editing and adding).
061: putViewData("label", editMode ? "person.edit.form.label"
062: : "person.add.form.label");
063:
064: form = buildPersonEditForm();
065: addWidget("personForm", form);
066: }
067:
068: private BeanFormWidget buildPersonEditForm() throws Exception {
069: /* Create the form, specifying the class of data that is binded to this form. */
070: BeanFormWidget form = new BeanFormWidget(PersonMO.class);
071:
072: /* Adding the elements is done like in our SimpleFormWidget example, except
073: * that Data type is determined from bean class automatically and specifying
074: * it is not needed. */
075:
076: //BeanFormWidget.addBeanElement(String elementName, String labelId, Control control, boolean mandatory)
077: form.addBeanElement("name", "#First name", new TextControl(),
078: true);
079: form.addBeanElement("surname", "#Last name", new TextControl(),
080: false);
081: form.addBeanElement("phone", "#Phone no", new TextControl(),
082: true);
083: form.addBeanElement("birthdate", "#Birthdate",
084: new DateControl(), false);
085: form.addBeanElement("salary", "#Salary", new FloatControl(),
086: false);
087:
088: if (editMode) {
089: /* fetch the person with given ID from database */
090: PersonMO person = (PersonMO) getGeneralDAO().getById(
091: PersonMO.class, personId);
092: /* and fill the form with current person data */
093: form.readFromBean(person);
094: } /* otherwise we have no data and all form fields are initially left blank */
095:
096: return form;
097: }
098:
099: public void handleEventSave(String eventParameter) throws Exception {
100: // if form data is found to be valid
101: if (form.convertAndValidate()) {
102: // get the current person data (retrieved from database by getGeneralDAO() in case person already has assigned ID);
103: PersonMO person = personId != null ? (PersonMO) getGeneralDAO()
104: .getById(PersonMO.class, personId)
105: : new PersonMO();
106: // read the application user supplied data from form into model object.
107: person = (PersonMO) form.writeToBean(person);
108:
109: if (editMode) {
110: // updates person object in database
111: getGeneralDAO().edit(person);
112: } else {
113: // saves new person object to database
114: personId = getGeneralDAO().add(person);
115: }
116:
117: /* finish current flow execution and return to calling flow, returning database ID
118: * of added or edited person */
119: getFlowCtx().finish(personId);
120: } else {
121: /* Do nothing, error messages are applied to MessageContext by validating methods
122: * so that application user receives immediate feedback about incorrectly filled
123: * form elements automatically. */
124: }
125: }
126:
127: /* Cancels the adding/editing of current person, returns to calling flow */
128: public void handleEventCancel(String eventParameter)
129: throws Exception {
130: getFlowCtx().cancel();
131: }
132: }
|