1 24 package org.ofbiz.shipment.thirdparty.ups; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.TreeSet ; 36 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import org.ofbiz.base.util.Base64; 40 import org.ofbiz.base.util.Debug; 41 import org.ofbiz.base.util.GeneralException; 42 import org.ofbiz.base.util.HttpClient; 43 import org.ofbiz.base.util.HttpClientException; 44 import org.ofbiz.base.util.StringUtil; 45 import org.ofbiz.base.util.UtilMisc; 46 import org.ofbiz.base.util.UtilProperties; 47 import org.ofbiz.base.util.UtilValidate; 48 import org.ofbiz.base.util.UtilXml; 49 import org.ofbiz.entity.GenericDelegator; 50 import org.ofbiz.entity.GenericEntityException; 51 import org.ofbiz.entity.GenericValue; 52 import org.ofbiz.entity.util.EntityUtil; 53 import org.ofbiz.service.DispatchContext; 54 import org.ofbiz.service.ServiceUtil; 55 import org.ofbiz.product.store.ProductStoreWorker; 56 57 import org.w3c.dom.Document ; 58 import org.w3c.dom.Element ; 59 import org.xml.sax.SAXException ; 60 61 69 public class UpsServices { 70 71 public final static String module = UpsServices.class.getName(); 72 73 public static Map unitsUpsToOfbiz = new HashMap (); 74 public static Map unitsOfbizToUps = new HashMap (); 75 static { 76 unitsUpsToOfbiz.put("LBS", "WT_lb"); 77 unitsUpsToOfbiz.put("KGS", "WT_kg"); 78 79 Iterator unitsUpsToOfbizIter = unitsUpsToOfbiz.entrySet().iterator(); 80 while (unitsUpsToOfbizIter.hasNext()) { 81 Map.Entry entry = (Map.Entry ) unitsUpsToOfbizIter.next(); 82 unitsOfbizToUps.put(entry.getValue(), entry.getKey()); 83 } 84 } 85 86 87 public static Map upsShipmentConfirm(DispatchContext dctx, Map context) { 88 Map result = new HashMap (); 89 GenericDelegator delegator = dctx.getDelegator(); 90 String shipmentId = (String ) context.get("shipmentId"); 91 String shipmentRouteSegmentId = (String ) context.get("shipmentRouteSegmentId"); 92 93 boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info")); 94 String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path"); 95 File shipmentUpsSaveCertificationFile = null; 96 if (shipmentUpsSaveCertificationInfo) { 97 shipmentUpsSaveCertificationFile = new File (shipmentUpsSaveCertificationPath); 98 if (!shipmentUpsSaveCertificationFile.exists()) { 99 shipmentUpsSaveCertificationFile.mkdirs(); 100 } 101 } 102 103 String shipmentConfirmResponseString = null; 104 105 try { 106 GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); 107 if (shipment == null) { 108 return ServiceUtil.returnError("Shipment not found with ID " + shipmentId); 109 } 110 GenericValue shipmentRouteSegment = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); 111 if (shipmentRouteSegment == null) { 112 return ServiceUtil.returnError("ShipmentRouteSegment not found with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 113 } 114 115 if (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) { 116 return ServiceUtil.returnError("ERROR: The Carrier for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is not UPS."); 117 } 118 119 if (UtilValidate.isNotEmpty(shipmentRouteSegment.getString("carrierServiceStatusId")) && !"SHRSCS_NOT_STARTED".equals(shipmentRouteSegment.getString("carrierServiceStatusId"))) { 121 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."); 122 } 123 124 GenericValue originPostalAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress"); 126 if (originPostalAddress == null) { 127 return ServiceUtil.returnError("OriginPostalAddress not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 128 } 129 GenericValue originTelecomNumber = shipmentRouteSegment.getRelatedOne("OriginTelecomNumber"); 130 if (originTelecomNumber == null) { 131 return ServiceUtil.returnError("OriginTelecomNumber not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 132 } 133 String originPhoneNumber = originTelecomNumber.getString("areaCode") + originTelecomNumber.getString("contactNumber"); 134 if (UtilValidate.isNotEmpty(originTelecomNumber.getString("countryCode")) && !"001".equals(originTelecomNumber.getString("countryCode"))) { 136 originPhoneNumber = originTelecomNumber.getString("countryCode") + originPhoneNumber; 137 } 138 originPhoneNumber = StringUtil.replaceString(originPhoneNumber, "-", ""); 139 originPhoneNumber = StringUtil.replaceString(originPhoneNumber, " ", ""); 140 GenericValue originCountryGeo = originPostalAddress.getRelatedOne("CountryGeo"); 142 if (originCountryGeo == null) { 143 return ServiceUtil.returnError("OriginCountryGeo not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 144 } 145 146 GenericValue destPostalAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress"); 148 if (destPostalAddress == null) { 149 return ServiceUtil.returnError("DestPostalAddress not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 150 } 151 152 GenericValue destTelecomNumber = shipmentRouteSegment.getRelatedOne("DestTelecomNumber"); 153 if (destTelecomNumber == null) { 154 String missingErrMsg = "DestTelecomNumber not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId; 155 Debug.logError(missingErrMsg, module); 156 } 159 String destPhoneNumber = null; 160 if (destTelecomNumber != null) { 161 destPhoneNumber = destTelecomNumber.getString("areaCode") + destTelecomNumber.getString("contactNumber"); 162 if (UtilValidate.isNotEmpty(destTelecomNumber.getString("countryCode")) && !"001".equals(destTelecomNumber.getString("countryCode"))) { 164 destPhoneNumber = destTelecomNumber.getString("countryCode") + destPhoneNumber; 165 } 166 destPhoneNumber = StringUtil.replaceString(destPhoneNumber, "-", ""); 167 destPhoneNumber = StringUtil.replaceString(destPhoneNumber, " ", ""); 168 } 169 170 GenericValue destCountryGeo = destPostalAddress.getRelatedOne("CountryGeo"); 172 if (destCountryGeo == null) { 173 return ServiceUtil.returnError("DestCountryGeo not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 174 } 175 176 Map findCarrierShipmentMethodMap = UtilMisc.toMap("partyId", shipmentRouteSegment.get("carrierPartyId"), "roleTypeId", "CARRIER", "shipmentMethodTypeId", shipmentRouteSegment.get("shipmentMethodTypeId")); 177 GenericValue carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod", findCarrierShipmentMethodMap); 178 if (carrierShipmentMethod == null) { 179 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")); 180 } 181 182 List shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId")); 183 if (shipmentPackageRouteSegs == null || shipmentPackageRouteSegs.size() == 0) { 184 return ServiceUtil.returnError("No ShipmentPackageRouteSegs (ie No Packages) found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 185 } 186 187 List itemIssuances = shipment.getRelated("ItemIssuance"); 188 Set orderIdSet = new TreeSet (); 189 Iterator itemIssuanceIter = itemIssuances.iterator(); 190 while (itemIssuanceIter.hasNext()) { 191 GenericValue itemIssuance = (GenericValue) itemIssuanceIter.next(); 192 orderIdSet.add(itemIssuance.get("orderId")); 193 } 194 String ordersDescription = ""; 195 if (orderIdSet.size() > 1) { 196 StringBuffer odBuf = new StringBuffer ("Orders "); 197 Iterator orderIdIter = orderIdSet.iterator(); 198 while (orderIdIter.hasNext()) { 199 String orderId = (String ) orderIdIter.next(); 200 odBuf.append(orderId); 201 if (orderIdIter.hasNext()) { 202 odBuf.append(", "); 203 } 204 } 205 ordersDescription = odBuf.toString(); 206 } else if (orderIdSet.size() > 0) { 207 ordersDescription = "Order " + (String ) orderIdSet.iterator().next(); 208 } 209 210 Document shipmentConfirmRequestDoc = UtilXml.makeEmptyXmlDocument("ShipmentConfirmRequest"); 212 Element shipmentConfirmRequestElement = shipmentConfirmRequestDoc.getDocumentElement(); 213 shipmentConfirmRequestElement.setAttribute("xml:lang", "en-US"); 214 215 Element requestElement = UtilXml.addChildElement(shipmentConfirmRequestElement, "Request", shipmentConfirmRequestDoc); 217 218 Element transactionReferenceElement = UtilXml.addChildElement(requestElement, "TransactionReference", shipmentConfirmRequestDoc); 219 UtilXml.addChildElementValue(transactionReferenceElement, "CustomerContext", "Ship Confirm / nonvalidate", shipmentConfirmRequestDoc); 220 UtilXml.addChildElementValue(transactionReferenceElement, "XpciVersion", "1.0001", shipmentConfirmRequestDoc); 221 222 UtilXml.addChildElementValue(requestElement, "RequestAction", "ShipConfirm", shipmentConfirmRequestDoc); 223 UtilXml.addChildElementValue(requestElement, "RequestOption", "nonvalidate", shipmentConfirmRequestDoc); 224 225 Element labelSpecificationElement = UtilXml.addChildElement(shipmentConfirmRequestElement, "LabelSpecification", shipmentConfirmRequestDoc); 227 228 Element labelPrintMethodElement = UtilXml.addChildElement(labelSpecificationElement, "LabelPrintMethod", shipmentConfirmRequestDoc); 229 UtilXml.addChildElementValue(labelPrintMethodElement, "Code", "GIF", shipmentConfirmRequestDoc); 230 231 UtilXml.addChildElementValue(labelSpecificationElement, "HTTPUserAgent", "Mozilla/5.0", shipmentConfirmRequestDoc); 232 233 Element labelImageFormatElement = UtilXml.addChildElement(labelSpecificationElement, "LabelImageFormat", shipmentConfirmRequestDoc); 234 UtilXml.addChildElementValue(labelImageFormatElement, "Code", "GIF", shipmentConfirmRequestDoc); 235 236 Element shipmentElement = UtilXml.addChildElement(shipmentConfirmRequestElement, "Shipment", shipmentConfirmRequestDoc); 238 UtilXml.addChildElementValue(shipmentElement, "Description", "Goods for Shipment " + shipment.get("shipmentId") + " from " + ordersDescription, shipmentConfirmRequestDoc); 239 240 Element shipperElement = UtilXml.addChildElement(shipmentElement, "Shipper", shipmentConfirmRequestDoc); 242 UtilXml.addChildElementValue(shipperElement, "Name", originPostalAddress.getString("toName"), shipmentConfirmRequestDoc); 243 UtilXml.addChildElementValue(shipperElement, "AttentionName", originPostalAddress.getString("attnName"), shipmentConfirmRequestDoc); 244 UtilXml.addChildElementValue(shipperElement, "PhoneNumber", originPhoneNumber, shipmentConfirmRequestDoc); 245 UtilXml.addChildElementValue(shipperElement, "ShipperNumber", UtilProperties.getPropertyValue("shipment", "shipment.ups.shipper.number"), shipmentConfirmRequestDoc); 246 247 Element shipperAddressElement = UtilXml.addChildElement(shipperElement, "Address", shipmentConfirmRequestDoc); 248 UtilXml.addChildElementValue(shipperAddressElement, "AddressLine1", originPostalAddress.getString("address1"), shipmentConfirmRequestDoc); 249 if (UtilValidate.isNotEmpty(originPostalAddress.getString("address2"))) { 250 UtilXml.addChildElementValue(shipperAddressElement, "AddressLine2", originPostalAddress.getString("address2"), shipmentConfirmRequestDoc); 251 } 252 UtilXml.addChildElementValue(shipperAddressElement, "City", originPostalAddress.getString("city"), shipmentConfirmRequestDoc); 254 UtilXml.addChildElementValue(shipperAddressElement, "StateProvinceCode", originPostalAddress.getString("stateProvinceGeoId"), shipmentConfirmRequestDoc); 255 UtilXml.addChildElementValue(shipperAddressElement, "PostalCode", originPostalAddress.getString("postalCode"), shipmentConfirmRequestDoc); 256 UtilXml.addChildElementValue(shipperAddressElement, "CountryCode", originCountryGeo.getString("geoCode"), shipmentConfirmRequestDoc); 257 259 260 Element shipToElement = UtilXml.addChildElement(shipmentElement, "ShipTo", shipmentConfirmRequestDoc); 262 UtilXml.addChildElementValue(shipToElement, "CompanyName", destPostalAddress.getString("toName"), shipmentConfirmRequestDoc); 263 UtilXml.addChildElementValue(shipToElement, "AttentionName", destPostalAddress.getString("attnName"), shipmentConfirmRequestDoc); 264 if (UtilValidate.isNotEmpty(destPhoneNumber)) { 265 UtilXml.addChildElementValue(shipToElement, "PhoneNumber", destPhoneNumber, shipmentConfirmRequestDoc); 266 } 267 Element shipToAddressElement = UtilXml.addChildElement(shipToElement, "Address", shipmentConfirmRequestDoc); 268 UtilXml.addChildElementValue(shipToAddressElement, "AddressLine1", destPostalAddress.getString("address1"), shipmentConfirmRequestDoc); 269 if (UtilValidate.isNotEmpty(destPostalAddress.getString("address2"))) { 270 UtilXml.addChildElementValue(shipToAddressElement, "AddressLine2", destPostalAddress.getString("address2"), shipmentConfirmRequestDoc); 271 } 272 UtilXml.addChildElementValue(shipToAddressElement, "City", destPostalAddress.getString("city"), shipmentConfirmRequestDoc); 274 UtilXml.addChildElementValue(shipToAddressElement, "StateProvinceCode", destPostalAddress.getString("stateProvinceGeoId"), shipmentConfirmRequestDoc); 275 UtilXml.addChildElementValue(shipToAddressElement, "PostalCode", destPostalAddress.getString("postalCode"), shipmentConfirmRequestDoc); 276 UtilXml.addChildElementValue(shipToAddressElement, "CountryCode", destCountryGeo.getString("geoCode"), shipmentConfirmRequestDoc); 277 278 Element shipFromElement = UtilXml.addChildElement(shipmentElement, "ShipFrom", shipmentConfirmRequestDoc); 280 UtilXml.addChildElementValue(shipFromElement, "CompanyName", originPostalAddress.getString("toName"), shipmentConfirmRequestDoc); 281 UtilXml.addChildElementValue(shipFromElement, "AttentionName", originPostalAddress.getString("attnName"), shipmentConfirmRequestDoc); 282 UtilXml.addChildElementValue(shipFromElement, "PhoneNumber", originPhoneNumber, shipmentConfirmRequestDoc); 283 Element shipFromAddressElement = UtilXml.addChildElement(shipFromElement, "Address", shipmentConfirmRequestDoc); 284 UtilXml.addChildElementValue(shipFromAddressElement, "AddressLine1", originPostalAddress.getString("address1"), shipmentConfirmRequestDoc); 285 if (UtilValidate.isNotEmpty(originPostalAddress.getString("address2"))) { 286 UtilXml.addChildElementValue(shipFromAddressElement, "AddressLine2", originPostalAddress.getString("address2"), shipmentConfirmRequestDoc); 287 } 288 UtilXml.addChildElementValue(shipFromAddressElement, "City", originPostalAddress.getString("city"), shipmentConfirmRequestDoc); 290 UtilXml.addChildElementValue(shipFromAddressElement, "StateProvinceCode", originPostalAddress.getString("stateProvinceGeoId"), shipmentConfirmRequestDoc); 291 UtilXml.addChildElementValue(shipFromAddressElement, "PostalCode", originPostalAddress.getString("postalCode"), shipmentConfirmRequestDoc); 292 UtilXml.addChildElementValue(shipFromAddressElement, "CountryCode", originCountryGeo.getString("geoCode"), shipmentConfirmRequestDoc); 293 294 Element paymentInformationElement = UtilXml.addChildElement(shipmentElement, "PaymentInformation", shipmentConfirmRequestDoc); 296 Element prepaidElement = UtilXml.addChildElement(paymentInformationElement, "Prepaid", shipmentConfirmRequestDoc); 297 Element billShipperElement = UtilXml.addChildElement(prepaidElement, "BillShipper", shipmentConfirmRequestDoc); 298 UtilXml.addChildElementValue(billShipperElement, "AccountNumber", UtilProperties.getPropertyValue("shipment", "shipment.ups.bill.shipper.account.number"), shipmentConfirmRequestDoc); 300 301 Element serviceElement = UtilXml.addChildElement(shipmentElement, "Service", shipmentConfirmRequestDoc); 303 UtilXml.addChildElementValue(serviceElement, "Code", carrierShipmentMethod.getString("carrierServiceCode"), shipmentConfirmRequestDoc); 304 305 Iterator shipmentPackageRouteSegIter = shipmentPackageRouteSegs.iterator(); 307 while (shipmentPackageRouteSegIter.hasNext()) { 308 GenericValue shipmentPackageRouteSeg = (GenericValue) shipmentPackageRouteSegIter.next(); 309 GenericValue shipmentPackage = shipmentPackageRouteSeg.getRelatedOne("ShipmentPackage"); 310 GenericValue shipmentBoxType = shipmentPackage.getRelatedOne("ShipmentBoxType"); 311 List carrierShipmentBoxTypes = shipmentPackage.getRelated("CarrierShipmentBoxType", UtilMisc.toMap("partyId", "UPS"), null); 312 GenericValue carrierShipmentBoxType = null; 313 if (carrierShipmentBoxTypes.size() > 0) { 314 carrierShipmentBoxType = (GenericValue) carrierShipmentBoxTypes.get(0); 315 } 316 317 Element packageElement = UtilXml.addChildElement(shipmentElement, "Package", shipmentConfirmRequestDoc); 318 Element packagingTypeElement = UtilXml.addChildElement(packageElement, "PackagingType", shipmentConfirmRequestDoc); 319 if (carrierShipmentBoxType != null && carrierShipmentBoxType.get("packagingTypeCode") != null) { 320 UtilXml.addChildElementValue(packagingTypeElement, "Code", carrierShipmentBoxType.getString("packagingTypeCode"), shipmentConfirmRequestDoc); 321 } else { 322 UtilXml.addChildElementValue(packagingTypeElement, "Code", "02", shipmentConfirmRequestDoc); 324 } 325 if (shipmentBoxType != null) { 326 Element dimensionsElement = UtilXml.addChildElement(packageElement, "Dimensions", shipmentConfirmRequestDoc); 327 Element unitOfMeasurementElement = UtilXml.addChildElement(dimensionsElement, "UnitOfMeasurement", shipmentConfirmRequestDoc); 328 GenericValue dimensionUom = shipmentBoxType.getRelatedOne("DimensionUom"); 329 if (dimensionUom != null) { 330 UtilXml.addChildElementValue(unitOfMeasurementElement, "Code", dimensionUom.getString("abbreviation"), shipmentConfirmRequestDoc); 331 } else { 332 UtilXml.addChildElementValue(unitOfMeasurementElement, "Code", "IN", shipmentConfirmRequestDoc); 334 } 335 UtilXml.addChildElementValue(dimensionsElement, "Length", shipmentBoxType.get("boxLength").toString(), shipmentConfirmRequestDoc); 336 UtilXml.addChildElementValue(dimensionsElement, "Width", shipmentBoxType.get("boxWidth").toString(), shipmentConfirmRequestDoc); 337 UtilXml.addChildElementValue(dimensionsElement, "Height", shipmentBoxType.get("boxHeight").toString(), shipmentConfirmRequestDoc); 338 } 339 340 Element packageWeightElement = UtilXml.addChildElement(packageElement, "PackageWeight", shipmentConfirmRequestDoc); 341 Element packageWeightUnitOfMeasurementElement = UtilXml.addChildElement(packageElement, "UnitOfMeasurement", shipmentConfirmRequestDoc); 342 String weightUomUps = (String ) unitsOfbizToUps.get(shipmentPackage.get("weightUomId")); 343 if (weightUomUps != null) { 344 UtilXml.addChildElementValue(packageWeightUnitOfMeasurementElement, "Code", weightUomUps, shipmentConfirmRequestDoc); 345 } else { 346 UtilXml.addChildElementValue(packageWeightUnitOfMeasurementElement, "Code", "LBS", shipmentConfirmRequestDoc); 348 } 349 350 if (shipmentPackage.getString("weight") == null) { 351 return ServiceUtil.returnError("Weight value not found for ShipmentRouteSegment with shipmentId " + shipmentId + ", shipmentRouteSegmentId " + shipmentRouteSegmentId + ", and shipmentPackageSeqId " + shipmentPackage.getString("shipmentPackageSeqId")); 352 } 353 UtilXml.addChildElementValue(packageWeightElement, "Weight", shipmentPackage.getString("weight"), shipmentConfirmRequestDoc); 354 355 Element referenceNumberElement = UtilXml.addChildElement(packageElement, "ReferenceNumber", shipmentConfirmRequestDoc); 356 UtilXml.addChildElementValue(referenceNumberElement, "Code", "MK", shipmentConfirmRequestDoc); 357 UtilXml.addChildElementValue(referenceNumberElement, "Value", shipmentPackage.getString("shipmentPackageSeqId"), shipmentConfirmRequestDoc); 358 359 if (carrierShipmentBoxType != null && carrierShipmentBoxType.get("oversizeCode") != null) { 360 UtilXml.addChildElementValue(packageElement, "OversizePackage", carrierShipmentBoxType.getString("oversizeCode"), shipmentConfirmRequestDoc); 361 } 362 } 363 364 String shipmentConfirmRequestString = null; 365 try { 366 shipmentConfirmRequestString = UtilXml.writeXmlDocument(shipmentConfirmRequestDoc); 367 } catch (IOException e) { 368 String ioeErrMsg = "Error writing the ShipmentConfirmRequest XML Document to a String: " + e.toString(); 369 Debug.logError(e, ioeErrMsg, module); 370 return ServiceUtil.returnError(ioeErrMsg); 371 } 372 373 Document accessRequestDocument = createAccessRequestDocument(); 375 String accessRequestString = null; 376 try { 377 accessRequestString = UtilXml.writeXmlDocument(accessRequestDocument); 378 } catch (IOException e) { 379 String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString(); 380 Debug.logError(e, ioeErrMsg, module); 381 return ServiceUtil.returnError(ioeErrMsg); 382 } 383 384 StringBuffer xmlString = new StringBuffer (); 388 xmlString.append(accessRequestString); 390 xmlString.append(shipmentConfirmRequestString); 391 392 if (shipmentUpsSaveCertificationInfo) { 393 String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentConfirmRequest" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 394 try { 395 FileOutputStream fileOut = new FileOutputStream (outFileName); 396 fileOut.write(xmlString.toString().getBytes()); 397 fileOut.flush(); 398 fileOut.close(); 399 } catch (IOException e) { 400 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 401 } 402 } 403 404 try { 405 shipmentConfirmResponseString = sendUpsRequest("ShipConfirm", xmlString.toString()); 406 } catch (UpsConnectException e) { 407 String uceErrMsg = "Error sending UPS request for UPS Service ShipConfirm: " + e.toString(); 408 Debug.logError(e, uceErrMsg, module); 409 return ServiceUtil.returnError(uceErrMsg); 410 } 411 412 if (shipmentUpsSaveCertificationInfo) { 413 String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentConfirmResponse" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 414 try { 415 FileOutputStream fileOut = new FileOutputStream (outFileName); 416 fileOut.write(shipmentConfirmResponseString.getBytes()); 417 fileOut.flush(); 418 fileOut.close(); 419 } catch (IOException e) { 420 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 421 } 422 } 423 424 Document shipmentConfirmResponseDocument = null; 425 try { 426 shipmentConfirmResponseDocument = UtilXml.readXmlDocument(shipmentConfirmResponseString, false); 427 } catch (SAXException e2) { 428 String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString(); 429 Debug.logError(e2, excErrMsg, module); 430 return ServiceUtil.returnError(excErrMsg); 431 } catch (ParserConfigurationException e2) { 432 String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString(); 433 Debug.logError(e2, excErrMsg, module); 434 return ServiceUtil.returnError(excErrMsg); 435 } catch (IOException e2) { 436 String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString(); 437 Debug.logError(e2, excErrMsg, module); 438 return ServiceUtil.returnError(excErrMsg); 439 } 440 441 return handleUpsShipmentConfirmResponse(shipmentConfirmResponseDocument, shipmentRouteSegment); 442 } catch (GenericEntityException e) { 443 Debug.logError(e, module); 444 if (shipmentConfirmResponseString != null) { 445 Debug.logError("Got XML ShipmentConfirmRespose: " + shipmentConfirmResponseString, module); 446 return ServiceUtil.returnError(UtilMisc.toList( 447 "Error reading or writing Shipment data for UPS Shipment Confirm: " + e.toString(), 448 "A ShipmentConfirmRespose was received: " + shipmentConfirmResponseString)); 449 } else { 450 return ServiceUtil.returnError("Error reading or writing Shipment data for UPS Shipment Confirm: " + e.toString()); 451 } 452 } 453 } 454 455 public static Map handleUpsShipmentConfirmResponse(Document shipmentConfirmResponseDocument, GenericValue shipmentRouteSegment) throws GenericEntityException { 456 Element shipmentConfirmResponseElement = shipmentConfirmResponseDocument.getDocumentElement(); 458 459 Element responseElement = UtilXml.firstChildElement(shipmentConfirmResponseElement, "Response"); 461 Element responseTransactionReferenceElement = UtilXml.firstChildElement(responseElement, "TransactionReference"); 462 String responseTransactionReferenceCustomerContext = UtilXml.childElementValue(responseTransactionReferenceElement, "CustomerContext"); 463 String responseTransactionReferenceXpciVersion = UtilXml.childElementValue(responseTransactionReferenceElement, "XpciVersion"); 464 465 String responseStatusCode = UtilXml.childElementValue(responseElement, "ResponseStatusCode"); 466 String responseStatusDescription = UtilXml.childElementValue(responseElement, "ResponseStatusDescription"); 467 List errorList = new LinkedList (); 468 UpsServices.handleErrors(responseElement, errorList); 469 470 if ("1".equals(responseStatusCode)) { 471 Element shipmentChargesElement = UtilXml.firstChildElement(shipmentConfirmResponseElement, "ShipmentCharges"); 473 474 Element transportationChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "TransportationCharges"); 475 String transportationCurrencyCode = UtilXml.childElementValue(transportationChargesElement, "CurrencyCode"); 476 String transportationMonetaryValue = UtilXml.childElementValue(transportationChargesElement, "MonetaryValue"); 477 478 Element serviceOptionsChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "ServiceOptionsCharges"); 479 String serviceOptionsCurrencyCode = UtilXml.childElementValue(serviceOptionsChargesElement, "CurrencyCode"); 480 String serviceOptionsMonetaryValue = UtilXml.childElementValue(serviceOptionsChargesElement, "MonetaryValue"); 481 482 Element totalChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "TotalCharges"); 483 String totalCurrencyCode = UtilXml.childElementValue(totalChargesElement, "CurrencyCode"); 484 String totalMonetaryValue = UtilXml.childElementValue(totalChargesElement, "MonetaryValue"); 485 486 if (UtilValidate.isNotEmpty(totalCurrencyCode)) { 487 if (UtilValidate.isEmpty(shipmentRouteSegment.getString("currencyUomId"))) { 488 shipmentRouteSegment.set("currencyUomId", totalCurrencyCode); 489 } else if(!totalCurrencyCode.equals(shipmentRouteSegment.getString("currencyUomId"))) { 490 errorList.add("The Currency Unit of Measure returned [" + totalCurrencyCode + "] is not the same as the original [" + shipmentRouteSegment.getString("currencyUomId") + "], setting to the new one."); 491 shipmentRouteSegment.set("currencyUomId", totalCurrencyCode); 492 } 493 } 494 495 try { 496 shipmentRouteSegment.set("actualTransportCost", Double.valueOf(transportationMonetaryValue)); 497 } catch (NumberFormatException e) { 498 String excErrMsg = "Error parsing the transportationMonetaryValue [" + transportationMonetaryValue + "]: " + e.toString(); 499 Debug.logError(e, excErrMsg, module); 500 errorList.add(excErrMsg); 501 } 502 try { 503 shipmentRouteSegment.set("actualServiceCost", Double.valueOf(serviceOptionsMonetaryValue)); 504 } catch (NumberFormatException e) { 505 String excErrMsg = "Error parsing the serviceOptionsMonetaryValue [" + serviceOptionsMonetaryValue + "]: " + e.toString(); 506 Debug.logError(e, excErrMsg, module); 507 errorList.add(excErrMsg); 508 } 509 try { 510 shipmentRouteSegment.set("actualCost", Double.valueOf(totalMonetaryValue)); 511 } catch (NumberFormatException e) { 512 String excErrMsg = "Error parsing the totalMonetaryValue [" + totalMonetaryValue + "]: " + e.toString(); 513 Debug.logError(e, excErrMsg, module); 514 errorList.add(excErrMsg); 515 } 516 517 Element billingWeightElement = UtilXml.firstChildElement(shipmentConfirmResponseElement, "BillingWeight"); 519 Element billingWeightUnitOfMeasurementElement = UtilXml.firstChildElement(billingWeightElement, "UnitOfMeasurement"); 520 String billingWeightUnitOfMeasurement = UtilXml.childElementValue(billingWeightUnitOfMeasurementElement, "Code"); 521 String billingWeight = UtilXml.childElementValue(billingWeightElement, "Weight"); 522 try { 523 shipmentRouteSegment.set("billingWeight", Double.valueOf(billingWeight)); 524 } catch (NumberFormatException e) { 525 String excErrMsg = "Error parsing the billingWeight [" + billingWeight + "]: " + e.toString(); 526 Debug.logError(e, excErrMsg, module); 527 errorList.add(excErrMsg); 528 } 529 shipmentRouteSegment.set("billingWeightUomId", unitsUpsToOfbiz.get(billingWeightUnitOfMeasurement)); 530 531 String shipmentIdentificationNumber = UtilXml.childElementValue(shipmentConfirmResponseElement, "ShipmentIdentificationNumber"); 533 String shipmentDigest = UtilXml.childElementValue(shipmentConfirmResponseElement, "ShipmentDigest"); 534 shipmentRouteSegment.set("trackingIdNumber", shipmentIdentificationNumber); 535 shipmentRouteSegment.set("trackingDigest", shipmentDigest); 536 537 shipmentRouteSegment.put("carrierServiceStatusId", "SHRSCS_CONFIRMED"); 539 540 shipmentRouteSegment.store(); 542 543 545 StringBuffer successString = new StringBuffer ("The UPS ShipmentConfirm succeeded"); 546 if (errorList.size() > 0) { 547 successString.append(", but the following occurred: "); 549 Iterator errorListIter = errorList.iterator(); 550 while (errorListIter.hasNext()) { 551 String errorMsg = (String ) errorListIter.next(); 552 successString.append(errorMsg); 553 if (errorListIter.hasNext()) { 554 successString.append(", "); 555 } 556 } 557 } 558 return ServiceUtil.returnSuccess(successString.toString()); 559 } else { 560 errorList.add(0, "The UPS ShipmentConfirm failed"); 561 return ServiceUtil.returnError(errorList); 562 } 563 } 564 565 public static Map upsShipmentAccept(DispatchContext dctx, Map context) { 566 Map result = new HashMap (); 567 GenericDelegator delegator = dctx.getDelegator(); 568 String shipmentId = (String ) context.get("shipmentId"); 569 String shipmentRouteSegmentId = (String ) context.get("shipmentRouteSegmentId"); 570 571 boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info")); 572 String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path"); 573 File shipmentUpsSaveCertificationFile = null; 574 if (shipmentUpsSaveCertificationInfo) { 575 shipmentUpsSaveCertificationFile = new File (shipmentUpsSaveCertificationPath); 576 if (!shipmentUpsSaveCertificationFile.exists()) { 577 shipmentUpsSaveCertificationFile.mkdirs(); 578 } 579 } 580 581 String shipmentAcceptResponseString = null; 582 583 try { 584 GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); 585 GenericValue shipmentRouteSegment = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); 586 587 if (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) { 588 return ServiceUtil.returnError("ERROR: The Carrier for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is not UPS."); 589 } 590 591 if (!"SHRSCS_CONFIRMED".equals(shipmentRouteSegment.getString("carrierServiceStatusId"))) { 593 return ServiceUtil.returnError("ERROR: The Carrier Service Status for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is [" + shipmentRouteSegment.getString("carrierServiceStatusId") + "], but must be [SHRSCS_CONFIRMED] to perform the UPS Shipment Accept operation."); 594 } 595 596 List shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId")); 597 if (shipmentPackageRouteSegs == null || shipmentPackageRouteSegs.size() == 0) { 598 return ServiceUtil.returnError("No ShipmentPackageRouteSegs found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 599 } 600 601 if (UtilValidate.isEmpty(shipmentRouteSegment.getString("trackingDigest"))) { 602 return ServiceUtil.returnError("ERROR: The trackingDigest was not set for this Route Segment, meaning that a UPS shipment confirm has not been done."); 603 } 604 605 Document shipmentAcceptRequestDoc = UtilXml.makeEmptyXmlDocument("ShipmentAcceptRequest"); 606 Element shipmentAcceptRequestElement = shipmentAcceptRequestDoc.getDocumentElement(); 607 shipmentAcceptRequestElement.setAttribute("xml:lang", "en-US"); 608 609 Element requestElement = UtilXml.addChildElement(shipmentAcceptRequestElement, "Request", shipmentAcceptRequestDoc); 611 612 Element transactionReferenceElement = UtilXml.addChildElement(requestElement, "TransactionReference", shipmentAcceptRequestDoc); 613 UtilXml.addChildElementValue(transactionReferenceElement, "CustomerContext", "ShipAccept / 01", shipmentAcceptRequestDoc); 614 UtilXml.addChildElementValue(transactionReferenceElement, "XpciVersion", "1.0001", shipmentAcceptRequestDoc); 615 616 UtilXml.addChildElementValue(requestElement, "RequestAction", "ShipAccept", shipmentAcceptRequestDoc); 617 UtilXml.addChildElementValue(requestElement, "RequestOption", "01", shipmentAcceptRequestDoc); 618 619 UtilXml.addChildElementValue(shipmentAcceptRequestElement, "ShipmentDigest", shipmentRouteSegment.getString("trackingDigest"), shipmentAcceptRequestDoc); 620 621 622 String shipmentAcceptRequestString = null; 623 try { 624 shipmentAcceptRequestString = UtilXml.writeXmlDocument(shipmentAcceptRequestDoc); 625 } catch (IOException e) { 626 String ioeErrMsg = "Error writing the ShipmentAcceptRequest XML Document to a String: " + e.toString(); 627 Debug.logError(e, ioeErrMsg, module); 628 return ServiceUtil.returnError(ioeErrMsg); 629 } 630 631 Document accessRequestDocument = createAccessRequestDocument(); 633 String accessRequestString = null; 634 try { 635 accessRequestString = UtilXml.writeXmlDocument(accessRequestDocument); 636 } catch (IOException e) { 637 String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString(); 638 Debug.logError(e, ioeErrMsg, module); 639 return ServiceUtil.returnError(ioeErrMsg); 640 } 641 642 StringBuffer xmlString = new StringBuffer (); 646 xmlString.append(accessRequestString); 648 xmlString.append(shipmentAcceptRequestString); 649 650 if (shipmentUpsSaveCertificationInfo) { 651 String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentAcceptRequest" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 652 try { 653 FileOutputStream fileOut = new FileOutputStream (outFileName); 654 fileOut.write(xmlString.toString().getBytes()); 655 fileOut.flush(); 656 fileOut.close(); 657 } catch (IOException e) { 658 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 659 } 660 } 661 662 try { 663 shipmentAcceptResponseString = sendUpsRequest("ShipAccept", xmlString.toString()); 664 } catch (UpsConnectException e) { 665 String uceErrMsg = "Error sending UPS request for UPS Service ShipAccept: " + e.toString(); 666 Debug.logError(e, uceErrMsg, module); 667 return ServiceUtil.returnError(uceErrMsg); 668 } 669 670 if (shipmentUpsSaveCertificationInfo) { 671 String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentAcceptResponse" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 672 try { 673 FileOutputStream fileOut = new FileOutputStream (outFileName); 674 fileOut.write(shipmentAcceptResponseString.getBytes()); 675 fileOut.flush(); 676 fileOut.close(); 677 } catch (IOException e) { 678 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 679 } 680 } 681 682 Document shipmentAcceptResponseDocument = null; 683 try { 684 shipmentAcceptResponseDocument = UtilXml.readXmlDocument(shipmentAcceptResponseString, false); 685 } catch (SAXException e2) { 686 String excErrMsg = "Error parsing the ShipmentAcceptResponse: " + e2.toString(); 687 Debug.logError(e2, excErrMsg, module); 688 return ServiceUtil.returnError(excErrMsg); 689 } catch (ParserConfigurationException e2) { 690 String excErrMsg = "Error parsing the ShipmentAcceptResponse: " + e2.toString(); 691 Debug.logError(e2, excErrMsg, module); 692 return ServiceUtil.returnError(excErrMsg); 693 } catch (IOException e2) { 694 String excErrMsg = "Error parsing the ShipmentAcceptResponse: " + e2.toString(); 695 Debug.logError(e2, excErrMsg, module); 696 return ServiceUtil.returnError(excErrMsg); 697 } 698 699 return handleUpsShipmentAcceptResponse(shipmentAcceptResponseDocument, shipmentRouteSegment, shipmentPackageRouteSegs); 700 } catch (GenericEntityException e) { 701 Debug.logError(e, module); 702 return ServiceUtil.returnError("Error reading or writing Shipment data for UPS Shipment Accept: " + e.toString()); 703 } 704 } 705 706 public static Map handleUpsShipmentAcceptResponse(Document shipmentAcceptResponseDocument, GenericValue shipmentRouteSegment, List shipmentPackageRouteSegs) throws GenericEntityException { 707 boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info")); 708 String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path"); 709 File shipmentUpsSaveCertificationFile = null; 710 if (shipmentUpsSaveCertificationInfo) { 711 shipmentUpsSaveCertificationFile = new File (shipmentUpsSaveCertificationPath); 712 if (!shipmentUpsSaveCertificationFile.exists()) { 713 shipmentUpsSaveCertificationFile.mkdirs(); 714 } 715 } 716 717 Element shipmentAcceptResponseElement = shipmentAcceptResponseDocument.getDocumentElement(); 719 720 Element responseElement = UtilXml.firstChildElement(shipmentAcceptResponseElement, "Response"); 722 Element responseTransactionReferenceElement = UtilXml.firstChildElement(responseElement, "TransactionReference"); 723 String responseTransactionReferenceCustomerContext = UtilXml.childElementValue(responseTransactionReferenceElement, "CustomerContext"); 724 String responseTransactionReferenceXpciVersion = UtilXml.childElementValue(responseTransactionReferenceElement, "XpciVersion"); 725 726 String responseStatusCode = UtilXml.childElementValue(responseElement, "ResponseStatusCode"); 727 String responseStatusDescription = UtilXml.childElementValue(responseElement, "ResponseStatusDescription"); 728 List errorList = new LinkedList (); 729 UpsServices.handleErrors(responseElement, errorList); 730 731 if ("1".equals(responseStatusCode)) { 732 Element shipmentResultsElement = UtilXml.firstChildElement(shipmentAcceptResponseElement, "ShipmentResults"); 733 734 738 739 Element shipmentChargesElement = UtilXml.firstChildElement(shipmentResultsElement, "ShipmentCharges"); 741 742 Element transportationChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "TransportationCharges"); 743 String transportationCurrencyCode = UtilXml.childElementValue(transportationChargesElement, "CurrencyCode"); 744 String transportationMonetaryValue = UtilXml.childElementValue(transportationChargesElement, "MonetaryValue"); 745 746 Element serviceOptionsChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "ServiceOptionsCharges"); 747 String serviceOptionsCurrencyCode = UtilXml.childElementValue(serviceOptionsChargesElement, "CurrencyCode"); 748 String serviceOptionsMonetaryValue = UtilXml.childElementValue(serviceOptionsChargesElement, "MonetaryValue"); 749 750 Element totalChargesElement = UtilXml.firstChildElement(shipmentChargesElement, "TotalCharges"); 751 String totalCurrencyCode = UtilXml.childElementValue(totalChargesElement, "CurrencyCode"); 752 String totalMonetaryValue = UtilXml.childElementValue(totalChargesElement, "MonetaryValue"); 753 754 if (UtilValidate.isNotEmpty(totalCurrencyCode)) { 755 if (UtilValidate.isEmpty(shipmentRouteSegment.getString("currencyUomId"))) { 756 shipmentRouteSegment.set("currencyUomId", totalCurrencyCode); 757 } else if(!totalCurrencyCode.equals(shipmentRouteSegment.getString("currencyUomId"))) { 758 errorList.add("The Currency Unit of Measure returned [" + totalCurrencyCode + "] is not the same as the original [" + shipmentRouteSegment.getString("currencyUomId") + "], setting to the new one."); 759 shipmentRouteSegment.set("currencyUomId", totalCurrencyCode); 760 } 761 } 762 763 try { 764 shipmentRouteSegment.set("actualTransportCost", Double.valueOf(transportationMonetaryValue)); 765 } catch (NumberFormatException e) { 766 String excErrMsg = "Error parsing the transportationMonetaryValue [" + transportationMonetaryValue + "]: " + e.toString(); 767 Debug.logError(e, excErrMsg, module); 768 errorList.add(excErrMsg); 769 } 770 try { 771 shipmentRouteSegment.set("actualServiceCost", Double.valueOf(serviceOptionsMonetaryValue)); 772 } catch (NumberFormatException e) { 773 String excErrMsg = "Error parsing the serviceOptionsMonetaryValue [" + serviceOptionsMonetaryValue + "]: " + e.toString(); 774 Debug.logError(e, excErrMsg, module); 775 errorList.add(excErrMsg); 776 } 777 try { 778 shipmentRouteSegment.set("actualCost", Double.valueOf(totalMonetaryValue)); 779 } catch (NumberFormatException e) { 780 String excErrMsg = "Error parsing the totalMonetaryValue [" + totalMonetaryValue + "]: " + e.toString(); 781 Debug.logError(e, excErrMsg, module); 782 errorList.add(excErrMsg); 783 } 784 785 Element billingWeightElement = UtilXml.firstChildElement(shipmentResultsElement, "BillingWeight"); 787 Element billingWeightUnitOfMeasurementElement = UtilXml.firstChildElement(billingWeightElement, "UnitOfMeasurement"); 788 String billingWeightUnitOfMeasurement = UtilXml.childElementValue(billingWeightUnitOfMeasurementElement, "Code"); 789 String billingWeight = UtilXml.childElementValue(billingWeightElement, "Weight"); 790 try { 791 shipmentRouteSegment.set("billingWeight", Double.valueOf(billingWeight)); 792 } catch (NumberFormatException e) { 793 String excErrMsg = "Error parsing the billingWeight [" + billingWeight + "]: " + e.toString(); 794 Debug.logError(e, excErrMsg, module); 795 errorList.add(excErrMsg); 796 } 797 shipmentRouteSegment.set("billingWeightUomId", unitsUpsToOfbiz.get(billingWeightUnitOfMeasurement)); 798 799 String shipmentIdentificationNumber = UtilXml.childElementValue(shipmentResultsElement, "ShipmentIdentificationNumber"); 801 shipmentRouteSegment.set("trackingIdNumber", shipmentIdentificationNumber); 803 804 shipmentRouteSegment.put("carrierServiceStatusId", "SHRSCS_ACCEPTED"); 806 807 shipmentRouteSegment.store(); 809 810 List packageResultsElements = UtilXml.childElementList(shipmentResultsElement, "PackageResults"); 812 Iterator packageResultsElementIter = packageResultsElements.iterator(); 813 Iterator shipmentPackageRouteSegIter = shipmentPackageRouteSegs.iterator(); 814 while (packageResultsElementIter.hasNext()) { 815 Element packageResultsElement = (Element ) packageResultsElementIter.next(); 816 817 String trackingNumber = UtilXml.childElementValue(packageResultsElement, "TrackingNumber"); 818 819 Element packageServiceOptionsChargesElement = UtilXml.firstChildElement(packageResultsElement, "ServiceOptionsCharges"); 820 String packageServiceOptionsCurrencyCode = UtilXml.childElementValue(packageServiceOptionsChargesElement, "CurrencyCode"); 821 String packageServiceOptionsMonetaryValue = UtilXml.childElementValue(packageServiceOptionsChargesElement, "MonetaryValue"); 822 823 Element packageLabelImageElement = UtilXml.firstChildElement(packageResultsElement, "LabelImage"); 824 Element packageLabelImageFormatElement = UtilXml.firstChildElement(packageResultsElement, "LabelImageFormat"); 825 String packageLabelImageFormatCode = UtilXml.childElementValue(packageLabelImageFormatElement, "Code"); 827 String packageLabelImageFormatDescription = UtilXml.childElementValue(packageLabelImageFormatElement, "Description"); 828 String packageLabelGraphicImageString = UtilXml.childElementValue(packageLabelImageElement, "GraphicImage"); 829 String packageLabelInternationalSignatureGraphicImageString = UtilXml.childElementValue(packageLabelImageElement, "InternationalSignatureGraphicImage"); 830 String packageLabelHTMLImageString = UtilXml.childElementValue(packageLabelImageElement, "HTMLImage"); 831 832 if (!shipmentPackageRouteSegIter.hasNext()) { 833 errorList.add("Error: More PackageResults were returned than there are Packages on this Shipment; the TrackingNumber is [" + trackingNumber + "], the ServiceOptionsCharges were " + packageServiceOptionsMonetaryValue + packageServiceOptionsCurrencyCode); 834 continue; 836 } 837 838 GenericValue shipmentPackageRouteSeg = (GenericValue) shipmentPackageRouteSegIter.next(); 840 shipmentPackageRouteSeg.set("trackingCode", trackingNumber); 841 shipmentPackageRouteSeg.set("boxNumber", ""); 842 shipmentPackageRouteSeg.set("currencyUomId", packageServiceOptionsCurrencyCode); 843 try { 844 shipmentPackageRouteSeg.set("packageServiceCost", Double.valueOf(packageServiceOptionsMonetaryValue)); 845 } catch (NumberFormatException e) { 846 String excErrMsg = "Error parsing the packageServiceOptionsMonetaryValue [" + packageServiceOptionsMonetaryValue + "] for Package [" + shipmentPackageRouteSeg.getString("shipmentPackageSeqId") + "]: " + e.toString(); 847 Debug.logError(e, excErrMsg, module); 848 errorList.add(excErrMsg); 849 } 850 851 byte[] labelImageBytes = null; 852 if (packageLabelGraphicImageString != null) { 853 labelImageBytes = Base64.base64Decode(packageLabelGraphicImageString.getBytes()); 854 shipmentPackageRouteSeg.setBytes("labelImage", labelImageBytes); 855 } 856 byte[] labelInternationalSignatureGraphicImageBytes = null; 857 if (packageLabelInternationalSignatureGraphicImageString != null) { 858 labelInternationalSignatureGraphicImageBytes = Base64.base64Decode(packageLabelInternationalSignatureGraphicImageString.getBytes()); 859 shipmentPackageRouteSeg.set("labelIntlSignImage", labelInternationalSignatureGraphicImageBytes); 860 } 861 String packageLabelHTMLImageStringDecoded = Base64.base64Decode(packageLabelHTMLImageString); 862 shipmentPackageRouteSeg.set("labelHtml", packageLabelHTMLImageStringDecoded); 863 864 if (shipmentUpsSaveCertificationInfo) { 865 if (labelImageBytes != null) { 866 String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentLabelImage" + shipmentRouteSegment.getString("shipmentId") + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + "_" + shipmentPackageRouteSeg.getString("shipmentPackageSeqId") + ".gif"; 867 try { 868 FileOutputStream fileOut = new FileOutputStream (outFileName); 869 fileOut.write(labelImageBytes); 870 fileOut.flush(); 871 fileOut.close(); 872 } catch (IOException e) { 873 Debug.log(e, "Could not save UPS LabelImage GIF file: [[[" + packageLabelGraphicImageString + "]]] to file: " + outFileName, module); 874 } 875 } 876 if (labelInternationalSignatureGraphicImageBytes != null) { 877 String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentLabelIntlSignImage" + shipmentRouteSegment.getString("shipmentId") + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + "_" + shipmentPackageRouteSeg.getString("shipmentPackageSeqId") + ".gif"; 878 try { 879 FileOutputStream fileOut = new FileOutputStream (outFileName); 880 fileOut.write(labelInternationalSignatureGraphicImageBytes); 881 fileOut.flush(); 882 fileOut.close(); 883 } catch (IOException e) { 884 Debug.log(e, "Could not save UPS IntlSign LabelImage GIF file: [[[" + packageLabelInternationalSignatureGraphicImageString + "]]] to file: " + outFileName, module); 885 } 886 } 887 if (packageLabelHTMLImageStringDecoded != null) { 888 String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentLabelHTMLImage" + shipmentRouteSegment.getString("shipmentId") + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + "_" + shipmentPackageRouteSeg.getString("shipmentPackageSeqId") + ".html"; 889 try { 890 FileOutputStream fileOut = new FileOutputStream (outFileName); 891 fileOut.write(packageLabelHTMLImageStringDecoded.getBytes()); 892 fileOut.flush(); 893 fileOut.close(); 894 } catch (IOException e) { 895 Debug.log(e, "Could not save UPS LabelImage HTML file: [[[" + packageLabelHTMLImageStringDecoded + "]]] to file: " + outFileName, module); 896 } 897 } 898 } 899 900 shipmentPackageRouteSeg.store(); 901 } 902 903 if (shipmentPackageRouteSegIter.hasNext()) { 904 errorList.add("Error: There are more Packages on this Shipment than there were PackageResults returned from UPS"); 905 while (shipmentPackageRouteSegIter.hasNext()) { 906 GenericValue shipmentPackageRouteSeg = (GenericValue) shipmentPackageRouteSegIter.next(); 907 errorList.add("Error: No PackageResults were returned for the Package [" + shipmentPackageRouteSeg.getString("shipmentPackageSeqId") + "]"); 908 } 909 } 910 911 StringBuffer successString = new StringBuffer ("The UPS ShipmentAccept succeeded"); 913 if (errorList.size() > 0) { 914 successString.append(", but the following occurred: "); 916 Iterator errorListIter = errorList.iterator(); 917 while (errorListIter.hasNext()) { 918 String errorMsg = (String ) errorListIter.next(); 919 successString.append(errorMsg); 920 if (errorListIter.hasNext()) { 921 successString.append(", "); 922 } 923 } 924 } 925 return ServiceUtil.returnSuccess(successString.toString()); 926 } else { 927 errorList.add(0, "The UPS ShipmentConfirm failed"); 928 return ServiceUtil.returnError(errorList); 929 } 930 } 931 932 public static Map upsVoidShipment(DispatchContext dctx, Map context) { 933 Map result = new HashMap (); 934 GenericDelegator delegator = dctx.getDelegator(); 935 String shipmentId = (String ) context.get("shipmentId"); 936 String shipmentRouteSegmentId = (String ) context.get("shipmentRouteSegmentId"); 937 938 boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info")); 939 String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path"); 940 File shipmentUpsSaveCertificationFile = null; 941 if (shipmentUpsSaveCertificationInfo) { 942 shipmentUpsSaveCertificationFile = new File (shipmentUpsSaveCertificationPath); 943 if (!shipmentUpsSaveCertificationFile.exists()) { 944 shipmentUpsSaveCertificationFile.mkdirs(); 945 } 946 } 947 948 String voidShipmentResponseString = null; 949 950 try { 951 GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); 952 GenericValue shipmentRouteSegment = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); 953 954 if (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) { 955 return ServiceUtil.returnError("ERROR: The Carrier for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is not UPS."); 956 } 957 958 if (!"SHRSCS_CONFIRMED".equals(shipmentRouteSegment.getString("carrierServiceStatusId")) && 960 !"SHRSCS_ACCEPTED".equals(shipmentRouteSegment.getString("carrierServiceStatusId"))) { 961 return ServiceUtil.returnError("ERROR: The Carrier Service Status for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is [" + shipmentRouteSegment.getString("carrierServiceStatusId") + "], but must be [SHRSCS_CONFIRMED] or [SHRSCS_ACCEPTED] to perform the UPS Void Shipment operation."); 962 } 963 964 if (UtilValidate.isEmpty(shipmentRouteSegment.getString("trackingIdNumber"))) { 965 return ServiceUtil.returnError("ERROR: The trackingIdNumber was not set for this Route Segment, meaning that a UPS shipment confirm has not been done."); 966 } 967 968 Document voidShipmentRequestDoc = UtilXml.makeEmptyXmlDocument("VoidShipmentRequest"); 969 Element voidShipmentRequestElement = voidShipmentRequestDoc.getDocumentElement(); 970 voidShipmentRequestElement.setAttribute("xml:lang", "en-US"); 971 972 Element requestElement = UtilXml.addChildElement(voidShipmentRequestElement, "Request", voidShipmentRequestDoc); 974 975 Element transactionReferenceElement = UtilXml.addChildElement(requestElement, "TransactionReference", voidShipmentRequestDoc); 976 UtilXml.addChildElementValue(transactionReferenceElement, "CustomerContext", "Void / 1", voidShipmentRequestDoc); 977 UtilXml.addChildElementValue(transactionReferenceElement, "XpciVersion", "1.0001", voidShipmentRequestDoc); 978 979 UtilXml.addChildElementValue(requestElement, "RequestAction", "Void", voidShipmentRequestDoc); 980 UtilXml.addChildElementValue(requestElement, "RequestOption", "1", voidShipmentRequestDoc); 981 982 UtilXml.addChildElementValue(voidShipmentRequestElement, "ShipmentIdentificationNumber", shipmentRouteSegment.getString("trackingIdNumber"), voidShipmentRequestDoc); 983 984 String voidShipmentRequestString = null; 985 try { 986 voidShipmentRequestString = UtilXml.writeXmlDocument(voidShipmentRequestDoc); 987 } catch (IOException e) { 988 String ioeErrMsg = "Error writing the VoidShipmentRequest XML Document to a String: " + e.toString(); 989 Debug.logError(e, ioeErrMsg, module); 990 return ServiceUtil.returnError(ioeErrMsg); 991 } 992 993 Document accessRequestDocument = createAccessRequestDocument(); 995 String accessRequestString = null; 996 try { 997 accessRequestString = UtilXml.writeXmlDocument(accessRequestDocument); 998 } catch (IOException e) { 999 String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString(); 1000 Debug.logError(e, ioeErrMsg, module); 1001 return ServiceUtil.returnError(ioeErrMsg); 1002 } 1003 1004 StringBuffer xmlString = new StringBuffer (); 1008 xmlString.append(accessRequestString); 1010 xmlString.append(voidShipmentRequestString); 1011 1012 if (shipmentUpsSaveCertificationInfo) { 1013 String outFileName = shipmentUpsSaveCertificationPath + "/UpsVoidShipmentRequest" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 1014 try { 1015 FileOutputStream fileOut = new FileOutputStream (outFileName); 1016 fileOut.write(xmlString.toString().getBytes()); 1017 fileOut.flush(); 1018 fileOut.close(); 1019 } catch (IOException e) { 1020 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 1021 } 1022 } 1023 1024 try { 1025 voidShipmentResponseString = sendUpsRequest("Void", xmlString.toString()); 1026 } catch (UpsConnectException e) { 1027 String uceErrMsg = "Error sending UPS request for UPS Service Void: " + e.toString(); 1028 Debug.logError(e, uceErrMsg, module); 1029 return ServiceUtil.returnError(uceErrMsg); 1030 } 1031 1032 if (shipmentUpsSaveCertificationInfo) { 1033 String outFileName = shipmentUpsSaveCertificationPath + "/UpsVoidShipmentResponse" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 1034 try { 1035 FileOutputStream fileOut = new FileOutputStream (outFileName); 1036 fileOut.write(voidShipmentResponseString.getBytes()); 1037 fileOut.flush(); 1038 fileOut.close(); 1039 } catch (IOException e) { 1040 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 1041 } 1042 } 1043 1044 Document voidShipmentResponseDocument = null; 1045 try { 1046 voidShipmentResponseDocument = UtilXml.readXmlDocument(voidShipmentResponseString, false); 1047 } catch (SAXException e2) { 1048 String excErrMsg = "Error parsing the VoidShipmentResponse: " + e2.toString(); 1049 Debug.logError(e2, excErrMsg, module); 1050 return ServiceUtil.returnError(excErrMsg); 1051 } catch (ParserConfigurationException e2) { 1052 String excErrMsg = "Error parsing the VoidShipmentResponse: " + e2.toString(); 1053 Debug.logError(e2, excErrMsg, module); 1054 return ServiceUtil.returnError(excErrMsg); 1055 } catch (IOException e2) { 1056 String excErrMsg = "Error parsing the VoidShipmentResponse: " + e2.toString(); 1057 Debug.logError(e2, excErrMsg, module); 1058 return ServiceUtil.returnError(excErrMsg); 1059 } 1060 1061 return handleUpsVoidShipmentResponse(voidShipmentResponseDocument, shipmentRouteSegment); 1062 } catch (GenericEntityException e) { 1063 Debug.logError(e, module); 1064 return ServiceUtil.returnError("Error reading or writing Shipment data for UPS Void Shipment: " + e.toString()); 1065 } 1066 } 1067 1068 public static Map handleUpsVoidShipmentResponse(Document voidShipmentResponseDocument, GenericValue shipmentRouteSegment) throws GenericEntityException { 1069 Element voidShipmentResponseElement = voidShipmentResponseDocument.getDocumentElement(); 1071 1072 Element responseElement = UtilXml.firstChildElement(voidShipmentResponseElement, "Response"); 1074 Element responseTransactionReferenceElement = UtilXml.firstChildElement(responseElement, "TransactionReference"); 1075 String responseTransactionReferenceCustomerContext = UtilXml.childElementValue(responseTransactionReferenceElement, "CustomerContext"); 1076 String responseTransactionReferenceXpciVersion = UtilXml.childElementValue(responseTransactionReferenceElement, "XpciVersion"); 1077 1078 String responseStatusCode = UtilXml.childElementValue(responseElement, "ResponseStatusCode"); 1079 String responseStatusDescription = UtilXml.childElementValue(responseElement, "ResponseStatusDescription"); 1080 List errorList = new LinkedList (); 1081 UpsServices.handleErrors(responseElement, errorList); 1082 1083 Element statusElement = UtilXml.firstChildElement(voidShipmentResponseElement, "Status"); 1085 1086 Element statusTypeElement = UtilXml.firstChildElement(statusElement, "StatusType"); 1087 String statusTypeCode = UtilXml.childElementValue(statusTypeElement, "Code"); 1088 String statusTypeDescription = UtilXml.childElementValue(statusTypeElement, "Description"); 1089 1090 Element statusCodeElement = UtilXml.firstChildElement(statusElement, "StatusCode"); 1091 String statusCodeCode = UtilXml.childElementValue(statusCodeElement, "Code"); 1092 String statusCodeDescription = UtilXml.childElementValue(statusCodeElement, "Description"); 1093 1094 if ("1".equals(responseStatusCode)) { 1095 shipmentRouteSegment.put("carrierServiceStatusId", "SHRSCS_VOIDED"); 1097 shipmentRouteSegment.store(); 1098 1099 StringBuffer successString = new StringBuffer ("The UPS VoidShipment succeeded; the StatusType is: [" + statusTypeCode + ":" + statusTypeDescription + "], the StatusCode is: [" + statusCodeCode + ":" + statusCodeDescription + "]"); 1101 if (errorList.size() > 0) { 1102 successString.append(", but the following occurred: "); 1104 Iterator errorListIter = errorList.iterator(); 1105 while (errorListIter.hasNext()) { 1106 String errorMsg = (String ) errorListIter.next(); 1107 successString.append(errorMsg); 1108 if (errorListIter.hasNext()) { 1109 successString.append(", "); 1110 } 1111 } 1112 } 1113 return ServiceUtil.returnSuccess(successString.toString()); 1114 } else { 1115 errorList.add(0, "The UPS ShipmentConfirm failed; the StatusType is: [" + statusTypeCode + ":" + statusTypeDescription + "], the StatusCode is: [" + statusCodeCode + ":" + statusCodeDescription + "]"); 1116 return ServiceUtil.returnError(errorList); 1117 } 1118 } 1119 1120 public static Map upsTrackShipment(DispatchContext dctx, Map context) { 1121 Map result = new HashMap (); 1122 GenericDelegator delegator = dctx.getDelegator(); 1123 String shipmentId = (String ) context.get("shipmentId"); 1124 String shipmentRouteSegmentId = (String ) context.get("shipmentRouteSegmentId"); 1125 1126 boolean shipmentUpsSaveCertificationInfo = "true".equals(UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.info")); 1127 String shipmentUpsSaveCertificationPath = UtilProperties.getPropertyValue("shipment", "shipment.ups.save.certification.path"); 1128 File shipmentUpsSaveCertificationFile = null; 1129 if (shipmentUpsSaveCertificationInfo) { 1130 shipmentUpsSaveCertificationFile = new File (shipmentUpsSaveCertificationPath); 1131 if (!shipmentUpsSaveCertificationFile.exists()) { 1132 shipmentUpsSaveCertificationFile.mkdirs(); 1133 } 1134 } 1135 1136 String trackResponseString = null; 1137 1138 try { 1139 GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); 1140 GenericValue shipmentRouteSegment = delegator.findByPrimaryKey("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId)); 1141 1142 if (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) { 1143 return ServiceUtil.returnError("ERROR: The Carrier for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is not UPS."); 1144 } 1145 1146 if (!"SHRSCS_ACCEPTED".equals(shipmentRouteSegment.getString("carrierServiceStatusId"))) { 1148 return ServiceUtil.returnError("ERROR: The Carrier Service Status for ShipmentRouteSegment " + shipmentRouteSegmentId + " of Shipment " + shipmentId + ", is [" + shipmentRouteSegment.getString("carrierServiceStatusId") + "], but must be [SHRSCS_ACCEPTED] to perform the UPS Track Shipment operation."); 1149 } 1150 1151 List shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId")); 1152 if (shipmentPackageRouteSegs == null || shipmentPackageRouteSegs.size() == 0) { 1153 return ServiceUtil.returnError("No ShipmentPackageRouteSegs found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId); 1154 } 1155 1156 if (UtilValidate.isEmpty(shipmentRouteSegment.getString("trackingIdNumber"))) { 1157 return ServiceUtil.returnError("ERROR: The trackingIdNumber was not set for this Route Segment, meaning that a UPS shipment confirm has not been done."); 1158 } 1159 1160 Document trackRequestDoc = UtilXml.makeEmptyXmlDocument("TrackRequest"); 1161 Element trackRequestElement = trackRequestDoc.getDocumentElement(); 1162 trackRequestElement.setAttribute("xml:lang", "en-US"); 1163 1164 Element requestElement = UtilXml.addChildElement(trackRequestElement, "Request", trackRequestDoc); 1166 1167 Element transactionReferenceElement = UtilXml.addChildElement(requestElement, "TransactionReference", trackRequestDoc); 1168 UtilXml.addChildElementValue(transactionReferenceElement, "CustomerContext", "Track", trackRequestDoc); 1169 UtilXml.addChildElementValue(transactionReferenceElement, "XpciVersion", "1.0001", trackRequestDoc); 1170 1171 UtilXml.addChildElementValue(requestElement, "RequestAction", "Track", trackRequestDoc); 1172 1173 UtilXml.addChildElementValue(trackRequestElement, "ShipmentIdentificationNumber", shipmentRouteSegment.getString("trackingIdNumber"), trackRequestDoc); 1174 1175 String trackRequestString = null; 1176 try { 1177 trackRequestString = UtilXml.writeXmlDocument(trackRequestDoc); 1178 } catch (IOException e) { 1179 String ioeErrMsg = "Error writing the TrackRequest XML Document to a String: " + e.toString(); 1180 Debug.logError(e, ioeErrMsg, module); 1181 return ServiceUtil.returnError(ioeErrMsg); 1182 } 1183 1184 Document accessRequestDocument = createAccessRequestDocument(); 1186 String accessRequestString = null; 1187 try { 1188 accessRequestString = UtilXml.writeXmlDocument(accessRequestDocument); 1189 } catch (IOException e) { 1190 String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString(); 1191 Debug.logError(e, ioeErrMsg, module); 1192 return ServiceUtil.returnError(ioeErrMsg); 1193 } 1194 1195 StringBuffer xmlString = new StringBuffer (); 1199 xmlString.append(accessRequestString); 1201 xmlString.append(trackRequestString); 1202 1203 if (shipmentUpsSaveCertificationInfo) { 1204 String outFileName = shipmentUpsSaveCertificationPath + "/UpsTrackRequest" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 1205 try { 1206 FileOutputStream fileOut = new FileOutputStream (outFileName); 1207 fileOut.write(xmlString.toString().getBytes()); 1208 fileOut.flush(); 1209 fileOut.close(); 1210 } catch (IOException e) { 1211 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 1212 } 1213 } 1214 1215 try { 1216 trackResponseString = sendUpsRequest("Track", xmlString.toString()); 1217 } catch (UpsConnectException e) { 1218 String uceErrMsg = "Error sending UPS request for UPS Service Track: " + e.toString(); 1219 Debug.logError(e, uceErrMsg, module); 1220 return ServiceUtil.returnError(uceErrMsg); 1221 } 1222 1223 if (shipmentUpsSaveCertificationInfo) { 1224 String outFileName = shipmentUpsSaveCertificationPath + "/UpsTrackResponseString" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml"; 1225 try { 1226 FileOutputStream fileOut = new FileOutputStream (outFileName); 1227 fileOut.write(trackResponseString.getBytes()); 1228 fileOut.flush(); 1229 fileOut.close(); 1230 } catch (IOException e) { 1231 Debug.log(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module); 1232 } 1233 } 1234 1235 Document trackResponseDocument = null; 1236 try { 1237 trackResponseDocument = UtilXml.readXmlDocument(trackResponseString, false); 1238 } catch (SAXException e2) { 1239 String excErrMsg = "Error parsing the TrackResponse: " + e2.toString(); 1240 Debug.logError(e2, excErrMsg, module); 1241 return ServiceUtil.returnError(excErrMsg); 1242 } catch (ParserConfigurationException e2) { 1243 String excErrMsg = "Error parsing the TrackResponse: " + e2.toString(); 1244 Debug.logError(e2, excErrMsg, module); 1245 return ServiceUtil.returnError(excErrMsg); 1246 } catch (IOException e2) { 1247 String excErrMsg = "Error parsing the TrackResponse: " + e2.toString(); 1248 Debug.logError(e2, excErrMsg, module); 1249 return ServiceUtil.returnError(excErrMsg); 1250 } 1251 1252 return handleUpsTrackShipmentResponse(trackResponseDocument, shipmentRouteSegment, shipmentPackageRouteSegs); 1253 } catch (GenericEntityException e) { 1254 Debug.logError(e, module); 1255 return ServiceUtil.returnError("Error reading or writing Shipment data for UPS Track Shipment: " + e.toString()); 1256 } 1257 } 1258 1259 public static Map handleUpsTrackShipmentResponse(Document trackResponseDocument, GenericValue shipmentRouteSegment, List shipmentPackageRouteSegs) throws GenericEntityException { 1260 Element trackResponseElement = trackResponseDocument.getDocumentElement(); 1262 1263 Element responseElement = UtilXml.firstChildElement(trackResponseElement, "Response"); 1265 Element responseTransactionReferenceElement = UtilXml.firstChildElement(responseElement, "TransactionReference"); 1266 String responseTransactionReferenceCustomerContext = UtilXml.childElementValue(responseTransactionReferenceElement, "CustomerContext"); 1267 String responseTransactionReferenceXpciVersion = UtilXml.childElementValue(responseTransactionReferenceElement, "XpciVersion"); 1268 1269 String responseStatusCode = UtilXml.childElementValue(responseElement, "ResponseStatusCode"); 1270 String responseStatusDescription = UtilXml.childElementValue(responseElement, "ResponseStatusDescription"); 1271 List errorList = new LinkedList (); 1272 UpsServices.handleErrors(responseElement, errorList); 1273 1274 if ("1".equals(responseStatusCode)) { 1275 Element shipmentElement = UtilXml.firstChildElement(trackResponseElement, "Shipment"); 1277 1278 Element shipperElement = UtilXml.firstChildElement(shipmentElement, "Shipper"); 1279 String shipperNumber = UtilXml.childElementValue(shipperElement, "ShipperNumber"); 1280 1281 Element serviceElement = UtilXml.firstChildElement(shipmentElement, "Service"); 1282 String serviceCode = UtilXml.childElementValue(serviceElement, "Code"); 1283 String serviceDescription = UtilXml.childElementValue(serviceElement, "Description"); 1284 1285 String shipmentIdentificationNumber = UtilXml.childElementValue(shipmentElement, "ShipmentIdentificationNumber"); 1286 1287 List packageElements = UtilXml.childElementList(shipmentElement, "Package"); 1288 Iterator packageElementIter = packageElements.iterator(); 1289 while (packageElementIter.hasNext()) { 1290 Element packageElement = (Element ) packageElementIter.next(); 1291 } 1292 1327 1328 1329 StringBuffer successString = new StringBuffer ("The UPS TrackShipment succeeded"); 1331 if (errorList.size() > 0) { 1332 successString.append(", but the following occurred: "); 1334 Iterator errorListIter = errorList.iterator(); 1335 while (errorListIter.hasNext()) { 1336 String errorMsg = (String ) errorListIter.next(); 1337 successString.append(errorMsg); 1338 if (errorListIter.hasNext()) { 1339 successString.append(", "); 1340 } 1341 } 1342 } 1343 return ServiceUtil.returnSuccess(successString.toString()); 1344 } else { 1345 errorList.add(0, "The UPS ShipmentConfirm failed"); 1346 return ServiceUtil.returnError(errorList); 1347 } 1348 } 1349 1350 public static Map upsRateInquire(DispatchContext dctx, Map context) { 1351 GenericDelegator delegator = dctx.getDelegator(); 1352 String serviceConfigProps = (String ) context.get("serviceConfigProps"); 1354 String upsRateInquireMode = (String ) context.get("upsRateInquireMode"); 1355 String productStoreId = (String ) context.get("productStoreId"); 1356 String carrierRoleTypeId = (String ) context.get("carrierRoleTypeId"); 1357 String carrierPartyId = (String ) context.get("carrierPartyId"); 1358 String shipmentMethodTypeId = (String ) context.get("shipmentMethodTypeId"); 1359 String shippingContactMechId = (String ) context.get("shippingContactMechId"); 1360 1361 List shippableItemInfo = (List ) context.get("shippableItemInfo"); 1362 Double shippableTotal = (Double ) context.get("shippableTotal"); 1363 Double shippableQuantity = (Double ) context.get("shippableQuantity"); 1364 Double shippableWeight = (Double ) context.get("shippableWeight"); 1365 1366 if (shippableTotal == null) { 1367 shippableTotal = new Double (0.00); 1368 } 1369 if (shippableQuantity == null) { 1370 shippableQuantity = new Double (0.00); 1371 } 1372 if (shippableWeight == null) { 1373 shippableWeight = new Double (0.00); 1374 } 1375 if (serviceConfigProps == null) { 1376 serviceConfigProps = "shipment.properties"; 1377 } 1378 if (upsRateInquireMode == null || !"Shop".equals(upsRateInquireMode)) { 1379 Debug.logWarning("No upsRateInquireMode set, defaulting to 'Rate'", module); 1381 upsRateInquireMode = "Rate"; 1382 } 1383 1384 String pickupType = UtilProperties.getPropertyValue(serviceConfigProps, "shipment.ups.shipper.pickup.type", "01"); 1386 1387 GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator); 1389 GenericValue shipFromAddress = null; 1390 if (productStore != null && productStore.get("inventoryFacilityId") != null) { 1391 List shipLocs = null; 1392 try { 1393 shipLocs = delegator.findByAnd("FacilityContactMechPurpose", UtilMisc.toMap("facilityId", 1394 productStore.getString("inventoryFacilityId"), "contactMechPurposeTypeId", 1395 "SHIP_ORIG_LOCATION"), UtilMisc.toList("-fromDate")); 1396 } catch (GenericEntityException e) { 1397 Debug.logError(e, module); 1398 } 1399 if (shipLocs != null) { 1400 shipLocs = EntityUtil.filterByDate(shipLocs); 1401 GenericValue purp = EntityUtil.getFirst(shipLocs); 1402 if (purp != null) { 1403 try { 1404 shipFromAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", purp.getString("contactMechId"))); 1405 } catch (GenericEntityException e) { 1406 Debug.logError(e, module); 1407 } 1408 } 1409 } 1410 } 1411 if (shipFromAddress == null) { 1412 return ServiceUtil.returnError("Unable to determine ship-from address"); 1413 } 1414 1415 GenericValue shipToAddress = null; 1417 if (shippingContactMechId != null) { 1418 try { 1419 shipToAddress = delegator.findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId", shippingContactMechId)); 1420 } catch (GenericEntityException e) { 1421 Debug.logError(e, module); 1422 } 1423 } 1424 if (shipToAddress == null) { 1425 return ServiceUtil.returnError("Unable to determine ship-to address"); 1426 } 1427 1428 String serviceCode = null; 1430 if (!"Shop".equals(upsRateInquireMode)) { 1431 GenericValue carrierShipmentMethod = null; 1433 try { 1434 carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod", UtilMisc.toMap("shipmentMethodTypeId", 1435 shipmentMethodTypeId, "partyId", carrierPartyId, "roleTypeId", carrierRoleTypeId)); 1436 } catch (GenericEntityException e) { 1437 Debug.logError(e, module); 1438 } 1439 if (carrierShipmentMethod == null) { 1440 return ServiceUtil.returnError("Unable to locate the shipping method requested"); 1441 } 1442 1443 serviceCode = carrierShipmentMethod.getString("carrierServiceCode"); 1445 } 1446 1447 Document rateRequestDoc = UtilXml.makeEmptyXmlDocument("RatingServiceSelectionRequest"); 1449 Element rateRequestElement = rateRequestDoc.getDocumentElement(); 1450 rateRequestElement.setAttribute("xml:lang", "en-US"); 1451 1452 Element requestElement = UtilXml.addChildElement(rateRequestElement, "Request", rateRequestDoc); 1454 Element transactionReferenceElement = UtilXml.addChildElement(requestElement, "TransactionReference", rateRequestDoc); 1455 UtilXml.addChildElementValue(transactionReferenceElement, "CustomerContext", "Rating and Service", rateRequestDoc); 1456 UtilXml.addChildElementValue(transactionReferenceElement, "XpciVersion", "1.0001", rateRequestDoc); 1457 1458 UtilXml.addChildElementValue(requestElement, "RequestAction", "Rate", rateRequestDoc); 1460 UtilXml.addChildElementValue(requestElement, "RequestOption", upsRateInquireMode, rateRequestDoc); 1461 1462 Element pickupElement = UtilXml.addChildElement(rateRequestElement, "PickupType", rateRequestDoc); 1464 UtilXml.addChildElementValue(pickupElement, "Code", pickupType, rateRequestDoc); 1465 1466 Element shipmentElement = UtilXml.addChildElement(rateRequestElement, "Shipment", rateRequestDoc); 1468 1469 Element shipperElement = UtilXml.addChildElement(shipmentElement, "Shipper", rateRequestDoc); 1471 Element shipperAddrElement = UtilXml.addChildElement(shipperElement, "Address", rateRequestDoc); 1472 UtilXml.addChildElementValue(shipperAddrElement, "PostalCode", shipFromAddress.getString("postalCode"), rateRequestDoc); 1473 1474 Element shiptoElement = UtilXml.addChildElement(shipmentElement, "ShipTo", rateRequestDoc); 1476 Element shiptoAddrElement = UtilXml.addChildElement(shiptoElement, "Address", rateRequestDoc); 1477 UtilXml.addChildElementValue(shiptoAddrElement, "PostalCode", shipToAddress.getString("postalCode"), rateRequestDoc); 1478 1479 if (serviceCode != null) { 1481 Element serviceElement = UtilXml.addChildElement(shipmentElement, "Service", rateRequestDoc); 1482 UtilXml.addChildElementValue(serviceElement, "Code", serviceCode, rateRequestDoc); 1483 } 1484 1485 String maxWeightStr = UtilProperties.getPropertyValue(serviceConfigProps, "shipment.ups.max.estimate.weight", "99"); 1487 double maxWeight = 99; 1488 try { 1489 maxWeight = Double.parseDouble(maxWeightStr); 1490 } catch (NumberFormatException e) { 1491 maxWeight = 99; 1492 } 1493 1494 splitEstimatePackages(rateRequestDoc, shipmentElement, shippableItemInfo, maxWeight); 1495 1496 UtilXml.addChildElement(shipmentElement, "ShipmentServiceOptions", rateRequestDoc); 1498 1499 String rateRequestString = null; 1500 try { 1501 rateRequestString = UtilXml.writeXmlDocument(rateRequestDoc); 1502 } catch (IOException e) { 1503 String ioeErrMsg = "Error writing the RatingServiceSelectionRequest XML Document to a String: " + e.toString(); 1504 Debug.logError(e, ioeErrMsg, module); 1505 return ServiceUtil.returnError(ioeErrMsg); 1506 } 1507 1508 Document accessRequestDocument = createAccessRequestDocument(serviceConfigProps); 1510 String accessRequestString = null; 1511 try { 1512 accessRequestString = UtilXml.writeXmlDocument(accessRequestDocument); 1513 } catch (IOException e) { 1514 String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString(); 1515 Debug.logError(e, ioeErrMsg, module); 1516 return ServiceUtil.returnError(ioeErrMsg); 1517 } 1518 1519 StringBuffer xmlString = new StringBuffer (); 1521 xmlString.append(accessRequestString); 1522 xmlString.append(rateRequestString); 1523 1524 String rateResponseString = null; 1526 try { 1527 rateResponseString = sendUpsRequest("Rate", xmlString.toString()); 1528 } catch (UpsConnectException e) { 1529 String uceErrMsg = "Error sending UPS request for UPS Service Rate: " + e.toString(); 1530 Debug.logError(e, uceErrMsg, module); 1531 return ServiceUtil.returnError(uceErrMsg); 1532 } 1533 1534 Document rateResponseDocument = null; 1535 try { 1536 rateResponseDocument = UtilXml.readXmlDocument(rateResponseString, false); 1537 } catch (SAXException e2) { 1538 String excErrMsg = "Error parsing the RatingServiceSelectionResponse: " + e2.toString(); 1539 Debug.logError(e2, excErrMsg, module); 1540 return ServiceUtil.returnError(excErrMsg); 1541 } catch (ParserConfigurationException e2) { 1542 String excErrMsg = "Error parsing the RatingServiceSelectionResponse: " + e2.toString(); 1543 Debug.logError(e2, excErrMsg, module); 1544 return ServiceUtil.returnError(excErrMsg); 1545 } catch (IOException e2) { 1546 String excErrMsg = "Error parsing the RatingServiceSelectionResponse: " + e2.toString(); 1547 Debug.logError(e2, excErrMsg, module); 1548 return ServiceUtil.returnError(excErrMsg); 1549 } 1550 1551 return handleUpsRateInquireResponse(rateResponseDocument); 1552 1553 } 1554 1555 private static void splitEstimatePackages(Document requestDoc, Element shipmentElement, List shippableItemInfo, double maxWeight) { 1556 List packages = getPackageSplit(shippableItemInfo, maxWeight); 1557 Iterator i = packages.iterator(); 1558 while (i.hasNext()) { 1559 Map packageMap = (Map ) i.next(); 1560 double packageWeight = calcPackageWeight(packageMap, shippableItemInfo, 0); 1561 1562 Element packageElement = UtilXml.addChildElement(shipmentElement, "Package", requestDoc); 1564 Element packagingTypeElement = UtilXml.addChildElement(packageElement, "PackagingType", requestDoc); 1565 UtilXml.addChildElementValue(packagingTypeElement, "Code", "00", requestDoc); 1566 UtilXml.addChildElementValue(packagingTypeElement, "Description", "Unknown PackagingType", requestDoc); 1567 UtilXml.addChildElementValue(packageElement, "Description", "Package Description", requestDoc); 1568 Element packageWeightElement = UtilXml.addChildElement(packageElement, "PackageWeight", requestDoc); 1569 UtilXml.addChildElementValue(packageWeightElement, "Weight", new Double (packageWeight).toString(), requestDoc); 1570 } 1571 } 1572 1573 private static List getPackageSplit(List shippableItemInfo, double maxWeight) { 1574 List packages = new LinkedList (); 1576 1577 if (shippableItemInfo != null) { 1578 Iterator sii = shippableItemInfo.iterator(); 1579 while (sii.hasNext()) { 1580 Map itemInfo = (Map ) sii.next(); 1581 long pieces = ((Long ) itemInfo.get("piecesIncluded")).longValue(); 1582 double totalQuantity = ((Double ) itemInfo.get("quantity")).doubleValue(); 1583 double totalWeight = ((Double ) itemInfo.get("weight")).doubleValue(); 1584 String productId = (String ) itemInfo.get("productId"); 1585 1586 if (pieces < 1) { 1588 pieces = 1; } 1590 double weight = totalWeight / pieces; 1591 1592 for (int z = 1; z <= totalQuantity; z++) { 1593 double partialQty = pieces > 1 ? 1.000 / pieces : 1; 1594 for (long x = 0; x < pieces; x++) { 1595 if (weight >= maxWeight) { 1596 Map newPackage = new HashMap (); 1597 newPackage.put(productId, new Double (partialQty)); 1598 packages.add(newPackage); 1599 } else if (totalWeight > 0) { 1600 if (packages.size() == 0) { 1602 packages.add(new HashMap ()); 1603 } 1604 1605 int packageSize = packages.size(); 1607 boolean addedToPackage = false; 1608 for (int pi = 0; pi < packageSize; pi++) { 1609 if (!addedToPackage) { 1610 Map packageMap = (Map ) packages.get(pi); 1611 double packageWeight = calcPackageWeight(packageMap, shippableItemInfo, weight); 1612 if (packageWeight <= maxWeight) { 1613 Double qtyD = (Double ) packageMap.get(productId); 1614 double qty = qtyD == null ? 0 : qtyD.doubleValue(); 1615 packageMap.put(productId, new Double (qty + partialQty)); 1616 addedToPackage = true; 1617 } 1618 } 1619 } 1620 if (!addedToPackage) { 1621 Map packageMap = new HashMap (); 1622 packageMap.put(productId, new Double (partialQty)); 1623 packages.add(packageMap); 1624 } 1625 } 1626 } 1627 } 1628 } 1629 } 1630 return packages; 1631 } 1632 1633 private static double calcPackageWeight(Map packageMap, List shippableItemInfo, double additionalWeight) { 1634 double totalWeight = 0.00; 1635 Iterator i = packageMap.keySet().iterator(); 1636 while (i.hasNext()) { 1637 String productId = (String ) i.next(); 1638 Map productInfo = getProductItemInfo(shippableItemInfo, productId); 1639 double productWeight = ((Double ) productInfo.get("weight")).doubleValue(); 1640 double quantity = ((Double ) packageMap.get(productId)).doubleValue(); 1641 totalWeight += (productWeight * quantity); 1642 } 1643 return totalWeight + additionalWeight; 1644 } 1645 1646 private static Map getProductItemInfo(List shippableItemInfo, String productId) { 1647 if (shippableItemInfo != null) { 1648 Iterator i = shippableItemInfo.iterator(); 1649 while (i.hasNext()) { 1650 Map testMap = (Map ) i.next(); 1651 String id = (String ) testMap.get("productId"); 1652 if (productId.equals(id)) { 1653 return testMap; 1654 } 1655 } 1656 } 1657 return null; 1658 } 1659 1660 public static Map handleUpsRateInquireResponse(Document rateResponseDocument) { 1661 Element rateResponseElement = rateResponseDocument.getDocumentElement(); 1663 1664 Element responseElement = UtilXml.firstChildElement(rateResponseElement, "Response"); 1666 Element responseTransactionReferenceElement = UtilXml.firstChildElement(responseElement, "TransactionReference"); 1667 String responseTransactionReferenceCustomerContext = UtilXml.childElementValue(responseTransactionReferenceElement, "CustomerContext"); 1668 String responseTransactionReferenceXpciVersion = UtilXml.childElementValue(responseTransactionReferenceElement, "XpciVersion"); 1669 1670 String responseStatusCode = UtilXml.childElementValue(responseElement, "ResponseStatusCode"); 1671 String responseStatusDescription = UtilXml.childElementValue(responseElement, "ResponseStatusDescription"); 1672 List errorList = new LinkedList (); 1673 UpsServices.handleErrors(responseElement, errorList); 1674 1675 if ("1".equals(responseStatusCode)) { 1676 List rates = UtilXml.childElementList(rateResponseElement, "RatedShipment"); 1677 Map rateMap = new HashMap (); 1678 Double firstRate = null; 1679 if (rates == null || rates.size() == 0) { 1680 return ServiceUtil.returnError("No rates available at this time"); 1681 } else { 1682 Iterator i = rates.iterator(); 1683 while (i.hasNext()) { 1684 Element element = (Element ) i.next(); 1685 1686 Element service = UtilXml.firstChildElement(element, "Service"); 1688 String serviceCode = UtilXml.childElementValue(service, "Code"); 1689 1690 Element totalCharges = UtilXml.firstChildElement(element, "TotalCharges"); 1692 String totalString = UtilXml.childElementValue(totalCharges, "MonetaryValue"); 1693 1694 rateMap.put(serviceCode, new Double (totalString)); 1695 if (firstRate == null) { 1696 firstRate = (Double ) rateMap.get(serviceCode); 1697 } 1698 } 1699 } 1700 1701 Debug.log("UPS Rate Map : " + rateMap, module); 1702 1703 Map resp = ServiceUtil.returnSuccess(); 1704 resp.put("upsRateCodeMap", rateMap); 1705 resp.put("shippingEstimateAmount", firstRate); 1706 return resp; 1707 } else { 1708 errorList.add("Error status code : " + responseStatusCode); 1709 return ServiceUtil.returnError(errorList); 1710 } 1711 } 1712 1713 public static Document createAccessRequestDocument() { 1714 return createAccessRequestDocument("shipment.properties"); 1715 } 1716 1717 public static Document createAccessRequestDocument(String props) { 1718 Document accessRequestDocument = UtilXml.makeEmptyXmlDocument("AccessRequest"); 1719 Element accessRequestElement = accessRequestDocument.getDocumentElement(); 1720 UtilXml.addChildElementValue(accessRequestElement, "AccessLicenseNumber", UtilProperties.getPropertyValue(props, "shipment.ups.access.license.number"), accessRequestDocument); 1721 UtilXml.addChildElementValue(accessRequestElement, "UserId", UtilProperties.getPropertyValue(props, "shipment.ups.access.user.id"), accessRequestDocument); 1722 UtilXml.addChildElementValue(accessRequestElement, "Password", UtilProperties.getPropertyValue(props, "shipment.ups.access.password"), accessRequestDocument); 1723 return accessRequestDocument; 1724 } 1725 1726 public static void handleErrors(Element responseElement, List errorList) { 1727 List errorElements = UtilXml.childElementList(responseElement, "Error"); 1728 Iterator errorElementIter = errorElements.iterator(); 1729 while (errorElementIter.hasNext()) { 1730 StringBuffer errorMessageBuf = new StringBuffer (); 1731 Element errorElement = (Element ) errorElementIter.next(); 1732 1733 String errorSeverity = UtilXml.childElementValue(errorElement, "ErrorSeverity"); 1734 String errorCode = UtilXml.childElementValue(errorElement, "ErrorCode"); 1735 String errorDescription = UtilXml.childElementValue(errorElement, "ErrorDescription"); 1736 String minimumRetrySeconds = UtilXml.childElementValue(errorElement, "MinimumRetrySeconds"); 1737 1738 errorMessageBuf.append("An error occurred [code:"); 1739 errorMessageBuf.append(errorCode); 1740 errorMessageBuf.append("] with severity "); 1741 errorMessageBuf.append(errorSeverity); 1742 errorMessageBuf.append(": "); 1743 errorMessageBuf.append(errorDescription); 1744 if (UtilValidate.isNotEmpty(minimumRetrySeconds)) { 1745 errorMessageBuf.append("; you should wait "); 1746 errorMessageBuf.append(minimumRetrySeconds); 1747 errorMessageBuf.append(" seconds before retrying. "); 1748 } else { 1749 errorMessageBuf.append(". "); 1750 } 1751 1752 List errorLocationElements = UtilXml.childElementList(errorElement, "ErrorLocation"); 1753 Iterator errorLocationElementIter = errorLocationElements.iterator(); 1754 while (errorLocationElementIter.hasNext()) { 1755 Element errorLocationElement = (Element ) errorLocationElementIter.next(); 1756 String errorLocationElementName = UtilXml.childElementValue(errorLocationElement, "ErrorLocationElementName"); 1757 String errorLocationAttributeName = UtilXml.childElementValue(errorLocationElement, "ErrorLocationAttributeName"); 1758 1759 errorMessageBuf.append("The error was at Element ["); 1760 errorMessageBuf.append(errorLocationElementName); 1761 errorMessageBuf.append("]"); 1762 1763 if (UtilValidate.isNotEmpty(errorLocationAttributeName)) { 1764 errorMessageBuf.append(" in the attribute ["); 1765 errorMessageBuf.append(errorLocationAttributeName); 1766 errorMessageBuf.append("]"); 1767 } 1768 1769 List errorDigestElements = UtilXml.childElementList(errorLocationElement, "ErrorDigest"); 1770 Iterator errorDigestElementIter = errorDigestElements.iterator(); 1771 while (errorDigestElementIter.hasNext()) { 1772 Element errorDigestElement = (Element ) errorDigestElementIter.next(); 1773 errorMessageBuf.append(" full text: ["); 1774 errorMessageBuf.append(UtilXml.elementValue(errorDigestElement)); 1775 errorMessageBuf.append("]"); 1776 } 1777 } 1778 1779 errorList.add(errorMessageBuf.toString()); 1780 } 1781 } 1782 1783 1790 public static String sendUpsRequest(String upsService, String xmlString) throws UpsConnectException { 1791 String conStr = UtilProperties.getPropertyValue("shipment.properties", "shipment.ups.connect.url"); 1792 if (conStr == null) { 1793 throw new UpsConnectException("Incomplete connection URL; check your UPS configuration"); 1794 } 1795 1796 if (upsService == null) { 1798 throw new UpsConnectException("UPS service name cannot be null"); 1799 } 1800 1801 if (xmlString == null) { 1804 throw new UpsConnectException("XML message cannot be null"); 1805 } 1806 1807 conStr = conStr.trim(); 1809 if (!conStr.endsWith("/")) { 1810 conStr = conStr + "/"; 1811 } 1812 conStr = conStr + upsService; 1813 1814 String timeOutStr = UtilProperties.getPropertyValue("shipment.properties", "shipment.ups.connect.timeout", "60"); 1815 int timeout = 60; 1816 try { 1817 timeout = Integer.parseInt(timeOutStr); 1818 } catch (NumberFormatException e) { 1819 Debug.logError(e, "Unable to set timeout to " + timeOutStr + " using default " + timeout); 1820 } 1821 1822 1825 HttpClient http = new HttpClient(conStr); 1826 http.setTimeout(timeout * 1000); 1827 String response = null; 1828 try { 1829 response = http.post(xmlString); 1830 } catch (HttpClientException e) { 1831 Debug.logError(e, "Problem connecting with UPS server", module); 1832 throw new UpsConnectException("URL Connection problem", e); 1833 } 1834 1835 if (response == null) { 1836 throw new UpsConnectException("Received a null response"); 1837 } 1838 if (Debug.verboseOn()) Debug.logVerbose("UPS Response : " + response, module); 1839 1840 return response; 1841 } 1842} 1843 1844class UpsConnectException extends GeneralException { 1845 UpsConnectException() { 1846 super(); 1847 } 1848 1849 UpsConnectException(String msg) { 1850 super(msg); 1851 } 1852 1853 UpsConnectException(Throwable t) { 1854 super(t); 1855 } 1856 1857 UpsConnectException(String msg, Throwable t) { 1858 super(msg, t); 1859 } 1860} 1861 1862 1863 1940 1941 1942 2400 2401 2402 | Popular Tags |