1 4 package com.openedit.store; 5 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Map ; 10 import java.util.Set ; 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 PRODUCTID = "productid"; 25 26 27 public void addItem(WebPageRequest inReq, Cart inCart) throws StoreException 28 { 29 int productCount = computeProductCount(inReq); 31 Map params = inReq.getParameterMap(); 32 for (int i = 0; i < productCount + 2; i++) 33 { 34 String counter = null; 35 if( i == 0) 36 { 37 counter =""; 38 } 39 else 40 { 41 counter = "." + i; 42 } 43 String productId = (String )params.get("productid" + counter); 44 if( productId == null) 45 { 46 continue; 47 } 48 String 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 continue; 58 } 59 if( checked != null) 60 { 61 inCart.removeProduct(product); 62 } 63 String quantityStr = (String )params.get("quantity" + counter); 64 int quantity = 1; 65 if (quantityStr != null && !quantityStr.equals("quantity")) 67 { 68 quantity = Integer.parseInt(quantityStr); 69 } 70 Set options = readOptions(counter, params, product); 72 String size = (String )params.get("size" + counter); 74 if( size != null) 75 { 76 Option option = makeOption(product, "size", size); 77 options.add(option); 78 } 79 String color = (String )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 List all = cartItem.getInventoryItem().getAllOptions(); 117 for (Iterator 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 130 if (product.isCustomPrice()) 132 { 133 String priceStr = (String )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 readOptions(String code, Map params, Product product) 144 { 145 Set options = new HashSet (); 146 for (Iterator iter = params.keySet().iterator(); iter.hasNext();) 147 { 148 String paramid = (String )iter.next(); 149 String start = "option"+ code + "."; 150 if( paramid.startsWith(start)) 151 { 152 String oid = paramid.substring(start.length()); 153 String value = (String )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 oid, String 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 185 { 186 String 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 ("Product ID not found."); 201 } 202 } 203 } 204 205 public int computeProductCount(WebPageRequest inReq) throws StoreException 206 { 207 String productId = inReq.getRequestParameter(PRODUCTID); 209 if (productId != null) 210 { 211 return 1; 212 } 213 else 214 { 215 int count = 0; 217 String 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 234 public void setInventoryItem(CartItem inItem, Product inProduct) throws StoreException 235 { 236 if ( inProduct.getInventoryItemCount() == 0) 237 { 238 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 250 251 for (Iterator 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 for (Iterator 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 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 reload = inReq.getRequestParameter("reloadcart"); 282 if (Boolean.parseBoolean(reload)) 283 { 284 inCart.removeAllItems(); 285 } 286 287 addItem(inReq, inCart); 288 } 346 347 348 public void addCoupon(WebPageRequest inReq, Cart inCart) throws Exception 349 { 350 String coupon = inReq.getRequestParameter("couponcode"); 351 if ( coupon != null && coupon.length() > 0) 352 { 353 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 productId = hit.get("id"); 362 Product product = store.getProduct(productId); 363 if ( product != null) 364 { 365 String min = product.get( "fldDesc3" ); 367 if ( min != null) 368 { 369 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 for (Iterator 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 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 inReq.putPageValue("errorMessage","No such product"); 411 } 412 414 } 415 } 416 } 417 | Popular Tags |