KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > auctionMonitor > stubs > StubServer


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.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 JavaDoc;
41 import java.net.MalformedURLException JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.text.ParseException JavaDoc;
44 import java.text.SimpleDateFormat JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Calendar JavaDoc;
47 import java.util.Date JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Properties JavaDoc;
50
51 /**
52  * The original concept of the Auction Monitor demo relied on the ebay SDK to
53  * retrieve "live" auction data. From the standpoint of a demonstration app,
54  * the requirement to have all the proper ebay jars as well as a valid
55  * development token became a problem. Instead, we've stubbed out the minimum
56  * required set of classes and methods and we fake the API calls by simply
57  * reading in a property file and populating the required data classes. In this
58  * way, we make it easier to switch over to the real ebay SDK libraries as
59  * required
60  */

61 public class StubServer {
62     // Variables
63
private ItemType[] itemList = null;
64     private static StubServer ourInstance = new StubServer();
65     private static Log log = LogFactory.getLog(StubServer.class);
66
67     // Property variables
68
public static final String JavaDoc ITEM_PROPERTIES_RESOURCE =
69             "com/icesoft/applications/faces/auctionMonitor/stubs/auction.properties";
70     private static final String JavaDoc ITEM = "item";
71     private static final String JavaDoc DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
72     private static final String JavaDoc ID = "id";
73     private static final String JavaDoc END_TIME = "endTime";
74     private static final String JavaDoc DESCRIPTION = "description";
75     private static final String JavaDoc IMAGE = "image";
76     private static final String JavaDoc BID_COUNT = "bidCount";
77     private static final String JavaDoc INITIAL_BID_COUNT = "initialBidCount";
78     private static final String JavaDoc CURRENCY = "currency";
79     private static final String JavaDoc PRICE = "price";
80     private static final String JavaDoc INITIAL_PRICE = "initialPrice";
81     private static final String JavaDoc SITE = "site";
82     private static final String JavaDoc TITLE = "title";
83     private static final String JavaDoc LOCATION = "location";
84     private static final String JavaDoc SELLER = "seller";
85     private static final String JavaDoc EXPIRESINDAYS = "expiresindays";
86
87     /**
88      * Private constructor to fufill singleton requirements
89      */

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     /**
101      * Method used to find and read an item properties file The item values will
102      * then be loaded into the global auction state In the getSearchResults
103      * method, these values will be converted into a list of ItemTypes
104      */

105     private void loadItemList() {
106         // Determine the location of the property file
107
Properties JavaDoc props = new Properties JavaDoc();
108         try {
109             ClassLoader JavaDoc cl = this.getClass().getClassLoader();
110             props = new Properties JavaDoc();
111             props.load(cl.getResourceAsStream(ITEM_PROPERTIES_RESOURCE));
112         } catch (IOException JavaDoc 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         // Loop through each property and put the values in the global auction state
120
int itemCounter = 0;
121         String JavaDoc itemPrefix, idValue, key;
122         while (true) {
123             itemPrefix = ITEM + itemCounter + ".";
124             idValue = props.getProperty(itemPrefix + ID);
125             key = idValue + ".";
126
127             // No more item sets were found, so break the loop
128
if (idValue == null) {
129                 break;
130             }
131
132             // Add the core values for the current item
133
AuctionState.getAuctionMap().put(key + ID, idValue);
134             AuctionState.getAuctionMap().put(key + BID_COUNT, new Integer JavaDoc(
135                     props.getProperty(itemPrefix + BID_COUNT)));
136             AuctionState.getAuctionMap().put(key + INITIAL_BID_COUNT,
137                                              new Integer JavaDoc(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 JavaDoc(
149                     props.getProperty(itemPrefix + PRICE)));
150             AuctionState.getAuctionMap().put(key + INITIAL_PRICE, new Double JavaDoc(
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             // Calculate and add the expiry date
163
Calendar JavaDoc calendar = Calendar.getInstance();
164             calendar.add(Calendar.DATE, Integer.parseInt((String JavaDoc) AuctionState
165                     .getAuctionMap().get(key + EXPIRESINDAYS)));
166
167             AuctionState.getAuctionMap().put(key + END_TIME, calendar);
168
169             // Update the running total of number of items
170
itemCounter++;
171         }
172     }
173
174     /**
175      * Method to convert the global properties file values into ItemTypes
176      * Ideally this method would use the Ebay SDK, but for now will "fake it"
177      * with the property file
178      *
179      * @return ItemType[] resulting list of auction items
180      */

181     public ItemType[] getSearchResults() {
182         // Ensure that the item list is only read once
183
if (itemList == null) {
184             ItemType it;
185             ArrayList JavaDoc items = new ArrayList JavaDoc();
186             String JavaDoc bidId, prefix;
187
188             // Loop until no new properties remain
189
Iterator JavaDoc keys = AuctionState.getAuctionMap().keySet().iterator();
190             while (keys.hasNext()) {
191                 bidId = keys.next().toString();
192
193                 // Ensure a valid ID is present before using this property
194
if (bidId.indexOf(".id") > 0) {
195                     // Break down the bid ID into a useable value
196
bidId = bidId.substring(0, bidId.indexOf(".id"));
197                     prefix = bidId + ".";
198
199                     // Create a new ItemType and start to populate it with the required values
200
it = new ItemType();
201
202                     // Set the ID
203
it.setItemID(bidId);
204
205                     // Set the title
206
it.setTitle(AuctionState.getAuctionMap()
207                             .get(prefix + TITLE).toString());
208
209                     // Set the end time
210
it.setEndTimeCal((Calendar JavaDoc) AuctionState.getAuctionMap()
211                             .get(prefix + END_TIME));
212
213                     // Set the description
214
it.setDescription(AuctionState.getAuctionMap()
215                             .get(prefix + DESCRIPTION).toString());
216
217                     // Set the location
218
it.setLocation(AuctionState.getAuctionMap()
219                             .get(prefix + LOCATION).toString());
220
221                     // Set the seller
222
it.setSeller(AuctionState.getAuctionMap()
223                             .get(prefix + SELLER).toString());
224
225                     // Set the picture
226
try {
227                         it.setPictureURL(new URL JavaDoc(AuctionState.getAuctionMap()
228                                 .get(prefix + IMAGE).toString()));
229                     } catch (MalformedURLException JavaDoc e) {
230                         if (log.isWarnEnabled()) {
231                             log.warn("Malformed picture URL because of " + e);
232                         }
233                     }
234
235                     // Add the populated item to the available list
236
items.add(it);
237                 }
238             }
239
240             // Convert the results to a simple array and store them
241
itemList = (ItemType[]) items.toArray(new ItemType[items.size()]);
242         }
243
244         return (itemList);
245     }
246
247     /**
248      * Convience method to get a formatted calendar based on the passed date
249      * time string
250      *
251      * @param dateTimeValue date time to use as a base
252      * @return Calendar based on passed String
253      * @throws ParseException on invalid date format
254      */

255     private Calendar JavaDoc getCalendar(String JavaDoc dateTimeValue) throws ParseException JavaDoc {
256         SimpleDateFormat JavaDoc parser = new SimpleDateFormat JavaDoc(DATE_FORMAT);
257         Calendar JavaDoc cal = Calendar.getInstance();
258         Date JavaDoc parsedDate = parser.parse(dateTimeValue);
259         cal.setTime(parsedDate);
260
261         return cal;
262     }
263
264     /**
265      * Method to retrieve a single ItemType with a matching itemID
266      *
267      * @param itemID itemID to match
268      * @return ItemType matching item, or null if not found
269      */

270     public ItemType getItem(String JavaDoc itemID) {
271         // Get the whole list of items
272
ItemType[] items = getSearchResults();
273         ItemType item;
274
275         // Loop through the list of items looking for a matching ID
276
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     /**
287      * Method to reset the auction state This will perform 4 actions on each
288      * item: reset price, reset bid, reset expiry days, reset end time
289      */

290     public static void resetAuction() {
291         Calendar JavaDoc calendar;
292         String JavaDoc endDate;
293         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
294         String JavaDoc bidId, prefix;
295         Iterator JavaDoc keys = AuctionState.getAuctionMap().keySet().iterator();
296
297         // Loop through the global list of available IDs
298
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                 // Reset the price and bid count of the current item
305
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                 // Reset the number of expiry days
314
calendar = Calendar.getInstance();
315                 calendar.add(Calendar.DATE, Integer.parseInt(AuctionState
316                         .getAuctionMap()
317                         .get(prefix + EXPIRESINDAYS).toString()));
318
319                 // Reset the end time
320
AuctionState.getAuctionMap().put(prefix + END_TIME, calendar);
321             }
322         }
323     }
324 }
325
Popular Tags