01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.example.main.web.company;
16:
17: import org.apache.commons.logging.Log;
18: import org.apache.commons.logging.LogFactory;
19: import org.araneaframework.example.main.TemplateBaseWidget;
20: import org.araneaframework.example.main.business.model.CompanyMO;
21: import org.araneaframework.uilib.form.BeanFormWidget;
22: import org.araneaframework.uilib.form.control.TextControl;
23:
24: /**
25: * This widget is for adding new and editing existing companies.
26: * It retruns the Id of stored company or cancels current call.
27: *
28: * @author Rein Raudjärv <reinra@ut.ee>
29: */
30: public class CompanyEditWidget extends TemplateBaseWidget {
31: private static final long serialVersionUID = 1L;
32: private static final Log log = LogFactory
33: .getLog(CompanyEditWidget.class);
34: private Long id = null;
35: private BeanFormWidget form;
36:
37: /**
38: * Constructor for adding new company.
39: */
40: public CompanyEditWidget() {
41: }
42:
43: /**
44: * Constructor for editing existing company with specified Id.
45: * @param id Company's Id.
46: */
47: public CompanyEditWidget(Long id) {
48: this .id = id;
49: }
50:
51: protected void init() throws Exception {
52: setViewSelector("company/companyAddEdit");
53: putViewData("formLabel", id != null ? "company.edit.form.label"
54: : "company.add.form.label");
55: log.debug("CompanyEditWidget init called");
56:
57: form = new BeanFormWidget(CompanyMO.class);
58: form.addBeanElement("name", "#Name", new TextControl(), true);
59: form.addBeanElement("address", "#Address", new TextControl(),
60: true);
61:
62: if (id != null) {
63: CompanyMO company = (CompanyMO) getGeneralDAO().getById(
64: CompanyMO.class, id);
65: form.readFromBean(company);
66: }
67:
68: addWidget("form", form);
69: }
70:
71: public void handleEventSave(String eventParameter) throws Exception {
72: if (form.convertAndValidate()) {
73: CompanyMO company = id != null ? (CompanyMO) getGeneralDAO()
74: .getById(CompanyMO.class, id)
75: : new CompanyMO();
76:
77: company = (CompanyMO) form.writeToBean(company);
78:
79: if (id != null) {
80: getGeneralDAO().edit(company);
81: } else {
82: id = getGeneralDAO().add(company);
83: }
84: log.debug("Company saved, id = " + id);
85: getFlowCtx().finish(id);
86: }
87: }
88:
89: public void handleEventCancel(String eventParameter)
90: throws Exception {
91: getFlowCtx().cancel();
92: }
93: }
|