3 Analyzing EDI-to-XML Conversions : Managing Transmission Responses

Managing Transmission Responses
Some EDI specifications define the interchanges and messages to be used to notify the EDI sender about transmission status, from initial receipt of the transmission to the errors, if any, encountered in the EDI data stream received from the EDI sender. For example:
NOTE: This also includes EDI dialects that use the EDIFACT syntax such as EANCOM, Edig@s, and IATA/PADIS.
As described in “Response Element”, the analyze method generates dialect-specific transmission responses as Receipt and Acknowledgement elements in the EDI analysis report.
This section covers the following topics:
Receipt Element Example
Here is an example of the Receipt element from the analysis report created using the analyze method to convert the sample file threemsgs.x12 to XML:
        <Receipt>
            <X12>
                <ISA>
                    <ISA01>00</ISA01>
                    <ISA03>00</ISA03>
                    <ISA05>01</ISA05>
                    <ISA06>5151515151</ISA06>
                    <ISA07>01</ISA07>
                    <ISA08>1515151515</ISA08>
                    <ISA11>^</ISA11>
                    <ISA12>00403</ISA12>
                    <ISA13>0</ISA13>
                    <ISA14>0</ISA14>
                    <ISA15>P</ISA15>
                    <ISA16>*</ISA16>
                </ISA>
                <TA1>
                    <TA101>32123</TA101>
                    <TA102>041201</TA102>
                    <TA103>1217</TA103>
                    <TA104>A</TA104>
                    <TA105>000</TA105>
                </TA1>
                <IEA/>
            </X12>
        </Receipt>
The specific structure of the Receipt element varies based on the dialect, and contents, of the EDI stream being converted. In this example:
Other EDI segments that are computed when the XML is converted to EDI include the Transaction Set Trailer (SE) and Function Group Trailer (GE).
Acknowledgement Element Example
Following is an example of the Acknowledgement element from the same analysis report created using the analyze method to convert the sample file threemsgs.x12 to XML. Note that is has been abbreviated for formatting considerations.
        <Acknowledgement>
            <X12>
                <ISA> … </ISA>
                <GS> … </GS>
                <TS_997>
                    <ST>
                        <ST01>997</ST01>
                        <ST02>0</ST02>
                    </ST>
                    <AK1>
                        <AK101>CT</AK101>
                        <AK102>128</AK102>
                    </AK1>
                    <AK2>
                        <AK201>831</AK201>
                        <AK202>00128001</AK202>
                    </AK2>
                    <AK5> … </AK5>
                    <AK2>
                        <AK201>831</AK201>
                        <AK202>00128002</AK202>
                    </AK2>
                    <AK3> … </AK3>
                    <AK4>
                        <AK401> … </AK401>
                        <AK402>782</AK402>
                        <AK403>6</AK403>
                        <AK404>ZZZZ</AK404>
                    </AK4>
                    <AK5> … </AK5>
                    <AK2> … </AK2>
                        <AK201>831</AK201>
                        <AK202>00128003</AK202>
                    <AK5> … </AK5>
                    <AK9>
                        <AK901>P</AK901>
                        <AK902>3</AK902>
                        <AK903>3</AK903>
                        <AK904>2</AK904>
                    </AK9>
                    <SE/>
                </TS_997>
                <GE/>
                <IEA/>
            </X12>
        </Acknowledgement>
In this example:
NOTES:
The type of transaction message that is generated for X12 (997 or 999) depends on the xr= URI property. See Table 5-9, “Properties for the EDI XML Converter” for details about setting the xr= URI property. The preceding example shows a functional acknowledgement transaction message as indicated by the TS_997 element. Similarly, the TS_999 element is the message wrapper for X12 implementation acknowledgement transaction messages.
If a message contains an error, the analysis report can also contain elements representing the following segments:
NOTE: The Acknowledgement element can also include a TA1 element, as controlled by a combination of the ISA14 element in the incoming transaction set and the ta1= URI property. If a TA1 element is generated, it appears immediately before the ISA element.
Converting Response Messages to EDI
Because transmission responses are structured as XML, they need to be converted to EDI before they can be returned to the EDI sender. This example takes you through the following tasks:
Use the analyze method to generate the analysis report and convert the EDI data stream (in this case, a sample X12 EDI document, threemsgs.x12) to XML.
To see a complete example application that converts both Receipt and Acknowledgement responses to EDI, see “Example 14”.
Invoking the analyze Method
The EDI XML Converter is used to initiate the conversion of the source EDI document, threemsgs.x12 to EDI:
InputStream inStream = null;
   try {
   Source ediSource = new StreamSource(exampleURI + "threemsgs.x12");
   ConvertToXML toXml = factory.newConvertToXML("converter:EDI");
Next, the analyze method is used to generate the analysis report and save the output to report.xml:
Result reportResult = new StreamResult("report.xml");
toXml.analyze(ediSource, reportResult);
See “Receipt Element Example” for a sample of the analysis report, report.xml.
Converting the Source EDI
The analysis report is used as input to convert the EDI stream to XML. Any errors in the EDI stream are recorded in the analysis report, which is used by ConvertToXML as a filter so that only valid EDI messages are converted to XML. Here, the valid EDI is written to an XML document, twomsgs.xml.
Source reportSource = new StreamSource("report.xml");
Result xmlResult = new StreamResult("twomsgs.xml");
toXml.convert(ediSource, xmlResult, reportSource);
Locating Response Messages
The EDI analysis report is used again, this time as the source for the EDI transmission response messages that have been generated in the Receipt and Acknowledgement elements in the XML report. The content of these elements are converted to EDI for transmission back to the EDI sender. Here, the report is opened with an instance of XMLStreamReader.
inStream = new FileInputStream("report.xml");
XMLStreamReader rdr =
   XMLInputFactory.newInstance().createXMLStreamReader(inStream);
Once the XMLStreamReader object is created, we can read through the analysis report, skipping first to the Receipt element, then to the X12 element:
do {
   rdr.next();
   } while(rdr.getEventType() != XMLStreamConstants.START_ELEMENT
      || !rdr.getName().getLocalPart().equals("Receipt"));
      do {
         rdr.next();
      } while(rdr.getEventType() != XMLStreamConstants.START_ELEMENT
         || !rdr.getName().getLocalPart().equals("X12"));
For a refresher of the Receipt element structure, see “Receipt Element Example”.
Converting the Receipt Element to EDI
Once the Receipt element is located in the analysis report, it can be converted to EDI for transmission back to the EDI sender. Note that the converter: property is specified as EDI.
ConvertFromXML converter = factory.newConvertFromXML("converter:EDI");
Source responseSource = new XMLStreamReaderSource(rdr);
Result receiptResult = new StreamResult("receipt.x12");
converter.convert(responseSource, receiptResult);
The resulting EDI is written to the file, receipt.x12, which contains the following:
ISA+00+ +00+ +01+5151515151 +01+1515151515 +090818+1110+^+00403+000000000+0+P+*'
TA1+000032123+041201+1217+A+000'
IEA+0+000000000'
Notice that the IEA segment (Interchange Control Trailer), which was represented in the original XML conversion of the source EDI document as an empty element (<IEA/) has been automatically computed by the EDI XML Converter and now includes values for the Number of Included Functional Groups (IEA01) and Interchange Control Number (IEA02) segments.
Sending Responses to the EDI Sender
Typically, when you send a response to an EDI sender, you must provide each interchange with a unique identifier. For X12, for example, this is the Interchange Control Number (ISA12) segment; for EDIFACT, this is the Interchange Control Reference (UNB05) segment.
You can perform this task as part of the application code that extracts the Receipt or Acknowledgement element from the analysis report; it is expected that the generation of a unique identifier is handled elsewhere.
Example
This simple XQuery locates the Interchange Control Header (ISA) segment, replaces the value of the Interchange Control Number (ISA12) segment with 1000, and converts the node to EDI for transmission to the EDI sender.
declare option ddtek:serialize "method=EDI";
copy $temp := /AnalyzeReport/Response/Acknowledgement/X12
modify replace value of node $temp/ISA/ISA12 with 1000
return $temp