| 1 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 ; 45 46 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 imageUrl; 60 private long[] timeLeftBrokenDown; 61 private String timeLeftStyleClass; 62 private Effect effect; 63 64 private static final String NO_PHOTO_URL = "./images/noimage.gif"; 65 private static final String TIME_LEFT_5 = "./images/time_left_5.gif"; 66 private static final String TIME_LEFT_10 = "./images/time_left_10.gif"; 67 private static final String TIME_LEFT_15 = "./images/time_left_15.gif"; 68 private static final String TIME_LEFT_30 = "./images/time_left_30.gif"; 69 private static final String TIME_LEFT_60 = "./images/time_left_45.gif"; 70 private static final String TIME_LEFT_DAYS = "./images/time_left_days.gif"; 71 private static final String TIME_LEFT_HOURS = 72 "./images/time_left_hours.gif"; 73 private static final String TRIANGLE_OPEN = "./images/triangle_open.gif"; 74 private static final String TRIANGLE_CLOSED = "./images/triangle_close.gif"; 75 private static final String SUCCESS = "success"; 76 77 private static final String 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 88 public AuctionMonitorItemBean(ItemType item) { 89 super(item); 90 localHighBid = getCurrentPrice(); 91 92 tempLocalBid = localHighBid; 94 } 95 96 public String getPicture() { 97 try { 98 imageUrl = getPictureURL().toString(); 99 if (imageUrl.startsWith("file:")) { 100 imageUrl = imageUrl.substring(5); 101 } 102 } catch (NullPointerException 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 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 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 (localHighBid)); 179 AuctionState.getAuctionMap().put(getItemID() + ".bidCount", 180 new Integer (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 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 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 endTimeCal = (Calendar ) 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 timeLeftStyleClass) { 251 } 252 253 public String getTimeLeftStyleClass() { 254 return timeLeftStyleClass; 255 } 256 257 public void setTimeImageUrl(String timeImageUrl) { 258 } 259 260 public String getTimeImageUrl() { 261 timeLeftBrokenDown = getTimeLeftBrokenDown(); 262 String 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 timeLeftString) { 290 } 291 292 public String getTimeLeftString() { 293 if (getTimeLeft() < 0) { 294 return " Expired"; 295 } 296 297 StringBuffer buf = new StringBuffer (); 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 expandedStyleClass) { 325 } 326 327 public String getExpandedStyleClass() { 328 if (expanded) { 329 return STYLE_CLASS_EXPANDED_ROW; 330 } else { 331 return ""; 332 } 333 } 334 335 public String 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 expandTriangleImage) { 349 } 350 351 public String getExpandTriangleImage() { 352 if (expanded) { 353 return TRIANGLE_OPEN; 354 } else { 355 return TRIANGLE_CLOSED; 356 } 357 } 358 359 public String 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 |