*/
public static Map<String, Object> uspsDeliveryConfirmation(DispatchContext dctx, Map<String, ? extends Object> context) {
GenericDelegator delegator = dctx.getDelegator();
String shipmentId = (String) context.get("shipmentId");
String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId");
// ShipmentRouteSegment identifier - used in error messages
String srsKeyString = "[" + shipmentId + "," + shipmentRouteSegmentId + "]";
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);
}
// ensure the carrier is USPS
if (!"USPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) {
return ServiceUtil.returnError("The Carrier for ShipmentRouteSegment " + srsKeyString + ", is not USPS");
}
// get the origin address
GenericValue originAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress");
if (originAddress == null) {
return ServiceUtil.returnError("OriginPostalAddress not found for ShipmentRouteSegment [" +
shipmentId + ":" + shipmentRouteSegmentId + "]");
}
if (!"USA".equals(originAddress.getString("countryGeoId"))) {
return ServiceUtil.returnError("ShipmentRouteSeqment " + srsKeyString + " does not originate from a US address");
}
// get the destination address
GenericValue destinationAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress");
if (destinationAddress == null) {
return ServiceUtil.returnError("DestPostalAddress not found for ShipmentRouteSegment " + srsKeyString);
}
if (!"USA".equals(destinationAddress.getString("countryGeoId"))) {
return ServiceUtil.returnError("ShipmentRouteSeqment " + srsKeyString + " is not destined for a US address");
}
// get the service type from the CarrierShipmentMethod
String shipmentMethodTypeId = shipmentRouteSegment.getString("shipmentMethodTypeId");
String partyId = shipmentRouteSegment.getString("carrierPartyId");
String csmKeystring = "[" + shipmentMethodTypeId + "," + partyId + ",CARRIER]";
GenericValue carrierShipmentMethod = delegator.findByPrimaryKey("CarrierShipmentMethod",
UtilMisc.toMap("partyId", partyId, "roleTypeId", "CARRIER", "shipmentMethodTypeId", shipmentMethodTypeId));
if (carrierShipmentMethod == null) {
return ServiceUtil.returnError("CarrierShipmentMethod " + csmKeystring +
" not found for ShipmentRouteSegment " + srsKeyString);
}
String serviceType = carrierShipmentMethod.getString("carrierServiceCode");
if (UtilValidate.isEmpty(serviceType)) {
return ServiceUtil.returnError("carrierServiceCode not found for CarrierShipmentMethod" + csmKeystring);
}
// get the packages for this shipment route segment
List<GenericValue> shipmentPackageRouteSegList = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null,
UtilMisc.toList("+shipmentPackageSeqId"));
if (UtilValidate.isEmpty(shipmentPackageRouteSegList)) {
return ServiceUtil.returnError("No packages found for ShipmentRouteSegment " + srsKeyString);
}
for (GenericValue shipmentPackageRouteSeg: shipmentPackageRouteSegList) {
Document requestDocument = createUspsRequestDocument("DeliveryConfirmationV2.0Request");
Element requestElement = requestDocument.getDocumentElement();
UtilXml.addChildElementValue(requestElement, "Option", "3", requestDocument);
UtilXml.addChildElement(requestElement, "ImageParameters", requestDocument);
// From address
if (UtilValidate.isNotEmpty(originAddress.getString("attnName"))) {
UtilXml.addChildElementValue(requestElement, "FromName", originAddress.getString("attnName"), requestDocument);
UtilXml.addChildElementValue(requestElement, "FromFirm", originAddress.getString("toName"), requestDocument);
} else {
UtilXml.addChildElementValue(requestElement, "FromName", originAddress.getString("toName"), requestDocument);
}
// The following 2 assignments are not typos - USPS address1 = OFBiz address2, USPS address2 = OFBiz address1
UtilXml.addChildElementValue(requestElement, "FromAddress1", originAddress.getString("address2"), requestDocument);
UtilXml.addChildElementValue(requestElement, "FromAddress2", originAddress.getString("address1"), requestDocument);
UtilXml.addChildElementValue(requestElement, "FromCity", originAddress.getString("city"), requestDocument);
UtilXml.addChildElementValue(requestElement, "FromState", originAddress.getString("stateProvinceGeoId"), requestDocument);
UtilXml.addChildElementValue(requestElement, "FromZip5", originAddress.getString("postalCode"), requestDocument);
UtilXml.addChildElement(requestElement, "FromZip4", requestDocument);
// To address
if (UtilValidate.isNotEmpty(destinationAddress.getString("attnName"))) {
UtilXml.addChildElementValue(requestElement, "ToName", destinationAddress.getString("attnName"), requestDocument);
UtilXml.addChildElementValue(requestElement, "ToFirm", destinationAddress.getString("toName"), requestDocument);
} else {
UtilXml.addChildElementValue(requestElement, "ToName", destinationAddress.getString("toName"), requestDocument);
}
// The following 2 assignments are not typos - USPS address1 = OFBiz address2, USPS address2 = OFBiz address1
UtilXml.addChildElementValue(requestElement, "ToAddress1", destinationAddress.getString("address2"), requestDocument);
UtilXml.addChildElementValue(requestElement, "ToAddress2", destinationAddress.getString("address1"), requestDocument);
UtilXml.addChildElementValue(requestElement, "ToCity", destinationAddress.getString("city"), requestDocument);
UtilXml.addChildElementValue(requestElement, "ToState", destinationAddress.getString("stateProvinceGeoId"), requestDocument);
UtilXml.addChildElementValue(requestElement, "ToZip5", destinationAddress.getString("postalCode"), requestDocument);
UtilXml.addChildElement(requestElement, "ToZip4", requestDocument);
GenericValue shipmentPackage = shipmentPackageRouteSeg.getRelatedOne("ShipmentPackage");
String spKeyString = "[" + shipmentPackage.getString("shipmentId") + "," +
shipmentPackage.getString("shipmentPackageSeqId") + "]";
// WeightInOunces
String weightStr = shipmentPackage.getString("weight");
if (UtilValidate.isEmpty(weightStr)) {
return ServiceUtil.returnError("weight not found for ShipmentPackage " + spKeyString);
}
BigDecimal weight = BigDecimal.ZERO;
try {
weight = new BigDecimal(weightStr);
} catch (NumberFormatException nfe) {
nfe.printStackTrace(); // TODO: handle exception
}
String weightUomId = shipmentPackage.getString("weightUomId");
if (UtilValidate.isEmpty(weightUomId)) {
// assume weight is in pounds for consistency (this assumption is made in uspsDomesticRate also)
weightUomId = "WT_lb";
}
if (!"WT_oz".equals(weightUomId)) {
// attempt a conversion to pounds
GenericValue uomConversion = delegator.findByPrimaryKey("UomConversion",
UtilMisc.toMap("uomId", weightUomId, "uomIdTo", "WT_oz"));
if (uomConversion == null || UtilValidate.isEmpty(uomConversion.getString("conversionFactor"))) {
return ServiceUtil.returnError("Unsupported weightUom [" + weightUomId + "] for ShipmentPackage " +
spKeyString + ", could not find a conversion factor for WT_oz");
}