KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > auctionMonitor > beans > AuctionBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.applications.faces.auctionMonitor.beans;
35
36 import com.icesoft.applications.faces.auctionMonitor.AuctionEvent;
37 import com.icesoft.applications.faces.auctionMonitor.AuctionListener;
38 import com.icesoft.applications.faces.auctionMonitor.AuctionMonitorItemDetailer;
39 import com.icesoft.applications.faces.auctionMonitor.AuctionState;
40 import com.icesoft.applications.faces.auctionMonitor.comparator.AuctionMonitorItemBidsComparator;
41 import com.icesoft.applications.faces.auctionMonitor.comparator.AuctionMonitorItemComparator;
42 import com.icesoft.applications.faces.auctionMonitor.comparator.AuctionMonitorItemPriceComparator;
43 import com.icesoft.applications.faces.auctionMonitor.comparator.AuctionMonitorItemTimeLeftComparator;
44 import com.icesoft.applications.faces.auctionMonitor.comparator.AuctionMonitorItemTitleComparator;
45 import com.icesoft.applications.faces.auctionMonitor.stubs.ItemType;
46 import com.icesoft.applications.faces.auctionMonitor.stubs.StubServer;
47 import com.icesoft.faces.async.render.OnDemandRenderer;
48 import com.icesoft.faces.async.render.RenderManager;
49 import com.icesoft.faces.async.render.Renderable;
50 import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
51 import com.icesoft.faces.webapp.xmlhttp.RenderingException;
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54
55 import java.util.Arrays JavaDoc;
56
57 /**
58  * Class used to handle searching and sorting of auction items, as well as front
59  * end interpretation
60  */

61 public class AuctionBean implements AuctionListener, Renderable {
62     private static Log log = LogFactory.getLog(AuctionBean.class);
63     private static int userCount = 0;
64     public static final String JavaDoc RENDERER_NAME = "demand";
65     private static final String JavaDoc SUCCESS = "success";
66     private String JavaDoc queryItemID;
67     private String JavaDoc queryString;
68     private AuctionMonitorItemBean[] searchItemBeans;
69     private AuctionMonitorItemComparator searchItemsComparator =
70             new AuctionMonitorItemTimeLeftComparator();
71     private String JavaDoc autoLoad = " ";
72     private PersistentFacesState persistentState = null;
73     private boolean isFreshSearch;
74     private AuctionMonitorItemBean queryItem;
75     private OnDemandRenderer renderer = null;
76
77     // style related constants
78

79     // title text
80
private static final String JavaDoc TABLE_HEADER_ASC_TITLE = "tableHeaderSelAsc1";
81     private static final String JavaDoc TABLE_HEADER_DESC_TITLE = "tableHeaderSelDesc1";
82     private static final String JavaDoc TABLE_HEADER_TITLE = "column1";
83
84     // bids text
85
private static final String JavaDoc TABLE_HEADER_ASC_BID = "tableHeaderSelAsc2";
86     private static final String JavaDoc TABLE_HEADER_DESC_BID = "tableHeaderSelDesc2";
87     private static final String JavaDoc TABLE_HEADER_BID = "column2";
88
89     // price text
90
private static final String JavaDoc TABLE_HEADER_ASC_PRICE = "tableHeaderSelAsc3";
91     private static final String JavaDoc TABLE_HEADER_DESC_PRICE = "tableHeaderSelDesc3";
92     private static final String JavaDoc TABLE_HEADER_PRICE = "column3";
93
94     // time left column
95
private static final String JavaDoc TABLE_HEADER_ASC_TIME = "tableHeaderSelAsc4";
96     private static final String JavaDoc TABLE_HEADER_DESC_TIME = "tableHeaderSelDesc4";
97     private static final String JavaDoc TABLE_HEADER_TIME = "column4";
98
99
100     public AuctionBean() {
101         AuctionState.getInstance().addAuctionListener(this);
102         persistentState = PersistentFacesState.getInstance();
103     }
104
105     public ItemType getItem(String JavaDoc itemIDStr) throws Exception JavaDoc {
106         return StubServer.getInstance().getItem(itemIDStr);
107     }
108
109     public ItemType[] getSearchResults(String JavaDoc filterString) throws Exception JavaDoc {
110         return StubServer.getInstance().getSearchResults();
111     }
112
113     public synchronized AuctionMonitorItemBean[] getSearchItems() {
114         if (null == queryString) {
115             return null;
116         }
117
118         if (!isFreshSearch) {
119             sortSearchItems();
120             return searchItemBeans;
121         }
122
123         try {
124             ItemType searchItems[] = getSearchResults(queryString);
125             if (null == searchItems) {
126                 return null;
127             }
128             searchItemBeans = new AuctionMonitorItemBean[searchItems.length];
129
130             for (int i = 0; i < searchItems.length; i++) {
131                 searchItemBeans[i] = new AuctionMonitorItemBean(searchItems[i]);
132             }
133             sortSearchItems();
134             isFreshSearch = false;
135             Thread JavaDoc t = new Thread JavaDoc(
136                     new AuctionMonitorItemDetailer(this, searchItemBeans));
137             t.start();
138             return searchItemBeans;
139         } catch (Exception JavaDoc e) {
140             if (log.isErrorEnabled()) {
141                 log.error(
142                         "Failed to read the available search items because of " +
143                         e);
144             }
145         }
146         return null;
147     }
148
149     public void reSearchItems() {
150         isFreshSearch = true;
151         getSearchItems();
152     }
153
154     public String JavaDoc getTitleTextStyle() {
155         if (searchItemsComparator instanceof AuctionMonitorItemTitleComparator)
156         {
157             if (searchItemsComparator.isAscending) {
158                 return TABLE_HEADER_ASC_TITLE;
159             } else {
160                 return TABLE_HEADER_DESC_TITLE;
161             }
162         } else {
163             return TABLE_HEADER_TITLE;
164         }
165     }
166
167     public String JavaDoc getBidsTextStyle() {
168         if (searchItemsComparator instanceof AuctionMonitorItemBidsComparator) {
169             if (searchItemsComparator.isAscending) {
170                 return TABLE_HEADER_ASC_BID;
171             } else {
172                 return TABLE_HEADER_DESC_BID;
173             }
174         } else {
175             return TABLE_HEADER_BID;
176         }
177     }
178
179     public String JavaDoc getPriceTextStyle() {
180         if (searchItemsComparator instanceof AuctionMonitorItemPriceComparator)
181         {
182             if (searchItemsComparator.isAscending) {
183                 return TABLE_HEADER_ASC_PRICE;
184             } else {
185                 return TABLE_HEADER_DESC_PRICE;
186             }
187         } else {
188             return TABLE_HEADER_PRICE;
189         }
190     }
191
192     public String JavaDoc getTimeLeftTextStyle() {
193         if (searchItemsComparator instanceof AuctionMonitorItemTimeLeftComparator)
194         {
195             if (searchItemsComparator.isAscending) {
196                 return TABLE_HEADER_ASC_TIME;
197             } else {
198                 return TABLE_HEADER_DESC_TIME;
199             }
200         } else {
201             return TABLE_HEADER_TIME;
202         }
203     }
204
205     public void setQueryItemID(String JavaDoc queryItemID) {
206         this.queryItemID = queryItemID;
207         try {
208             queryItem = new AuctionMonitorItemBean(getItem(queryItemID));
209         } catch (Exception JavaDoc e) {
210             if (log.isWarnEnabled()) {
211                 log.warn("Failed to retrieve a query item based on ID " +
212                          queryItemID + " because of " + e);
213             }
214         }
215     }
216
217     public String JavaDoc getQueryItemID() {
218         return queryItemID;
219     }
220
221     public void setQueryItem(AuctionMonitorItemBean queryItem) {
222         this.queryItem = queryItem;
223     }
224
225     public AuctionMonitorItemBean getQueryItem() {
226         return queryItem;
227     }
228
229     public void setQueryString(String JavaDoc queryString) {
230         if ("".equals(queryString)) {
231             return;
232         }
233
234         if (queryString.equals(this.queryString)) {
235             return;
236         }
237
238         this.queryString = queryString;
239         isFreshSearch = true;
240     }
241
242     public String JavaDoc getQueryString() {
243         return queryString;
244     }
245
246 /*
247     private Collection getListAsSelectItems(ArrayList list) {
248         if (list == null) {
249             return new ArrayList();
250         }
251         
252         ArrayList selectItems = new ArrayList( list.size() );
253         Integer val;
254         
255         for(int index = 0; index < list.size(); index++) {
256             val = (Integer)list.get(index);
257             int seconds = val.intValue()/1000;
258             selectItems.add(new SelectItem(val, Integer.toString(seconds), ""));
259         }
260         
261         return selectItems;
262     }
263 */

264
265     public String JavaDoc getAutoLoad() {
266         if (this.autoLoad.equals(" ")) {
267             this.autoLoad = "Loaded";
268             this.setQueryString("ice");
269         }
270
271         return this.autoLoad;
272     }
273
274     public String JavaDoc sortByTitle() {
275         if (searchItemsComparator instanceof AuctionMonitorItemTitleComparator)
276         {
277             searchItemsComparator.isAscending =
278                     !searchItemsComparator.isAscending;
279         } else {
280             searchItemsComparator = new AuctionMonitorItemTitleComparator();
281         }
282
283         sortSearchItems();
284         return SUCCESS;
285     }
286
287     public String JavaDoc sortByTimeLeft() {
288         if (searchItemsComparator instanceof AuctionMonitorItemTimeLeftComparator)
289         {
290             searchItemsComparator.isAscending =
291                     !searchItemsComparator.isAscending;
292         } else {
293             searchItemsComparator = new AuctionMonitorItemTimeLeftComparator();
294         }
295
296         sortSearchItems();
297         return SUCCESS;
298     }
299
300     public String JavaDoc sortByBids() {
301         if (searchItemsComparator instanceof AuctionMonitorItemBidsComparator) {
302             searchItemsComparator.isAscending =
303                     !searchItemsComparator.isAscending;
304         } else {
305             searchItemsComparator = new AuctionMonitorItemBidsComparator();
306         }
307
308         sortSearchItems();
309         return SUCCESS;
310     }
311
312     public String JavaDoc sortByPrice() {
313         if (searchItemsComparator instanceof AuctionMonitorItemPriceComparator)
314         {
315             searchItemsComparator.isAscending =
316                     !searchItemsComparator.isAscending;
317         } else {
318             searchItemsComparator = new AuctionMonitorItemPriceComparator();
319         }
320
321         sortSearchItems();
322         return SUCCESS;
323     }
324
325     private void sortSearchItems() {
326         if (searchItemBeans == null) {
327             return;
328         }
329
330         Arrays.sort(searchItemBeans, searchItemsComparator);
331     }
332
333     public void handleAuctionEvent(AuctionEvent auctionEvent) {
334         reRender();
335     }
336
337     public static synchronized void incrementUsers() {
338         userCount++;
339     }
340
341     public static synchronized void decrementUsers() {
342         userCount--;
343
344         if (userCount <= 0) {
345             userCount = 0;
346             StubServer.resetAuction();
347         }
348     }
349
350     public void reRender() {
351         if (renderer != null) {
352             renderer.requestRender();
353         } else {
354             if (log.isDebugEnabled()) {
355                 log.debug("OnDemandRenderer was not available (it was null)");
356             }
357         }
358     }
359
360     public void setRenderManager(RenderManager manager) {
361         if (manager != null) {
362             renderer = manager.getOnDemandRenderer(RENDERER_NAME);
363             renderer.add(this);
364         }
365     }
366
367     public PersistentFacesState getState() {
368         return persistentState;
369     }
370
371     public void renderingException(RenderingException renderingException) {
372         if (log.isWarnEnabled()) {
373             log.warn(
374                     "Rerender failed on while trying to handle an auction event due to " +
375                     renderingException);
376         }
377     }
378 }
379
Popular Tags