📄 dhlservices.java
字号:
.getDocumentElement(); DhlServices.handleErrors(registerResponseElement, errorList); if (UtilValidate.isNotEmpty(errorList)) { return ServiceUtil.returnError(errorList); } // handle Response element info Element responseElement = UtilXml.firstChildElement( registerResponseElement, "Register"); Element responseResultElement = UtilXml.firstChildElement( responseElement, "Result"); DhlServices.handleErrors(responseElement, errorList); if (UtilValidate.isNotEmpty(errorList)) { return ServiceUtil.returnError(errorList); } String responseStatusCode = UtilXml.childElementValue( responseResultElement, "Code"); String responseStatusDescription = UtilXml.childElementValue( responseResultElement, "Desc"); String responseShippingKey = UtilXml.childElementValue(responseElement, "ShippingKey"); String responsePostalCode = UtilXml.childElementValue(responseElement, "PostalCode"); Map result = ServiceUtil.returnSuccess(); result.put("shippingKey", responseShippingKey); return result; } /* * Pass a shipment request to DHL via ShipIT and get a tracking number and a label back, among other things */ public static Map dhlShipmentConfirm(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Locale locale = (Locale) context.get("locale"); String shipmentId = (String) context.get("shipmentId"); String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId"); Map result = new HashMap(); String shipmentConfirmResponseString = null; try { GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); if (shipment == null) { return ServiceUtil.returnError("Shipment not found with ID " + shipmentId); } GenericValue shipmentRouteSegment = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); if (shipmentRouteSegment == null) { return ServiceUtil.returnError("ShipmentRouteSegment not found with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } if (!"DHL".equals(shipmentRouteSegment.getString("carrierPartyId"))) { return ServiceUtil.returnError("ERROR: The Carrier for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is not DHL."); } // add ShipmentRouteSegment carrierServiceStatusId, check before all DHL services if (UtilValidate.isNotEmpty(shipmentRouteSegment.getString("carrierServiceStatusId")) && !"SHRSCS_NOT_STARTED".equals(shipmentRouteSegment.getString("carrierServiceStatusId"))) { return ServiceUtil.returnError("ERROR: The Carrier Service Status for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is [" + shipmentRouteSegment.getString("carrierServiceStatusId") + "], but must be not-set or [SHRSCS_NOT_STARTED] to perform the DHL Shipment Confirm operation."); } // Get Origin Info GenericValue originPostalAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress"); if (originPostalAddress == null) { return ServiceUtil.returnError("OriginPostalAddress not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } GenericValue originTelecomNumber = shipmentRouteSegment.getRelatedOne("OriginTelecomNumber"); if (originTelecomNumber == null) { return ServiceUtil.returnError("OriginTelecomNumber not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } String originPhoneNumber = originTelecomNumber.getString("areaCode") + originTelecomNumber.getString("contactNumber"); // don't put on country code if not specified or is the US country code (UPS wants it this way and assuming DHL will accept this) if (UtilValidate.isNotEmpty(originTelecomNumber.getString("countryCode")) && !"001".equals(originTelecomNumber.getString("countryCode"))) { originPhoneNumber = originTelecomNumber.getString("countryCode") + originPhoneNumber; } originPhoneNumber = StringUtil.replaceString(originPhoneNumber, "-", ""); originPhoneNumber = StringUtil.replaceString(originPhoneNumber, " ", ""); // lookup the two letter country code (in the geoCode field) GenericValue originCountryGeo = originPostalAddress.getRelatedOne("CountryGeo"); if (originCountryGeo == null) { return ServiceUtil.returnError("OriginCountryGeo not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } // Get Dest Info GenericValue destPostalAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress"); if (destPostalAddress == null) { return ServiceUtil.returnError("DestPostalAddress not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } // DHL requires destination phone number, default to sender # if no customer number String destPhoneNumber = originPhoneNumber; GenericValue destTelecomNumber = shipmentRouteSegment.getRelatedOne("DestTelecomNumber"); if (destTelecomNumber != null) { destPhoneNumber = destTelecomNumber.getString("areaCode") + destTelecomNumber.getString("contactNumber"); // don't put on country code if not specified or is the US country code (UPS wants it this way) if (UtilValidate.isNotEmpty(destTelecomNumber.getString("countryCode")) && !"001".equals(destTelecomNumber.getString("countryCode"))) { destPhoneNumber = destTelecomNumber.getString("countryCode") + destPhoneNumber; } destPhoneNumber = StringUtil.replaceString(destPhoneNumber, "-", ""); destPhoneNumber = StringUtil.replaceString(destPhoneNumber, " ", ""); } // lookup the two letter country code (in the geoCode field) GenericValue destCountryGeo = destPostalAddress.getRelatedOne("CountryGeo"); if (destCountryGeo == null) { return ServiceUtil.returnError("DestCountryGeo not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } List shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId")); if (shipmentPackageRouteSegs == null || shipmentPackageRouteSegs.size() == 0) { return ServiceUtil.returnError("No ShipmentPackageRouteSegs (ie No Packages) found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); } if (shipmentPackageRouteSegs.size() != 1) { return ServiceUtil.returnError("Cannot confirm shipment: DHL ShipIT does not currently support more than one package per shipment."); } // get the weight from the ShipmentRouteSegment first, which overrides all later weight computations boolean hasBillingWeight = false; // for later overrides Double billingWeight = shipmentRouteSegment.getDouble("billingWeight"); String billingWeightUomId = shipmentRouteSegment.getString("billingWeightUomId"); if ((billingWeight != null) && (billingWeight.doubleValue() > 0)) { hasBillingWeight = true; if (billingWeightUomId == null) { Debug.logWarning("Shipment Route Segment missing billingWeightUomId in shipmentId " + shipmentId, module); billingWeightUomId = "WT_lb"; // TODO: this should be specified in a properties file } // convert Map results = dispatcher.runSync("convertUom", UtilMisc.toMap("uomId", billingWeightUomId, "uomIdTo", DHL_WEIGHT_UOM_ID, "originalValue", billingWeight)); if (ServiceUtil.isError(results) || (results.get("convertedValue") == null)) { Debug.logWarning("Unable to convert billing weights for shipmentId " + shipmentId , module); // try getting the weight from package instead hasBillingWeight = false; } else { billingWeight = (Double) results.get("convertedValue"); } } // loop through Shipment segments (NOTE: only one supported, loop is here for future refactoring reference) String length = null; String width = null; String height = null; Double packageWeight = null; Iterator shipmentPackageRouteSegIter = shipmentPackageRouteSegs.iterator(); while (shipmentPackageRouteSegIter.hasNext()) { GenericValue shipmentPackageRouteSeg = (GenericValue) shipmentPackageRouteSegIter.next(); GenericValue shipmentPackage = shipmentPackageRouteSeg.getRelatedOne("ShipmentPackage"); GenericValue shipmentBoxType = shipmentPackage.getRelatedOne("ShipmentBoxType"); List carrierShipmentBoxTypes = shipmentPackage.getRelated("CarrierShipmentBoxType", UtilMisc.toMap("partyId", "DHL"), null); GenericValue carrierShipmentBoxType = null; if (carrierShipmentBoxTypes.size() > 0) { carrierShipmentBoxType = (GenericValue) carrierShipmentBoxTypes.get(0); } // TODO: determine what default UoM is (assuming inches) - there should be a defaultDimensionUomId in Facility if (shipmentBoxType != null) { GenericValue dimensionUom = shipmentBoxType.getRelatedOne("DimensionUom"); length = shipmentBoxType.get("boxLength").toString(); width = shipmentBoxType.get("boxWidth").toString(); height = shipmentBoxType.get("boxHeight").toString(); } // next step is weight determination, so skip if we have a billing weight if (hasBillingWeight) continue; // compute total packageWeight (for now, just one package) if (shipmentPackage.getString("weight") != null) { packageWeight = Double.valueOf(shipmentPackage.getString("weight")); } else { // use default weight if available try { packageWeight = Double.valueOf(UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.default.weight.value")); } catch (NumberFormatException ne) { Debug.logWarning("Default shippable weight not configured (shipment.default.weight.value)", module); packageWeight = new Double(1.0); } } // convert weight String weightUomId = (String) shipmentPackage.get("weightUomId"); if (weightUomId == null) { Debug.logWarning("Shipment Route Segment missing weightUomId in shipmentId " + shipmentId, module); weightUomId = "WT_lb"; // TODO: this should be specified in a properties file } Map results = dispatcher.runSync("convertUom", UtilMisc.toMap("uomId", weightUomId, "uomIdTo", DHL_WEIGHT_UOM_ID, "originalValue", packageWeight)); if ((results == null) || (results.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) || (results.get("convertedValue") == null)) { Debug.logWarning("Unable to convert weights for shipmentId " + shipmentId , module); packageWeight = new Double(1.0); } else { packageWeight = (Double) results.get("convertedValue"); } } // pick which weight to use and round it Double weight = null; if (hasBillingWeight) { weight = billingWeight; } else { weight = packageWeight; } // want the rounded weight as a string, so we use the "" + int shortcut String roundedWeight = "" + Math.round(weight.doubleValue()); // translate shipmentMethodTypeId to DHL service code String shipmentMethodTypeId = shipmentRouteSegment.getString("shipmentMethodTypeId"); String dhlShipmentDetailCode = null; GenericValue carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod", UtilMisc.toMap("shipmentMethodTypeId", shipmentMethodTypeId, "partyId", "DHL", "roleTypeId", "CARRIER")); if (carrierShipmentMethod == null) { return ServiceUtil.returnError("No CarrierShipmentMethod entry for carrier DHL shipmentMethodTypeId " + shipmentMethodTypeId); } dhlShipmentDetailCode = carrierShipmentMethod.getString("carrierServiceCode"); // shipping credentials (configured in properties) String userid = UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.dhl.access.userid"); String password = UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.dhl.access.password"); String shippingKey = UtilProperties.getPropertyValue("shipment", "shipment.dhl.access.shippingKey"); String accountNbr = UtilProperties.getPropertyValue("shipment", "shipment.dhl.access.accountNbr"); if ((shippingKey == null) || (accountNbr == null) || (shippingKey.length() == 0) || (accountNbr.length() == 0)) { return ServiceUtil.returnError("DHL Shipping Credentials are not configured. (check shipment.dhl.access)"); } // label image preference (PNG or GIF) String labelImagePreference = UtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.dhl.label.image.format"); if (labelImagePreference == null) { Debug.logInfo("shipment.dhl.label.image.format not specified, assuming PNG", module); labelImagePreference="PNG"; } else if (!(labelImagePreference.equals("PNG") || labelImagePreference.equals("GIF"))) { Debug.logError("Illegal shipment.dhl.label.image.format: " + labelImagePreference, module); return ServiceUtil.returnError("Unknown DHL Label Image Format: " + labelImagePreference); } // create AccessRequest XML doc using FreeMarker template
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -