use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class DhlConnectException method dhlRateEstimate.
/*
* Service to obtain a rate estimate from DHL for a shipment. Notes: Only one package per shipment currently supported by DHL ShipIT.
* If this service returns a null shippingEstimateAmount, then the shipment has not been processed
*/
public static Map<String, Object> dhlRateEstimate(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
// some of these can be refactored
String carrierPartyId = (String) context.get("carrierPartyId");
String shipmentMethodTypeId = (String) context.get("shipmentMethodTypeId");
String shippingContactMechId = (String) context.get("shippingContactMechId");
BigDecimal shippableWeight = (BigDecimal) context.get("shippableWeight");
if ("NO_SHIPPING".equals(shipmentMethodTypeId)) {
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("shippingEstimateAmount", null);
return result;
}
// translate shipmentMethodTypeId to DHL service code
String dhlShipmentDetailCode = null;
try {
GenericValue carrierShipmentMethod = EntityQuery.use(delegator).from("CarrierShipmentMethod").where("shipmentMethodTypeId", shipmentMethodTypeId, "partyId", carrierPartyId, "roleTypeId", "CARRIER").queryOne();
if (carrierShipmentMethod == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlNoCarrierShipmentMethod", UtilMisc.toMap("carrierPartyId", carrierPartyId, "shipmentMethodTypeId", shipmentMethodTypeId), locale));
}
dhlShipmentDetailCode = carrierShipmentMethod.getString("carrierServiceCode");
} catch (GenericEntityException e) {
Debug.logError(e, "Failed to get rate estimate: " + e.getMessage(), module);
}
String resource = (String) context.get("serviceConfigProps");
String shipmentGatewayConfigId = (String) context.get("shipmentGatewayConfigId");
// shipping credentials (configured in properties)
String userid = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "accessUserId", resource, "shipment.dhl.access.userid");
String password = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "accessPassword", resource, "shipment.dhl.access.password");
String shippingKey = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "accessShippingKey", resource, "shipment.dhl.access.shippingKey");
String accountNbr = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "accessAccountNbr", resource, "shipment.dhl.access.accountNbr");
if ((shippingKey.length() == 0) || (accountNbr.length() == 0)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlGatewayNotAvailable", locale));
}
// obtain the ship-to address
GenericValue shipToAddress = null;
if (shippingContactMechId != null) {
try {
shipToAddress = EntityQuery.use(delegator).from("PostalAddress").where("contactMechId", shippingContactMechId).queryOne();
if (shipToAddress == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUnableFoundShipToAddresss", locale));
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
if ((shippableWeight == null) || (shippableWeight.compareTo(BigDecimal.ZERO) <= 0)) {
String tmpValue = EntityUtilProperties.getPropertyValue(shipmentPropertiesFile, "shipment.default.weight.value", delegator);
if (tmpValue != null) {
try {
shippableWeight = new BigDecimal(tmpValue);
} catch (Exception e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlDefaultShippableWeightNotConfigured", locale));
}
}
}
// TODO: if a weight UOM is passed in, use convertUom service to convert it here
if (shippableWeight.compareTo(BigDecimal.ONE) < 0) {
Debug.logWarning("DHL Estimate: Weight is less than 1 lb, submitting DHL minimum of 1 lb for estimate.", module);
shippableWeight = BigDecimal.ONE;
}
if (("G".equals(dhlShipmentDetailCode) && shippableWeight.compareTo(new BigDecimal("999")) > 0) || (shippableWeight.compareTo(new BigDecimal("150")) > 0)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlShippableWeightExceed", locale));
}
String weight = shippableWeight.toString();
// create AccessRequest XML doc using FreeMarker template
String templateName = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "rateEstimateTemplate", resource, "shipment.dhl.template.rate.estimate");
if (templateName.trim().length() == 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlShipmentTemplateLocationNotFound", locale));
}
StringWriter outWriter = new StringWriter();
Map<String, Object> inContext = new HashMap<String, Object>();
inContext.put("action", "RateEstimate");
inContext.put("userid", userid);
inContext.put("password", password);
inContext.put("accountNbr", accountNbr);
inContext.put("shippingKey", shippingKey);
inContext.put("shipDate", UtilDateTime.nowTimestamp());
inContext.put("dhlShipmentDetailCode", dhlShipmentDetailCode);
inContext.put("weight", weight);
inContext.put("state", shipToAddress.getString("stateProvinceGeoId"));
// DHL ShipIT API does not accept ZIP+4
if ((shipToAddress.getString("postalCode") != null) && (shipToAddress.getString("postalCode").length() > 5)) {
inContext.put("postalCode", shipToAddress.getString("postalCode").substring(0, 5));
} else {
inContext.put("postalCode", shipToAddress.getString("postalCode"));
}
try {
ContentWorker.renderContentAsText(dispatcher, templateName, outWriter, inContext, locale, "text/plain", null, null, false);
} catch (Exception e) {
Debug.logError(e, "Cannot get DHL Estimate: Failed to render DHL XML Request.", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlShipmentTemplateError", locale));
}
String requestString = outWriter.toString();
if (Debug.verboseOn()) {
Debug.logVerbose(requestString, module);
}
// send the request
String rateResponseString = null;
try {
rateResponseString = sendDhlRequest(requestString, delegator, shipmentGatewayConfigId, resource, locale);
if (Debug.verboseOn()) {
Debug.logVerbose(rateResponseString, module);
}
} catch (DhlConnectException e) {
String uceErrMsg = "Error sending DHL request for DHL Service Rate: " + e.toString();
Debug.logError(e, uceErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentDhlShipmentTemplateSendingError", UtilMisc.toMap("errorString", e.toString()), locale));
}
Document rateResponseDocument = null;
try {
rateResponseDocument = UtilXml.readXmlDocument(rateResponseString, false);
return handleDhlRateResponse(rateResponseDocument, locale);
} catch (SAXException e2) {
String excErrMsg = "Error parsing the RatingServiceResponse: " + e2.toString();
Debug.logError(e2, excErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentFedexShipmentTemplateParsingError", UtilMisc.toMap("errorString", e2.toString()), locale));
} catch (ParserConfigurationException e2) {
String excErrMsg = "Error parsing the RatingServiceResponse: " + e2.toString();
Debug.logError(e2, excErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentFedexShipmentTemplateParsingError", UtilMisc.toMap("errorString", e2.toString()), locale));
} catch (IOException e2) {
String excErrMsg = "Error parsing the RatingServiceResponse: " + e2.toString();
Debug.logError(e2, excErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentFedexShipmentTemplateParsingError", UtilMisc.toMap("errorString", e2.toString()), locale));
}
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class UpsConnectException method upsEmailReturnLabel.
public static Map<String, Object> upsEmailReturnLabel(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
String shipmentId = (String) context.get("shipmentId");
String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId");
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
Map<String, Object> shipmentGatewayConfig = ShipmentServices.getShipmentGatewayConfigFromShipment(delegator, shipmentId, locale);
String shipmentGatewayConfigId = (String) shipmentGatewayConfig.get("shipmentGatewayConfigId");
String resource = (String) shipmentGatewayConfig.get("configProps");
if (UtilValidate.isEmpty(shipmentGatewayConfigId) && UtilValidate.isEmpty(resource)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsGatewayNotAvailable", locale));
}
boolean shipmentUpsSaveCertificationInfo = "true".equals(getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "saveCertInfo", resource, "shipment.ups.save.certification.info", "true"));
String shipmentUpsSaveCertificationPath = FlexibleStringExpander.expandString(getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "saveCertPath", resource, "shipment.ups.save.certification.path", ""), context);
File shipmentUpsSaveCertificationFile = null;
if (shipmentUpsSaveCertificationInfo) {
shipmentUpsSaveCertificationFile = new File(shipmentUpsSaveCertificationPath);
if (!shipmentUpsSaveCertificationFile.exists()) {
shipmentUpsSaveCertificationFile.mkdirs();
}
}
// Shipment Confirm request
String shipmentConfirmResponseString = null;
try {
GenericValue shipment = EntityQuery.use(delegator).from("Shipment").where("shipmentId", shipmentId).queryOne();
if (shipment == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductShipmentNotFoundId", locale) + " " + shipmentId);
}
GenericValue shipmentRouteSegment = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId).queryOne();
if (shipmentRouteSegment == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductShipmentRouteSegmentNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
if (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsNotRouteSegmentCarrier", UtilMisc.toMap("shipmentRouteSegmentId", shipmentRouteSegmentId, "shipmentId", shipmentId), locale));
}
// Get Origin Info
GenericValue originPostalAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress", false);
if (originPostalAddress == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsRouteSegmentOriginPostalAddressNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
GenericValue originTelecomNumber = shipmentRouteSegment.getRelatedOne("OriginTelecomNumber", false);
if (originTelecomNumber == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsRouteSegmentOriginTelecomNumberNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
String originPhoneNumber = originTelecomNumber.getString("areaCode") + originTelecomNumber.getString("contactNumber");
// don't put on country code if not specified or is the US country code (UPS wants it this way)
if (UtilValidate.isNotEmpty(originTelecomNumber.getString("countryCode")) && !"001".equals(originTelecomNumber.getString("countryCode"))) {
originPhoneNumber = originTelecomNumber.getString("countryCode") + originPhoneNumber;
}
originPhoneNumber = StringUtil.replaceString(originPhoneNumber, "-", "");
originPhoneNumber = StringUtil.replaceString(originPhoneNumber, " ", "");
// lookup the two letter country code (in the geoCode field)
GenericValue originCountryGeo = originPostalAddress.getRelatedOne("CountryGeo", false);
if (originCountryGeo == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsRouteSegmentOriginCountryGeoNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
// Get Dest Info
GenericValue destPostalAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress", false);
if (destPostalAddress == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsRouteSegmentDestPostalAddressNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
GenericValue destTelecomNumber = shipmentRouteSegment.getRelatedOne("DestTelecomNumber", false);
if (destTelecomNumber == null) {
String missingErrMsg = "DestTelecomNumber not found for ShipmentRouteSegment with shipmentId " + shipmentId + " and shipmentRouteSegmentId " + shipmentRouteSegmentId;
Debug.logError(missingErrMsg, module);
}
String destPhoneNumber = null;
if (destTelecomNumber != null) {
destPhoneNumber = destTelecomNumber.getString("areaCode") + destTelecomNumber.getString("contactNumber");
// don't put on country code if not specified or is the US country code (UPS wants it this way)
if (UtilValidate.isNotEmpty(destTelecomNumber.getString("countryCode")) && !"001".equals(destTelecomNumber.getString("countryCode"))) {
destPhoneNumber = destTelecomNumber.getString("countryCode") + destPhoneNumber;
}
destPhoneNumber = StringUtil.replaceString(destPhoneNumber, "-", "");
destPhoneNumber = StringUtil.replaceString(destPhoneNumber, " ", "");
}
// lookup the two letter country code (in the geoCode field)
GenericValue destCountryGeo = destPostalAddress.getRelatedOne("CountryGeo", false);
if (destCountryGeo == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsRouteSegmentDestCountryGeoNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
GenericValue carrierShipmentMethod = EntityQuery.use(delegator).from("CarrierShipmentMethod").where("partyId", shipmentRouteSegment.get("carrierPartyId"), "roleTypeId", "CARRIER", "shipmentMethodTypeId", shipmentRouteSegment.get("shipmentMethodTypeId")).queryOne();
if (carrierShipmentMethod == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsRouteSegmentCarrierShipmentMethodNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId, "carrierPartyId", shipmentRouteSegment.get("carrierPartyId"), "shipmentMethodTypeId", shipmentRouteSegment.get("shipmentMethodTypeId")), locale));
}
Map<String, Object> destEmail = dispatcher.runSync("getPartyEmail", UtilMisc.toMap("partyId", shipment.get("partyIdTo"), "userLogin", userLogin));
if (ServiceUtil.isError(destEmail)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(destEmail));
}
String recipientEmail = null;
if (UtilValidate.isNotEmpty(destEmail.get("emailAddress"))) {
recipientEmail = (String) destEmail.get("emailAddress");
}
String senderEmail = null;
Map<String, Object> originEmail = dispatcher.runSync("getPartyEmail", UtilMisc.toMap("partyId", shipment.get("partyIdFrom"), "userLogin", userLogin));
if (ServiceUtil.isError(originEmail)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(originEmail));
}
if (UtilValidate.isNotEmpty(originEmail.get("emailAddress"))) {
senderEmail = (String) originEmail.get("emailAddress");
}
List<GenericValue> shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId"), false);
if (UtilValidate.isEmpty(shipmentPackageRouteSegs)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsPackageRouteSegsNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
// Okay, start putting the XML together...
Document shipmentConfirmRequestDoc = UtilXml.makeEmptyXmlDocument("ShipmentConfirmRequest");
Element shipmentConfirmRequestElement = shipmentConfirmRequestDoc.getDocumentElement();
shipmentConfirmRequestElement.setAttribute("xml:lang", "en-US");
// Top Level Element: Request
Element requestElement = UtilXml.addChildElement(shipmentConfirmRequestElement, "Request", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(requestElement, "RequestAction", "ShipConfirm", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(requestElement, "RequestOption", "nonvalidate", shipmentConfirmRequestDoc);
// Top Level Element: Shipment
Element shipmentElement = UtilXml.addChildElement(shipmentConfirmRequestElement, "Shipment", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipmentElement, "Description", "Goods for Shipment " + shipment.get("shipmentId"), shipmentConfirmRequestDoc);
// Child of Shipment: ReturnService
Element returnServiceElement = UtilXml.addChildElement(shipmentElement, "ReturnService", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(returnServiceElement, "Code", String.valueOf(returnServiceCode), shipmentConfirmRequestDoc);
// Child of Shipment: Shipper
String shipperNumber = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "shipperNumber", resource, "shipment.ups.shipper.number", "");
Element shipperElement = UtilXml.addChildElement(shipmentElement, "Shipper", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperElement, "Name", UtilValidate.isNotEmpty(originPostalAddress.getString("toName")) ? originPostalAddress.getString("toName") : "", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperElement, "AttentionName", UtilValidate.isNotEmpty(originPostalAddress.getString("attnName")) ? originPostalAddress.getString("attnName") : "", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperElement, "PhoneNumber", originPhoneNumber, shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperElement, "ShipperNumber", shipperNumber, shipmentConfirmRequestDoc);
Element shipperAddressElement = UtilXml.addChildElement(shipperElement, "Address", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperAddressElement, "AddressLine1", originPostalAddress.getString("address1"), shipmentConfirmRequestDoc);
if (UtilValidate.isNotEmpty(originPostalAddress.getString("address2"))) {
UtilXml.addChildElementValue(shipperAddressElement, "AddressLine2", originPostalAddress.getString("address2"), shipmentConfirmRequestDoc);
}
UtilXml.addChildElementValue(shipperAddressElement, "City", originPostalAddress.getString("city"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperAddressElement, "StateProvinceCode", originPostalAddress.getString("stateProvinceGeoId"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperAddressElement, "PostalCode", originPostalAddress.getString("postalCode"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipperAddressElement, "CountryCode", originCountryGeo.getString("geoCode"), shipmentConfirmRequestDoc);
// Child of Shipment: ShipTo
Element shipToElement = UtilXml.addChildElement(shipmentElement, "ShipTo", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipToElement, "CompanyName", UtilValidate.isNotEmpty(destPostalAddress.getString("toName")) ? destPostalAddress.getString("toName") : "", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipToElement, "AttentionName", UtilValidate.isNotEmpty(destPostalAddress.getString("attnName")) ? destPostalAddress.getString("attnName") : "", shipmentConfirmRequestDoc);
if (UtilValidate.isNotEmpty(destPhoneNumber)) {
UtilXml.addChildElementValue(shipToElement, "PhoneNumber", destPhoneNumber, shipmentConfirmRequestDoc);
}
Element shipToAddressElement = UtilXml.addChildElement(shipToElement, "Address", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipToAddressElement, "AddressLine1", destPostalAddress.getString("address1"), shipmentConfirmRequestDoc);
if (UtilValidate.isNotEmpty(destPostalAddress.getString("address2"))) {
UtilXml.addChildElementValue(shipToAddressElement, "AddressLine2", destPostalAddress.getString("address2"), shipmentConfirmRequestDoc);
}
UtilXml.addChildElementValue(shipToAddressElement, "City", destPostalAddress.getString("city"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipToAddressElement, "StateProvinceCode", destPostalAddress.getString("stateProvinceGeoId"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipToAddressElement, "PostalCode", destPostalAddress.getString("postalCode"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipToAddressElement, "CountryCode", destCountryGeo.getString("geoCode"), shipmentConfirmRequestDoc);
if (UtilValidate.isNotEmpty(shipmentRouteSegment.getString("homeDeliveryType"))) {
UtilXml.addChildElement(shipToAddressElement, "ResidentialAddress", shipmentConfirmRequestDoc);
}
// Child of Shipment: ShipFrom
Element shipFromElement = UtilXml.addChildElement(shipmentElement, "ShipFrom", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipFromElement, "CompanyName", UtilValidate.isNotEmpty(originPostalAddress.getString("toName")) ? originPostalAddress.getString("toName") : "", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipFromElement, "AttentionName", UtilValidate.isNotEmpty(originPostalAddress.getString("attnName")) ? originPostalAddress.getString("attnName") : "", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipFromElement, "PhoneNumber", originPhoneNumber, shipmentConfirmRequestDoc);
Element shipFromAddressElement = UtilXml.addChildElement(shipFromElement, "Address", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipFromAddressElement, "AddressLine1", originPostalAddress.getString("address1"), shipmentConfirmRequestDoc);
if (UtilValidate.isNotEmpty(originPostalAddress.getString("address2"))) {
UtilXml.addChildElementValue(shipFromAddressElement, "AddressLine2", originPostalAddress.getString("address2"), shipmentConfirmRequestDoc);
}
UtilXml.addChildElementValue(shipFromAddressElement, "City", originPostalAddress.getString("city"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipFromAddressElement, "StateProvinceCode", originPostalAddress.getString("stateProvinceGeoId"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipFromAddressElement, "PostalCode", originPostalAddress.getString("postalCode"), shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(shipFromAddressElement, "CountryCode", originCountryGeo.getString("geoCode"), shipmentConfirmRequestDoc);
// Child of Shipment: PaymentInformation
Element paymentInformationElement = UtilXml.addChildElement(shipmentElement, "PaymentInformation", shipmentConfirmRequestDoc);
String thirdPartyAccountNumber = shipmentRouteSegment.getString("thirdPartyAccountNumber");
if (UtilValidate.isEmpty(thirdPartyAccountNumber)) {
// Paid by shipper
Element prepaidElement = UtilXml.addChildElement(paymentInformationElement, "Prepaid", shipmentConfirmRequestDoc);
Element billShipperElement = UtilXml.addChildElement(prepaidElement, "BillShipper", shipmentConfirmRequestDoc);
// fill in BillShipper AccountNumber element from properties file
String billShipperAccountNumber = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "billShipperAccountNumber", resource, "shipment.ups.bill.shipper.account.number", "");
UtilXml.addChildElementValue(billShipperElement, "AccountNumber", billShipperAccountNumber, shipmentConfirmRequestDoc);
}
// Child of Shipment: Service
Element serviceElement = UtilXml.addChildElement(shipmentElement, "Service", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(serviceElement, "Code", carrierShipmentMethod.getString("carrierServiceCode"), shipmentConfirmRequestDoc);
// Child of Shipment: ShipmentServiceOptions
String defaultReturnLabelMemo = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "defaultReturnLabelMemo", resource, "shipment.ups.default.returnLabel.memo", "");
String defaultReturnLabelSubject = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "defaultReturnLabelSubject", resource, "shipment.ups.default.returnLabel.subject", "");
Element shipmentServiceOptionsElement = UtilXml.addChildElement(shipmentElement, "ShipmentServiceOptions", shipmentConfirmRequestDoc);
Element labelDeliveryElement = UtilXml.addChildElement(shipmentServiceOptionsElement, "LabelDelivery", shipmentConfirmRequestDoc);
Element emailMessageElement = UtilXml.addChildElement(labelDeliveryElement, "EMailMessage", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(emailMessageElement, "EMailAddress", recipientEmail, shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(emailMessageElement, "FromEMailAddress", senderEmail, shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(emailMessageElement, "FromName", UtilValidate.isNotEmpty(originPostalAddress.getString("attnName")) ? originPostalAddress.getString("attnName") : "", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(emailMessageElement, "Memo", defaultReturnLabelMemo, shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(emailMessageElement, "Subject", defaultReturnLabelSubject, shipmentConfirmRequestDoc);
// Child of Shipment: Package
Element packageElement = UtilXml.addChildElement(shipmentElement, "Package", shipmentConfirmRequestDoc);
Element packagingTypeElement = UtilXml.addChildElement(packageElement, "PackagingType", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(packagingTypeElement, "Code", "02", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(packageElement, "Description", "Package Description", shipmentConfirmRequestDoc);
Element packageWeightElement = UtilXml.addChildElement(packageElement, "PackageWeight", shipmentConfirmRequestDoc);
Element packageWeightUnitOfMeasurementElement = UtilXml.addChildElement(packageElement, "UnitOfMeasurement", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(packageWeightUnitOfMeasurementElement, "Code", "LBS", shipmentConfirmRequestDoc);
UtilXml.addChildElementValue(packageWeightElement, "Weight", EntityUtilProperties.getPropertyValue("shipment", "shipment.default.weight.value", delegator), shipmentConfirmRequestDoc);
String shipmentConfirmRequestString = null;
try {
shipmentConfirmRequestString = UtilXml.writeXmlDocument(shipmentConfirmRequestDoc);
} catch (IOException e) {
String ioeErrMsg = "Error writing the ShipmentConfirmRequest XML Document to a String: " + e.toString();
Debug.logError(e, ioeErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorShipmentConfirmRequestXmlToString", UtilMisc.toMap("errorString", e.toString()), locale));
}
// create AccessRequest XML doc
Document accessRequestDocument = createAccessRequestDocument(delegator, shipmentGatewayConfigId, resource);
String accessRequestString = null;
try {
accessRequestString = UtilXml.writeXmlDocument(accessRequestDocument);
} catch (IOException e) {
String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString();
Debug.logError(e, ioeErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorAccessRequestXmlToString", UtilMisc.toMap("errorString", e.toString()), locale));
}
// connect to UPS server, send AccessRequest to auth
// send ShipmentConfirmRequest String
// get ShipmentConfirmResponse String back
StringBuilder xmlString = new StringBuilder();
xmlString.append(accessRequestString);
xmlString.append(shipmentConfirmRequestString);
try {
shipmentConfirmResponseString = sendUpsRequest("ShipConfirm", xmlString.toString(), shipmentGatewayConfigId, resource, delegator, locale);
} catch (UpsConnectException e) {
String uceErrMsg = "Error sending UPS request for UPS Service ShipConfirm: " + e.toString();
Debug.logError(e, uceErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorSendingShipConfirm", UtilMisc.toMap("errorString", e.toString()), locale));
}
Document shipmentConfirmResponseDocument = null;
try {
shipmentConfirmResponseDocument = UtilXml.readXmlDocument(shipmentConfirmResponseString, false);
} catch (SAXException e2) {
String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString();
Debug.logError(e2, excErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorParsingShipmentConfirm", UtilMisc.toMap("errorString", e2.toString()), locale));
} catch (ParserConfigurationException e2) {
String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString();
Debug.logError(e2, excErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorParsingShipmentConfirm", UtilMisc.toMap("errorString", e2.toString()), locale));
} catch (IOException e2) {
String excErrMsg = "Error parsing the ShipmentConfirmResponse: " + e2.toString();
Debug.logError(e2, excErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorParsingShipmentConfirm", UtilMisc.toMap("errorString", e2.toString()), locale));
}
Element shipmentConfirmResponseElement = shipmentConfirmResponseDocument.getDocumentElement();
// handle Response element info
Element responseElement = UtilXml.firstChildElement(shipmentConfirmResponseElement, "Response");
String responseStatusCode = UtilXml.childElementValue(responseElement, "ResponseStatusCode");
List<Object> errorList = new LinkedList<Object>();
UpsServices.handleErrors(responseElement, errorList, locale);
if (!"1".equals(responseStatusCode)) {
errorList.add(0, UtilProperties.getMessage(resourceError, "FacilityShipmentUpsShipmentConfirmFailedForReturnShippingLabel", locale));
return ServiceUtil.returnError(errorList);
}
// Shipment Accept Request follows
if (!"UPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsNotRouteSegmentCarrier", UtilMisc.toMap("shipmentRouteSegmentId", shipmentRouteSegmentId, "shipmentId", shipmentId), locale));
}
if (UtilValidate.isEmpty(shipmentPackageRouteSegs)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsPackageRouteSegsNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
String shipmentDigest = UtilXml.childElementValue(shipmentConfirmResponseElement, "ShipmentDigest");
if (UtilValidate.isEmpty(shipmentDigest)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsTrackingDigestWasNotSet", locale));
}
Document shipmentAcceptRequestDoc = UtilXml.makeEmptyXmlDocument("ShipmentAcceptRequest");
Element shipmentAcceptRequestElement = shipmentAcceptRequestDoc.getDocumentElement();
shipmentAcceptRequestElement.setAttribute("xml:lang", "en-US");
// Top Level Element: Request
Element acceptRequestElement = UtilXml.addChildElement(shipmentAcceptRequestElement, "Request", shipmentAcceptRequestDoc);
Element acceptTransactionReferenceElement = UtilXml.addChildElement(acceptRequestElement, "TransactionReference", shipmentAcceptRequestDoc);
UtilXml.addChildElementValue(acceptTransactionReferenceElement, "CustomerContext", "ShipAccept / 01", shipmentAcceptRequestDoc);
UtilXml.addChildElementValue(acceptTransactionReferenceElement, "XpciVersion", "1.0001", shipmentAcceptRequestDoc);
UtilXml.addChildElementValue(acceptRequestElement, "RequestAction", "ShipAccept", shipmentAcceptRequestDoc);
UtilXml.addChildElementValue(acceptRequestElement, "RequestOption", "01", shipmentAcceptRequestDoc);
UtilXml.addChildElementValue(shipmentAcceptRequestElement, "ShipmentDigest", shipmentDigest, shipmentAcceptRequestDoc);
String shipmentAcceptRequestString = null;
try {
shipmentAcceptRequestString = UtilXml.writeXmlDocument(shipmentAcceptRequestDoc);
} catch (IOException e) {
String ioeErrMsg = "Error writing the ShipmentAcceptRequest XML Document to a String: " + e.toString();
Debug.logError(e, ioeErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorShipmentAcceptRequestXmlToString", UtilMisc.toMap("errorString", e.toString()), locale));
}
// create AccessRequest XML doc
Document acceptAccessRequestDocument = createAccessRequestDocument(delegator, shipmentGatewayConfigId, resource);
String acceptAccessRequestString = null;
try {
acceptAccessRequestString = UtilXml.writeXmlDocument(acceptAccessRequestDocument);
} catch (IOException e) {
String ioeErrMsg = "Error writing the AccessRequest XML Document to a String: " + e.toString();
Debug.logError(e, ioeErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorAccessRequestXmlToString", UtilMisc.toMap("errorString", e.toString()), locale));
}
// connect to UPS server, send AccessRequest to auth
StringBuilder acceptXmlString = new StringBuilder();
acceptXmlString.append(acceptAccessRequestString);
acceptXmlString.append(shipmentAcceptRequestString);
if (shipmentUpsSaveCertificationInfo) {
String outFileName = shipmentUpsSaveCertificationPath + "/UpsShipmentAcceptRequest" + shipmentId + "_" + shipmentRouteSegment.getString("shipmentRouteSegmentId") + ".xml";
try (FileOutputStream fileOut = new FileOutputStream(outFileName)) {
fileOut.write(xmlString.toString().getBytes(UtilIO.getUtf8()));
fileOut.flush();
} catch (IOException e) {
Debug.logInfo(e, "Could not save UPS XML file: [[[" + xmlString.toString() + "]]] to file: " + outFileName, module);
}
}
try {
sendUpsRequest("ShipAccept", acceptXmlString.toString(), shipmentGatewayConfigId, resource, delegator, locale);
} catch (UpsConnectException e) {
String uceErrMsg = "Error sending UPS request for UPS Service ShipAccept: " + e.toString();
Debug.logError(e, uceErrMsg, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorSendingShipAccept", UtilMisc.toMap("errorString", e.toString()), locale));
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorDataShipmentAccept", UtilMisc.toMap("errorString", e.toString()), locale));
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUpsErrorDataShipmentConfirm", UtilMisc.toMap("errorString", e.toString()), locale));
}
return ServiceUtil.returnSuccess(UtilProperties.getMessage("OrderUiLabels", "OrderReturnLabelEmailSuccessful", locale));
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class UspsRequestException method uspsUpdateShipmentRateInfo.
// Warning: I don't think the following 2 services were completed or fully tested - 2004.09.06 JFE
/* --- ShipmentRouteSegment services --------------------------------------------------------------------------- */
public static Map<String, Object> uspsUpdateShipmentRateInfo(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
String shipmentId = (String) context.get("shipmentId");
String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId");
Locale locale = (Locale) context.get("locale");
Map<String, Object> shipmentGatewayConfig = ShipmentServices.getShipmentGatewayConfigFromShipment(delegator, shipmentId, locale);
String shipmentGatewayConfigId = (String) shipmentGatewayConfig.get("shipmentGatewayConfigId");
String resource = (String) shipmentGatewayConfig.get("configProps");
if (UtilValidate.isEmpty(shipmentGatewayConfigId) && UtilValidate.isEmpty(resource)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsGatewayNotAvailable", locale));
}
try {
GenericValue shipmentRouteSegment = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId).queryOne();
if (shipmentRouteSegment == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "ProductShipmentRouteSegmentNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
// ensure the carrier is USPS
if (!"USPS".equals(shipmentRouteSegment.getString("carrierPartyId"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsNotRouteSegmentCarrier", UtilMisc.toMap("shipmentRouteSegmentId", shipmentRouteSegmentId, "shipmentId", shipmentId), locale));
}
// get the origin address
GenericValue originAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress", false);
if (originAddress == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentRouteSegmentOriginPostalAddressNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
if (!"USA".equals(originAddress.getString("countryGeoId"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRouteSegmentOriginCountryGeoNotInUsa", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
String originZip = originAddress.getString("postalCode");
if (UtilValidate.isEmpty(originZip)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRouteSegmentOriginZipCodeMissing", UtilMisc.toMap("contactMechId", originAddress.getString("contactMechId")), locale));
}
// get the destination address
GenericValue destinationAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress", false);
if (destinationAddress == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentRouteSegmentDestPostalAddressNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
if (!"USA".equals(destinationAddress.getString("countryGeoId"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRouteSegmentOriginCountryGeoNotInUsa", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
String destinationZip = destinationAddress.getString("postalCode");
if (UtilValidate.isEmpty(destinationZip)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRouteSegmentDestinationZipCodeMissing", UtilMisc.toMap("contactMechId", destinationAddress.getString("contactMechId")), locale));
}
// get the service type from the CarrierShipmentMethod
String shipmentMethodTypeId = shipmentRouteSegment.getString("shipmentMethodTypeId");
String partyId = shipmentRouteSegment.getString("carrierPartyId");
GenericValue carrierShipmentMethod = EntityQuery.use(delegator).from("CarrierShipmentMethod").where("partyId", partyId, "roleTypeId", "CARRIER", "shipmentMethodTypeId", shipmentMethodTypeId).queryOne();
if (carrierShipmentMethod == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsNoCarrierShipmentMethod", UtilMisc.toMap("carrierPartyId", partyId, "shipmentMethodTypeId", shipmentMethodTypeId), locale));
}
String serviceType = carrierShipmentMethod.getString("carrierServiceCode");
if (UtilValidate.isEmpty(serviceType)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsNoCarrierServiceCodeFound", UtilMisc.toMap("carrierPartyId", partyId, "shipmentMethodTypeId", shipmentMethodTypeId), locale));
}
// get the packages for this shipment route segment
List<GenericValue> shipmentPackageRouteSegList = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, UtilMisc.toList("+shipmentPackageSeqId"), false);
if (UtilValidate.isEmpty(shipmentPackageRouteSegList)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentPackageRouteSegsNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
BigDecimal actualTransportCost = BigDecimal.ZERO;
String carrierDeliveryZone = null;
String carrierRestrictionCodes = null;
String carrierRestrictionDesc = null;
// send a new request for each package
for (Iterator<GenericValue> i = shipmentPackageRouteSegList.iterator(); i.hasNext(); ) {
GenericValue shipmentPackageRouteSeg = i.next();
Document requestDocument = createUspsRequestDocument("RateRequest", true, delegator, shipmentGatewayConfigId, resource);
Element packageElement = UtilXml.addChildElement(requestDocument.getDocumentElement(), "Package", requestDocument);
packageElement.setAttribute("ID", "0");
UtilXml.addChildElementValue(packageElement, "Service", serviceType, requestDocument);
UtilXml.addChildElementValue(packageElement, "ZipOrigination", originZip, requestDocument);
UtilXml.addChildElementValue(packageElement, "ZipDestination", destinationZip, requestDocument);
GenericValue shipmentPackage = null;
shipmentPackage = shipmentPackageRouteSeg.getRelatedOne("ShipmentPackage", false);
// weight elements - Pounds, Ounces
String weightStr = shipmentPackage.getString("weight");
if (UtilValidate.isEmpty(weightStr)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsWeightNotFound", UtilMisc.toMap("shipmentId", shipmentPackage.getString("shipmentId"), "shipmentPackageSeqId", shipmentPackage.getString("shipmentPackageSeqId")), locale));
}
BigDecimal weight = BigDecimal.ZERO;
try {
weight = new BigDecimal(weightStr);
} catch (NumberFormatException nfe) {
// TODO: handle exception
Debug.logError(nfe, module);
}
String weightUomId = shipmentPackage.getString("weightUomId");
if (UtilValidate.isEmpty(weightUomId)) {
// assume weight is in pounds
weightUomId = "WT_lb";
}
if (!"WT_lb".equals(weightUomId)) {
// attempt a conversion to pounds
Map<String, Object> result;
try {
result = dispatcher.runSync("convertUom", UtilMisc.<String, Object>toMap("uomId", weightUomId, "uomIdTo", "WT_lb", "originalValue", weight));
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
} catch (GenericServiceException ex) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsWeightConversionError", UtilMisc.toMap("errorString", ex.getMessage()), locale));
}
if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS) && result.get("convertedValue") != null) {
weight = weight.multiply((BigDecimal) result.get("convertedValue"));
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsWeightUnsupported", UtilMisc.toMap("weightUomId", weightUomId, "shipmentId", shipmentPackage.getString("shipmentId"), "shipmentPackageSeqId", shipmentPackage.getString("shipmentPackageSeqId"), "weightUom", "WT_lb"), locale));
}
}
BigDecimal weightPounds = weight.setScale(0, RoundingMode.FLOOR);
BigDecimal weightOunces = weight.multiply(new BigDecimal("16")).remainder(new BigDecimal("16")).setScale(0, RoundingMode.CEILING);
DecimalFormat df = new DecimalFormat("#");
UtilXml.addChildElementValue(packageElement, "Pounds", df.format(weightPounds), requestDocument);
UtilXml.addChildElementValue(packageElement, "Ounces", df.format(weightOunces), requestDocument);
// Container element
GenericValue carrierShipmentBoxType = null;
List<GenericValue> carrierShipmentBoxTypes = null;
carrierShipmentBoxTypes = shipmentPackage.getRelated("CarrierShipmentBoxType", UtilMisc.toMap("partyId", "USPS"), null, false);
if (carrierShipmentBoxTypes.size() > 0) {
carrierShipmentBoxType = carrierShipmentBoxTypes.get(0);
}
if (carrierShipmentBoxType != null && UtilValidate.isNotEmpty(carrierShipmentBoxType.getString("packagingTypeCode"))) {
UtilXml.addChildElementValue(packageElement, "Container", carrierShipmentBoxType.getString("packagingTypeCode"), requestDocument);
} else {
// default to "None", for customers using their own package
UtilXml.addChildElementValue(packageElement, "Container", "None", requestDocument);
}
// Size element
if (carrierShipmentBoxType != null && UtilValidate.isNotEmpty("oversizeCode")) {
UtilXml.addChildElementValue(packageElement, "Size", carrierShipmentBoxType.getString("oversizeCode"), requestDocument);
} else {
// default to "Regular", length + girth measurement <= 84 inches
UtilXml.addChildElementValue(packageElement, "Size", "Regular", requestDocument);
}
// Although only applicable for Parcel Post, this tag is required for all requests
UtilXml.addChildElementValue(packageElement, "Machinable", "False", requestDocument);
Document responseDocument = null;
try {
responseDocument = sendUspsRequest("Rate", requestDocument, delegator, shipmentGatewayConfigId, resource, locale);
} catch (UspsRequestException e) {
Debug.logInfo(e, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticSendingError", UtilMisc.toMap("errorString", e.getMessage()), locale));
}
Element respPackageElement = UtilXml.firstChildElement(responseDocument.getDocumentElement(), "Package");
if (respPackageElement == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticResponseIncompleteElementPackage", locale));
}
Element respErrorElement = UtilXml.firstChildElement(respPackageElement, "Error");
if (respErrorElement != null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticResponseError", UtilMisc.toMap("errorString", UtilXml.childElementValue(respErrorElement, "Description")), locale));
}
// update the ShipmentPackageRouteSeg
String postageString = UtilXml.childElementValue(respPackageElement, "Postage");
if (UtilValidate.isEmpty(postageString)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticResponseIncompleteElementPostage", locale));
}
BigDecimal postage = BigDecimal.ZERO;
try {
postage = new BigDecimal(postageString);
} catch (NumberFormatException nfe) {
// TODO: handle exception
Debug.logError(nfe, module);
}
actualTransportCost = actualTransportCost.add(postage);
shipmentPackageRouteSeg.setString("packageTransportCost", postageString);
shipmentPackageRouteSeg.store();
// if this is the last package, get the zone and APO/FPO restrictions for the ShipmentRouteSegment
if (!i.hasNext()) {
carrierDeliveryZone = UtilXml.childElementValue(respPackageElement, "Zone");
carrierRestrictionCodes = UtilXml.childElementValue(respPackageElement, "RestrictionCodes");
carrierRestrictionDesc = UtilXml.childElementValue(respPackageElement, "RestrictionDescription");
}
}
// update the ShipmentRouteSegment
shipmentRouteSegment.set("carrierDeliveryZone", carrierDeliveryZone);
shipmentRouteSegment.set("carrierRestrictionCodes", carrierRestrictionCodes);
shipmentRouteSegment.set("carrierRestrictionDesc", carrierRestrictionDesc);
shipmentRouteSegment.setString("actualTransportCost", String.valueOf(actualTransportCost));
shipmentRouteSegment.store();
} catch (GenericEntityException gee) {
Debug.logInfo(gee, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticReadingError", UtilMisc.toMap("errorString", gee.getMessage()), locale));
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ShipmentServices method quickScheduleShipmentRouteSegment.
/**
* Service to call a ShipmentRouteSegment.carrierPartyId's confirm shipment method asynchronously
*/
public static Map<String, Object> quickScheduleShipmentRouteSegment(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String shipmentId = (String) context.get("shipmentId");
String shipmentRouteSegmentId = (String) context.get("shipmentRouteSegmentId");
String carrierPartyId = null;
// get the carrierPartyId
try {
GenericValue shipmentRouteSegment = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId).cache(true).queryOne();
carrierPartyId = shipmentRouteSegment.getString("carrierPartyId");
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// if we can't confirm a single shipment, then all shipment route segments in a multi-form are rolled back.
try {
Map<String, Object> input = UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId, "userLogin", userLogin);
// for DHL, we just need to confirm the shipment to get the label. Other carriers may have more elaborate requirements.
if ("DHL".equals(carrierPartyId)) {
dispatcher.runAsync("dhlShipmentConfirm", input);
} else {
Debug.logError(carrierPartyId + " is not supported at this time. Sorry.", module);
}
} catch (GenericServiceException se) {
Debug.logError(se, se.getMessage(), module);
}
// don't return an error
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.service.LocalDispatcher in project ofbiz-framework by apache.
the class ShipmentServices method updateShipmentsFromStaging.
public static Map<String, Object> updateShipmentsFromStaging(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
Map<String, String> shipmentMap = new HashMap<String, String>();
EntityQuery eq = EntityQuery.use(delegator).from("OdbcPackageIn").orderBy("shipmentId", "shipmentPackageSeqId", "voidIndicator");
try (EntityListIterator eli = eq.queryIterator()) {
GenericValue pkgInfo;
while ((pkgInfo = eli.next()) != null) {
String packageSeqId = pkgInfo.getString("shipmentPackageSeqId");
String shipmentId = pkgInfo.getString("shipmentId");
// locate the shipment package
GenericValue shipmentPackage = EntityQuery.use(delegator).from("ShipmentPackage").where("shipmentId", shipmentId, "shipmentPackageSeqId", packageSeqId).queryOne();
if (shipmentPackage != null) {
if ("00001".equals(packageSeqId)) {
// only need to do this for the first package
GenericValue rtSeg = null;
rtSeg = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001").queryOne();
if (rtSeg == null) {
rtSeg = delegator.makeValue("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001"));
try {
delegator.create(rtSeg);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
rtSeg.set("actualStartDate", pkgInfo.get("shippedDate"));
rtSeg.set("billingWeight", pkgInfo.get("billingWeight"));
rtSeg.set("actualCost", pkgInfo.get("shippingTotal"));
rtSeg.set("trackingIdNumber", pkgInfo.get("trackingNumber"));
delegator.store(rtSeg);
}
Map<String, Object> pkgCtx = new HashMap<String, Object>();
pkgCtx.put("shipmentId", shipmentId);
pkgCtx.put("shipmentPackageSeqId", packageSeqId);
// first update the weight of the package
GenericValue pkg = null;
pkg = EntityQuery.use(delegator).from("ShipmentPackage").where(pkgCtx).queryOne();
if (pkg == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentPackageNotFound", UtilMisc.toMap("shipmentPackageSeqId", packageSeqId, "shipmentId", shipmentId), locale));
}
pkg.set("weight", pkgInfo.get("packageWeight"));
delegator.store(pkg);
// need if we are the first package (only) update the route seg info
pkgCtx.put("shipmentRouteSegmentId", "00001");
GenericValue pkgRtSeg = null;
pkgRtSeg = EntityQuery.use(delegator).from("ShipmentPackageRouteSeg").where(pkgCtx).queryOne();
if (pkgRtSeg == null) {
pkgRtSeg = delegator.makeValue("ShipmentPackageRouteSeg", pkgCtx);
try {
delegator.create(pkgRtSeg);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
pkgRtSeg.set("trackingCode", pkgInfo.get("trackingNumber"));
pkgRtSeg.set("boxNumber", pkgInfo.get("shipmentPackageSeqId"));
pkgRtSeg.set("packageServiceCost", pkgInfo.get("packageTotal"));
delegator.store(pkgRtSeg);
shipmentMap.put(shipmentId, pkgInfo.getString("voidIndicator"));
}
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// update the status of each shipment
for (Map.Entry<String, String> entry : shipmentMap.entrySet()) {
String shipmentId = entry.getKey();
String voidInd = entry.getValue();
Map<String, Object> shipCtx = new HashMap<String, Object>();
shipCtx.put("shipmentId", shipmentId);
if ("Y".equals(voidInd)) {
shipCtx.put("statusId", "SHIPMENT_CANCELLED");
} else {
shipCtx.put("statusId", "SHIPMENT_SHIPPED");
}
shipCtx.put("userLogin", userLogin);
Map<String, Object> shipResp = null;
try {
shipResp = dispatcher.runSync("updateShipment", shipCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(shipResp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(shipResp));
}
// remove the shipment info
Map<String, Object> clearResp = null;
try {
clearResp = dispatcher.runSync("clearShipmentStaging", UtilMisc.<String, Object>toMap("shipmentId", shipmentId, "userLogin", userLogin));
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(clearResp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(clearResp));
}
}
return ServiceUtil.returnSuccess();
}
Aggregations