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.contract;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019: import org.araneaframework.Widget;
020: import org.araneaframework.example.common.framework.container.StandardWizardWidget;
021: import org.araneaframework.example.common.framework.context.WizardContext;
022: import org.araneaframework.example.main.TemplateBaseWidget;
023: import org.araneaframework.example.main.business.model.ContractMO;
024:
025: /**
026: * This widget is for adding new and editing existing contracts.
027: * It returns the Id of stored contract or cancels current call.
028: *
029: * @author Rein Raudjärv <reinra@ut.ee>
030: */
031: public class ContractAddEditWidget extends TemplateBaseWidget {
032:
033: private static final long serialVersionUID = 1L;
034:
035: private static final Log log = LogFactory
036: .getLog(ContractAddEditWidget.class);
037:
038: private Long id = null;
039: private ContractCompanyEditWidget company;
040: private ContractPersonEditWidget person;
041: private ContractNotesEditWidget notes;
042:
043: /**
044: * Constructor for adding new contract.
045: */
046: public ContractAddEditWidget() {
047: super ();
048: }
049:
050: /**
051: * Constructor for editing existing contract with specified Id.
052: * @param id Contract's Id.
053: */
054: public ContractAddEditWidget(Long id) {
055: this ();
056: this .id = id;
057: }
058:
059: protected void init() throws Exception {
060: setViewSelector("contract/contractAddEdit");
061: company = new ContractCompanyEditWidget();
062: person = new ContractPersonEditWidget();
063: notes = new ContractNotesEditWidget();
064:
065: StandardWizardWidget wizard = new StandardWizardWidget();
066: addWidget("wizard", wizard);
067: wizard.addPage(company);
068: wizard.addPage(person);
069: wizard.addPage(notes);
070:
071: if (id != null) {
072: ContractMO contract = (ContractMO) getGeneralDAO().getById(
073: ContractMO.class, id);
074: company.setCompany(contract.getCompany());
075: person.setPerson(contract.getPerson());
076: notes.setNotes(contract.getNotes());
077: notes.setTotal(contract.getTotal());
078: }
079:
080: wizard.addEventListener(new WizardContext.EventListener() {
081: private static final long serialVersionUID = 1L;
082:
083: public void onGoto(Widget page) throws Exception {
084: }
085:
086: public void onSubmit() throws Exception {
087: if (validate()) {
088: ContractMO contract = id != null ? (ContractMO) getGeneralDAO()
089: .getById(ContractMO.class, id)
090: : new ContractMO();
091: contract.setCompany(company.getCompany());
092: contract.setPerson(person.getPerson());
093: contract.setNotes(notes.getNotes());
094: contract.setTotal(notes.getTotal());
095: if (id != null) {
096: getGeneralDAO().edit(contract);
097: } else {
098: id = getGeneralDAO().add(contract);
099: }
100: log.debug("Contract saved, id = " + id);
101: getFlowCtx().finish(id);
102: }
103: }
104:
105: public void onCancel() throws Exception {
106: getFlowCtx().cancel();
107: }
108: });
109: }
110:
111: protected boolean validate() throws Exception {
112: return company.getCompany() != null
113: && person.getPerson() != null
114: && notes.getForm().convertAndValidate();
115: }
116: }
|