use of org.apache.ofbiz.entity.Delegator 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.entity.Delegator 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();
}
use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ShipmentServices method duplicateShipmentRouteSegment.
public static Map<String, Object> duplicateShipmentRouteSegment(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");
Locale locale = (Locale) context.get("locale");
Map<String, Object> results = ServiceUtil.returnSuccess();
try {
GenericValue shipmentRouteSeg = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId).queryOne();
if (shipmentRouteSeg == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentRouteSegmentNotFound", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId), locale));
}
Map<String, Object> params = UtilMisc.<String, Object>toMap("shipmentId", shipmentId, "carrierPartyId", shipmentRouteSeg.getString("carrierPartyId"), "shipmentMethodTypeId", shipmentRouteSeg.getString("shipmentMethodTypeId"), "originFacilityId", shipmentRouteSeg.getString("originFacilityId"), "originContactMechId", shipmentRouteSeg.getString("originContactMechId"), "originTelecomNumberId", shipmentRouteSeg.getString("originTelecomNumberId"));
params.put("destFacilityId", shipmentRouteSeg.getString("destFacilityId"));
params.put("destContactMechId", shipmentRouteSeg.getString("destContactMechId"));
params.put("destTelecomNumberId", shipmentRouteSeg.getString("destTelecomNumberId"));
params.put("billingWeight", shipmentRouteSeg.get("billingWeight"));
params.put("billingWeightUomId", shipmentRouteSeg.get("billingWeightUomId"));
params.put("userLogin", userLogin);
Map<String, Object> tmpResult = dispatcher.runSync("createShipmentRouteSegment", params);
if (ServiceUtil.isError(tmpResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(tmpResult));
} else {
results.put("newShipmentRouteSegmentId", tmpResult.get("shipmentRouteSegmentId"));
return results;
}
} catch (GenericEntityException ex) {
return ServiceUtil.returnError(ex.getMessage());
} catch (GenericServiceException ex) {
return ServiceUtil.returnError(ex.getMessage());
}
}
use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ShipmentServices method sendShipmentCompleteNotification.
public static Map<String, Object> sendShipmentCompleteNotification(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String shipmentId = (String) context.get("shipmentId");
String sendTo = (String) context.get("sendTo");
String screenUri = (String) context.get("screenUri");
Locale localePar = (Locale) context.get("locale");
// prepare the shipment information
Map<String, Object> sendMap = new HashMap<String, Object>();
GenericValue shipment = null;
GenericValue orderHeader = null;
try {
shipment = EntityQuery.use(delegator).from("Shipment").where("shipmentId", shipmentId).queryOne();
orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", shipment.getString("primaryOrderId")).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting info from database", module);
}
GenericValue productStoreEmail = null;
try {
productStoreEmail = EntityQuery.use(delegator).from("ProductStoreEmailSetting").where("productStoreId", orderHeader.get("productStoreId"), "emailType", "PRDS_ODR_SHIP_COMPLT").queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting the ProductStoreEmailSetting for productStoreId =" + orderHeader.get("productStoreId") + " and emailType = PRDS_ODR_SHIP_COMPLT", module);
}
if (productStoreEmail == null) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "ProductProductStoreEmailSettingsNotValid", UtilMisc.toMap("productStoreId", orderHeader.get("productStoreId"), "emailType", "PRDS_ODR_SHIP_COMPLT"), localePar));
}
// the override screenUri
if (UtilValidate.isEmpty(screenUri)) {
String bodyScreenLocation = productStoreEmail.getString("bodyScreenLocation");
sendMap.put("bodyScreenUri", bodyScreenLocation);
} else {
sendMap.put("bodyScreenUri", screenUri);
}
String partyId = shipment.getString("partyIdTo");
// get the email address
String emailString = null;
GenericValue email = PartyWorker.findPartyLatestContactMech(partyId, "EMAIL_ADDRESS", delegator);
if (UtilValidate.isNotEmpty(email)) {
emailString = email.getString("infoString");
}
if (UtilValidate.isEmpty(emailString)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductProductStoreEmailSettingsNoSendToFound", localePar));
}
Locale locale = PartyWorker.findPartyLastLocale(partyId, delegator);
if (locale == null) {
locale = Locale.getDefault();
}
Map<String, Object> bodyParameters = UtilMisc.<String, Object>toMap("partyId", partyId, "shipmentId", shipmentId, "orderId", shipment.getString("primaryOrderId"), "userLogin", userLogin, "locale", locale);
sendMap.put("bodyParameters", bodyParameters);
sendMap.put("userLogin", userLogin);
sendMap.put("subject", productStoreEmail.getString("subject"));
sendMap.put("contentType", productStoreEmail.get("contentType"));
sendMap.put("sendFrom", productStoreEmail.get("fromAddress"));
sendMap.put("sendCc", productStoreEmail.get("ccAddress"));
sendMap.put("sendBcc", productStoreEmail.get("bccAddress"));
if ((sendTo != null) && UtilValidate.isEmail(sendTo)) {
sendMap.put("sendTo", sendTo);
} else {
sendMap.put("sendTo", emailString);
}
// send the notification
Map<String, Object> sendResp = null;
try {
sendResp = dispatcher.runSync("sendMailFromScreen", sendMap);
} catch (GenericServiceException gse) {
Debug.logError(gse, "Problem sending mail", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemSendingEmail", localePar));
} catch (Exception e) {
Debug.logError(e, "Problem sending mail", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemSendingEmail", localePar));
}
// check for errors
if (sendResp != null && ServiceUtil.isError(sendResp)) {
sendResp.put("emailType", "PRDS_ODR_SHIP_COMPLT");
return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemSendingEmail", localePar), null, null, sendResp);
}
return sendResp;
}
use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ShipmentServices method fillShipmentStagingTables.
public static Map<String, Object> fillShipmentStagingTables(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
String shipmentId = (String) context.get("shipmentId");
Locale locale = (Locale) context.get("locale");
GenericValue shipment = null;
if (shipmentId != null) {
try {
shipment = EntityQuery.use(delegator).from("Shipment").where("shipmentId", shipmentId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
if (shipment == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentNotFoundId", locale));
}
String shipmentStatusId = shipment.getString("statusId");
if ("SHIPMENT_PACKED".equals(shipmentStatusId)) {
GenericValue address = null;
try {
address = shipment.getRelatedOne("DestinationPostalAddress", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (address == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentNoAddressFound", locale));
}
List<GenericValue> packages = null;
try {
packages = shipment.getRelated("ShipmentPackage", null, null, false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (UtilValidate.isEmpty(packages)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentNoPackagesAvailable", locale));
}
List<GenericValue> routeSegs = null;
try {
routeSegs = shipment.getRelated("ShipmentRouteSegment", null, null, false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
GenericValue routeSeg = EntityUtil.getFirst(routeSegs);
// to store list
List<GenericValue> toStore = new LinkedList<GenericValue>();
// make the staging records
GenericValue stageShip = delegator.makeValue("OdbcShipmentOut");
stageShip.set("shipmentId", shipment.get("shipmentId"));
stageShip.set("partyId", shipment.get("partyIdTo"));
stageShip.set("carrierPartyId", routeSeg.get("carrierPartyId"));
stageShip.set("shipmentMethodTypeId", routeSeg.get("shipmentMethodTypeId"));
stageShip.set("toName", address.get("toName"));
stageShip.set("attnName", address.get("attnName"));
stageShip.set("address1", address.get("address1"));
stageShip.set("address2", address.get("address2"));
stageShip.set("directions", address.get("directions"));
stageShip.set("city", address.get("city"));
stageShip.set("postalCode", address.get("postalCode"));
stageShip.set("postalCodeExt", address.get("postalCodeExt"));
stageShip.set("countryGeoId", address.get("countryGeoId"));
stageShip.set("stateProvinceGeoId", address.get("stateProvinceGeoId"));
stageShip.set("numberOfPackages", Long.valueOf(packages.size()));
stageShip.set("handlingInstructions", shipment.get("handlingInstructions"));
toStore.add(stageShip);
for (GenericValue shipmentPkg : packages) {
GenericValue stagePkg = delegator.makeValue("OdbcPackageOut");
stagePkg.set("shipmentId", shipmentPkg.get("shipmentId"));
stagePkg.set("shipmentPackageSeqId", shipmentPkg.get("shipmentPackageSeqId"));
stagePkg.set("orderId", shipment.get("primaryOrderId"));
stagePkg.set("shipGroupSeqId", shipment.get("primaryShipGroupSeqId"));
stagePkg.set("shipmentBoxTypeId", shipmentPkg.get("shipmentBoxTypeId"));
stagePkg.set("weight", shipmentPkg.get("weight"));
toStore.add(stagePkg);
}
try {
delegator.storeAll(toStore);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
} else {
Debug.logWarning("Shipment #" + shipmentId + " is not available for shipment; not setting in staging tables.", module);
}
return ServiceUtil.returnSuccess();
}
Aggregations