KickJava   Java API By Example, From Geeks To Geeks.

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


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.AuctionState;
38 import com.icesoft.applications.faces.auctionMonitor.stubs.ItemType;
39 import com.icesoft.faces.context.effects.Effect;
40 import com.icesoft.faces.context.effects.Highlight;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 import java.util.Calendar JavaDoc;
45
46 /**
47  * Class used to represent a single item within the auction This class handles
48  * such things as pricing, bidding, UI management, etc.
49  */

50 public class AuctionMonitorItemBean extends ItemType {
51     private static Log log = LogFactory.getLog(AuctionMonitorItemBean.class);
52     private int localBidCount;
53     private double localHighBid;
54     private boolean expanded;
55     private boolean bidExpanded;
56     private double oldBid;
57     private double tempLocalBid;
58     private boolean bidMessage;
59     private String JavaDoc imageUrl;
60     private long[] timeLeftBrokenDown;
61     private String JavaDoc timeLeftStyleClass;
62     private Effect effect;
63
64     private static final String JavaDoc NO_PHOTO_URL = "./images/noimage.gif";
65     private static final String JavaDoc TIME_LEFT_5 = "./images/time_left_5.gif";
66     private static final String JavaDoc TIME_LEFT_10 = "./images/time_left_10.gif";
67     private static final String JavaDoc TIME_LEFT_15 = "./images/time_left_15.gif";
68     private static final String JavaDoc TIME_LEFT_30 = "./images/time_left_30.gif";
69     private static final String JavaDoc TIME_LEFT_60 = "./images/time_left_45.gif";
70     private static final String JavaDoc TIME_LEFT_DAYS = "./images/time_left_days.gif";
71     private static final String JavaDoc TIME_LEFT_HOURS =
72             "./images/time_left_hours.gif";
73     private static final String JavaDoc TRIANGLE_OPEN = "./images/triangle_open.gif";
74     private static final String JavaDoc TRIANGLE_CLOSED = "./images/triangle_close.gif";
75     private static final String JavaDoc SUCCESS = "success";
76
77     private static final String JavaDoc STYLE_CLASS_EXPANDED_ROW = "rowClassHilite";
78
79     public static final int MAX_BID_INCREASE = 1000000;
80     public static final long MAX_BID = 1000000000;
81
82     private static final int TIME_DAYS = 24 * 60 * 60 * 1000;
83     private static final int TIME_HOURS = 60 * 60 * 1000;
84     private static final int TIME_MINUTES = 60 * 1000;
85
86 // private static final double NEW_BID_PRICE_INCREASE = 0.5;
87

88     public AuctionMonitorItemBean(ItemType item) {
89         super(item);
90         localHighBid = getCurrentPrice();
91
92         // Instead of a meaningless initial price of 0.0,
93
tempLocalBid = localHighBid;
94     }
95
96     public String JavaDoc getPicture() {
97         try {
98             imageUrl = getPictureURL().toString();
99             if (imageUrl.startsWith("file:")) {
100                 imageUrl = imageUrl.substring(5);
101             }
102         } catch (NullPointerException JavaDoc e) {
103             if (log.isWarnEnabled()) {
104                 log.warn("Failed to get the picture for an item because of " +
105                          e);
106             }
107         }
108
109         return (null == imageUrl ? NO_PHOTO_URL :
110                 imageUrl);
111     }
112
113     public Effect getEffect() {
114         return effect;
115     }
116
117     public void setEffect(Effect effect) {
118         this.effect = effect;
119     }
120
121     public void setCurrentPrice(double currentPrice) {
122     }
123
124     public double getCurrentPrice() {
125         double newBid = getLocalBid();
126
127         if ((oldBid > 0) && (newBid > oldBid)) {
128             if (effect == null) {
129                 effect = new Highlight("#FFCC0B");
130             }
131             effect.setFired(false);
132         }
133         oldBid = newBid;
134
135         return oldBid;
136     }
137
138     public void setBidCount(int bidCount) {
139     }
140
141     public int getBidCount() {
142         try {
143             localBidCount = Integer.parseInt(AuctionState.getAuctionMap()
144                     .get(getItemID() + ".bidCount").toString());
145         } catch (NullPointerException JavaDoc e) {
146             if (log.isWarnEnabled()) {
147                 log.warn("Failed to get the bid count for an item because of " +
148                          e);
149             }
150         }
151
152         return (localBidCount);
153     }
154
155     public void setTempLocalBid(double tempLocalBid) {
156         if (tempLocalBid <= MAX_BID &&
157             tempLocalBid - localHighBid <= MAX_BID_INCREASE) {
158             this.tempLocalBid = tempLocalBid;
159             setLocalBid();
160             bidMessage = false;
161         }
162         else{
163             bidMessage = true;
164         }
165     }
166
167     public double getTempLocalBid() {
168         return tempLocalBid;
169     }
170
171     public String JavaDoc setLocalBid() {
172         if (tempLocalBid > localHighBid &&
173             tempLocalBid <= MAX_BID &&
174             tempLocalBid - localHighBid <= MAX_BID_INCREASE) {
175             localHighBid = tempLocalBid;
176             bidMessage = false;
177             AuctionState.getAuctionMap().put(getItemID() + ".price",
178                                              new Double JavaDoc(localHighBid));
179             AuctionState.getAuctionMap().put(getItemID() + ".bidCount",
180                                              new Integer JavaDoc(Integer.parseInt(
181                                                      AuctionState
182                                                              .getAuctionMap()
183                                                              .get(getItemID() +
184                                                                   ".bidCount").toString()) +
185                                                                                            1));
186             AuctionState auctionState = AuctionState.getInstance();
187             getCurrentPrice();
188             if (null != auctionState) {
189                 auctionState.fireAuctionEvent(
190                         new AuctionEvent(getItemID(), localHighBid));
191             }
192
193         }
194         else if (tempLocalBid <= localHighBid){
195             bidMessage = false;
196         }
197         else {
198             bidMessage = true;
199         }
200         return SUCCESS;
201     }
202
203     public String JavaDoc getBidMessage(){
204         if (!bidMessage){
205             return "";
206         }
207         else{
208             return "<br />Bid declined.";
209         }
210     }
211
212     public double getLocalBid() {
213         try {
214             localHighBid =
215                     Double.parseDouble(
216                             AuctionState.getAuctionMap().get(getItemID() +
217                                                              ".price").toString());
218         } catch (NullPointerException JavaDoc e) {
219             if (log.isErrorEnabled()) {
220                 log.error("Error getting local bid:");
221             }
222         }
223
224         return localHighBid;
225     }
226
227     public void setTimeLeft(long timeLeft) {
228     }
229
230     public long getTimeLeft() {
231         Calendar JavaDoc endTimeCal = (Calendar JavaDoc) AuctionState.getAuctionMap()
232                 .get(getItemID() + ".endTime");
233         long endMillis = endTimeCal.getTime().getTime();
234         return (endMillis - Calendar.getInstance().getTime().getTime());
235     }
236
237     public long[] getTimeLeftBrokenDown() {
238         long left, days, hours, minutes, seconds;
239         left = getTimeLeft();
240         days = left / TIME_DAYS;
241         left = left - days * TIME_DAYS;
242         hours = left / TIME_HOURS;
243         left = left - hours * TIME_HOURS;
244         minutes = left / TIME_MINUTES;
245         left = left - minutes * TIME_MINUTES;
246         seconds = left / 1000;
247         return new long[]{days, hours, minutes, seconds};
248     }
249
250     public void setTimeLeftStyleClass(String JavaDoc timeLeftStyleClass) {
251     }
252
253     public String JavaDoc getTimeLeftStyleClass() {
254         return timeLeftStyleClass;
255     }
256
257     public void setTimeImageUrl(String JavaDoc timeImageUrl) {
258     }
259
260     public String JavaDoc getTimeImageUrl() {
261         timeLeftBrokenDown = getTimeLeftBrokenDown();
262         String JavaDoc timeImageUrl;
263         if (0 != timeLeftBrokenDown[0]) {
264             timeImageUrl = TIME_LEFT_DAYS;
265             timeLeftStyleClass = "timeCellDays";
266         } else if (0 != timeLeftBrokenDown[1]) {
267             timeImageUrl = TIME_LEFT_HOURS;
268             timeLeftStyleClass = "timeCellHours";
269         } else if (timeLeftBrokenDown[2] >= 30) {
270             timeImageUrl = TIME_LEFT_60;
271             timeLeftStyleClass = "timeCellMins";
272         } else if (timeLeftBrokenDown[2] >= 15) {
273             timeImageUrl = TIME_LEFT_30;
274             timeLeftStyleClass = "timeCellMins";
275         } else if (timeLeftBrokenDown[2] >= 10) {
276             timeImageUrl = TIME_LEFT_15;
277             timeLeftStyleClass = "timeCellMins";
278         } else if (timeLeftBrokenDown[2] >= 5) {
279             timeImageUrl = TIME_LEFT_10;
280             timeLeftStyleClass = "timeCellMins";
281         } else {
282             timeImageUrl = TIME_LEFT_5;
283             timeLeftStyleClass = "timeCellMins";
284         }
285
286         return timeImageUrl;
287     }
288
289     public void setTimeLeftString(String JavaDoc timeLeftString) {
290     }
291
292     public String JavaDoc getTimeLeftString() {
293         if (getTimeLeft() < 0) {
294             return " Expired";
295         }
296
297         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
298         buf.append(" ");
299         if (0 != timeLeftBrokenDown[0]) {
300             buf.append(Long.toString(timeLeftBrokenDown[0]));
301             buf.append("d ");
302         }
303
304         if (0 != timeLeftBrokenDown[1]) {
305             buf.append(Long.toString(timeLeftBrokenDown[1]));
306             buf.append(":");
307             if (timeLeftBrokenDown[2] < 10) {
308                 buf.append("0");
309             }
310         }
311
312         buf.append(Long.toString(timeLeftBrokenDown[2]));
313         buf.append(":");
314
315         if (timeLeftBrokenDown[3] < 10) {
316             buf.append("0");
317         }
318
319         buf.append(Long.toString(timeLeftBrokenDown[3]));
320
321         return buf.toString();
322     }
323
324     public void setExpandedStyleClass(String JavaDoc expandedStyleClass) {
325     }
326
327     public String JavaDoc getExpandedStyleClass() {
328         if (expanded) {
329             return STYLE_CLASS_EXPANDED_ROW;
330         } else {
331             return "";
332         }
333     }
334
335     public String JavaDoc pressExpandButton() {
336         expanded = !expanded;
337         return SUCCESS;
338     }
339
340     public boolean isExpanded() {
341         return expanded;
342     }
343
344     public void setExpanded(boolean expanded) {
345         this.expanded = expanded;
346     }
347
348     public void setExpandTriangleImage(String JavaDoc expandTriangleImage) {
349     }
350
351     public String JavaDoc getExpandTriangleImage() {
352         if (expanded) {
353             return TRIANGLE_OPEN;
354         } else {
355             return TRIANGLE_CLOSED;
356         }
357     }
358
359     public String JavaDoc pressBidButton() {
360         bidExpanded = !bidExpanded;
361         return SUCCESS;
362     }
363
364     public boolean isExpired() {
365         if (getTimeLeft() < 0) {
366             return (true);
367         }
368         return (false);
369     }
370
371     public boolean isBidExpanded() {
372         return bidExpanded;
373     }
374
375     public void setBidExpanded(boolean bidExpanded) {
376         this.bidExpanded = bidExpanded;
377     }
378 }
379
Popular Tags