Source Code Cross Referenced for ShoppingListServices.java in  » ERP-CRM-Financial » ofbiz » org » ofbiz » order » shoppinglist » 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 » ERP CRM Financial » ofbiz » org.ofbiz.order.shoppinglist 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         *******************************************************************************/package org.ofbiz.order.shoppinglist;
019:
020:        import java.util.Map;
021:        import java.util.List;
022:        import java.util.Locale;
023:        import java.util.Iterator;
024:        import java.util.HashMap;
025:        import java.sql.Timestamp;
026:
027:        import org.ofbiz.service.DispatchContext;
028:        import org.ofbiz.service.ServiceUtil;
029:        import org.ofbiz.service.LocalDispatcher;
030:        import org.ofbiz.service.GenericServiceException;
031:        import org.ofbiz.service.calendar.RecurrenceInfo;
032:        import org.ofbiz.service.calendar.RecurrenceInfoException;
033:        import org.ofbiz.order.shoppingcart.ShoppingCart;
034:        import org.ofbiz.order.shoppingcart.CartItemModifyException;
035:        import org.ofbiz.order.shoppingcart.ItemNotFoundException;
036:        import org.ofbiz.order.shoppingcart.CheckOutHelper;
037:        import org.ofbiz.order.order.OrderReadHelper;
038:        import org.ofbiz.entity.GenericDelegator;
039:        import org.ofbiz.entity.GenericValue;
040:        import org.ofbiz.entity.GenericEntityException;
041:        import org.ofbiz.entity.condition.EntityExpr;
042:        import org.ofbiz.entity.condition.EntityOperator;
043:        import org.ofbiz.entity.condition.EntityCondition;
044:        import org.ofbiz.entity.condition.EntityConditionList;
045:        import org.ofbiz.entity.transaction.TransactionUtil;
046:        import org.ofbiz.entity.util.EntityListIterator;
047:        import org.ofbiz.entity.util.EntityUtil;
048:        import org.ofbiz.base.util.UtilMisc;
049:        import org.ofbiz.base.util.Debug;
050:        import org.ofbiz.base.util.UtilValidate;
051:        import org.ofbiz.base.util.UtilDateTime;
052:        import org.ofbiz.base.util.GeneralException;
053:        import org.ofbiz.base.util.UtilProperties;
054:        import org.ofbiz.product.store.ProductStoreWorker;
055:
056:        /**
057:         * Shopping List Services
058:         */
059:        public class ShoppingListServices {
060:
061:            public static final String module = ShoppingListServices.class
062:                    .getName();
063:            public static final String resource_error = "OrderErrorUiLabels";
064:
065:            public static Map setShoppingListRecurrence(DispatchContext dctx,
066:                    Map context) {
067:                GenericDelegator delegator = dctx.getDelegator();
068:                Timestamp startDate = (Timestamp) context.get("startDateTime");
069:                Timestamp endDate = (Timestamp) context.get("endDateTime");
070:                Integer frequency = (Integer) context.get("frequency");
071:                Integer interval = (Integer) context.get("intervalNumber");
072:                Locale locale = (Locale) context.get("locale");
073:
074:                if (frequency == null || interval == null) {
075:                    Debug.logWarning(UtilProperties.getMessage(resource_error,
076:                            "OrderFrequencyOrIntervalWasNotSpecified", locale),
077:                            module);
078:                    return ServiceUtil.returnSuccess();
079:                }
080:
081:                if (startDate == null) {
082:                    switch (frequency.intValue()) {
083:                    case 5:
084:                        startDate = UtilDateTime.getWeekStart(UtilDateTime
085:                                .nowTimestamp(), 0, interval.intValue());
086:                        break;
087:                    case 6:
088:                        startDate = UtilDateTime.getMonthStart(UtilDateTime
089:                                .nowTimestamp(), 0, interval.intValue());
090:                        break;
091:                    case 7:
092:                        startDate = UtilDateTime.getYearStart(UtilDateTime
093:                                .nowTimestamp(), 0, interval.intValue());
094:                        break;
095:                    default:
096:                        return ServiceUtil
097:                                .returnError(UtilProperties
098:                                        .getMessage(
099:                                                resource_error,
100:                                                "OrderInvalidFrequencyForShoppingListRecurrence",
101:                                                locale));
102:                    }
103:                }
104:
105:                long startTime = startDate.getTime();
106:                long endTime = 0;
107:                if (endDate != null) {
108:                    endTime = endDate.getTime();
109:                }
110:
111:                RecurrenceInfo recInfo = null;
112:                try {
113:                    recInfo = RecurrenceInfo.makeInfo(delegator, startTime,
114:                            frequency.intValue(), interval.intValue(), -1,
115:                            endTime);
116:                } catch (RecurrenceInfoException e) {
117:                    Debug.logError(e, module);
118:                    return ServiceUtil
119:                            .returnError(UtilProperties
120:                                    .getMessage(
121:                                            resource_error,
122:                                            "OrderUnableToCreateShoppingListRecurrenceInformation",
123:                                            locale));
124:                }
125:
126:                Debug.log("Next Recurrence - "
127:                        + UtilDateTime.getTimestamp(recInfo.next()), module);
128:                Map result = ServiceUtil.returnSuccess();
129:                result.put("recurrenceInfoId", recInfo.getID());
130:
131:                return result;
132:            }
133:
134:            public static Map createListReorders(DispatchContext dctx,
135:                    Map context) {
136:                LocalDispatcher dispatcher = dctx.getDispatcher();
137:                GenericDelegator delegator = dctx.getDelegator();
138:
139:                GenericValue userLogin = (GenericValue) context
140:                        .get("userLogin");
141:                Locale locale = (Locale) context.get("locale");
142:
143:                boolean beganTransaction = false;
144:                try {
145:                    beganTransaction = TransactionUtil.begin();
146:
147:                    List exprs = UtilMisc.toList(new EntityExpr(
148:                            "shoppingListTypeId", EntityOperator.EQUALS,
149:                            "SLT_AUTO_REODR"), new EntityExpr("isActive",
150:                            EntityOperator.EQUALS, "Y"));
151:                    EntityCondition cond = new EntityConditionList(exprs,
152:                            EntityOperator.AND);
153:                    List order = UtilMisc.toList("-lastOrderedDate");
154:
155:                    EntityListIterator eli = null;
156:                    eli = delegator.findListIteratorByCondition("ShoppingList",
157:                            cond, null, order);
158:
159:                    if (eli != null) {
160:                        GenericValue shoppingList;
161:                        while (((shoppingList = (GenericValue) eli.next()) != null)) {
162:                            Timestamp lastOrder = shoppingList
163:                                    .getTimestamp("lastOrderedDate");
164:                            GenericValue recurrenceInfo = null;
165:                            recurrenceInfo = shoppingList
166:                                    .getRelatedOne("RecurrenceInfo");
167:
168:                            Timestamp startDateTime = recurrenceInfo
169:                                    .getTimestamp("startDateTime");
170:                            RecurrenceInfo recurrence = null;
171:                            if (recurrenceInfo != null) {
172:                                try {
173:                                    recurrence = new RecurrenceInfo(
174:                                            recurrenceInfo);
175:                                } catch (RecurrenceInfoException e) {
176:                                    Debug.logError(e, module);
177:                                }
178:                            }
179:
180:                            // check the next recurrence
181:                            if (recurrence != null) {
182:                                long next = lastOrder == null ? recurrence
183:                                        .next(startDateTime.getTime())
184:                                        : recurrence.next(lastOrder.getTime());
185:                                Timestamp now = UtilDateTime.nowTimestamp();
186:                                Timestamp nextOrder = UtilDateTime
187:                                        .getDayStart(UtilDateTime
188:                                                .getTimestamp(next));
189:
190:                                if (nextOrder.after(now)) {
191:                                    continue;
192:                                }
193:                            } else {
194:                                continue;
195:                            }
196:
197:                            ShoppingCart listCart = makeShoppingListCart(
198:                                    dispatcher, shoppingList, locale);
199:                            CheckOutHelper helper = new CheckOutHelper(
200:                                    dispatcher, delegator, listCart);
201:
202:                            // store the order
203:                            Map createResp = helper.createOrder(userLogin);
204:                            if (createResp != null
205:                                    && ServiceUtil.isError(createResp)) {
206:                                Debug.logError(
207:                                        "Cannot create order for shopping list - "
208:                                                + shoppingList, module);
209:                            } else {
210:                                String orderId = (String) createResp
211:                                        .get("orderId");
212:
213:                                // authorize the payments
214:                                Map payRes = null;
215:                                try {
216:                                    payRes = helper
217:                                            .processPayment(
218:                                                    ProductStoreWorker
219:                                                            .getProductStore(
220:                                                                    listCart
221:                                                                            .getProductStoreId(),
222:                                                                    delegator),
223:                                                    userLogin);
224:                                } catch (GeneralException e) {
225:                                    Debug.logError(e, module);
226:                                }
227:
228:                                if (payRes != null
229:                                        && ServiceUtil.isError(payRes)) {
230:                                    Debug.logError(
231:                                            "Payment processing problems with shopping list - "
232:                                                    + shoppingList, module);
233:                                }
234:
235:                                shoppingList.set("lastOrderedDate",
236:                                        UtilDateTime.nowTimestamp());
237:                                shoppingList.store();
238:
239:                                // send notification
240:                                try {
241:                                    dispatcher.runAsync(
242:                                            "sendOrderPayRetryNotification",
243:                                            UtilMisc.toMap("orderId", orderId));
244:                                } catch (GenericServiceException e) {
245:                                    Debug.logError(e, module);
246:                                }
247:
248:                                // increment the recurrence
249:                                recurrence.incrementCurrentCount();
250:                            }
251:                        }
252:
253:                        eli.close();
254:                    }
255:
256:                    return ServiceUtil.returnSuccess();
257:                } catch (GenericEntityException e) {
258:                    try {
259:                        // only rollback the transaction if we started one...
260:                        TransactionUtil
261:                                .rollback(
262:                                        beganTransaction,
263:                                        "Error creating shopping list auto-reorders",
264:                                        e);
265:                    } catch (GenericEntityException e2) {
266:                        Debug.logError(e2,
267:                                "[GenericDelegator] Could not rollback transaction: "
268:                                        + e2.toString(), module);
269:                    }
270:
271:                    String errMsg = "Error while creating new shopping list based automatic reorder"
272:                            + e.toString();
273:                    Debug.logError(e, errMsg, module);
274:                    return ServiceUtil.returnError(errMsg);
275:                } finally {
276:                    try {
277:                        // only commit the transaction if we started one... this will throw an exception if it fails
278:                        TransactionUtil.commit(beganTransaction);
279:                    } catch (GenericEntityException e) {
280:                        Debug
281:                                .logError(
282:                                        e,
283:                                        "Could not commit transaction for creating new shopping list based automatic reorder",
284:                                        module);
285:                    }
286:                }
287:            }
288:
289:            public static Map splitShipmentMethodString(DispatchContext dctx,
290:                    Map context) {
291:                String shipmentMethodString = (String) context
292:                        .get("shippingMethodString");
293:                Map result = ServiceUtil.returnSuccess();
294:
295:                if (UtilValidate.isNotEmpty(shipmentMethodString)) {
296:                    int delimiterPos = shipmentMethodString.indexOf('@');
297:                    String shipmentMethodTypeId = null;
298:                    String carrierPartyId = null;
299:
300:                    if (delimiterPos > 0) {
301:                        shipmentMethodTypeId = shipmentMethodString.substring(
302:                                0, delimiterPos);
303:                        carrierPartyId = shipmentMethodString
304:                                .substring(delimiterPos + 1);
305:                        result
306:                                .put("shipmentMethodTypeId",
307:                                        shipmentMethodTypeId);
308:                        result.put("carrierPartyId", carrierPartyId);
309:                    }
310:                }
311:                return result;
312:            }
313:
314:            public static Map makeListFromOrder(DispatchContext dctx,
315:                    Map context) {
316:                LocalDispatcher dispatcher = dctx.getDispatcher();
317:                GenericDelegator delegator = dctx.getDelegator();
318:
319:                String shoppingListTypeId = (String) context
320:                        .get("shoppingListTypeId");
321:                String shoppingListId = (String) context.get("shoppingListId");
322:                String orderId = (String) context.get("orderId");
323:                String partyId = (String) context.get("partyId");
324:
325:                Timestamp startDate = (Timestamp) context.get("startDateTime");
326:                Timestamp endDate = (Timestamp) context.get("endDateTime");
327:                Integer frequency = (Integer) context.get("frequency");
328:                Integer interval = (Integer) context.get("intervalNumber");
329:
330:                GenericValue userLogin = (GenericValue) context
331:                        .get("userLogin");
332:                Locale locale = (Locale) context.get("locale");
333:
334:                boolean beganTransaction = false;
335:                try {
336:                    beganTransaction = TransactionUtil.begin();
337:
338:                    GenericValue orderHeader = null;
339:                    orderHeader = delegator.findByPrimaryKey("OrderHeader",
340:                            UtilMisc.toMap("orderId", orderId));
341:
342:                    if (orderHeader == null) {
343:                        return ServiceUtil.returnError(UtilProperties
344:                                .getMessage(resource_error,
345:                                        "OrderUnableToLocateOrder", UtilMisc
346:                                                .toMap("orderId", orderId),
347:                                        locale));
348:                    }
349:                    String productStoreId = orderHeader
350:                            .getString("productStoreId");
351:
352:                    if (UtilValidate.isEmpty(shoppingListId)) {
353:                        // create a new shopping list
354:                        if (partyId == null) {
355:                            partyId = userLogin.getString("partyId");
356:                        }
357:
358:                        Map serviceCtx = UtilMisc.toMap("userLogin", userLogin,
359:                                "partyId", partyId, "productStoreId",
360:                                productStoreId, "listName",
361:                                "List Created From Order #" + orderId);
362:
363:                        if (UtilValidate.isNotEmpty(shoppingListTypeId)) {
364:                            serviceCtx.put("shoppingListTypeId",
365:                                    shoppingListTypeId);
366:                        }
367:
368:                        Map newListResult = null;
369:                        try {
370:
371:                            newListResult = dispatcher.runSync(
372:                                    "createShoppingList", serviceCtx);
373:                        } catch (GenericServiceException e) {
374:                            Debug.logError(e,
375:                                    "Problems creating new ShoppingList",
376:                                    module);
377:                            return ServiceUtil
378:                                    .returnError(UtilProperties
379:                                            .getMessage(
380:                                                    resource_error,
381:                                                    "OrderUnableToCreateNewShoppingList",
382:                                                    locale));
383:                        }
384:
385:                        // check for errors
386:                        if (ServiceUtil.isError(newListResult)) {
387:                            return ServiceUtil.returnError(ServiceUtil
388:                                    .getErrorMessage(newListResult));
389:                        }
390:
391:                        // get the new list id
392:                        if (newListResult != null) {
393:                            shoppingListId = (String) newListResult
394:                                    .get("shoppingListId");
395:                        }
396:                    }
397:
398:                    GenericValue shoppingList = null;
399:                    shoppingList = delegator.findByPrimaryKey("ShoppingList",
400:                            UtilMisc.toMap("shoppingListId", shoppingListId));
401:
402:                    if (shoppingList == null) {
403:                        return ServiceUtil
404:                                .returnError(UtilProperties.getMessage(
405:                                        resource_error,
406:                                        "OrderNoShoppingListAvailable", locale));
407:                    }
408:                    shoppingListTypeId = shoppingList
409:                            .getString("shoppingListTypeId");
410:
411:                    OrderReadHelper orh = new OrderReadHelper(orderHeader);
412:                    if (orh == null) {
413:                        return ServiceUtil.returnError(UtilProperties
414:                                .getMessage(resource_error,
415:                                        "OrderUnableToLoadOrderReadHelper",
416:                                        UtilMisc.toMap("orderId", orderId),
417:                                        locale));
418:                    }
419:
420:                    List orderItems = orh.getOrderItems();
421:                    Iterator i = orderItems.iterator();
422:                    while (i.hasNext()) {
423:                        GenericValue orderItem = (GenericValue) i.next();
424:                        if (orderItem.get("productId") != null) {
425:                            Map ctx = UtilMisc.toMap("userLogin", userLogin,
426:                                    "shoppingListId", shoppingListId,
427:                                    "productId", orderItem.get("productId"),
428:                                    "quantity", orderItem.get("quantity"));
429:                            Map serviceResult = null;
430:                            try {
431:                                serviceResult = dispatcher.runSync(
432:                                        "createShoppingListItem", ctx);
433:                            } catch (GenericServiceException e) {
434:                                Debug.logError(e, module);
435:                            }
436:                            if (serviceResult == null
437:                                    || ServiceUtil.isError(serviceResult)) {
438:                                return ServiceUtil
439:                                        .returnError(UtilProperties
440:                                                .getMessage(
441:                                                        resource_error,
442:                                                        "OrderUnableToAddItemToShoppingList",
443:                                                        UtilMisc
444:                                                                .toMap(
445:                                                                        "shoppingListId",
446:                                                                        shoppingListId),
447:                                                        locale));
448:                            }
449:                        }
450:                    }
451:
452:                    if ("SLT_AUTO_REODR".equals(shoppingListTypeId)) {
453:                        GenericValue paymentPref = EntityUtil.getFirst(orh
454:                                .getPaymentPreferences());
455:                        GenericValue shipGroup = EntityUtil.getFirst(orh
456:                                .getOrderItemShipGroups());
457:
458:                        Map slCtx = new HashMap();
459:                        slCtx.put("shipmentMethodTypeId", shipGroup
460:                                .get("shipmentMethodTypeId"));
461:                        slCtx.put("carrierRoleTypeId", shipGroup
462:                                .get("carrierRoleTypeId"));
463:                        slCtx.put("carrierPartyId", shipGroup
464:                                .get("carrierPartyId"));
465:                        slCtx.put("contactMechId", shipGroup
466:                                .get("contactMechId"));
467:                        slCtx.put("paymentMethodId", paymentPref
468:                                .get("paymentMethodId"));
469:                        slCtx.put("currencyUom", orh.getCurrency());
470:                        slCtx.put("startDateTime", startDate);
471:                        slCtx.put("endDateTime", endDate);
472:                        slCtx.put("frequency", frequency);
473:                        slCtx.put("intervalNumber", interval);
474:                        slCtx.put("isActive", "Y");
475:                        slCtx.put("shoppingListId", shoppingListId);
476:                        slCtx.put("userLogin", userLogin);
477:
478:                        Map slUpResp = null;
479:                        try {
480:                            slUpResp = dispatcher.runSync("updateShoppingList",
481:                                    slCtx);
482:                        } catch (GenericServiceException e) {
483:                            Debug.logError(e, module);
484:                        }
485:
486:                        if (slUpResp == null || ServiceUtil.isError(slUpResp)) {
487:                            return ServiceUtil
488:                                    .returnError(UtilProperties
489:                                            .getMessage(
490:                                                    resource_error,
491:                                                    "OrderUnableToUpdateShoppingListInformation",
492:                                                    UtilMisc.toMap(
493:                                                            "shoppingListId",
494:                                                            shoppingListId),
495:                                                    locale));
496:                        }
497:                    }
498:
499:                    Map result = ServiceUtil.returnSuccess();
500:                    result.put("shoppingListId", shoppingListId);
501:                    return result;
502:
503:                } catch (GenericEntityException e) {
504:                    try {
505:                        // only rollback the transaction if we started one...
506:                        TransactionUtil.rollback(beganTransaction,
507:                                "Error making shopping list from order", e);
508:                    } catch (GenericEntityException e2) {
509:                        Debug.logError(e2,
510:                                "[GenericDelegator] Could not rollback transaction: "
511:                                        + e2.toString(), module);
512:                    }
513:
514:                    String errMsg = "Error while creating new shopping list based on order"
515:                            + e.toString();
516:                    Debug.logError(e, errMsg, module);
517:                    return ServiceUtil.returnError(errMsg);
518:                } finally {
519:                    try {
520:                        // only commit the transaction if we started one... this will throw an exception if it fails
521:                        TransactionUtil.commit(beganTransaction);
522:                    } catch (GenericEntityException e) {
523:                        Debug
524:                                .logError(
525:                                        e,
526:                                        "Could not commit transaction for creating new shopping list based on order",
527:                                        module);
528:                    }
529:                }
530:            }
531:
532:            public static ShoppingCart makeShoppingListCart(
533:                    LocalDispatcher dispatcher, GenericValue shoppingList,
534:                    Locale locale) {
535:                GenericDelegator delegator = dispatcher.getDelegator();
536:                ShoppingCart listCart = null;
537:                if (shoppingList != null
538:                        && shoppingList.get("productStoreId") != null) {
539:                    String productStoreId = shoppingList
540:                            .getString("productStoreId");
541:                    String currencyUom = shoppingList.getString("currencyUom");
542:                    if (currencyUom == null) {
543:                        GenericValue productStore = ProductStoreWorker
544:                                .getProductStore(productStoreId, delegator);
545:                        if (productStore == null) {
546:                            return null;
547:                        }
548:                        currencyUom = productStore
549:                                .getString("defaultCurrencyUomId");
550:                    }
551:                    if (locale == null) {
552:                        locale = Locale.getDefault();
553:                    }
554:
555:                    List items = null;
556:                    try {
557:                        items = shoppingList.getRelated("ShoppingListItem",
558:                                UtilMisc.toList("shoppingListItemSeqId"));
559:                    } catch (GenericEntityException e) {
560:                        Debug.logError(e, module);
561:                    }
562:
563:                    if (UtilValidate.isNotEmpty(items)) {
564:                        listCart = new ShoppingCart(delegator, productStoreId,
565:                                locale, currencyUom);
566:                        listCart.setOrderPartyId(shoppingList
567:                                .getString("partyId"));
568:                        listCart.setAutoOrderShoppingListId(shoppingList
569:                                .getString("shoppingListId"));
570:
571:                        Iterator i = items.iterator();
572:                        while (i.hasNext()) {
573:                            GenericValue shoppingListItem = (GenericValue) i
574:                                    .next();
575:                            String productId = shoppingListItem
576:                                    .getString("productId");
577:                            Double quantity = shoppingListItem
578:                                    .getDouble("quantity");
579:                            Timestamp reservStart = shoppingListItem
580:                                    .getTimestamp("reservStart");
581:                            Double reservLength = null;
582:                            if (shoppingListItem.get("reservLength") != null) {
583:                                reservLength = shoppingListItem
584:                                        .getDouble("reservLength");
585:                            }
586:                            Double reservPersons = null;
587:                            ;
588:                            if (shoppingListItem.get("reservPersons") != null) {
589:                                reservPersons = shoppingListItem
590:                                        .getDouble("reservPersons");
591:                            }
592:                            if (UtilValidate.isNotEmpty(productId)
593:                                    && quantity != null) {
594:                                // list items are noted in the shopping cart
595:                                String listId = shoppingListItem
596:                                        .getString("shoppingListId");
597:                                String itemId = shoppingListItem
598:                                        .getString("shoppingListItemSeqId");
599:                                Map attributes = UtilMisc.toMap(
600:                                        "shoppingListId", listId,
601:                                        "shoppingListItemSeqId", itemId);
602:
603:                                try {
604:                                    listCart.addOrIncreaseItem(productId, null,
605:                                            quantity.doubleValue(),
606:                                            reservStart, reservLength,
607:                                            reservPersons, null, null, null,
608:                                            attributes, null, null, null, null,
609:                                            null, dispatcher);
610:                                } catch (CartItemModifyException e) {
611:                                    Debug.logError(e,
612:                                            "Unable to add product to List Cart - "
613:                                                    + productId, module);
614:                                } catch (ItemNotFoundException e) {
615:                                    Debug.logError(e, "Product not found - "
616:                                            + productId, module);
617:                                }
618:                            }
619:                        }
620:
621:                        if (listCart.size() > 0) {
622:                            if (UtilValidate.isNotEmpty(shoppingList
623:                                    .get("paymentMethodId"))) {
624:                                listCart.addPayment(shoppingList
625:                                        .getString("paymentMethodId"));
626:                            }
627:                            if (UtilValidate.isNotEmpty(shoppingList
628:                                    .get("contactMechId"))) {
629:                                listCart
630:                                        .setShippingContactMechId(
631:                                                0,
632:                                                shoppingList
633:                                                        .getString("contactMechId"));
634:                            }
635:                            if (UtilValidate.isNotEmpty(shoppingList
636:                                    .get("shipmentMethodTypeId"))) {
637:                                listCart
638:                                        .setShipmentMethodTypeId(
639:                                                0,
640:                                                shoppingList
641:                                                        .getString("shipmentMethodTypeId"));
642:                            }
643:                            if (UtilValidate.isNotEmpty(shoppingList
644:                                    .get("carrierPartyId"))) {
645:                                listCart.setCarrierPartyId(0, shoppingList
646:                                        .getString("carrierPartyId"));
647:                            }
648:                            if (UtilValidate.isNotEmpty(shoppingList
649:                                    .getString("productPromoCodeId"))) {
650:                                listCart.addProductPromoCode(shoppingList
651:                                        .getString("productPromoCodeId"),
652:                                        dispatcher);
653:                            }
654:                        }
655:                    }
656:                }
657:                return listCart;
658:            }
659:
660:            public static ShoppingCart makeShoppingListCart(
661:                    LocalDispatcher dispatcher, String shoppingListId,
662:                    Locale locale) {
663:                GenericDelegator delegator = dispatcher.getDelegator();
664:                GenericValue shoppingList = null;
665:                try {
666:                    shoppingList = delegator.findByPrimaryKey("ShoppingList",
667:                            UtilMisc.toMap("shoppingListId", shoppingListId));
668:                } catch (GenericEntityException e) {
669:                    Debug.logError(e, module);
670:                }
671:                return makeShoppingListCart(dispatcher, shoppingList, locale);
672:            }
673:
674:            /**
675:             * 
676:             * Given an orderId, this service will look through all its OrderItems and for each shoppingListItemId
677:             * and shoppingListItemSeqId, update the quantity purchased in the ShoppingListItem entity.  Used for
678:             * tracking how many of shopping list items are purchased.  This service is mounted as a seca on storeOrder.
679:             *
680:             * @param ctx - The DispatchContext that this service is operating in
681:             * @param context - Map containing the input parameters
682:             * @return Map with the result of the service, the output parameters
683:             */
684:            public static Map updateShoppingListQuantitiesFromOrder(
685:                    DispatchContext ctx, Map context) {
686:                Map result = new HashMap();
687:                GenericDelegator delegator = ctx.getDelegator();
688:                String orderId = (String) context.get("orderId");
689:                try {
690:                    List orderItems = delegator.findByAnd("OrderItem", UtilMisc
691:                            .toMap("orderId", orderId));
692:                    Iterator iter = orderItems.iterator();
693:                    while (iter.hasNext()) {
694:                        GenericValue orderItem = (GenericValue) iter.next();
695:                        String shoppingListId = orderItem
696:                                .getString("shoppingListId");
697:                        String shoppingListItemSeqId = orderItem
698:                                .getString("shoppingListItemSeqId");
699:                        if ((shoppingListId != null)
700:                                && (shoppingListId.length() > 0)) {
701:                            GenericValue shoppingListItem = delegator
702:                                    .findByPrimaryKey("ShoppingListItem",
703:                                            UtilMisc.toMap("shoppingListId",
704:                                                    shoppingListId,
705:                                                    "shoppingListItemSeqId",
706:                                                    shoppingListItemSeqId));
707:                            if (shoppingListItem != null) {
708:                                Double quantityPurchased = shoppingListItem
709:                                        .getDouble("quantityPurchased");
710:                                Double orderQuantity = orderItem
711:                                        .getDouble("quantity");
712:                                if (quantityPurchased != null) {
713:                                    shoppingListItem.set("quantityPurchased",
714:                                            new Double(orderQuantity
715:                                                    .doubleValue()
716:                                                    + quantityPurchased
717:                                                            .doubleValue()));
718:                                } else {
719:                                    shoppingListItem.set("quantityPurchased",
720:                                            orderQuantity);
721:                                }
722:                                shoppingListItem.store();
723:                            }
724:                        }
725:                    }
726:                } catch (Exception e) {
727:                    Debug.log("updateShoppingListQuantitiesFromOrder error:"
728:                            + e.getMessage());
729:                }
730:                return result;
731:            }
732:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.