1 33 34 package com.icesoft.applications.faces.auctionMonitor.stubs; 35 36 import com.icesoft.applications.faces.auctionMonitor.AuctionState; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 import java.io.IOException ; 41 import java.net.MalformedURLException ; 42 import java.net.URL ; 43 import java.text.ParseException ; 44 import java.text.SimpleDateFormat ; 45 import java.util.ArrayList ; 46 import java.util.Calendar ; 47 import java.util.Date ; 48 import java.util.Iterator ; 49 import java.util.Properties ; 50 51 61 public class StubServer { 62 private ItemType[] itemList = null; 64 private static StubServer ourInstance = new StubServer(); 65 private static Log log = LogFactory.getLog(StubServer.class); 66 67 public static final String ITEM_PROPERTIES_RESOURCE = 69 "com/icesoft/applications/faces/auctionMonitor/stubs/auction.properties"; 70 private static final String ITEM = "item"; 71 private static final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss"; 72 private static final String ID = "id"; 73 private static final String END_TIME = "endTime"; 74 private static final String DESCRIPTION = "description"; 75 private static final String IMAGE = "image"; 76 private static final String BID_COUNT = "bidCount"; 77 private static final String INITIAL_BID_COUNT = "initialBidCount"; 78 private static final String CURRENCY = "currency"; 79 private static final String PRICE = "price"; 80 private static final String INITIAL_PRICE = "initialPrice"; 81 private static final String SITE = "site"; 82 private static final String TITLE = "title"; 83 private static final String LOCATION = "location"; 84 private static final String SELLER = "seller"; 85 private static final String EXPIRESINDAYS = "expiresindays"; 86 87 90 private StubServer() { 91 if (AuctionState.getAuctionMap().isEmpty()) { 92 loadItemList(); 93 } 94 } 95 96 public static synchronized StubServer getInstance() { 97 return ourInstance; 98 } 99 100 105 private void loadItemList() { 106 Properties props = new Properties (); 108 try { 109 ClassLoader cl = this.getClass().getClassLoader(); 110 props = new Properties (); 111 props.load(cl.getResourceAsStream(ITEM_PROPERTIES_RESOURCE)); 112 } catch (IOException e) { 113 if (log.isErrorEnabled()) { 114 log.error("Property file \'" + ITEM_PROPERTIES_RESOURCE + 115 "\' could not be found because of " + e); 116 } 117 } 118 119 int itemCounter = 0; 121 String itemPrefix, idValue, key; 122 while (true) { 123 itemPrefix = ITEM + itemCounter + "."; 124 idValue = props.getProperty(itemPrefix + ID); 125 key = idValue + "."; 126 127 if (idValue == null) { 129 break; 130 } 131 132 AuctionState.getAuctionMap().put(key + ID, idValue); 134 AuctionState.getAuctionMap().put(key + BID_COUNT, new Integer ( 135 props.getProperty(itemPrefix + BID_COUNT))); 136 AuctionState.getAuctionMap().put(key + INITIAL_BID_COUNT, 137 new Integer (props.getProperty( 138 itemPrefix + BID_COUNT))); 139 AuctionState.getAuctionMap().put(key + CURRENCY, props.getProperty( 140 itemPrefix + CURRENCY)); 141 AuctionState.getAuctionMap().put(key + DESCRIPTION, 142 props.getProperty( 143 itemPrefix + DESCRIPTION)); 144 AuctionState.getAuctionMap() 145 .put(key + IMAGE, props.getProperty(itemPrefix + IMAGE)); 146 AuctionState.getAuctionMap().put(key + LOCATION, props.getProperty( 147 itemPrefix + LOCATION)); 148 AuctionState.getAuctionMap().put(key + PRICE, new Double ( 149 props.getProperty(itemPrefix + PRICE))); 150 AuctionState.getAuctionMap().put(key + INITIAL_PRICE, new Double ( 151 props.getProperty(itemPrefix + PRICE))); 152 AuctionState.getAuctionMap() 153 .put(key + SITE, props.getProperty(itemPrefix + SITE)); 154 AuctionState.getAuctionMap() 155 .put(key + SELLER, props.getProperty(itemPrefix + SELLER)); 156 AuctionState.getAuctionMap() 157 .put(key + TITLE, props.getProperty(itemPrefix + TITLE)); 158 AuctionState.getAuctionMap().put(key + EXPIRESINDAYS, 159 props.getProperty(itemPrefix + 160 EXPIRESINDAYS)); 161 162 Calendar calendar = Calendar.getInstance(); 164 calendar.add(Calendar.DATE, Integer.parseInt((String ) AuctionState 165 .getAuctionMap().get(key + EXPIRESINDAYS))); 166 167 AuctionState.getAuctionMap().put(key + END_TIME, calendar); 168 169 itemCounter++; 171 } 172 } 173 174 181 public ItemType[] getSearchResults() { 182 if (itemList == null) { 184 ItemType it; 185 ArrayList items = new ArrayList (); 186 String bidId, prefix; 187 188 Iterator keys = AuctionState.getAuctionMap().keySet().iterator(); 190 while (keys.hasNext()) { 191 bidId = keys.next().toString(); 192 193 if (bidId.indexOf(".id") > 0) { 195 bidId = bidId.substring(0, bidId.indexOf(".id")); 197 prefix = bidId + "."; 198 199 it = new ItemType(); 201 202 it.setItemID(bidId); 204 205 it.setTitle(AuctionState.getAuctionMap() 207 .get(prefix + TITLE).toString()); 208 209 it.setEndTimeCal((Calendar ) AuctionState.getAuctionMap() 211 .get(prefix + END_TIME)); 212 213 it.setDescription(AuctionState.getAuctionMap() 215 .get(prefix + DESCRIPTION).toString()); 216 217 it.setLocation(AuctionState.getAuctionMap() 219 .get(prefix + LOCATION).toString()); 220 221 it.setSeller(AuctionState.getAuctionMap() 223 .get(prefix + SELLER).toString()); 224 225 try { 227 it.setPictureURL(new URL (AuctionState.getAuctionMap() 228 .get(prefix + IMAGE).toString())); 229 } catch (MalformedURLException e) { 230 if (log.isWarnEnabled()) { 231 log.warn("Malformed picture URL because of " + e); 232 } 233 } 234 235 items.add(it); 237 } 238 } 239 240 itemList = (ItemType[]) items.toArray(new ItemType[items.size()]); 242 } 243 244 return (itemList); 245 } 246 247 255 private Calendar getCalendar(String dateTimeValue) throws ParseException { 256 SimpleDateFormat parser = new SimpleDateFormat (DATE_FORMAT); 257 Calendar cal = Calendar.getInstance(); 258 Date parsedDate = parser.parse(dateTimeValue); 259 cal.setTime(parsedDate); 260 261 return cal; 262 } 263 264 270 public ItemType getItem(String itemID) { 271 ItemType[] items = getSearchResults(); 273 ItemType item; 274 275 for (int index = 0; index < items.length; index++) { 277 item = items[index]; 278 if (itemID.equals(item.getItemID())) { 279 return item; 280 } 281 } 282 283 return null; 284 } 285 286 290 public static void resetAuction() { 291 Calendar calendar; 292 String endDate; 293 SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 294 String bidId, prefix; 295 Iterator keys = AuctionState.getAuctionMap().keySet().iterator(); 296 297 while (keys.hasNext()) { 299 bidId = keys.next().toString(); 300 if (bidId.indexOf(".id") > 0) { 301 bidId = bidId.substring(0, bidId.indexOf(".id")); 302 prefix = bidId + "."; 303 304 AuctionState.getAuctionMap().put(prefix + PRICE, AuctionState 306 .getAuctionMap().get(prefix + INITIAL_PRICE)); 307 AuctionState.getAuctionMap().put(prefix + BID_COUNT, 308 AuctionState 309 .getAuctionMap().get( 310 prefix + 311 INITIAL_BID_COUNT)); 312 313 calendar = Calendar.getInstance(); 315 calendar.add(Calendar.DATE, Integer.parseInt(AuctionState 316 .getAuctionMap() 317 .get(prefix + EXPIRESINDAYS).toString())); 318 319 AuctionState.getAuctionMap().put(prefix + END_TIME, calendar); 321 } 322 } 323 } 324 } 325 | Popular Tags |