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.shoppingcart.shipping;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.order.shoppingcart.ShoppingCart;
029: import org.ofbiz.product.store.ProductStoreWorker;
030: import org.ofbiz.service.LocalDispatcher;
031:
032: public class ShippingEstimateWrapper {
033:
034: public static final String module = ShippingEstimateWrapper.class
035: .getName();
036:
037: protected GenericDelegator delegator = null;
038: protected LocalDispatcher dispatcher = null;
039:
040: protected Map shippingEstimates = null;
041: protected List shippingMethods = null;
042:
043: protected GenericValue shippingAddress = null;
044: protected Map shippableItemFeatures = null;
045: protected List shippableItemSizes = null;
046: protected List shippableItemInfo = null;
047: protected String productStoreId = null;
048: protected double shippableQuantity = 0;
049: protected double shippableWeight = 0;
050: protected double shippableTotal = 0;
051:
052: public static ShippingEstimateWrapper getWrapper(
053: LocalDispatcher dispatcher, ShoppingCart cart, int shipGroup) {
054: return new ShippingEstimateWrapper(dispatcher, cart, shipGroup);
055: }
056:
057: public ShippingEstimateWrapper(LocalDispatcher dispatcher,
058: ShoppingCart cart, int shipGroup) {
059: this .dispatcher = dispatcher;
060: this .delegator = cart.getDelegator();
061:
062: this .shippableItemFeatures = cart.getFeatureIdQtyMap(shipGroup);
063: this .shippableItemSizes = cart.getShippableSizes(shipGroup);
064: this .shippableItemInfo = cart.getShippableItemInfo(shipGroup);
065: this .shippableQuantity = cart.getShippableQuantity(shipGroup);
066: this .shippableWeight = cart.getShippableWeight(shipGroup);
067: this .shippableTotal = cart.getShippableTotal(shipGroup);
068: this .shippingAddress = cart.getShippingAddress(shipGroup);
069: this .productStoreId = cart.getProductStoreId();
070:
071: this .loadShippingMethods();
072: this .loadEstimates();
073: }
074:
075: protected void loadShippingMethods() {
076: try {
077: this .shippingMethods = ProductStoreWorker
078: .getAvailableStoreShippingMethods(delegator,
079: productStoreId, shippingAddress,
080: shippableItemSizes, shippableItemFeatures,
081: shippableWeight, shippableTotal);
082: } catch (Throwable t) {
083: Debug.logError(t, module);
084: }
085: }
086:
087: protected void loadEstimates() {
088: this .shippingEstimates = new HashMap();
089: if (shippingMethods != null) {
090: Iterator i = shippingMethods.iterator();
091: while (i.hasNext()) {
092: GenericValue shipMethod = (GenericValue) i.next();
093: String shippingMethodTypeId = shipMethod
094: .getString("shipmentMethodTypeId");
095: String carrierRoleTypeId = shipMethod
096: .getString("roleTypeId");
097: String carrierPartyId = shipMethod.getString("partyId");
098: String shippingCmId = shippingAddress != null ? shippingAddress
099: .getString("contactMechId")
100: : null;
101:
102: Map estimateMap = ShippingEvents.getShipGroupEstimate(
103: dispatcher, delegator, "SALES_ORDER",
104: shippingMethodTypeId, carrierPartyId,
105: carrierRoleTypeId, shippingCmId,
106: productStoreId, shippableItemInfo,
107: shippableWeight, shippableQuantity,
108: shippableTotal);
109:
110: Double shippingTotal = (Double) estimateMap
111: .get("shippingTotal");
112: shippingEstimates.put(shipMethod, shippingTotal);
113: }
114: }
115: }
116:
117: public List getShippingMethods() {
118: return shippingMethods;
119: }
120:
121: public Map getAllEstimates() {
122: return shippingEstimates;
123: }
124:
125: public Double getShippingEstimate(
126: GenericValue storeCarrierShipMethod) {
127: return (Double) shippingEstimates.get(storeCarrierShipMethod);
128: }
129:
130: }
|