001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/samples/tags/sakai_2-4-1/sample-tool-jsf/src/java/org/sakaiproject/sample/tool/JsfSampleBean.java $
003: * $Id: JsfSampleBean.java 17085 2006-10-12 19:38:14Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.sample.tool;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.Vector;
025:
026: import javax.faces.model.DataModel;
027: import javax.faces.model.ListDataModel;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * <p>
034: * Sample JSF UI controller bean for the sample jsf tool.
035: * </p>
036: */
037: public class JsfSampleBean {
038: /** Our log (commons). */
039: private static Log M_log = LogFactory.getLog(JsfSampleBean.class);
040:
041: /** Our service, injected. */
042: protected ItemService itemService;
043:
044: /** This needs to hang around for the session, between response and request. */
045: protected DataModel items = null;
046:
047: /**
048: * Access our item service.
049: *
050: * @return The injected item service.
051: */
052: public ItemService getItemService() {
053: return itemService;
054: }
055:
056: /**
057: * Set the item service - used to inject the item service.
058: *
059: * @param itemService
060: * the item service to use.
061: */
062: public void setItemService(ItemService itemService) {
063: M_log.info("setItemSevice: " + itemService);
064:
065: this .itemService = itemService;
066: }
067:
068: /**
069: * Construct.
070: */
071: public JsfSampleBean() {
072: M_log.info("constructed");
073: }
074:
075: /**
076: * An action method to process and select the navigation for a UI control (button, link).<br />
077: * Used for a button of the main page.
078: *
079: * @return The navigation outcome string.
080: */
081: public String getNextStep() {
082: M_log.info("getNextStep: returning 'list'");
083: return "list";
084: }
085:
086: /**
087: * An action method to process and select the navigation for a UI control (button, link).<br />
088: * Used for the tool bar in the main page.
089: *
090: * @return The navigation outcome string.
091: */
092: public String processToolBarItem() {
093: M_log.info("processToolBarItem: returning 'list'");
094: return "list";
095: }
096:
097: /**
098: * Get data items (entities), wrapped in UI controllers, for a JSF data table.
099: *
100: * @return The items for the table.
101: */
102: public DataModel getItems() {
103: // if we have not yet computed, do so
104: if (this .items == null) {
105: setItems(computeItems());
106: }
107:
108: return this .items;
109: }
110:
111: /**
112: * Set the data items.
113: *
114: * @param items
115: * Our data items.
116: */
117: public void setItems(DataModel items) {
118: this .items = items;
119: }
120:
121: /**
122: * Create the DataModel for the UI table component that has our UI controller objects for our item collection.
123: *
124: * @return The items for the table.
125: */
126: protected DataModel computeItems() {
127: M_log.info("computeItems");
128:
129: // collect information from the appropriate service
130: Collection items = getItemService().getItems();
131:
132: // wrap the entities in controllers
133: Collection wrappers = new Vector();
134: for (Iterator i = items.iterator(); i.hasNext();) {
135: wrappers.add(new ItemController((Item) i.next()));
136: }
137:
138: // wrap a JSF object around our controllers
139: DataModel rv = new ListDataModel();
140: rv.setWrappedData(wrappers);
141:
142: return rv;
143: }
144:
145: /**
146: * This is a UI controller class that wraps the item's properties and methods,<br />
147: * adding what we need to hook up with the UI components.
148: */
149: public class ItemController {
150: /** We store only a reference (id), not the entire item (which would be large and caching). */
151: protected String itemId = null;
152:
153: /** Set when this item was selected in the interface. */
154: boolean selected = false;
155:
156: /** Set when this item was clicked on from the interface. */
157: boolean clicked = false;
158:
159: public ItemController(Item item) {
160: this .itemId = item.getId();
161: this .selected = false;
162: }
163:
164: /**
165: * When we need an Item, we get it from the service. Thread-local caching in the service is appreciated.
166: *
167: * @return The item associated with our stored id.
168: */
169: protected Item getItem() {
170: return itemService.getItem(itemId);
171: }
172:
173: public boolean isClicked() {
174: return clicked;
175: }
176:
177: public void setClicked(boolean clicked) {
178: this .clicked = clicked;
179: }
180:
181: /**
182: * Action method on the item.
183: *
184: * @return The next navigation outcome value.
185: */
186: public String processClick() {
187: M_log.info("processClick: id: " + itemId);
188: setClicked(true);
189: return "itemClick";
190: }
191:
192: public String getA() {
193: return getItem().getA();
194: }
195:
196: public String getB() {
197: return getItem().getB();
198: }
199:
200: public String getC() {
201: return getItem().getC();
202: }
203:
204: /* public void setA(String a)
205: {
206: getItem().setA(a);
207: }
208:
209: public void setB(String b)
210: {
211: getItem().setB(b);
212: }
213:
214: public void setC(String c)
215: {
216: getItem().setC(c);
217: }
218: */
219:
220: public boolean isSelected() {
221: return selected;
222: }
223:
224: public void setSelected(boolean selected) {
225: M_log.info("setSelected: id: " + itemId + " selected: "
226: + selected);
227: this .selected = selected;
228: }
229:
230: public String getId() {
231: return itemId;
232: }
233:
234: /* public void setId(String id)
235: {
236: getItem().setId(id);
237: }
238: */
239: }
240: }
|