1 27 33 package olstore.session; 34 35 import java.util.ArrayList; 36 import java.util.Collection; 37 import java.util.Date; 38 import java.util.HashMap; 39 import java.util.Iterator; 40 import java.util.Set; 41 import java.math.BigDecimal; 42 43 import javax.ejb.SessionBean; 44 45 import olstore.dto.ShoppingCartItem; 46 import olstore.framework.EJBHomeFactory; 47 48 import olstore.entity.ItemLocal; 49 import olstore.entity.ItemLocalHome; 50 import olstore.entity.OrderLocalHome; 51 import olstore.entity.UserLocal; 52 53 59 65 67 68 101 102 public abstract class ShoppingCartBean implements SessionBean { 103 private UserLocal customer; 104 private HashMap cart; 105 private javax.ejb.SessionContext ejbContext; 106 private javax.naming.Context initialContext; 107 108 109 114 115 public void ejbCreate(olstore.entity.UserLocal user){ 116 customer = user; 117 cart = new HashMap(); 118 } 119 120 127 128 public olstore.entity.UserLocal getUser(){ 129 return customer; 130 } 131 132 141 private void addItemToCart(Integer itemPK, Integer quantity){ 142 ItemLocal item = null; 143 Integer qty; 144 145 if(quantity.intValue() <= 0){ 146 147 throw new NumberFormatException("Quantity must be greater than 0:"); 148 } 149 try{ 150 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 151 ItemLocalHome home = (ItemLocalHome) factory.getLocalHome(EJBHomeFactory.ITEM); 152 item = home.findByPrimaryKey(itemPK); 153 if((qty = (Integer)cart.get(item))!=null){ 154 cart.remove(item); 155 } 156 cart.put(item, quantity); 157 158 }catch(Exception e){ 159 throw new javax.ejb.EJBException("Could not add item " 160 + item , e); 161 } 162 } 163 164 private BigDecimal recalculateTotal(){ 165 Iterator it = cart.keySet().iterator(); 166 BigDecimal totalCost = new BigDecimal(0.00f); 167 168 while(it.hasNext()) 169 totalCost = totalCost.add(getCostForItem(((ItemLocal) it.next()).getItemId())); 170 return totalCost; 171 } 172 173 181 182 public Integer getQuantity(Integer itemPK){ 183 ItemLocal item = null; 184 185 try{ 186 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 187 ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM); 188 item = home.findByPrimaryKey(itemPK); 189 return ((Integer) cart.get(item)); 190 }catch(Exception e){ 191 throw new javax.ejb.EJBException("Could not get the quantity for item" 192 + item , e); 193 } 194 195 } 196 197 205 206 private void removeItemFromCart(Integer itemPK, Integer quantity){ 207 ItemLocal item = null; 208 try{ 209 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 210 ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM); 211 item = home.findByPrimaryKey(itemPK); 212 cart.remove(item); 213 cart.put(item, quantity); 214 }catch(Exception e){ 215 throw new javax.ejb.EJBException("Could not remove item " 216 + item , e); 217 } 218 219 } 220 221 229 230 public void removeItemFromCart(Integer itemPK){ 231 ItemLocal item = null; 232 try{ 233 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 234 ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM); 235 item = home.findByPrimaryKey(itemPK); 236 cart.remove(item); 237 }catch(Exception e){ 238 throw new javax.ejb.EJBException("Could not remove item " 239 + item , e); 240 241 } 242 } 243 244 253 254 public void updateQuantity(Integer itemPK, Integer quantity){ 255 ItemLocal item = null; 256 if(quantity.intValue() <= 0){ 257 258 throw new NumberFormatException("Quantity must be greater than 0:"); 259 } 260 261 try{ 262 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 263 ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM); 264 item = home.findByPrimaryKey(itemPK); 265 int orig = 0; Integer origInt; 266 if((origInt = ((Integer) (cart.get(item))))!=null) 267 orig = ((Integer) (cart.get(item))).intValue(); 268 if(orig < quantity.intValue()) 269 addItemToCart(itemPK, quantity); 270 else 271 removeItemFromCart(itemPK, quantity); 272 273 274 }catch(Exception e){ 275 throw new javax.ejb.EJBException("Could not update item " 276 + item , e); 277 278 } 279 } 280 281 290 291 public boolean contains(Integer itemPK){ 292 ItemLocal item = null; 293 try{ 294 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 295 ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM); 296 item = home.findByPrimaryKey(itemPK); 297 return cart.containsKey(item); 298 }catch(Exception e){ 299 throw new javax.ejb.EJBException("Could not find item " 300 + itemPK , e); 301 } 302 } 303 304 311 312 public Integer listNumOfItems(){ 313 return new Integer(cart.size()); 314 } 315 316 324 325 public BigDecimal getTotalCost(){ 326 327 return recalculateTotal(); 328 329 } 330 331 339 340 public Collection getItemsInCart(){ 341 return cart.keySet(); 342 343 } 344 345 346 351 352 public void createOrders(){ 353 ItemLocal item = null; 354 String DATE_FORMAT = "yyyy-MM-dd"; 355 Date date = new Date(); 356 java.text.SimpleDateFormat sdf = 357 new java.text.SimpleDateFormat(DATE_FORMAT); 358 String strdate = sdf.format(date); 359 360 try{ 361 Set items = cart.keySet(); 362 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 363 OrderLocalHome orderHome = (OrderLocalHome)factory.getLocalHome (EJBHomeFactory.ORDER); 364 Iterator it = items.iterator(); 365 366 Collection purchased = customer.getPurchased(); 367 while(it.hasNext()) 368 { 369 item = ((ItemLocal) it.next()); 370 olstore.entity.OrderLocal order = orderHome.create(customer, 371 item, (Integer)(cart.get(item)), new String("Ordered"), 372 strdate, getCostForItem(item.getItemId())); 373 if(!purchased.contains(item)){ 374 purchased.add(item); 375 } 376 } 377 378 customer.setPurchased(purchased); 379 clearContents(); 380 }catch(Exception e){ 381 throw new javax.ejb.EJBException("Could not add item " 382 + item , e); 383 } 384 } 385 386 private void clearContents(){ 387 cart.clear(); 388 } 389 390 394 public ArrayList shoppingCartToDTOs(){ 395 Iterator itemIterator = getItemsInCart().iterator(); 396 ArrayList cartEntries = new ArrayList(); 397 while(itemIterator.hasNext()){ 398 ItemLocal currItemLocal = (ItemLocal)itemIterator.next(); 399 ShoppingCartItem cartItem = new ShoppingCartItem(currItemLocal); 400 cartItem.setQuantity(getQuantity(cartItem.getItemId()).toString()); 401 cartItem.setPrice(getCostForItem(cartItem.getItemId())); 402 cartEntries.add(cartItem); 403 } 404 405 return cartEntries; 406 } 407 408 409 415 public void DTOsToShoppingCart(ArrayList cartEntries){ 416 Iterator entriesIterator = cartEntries.iterator(); 417 while(entriesIterator.hasNext()){ 418 ShoppingCartItem cartItem = (ShoppingCartItem) entriesIterator.next(); 419 420 if(Integer.parseInt(cartItem.getQuantity())==0) 421 removeItemFromCart(cartItem.getItemId()); 422 else 423 updateQuantity(cartItem.getItemId(), new Integer(cartItem.getQuantity())); 424 } 425 426 recalculateTotal(); 427 } 428 429 430 431 435 436 public BigDecimal getCostForItem(Integer itemPK){ 437 ItemLocal item = null; 438 try{ 439 EJBHomeFactory factory = EJBHomeFactory.getInstance(); 440 ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM); 441 item = home.findByPrimaryKey(itemPK); 442 return (item.getPrice().multiply(new BigDecimal(((Integer) cart.get(item)).toString()))); 443 }catch(Exception e){ 444 throw new javax.ejb.EJBException("Could not find item " 445 + itemPK , e); 446 } 447 448 449 } 450 451 public void ejbRemove(){ 452 } 453 454 public void ejbActivate(){ 455 } 456 457 public void ejbPassivate(){ 458 } 459 460 public void setSessionContext(javax.ejb.SessionContext cntx){ 461 ejbContext = cntx; 462 try{ 463 initialContext = new javax.naming.InitialContext(); 464 }catch(javax.naming.NamingException ne){ 465 throw new javax.ejb.EJBException(ne); 466 } 467 } 468 } 469 470 471 | Popular Tags |