KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > order > shoppinglist > ShoppingListServices


1 /*
2  * $Id: ShoppingListServices.java 6142 2005-11-18 18:56:08Z sichen $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.order.shoppinglist;
26
27 import java.util.Map JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.sql.Timestamp JavaDoc;
33
34 import org.ofbiz.service.DispatchContext;
35 import org.ofbiz.service.ServiceUtil;
36 import org.ofbiz.service.LocalDispatcher;
37 import org.ofbiz.service.GenericServiceException;
38 import org.ofbiz.service.calendar.RecurrenceInfo;
39 import org.ofbiz.service.calendar.RecurrenceInfoException;
40 import org.ofbiz.order.shoppingcart.ShoppingCart;
41 import org.ofbiz.order.shoppingcart.CartItemModifyException;
42 import org.ofbiz.order.shoppingcart.ItemNotFoundException;
43 import org.ofbiz.order.shoppingcart.CheckOutHelper;
44 import org.ofbiz.order.order.OrderReadHelper;
45 import org.ofbiz.entity.GenericDelegator;
46 import org.ofbiz.entity.GenericValue;
47 import org.ofbiz.entity.GenericEntityException;
48 import org.ofbiz.entity.condition.EntityExpr;
49 import org.ofbiz.entity.condition.EntityOperator;
50 import org.ofbiz.entity.condition.EntityCondition;
51 import org.ofbiz.entity.condition.EntityConditionList;
52 import org.ofbiz.entity.transaction.TransactionUtil;
53 import org.ofbiz.entity.util.EntityListIterator;
54 import org.ofbiz.entity.util.EntityUtil;
55 import org.ofbiz.base.util.UtilMisc;
56 import org.ofbiz.base.util.Debug;
57 import org.ofbiz.base.util.UtilValidate;
58 import org.ofbiz.base.util.UtilDateTime;
59 import org.ofbiz.base.util.GeneralException;
60 import org.ofbiz.base.util.UtilProperties;
61 import org.ofbiz.product.store.ProductStoreWorker;
62
63 /**
64  * Shopping List Services
65  *
66  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
67  * @version $Rev: 6142 $
68  * @since 3.3
69  */

70 public class ShoppingListServices {
71
72     public static final String JavaDoc module = ShoppingListServices.class.getName();
73     public static final String JavaDoc resource_error = "OrderErrorUiLabels";
74
75     public static Map JavaDoc setShoppingListRecurrence(DispatchContext dctx, Map JavaDoc context) {
76         GenericDelegator delegator = dctx.getDelegator();
77         Timestamp JavaDoc startDate = (Timestamp JavaDoc) context.get("startDateTime");
78         Timestamp JavaDoc endDate = (Timestamp JavaDoc) context.get("endDateTime");
79         Integer JavaDoc frequency = (Integer JavaDoc) context.get("frequency");
80         Integer JavaDoc interval = (Integer JavaDoc) context.get("intervalNumber");
81         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
82
83         if (frequency == null || interval == null) {
84             Debug.logWarning(UtilProperties.getMessage(resource_error,"OrderFrequencyOrIntervalWasNotSpecified", locale), module);
85             return ServiceUtil.returnSuccess();
86         }
87
88         if (startDate == null) {
89             switch (frequency.intValue()) {
90                 case 5:
91                     startDate = UtilDateTime.getWeekStart(UtilDateTime.nowTimestamp(), 0, interval.intValue());
92                     break;
93                 case 6:
94                     startDate = UtilDateTime.getMonthStart(UtilDateTime.nowTimestamp(), 0, interval.intValue());
95                     break;
96                 case 7:
97                     startDate = UtilDateTime.getYearStart(UtilDateTime.nowTimestamp(), 0, interval.intValue());
98                     break;
99                 default:
100                     return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderInvalidFrequencyForShoppingListRecurrence",locale));
101             }
102         }
103
104         long startTime = startDate.getTime();
105         long endTime = 0;
106         if (endDate != null) {
107             endTime = endDate.getTime();
108         }
109
110         RecurrenceInfo recInfo = null;
111         try {
112             recInfo = RecurrenceInfo.makeInfo(delegator, startTime, frequency.intValue(), interval.intValue(), -1, endTime);
113         } catch (RecurrenceInfoException e) {
114             Debug.logError(e, module);
115             return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToCreateShoppingListRecurrenceInformation",locale));
116         }
117
118         Debug.log("Next Recurrence - " + UtilDateTime.getTimestamp(recInfo.next()), module);
119         Map JavaDoc result = ServiceUtil.returnSuccess();
120         result.put("recurrenceInfoId", recInfo.getID());
121
122         return result;
123     }
124
125     public static Map JavaDoc createListReorders(DispatchContext dctx, Map JavaDoc context) {
126         LocalDispatcher dispatcher = dctx.getDispatcher();
127         GenericDelegator delegator = dctx.getDelegator();
128
129         GenericValue userLogin = (GenericValue) context.get("userLogin");
130         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
131
132         boolean beganTransaction = false;
133         try {
134             beganTransaction = TransactionUtil.begin();
135         
136             List JavaDoc exprs = UtilMisc.toList(new EntityExpr("shoppingListTypeId", EntityOperator.EQUALS, "SLT_AUTO_REODR"),
137                     new EntityExpr("isActive", EntityOperator.EQUALS, "Y"));
138             EntityCondition cond = new EntityConditionList(exprs, EntityOperator.AND);
139             List JavaDoc order = UtilMisc.toList("-lastOrderedDate");
140
141             EntityListIterator eli = null;
142             eli = delegator.findListIteratorByCondition("ShoppingList", cond, null, order);
143     
144             if (eli != null) {
145                 GenericValue shoppingList;
146                 while (((shoppingList = (GenericValue) eli.next()) != null)) {
147                     Timestamp JavaDoc lastOrder = shoppingList.getTimestamp("lastOrderedDate");
148                     GenericValue recurrenceInfo = null;
149                     recurrenceInfo = shoppingList.getRelatedOne("RecurrenceInfo");
150     
151                     Timestamp JavaDoc startDateTime = recurrenceInfo.getTimestamp("startDateTime");
152                     RecurrenceInfo recurrence = null;
153                     if (recurrenceInfo != null) {
154                         try {
155                             recurrence = new RecurrenceInfo(recurrenceInfo);
156                         } catch (RecurrenceInfoException e) {
157                             Debug.logError(e, module);
158                         }
159                     }
160     
161                     // check the next recurrence
162
if (recurrence != null) {
163                         long next = lastOrder == null ? recurrence.next(startDateTime.getTime()) : recurrence.next(lastOrder.getTime());
164                         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
165                         Timestamp JavaDoc nextOrder = UtilDateTime.getDayStart(UtilDateTime.getTimestamp(next));
166     
167                         if (nextOrder.after(now)) {
168                             continue;
169                         }
170                     } else {
171                         continue;
172                     }
173     
174                     ShoppingCart listCart = makeShoppingListCart(dispatcher, shoppingList, locale);
175                     CheckOutHelper helper = new CheckOutHelper(dispatcher, delegator, listCart);
176     
177                     // store the order
178
Map JavaDoc createResp = helper.createOrder(userLogin);
179                     if (createResp != null && ServiceUtil.isError(createResp)) {
180                         Debug.logError("Cannot create order for shopping list - " + shoppingList, module);
181                     } else {
182                         String JavaDoc orderId = (String JavaDoc) createResp.get("orderId");
183     
184                         // authorize the payments
185
Map JavaDoc payRes = null;
186                         try {
187                             payRes = helper.processPayment(ProductStoreWorker.getProductStore(listCart.getProductStoreId(), delegator), userLogin);
188                         } catch (GeneralException e) {
189                             Debug.logError(e, module);
190                         }
191     
192                         if (payRes != null && ServiceUtil.isError(payRes)) {
193                             Debug.logError("Payment processing problems with shopping list - " + shoppingList, module);
194                         }
195     
196                         shoppingList.set("lastOrderedDate", UtilDateTime.nowTimestamp());
197                         shoppingList.store();
198     
199                         // send notification
200
try {
201                             dispatcher.runAsync("sendOrderPayRetryNotification", UtilMisc.toMap("orderId", orderId));
202                         } catch (GenericServiceException e) {
203                             Debug.logError(e, module);
204                         }
205     
206                         // increment the recurrence
207
recurrence.incrementCurrentCount();
208                     }
209                 }
210     
211                 eli.close();
212             }
213             
214             return ServiceUtil.returnSuccess();
215         } catch (GenericEntityException e) {
216             try {
217                 // only rollback the transaction if we started one...
218
TransactionUtil.rollback(beganTransaction, "Error creating shopping list auto-reorders", e);
219             } catch (GenericEntityException e2) {
220                 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module);
221             }
222
223             String JavaDoc errMsg = "Error while creating new shopping list based automatic reorder" + e.toString();
224             Debug.logError(e, errMsg, module);
225             return ServiceUtil.returnError(errMsg);
226         } finally {
227             try {
228                 // only commit the transaction if we started one... this will throw an exception if it fails
229
TransactionUtil.commit(beganTransaction);
230             } catch (GenericEntityException e) {
231                 Debug.logError(e, "Could not commit transaction for creating new shopping list based automatic reorder", module);
232             }
233         }
234     }
235
236     public static Map JavaDoc splitShipmentMethodString(DispatchContext dctx, Map JavaDoc context) {
237         String JavaDoc shipmentMethodString = (String JavaDoc) context.get("shippingMethodString");
238         Map JavaDoc result = ServiceUtil.returnSuccess();
239
240         if (UtilValidate.isNotEmpty(shipmentMethodString)) {
241             int delimiterPos = shipmentMethodString.indexOf('@');
242             String JavaDoc shipmentMethodTypeId = null;
243             String JavaDoc carrierPartyId = null;
244
245             if (delimiterPos > 0) {
246                 shipmentMethodTypeId = shipmentMethodString.substring(0, delimiterPos);
247                 carrierPartyId = shipmentMethodString.substring(delimiterPos + 1);
248                 result.put("shipmentMethodTypeId", shipmentMethodTypeId);
249                 result.put("carrierPartyId", carrierPartyId);
250             }
251         }
252         return result;
253     }
254
255     public static Map JavaDoc makeListFromOrder(DispatchContext dctx, Map JavaDoc context) {
256         LocalDispatcher dispatcher = dctx.getDispatcher();
257         GenericDelegator delegator = dctx.getDelegator();
258
259         String JavaDoc shoppingListTypeId = (String JavaDoc) context.get("shoppingListTypeId");
260         String JavaDoc shoppingListId = (String JavaDoc) context.get("shoppingListId");
261         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
262         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
263
264         Timestamp JavaDoc startDate = (Timestamp JavaDoc) context.get("startDateTime");
265         Timestamp JavaDoc endDate = (Timestamp JavaDoc) context.get("endDateTime");
266         Integer JavaDoc frequency = (Integer JavaDoc) context.get("frequency");
267         Integer JavaDoc interval = (Integer JavaDoc) context.get("intervalNumber");
268
269         GenericValue userLogin = (GenericValue) context.get("userLogin");
270         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
271
272         boolean beganTransaction = false;
273         try {
274             beganTransaction = TransactionUtil.begin();
275         
276             GenericValue orderHeader = null;
277             orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
278     
279             if (orderHeader == null) {
280                 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToLocateOrder", UtilMisc.toMap("orderId",orderId), locale));
281             }
282             String JavaDoc productStoreId = orderHeader.getString("productStoreId");
283     
284             if (UtilValidate.isEmpty(shoppingListId)) {
285                 // create a new shopping list
286
if (partyId == null) {
287                     partyId = userLogin.getString("partyId");
288                 }
289     
290                 Map JavaDoc serviceCtx = UtilMisc.toMap("userLogin", userLogin, "partyId", partyId,
291                         "productStoreId", productStoreId, "listName", "List Created From Order #" + orderId);
292     
293                 if (UtilValidate.isNotEmpty(shoppingListTypeId)) {
294                     serviceCtx.put("shoppingListTypeId", shoppingListTypeId);
295                 }
296     
297                 Map JavaDoc newListResult = null;
298                 try {
299     
300                     newListResult = dispatcher.runSync("createShoppingList", serviceCtx);
301                 } catch (GenericServiceException e) {
302                     Debug.logError(e, "Problems creating new ShoppingList", module);
303                     return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToCreateNewShoppingList",locale));
304                 }
305     
306                 // check for errors
307
if (ServiceUtil.isError(newListResult)) {
308                     return ServiceUtil.returnError(ServiceUtil.getErrorMessage(newListResult));
309                 }
310     
311                 // get the new list id
312
if (newListResult != null) {
313                     shoppingListId = (String JavaDoc) newListResult.get("shoppingListId");
314                 }
315             }
316     
317             GenericValue shoppingList = null;
318             shoppingList = delegator.findByPrimaryKey("ShoppingList", UtilMisc.toMap("shoppingListId", shoppingListId));
319     
320             if (shoppingList == null) {
321                 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderNoShoppingListAvailable",locale));
322             }
323             shoppingListTypeId = shoppingList.getString("shoppingListTypeId");
324     
325             OrderReadHelper orh = new OrderReadHelper(orderHeader);
326             if (orh == null) {
327                 return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToLoadOrderReadHelper", UtilMisc.toMap("orderId",orderId), locale));
328             }
329     
330             List JavaDoc orderItems = orh.getOrderItems();
331             Iterator JavaDoc i = orderItems.iterator();
332             while (i.hasNext()) {
333                 GenericValue orderItem = (GenericValue) i.next();
334                 if (orderItem.get("productId") != null) {
335                     Map JavaDoc ctx = UtilMisc.toMap("userLogin", userLogin, "shoppingListId", shoppingListId, "productId",
336                             orderItem.get("productId"), "quantity", orderItem.get("quantity"));
337                     Map JavaDoc serviceResult = null;
338                     try {
339                         serviceResult = dispatcher.runSync("createShoppingListItem", ctx);
340                     } catch (GenericServiceException e) {
341                         Debug.logError(e, module);
342                     }
343                     if (serviceResult == null || ServiceUtil.isError(serviceResult)) {
344                         return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToAddItemToShoppingList",UtilMisc.toMap("shoppingListId",shoppingListId), locale));
345                     }
346                 }
347             }
348     
349             if ("SLT_AUTO_REODR".equals(shoppingListTypeId)) {
350                 GenericValue paymentPref = EntityUtil.getFirst(orh.getPaymentPreferences());
351                 GenericValue shipGroup = EntityUtil.getFirst(orh.getOrderItemShipGroups());
352     
353                 Map JavaDoc slCtx = new HashMap JavaDoc();
354                 slCtx.put("shipmentMethodTypeId", shipGroup.get("shipmentMethodTypeId"));
355                 slCtx.put("carrierRoleTypeId", shipGroup.get("carrierRoleTypeId"));
356                 slCtx.put("carrierPartyId", shipGroup.get("carrierPartyId"));
357                 slCtx.put("contactMechId", shipGroup.get("contactMechId"));
358                 slCtx.put("paymentMethodId", paymentPref.get("paymentMethodId"));
359                 slCtx.put("currencyUom", orh.getCurrency());
360                 slCtx.put("startDateTime", startDate);
361                 slCtx.put("endDateTime", endDate);
362                 slCtx.put("frequency", frequency);
363                 slCtx.put("intervalNumber", interval);
364                 slCtx.put("isActive", "Y");
365                 slCtx.put("shoppingListId", shoppingListId);
366                 slCtx.put("userLogin", userLogin);
367     
368                 Map JavaDoc slUpResp = null;
369                 try {
370                     slUpResp = dispatcher.runSync("updateShoppingList", slCtx);
371                 } catch (GenericServiceException e) {
372                     Debug.logError(e, module);
373                 }
374     
375                 if (slUpResp == null || ServiceUtil.isError(slUpResp)) {
376                     return ServiceUtil.returnError(UtilProperties.getMessage(resource_error,"OrderUnableToUpdateShoppingListInformation",UtilMisc.toMap("shoppingListId",shoppingListId), locale));
377                 }
378             }
379     
380             Map JavaDoc result = ServiceUtil.returnSuccess();
381             result.put("shoppingListId", shoppingListId);
382             return result;
383             
384         } catch (GenericEntityException e) {
385             try {
386                 // only rollback the transaction if we started one...
387
TransactionUtil.rollback(beganTransaction, "Error making shopping list from order", e);
388             } catch (GenericEntityException e2) {
389                 Debug.logError(e2, "[GenericDelegator] Could not rollback transaction: " + e2.toString(), module);
390             }
391
392             String JavaDoc errMsg = "Error while creating new shopping list based on order" + e.toString();
393             Debug.logError(e, errMsg, module);
394             return ServiceUtil.returnError(errMsg);
395         } finally {
396             try {
397                 // only commit the transaction if we started one... this will throw an exception if it fails
398
TransactionUtil.commit(beganTransaction);
399             } catch (GenericEntityException e) {
400                 Debug.logError(e, "Could not commit transaction for creating new shopping list based on order", module);
401             }
402         }
403     }
404
405     public static ShoppingCart makeShoppingListCart(LocalDispatcher dispatcher, GenericValue shoppingList, Locale JavaDoc locale) {
406         GenericDelegator delegator = dispatcher.getDelegator();
407         ShoppingCart listCart = null;
408         if (shoppingList != null && shoppingList.get("productStoreId") != null) {
409             String JavaDoc productStoreId = shoppingList.getString("productStoreId");
410             String JavaDoc currencyUom = shoppingList.getString("currencyUom");
411             if (currencyUom == null) {
412                 GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
413                 if (productStore == null) {
414                     return null;
415                 }
416                 currencyUom = productStore.getString("defaultCurrencyUomId");
417             }
418             if (locale == null) {
419                 locale = Locale.getDefault();
420             }
421
422             List JavaDoc items = null;
423             try {
424                 items = shoppingList.getRelated("ShoppingListItem", UtilMisc.toList("shoppingListItemSeqId"));
425             } catch (GenericEntityException e) {
426                 Debug.logError(e, module);
427             }
428
429             if (UtilValidate.isNotEmpty(items)) {
430                 listCart = new ShoppingCart(delegator, productStoreId, locale, currencyUom);
431                 listCart.setOrderPartyId(shoppingList.getString("partyId"));
432
433                 Iterator JavaDoc i = items.iterator();
434                 while (i.hasNext()) {
435                     GenericValue shoppingListItem = (GenericValue) i.next();
436                     String JavaDoc productId = shoppingListItem.getString("productId");
437                     Double JavaDoc quantity = shoppingListItem.getDouble("quantity");
438                     Timestamp JavaDoc reservStart = shoppingListItem.getTimestamp("reservStart");
439                     double reservLength = 0.00;
440                     if (shoppingListItem.get("reservLength") != null)
441                         reservLength = shoppingListItem.getDouble("reservLength").doubleValue();
442                     double reservPersons = 0.00;
443                     if (shoppingListItem.get("reservPersons") != null)
444                         reservPersons = shoppingListItem.getDouble("reservPersons").doubleValue();
445                     if (UtilValidate.isNotEmpty(productId) && quantity != null) {
446                         // list items are noted in the shopping cart
447
String JavaDoc listId = shoppingListItem.getString("shoppingListId");
448                         String JavaDoc itemId = shoppingListItem.getString("shoppingListItemSeqId");
449                         Map JavaDoc attributes = UtilMisc.toMap("shoppingListId", listId, "shoppingListItemSeqId", itemId);
450
451                         try {
452                             listCart.addOrIncreaseItem(productId, quantity.doubleValue(), reservStart, reservLength, reservPersons, null, attributes, null, dispatcher);
453                         } catch (CartItemModifyException e) {
454                             Debug.logError(e, "Unable to add product to List Cart - " + productId, module);
455                         } catch (ItemNotFoundException e) {
456                             Debug.logError(e, "Product not found - " + productId, module);
457                         }
458                     }
459                 }
460
461                 if (listCart.size() > 0) {
462                     if (shoppingList.get("paymentMethodId") != null) {
463                         listCart.addPayment(shoppingList.getString("paymentMethodId"));
464                     }
465                     if (shoppingList.get("contactMechId") != null) {
466                         listCart.setShippingContactMechId(0, shoppingList.getString("contactMechId"));
467                     }
468                     if (shoppingList.get("shipmentMethodTypeId") != null) {
469                         listCart.setShipmentMethodTypeId(0, shoppingList.getString("shipmentMethodTypeId"));
470                     }
471                     if (shoppingList.get("carrierPartyId") != null) {
472                         listCart.setCarrierPartyId(0, shoppingList.getString("carrierPartyId"));
473                     }
474                 }
475             }
476         }
477         return listCart;
478     }
479
480     public static ShoppingCart makeShoppingListCart(LocalDispatcher dispatcher, String JavaDoc shoppingListId, Locale JavaDoc locale) {
481         GenericDelegator delegator = dispatcher.getDelegator();
482         GenericValue shoppingList = null;
483         try {
484             shoppingList = delegator.findByPrimaryKey("ShoppingList", UtilMisc.toMap("shoppingListId", shoppingListId));
485         } catch (GenericEntityException e) {
486             Debug.logError(e, module);
487         }
488         return makeShoppingListCart(dispatcher, shoppingList, locale);
489     }
490
491     /**
492      *
493      * Given an orderId, this service will look through all its OrderItems and for each shoppingListItemId
494      * and shoppingListItemSeqId, update the quantity purchased in the ShoppingListItem entity. Used for
495      * tracking how many of shopping list items are purchased. This service is mounted as a seca on storeOrder.
496      *
497      * @param ctx - The DispatchContext that this service is operating in
498      * @param context - Map containing the input parameters
499      * @return Map with the result of the service, the output parameters
500      */

501     public static Map JavaDoc updateShoppingListQuantitiesFromOrder(DispatchContext ctx, Map JavaDoc context) {
502         Map JavaDoc result = new HashMap JavaDoc();
503         GenericDelegator delegator = ctx.getDelegator();
504         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
505         try {
506             List JavaDoc orderItems = delegator.findByAnd("OrderItem", UtilMisc.toMap("orderId", orderId));
507             Iterator JavaDoc iter = orderItems.iterator();
508             while (iter.hasNext()) {
509                 GenericValue orderItem = (GenericValue) iter.next();
510                 String JavaDoc shoppingListId = (String JavaDoc) orderItem.getString("shoppingListId");
511                 String JavaDoc shoppingListItemSeqId = (String JavaDoc) orderItem.getString("shoppingListItemSeqId");
512                 if ((shoppingListId != null) && (shoppingListId.length() > 0)) {
513                     GenericValue shoppingListItem=delegator.findByPrimaryKey("ShoppingListItem", UtilMisc.toMap("shoppingListId",
514                                 shoppingListId, "shoppingListItemSeqId", shoppingListItemSeqId));
515                     if (shoppingListItem != null) {
516                         Double JavaDoc quantityPurchased = shoppingListItem.getDouble("quantityPurchased");
517                         Double JavaDoc orderQuantity = orderItem.getDouble("quantity");
518                         if (quantityPurchased != null) {
519                             shoppingListItem.set("quantityPurchased", new Double JavaDoc(orderQuantity.doubleValue() + quantityPurchased.doubleValue()));
520                         }else{
521                             shoppingListItem.set("quantityPurchased", orderQuantity);
522                         }
523                         shoppingListItem.store();
524                     }
525                 }
526             }
527         } catch (Exception JavaDoc e) {
528             Debug.log("updateShoppingListQuantitiesFromOrder error:"+e.getMessage());
529         }
530         return result;
531     }
532 }
533
Popular Tags