KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ShippingEstimateWrapper.java 5462 2005-08-05 18:35:48Z jonesde $
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.shoppingcart.shipping;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.entity.GenericDelegator;
34 import org.ofbiz.entity.GenericValue;
35 import org.ofbiz.order.shoppingcart.ShoppingCart;
36 import org.ofbiz.product.store.ProductStoreWorker;
37 import org.ofbiz.service.LocalDispatcher;
38
39 /**
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 3.2
44  */

45 public class ShippingEstimateWrapper {
46
47     public static final String JavaDoc module = ShippingEstimateWrapper.class.getName();
48
49     protected GenericDelegator delegator = null;
50     protected LocalDispatcher dispatcher = null;
51
52     protected Map JavaDoc shippingEstimates = null;
53     protected List JavaDoc shippingMethods = null;
54
55     protected GenericValue shippingAddress = null;
56     protected Map JavaDoc shippableItemFeatures = null;
57     protected List JavaDoc shippableItemSizes = null;
58     protected List JavaDoc shippableItemInfo = null;
59     protected String JavaDoc productStoreId = null;
60     protected double shippableQuantity = 0;
61     protected double shippableWeight = 0;
62     protected double shippableTotal = 0;
63
64     public static ShippingEstimateWrapper getWrapper(LocalDispatcher dispatcher, ShoppingCart cart, int shipGroup) {
65         return new ShippingEstimateWrapper(dispatcher, cart, shipGroup);
66     }
67
68     public ShippingEstimateWrapper(LocalDispatcher dispatcher, ShoppingCart cart, int shipGroup) {
69         this.dispatcher = dispatcher;
70         this.delegator = cart.getDelegator();
71
72         this.shippableItemFeatures = cart.getFeatureIdQtyMap(shipGroup);
73         this.shippableItemSizes = cart.getShippableSizes(shipGroup);
74         this.shippableItemInfo = cart.getShippableItemInfo(shipGroup);
75         this.shippableQuantity = cart.getShippableQuantity(shipGroup);
76         this.shippableWeight = cart.getShippableWeight(shipGroup);
77         this.shippableTotal = cart.getShippableTotal(shipGroup);
78         this.shippingAddress = cart.getShippingAddress(shipGroup);
79         this.productStoreId = cart.getProductStoreId();
80
81         this.loadShippingMethods();
82         this.loadEstimates();
83     }
84
85     protected void loadShippingMethods() {
86         try {
87             this.shippingMethods = ProductStoreWorker.getAvailableStoreShippingMethods(delegator, productStoreId,
88                     shippingAddress, shippableItemSizes, shippableItemFeatures, shippableWeight, shippableTotal);
89         } catch (Throwable JavaDoc t) {
90             Debug.logError(t, module);
91         }
92     }
93
94     protected void loadEstimates() {
95         this.shippingEstimates = new HashMap JavaDoc();
96         if (shippingMethods != null) {
97             Iterator JavaDoc i = shippingMethods.iterator();
98             while (i.hasNext()) {
99                 GenericValue shipMethod = (GenericValue) i.next();
100                 String JavaDoc shippingMethodTypeId = shipMethod.getString("shipmentMethodTypeId");
101                 String JavaDoc carrierRoleTypeId = shipMethod.getString("roleTypeId");
102                 String JavaDoc carrierPartyId = shipMethod.getString("partyId");
103                 String JavaDoc shippingCmId = shippingAddress != null ? shippingAddress.getString("contactMechId") : null;
104
105                 Map JavaDoc estimateMap = ShippingEvents.getShipGroupEstimate(dispatcher, delegator, "SALES_ORDER",
106                         shippingMethodTypeId, carrierPartyId, carrierRoleTypeId, shippingCmId, productStoreId,
107                         shippableItemInfo, shippableWeight, shippableQuantity, shippableTotal);
108
109                 Double JavaDoc shippingTotal = (Double JavaDoc) estimateMap.get("shippingTotal");
110                 shippingEstimates.put(shipMethod, shippingTotal);
111             }
112         }
113     }
114
115     public List JavaDoc getShippingMethods() {
116         return shippingMethods;
117     }
118
119     public Map JavaDoc getAllEstimates() {
120         return shippingEstimates;
121     }
122
123     public Double JavaDoc getShippingEstimate(GenericValue storeCarrierShipMethod) {
124         return (Double JavaDoc) shippingEstimates.get(storeCarrierShipMethod);
125     }
126
127 }
128
Popular Tags