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.company;
016:
017: import java.util.List;
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.araneaframework.example.main.TemplateBaseWidget;
021: import org.araneaframework.example.main.business.data.IContractDAO;
022: import org.araneaframework.example.main.business.model.CompanyMO;
023: import org.araneaframework.framework.FlowContext;
024: import org.araneaframework.uilib.list.BeanListWidget;
025: import org.araneaframework.uilib.list.dataprovider.MemoryBasedListDataProvider;
026:
027: /**
028: * This widget is for listing companies.
029: * It returns selected company's Id or cancels current call.
030: * It also allows a user to add or remove companies if it's set to edit mode.
031: *
032: * @author Rein Raudjärv <reinra@ut.ee>
033: */
034: public class CompanyListWidget extends TemplateBaseWidget {
035: private static final long serialVersionUID = 1L;
036: protected static final Log log = LogFactory
037: .getLog(CompanyListWidget.class);
038: private BeanListWidget list;
039: private boolean editMode = true;
040:
041: private IContractDAO contractDAO;
042:
043: public CompanyListWidget() {
044: super ();
045: }
046:
047: public CompanyListWidget(boolean editMode) {
048: super ();
049: this .editMode = editMode;
050: }
051:
052: protected void init() throws Exception {
053: setViewSelector("company/companyList");
054: log.debug("TemplateCompanyListWidget init called");
055:
056: initList();
057: }
058:
059: protected void initList() throws Exception {
060: // Create the new list widget whose records are JavaBeans, instances of CompanyMO.
061: // CompanyMO has fields named id, name and address.
062: list = new BeanListWidget(CompanyMO.class);
063: addWidget("companyList", this .list);
064: // set the data provider for the list
065: list.setDataProvider(new TemplateCompanyListDataProvider());
066: // add the displayed columns to list.
067: // addField(String id, String label, boolean orderable)
068: // note that # before the label means that label is treated as unlocalized and outputted as-is
069: list.addField("id", "#Id", false);
070: // addField(...) returns FieldFilterHelper, like() sets LIKE filter on the column
071: list.addField("name", "#Name", true).like();
072: list.addField("address", "#Address", true).like();
073: list.addField("dummy", null, false);
074: }
075:
076: private void refreshList() throws Exception {
077: this .list.getDataProvider().refreshData();
078: }
079:
080: public void handleEventAdd(String eventParameter) throws Exception {
081: getFlowCtx().start(new CompanyEditWidget(),
082: new FlowContext.Handler() {
083: private static final long serialVersionUID = 1L;
084:
085: public void onFinish(Object returnValue)
086: throws Exception {
087: log.debug("Company added with Id of "
088: + returnValue + " sucessfully");
089: // trick to refresh the list data when we suspect it has changed
090: refreshList();
091: }
092:
093: public void onCancel() throws Exception {
094: }
095: });
096: }
097:
098: public void handleEventRemove(String eventParameter)
099: throws Exception {
100: Long id = ((CompanyMO) this .list
101: .getRowFromRequestId(eventParameter)).getId();
102: contractDAO.removeByCompanyId(id);
103: getGeneralDAO().remove(CompanyMO.class, id);
104: refreshList();
105: log.debug("Company with Id of " + id + " removed sucessfully");
106: }
107:
108: public void handleEventSelect(String eventParameter)
109: throws Exception {
110: Long id = ((CompanyMO) this .list
111: .getRowFromRequestId(eventParameter)).getId();
112: log.debug("Company selected with Id of " + id);
113: if (editMode)
114: getFlowCtx().start(new CompanyEditWidget(id),
115: new FlowContext.Handler() {
116: private static final long serialVersionUID = 1L;
117:
118: public void onFinish(Object returnValue)
119: throws Exception {
120: log.debug("Company added with Id of "
121: + returnValue + " sucessfully");
122: refreshList();
123: }
124:
125: public void onCancel() throws Exception {
126: }
127: });
128: else
129: getFlowCtx().finish(id);
130: }
131:
132: public void handleEventEdit(String eventParameter) throws Exception {
133: Long id = ((CompanyMO) this .list
134: .getRowFromRequestId(eventParameter)).getId();
135: log.debug("Company selected with Id of " + id);
136: getFlowCtx().start(new CompanyEditWidget(id),
137: new FlowContext.Handler() {
138: private static final long serialVersionUID = 1L;
139:
140: public void onFinish(Object returnValue)
141: throws Exception {
142: log.debug("Company added with Id of "
143: + returnValue + " sucessfully");
144: refreshList();
145: }
146:
147: public void onCancel() throws Exception {
148: }
149: });
150: }
151:
152: public void handleEventCancel(String eventParameter)
153: throws Exception {
154: getFlowCtx().cancel();
155: }
156:
157: private class TemplateCompanyListDataProvider extends
158: MemoryBasedListDataProvider {
159: private static final long serialVersionUID = 1L;
160:
161: // Overloading constructor with correct bean type.
162: protected TemplateCompanyListDataProvider() {
163: super (CompanyMO.class);
164: }
165:
166: // Overloading the real data loading method. Should
167: // return java.util.List containing CompanuMO objects.
168: public List loadData() throws Exception {
169: // Here, database query is performed and all rows from COMPANY table retrieved.
170: // But you could also get the data from parsing some XML file, /dev/random etc.
171: // All that matters is that returned List really contains CompanyMO objects.
172: return getGeneralDAO().getAll(CompanyMO.class);
173: }
174: }
175:
176: public void injectContractDAO(IContractDAO contractDAO) {
177: this.contractDAO = contractDAO;
178: }
179: }
|