📄 upsservices.java
字号:
/*
* $Id: UpsServices.java,v 1.1 2003/08/18 18:45:49 jonesde Exp $
*
* Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.shipment.thirdparty.ups;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.parsers.ParserConfigurationException;
import org.ofbiz.base.util.Base64;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.HttpClient;
import org.ofbiz.base.util.HttpClientException;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/**
* UPS ShipmentServices
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.1 $
* @since 2.2
*/
public class UpsServices {
public final static String module = UpsServices.class.getName();
public static Map unitsUpsToOfbiz = new HashMap();
public static Map unitsOfbizToUps = new HashMap();
static {
unitsUpsToOfbiz.put("LBS", "WT_lb");
unitsUpsToOfbiz.put("KGS", "WT_kg");
Iterator unitsUpsToOfbizIter = unitsUpsToOfbiz.entrySet().iterator();
while (unitsUpsToOfbizIter.hasNext()) {
Map.Entry entry = (Map.Entry) unitsUpsToOfbizIter.next();
unitsOfbizToUps.put(entry.getValue(), entry.getKey());
}
}
public static Map upsShipmentConfirm(DispatchContext dctx, Map context) {
Map result = new HashMap();
GenericDelegator delegator = dctx.getDelegator();
String shipmentId = (String) context.get("shipmentId");
String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId");
boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info"));
String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path");
File shipmentUpsSaveCertificationFile = null;
if (shipmentUpsSaveCertificationInfo) {
shipmentUpsSaveCertificationFile = new File(shipmentUpsSaveCertificationPath);
if (!shipmentUpsSaveCertificationFile.exists()) {
shipmentUpsSaveCertificationFile.mkdirs();
}
}
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 (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) {
return ServiceUtil.returnError("ERROR: The Carrier for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is not UPS.");
}
// add ShipmentRouteSegment carrierServiceStatusId, check before all UPS 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 UPS 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)
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);
}
GenericValue destTelecomNumber = shipmentRouteSegment.getRelatedOne("DestTelecomNumber");
if (destTelecomNumber == null) {
String missingErrMsg = "DestTelecomNumber not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId;
Debug.logError(missingErrMsg, module);
// for now we won't require the dest phone number, but is it required?
//return ServiceUtil.returnError(missingErrMsg);
}
String destPhoneNumber = null;
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);
}
Map findCarrierShipmentMethodMap = UtilMisc.toMap("partyId", shipmentRouteSegment.get("carrierPartyId"), "roleTypeId", "CARRIER", "shipmentMethodTypeId", shipmentRouteSegment.get("shipmentMethodTypeId"));
GenericValue carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod", findCarrierShipmentMethodMap);
if (carrierShipmentMethod == null) {
return ServiceUtil.returnError("CarrierShipmentMethod not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId + "; partyId is " + shipmentRouteSegment.get("carrierPartyId") + " and shipmentMethodTypeId is " + shipmentRouteSegment.get("shipmentMethodTypeId"));
}
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);
}
List itemIssuances = shipment.getRelated("ItemIssuance");
Set orderIdSet = new TreeSet();
Iterator itemIssuanceIter = itemIssuances.iterator();
while (itemIssuanceIter.hasNext()) {
GenericValue itemIssuance = (GenericValue) itemIssuanceIter.next();
orderIdSet.add(itemIssuance.get("orderId"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -