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: 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.PersonMO;
023: import org.araneaframework.framework.FlowContext;
024: import org.araneaframework.framework.OverlayContext;
025: import org.araneaframework.uilib.list.BeanListWidget;
026: import org.araneaframework.uilib.list.ListWidget;
027: import org.araneaframework.uilib.list.dataprovider.MemoryBasedListDataProvider;
028:
029: /**
030: * This widget is for listing persons.
031: * It returns selected person's Id or cancels current call.
032: * It also allows a user to add or remove persons if it's set to edit mode.
033: *
034: * @author <a href="mailto:rein@araneaframework.org">Rein Raudjärv</a>
035: */
036: public class PersonListWidget extends TemplateBaseWidget {
037:
038: private static final long serialVersionUID = 1L;
039:
040: protected static final Log log = LogFactory
041: .getLog(PersonListWidget.class);
042:
043: private boolean editMode = false;
044: private boolean selectOnly = false;
045:
046: private IContractDAO contractDAO;
047:
048: private ListWidget list;
049:
050: public PersonListWidget() {
051: super ();
052: }
053:
054: /**
055: * @param editMode whether to allow add or remove persons.
056: */
057: public PersonListWidget(boolean editMode) {
058: super ();
059: this .editMode = editMode;
060: }
061:
062: protected void init() throws Exception {
063: setViewSelector("person/personList");
064: initList();
065: }
066:
067: protected void initList() throws Exception {
068: this .list = new BeanListWidget(PersonMO.class);
069: list.setDataProvider(new TemplatePersonListDataProvider());
070: list.addField("id", "#Id");
071: list.setOrderableByDefault(true);
072: list.addField("name", "#First name").like();
073: list.addField("surname", "#Last name").setIgnoreCase(false)
074: .like();
075: list.addField("phone", "#Phone no").like();
076: list.addField("birthdate", "#Birthdate").range();
077: list.addField("salary", "#Salary").range();
078:
079: list.setInitialOrder("name", true);
080:
081: // The dummy column without label (in list rows, some listRowLinkButton's will be written there).
082: // Needed to write out componentListHeader with correct number of columns.
083: list.addField("dummy", null, false);
084:
085: addWidget("personList", this .list);
086: }
087:
088: protected void refreshList() throws Exception {
089: this .list.getDataProvider().refreshData();
090: }
091:
092: public void handleEventAdd(String eventParameter) throws Exception {
093: getFlowCtx().start(new PersonAddEditWidget(), null,
094: new FlowContext.Handler() {
095: private static final long serialVersionUID = 1L;
096:
097: public void onFinish(Object returnValue)
098: throws Exception {
099: log.debug("Person added with Id of "
100: + returnValue + " sucessfully");
101: refreshList();
102: }
103:
104: public void onCancel() throws Exception {
105: // ignore call cancel
106: }
107: });
108: }
109:
110: public void handleEventRemove(String eventParameter)
111: throws Exception {
112: if (!this .editMode) {
113: throw new RuntimeException(
114: "Event 'remove' shoud be called only in edit mode");
115: }
116: Long id = ((PersonMO) this .list
117: .getRowFromRequestId(eventParameter)).getId();
118: contractDAO.removeByPersonId(id);
119: getGeneralDAO().remove(PersonMO.class, id);
120: refreshList();
121: log.debug("Person with Id of " + id + " removed sucessfully");
122: }
123:
124: public void handleEventSelect(String eventParameter)
125: throws Exception {
126: Long id = ((PersonMO) this .list
127: .getRowFromRequestId(eventParameter)).getId();
128: if (!selectOnly) {
129: PersonViewWidget newFlow = new PersonViewWidget(id);
130: getFlowCtx().start(newFlow, null, null);
131: } else {
132: getFlowCtx().finish(id);
133: }
134: }
135:
136: public void handleEventEdit(String eventParameter) throws Exception {
137: Long id = ((PersonMO) this .list
138: .getRowFromRequestId(eventParameter)).getId();
139: PersonAddEditWidget newFlow = new PersonAddEditWidget(id);
140:
141: getFlowCtx().start(newFlow, new FlowContext.Handler() {
142: private static final long serialVersionUID = 1L;
143:
144: public void onFinish(Object returnValue) throws Exception {
145: refreshList();
146: }
147:
148: public void onCancel() throws Exception {
149: }
150: });
151: }
152:
153: public void handleEventCancel(String eventParameter)
154: throws Exception {
155: getFlowCtx().cancel();
156: }
157:
158: public void setSelectOnly(boolean selectOnly) {
159: this .selectOnly = selectOnly;
160: }
161:
162: private class TemplatePersonListDataProvider extends
163: MemoryBasedListDataProvider {
164: private static final long serialVersionUID = 1L;
165:
166: protected TemplatePersonListDataProvider() {
167: super (PersonMO.class);
168: }
169:
170: public List loadData() throws Exception {
171: return getGeneralDAO().getAll(PersonMO.class);
172: }
173: }
174:
175: public void injectContractDAO(IContractDAO contractDAO) {
176: this.contractDAO = contractDAO;
177: }
178: }
|