From 61e0a446c50009ec2cfc78f8d3fbe1dd4c3bc3a0 Mon Sep 17 00:00:00 2001 From: Anil Patel Date: Sat, 21 Jul 2007 03:34:07 +0000 Subject: [PATCH] adding cc auth process before processing shipment. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk/specialpurpose@558234 13f79535-47bb-0310-9956-ffa450edef68 --- oagis/servicedef/secas.xml | 4 +- oagis/servicedef/services.xml | 10 +- .../ofbiz/oagis/OagisShipmentServices.java | 452 ++++++++++-------- 3 files changed, 253 insertions(+), 213 deletions(-) diff --git a/oagis/servicedef/secas.xml b/oagis/servicedef/secas.xml index 641aa4e98..e0a2d57d8 100644 --- a/oagis/servicedef/secas.xml +++ b/oagis/servicedef/secas.xml @@ -28,9 +28,9 @@ under the License. - + - + diff --git a/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java b/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java index a5e840008..251752f84 100644 --- a/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java +++ b/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -43,6 +44,7 @@ import java.util.TreeSet; import javax.xml.parsers.ParserConfigurationException; import javolution.util.FastList; +import javolution.util.FastMap; import javolution.util.FastSet; import org.ofbiz.base.util.Debug; @@ -247,8 +249,10 @@ public class OagisShipmentServices { String saveToFilename = (String) context.get("saveToFilename"); String saveToDirectory = (String) context.get("saveToDirectory"); OutputStream out = (OutputStream) context.get("outputStream"); + Locale locale = (Locale) context.get("locale"); Map result = ServiceUtil.returnSuccess(); + Map comiCtx = new HashMap(); MapStack bodyParameters = MapStack.create(); if (userLogin == null) { try { @@ -257,215 +261,251 @@ public class OagisShipmentServices { Debug.logError(e, "Error getting userLogin", module); } } - GenericValue orderHeader = null; - GenericValue orderItemShipGroup = null; - try { - orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); - } catch (GenericEntityException e) { - Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); - } - GenericValue shipment =null; - if (orderHeader != null) { - String orderStatusId = orderHeader.getString("statusId"); - if (orderStatusId.equals("ORDER_APPROVED")) { - try { - Map cospResult= dispatcher.runSync("createOrderShipmentPlan", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin)); - shipmentId = (String) cospResult.get("shipmentId"); - } catch (GenericServiceException e) { - Debug.logError(e, module); - return ServiceUtil.returnError(e.getMessage()); + // check payment authorization + boolean checkPaymentAuth = false; + if (orderId != null) { + Map serviceContext = FastMap.newInstance(); + serviceContext.put("orderId", orderId); + serviceContext.put("userLogin", userLogin); + Map authResult = null; + try { + authResult = dispatcher.runSync("authOrderPayments", serviceContext); + String processResult = (String) authResult.get("processResult"); + if (processResult.equals("APPROVED")) { + checkPaymentAuth = true; + Debug.logInfo("payment authorization completed: ", module); + } else { + Debug.logInfo("payment authorization could not completed: ", module); } - try { - shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); - bodyParameters.put("shipment", shipment); - OrderReadHelper orderReadHelper = new OrderReadHelper(orderHeader); - if(orderReadHelper.hasShippingAddress()) { - GenericValue address = EntityUtil.getFirst(orderReadHelper.getShippingLocations()); - bodyParameters.put("address", address); - } - String emailString = orderReadHelper.getOrderEmailString(); - bodyParameters.put("emailString", emailString); - - String contactMechId = shipment.getString("destinationTelecomNumberId"); - GenericValue telecomNumber = delegator.findByPrimaryKey("TelecomNumber", UtilMisc.toMap("contactMechId", contactMechId)); - bodyParameters.put("telecomNumber", telecomNumber); - - List shipmentItems = delegator.findByAnd("ShipmentItem", UtilMisc.toMap("shipmentId", shipmentId)); - bodyParameters.put("shipmentItems", shipmentItems); - - orderItemShipGroup = EntityUtil.getFirst(delegator.findByAnd("OrderItemShipGroup", UtilMisc.toMap("orderId", orderId))); - bodyParameters.put("orderItemShipGroup", orderItemShipGroup); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - - Set correspondingPoIdSet = FastSet.newInstance(); - try { - List orderItems = delegator.findByAnd("OrderItem", UtilMisc.toMap("orderId", shipment.getString("primaryOrderId"))); - Iterator oiIter = orderItems.iterator(); - while (oiIter.hasNext()) { - GenericValue orderItem = (GenericValue) oiIter.next(); - String correspondingPoId = orderItem.getString("correspondingPoId"); - if (correspondingPoId != null) { - correspondingPoIdSet.add(correspondingPoId); - } - } - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - bodyParameters.put("correspondingPoIdSet", correspondingPoIdSet); - - Set externalIdSet = FastSet.newInstance(); - try { - GenericValue primaryOrderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", shipment.getString("primaryOrderId"))); - externalIdSet.add(primaryOrderHeader.getString("externalId")); - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - bodyParameters.put("externalIdSet", externalIdSet); - - // Check if order was a return replacement order (associated with return) - List returnItemResponses = null; - List returnItemRespExprs = UtilMisc.toList(new EntityExpr("replacementOrderId", EntityOperator.NOT_EQUAL, null)); - EntityCondition returnItemRespCond = new EntityConditionList(returnItemRespExprs, EntityOperator.AND); - // list of fields to select (initial list) - List fieldsToSelect = FastList.newInstance(); - fieldsToSelect.add("replacementOrderId"); - try { - returnItemResponses = delegator.findByCondition("ReturnItemResponse", returnItemRespCond, fieldsToSelect, null); - Iterator rirIter = returnItemResponses.iterator(); - while (rirIter.hasNext()) { - GenericValue returnItemResponse = (GenericValue) rirIter.next(); - String replacementOrderId = returnItemResponse.getString("replacementOrderId"); - if (replacementOrderId.equals(shipment.getString("primaryOrderId"))) { - bodyParameters.put("shipnotes", "RETURNLABEL"); - - // Get the associated return Id (replaceReturnId) - String returnItemResponseId = returnItemResponse.getString("returnItemResponseId"); - GenericValue returnItem = EntityUtil.getFirst(delegator.findByAnd("ReturnItem", UtilMisc.toMap("returnItemResponseId", returnItemResponseId))); - bodyParameters.put("replacementReturnId", returnItem.getString("returnId")); - } - } - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - String logicalId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.LOGICALID"); - bodyParameters.put("logicalId", logicalId); - result.put("logicalId", logicalId); - - String authId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.AUTHID"); - bodyParameters.put("authId", authId); - result.put("authId", authId); - - String referenceId = delegator.getNextSeqId("OagisMessageInfo"); - bodyParameters.put("referenceId", referenceId); - result.put("referenceId", referenceId); - - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'Z"); - Timestamp timestamp = UtilDateTime.nowTimestamp(); - String sentDate = dateFormat.format(timestamp); - bodyParameters.put("sentDate", sentDate); - result.put("sentDate", timestamp); - - // tracking shipper account - String partyId = shipment.getString("partyIdTo"); - List partyCarrierAccounts = new ArrayList(); - try { - partyCarrierAccounts = delegator.findByAnd("PartyCarrierAccount", UtilMisc.toMap("partyId", partyId)); - partyCarrierAccounts = EntityUtil.filterByDate(partyCarrierAccounts); - if (partyCarrierAccounts != null) { - Iterator pcaIter = partyCarrierAccounts.iterator(); - while (pcaIter.hasNext()) { - GenericValue partyCarrierAccount = (GenericValue) pcaIter.next(); - String carrierPartyId = partyCarrierAccount.getString("carrierPartyId"); - if (carrierPartyId.equals(orderItemShipGroup.getString("carrierPartyId"))) { - String accountNumber = partyCarrierAccount.getString("accountNumber"); - bodyParameters.put("shipperId", accountNumber); - } - } - } - } catch (GenericEntityException e) { - Debug.logError(e, module); - } - bodyParameters.put("shipmentId", shipmentId); - bodyParameters.put("orderId", orderId); - bodyParameters.put("userLogin", userLogin); - String bodyScreenUri = UtilProperties.getPropertyValue("oagis.properties", "Oagis.Template.ProcessShipment"); - - Writer writer = null; - if (out != null) { - writer = new OutputStreamWriter(out); - } else if (UtilValidate.isNotEmpty(saveToFilename)) { - try { - File outdir = new File(saveToDirectory); - if (!outdir.exists()) { - outdir.mkdir(); - } - writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outdir, saveToFilename)), "UTF-8"))); - } catch (Exception e) { - String errMsg = "Error opening file to save message to [" + saveToFilename + "]: " + e.toString(); - Debug.logError(e, errMsg, module); - return ServiceUtil.returnError(errMsg); - } - } else if (UtilValidate.isNotEmpty(sendToUrl)) { - writer = new StringWriter(); - } - - ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, new HtmlScreenRenderer()); - try { - screens.render(bodyScreenUri); - writer.close(); - } catch (Exception e) { - String errMsg = "Error rendering message: " + e.toString(); - Debug.logError(e, errMsg, module); - return ServiceUtil.returnError(errMsg); - } - - // TODO: call service with require-new-transaction=true to save the OagisMessageInfo data (to make sure it saves before) - - if (UtilValidate.isNotEmpty(sendToUrl)) { - HttpClient http = new HttpClient(sendToUrl); - - // test parameters - http.setHostVerificationLevel(SSLUtil.HOSTCERT_NO_CHECK); - http.setAllowUntrusted(true); - http.setDebug(true); - - // needed XML post parameters - if (UtilValidate.isNotEmpty(certAlias)) { - http.setClientCertificateAlias(certAlias); - } - if (UtilValidate.isNotEmpty(basicAuthUsername)) { - http.setBasicAuthInfo(basicAuthUsername, basicAuthPassword); - } - http.setContentType("text/xml"); - http.setKeepAlive(true); - - try { - String resp = http.post(writer.toString()); - } catch (Exception e) { - String errMsg = "Error posting message to server with UTL [" + sendToUrl + "]: " + e.toString(); - Debug.logError(e, errMsg, module); - return ServiceUtil.returnError(errMsg); - } - } - - // prepare map to Create Oagis Message Info - result.put("component", "INVENTORY"); - result.put("task", "SHIPREQUES"); // Actual value of task is "SHIPREQUEST" which is more than 10 char - result.put("outgoingMessage", "Y"); - result.put("confirmation", "1"); - result.put("bsrVerb", "PROCESS"); - result.put("bsrNoun", "SHIPMENT"); - result.put("bsrRevision", "001"); - result.put("processingStatusId", orderStatusId); - result.put("orderId", orderId); - result.put("shipmentId", shipmentId); - result.put("userLogin", userLogin); + } catch (GenericServiceException e) { + String errMsg = "Error authorizing payment: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); } } - return result; + if (checkPaymentAuth) { + GenericValue orderHeader = null; + GenericValue orderItemShipGroup = null; + try { + orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + GenericValue shipment =null; + if (orderHeader != null) { + String orderStatusId = orderHeader.getString("statusId"); + if (orderStatusId.equals("ORDER_APPROVED")) { + try { + Map cospResult= dispatcher.runSync("createOrderShipmentPlan", UtilMisc.toMap("orderId", orderId, "userLogin", userLogin)); + shipmentId = (String) cospResult.get("shipmentId"); + } catch (GenericServiceException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + try { + shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); + bodyParameters.put("shipment", shipment); + OrderReadHelper orderReadHelper = new OrderReadHelper(orderHeader); + if(orderReadHelper.hasShippingAddress()) { + GenericValue address = EntityUtil.getFirst(orderReadHelper.getShippingLocations()); + bodyParameters.put("address", address); + } + String emailString = orderReadHelper.getOrderEmailString(); + bodyParameters.put("emailString", emailString); + + String contactMechId = shipment.getString("destinationTelecomNumberId"); + GenericValue telecomNumber = delegator.findByPrimaryKey("TelecomNumber", UtilMisc.toMap("contactMechId", contactMechId)); + bodyParameters.put("telecomNumber", telecomNumber); + + List shipmentItems = delegator.findByAnd("ShipmentItem", UtilMisc.toMap("shipmentId", shipmentId)); + bodyParameters.put("shipmentItems", shipmentItems); + + orderItemShipGroup = EntityUtil.getFirst(delegator.findByAnd("OrderItemShipGroup", UtilMisc.toMap("orderId", orderId))); + bodyParameters.put("orderItemShipGroup", orderItemShipGroup); + } catch (GenericEntityException e) { + Debug.logError(e, module); + } + + Set correspondingPoIdSet = FastSet.newInstance(); + try { + List orderItems = delegator.findByAnd("OrderItem", UtilMisc.toMap("orderId", shipment.getString("primaryOrderId"))); + Iterator oiIter = orderItems.iterator(); + while (oiIter.hasNext()) { + GenericValue orderItem = (GenericValue) oiIter.next(); + String correspondingPoId = orderItem.getString("correspondingPoId"); + if (correspondingPoId != null) { + correspondingPoIdSet.add(correspondingPoId); + } + } + } catch (GenericEntityException e) { + Debug.logError(e, module); + } + bodyParameters.put("correspondingPoIdSet", correspondingPoIdSet); + + Set externalIdSet = FastSet.newInstance(); + try { + GenericValue primaryOrderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", shipment.getString("primaryOrderId"))); + String externalId = primaryOrderHeader.getString("externalId"); + if (externalId != null) { + externalIdSet.add(externalId); + } + } catch (GenericEntityException e) { + Debug.logError(e, module); + } + bodyParameters.put("externalIdSet", externalIdSet); + + // if order was a return replacement order (associated with return) + List returnItemResponses = null; + List returnItemRespExprs = UtilMisc.toList(new EntityExpr("replacementOrderId", EntityOperator.NOT_EQUAL, null)); + EntityCondition returnItemRespCond = new EntityConditionList(returnItemRespExprs, EntityOperator.AND); + // list of fields to select (initial list) + List fieldsToSelect = FastList.newInstance(); + fieldsToSelect.add("replacementOrderId"); + try { + returnItemResponses = delegator.findByCondition("ReturnItemResponse", returnItemRespCond, fieldsToSelect, null); + Iterator rirIter = returnItemResponses.iterator(); + while (rirIter.hasNext()) { + GenericValue returnItemResponse = (GenericValue) rirIter.next(); + String replacementOrderId = returnItemResponse.getString("replacementOrderId"); + if (replacementOrderId.equals(shipment.getString("primaryOrderId"))) { + bodyParameters.put("shipnotes", "RETURNLABEL"); + + // Get the associated return Id (replaceReturnId) + String returnItemResponseId = returnItemResponse.getString("returnItemResponseId"); + GenericValue returnItem = EntityUtil.getFirst(delegator.findByAnd("ReturnItem", UtilMisc.toMap("returnItemResponseId", returnItemResponseId))); + bodyParameters.put("replacementReturnId", returnItem.getString("returnId")); + } + } + } catch (GenericEntityException e) { + Debug.logError(e, module); + } + String logicalId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.LOGICALID"); + bodyParameters.put("logicalId", logicalId); + comiCtx.put("logicalId", logicalId); + + String authId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.AUTHID"); + bodyParameters.put("authId", authId); + comiCtx.put("authId", authId); + + String referenceId = delegator.getNextSeqId("OagisMessageInfo"); + bodyParameters.put("referenceId", referenceId); + comiCtx.put("referenceId", referenceId); + + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'Z"); + Timestamp timestamp = UtilDateTime.nowTimestamp(); + String sentDate = dateFormat.format(timestamp); + bodyParameters.put("sentDate", sentDate); + comiCtx.put("sentDate", timestamp); + + // tracking shipper account + String partyId = shipment.getString("partyIdTo"); + List partyCarrierAccounts = new ArrayList(); + try { + partyCarrierAccounts = delegator.findByAnd("PartyCarrierAccount", UtilMisc.toMap("partyId", partyId)); + partyCarrierAccounts = EntityUtil.filterByDate(partyCarrierAccounts); + if (partyCarrierAccounts != null) { + Iterator pcaIter = partyCarrierAccounts.iterator(); + while (pcaIter.hasNext()) { + GenericValue partyCarrierAccount = (GenericValue) pcaIter.next(); + String carrierPartyId = partyCarrierAccount.getString("carrierPartyId"); + if (carrierPartyId.equals(orderItemShipGroup.getString("carrierPartyId"))) { + String accountNumber = partyCarrierAccount.getString("accountNumber"); + bodyParameters.put("shipperId", accountNumber); + } + } + } + } catch (GenericEntityException e) { + Debug.logError(e, module); + } + bodyParameters.put("shipmentId", shipmentId); + bodyParameters.put("orderId", orderId); + bodyParameters.put("userLogin", userLogin); + String bodyScreenUri = UtilProperties.getPropertyValue("oagis.properties", "Oagis.Template.ProcessShipment"); + + Writer writer = null; + if (out != null) { + writer = new OutputStreamWriter(out); + } else if (UtilValidate.isNotEmpty(saveToFilename)) { + try { + File outdir = new File(saveToDirectory); + if (!outdir.exists()) { + outdir.mkdir(); + } + writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outdir, saveToFilename)), "UTF-8"))); + } catch (Exception e) { + String errMsg = "Error opening file to save message to [" + saveToFilename + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } else if (UtilValidate.isNotEmpty(sendToUrl)) { + writer = new StringWriter(); + } + + ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, new HtmlScreenRenderer()); + try { + screens.render(bodyScreenUri); + writer.close(); + } catch (Exception e) { + String errMsg = "Error rendering message: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + // TODO: call service with require-new-transaction=true to save the OagisMessageInfo data (to make sure it saves before) + // prepare map to Create Oagis Message Info + comiCtx.put("component", "INVENTORY"); + comiCtx.put("task", "SHIPREQUES"); // Actual value of task is "SHIPREQUEST" which is more than 10 char + comiCtx.put("outgoingMessage", "Y"); + comiCtx.put("confirmation", "1"); + comiCtx.put("bsrVerb", "PROCESS"); + comiCtx.put("bsrNoun", "SHIPMENT"); + comiCtx.put("bsrRevision", "001"); + comiCtx.put("processingStatusId", orderStatusId); + comiCtx.put("orderId", orderId); + comiCtx.put("shipmentId", shipmentId); + comiCtx.put("userLogin", userLogin); + + try { + Map oagisMsgInfoResult = dispatcher.runSync("createOagisMessageInfo", comiCtx); + } catch (GenericServiceException e){ + String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "OagisErrorInCreatingDataForOagisMessageInfoEntity", locale); + Debug.logError(e, errMsg, module); + } + + if (UtilValidate.isNotEmpty(sendToUrl)) { + HttpClient http = new HttpClient(sendToUrl); + + // test parameters + http.setHostVerificationLevel(SSLUtil.HOSTCERT_NO_CHECK); + http.setAllowUntrusted(true); + http.setDebug(true); + + // needed XML post parameters + if (UtilValidate.isNotEmpty(certAlias)) { + http.setClientCertificateAlias(certAlias); + } + if (UtilValidate.isNotEmpty(basicAuthUsername)) { + http.setBasicAuthInfo(basicAuthUsername, basicAuthPassword); + } + http.setContentType("text/xml"); + http.setKeepAlive(true); + + try { + String resp = http.post(writer.toString()); + } catch (Exception e) { + String errMsg = "Error posting message to server with UTL [" + sendToUrl + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } + } + } + return result; + } else { + String errMsg = "Service failed:"; + return ServiceUtil.returnError(errMsg); + } } public static Map oagisReceiveDelivery(DispatchContext dctx, Map context) {