KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > ProductAdder


1 /*
2  * Created on Jun 7, 2006
3  */

4 package com.openedit.store;
5
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.lucene.document.Document;
15 import org.openedit.money.Money;
16
17 import com.openedit.WebPageRequest;
18 import com.openedit.modules.search.LuceneHitTracker;
19
20 public class ProductAdder
21 {
22     private static final Log log = LogFactory.getLog(ProductAdder.class);
23
24     protected static final String JavaDoc PRODUCTID = "productid";
25
26
27     public void addItem(WebPageRequest inReq, Cart inCart) throws StoreException
28     {
29         // Get item info from request parameters
30
int productCount = computeProductCount(inReq);
31         Map JavaDoc params = inReq.getParameterMap();
32         for (int i = 0; i < productCount + 2; i++)
33         {
34             String JavaDoc counter = null;
35             if( i == 0)
36             {
37                 counter ="";
38             }
39             else
40             {
41                 counter = "." + i;
42             }
43             String JavaDoc productId = (String JavaDoc)params.get("productid" + counter);
44             if( productId == null)
45             {
46                 continue;
47             }
48             String JavaDoc checked = inReq.getRequestParameter("remove" +counter);
49             if( checked != null)
50             {
51                 continue;
52             }
53             Product product = inCart.getStore().getProduct(productId);
54             if( product == null)
55             {
56                 //log
57
continue;
58             }
59             if( checked != null)
60             {
61                 inCart.removeProduct(product);
62             }
63             String JavaDoc quantityStr = (String JavaDoc)params.get("quantity" + counter);
64             int quantity = 1;
65             // Quantity
66
if (quantityStr != null && !quantityStr.equals("quantity"))
67             {
68                 quantity = Integer.parseInt(quantityStr);
69             }
70             //Look for any options being passed to us. Option can be a size, color, count, hosting
71
Set JavaDoc options = readOptions(counter, params, product);
72             //legacy way
73
String JavaDoc size = (String JavaDoc)params.get("size" + counter);
74             if( size != null)
75             {
76                 Option option = makeOption(product, "size", size);
77                 options.add(option);
78             }
79             String JavaDoc color = (String JavaDoc)params.get("color" + counter);
80             if( color != null && color.length() > 0)
81             {
82                 Option option = makeOption(product, "color", color);
83                 options.add(option);
84             }
85             CartItem cartItem = null;
86             if( product.getInventoryItemCount() == 0)
87             {
88                 cartItem = new CartItem();
89                 cartItem.setInventoryItem(new InventoryItem());
90                 cartItem.getInventoryItem().setProduct(product);
91             }
92             else
93             {
94                 InventoryItem inventory = product.getInventoryItemByOptions(options);
95                 if( inventory == null)
96                 {
97                     inventory = product.getCloseInventoryItemByOptions(options);
98                 }
99                 if( inventory == null)
100                 {
101                     throw new StoreException("No such inventory");
102                 }
103                 cartItem = inCart.findCartItemWith(inventory);
104                 if( cartItem == null )
105                 {
106                     cartItem = new CartItem();
107                     inCart.addItem(cartItem);
108                 }
109                 cartItem.setInventoryItem(inventory);
110             }
111
112             cartItem.setQuantity(quantity);
113             cartItem.setOptions(options);
114             
115             //Adds any other required options
116
List JavaDoc all = cartItem.getInventoryItem().getAllOptions();
117             for (Iterator JavaDoc iter = all.iterator(); iter.hasNext();)
118             {
119                 Option remaining= (Option) iter.next();
120                 if ( remaining.isRequired())
121                 {
122                     if( !cartItem.hasOption(remaining.getId()))
123                     {
124                         cartItem.addOption(remaining);
125                     }
126                 }
127             }
128             //List inOptions, int inQuantity
129

130             // If product supports custom price, then get price from user
131
if (product.isCustomPrice())
132             {
133                 String JavaDoc priceStr = (String JavaDoc)params.get("price" + counter);
134                 if (priceStr != null)
135                 {
136                     priceStr = priceStr.trim();
137                     cartItem.setYourPrice(new Money(priceStr));
138                 }
139             }
140         }
141     }
142
143     protected Set JavaDoc readOptions(String JavaDoc code, Map JavaDoc params, Product product)
144     {
145         Set JavaDoc options = new HashSet JavaDoc();
146         for (Iterator JavaDoc iter = params.keySet().iterator(); iter.hasNext();)
147         {
148             String JavaDoc paramid = (String JavaDoc)iter.next();
149             String JavaDoc start = "option"+ code + ".";
150             if( paramid.startsWith(start))
151             {
152                 String JavaDoc oid = paramid.substring(start.length());
153                 String JavaDoc value = (String JavaDoc)params.get(paramid);
154                 if( value != null && value.trim().length() == 0)
155                 {
156                     value = null;
157                 }
158                 Option option = makeOption(product, oid, value);
159                 if (option.isRequired() || value != null)
160                 {
161                     options.add(option);
162                 }
163             }
164         }
165         return options;
166     }
167
168     private Option makeOption(Product product, String JavaDoc oid, String JavaDoc value)
169     {
170         Option option = new Option();
171         option.setId(oid);
172         option.setName(oid);
173         option.setValue(value);
174         Option realoption = product.getOption(oid);
175         if( realoption != null)
176         {
177             option.setName(realoption.getName());
178             option.setPriceSupport(realoption.getPriceSupport());
179             option.setRequired(realoption.isRequired());
180         }
181         return option;
182     }
183
184     public boolean isMultiProductMode(WebPageRequest inReq) throws Exception JavaDoc
185     {
186         String JavaDoc productId = inReq.getRequestParameter(PRODUCTID);
187         if (productId != null)
188         {
189             return false;
190         }
191         else
192         {
193             productId = inReq.getRequestParameter("productid.1");
194             if (productId != null)
195             {
196                 return true;
197             }
198             else
199             {
200                 throw new Exception JavaDoc("Product ID not found.");
201             }
202         }
203     }
204
205     public int computeProductCount(WebPageRequest inReq) throws StoreException
206     {
207         // One product
208
String JavaDoc productId = inReq.getRequestParameter(PRODUCTID);
209         if (productId != null)
210         {
211             return 1;
212         }
213         else
214         {
215                 // Multiple products
216
int count = 0;
217                 String JavaDoc productId1 = inReq.getRequestParameter("productid." + (count + 1));
218                 while (productId1 != null)
219                 {
220                     count++;
221                     productId1 = inReq.getRequestParameter("productid." + (count + 1));
222                 }
223                 return count;
224             }
225         }
226     
227
228     /**
229      * This should return null if no match
230      * @param inSize
231      * @param inColor
232      * @return
233      */

234     public void setInventoryItem(CartItem inItem, Product inProduct) throws StoreException
235     {
236         if ( inProduct.getInventoryItemCount() == 0)
237         {
238             //This item is very simple
239
CartItem item = new CartItem();
240             InventoryItem inven = new InventoryItem();
241             inven.setSku("noinventory");
242             inven.setProduct(inProduct);
243             item.setInventoryItem(inven);
244             return;
245         }
246
247 // CartItem cartItem = new CartItem();
248
// cartItem.setQuantity(inQuantity);
249

250         
251         //try for exact match of what they ordered
252
for (Iterator JavaDoc iter = inProduct.getInventoryItems().iterator(); iter.hasNext();)
253         {
254             InventoryItem inventoryItem = (InventoryItem) iter.next();
255             if ( inventoryItem.isExactMatch( inItem.getOptions() ) )
256             {
257                 inItem.setInventoryItem(inventoryItem);
258                 return;
259             }
260         }
261         //try just the same size
262
for (Iterator JavaDoc iter = inProduct.getInventoryItems().iterator(); iter.hasNext();)
263         {
264             InventoryItem inventoryItem = (InventoryItem) iter.next();
265             if ( inventoryItem.isCloseMatch( inItem.getOptions() ) )
266             {
267                 inItem.setInventoryItem(inventoryItem);
268                 return;
269             }
270         }
271
272         //take anything on the list. this should not happen
273
//log.error("Not hits on item");
274
InventoryItem item = (InventoryItem)inProduct.getInventoryItems().get(0);
275         inItem.setInventoryItem(item);
276         return ;
277     }
278
279     public void updateCart( WebPageRequest inReq,Cart inCart) throws StoreException
280     {
281         String JavaDoc reload = inReq.getRequestParameter("reloadcart");
282         if (Boolean.parseBoolean(reload))
283         {
284             inCart.removeAllItems();
285         }
286         
287         addItem(inReq, inCart);
288 // List cartItems = inCart.getItems();
289
// List cartItemsToDelete = new ArrayList();
290
//
291
// for (int i = 0; i < cartItems.size(); i++)
292
// {
293
// CartItem cartItem = (CartItem) cartItems.get(i);
294
// int rowNum = i + 1;
295
// String counter = "." + rowNum;
296
// // Check for remove flag
297
// String checked = inReq.getRequestParameter("remove" +counter);
298
// if (checked != null)
299
// {
300
// cartItemsToDelete.add(cartItem);
301
// }
302
// else
303
// {
304
// // Get quantity
305
// int quantity = cartItem.getQuantity();
306
// String quantityStr = inReq.getRequestParameter("quantity"+counter);
307
// if (quantityStr != null)
308
// {
309
// quantity = Integer.parseInt(quantityStr);
310
// }
311
// cartItem.setQuantity(quantity);
312
// if( quantity == 0)
313
// {
314
// cartItemsToDelete.add(cartItem);
315
// continue;
316
// }
317
// //Cant add options but you can set the value of certain options
318
//
319
// Set options = readOptions(counter, inReq.getParameterMap(), cartItem.getProduct());
320
// /*for (Iterator iter = cartItem.getOptions().iterator(); iter.hasNext();)
321
// {
322
// Option option = (Option) iter.next();
323
// String val = inReq.getRequestParameter( option.getId() + counter);
324
// if( val != null )
325
// {
326
// option.setValue(val); //this updates size and color
327
// }
328
// }*/
329
// InventoryItem newItem = cartItem.getProduct().getInventoryItemByOptions(options);
330
// if ( newItem != null)
331
// {
332
// cartItem.setInventoryItem(newItem);
333
// //throw new StoreException("No product of that type found " + cartItem.getSize() + " " + cartItem.getColor() );
334
// }
335
//
336
// }
337
// }
338
//
339
// // Do the deletes
340
// for (Iterator iter = cartItemsToDelete.iterator(); iter.hasNext();)
341
// {
342
// CartItem deleteCartItem = (CartItem) iter.next();
343
// inCart.removeItem( deleteCartItem );
344
// }
345
}
346     
347
348     public void addCoupon(WebPageRequest inReq, Cart inCart) throws Exception JavaDoc
349     {
350         String JavaDoc coupon = inReq.getRequestParameter("couponcode");
351         if ( coupon != null && coupon.length() > 0)
352         {
353             //make sure there is no negative product in there already then add the product ID in there
354
//we might have to look up the product ID up since W@W needs that
355
//use Lucene to search
356
Store store = inCart.getStore();
357             LuceneHitTracker hits = store.search("items:(" + coupon + ")");
358             if( hits.getTotal() > 0)
359             {
360                 Document hit = (Document)hits.get(0);
361                 String JavaDoc productId = hit.get("id");
362                 Product product = store.getProduct(productId);
363                 if ( product != null)
364                 {
365                     //Check the rules. This might have a dollar amount in it
366
String JavaDoc min = product.get( "fldDesc3" );
367                     if ( min != null)
368                     {
369                         //do a check
370
double val = inCart.getSubTotal().doubleValue();
371                         Money minmoney = new Money( min );
372                         if ( val < minmoney.doubleValue() )
373                         {
374                             inReq.putPageValue("errorMessage","That coupon can only be used on orders of " + minmoney.toString() + " or more");
375                             return;
376                         }
377                     }
378                     
379                     InventoryItem inventoryItem = product.getInventoryItemBySku(coupon);
380                     if ( inventoryItem != null)
381                     {
382                         CartItem cartItem = new CartItem();
383                         cartItem.setInventoryItem(inventoryItem);
384                         //remove any other coupons
385
for (Iterator JavaDoc iter = inCart.getItems().iterator(); iter.hasNext();)
386                         {
387                             CartItem olditem = (CartItem) iter.next();
388                             if ( olditem.getYourPrice().isNegative() )
389                             {
390                                 inCart.removeItem(olditem);
391                                 break;
392                             }
393                         }
394                         
395                         //make sure the cart is 2X the coupon value
396
if ( cartItem.getYourPrice().doubleValue()*2 < inCart.getSubTotal().doubleValue() )
397                         {
398                             inCart.addItem(cartItem);
399                         }
400                         else
401                         {
402                             inReq.putPageValue("errorMessage","Coupons may not be more than 50% off the total price");
403                         }
404                     }
405                 }
406             }
407             else
408             {
409                 //no such coupon code
410
inReq.putPageValue("errorMessage","No such product");
411             }
412             //loop over the cart and make sure there is only one negative price in it
413

414         }
415     }
416 }
417
Popular Tags