Source Code Cross Referenced for ShoppingCartBean.java in  » J2EE » JOnAS-4.8.6 » olstore » session » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » olstore.session 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003:         *
004:         * This library is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU Lesser General Public
006:         * License as published by the Free Software Foundation; either
007:         * version 2.1 of the License, or any later version.
008:         *
009:         * This library is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:         * Lesser General Public License for more details.
013:         *
014:         * You should have received a copy of the GNU Lesser General Public
015:         * License along with this library; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
017:         * USA
018:         *
019:         * Component of: Red Hat Application Server
020:         * 
021:         * Initial Developers: Aizaz Ahmed
022:         *                     Vivek Lakshmanan
023:         *                     Andrew Overholt
024:         *                     Matthew Wringe
025:         *
026:         */package olstore.session;
027:
028:        import java.util.ArrayList;
029:        import java.util.Collection;
030:        import java.util.Date;
031:        import java.util.HashMap;
032:        import java.util.Iterator;
033:        import java.util.Set;
034:        import java.math.BigDecimal;
035:
036:        import javax.ejb.SessionBean;
037:
038:        import olstore.dto.ShoppingCartItem;
039:        import olstore.framework.EJBHomeFactory;
040:
041:        import olstore.entity.ItemLocal;
042:        import olstore.entity.ItemLocalHome;
043:        import olstore.entity.OrderLocalHome;
044:        import olstore.entity.UserLocal;
045:
046:        /* The costs stored in the session bean would be unformatted float values,
047:         * @author mwringe
048:         *
049:         *  
050:         *
051:         */
052:        /* it is the presentation layer's responsibility to enforce the formatting
053:         * @author mwringe
054:         *
055:         *  
056:         *
057:         */
058:        /* the display of the costs.
059:         */
060:
061:        /**
062:         * @ejb.bean name="ShoppingCart"
063:         *	type="Stateful" 
064:         *  transaction-type="Container"
065:         *  view-type="local"
066:         *  local-jndi-name="ShoppingCartLocal_L"
067:         * 
068:         * @ejb.ejb-ref
069:         *  ejb-name="User"
070:         *  view-type="local"
071:         *  ref-name="ejb/UserLocal"
072:         * 
073:         * @ejb.ejb-ref
074:         *  ejb-name="Item"
075:         *  view-type="local"
076:         *  ref-name="ejb/ItemLocal"
077:         * 
078:         * @ejb.ejb-ref
079:         *  ejb-name="Order"
080:         *  view-type="local"
081:         *  ref-name="ejb/OrderLocal"
082:         * 
083:         * @ejb.transaction
084:         *  type="Required"
085:         *  
086:         *--
087:         * This is needed for JOnAS.
088:         * If you are not using JOnAS you can safely remove the tags below.
089:         * @jonas.bean ejb-name="ShoppingCart"
090:         * jndi-name="ShoppingCartLocal"
091:         * 
092:         *--
093:         **/
094:
095:        public abstract class ShoppingCartBean implements  SessionBean {
096:            private UserLocal customer;
097:            private HashMap cart;
098:            private javax.ejb.SessionContext ejbContext;
099:            private javax.naming.Context initialContext;
100:
101:            /**
102:             *
103:             * @ejb.create-method
104:             *
105:             */
106:
107:            public void ejbCreate(olstore.entity.UserLocal user) {
108:                customer = user;
109:                cart = new HashMap();
110:            }
111:
112:            /**
113:             * 
114:             * @return User
115:             * 
116:             * @ejb.interface-method
117:             * 
118:             */
119:
120:            public olstore.entity.UserLocal getUser() {
121:                return customer;
122:            }
123:
124:            /**
125:             * 
126:             * Add items to shopping cart
127:             * 
128:             * @param itemPK
129:             * @param quantity
130:             * 
131:             * 
132:             */
133:            private void addItemToCart(Integer itemPK, Integer quantity) {
134:                ItemLocal item = null;
135:                Integer qty;
136:
137:                if (quantity.intValue() <= 0) {
138:
139:                    throw new NumberFormatException(
140:                            "Quantity must be greater than 0:");
141:                }
142:                try {
143:                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
144:                    ItemLocalHome home = (ItemLocalHome) factory
145:                            .getLocalHome(EJBHomeFactory.ITEM);
146:                    item = home.findByPrimaryKey(itemPK);
147:                    if ((qty = (Integer) cart.get(item)) != null) {
148:                        cart.remove(item);
149:                    }
150:                    cart.put(item, quantity);
151:
152:                } catch (Exception e) {
153:                    throw new javax.ejb.EJBException("Could not add item "
154:                            + item, e);
155:                }
156:            }
157:
158:            private BigDecimal recalculateTotal() {
159:                Iterator it = cart.keySet().iterator();
160:                BigDecimal totalCost = new BigDecimal(0.00f);
161:
162:                while (it.hasNext())
163:                    totalCost = totalCost.add(getCostForItem(((ItemLocal) it
164:                            .next()).getItemId()));
165:                return totalCost;
166:            }
167:
168:            /**
169:             * 
170:             * @param itemPK
171:             * @return quantity of those items in the cart
172:             * 
173:             * @ejb.interface-method
174:             * 
175:             */
176:
177:            public Integer getQuantity(Integer itemPK) {
178:                ItemLocal item = null;
179:
180:                try {
181:                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
182:                    ItemLocalHome home = (ItemLocalHome) factory
183:                            .getLocalHome(EJBHomeFactory.ITEM);
184:                    item = home.findByPrimaryKey(itemPK);
185:                    return ((Integer) cart.get(item));
186:                } catch (Exception e) {
187:                    throw new javax.ejb.EJBException(
188:                            "Could not get the quantity for item" + item, e);
189:                }
190:
191:            }
192:
193:            /**
194:             * 
195:             * Remove a specified number of items from the shopping cart
196:             * 
197:             * @param itemPK
198:             * @param quantity
199:             * 
200:             */
201:
202:            private void removeItemFromCart(Integer itemPK, Integer quantity) {
203:                ItemLocal item = null;
204:                try {
205:                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
206:                    ItemLocalHome home = (ItemLocalHome) factory
207:                            .getLocalHome(EJBHomeFactory.ITEM);
208:                    item = home.findByPrimaryKey(itemPK);
209:                    cart.remove(item);
210:                    cart.put(item, quantity);
211:                } catch (Exception e) {
212:                    throw new javax.ejb.EJBException("Could not remove item "
213:                            + item, e);
214:                }
215:
216:            }
217:
218:            /**
219:             * Remove one item from the cart
220:             * 
221:             * @param itemPK
222:             * 
223:             * @ejb.interface-method
224:             * 
225:             */
226:
227:            public void removeItemFromCart(Integer itemPK) {
228:                ItemLocal item = null;
229:                try {
230:                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
231:                    ItemLocalHome home = (ItemLocalHome) factory
232:                            .getLocalHome(EJBHomeFactory.ITEM);
233:                    item = home.findByPrimaryKey(itemPK);
234:                    cart.remove(item);
235:                } catch (Exception e) {
236:                    throw new javax.ejb.EJBException("Could not remove item "
237:                            + item, e);
238:
239:                }
240:            }
241:
242:            /**
243:             * Update the quantity of an item in the cart
244:             * 
245:             * @param itemPK
246:             * @param quantity
247:             * 
248:             * @ejb.interface-method
249:             * 
250:             */
251:
252:            public void updateQuantity(Integer itemPK, Integer quantity) {
253:                ItemLocal item = null;
254:                if (quantity.intValue() <= 0) {
255:
256:                    throw new NumberFormatException(
257:                            "Quantity must be greater than 0:");
258:                }
259:
260:                try {
261:                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
262:                    ItemLocalHome home = (ItemLocalHome) factory
263:                            .getLocalHome(EJBHomeFactory.ITEM);
264:                    item = home.findByPrimaryKey(itemPK);
265:                    int orig = 0;
266:                    Integer origInt;
267:                    if ((origInt = ((Integer) (cart.get(item)))) != null)
268:                        orig = ((Integer) (cart.get(item))).intValue();
269:                    if (orig < quantity.intValue())
270:                        addItemToCart(itemPK, quantity);
271:                    else
272:                        removeItemFromCart(itemPK, quantity);
273:
274:                } catch (Exception e) {
275:                    throw new javax.ejb.EJBException("Could not update item "
276:                            + item, e);
277:
278:                }
279:            }
280:
281:            /**
282:             * Returns whether or not hte item is in the shopping cart
283:             * 
284:             * @param itemPK
285:             * @return whether or not the item is in the shopping cart
286:             * 
287:             * @ejb.interface-method
288:             * 
289:             */
290:
291:            public boolean contains(Integer itemPK) {
292:                ItemLocal item = null;
293:                try {
294:                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
295:                    ItemLocalHome home = (ItemLocalHome) factory
296:                            .getLocalHome(EJBHomeFactory.ITEM);
297:                    item = home.findByPrimaryKey(itemPK);
298:                    return cart.containsKey(item);
299:                } catch (Exception e) {
300:                    throw new javax.ejb.EJBException("Could not find item "
301:                            + itemPK, e);
302:                }
303:            }
304:
305:            /**
306:             * Returns the total number of items in the cart
307:             * 
308:             * @return total number of items in the cart
309:             * 
310:             * @ejb.interface-method
311:             */
312:
313:            public Integer listNumOfItems() {
314:                return new Integer(cart.size());
315:            }
316:
317:            /**
318:             * Returns the total cost of items in the cart
319:             * 
320:             * @return total cost of items in the cart
321:             * 
322:             * @ejb.interface-method
323:             * 
324:             */
325:
326:            public BigDecimal getTotalCost() {
327:
328:                return recalculateTotal();
329:
330:            }
331:
332:            /**
333:             * Returns the items in the cart
334:             * 
335:             * @return the items in the cart
336:             * 
337:             * @ejb.interface-method
338:             * 
339:             */
340:
341:            public Collection getItemsInCart() {
342:                return cart.keySet();
343:
344:            }
345:
346:            /**
347:             * Crate orders from the items in the cart
348:             *
349:             * @ejb.interface-method
350:             */
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 = new java.text.SimpleDateFormat(
357:                        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
364:                            .getLocalHome(EJBHomeFactory.ORDER);
365:                    Iterator it = items.iterator();
366:
367:                    Collection purchased = customer.getPurchased();
368:                    while (it.hasNext()) {
369:                        item = ((ItemLocal) it.next());
370:                        olstore.entity.OrderLocal order = orderHome.create(
371:                                customer, item, (Integer) (cart.get(item)),
372:                                new String("Ordered"), strdate,
373:                                getCostForItem(item.getItemId()));
374:                        if (!purchased.contains(item)) {
375:                            purchased.add(item);
376:                        }
377:                    }
378:
379:                    customer.setPurchased(purchased);
380:                    clearContents();
381:                } catch (Exception e) {
382:                    throw new javax.ejb.EJBException("Could not add item "
383:                            + item, e);
384:                }
385:            }
386:
387:            private void clearContents() {
388:                cart.clear();
389:            }
390:
391:            /**
392:             * @ejb.interface-method
393:             *
394:             */
395:            public ArrayList shoppingCartToDTOs() {
396:                Iterator itemIterator = getItemsInCart().iterator();
397:                ArrayList cartEntries = new ArrayList();
398:                while (itemIterator.hasNext()) {
399:                    ItemLocal currItemLocal = (ItemLocal) itemIterator.next();
400:                    ShoppingCartItem cartItem = new ShoppingCartItem(
401:                            currItemLocal);
402:                    cartItem.setQuantity(getQuantity(cartItem.getItemId())
403:                            .toString());
404:                    cartItem.setPrice(getCostForItem(cartItem.getItemId()));
405:                    cartEntries.add(cartItem);
406:                }
407:
408:                return cartEntries;
409:            }
410:
411:            /**
412:             * Takes updated information from the form and transfers to the Shopping Cart.
413:             * @param cartEntries
414:             * 
415:             * @ejb.interface-method
416:             */
417:            public void DTOsToShoppingCart(ArrayList cartEntries) {
418:                Iterator entriesIterator = cartEntries.iterator();
419:                while (entriesIterator.hasNext()) {
420:                    ShoppingCartItem cartItem = (ShoppingCartItem) entriesIterator
421:                            .next();
422:
423:                    if (Integer.parseInt(cartItem.getQuantity()) == 0)
424:                        removeItemFromCart(cartItem.getItemId());
425:                    else
426:                        updateQuantity(cartItem.getItemId(), new Integer(
427:                                cartItem.getQuantity()));
428:                }
429:
430:                recalculateTotal();
431:            }
432:
433:            /**
434:             * @ejb.interface-method
435:             *
436:             */
437:
438:            public BigDecimal getCostForItem(Integer itemPK) {
439:                ItemLocal item = null;
440:                try {
441:                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
442:                    ItemLocalHome home = (ItemLocalHome) factory
443:                            .getLocalHome(EJBHomeFactory.ITEM);
444:                    item = home.findByPrimaryKey(itemPK);
445:                    return (item.getPrice().multiply(new BigDecimal(
446:                            ((Integer) cart.get(item)).toString())));
447:                } catch (Exception e) {
448:                    throw new javax.ejb.EJBException("Could not find item "
449:                            + itemPK, e);
450:                }
451:
452:            }
453:
454:            public void ejbRemove() {
455:            }
456:
457:            public void ejbActivate() {
458:            }
459:
460:            public void ejbPassivate() {
461:            }
462:
463:            public void setSessionContext(javax.ejb.SessionContext cntx) {
464:                ejbContext = cntx;
465:                try {
466:                    initialContext = new javax.naming.InitialContext();
467:                } catch (javax.naming.NamingException ne) {
468:                    throw new javax.ejb.EJBException(ne);
469:                }
470:            }
471:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.