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.sql.Timestamp;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.http.HttpSession;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.GeneralException;
035: import org.ofbiz.base.util.UtilDateTime;
036: import org.ofbiz.base.util.UtilHttp;
037: import org.ofbiz.base.util.UtilMisc;
038: import org.ofbiz.base.util.UtilProperties;
039: import org.ofbiz.base.util.UtilValidate;
040: import org.ofbiz.entity.GenericDelegator;
041: import org.ofbiz.entity.GenericEntityException;
042: import org.ofbiz.entity.GenericValue;
043: import org.ofbiz.entity.util.EntityUtil;
044: import org.ofbiz.order.shoppingcart.CartItemModifyException;
045: import org.ofbiz.order.shoppingcart.ItemNotFoundException;
046: import org.ofbiz.order.shoppingcart.ShoppingCart;
047: import org.ofbiz.order.shoppingcart.ShoppingCartEvents;
048: import org.ofbiz.order.shoppingcart.ShoppingCartItem;
049: import org.ofbiz.product.catalog.CatalogWorker;
050: import org.ofbiz.product.store.ProductStoreWorker;
051: import org.ofbiz.service.GenericServiceException;
052: import org.ofbiz.service.LocalDispatcher;
053: import org.ofbiz.service.ModelService;
054: import org.ofbiz.service.ServiceUtil;
055:
056: /**
057: * Shopping cart events.
058: */
059: public class ShoppingListEvents {
060:
061: public static final String module = ShoppingListEvents.class
062: .getName();
063: public static final String resource = "OrderUiLabels";
064: public static final String err_resource = "OrderErrorUiLabel";
065: public static final String resource_error = "OrderErrorUiLabels";
066: public static final String PERSISTANT_LIST_NAME = "auto-save";
067:
068: public static String addBulkFromCart(HttpServletRequest request,
069: HttpServletResponse response) {
070: GenericDelegator delegator = (GenericDelegator) request
071: .getAttribute("delegator");
072: LocalDispatcher dispatcher = (LocalDispatcher) request
073: .getAttribute("dispatcher");
074: ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
075: GenericValue userLogin = (GenericValue) request.getSession()
076: .getAttribute("userLogin");
077:
078: String shoppingListId = request.getParameter("shoppingListId");
079: String selectedCartItems[] = request
080: .getParameterValues("selectedItem");
081:
082: try {
083: shoppingListId = addBulkFromCart(delegator, dispatcher,
084: cart, userLogin, shoppingListId, selectedCartItems,
085: true, true);
086: } catch (IllegalArgumentException e) {
087: request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
088: return "error";
089: }
090:
091: request.setAttribute("shoppingListId", shoppingListId);
092: return "success";
093: }
094:
095: public static String addBulkFromCart(GenericDelegator delegator,
096: LocalDispatcher dispatcher, ShoppingCart cart,
097: GenericValue userLogin, String shoppingListId,
098: String[] items, boolean allowPromo, boolean append)
099: throws IllegalArgumentException {
100: String errMsg = null;
101:
102: if (items == null || items.length == 0) {
103: errMsg = UtilProperties.getMessage(resource,
104: "shoppinglistevents.select_items_to_add_to_list",
105: cart.getLocale());
106: throw new IllegalArgumentException(errMsg);
107: }
108:
109: if (UtilValidate.isEmpty(shoppingListId)) {
110: // create a new shopping list
111: Map newListResult = null;
112: try {
113: newListResult = dispatcher.runSync(
114: "createShoppingList", UtilMisc.toMap(
115: "userLogin", userLogin,
116: "productStoreId", cart
117: .getProductStoreId(),
118: "partyId", cart.getOrderPartyId(),
119: "currencyUom", cart.getCurrency()));
120: } catch (GenericServiceException e) {
121: Debug.logError(e, "Problems creating new ShoppingList",
122: module);
123: errMsg = UtilProperties
124: .getMessage(
125: resource,
126: "shoppinglistevents.cannot_create_new_shopping_list",
127: cart.getLocale());
128: throw new IllegalArgumentException(errMsg);
129: }
130:
131: // check for errors
132: if (ServiceUtil.isError(newListResult)) {
133: throw new IllegalArgumentException(ServiceUtil
134: .getErrorMessage(newListResult));
135: }
136:
137: // get the new list id
138: if (newListResult != null) {
139: shoppingListId = (String) newListResult
140: .get("shoppingListId");
141: }
142:
143: // if no list was created throw an error
144: if (shoppingListId == null || shoppingListId.equals("")) {
145: errMsg = UtilProperties
146: .getMessage(
147: resource,
148: "shoppinglistevents.shoppingListId_is_required_parameter",
149: cart.getLocale());
150: throw new IllegalArgumentException(errMsg);
151: }
152: } else if (!append) {
153: try {
154: clearListInfo(delegator, shoppingListId);
155: } catch (GenericEntityException e) {
156: Debug.logError(e, module);
157: throw new IllegalArgumentException(
158: "Could not clear current shopping list: "
159: + e.toString());
160: }
161: }
162:
163: for (int i = 0; i < items.length; i++) {
164: Integer cartIdInt = null;
165: try {
166: cartIdInt = new Integer(items[i]);
167: } catch (Exception e) {
168: Debug.logWarning(e, UtilProperties.getMessage(
169: resource_error,
170: "OrderIllegalCharacterInSelectedItemField",
171: cart.getLocale()), module);
172: }
173: if (cartIdInt != null) {
174: ShoppingCartItem item = cart.findCartItem(cartIdInt
175: .intValue());
176: if (allowPromo || !item.getIsPromo()) {
177: Debug.logInfo("Adding cart item to shopping list ["
178: + shoppingListId + "], allowPromo="
179: + allowPromo + ", item.getIsPromo()="
180: + item.getIsPromo()
181: + ", item.getProductId()="
182: + item.getProductId()
183: + ", item.getQuantity()="
184: + item.getQuantity(), module);
185: Map serviceResult = null;
186: try {
187: Map ctx = UtilMisc.toMap("userLogin",
188: userLogin, "shoppingListId",
189: shoppingListId, "productId", item
190: .getProductId(), "quantity",
191: new Double(item.getQuantity()));
192: ctx.put("reservStart", item.getReservStart());
193: ctx.put("reservLength", new Double(item
194: .getReservLength()));
195: ctx.put("reservPersons", new Double(item
196: .getReservPersons()));
197: serviceResult = dispatcher.runSync(
198: "createShoppingListItem", ctx);
199: } catch (GenericServiceException e) {
200: Debug
201: .logError(
202: e,
203: "Problems creating ShoppingList item entity",
204: module);
205: errMsg = UtilProperties
206: .getMessage(
207: resource,
208: "shoppinglistevents.error_adding_item_to_shopping_list",
209: cart.getLocale());
210: throw new IllegalArgumentException(errMsg);
211: }
212:
213: // check for errors
214: if (ServiceUtil.isError(serviceResult)) {
215: throw new IllegalArgumentException(ServiceUtil
216: .getErrorMessage(serviceResult));
217: }
218: }
219: }
220: }
221:
222: // return the shoppinglist id
223: return shoppingListId;
224: }
225:
226: public static String addListToCart(HttpServletRequest request,
227: HttpServletResponse response) {
228: GenericDelegator delegator = (GenericDelegator) request
229: .getAttribute("delegator");
230: LocalDispatcher dispatcher = (LocalDispatcher) request
231: .getAttribute("dispatcher");
232: ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
233:
234: String shoppingListId = request.getParameter("shoppingListId");
235: String includeChild = request.getParameter("includeChild");
236: String prodCatalogId = CatalogWorker
237: .getCurrentCatalogId(request);
238:
239: String eventMessage = null;
240: try {
241: addListToCart(delegator, dispatcher, cart, prodCatalogId,
242: shoppingListId, (includeChild != null), true, true);
243: } catch (IllegalArgumentException e) {
244: request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
245: return "error";
246: }
247:
248: if (eventMessage != null && eventMessage.length() > 0) {
249: request.setAttribute("_EVENT_MESSAGE_", eventMessage);
250: }
251:
252: return "success";
253: }
254:
255: public static String addListToCart(GenericDelegator delegator,
256: LocalDispatcher dispatcher, ShoppingCart cart,
257: String prodCatalogId, String shoppingListId,
258: boolean includeChild, boolean setAsListItem, boolean append)
259: throws java.lang.IllegalArgumentException {
260: String errMsg = null;
261:
262: // no list; no add
263: if (shoppingListId == null) {
264: errMsg = UtilProperties.getMessage(resource,
265: "shoppinglistevents.choose_shopping_list", cart
266: .getLocale());
267: throw new IllegalArgumentException(errMsg);
268: }
269:
270: // get the shopping list
271: GenericValue shoppingList = null;
272: List shoppingListItems = null;
273: try {
274: shoppingList = delegator.findByPrimaryKey("ShoppingList",
275: UtilMisc.toMap("shoppingListId", shoppingListId));
276: if (shoppingList == null) {
277: errMsg = UtilProperties
278: .getMessage(
279: resource,
280: "shoppinglistevents.error_getting_shopping_list_and_items",
281: cart.getLocale());
282: throw new IllegalArgumentException(errMsg);
283: }
284:
285: shoppingListItems = shoppingList
286: .getRelated("ShoppingListItem");
287: if (shoppingListItems == null) {
288: shoppingListItems = new LinkedList();
289: }
290:
291: // include all items of child lists if flagged to do so
292: if (includeChild) {
293: List childShoppingLists = shoppingList
294: .getRelated("ChildShoppingList");
295: Iterator ci = childShoppingLists.iterator();
296: while (ci.hasNext()) {
297: GenericValue v = (GenericValue) ci.next();
298: List items = v.getRelated("ShoppingListItem");
299: shoppingListItems.addAll(items);
300: }
301: }
302:
303: } catch (GenericEntityException e) {
304: Debug
305: .logError(
306: e,
307: "Problems getting ShoppingList and ShoppingListItem records",
308: module);
309: errMsg = UtilProperties
310: .getMessage(
311: resource,
312: "shoppinglistevents.error_getting_shopping_list_and_items",
313: cart.getLocale());
314: throw new IllegalArgumentException(errMsg);
315: }
316:
317: // no items; not an error; just mention that nothing was added
318: if (shoppingListItems == null || shoppingListItems.size() == 0) {
319: errMsg = UtilProperties.getMessage(resource,
320: "shoppinglistevents.no_items_added", cart
321: .getLocale());
322: return errMsg;
323: }
324:
325: // check if we are to clear the cart first
326: if (!append) {
327: cart.clear();
328: }
329:
330: // get the survey info for all the items
331: Map shoppingListSurveyInfo = getItemSurveyInfos(shoppingListItems);
332:
333: // add the items
334: StringBuffer eventMessage = new StringBuffer();
335: Iterator i = shoppingListItems.iterator();
336: while (i.hasNext()) {
337: GenericValue shoppingListItem = (GenericValue) i.next();
338: String productId = shoppingListItem.getString("productId");
339: Double quantity = shoppingListItem.getDouble("quantity");
340: Timestamp reservStart = shoppingListItem
341: .getTimestamp("reservStart");
342: Double reservLength = shoppingListItem
343: .getDouble("reservLength");
344: Double reservPersons = shoppingListItem
345: .getDouble("reservPersons");
346: try {
347: String listId = shoppingListItem
348: .getString("shoppingListId");
349: String itemId = shoppingListItem
350: .getString("shoppingListItemSeqId");
351:
352: Map attributes = new HashMap();
353: // list items are noted in the shopping cart
354: if (setAsListItem) {
355: attributes.put("shoppingListId", listId);
356: attributes.put("shoppingListItemSeqId", itemId);
357: }
358:
359: // check if we have existing survey responses to append
360: if (shoppingListSurveyInfo.containsKey(listId + "."
361: + itemId)) {
362: attributes.put("surveyResponses",
363: shoppingListSurveyInfo.get(listId + "."
364: + itemId));
365: }
366:
367: // TODO: add code to check for survey response requirement
368:
369: // i cannot get the addOrDecrease function to accept a null reservStart field: i get a null pointer exception a null constant works....
370: if (reservStart == null) {
371: cart.addOrIncreaseItem(productId, null, quantity
372: .doubleValue(), null, null, null, null,
373: null, null, attributes, prodCatalogId,
374: null, null, null, null, dispatcher);
375: } else {
376: cart.addOrIncreaseItem(productId, null, quantity
377: .doubleValue(), reservStart, reservLength,
378: reservPersons, null, null, null,
379: attributes, prodCatalogId, null, null,
380: null, null, dispatcher);
381: }
382: Map messageMap = UtilMisc.toMap("productId", productId);
383: errMsg = UtilProperties.getMessage(resource,
384: "shoppinglistevents.added_product_to_cart",
385: messageMap, cart.getLocale());
386: eventMessage.append(errMsg + "\n");
387: } catch (CartItemModifyException e) {
388: Debug.logWarning(e, UtilProperties.getMessage(
389: resource_error,
390: "OrderProblemsAddingItemFromListToCart", cart
391: .getLocale()));
392: Map messageMap = UtilMisc.toMap("productId", productId);
393: errMsg = UtilProperties
394: .getMessage(
395: resource,
396: "shoppinglistevents.problem_adding_product_to_cart",
397: messageMap, cart.getLocale());
398: eventMessage.append(errMsg + "\n");
399: } catch (ItemNotFoundException e) {
400: Debug.logWarning(e, UtilProperties.getMessage(
401: resource_error, "OrderProductNotFound", cart
402: .getLocale()));
403: Map messageMap = UtilMisc.toMap("productId", productId);
404: errMsg = UtilProperties
405: .getMessage(
406: resource,
407: "shoppinglistevents.problem_adding_product_to_cart",
408: messageMap, cart.getLocale());
409: eventMessage.append(errMsg + "\n");
410: }
411: }
412:
413: if (eventMessage.length() > 0) {
414: return eventMessage.toString();
415: }
416:
417: // all done
418: return ""; // no message to return; will simply reply as success
419: }
420:
421: public static String replaceShoppingListItem(
422: HttpServletRequest request, HttpServletResponse response) {
423: String quantityStr = request.getParameter("quantity");
424:
425: // just call the updateShoppingListItem service
426: LocalDispatcher dispatcher = (LocalDispatcher) request
427: .getAttribute("dispatcher");
428: GenericValue userLogin = (GenericValue) request.getSession()
429: .getAttribute("userLogin");
430: Locale locale = UtilHttp.getLocale(request);
431:
432: Double quantity = null;
433: try {
434: quantity = Double.valueOf(quantityStr);
435: } catch (Exception e) {
436: // do nothing, just won't pass to service if it is null
437: }
438:
439: Map serviceInMap = new HashMap();
440: serviceInMap.put("shoppingListId", request
441: .getParameter("shoppingListId"));
442: serviceInMap.put("shoppingListItemSeqId", request
443: .getParameter("shoppingListItemSeqId"));
444: serviceInMap.put("productId", request
445: .getParameter("add_product_id"));
446: serviceInMap.put("userLogin", userLogin);
447: if (quantity != null)
448: serviceInMap.put("quantity", quantity);
449: Map result = null;
450: try {
451: result = dispatcher.runSync("updateShoppingListItem",
452: serviceInMap);
453: } catch (GenericServiceException e) {
454: String errMsg = UtilProperties.getMessage(
455: ShoppingListEvents.err_resource,
456: "shoppingListEvents.error_calling_update", locale)
457: + ": " + e.toString();
458: request.setAttribute("_ERROR_MESSAGE_", errMsg);
459: String errorMsg = "Error calling the updateShoppingListItem in handleShoppingListItemVariant: "
460: + e.toString();
461: Debug.logError(e, errorMsg, module);
462: return "error";
463: }
464:
465: ServiceUtil.getMessages(request, result, "", "", "", "", "",
466: "", "");
467: if ("error".equals(result.get(ModelService.RESPONSE_MESSAGE))) {
468: return "error";
469: } else {
470: return "success";
471: }
472: }
473:
474: /**
475: * Finds or creates a specialized (auto-save) shopping list used to record shopping bag contents between user visits.
476: */
477: public static String getAutoSaveListId(GenericDelegator delegator,
478: LocalDispatcher dispatcher, String partyId,
479: GenericValue userLogin, String productStoreId)
480: throws GenericEntityException, GenericServiceException {
481: if (partyId == null && userLogin != null) {
482: partyId = userLogin.getString("partyId");
483: }
484:
485: String autoSaveListId = null;
486: // TODO: add sorting, just in case there are multiple...
487: Map findMap = UtilMisc.toMap("partyId", partyId,
488: "productStoreId", productStoreId, "shoppingListTypeId",
489: "SLT_SPEC_PURP", "listName", PERSISTANT_LIST_NAME);
490: List existingLists = delegator.findByAnd("ShoppingList",
491: findMap);
492: Debug
493: .logInfo(
494: "Finding existing auto-save shopping list with: \nfindMap: "
495: + findMap + "\nlists: " + existingLists,
496: module);
497:
498: GenericValue list = null;
499: if (existingLists != null && !existingLists.isEmpty()) {
500: list = EntityUtil.getFirst(existingLists);
501: autoSaveListId = list.getString("shoppingListId");
502: }
503:
504: if (list == null && dispatcher != null && userLogin != null) {
505: Map listFields = UtilMisc.toMap("userLogin", userLogin,
506: "productStoreId", productStoreId,
507: "shoppingListTypeId", "SLT_SPEC_PURP", "listName",
508: PERSISTANT_LIST_NAME);
509: Map newListResult = dispatcher.runSync(
510: "createShoppingList", listFields);
511:
512: if (newListResult != null) {
513: autoSaveListId = (String) newListResult
514: .get("shoppingListId");
515: }
516: }
517:
518: return autoSaveListId;
519: }
520:
521: /**
522: * Fills the specialized shopping list with the current shopping cart if one exists (if not leaves it alone)
523: */
524: public static void fillAutoSaveList(ShoppingCart cart,
525: LocalDispatcher dispatcher) throws GeneralException {
526: if (cart != null && dispatcher != null) {
527: GenericValue userLogin = ShoppingListEvents
528: .getCartUserLogin(cart);
529: if (userLogin == null)
530: return; //only save carts when a user is logged in....
531: GenericDelegator delegator = cart.getDelegator();
532: String autoSaveListId = getAutoSaveListId(delegator,
533: dispatcher, null, userLogin, cart
534: .getProductStoreId());
535:
536: try {
537: String[] itemsArray = makeCartItemsArray(cart);
538: if (itemsArray != null && itemsArray.length != 0) {
539: addBulkFromCart(delegator, dispatcher, cart,
540: userLogin, autoSaveListId, itemsArray,
541: false, false);
542: }
543: } catch (IllegalArgumentException e) {
544: throw new GeneralException(e.getMessage(), e);
545: }
546: }
547: }
548:
549: /**
550: * Saves the shopping cart to the specialized (auto-save) shopping list
551: */
552: public static String saveCartToAutoSaveList(
553: HttpServletRequest request, HttpServletResponse response) {
554: LocalDispatcher dispatcher = (LocalDispatcher) request
555: .getAttribute("dispatcher");
556: ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
557:
558: try {
559: fillAutoSaveList(cart, dispatcher);
560: } catch (GeneralException e) {
561: Debug.logError(e,
562: "Error saving the cart to the auto-save list: "
563: + e.toString(), module);
564: }
565:
566: return "success";
567: }
568:
569: /**
570: * Restores the specialized (auto-save) shopping list back into the shopping cart
571: */
572: public static String restoreAutoSaveList(
573: HttpServletRequest request, HttpServletResponse response) {
574: GenericDelegator delegator = (GenericDelegator) request
575: .getAttribute("delegator");
576: LocalDispatcher dispatcher = (LocalDispatcher) request
577: .getAttribute("dispatcher");
578: GenericValue productStore = ProductStoreWorker
579: .getProductStore(request);
580:
581: if (!ProductStoreWorker.autoSaveCart(productStore)) {
582: // if auto-save is disabled just return here
583: return "success";
584: }
585:
586: HttpSession session = request.getSession();
587: ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
588:
589: // safety check for missing required parameter.
590: if (cart.getWebSiteId() == null) {
591: cart.setWebSiteId(CatalogWorker.getWebSiteId(request));
592: }
593:
594: // locate the user's identity
595: GenericValue userLogin = (GenericValue) session
596: .getAttribute("userLogin");
597: if (userLogin == null) {
598: userLogin = (GenericValue) session
599: .getAttribute("autoUserLogin");
600: }
601:
602: if (userLogin == null) {
603: // not logged in; cannot identify the user
604: return "success";
605: }
606:
607: // find the list ID
608: String autoSaveListId = cart.getAutoSaveListId();
609: if (autoSaveListId == null) {
610: try {
611: autoSaveListId = getAutoSaveListId(delegator,
612: dispatcher, null, userLogin, cart
613: .getProductStoreId());
614: } catch (GeneralException e) {
615: Debug.logError(e, module);
616: }
617: cart.setAutoSaveListId(autoSaveListId);
618: }
619:
620: // check to see if we are okay to load this list
621: java.sql.Timestamp lastLoad = cart.getLastListRestore();
622: boolean okayToLoad = autoSaveListId == null ? false
623: : (lastLoad == null ? true : false);
624: if (!okayToLoad && lastLoad != null) {
625: GenericValue shoppingList = null;
626: try {
627: shoppingList = delegator.findByPrimaryKey(
628: "ShoppingList", UtilMisc.toMap(
629: "shoppingListId", autoSaveListId));
630: } catch (GenericEntityException e) {
631: Debug.logError(e, module);
632: }
633: if (shoppingList != null) {
634: java.sql.Timestamp lastModified = shoppingList
635: .getTimestamp("lastAdminModified");
636: if (lastModified != null) {
637: if (lastModified.after(lastLoad)) {
638: okayToLoad = true;
639: }
640: if (cart.size() == 0
641: && lastModified.after(cart
642: .getCartCreatedTime())) {
643: okayToLoad = true;
644: }
645: }
646: }
647: }
648:
649: // load (restore) the list of we have determined it is okay to load
650: if (okayToLoad) {
651: String prodCatalogId = CatalogWorker
652: .getCurrentCatalogId(request);
653: try {
654: addListToCart(delegator, dispatcher, cart,
655: prodCatalogId, autoSaveListId, false, false,
656: false);
657: cart.setLastListRestore(UtilDateTime.nowTimestamp());
658: } catch (IllegalArgumentException e) {
659: Debug.logError(e, module);
660: }
661: }
662:
663: return "success";
664: }
665:
666: /**
667: * Remove all items from the given list.
668: */
669: public static int clearListInfo(GenericDelegator delegator,
670: String shoppingListId) throws GenericEntityException {
671: // remove the survey responses first
672: delegator.removeByAnd("ShoppingListItemSurvey", UtilMisc.toMap(
673: "shoppingListId", shoppingListId));
674:
675: // next remove the items
676: return delegator.removeByAnd("ShoppingListItem", UtilMisc
677: .toMap("shoppingListId", shoppingListId));
678: }
679:
680: /**
681: * Creates records for survey responses on survey items
682: */
683: public static int makeListItemSurveyResp(
684: GenericDelegator delegator, GenericValue item,
685: List surveyResps) throws GenericEntityException {
686: if (surveyResps != null && surveyResps.size() > 0) {
687: Iterator i = surveyResps.iterator();
688: int count = 0;
689: while (i.hasNext()) {
690: String responseId = (String) i.next();
691: GenericValue listResp = delegator.makeValue(
692: "ShoppingListItemSurvey", null);
693: listResp.set("shoppingListId", item
694: .getString("shoppingListId"));
695: listResp.set("shoppingListItemSeqId", item
696: .getString("shoppingListItemSeqId"));
697: listResp.set("surveyResponseId", responseId);
698: delegator.create(listResp);
699: count++;
700: }
701: return count;
702: }
703: return -1;
704: }
705:
706: /**
707: * Returns Map keyed on item sequence ID containing a list of survey response IDs
708: */
709: public static Map getItemSurveyInfos(List items) {
710: Map surveyInfos = new HashMap();
711: if (items != null && items.size() > 0) {
712: Iterator itemIt = items.iterator();
713: while (itemIt.hasNext()) {
714: GenericValue item = (GenericValue) itemIt.next();
715: String listId = item.getString("shoppingListId");
716: String itemId = item.getString("shoppingListItemSeqId");
717: surveyInfos.put(listId + "." + itemId,
718: getItemSurveyInfo(item));
719: }
720: }
721:
722: return surveyInfos;
723: }
724:
725: /**
726: * Returns a list of survey response IDs for a shopping list item
727: */
728: public static List getItemSurveyInfo(GenericValue item) {
729: List responseIds = new ArrayList();
730: List surveyResp = null;
731: try {
732: surveyResp = item.getRelated("ShoppingListItemSurvey");
733: } catch (GenericEntityException e) {
734: Debug.logError(e, module);
735: }
736:
737: if (surveyResp != null && surveyResp.size() > 0) {
738: Iterator respIt = surveyResp.iterator();
739: while (respIt.hasNext()) {
740: GenericValue resp = (GenericValue) respIt.next();
741: responseIds.add(resp.getString("surveyResponseId"));
742: }
743: }
744:
745: return responseIds;
746: }
747:
748: private static GenericValue getCartUserLogin(ShoppingCart cart) {
749: GenericValue ul = cart.getUserLogin();
750: if (ul == null) {
751: ul = cart.getAutoUserLogin();
752: }
753: return ul;
754: }
755:
756: private static String[] makeCartItemsArray(ShoppingCart cart) {
757: int len = cart.size();
758: String[] arr = new String[len];
759: for (int i = 0; i < len; i++) {
760: arr[i] = Integer.toString(i);
761: }
762: return arr;
763: }
764: }
|