KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > order > shoppingcart > shipping > ShippingEvents


1 /*
2  * $Id: ShippingEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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 package org.ofbiz.order.shoppingcart.shipping;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.GeneralException;
35 import org.ofbiz.base.util.UtilValidate;
36 import org.ofbiz.entity.GenericDelegator;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.order.order.OrderReadHelper;
39 import org.ofbiz.order.shoppingcart.ShoppingCart;
40 import org.ofbiz.product.store.ProductStoreWorker;
41 import org.ofbiz.service.GenericServiceException;
42 import org.ofbiz.service.LocalDispatcher;
43 import org.ofbiz.service.ModelService;
44 import org.ofbiz.service.ServiceUtil;
45 import org.ofbiz.base.util.UtilMisc;
46
47 /**
48  * ShippingEvents - Events used for processing shipping fees
49  *
50  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
51  * @version $Rev: 5462 $
52  * @since 2.0
53  */

54 public class ShippingEvents {
55
56     public static final String JavaDoc module = ShippingEvents.class.getName();
57
58     public static String JavaDoc getShipEstimate(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
59         ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");
60         LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
61         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
62
63         int shipGroups = cart.getShipGroupSize();
64         for (int i = 0; i < shipGroups; i++) {
65             Map JavaDoc result = getShipGroupEstimate(dispatcher, delegator, cart, i);
66             ServiceUtil.getMessages(request, result, null, "", "", "", "", null, null);
67             if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) {
68                 return "error";
69             }
70
71             Double JavaDoc shippingTotal = (Double JavaDoc) result.get("shippingTotal");
72             if (shippingTotal == null) {
73                 shippingTotal = new Double JavaDoc(0.00);
74             }
75             cart.setItemShipGroupEstimate(shippingTotal.doubleValue(), i);
76         }
77
78         // all done
79
return "success";
80     }
81
82     public static Map JavaDoc getShipGroupEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, ShoppingCart cart, int groupNo) {
83         // check for shippable items
84
if (!cart.shippingApplies()) {
85             Map JavaDoc responseResult = ServiceUtil.returnSuccess();
86             responseResult.put("shippingTotal", new Double JavaDoc(0.00));
87             return responseResult;
88         }
89
90         String JavaDoc shipmentMethodTypeId = cart.getShipmentMethodTypeId(groupNo);
91         String JavaDoc carrierPartyId = cart.getCarrierPartyId(groupNo);
92
93         return getShipGroupEstimate(dispatcher, delegator, cart.getOrderType(), shipmentMethodTypeId, carrierPartyId, null,
94                 cart.getShippingContactMechId(groupNo), cart.getProductStoreId(), cart.getShippableItemInfo(groupNo),
95                 cart.getShippableWeight(groupNo), cart.getShippableQuantity(groupNo), cart.getShippableTotal(groupNo));
96     }
97
98     public static Map JavaDoc getShipEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, OrderReadHelper orh, String JavaDoc shipGroupSeqId) {
99         // check for shippable items
100
if (!orh.shippingApplies()) {
101             Map JavaDoc responseResult = ServiceUtil.returnSuccess();
102             responseResult.put("shippingTotal", new Double JavaDoc(0.00));
103             return responseResult;
104         }
105
106         GenericValue shipGroup = orh.getOrderItemShipGroup(shipGroupSeqId);
107         String JavaDoc shipmentMethodTypeId = shipGroup.getString("shipmentMethodTypeId");
108         String JavaDoc carrierRoleTypeId = shipGroup.getString("carrierRoleTypeId");
109         String JavaDoc carrierPartyId = shipGroup.getString("carrierPartyId");
110
111         GenericValue shipAddr = orh.getShippingAddress(shipGroupSeqId);
112         if (shipAddr == null) {
113             return UtilMisc.toMap("shippingTotal", new Double JavaDoc(0));
114         }
115
116         String JavaDoc contactMechId = shipAddr.getString("contactMechId");
117         return getShipGroupEstimate(dispatcher, delegator, orh.getOrderTypeId(), shipmentMethodTypeId, carrierPartyId, carrierRoleTypeId,
118                 contactMechId, orh.getProductStoreId(), orh.getShippableItemInfo(shipGroupSeqId), orh.getShippableWeight(shipGroupSeqId),
119                 orh.getShippableQuantity(shipGroupSeqId), orh.getShippableTotal(shipGroupSeqId));
120     }
121
122     public static Map JavaDoc getShipGroupEstimate(LocalDispatcher dispatcher, GenericDelegator delegator, String JavaDoc orderTypeId,
123             String JavaDoc shipmentMethodTypeId, String JavaDoc carrierPartyId, String JavaDoc carrierRoleTypeId, String JavaDoc shippingContactMechId,
124             String JavaDoc productStoreId, List JavaDoc itemInfo, double shippableWeight, double shippableQuantity,
125             double shippableTotal) {
126         String JavaDoc standardMessage = "A problem occurred calculating shipping. Fees will be calculated offline.";
127         List JavaDoc errorMessageList = new ArrayList JavaDoc();
128
129         if (shipmentMethodTypeId == null || carrierPartyId == null) {
130             if ("SALES_ORDER".equals(orderTypeId)) {
131                 errorMessageList.add("Please Select Your Shipping Method.");
132                 return ServiceUtil.returnError(errorMessageList);
133             } else {
134                 return ServiceUtil.returnSuccess();
135             }
136         }
137
138         if (carrierRoleTypeId == null) {
139             carrierRoleTypeId = "CARRIER";
140         }
141
142         if (shippingContactMechId == null) {
143             errorMessageList.add("Please Select Your Shipping Address.");
144             return ServiceUtil.returnError(errorMessageList);
145         }
146
147         // no shippable items; we won't change any shipping at all
148
if (shippableQuantity == 0) {
149             Map JavaDoc result = ServiceUtil.returnSuccess();
150             result.put("shippingTotal", new Double JavaDoc(0));
151             return result;
152         }
153
154         // check for an external service call
155
GenericValue storeShipMethod = ProductStoreWorker.getProductStoreShipmentMethod(delegator, productStoreId,
156                 shipmentMethodTypeId, carrierPartyId, carrierRoleTypeId);
157
158         if (storeShipMethod == null) {
159             errorMessageList.add("No applicable shipment method found.");
160             return ServiceUtil.returnError(errorMessageList);
161         }
162
163         // the initial amount before manual estimates
164
double shippingTotal = 0.00;
165
166         // prepare the service invocation fields
167
Map JavaDoc serviceFields = new HashMap JavaDoc();
168         serviceFields.put("initialEstimateAmt", new Double JavaDoc(shippingTotal));
169         serviceFields.put("shippableTotal", new Double JavaDoc(shippableTotal));
170         serviceFields.put("shippableQuantity", new Double JavaDoc(shippableQuantity));
171         serviceFields.put("shippableWeight", new Double JavaDoc(shippableWeight));
172         serviceFields.put("shippableItemInfo", itemInfo);
173         serviceFields.put("productStoreId", productStoreId);
174         serviceFields.put("carrierRoleTypeId", "CARRIER");
175         serviceFields.put("carrierPartyId", carrierPartyId);
176         serviceFields.put("shipmentMethodTypeId", shipmentMethodTypeId);
177         serviceFields.put("shippingContactMechId", shippingContactMechId);
178
179         // call the external shipping service
180
try {
181             Double JavaDoc externalAmt = getExternalShipEstimate(dispatcher, storeShipMethod, serviceFields);
182             if (externalAmt != null) {
183                 shippingTotal += externalAmt.doubleValue();
184             }
185         } catch (GeneralException e) {
186             return ServiceUtil.returnSuccess(standardMessage);
187         }
188
189         // update the initial amount
190
serviceFields.put("initialEstimateAmt", new Double JavaDoc(shippingTotal));
191
192         // call the generic estimate service
193
try {
194             Double JavaDoc genericAmt = getGenericShipEstimate(dispatcher, storeShipMethod, serviceFields);
195             if (genericAmt != null) {
196                 shippingTotal += genericAmt.doubleValue();
197             }
198         } catch (GeneralException e) {
199             return ServiceUtil.returnSuccess(standardMessage);
200         }
201
202         // return the totals
203
Map JavaDoc responseResult = ServiceUtil.returnSuccess();
204         responseResult.put("shippingTotal", new Double JavaDoc(shippingTotal));
205         return responseResult;
206     }
207
208     public static Double JavaDoc getGenericShipEstimate(LocalDispatcher dispatcher, GenericValue storeShipMeth, Map JavaDoc context) throws GeneralException {
209         // invoke the generic estimate service next -- append to estimate amount
210
Map JavaDoc genericEstimate = null;
211         Double JavaDoc genericShipAmt = null;
212         try {
213             genericEstimate = dispatcher.runSync("calcShipmentCostEstimate", context);
214         } catch (GenericServiceException e) {
215             Debug.logError(e, "Shipment Service Error", module);
216             throw new GeneralException();
217         }
218         if (ServiceUtil.isError(genericEstimate)) {
219             Debug.logError(ServiceUtil.getErrorMessage(genericEstimate), module);
220             throw new GeneralException();
221         } else {
222             genericShipAmt = (Double JavaDoc) genericEstimate.get("shippingEstimateAmount");
223         }
224         return genericShipAmt;
225     }
226
227     public static Double JavaDoc getExternalShipEstimate(LocalDispatcher dispatcher, GenericValue storeShipMeth, Map JavaDoc context) throws GeneralException {
228         // invoke the external shipping estimate service
229
Double JavaDoc externalShipAmt = null;
230         if (storeShipMeth.get("serviceName") != null) {
231             String JavaDoc serviceName = storeShipMeth.getString("serviceName");
232             String JavaDoc configProps = storeShipMeth.getString("configProps");
233             if (UtilValidate.isNotEmpty(serviceName)) {
234                 // prepare the external service context
235
context.put("serviceConfigProps", configProps);
236
237                 // invoke the service
238
Map JavaDoc serviceResp = null;
239                 try {
240                     Debug.log("Service : " + serviceName + " / " + configProps + " -- " + context, module);
241                     // because we don't want to blow up too big or rollback the transaction when this happens, always have it run in its own transaction...
242
serviceResp = dispatcher.runSync(serviceName, context, 0, true);
243                 } catch (GenericServiceException e) {
244                     Debug.logError(e, "Shipment Service Error", module);
245                     throw new GeneralException();
246                 }
247                 if (!ServiceUtil.isError(serviceResp)) {
248                     externalShipAmt = (Double JavaDoc) serviceResp.get("shippingEstimateAmount");
249                 } else {
250                     String JavaDoc errMsg = "Error getting external shipment cost estimate: " + ServiceUtil.getErrorMessage(serviceResp);
251                     Debug.logError(errMsg, module);
252                     throw new GeneralException(errMsg);
253                 }
254             }
255         }
256         return externalShipAmt;
257     }
258 }
259
260
Popular Tags