Technical Blog Post
Abstract
How to remove trailing spaces at the end of an EDI file in Sterling B2B Integrator
Body
Sterling B2B Integrator's EDIDeenvelope business process will process EDI files with trailing spaces and other extraneous data - the EDI will be processed, but if you view the Status Report for EDIDeenvelope, you may see something like:
Extracting CII interchanges... 0 interchanges extracted Extracting ACH interchanges... 0 interchanges extracted Extracting SWIFT XML format 2 interchanges... 0 interchanges extracted Extracting SWIFT FIN interchanges... 0 interchanges extracted Extracting CHIPS interchanges... 0 interchanges extracted Extracting FEDWIRE interchanges... 0 interchanges extracted Extracting SPEC2000 XML interchanges... 0 interchanges extracted Extracting SPEC2000 Cmd interchanges... 0 interchanges extracted Extracting interchanges... X12 between 0 and 1423 1 interchanges extracted Bad data found between 1423 and 1581 (end of document) --> EDIDeenvelope took 3 ms in thread
Can we remove trailing spaces from EDI files so that the data submitted to EDIDeenvelope is shorn of trailing spaces? The short answer is "Yes."
My first attempt involved using Java Task Service and Java's RandomAccessFile class. However, once I had removed the offending spaces from the end of the file, I had difficulty converting the RandomAccessFile back to a com.sterlingcommerce.woodstock.workflow.Document object, so I abandoned that approach and found one that was much simpler:
import com.sterlingcommerce.woodstock.workflow.Document;
import java.io.*;
import java.lang.System.*;
final int BUF_LEN = 8192;
Document document = new Document();
Document srcDoc = wfc.getPrimaryDocument();
InputStream is = srcDoc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder contents = new StringBuilder();
String thisStr = "foo";
char [] cBuf = new char[BUF_LEN];
int n;
while ((n = br.read(cBuf, 0, BUF_LEN)) != -1) {
if (n == BUF_LEN) {
thisStr = new String(cBuf);
}
else {
thisStr = String.valueOf(cBuf, 0, n);
}
contents.append(thisStr);
}
br.close();
document.setBody(contents.toString().trim().getBytes());
wfc.putPrimaryDocument(document);
return "OK";
The Java Task Service works in conjunction with a business process:
<process name="rmSpacesBeforeDeenveloping"> <sequence> <operation name="JavaTask Service"> <participant name="removeSpaces_JavaTask"/> <output message="JavaTaskInputMessage"> <assign to="." from="*"></assign> </output> <input message="inmsg"> <assign to="." from="*"></assign> </input> </operation> <operation name="Invoke Business Process Service"> <participant name="InvokeBusinessProcessService"/> <output message="InvokeBusinessProcessServiceTypeInputMessage"> <assign to="WFD_NAME">EDIDeenvelope</assign> <assign to="INVOKE_MODE">ASYNC</assign> <assign to="." from="*"></assign> </output> <input message="inmsg"> <assign to="." from="*"></assign> </input> </operation> </sequence> </process>
Now for the moment of truth:
The business process achieved the desired result - there was no extraneous data at the end of the EDI file:
Extracting CII interchanges... 0 interchanges extracted Extracting ACH interchanges... 0 interchanges extracted Extracting SWIFT XML format 2 interchanges... 0 interchanges extracted Extracting SWIFT FIN interchanges... 0 interchanges extracted Extracting CHIPS interchanges... 0 interchanges extracted Extracting FEDWIRE interchanges... 0 interchanges extracted Extracting SPEC2000 XML interchanges... 0 interchanges extracted Extracting SPEC2000 Cmd interchanges... 0 interchanges extracted Extracting interchanges... X12 between 0 and 1423 1 interchanges extracted EDIDeenvelope took 1 ms in thread
For those who prefer a mapping approach, the following may prove useful. You will need to specify the delimiters on the source and destination sides of the map. Note that though this example uses the X12 standard, the same appoach would work for EDIFACT and TRADACOMS:
The Extended Rule is:
string [1024] buffer;
string [3] match;
integer matchlen;
match = "IEA";
matchlen = len(match);
while (readblock(buffer)) do
begin
writeblock(trim(buffer));
if (left(buffer, matchlen) = match) then
begin
break;
end
end
UID
ibm11120881