From 84b6830ea86cba33c4bafb285680763cb5281deb Mon Sep 17 00:00:00 2001 From: "David E. Jones" Date: Fri, 3 Aug 2007 13:05:18 +0000 Subject: [PATCH] Some code updates to handle errors better and save interim information, and clean up how certain things flow and are done for more consistent operation git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk/specialpurpose@562455 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/ofbiz/oagis/OagisMessageServices.xml | 17 +- .../ofbiz/oagis/OagisInventoryServices.java | 16 +- oagis/src/org/ofbiz/oagis/OagisServices.java | 129 +++++---- .../ofbiz/oagis/OagisShipmentServices.java | 246 +++++++----------- 4 files changed, 177 insertions(+), 231 deletions(-) diff --git a/oagis/script/org/ofbiz/oagis/OagisMessageServices.xml b/oagis/script/org/ofbiz/oagis/OagisMessageServices.xml index bf3fa5ae5..9bd96130c 100644 --- a/oagis/script/org/ofbiz/oagis/OagisMessageServices.xml +++ b/oagis/script/org/ofbiz/oagis/OagisMessageServices.xml @@ -49,9 +49,7 @@ under the License. - - - + @@ -78,17 +76,4 @@ under the License. - - - - - - - - - - - - diff --git a/oagis/src/org/ofbiz/oagis/OagisInventoryServices.java b/oagis/src/org/ofbiz/oagis/OagisInventoryServices.java index e45e24c92..ac436a4e9 100644 --- a/oagis/src/org/ofbiz/oagis/OagisInventoryServices.java +++ b/oagis/src/org/ofbiz/oagis/OagisInventoryServices.java @@ -315,7 +315,7 @@ public class OagisInventoryServices { } } try { - dispatcher.runAsync("createOagisMessageInfo", comiCtx, true); + dispatcher.runSync("createOagisMessageInfo", comiCtx, 60, true); /* now calling async for better error handling if (ServiceUtil.isError(comiResult)) { String errMsg = ServiceUtil.getErrorMessage(comiResult); @@ -324,7 +324,7 @@ public class OagisInventoryServices { */ } catch (GenericServiceException e) { String errMsg = "Error creating OagisMessageInfo for the Incoming Message: " + e.toString(); - errorMapList.add(UtilMisc.toMap("reasonCode", "CreateOagisMessageInfoError", "description", errMsg)); + //errorMapList.add(UtilMisc.toMap("reasonCode", "CreateOagisMessageInfoError", "description", errMsg)); Debug.logError(e, errMsg, module); } @@ -524,7 +524,7 @@ public class OagisInventoryServices { } } try { - dispatcher.runAsync("createOagisMessageInfo", comiCtx, true); + dispatcher.runSync("createOagisMessageInfo", comiCtx, 60, true); /* running async for better error handling if (ServiceUtil.isError(comiResult)) { String errMsg = ServiceUtil.getErrorMessage(comiResult); @@ -533,8 +533,8 @@ public class OagisInventoryServices { */ } catch (GenericServiceException e) { String errMsg = "Error creating OagisMessageInfo for the Incoming Message: " + e.toString(); - // TODO: reconsider sending this error back to other server, not much they can do about it, and it may not be a critical error causing the message to be rejected... - errorMapList.add(UtilMisc.toMap("reasonCode", "CreateOagisMessageInfoError", "description", errMsg)); + // reconsider sending this error back to other server, not much they can do about it, and it may not be a critical error causing the message to be rejected... + //errorMapList.add(UtilMisc.toMap("reasonCode", "CreateOagisMessageInfoError", "description", errMsg)); Debug.logError(e, errMsg, module); } Map result = FastMap.newInstance(); @@ -766,7 +766,7 @@ public class OagisInventoryServices { } } try { - dispatcher.runAsync("createOagisMessageInfo", comiCtx); + dispatcher.runSync("createOagisMessageInfo", comiCtx, 60, true); /* running async for better error handling if (ServiceUtil.isError(comiResult)) { String errMsg = ServiceUtil.getErrorMessage(comiResult); @@ -775,8 +775,8 @@ public class OagisInventoryServices { */ } catch (GenericServiceException e) { String errMsg = "Error creating OagisMessageInfo for the Incoming Message: " + e.toString(); - // TODO: reconsider sending this error back to other server, not much they can do about it, and it may not be a critical error causing the message to be rejected... - errorMapList.add(UtilMisc.toMap("reasonCode", "CreateOagisMessageInfoError", "description", errMsg)); + // reconsider sending this error back to other server, not much they can do about it, and it may not be a critical error causing the message to be rejected... + //errorMapList.add(UtilMisc.toMap("reasonCode", "CreateOagisMessageInfoError", "description", errMsg)); Debug.logError(e, errMsg, module); } diff --git a/oagis/src/org/ofbiz/oagis/OagisServices.java b/oagis/src/org/ofbiz/oagis/OagisServices.java index fd83631ad..721767b40 100644 --- a/oagis/src/org/ofbiz/oagis/OagisServices.java +++ b/oagis/src/org/ofbiz/oagis/OagisServices.java @@ -1,6 +1,4 @@ -package org.ofbiz.oagis; - -/** +/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information @@ -17,7 +15,9 @@ software distributed under the License is distributed on an KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -**/ +*/ +package org.ofbiz.oagis; + import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; @@ -127,31 +127,13 @@ public class OagisServices { bodyParameters.put("origRef", context.get("origRefId")); String bodyScreenUri = UtilProperties.getPropertyValue("oagis.properties", "Oagis.Template.ConfirmBod"); - 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(); - } else { - return ServiceUtil.returnError("No send to information pass (url, file, or out stream)"); - } - - ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, new HtmlScreenRenderer()); + String outText = null; try { + Writer writer = new StringWriter(); + ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, new HtmlScreenRenderer()); screens.render(bodyScreenUri); writer.close(); + outText = writer.toString(); } catch (Exception e) { String errMsg = "Error rendering message: " + e.toString(); Debug.logError(e, errMsg, module); @@ -171,8 +153,11 @@ public class OagisServices { oagisMsgInfoContext.put("bsrNoun", "BOD"); oagisMsgInfoContext.put("bsrRevision", "004"); oagisMsgInfoContext.put("userLogin", userLogin); + if (OagisServices.debugSaveXmlOut) { + oagisMsgInfoContext.put("fullMessageXml", outText); + } try { - dispatcher.runAsync("createOagisMessageInfo", oagisMsgInfoContext, true); + dispatcher.runSync("createOagisMessageInfo", oagisMsgInfoContext, 60, true); /* running async for better error handling if (ServiceUtil.isError(oagisMsgInfoResult)) return ServiceUtil.returnError("Error creating OagisMessageInfo"); */ @@ -181,31 +166,9 @@ public class OagisServices { Debug.logError(e, "Saving message to database failed", 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); - } + Map sendMessageReturn = OagisServices.sendMessageText(outText, out, sendToUrl, saveToDirectory, saveToFilename); + if (sendMessageReturn != null) { + return sendMessageReturn; } return ServiceUtil.returnSuccess("Service Completed Successfully"); @@ -272,7 +235,7 @@ public class OagisServices { oagisMsgInfoCtx.put("outgoingMessage", "N"); oagisMsgInfoCtx.put("userLogin", userLogin); try { - dispatcher.runAsync("createOagisMessageInfo", oagisMsgInfoCtx, true); + dispatcher.runSync("createOagisMessageInfo", oagisMsgInfoCtx, 60, true); /* running async for better error handling if (ServiceUtil.isError(oagisMsgInfoResult)){ String errMsg = "Error creating OagisMessageInfo for the Incoming Message: "+ServiceUtil.getErrorMessage(oagisMsgInfoResult); @@ -371,7 +334,7 @@ public class OagisServices { ByteArrayInputStream bis = new ByteArrayInputStream(xmlText.getBytes("UTF-8")); doc = UtilXml.readXmlDocument(bis, true, "OagisMessage"); } catch (SAXException e) { - String errMsg = "XML Error parsing the Received Message [" + e.toString() + "]; The text received we could not parse is: " + xmlText; + String errMsg = "XML Error parsing the Received Message [" + e.toString() + "]; The text received we could not parse is: [" + xmlText + "]"; errorList.add(UtilMisc.toMap("description", errMsg, "reasonCode", "SAXException")); Debug.logError(e, errMsg, module); } catch (ParserConfigurationException e) { @@ -469,4 +432,62 @@ public class OagisServices { return result; } + + public static Map sendMessageText(String outText, OutputStream out, String sendToUrl, String saveToDirectory, String saveToFilename) { + if (out != null) { + Writer outWriter = new OutputStreamWriter(out); + try { + outWriter.write(outText); + outWriter.close(); + } catch (IOException e) { + String errMsg = "Error writing message to output stream: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } else if (UtilValidate.isNotEmpty(saveToFilename)) { + try { + File outdir = new File(saveToDirectory); + if (!outdir.exists()) { + outdir.mkdir(); + } + Writer outWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outdir, saveToFilename)), "UTF-8"))); + outWriter.write(outText); + outWriter.close(); + } catch (Exception e) { + String errMsg = "Error saving message to file [" + saveToFilename + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } else 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 { + http.post(outText); + } catch (Exception e) { + String errMsg = "Error posting message to server with URL [" + sendToUrl + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } else { + if (Debug.infoOn()) Debug.logInfo("No send to information, so here is the message: " + outText, module); + return ServiceUtil.returnError("No send to information pass (url, file, or out stream)"); + } + + return null; + } } diff --git a/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java b/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java index a7e3d5388..3bd034c3a 100644 --- a/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java +++ b/oagis/src/org/ofbiz/oagis/OagisShipmentServices.java @@ -1,6 +1,4 @@ -package org.ofbiz.oagis; - -/** +/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information @@ -17,39 +15,29 @@ software distributed under the License is distributed on an KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -**/ -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; +*/ + +package org.ofbiz.oagis; + import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; 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; - -import javax.xml.parsers.ParserConfigurationException; import javolution.util.FastList; import javolution.util.FastMap; import javolution.util.FastSet; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.HttpClient; -import org.ofbiz.base.util.SSLUtil; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilProperties; @@ -65,18 +53,16 @@ import org.ofbiz.entity.condition.EntityExpr; import org.ofbiz.entity.condition.EntityOperator; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.order.order.OrderReadHelper; +import org.ofbiz.party.party.PartyWorker; import org.ofbiz.service.DispatchContext; import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; -import org.ofbiz.service.ModelService; import org.ofbiz.service.ServiceUtil; import org.ofbiz.widget.fo.FoFormRenderer; import org.ofbiz.widget.html.HtmlScreenRenderer; import org.ofbiz.widget.screen.ScreenRenderer; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.xml.sax.SAXException; -import org.ofbiz.party.party.PartyWorker; public class OagisShipmentServices { @@ -298,7 +284,6 @@ public class OagisShipmentServices { LocalDispatcher dispatcher = ctx.getDispatcher(); GenericDelegator delegator = ctx.getDelegator(); String orderId = (String) context.get("orderId"); - String shipmentId = (String) context.get("shipmentId"); String sendToUrl = (String) context.get("sendToUrl"); if (UtilValidate.isEmpty(sendToUrl)) { @@ -306,9 +291,19 @@ public class OagisShipmentServices { } String saveToFilename = (String) context.get("saveToFilename"); + if (UtilValidate.isEmpty(saveToFilename)) { + String saveToFilenameBase = UtilProperties.getPropertyValue("oagis.properties", "test.save.outgoing.filename.base", ""); + saveToFilename = saveToFilenameBase + "ProcessShipment" + orderId + ".xml"; + } String saveToDirectory = (String) context.get("saveToDirectory"); + if (UtilValidate.isEmpty(saveToDirectory)) { + saveToDirectory = UtilProperties.getPropertyValue("oagis.properties", "test.save.outgoing.directory"); + } + OutputStream out = (OutputStream) context.get("outputStream"); + if (Debug.infoOn()) Debug.logInfo("Call to oagisProcessShipment for orderId [" + orderId + "], sendToUrl=[" + sendToUrl + "], saveToDirectory=[" + saveToDirectory + "], saveToFilename=[" + saveToFilename + "]", module); + Map result = ServiceUtil.returnSuccess(); MapStack bodyParameters = MapStack.create(); @@ -347,8 +342,45 @@ public class OagisShipmentServices { if (orderHeader != null) { String orderStatusId = orderHeader.getString("statusId"); if (orderStatusId.equals("ORDER_APPROVED")) { - try { + String logicalId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.LOGICALID"); + bodyParameters.put("logicalId", logicalId); + Map comiCtx = UtilMisc.toMap("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); + // prepare map to Create Oagis Message Info + comiCtx.put("processingStatusId", "OAGMP_TRIGGERED"); + 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("userLogin", userLogin); + try { + dispatcher.runSync("createOagisMessageInfo", comiCtx, 60, true); + } catch (GenericServiceException e){ + String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "OagisErrorInCreatingDataForOagisMessageInfoEntity", (Locale) context.get("locale")); + Debug.logError(e, errMsg, module); + } + if (Debug.infoOn()) Debug.logInfo("Saved OagisMessageInfo for oagisProcessShipment message for orderId [" + orderId + "]", module); + + String shipmentId = null; + try { // check to see if there is already a Shipment for this order EntityCondition findShipmentCondition = new EntityConditionList(UtilMisc.toList( new EntityExpr("primaryOrderId", EntityOperator.EQUALS, orderId), @@ -437,110 +469,51 @@ public class OagisShipmentServices { return ServiceUtil.returnError(errMsg); } - String logicalId = UtilProperties.getPropertyValue("oagis.properties", "CNTROLAREA.SENDER.LOGICALID"); - bodyParameters.put("logicalId", logicalId); - Map comiCtx = UtilMisc.toMap("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); - 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(); - } else { - return ServiceUtil.returnError("No send to information pass (url, file, or out stream)"); - } - - ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, new HtmlScreenRenderer()); + String outText = null; try { + Writer writer = new StringWriter(); + ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, htmlScreenRenderer); screens.render(bodyScreenUri); writer.close(); + outText = writer.toString(); } catch (Exception e) { String errMsg = "Error rendering message: " + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } - - // TODO: should we make sure this is saved in error conditions - // 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); + if (Debug.infoOn()) Debug.logInfo("Finished rendering oagisProcessShipment message for orderId [" + orderId + "]", module); + + comiCtx.put("processingStatusId", "OAGMP_OGEN_SUCCESS"); comiCtx.put("shipmentId", shipmentId); - comiCtx.put("userLogin", userLogin); if (OagisServices.debugSaveXmlOut) { - comiCtx.put("fullMessageXml", writer.toString()); + comiCtx.put("fullMessageXml", outText); } - try { - dispatcher.runAsync("createOagisMessageInfo", comiCtx, true); + dispatcher.runSync("updateOagisMessageInfo", comiCtx, 60, true); } catch (GenericServiceException e){ String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "OagisErrorInCreatingDataForOagisMessageInfoEntity", (Locale) context.get("locale")); Debug.logError(e, errMsg, module); } - - if (UtilValidate.isNotEmpty(sendToUrl)) { - HttpClient http = new HttpClient(sendToUrl); + Map sendMessageReturn = OagisServices.sendMessageText(outText, out, sendToUrl, saveToDirectory, saveToFilename); + if (sendMessageReturn != null) { + return sendMessageReturn; + } + if (Debug.infoOn()) Debug.logInfo("Message send done for oagisProcessShipment for orderId [" + orderId + "], sendToUrl=[" + sendToUrl + "], saveToDirectory=[" + saveToDirectory + "], saveToFilename=[" + saveToFilename + "]", module); - // 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 { - 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); - } + comiCtx.put("processingStatusId", "OAGMP_SENT"); + try { + dispatcher.runSync("updateOagisMessageInfo", comiCtx, 60, true); + } catch (GenericServiceException e){ + String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "OagisErrorInCreatingDataForOagisMessageInfoEntity", (Locale) context.get("locale")); + Debug.logError(e, errMsg, module); } } } @@ -559,7 +532,14 @@ public class OagisShipmentServices { } String saveToFilename = (String) context.get("saveToFilename"); + if (UtilValidate.isEmpty(saveToFilename)) { + String saveToFilenameBase = UtilProperties.getPropertyValue("oagis.properties", "test.save.outgoing.filename.base", ""); + saveToFilename = saveToFilenameBase + "ProcessShipment" + returnId + ".xml"; + } String saveToDirectory = (String) context.get("saveToDirectory"); + if (UtilValidate.isEmpty(saveToDirectory)) { + saveToDirectory = UtilProperties.getPropertyValue("oagis.properties", "test.save.outgoing.directory"); + } OutputStream out = (OutputStream) context.get("outputStream"); Map result = ServiceUtil.returnSuccess(); @@ -651,37 +631,19 @@ public class OagisShipmentServices { bodyParameters.put("returnId", returnId); String bodyScreenUri = UtilProperties.getPropertyValue("oagis.properties", "Oagis.Template.ReceiveDelivery"); - 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(); - } else { - return ServiceUtil.returnError("No send to information pass (url, file, or out stream)"); - } - - ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, new HtmlScreenRenderer()); + String outText = null; try { + Writer writer = new StringWriter(); + ScreenRenderer screens = new ScreenRenderer(writer, bodyParameters, htmlScreenRenderer); screens.render(bodyScreenUri); writer.close(); + outText = writer.toString(); } 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"); @@ -695,41 +657,19 @@ public class OagisShipmentServices { comiCtx.put("returnId", returnId); comiCtx.put("userLogin", userLogin); if (OagisServices.debugSaveXmlOut) { - comiCtx.put("fullMessageXml", writer.toString()); + comiCtx.put("fullMessageXml", outText); } try { - dispatcher.runAsync("createOagisMessageInfo", comiCtx, true); + dispatcher.runSync("createOagisMessageInfo", comiCtx, 60, true); } catch (GenericServiceException e) { String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "OagisErrorInCreatingDataForOagisMessageInfoEntity", (Locale) context.get("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 { - 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); - } + Map sendMessageReturn = OagisServices.sendMessageText(outText, out, sendToUrl, saveToDirectory, saveToFilename); + if (sendMessageReturn != null) { + return sendMessageReturn; } } }