IBM Support

How to ensure that all records in a file contain the required number of characters

Technical Blog Post


Abstract

How to ensure that all records in a file contain the required number of characters

Body

Here is a real-world issue that crossed my desk recently:  our client received inbound files from trading partners in which each file record needed be exactly 512 characters in length. If a record contained fewer than 512 characters, the record needed to be padded with spaces. One trading partner sometimes sent records shorter than the required length, so the customer needed a means of ensuring that all records were compliant.  I thought about this issue and suggested the following code, which is deployed within a Java Task Service:

 

import com.sterlingcommerce.woodstock.workflow.Document;
import java.io.*;
import java.lang.System.*;
final int MAX_LEN = 512;
final String LS = System.getProperty("line.separator");
Document document = new Document();
Document srcDoc = wfc.getPrimaryDocument();
InputStream is = srcDoc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String thisStr = "default value";
StringBuffer contents = new StringBuffer();
char [] cBuf = new char[MAX_LEN];
char [] tmpArray;
int i;
while ((thisStr = br.readLine()) != null) {
  if (thisStr.length() < MAX_LEN) {
    tmpArray = thisStr.toCharArray();
    cBuf = new char[MAX_LEN];
    System.arraycopy(tmpArray, 0, cBuf, 0, thisStr.length());
    for (i = thisStr.length(); i < MAX_LEN; i++) {
      cBuf[i] = ' ';
     }
     thisStr = new String(cBuf, 0, MAX_LEN);
   }
   contents.append(thisStr);
   contents.append(LS);
}
br.close();
document.setBody(contents.toString().getBytes());
wfc.putPrimaryDocument(document);
return "OK";

 

Now that the code was created, a Java Task Service needed to be configured to call it.

This was done via Deployment > Services > Configuration > New Service > Go.


image

 

You have a choice of File or Inline for your code. If you choose File, you then specify the name of the file on the File System that contains the code. Choosing File will be necessary if there are many lines of code to be executed. This approach also comes in handy when you are first testing the code, as you can edit the file on the File System without having to go into the Service Configuration should you need to modify the code. In this particular case, though, I chose Inline, as the 31 lines of code were not too unwieldy:

image

 

 

After saving the Service, a simple Business Process allows us to test the service:

<process name="padLineTest">
  <sequence>
    <operation name="JavaTask Service">
    <participant name="padLine_javaTask"/>
      <output message="JavaTaskInputMessage">
        <assign to="." from="*"></assign>
      </output>
      <input message="inmsg">
        <assign to="." from="*"></assign>
      </input>
    </operation>
  </sequence>
</process>

 

This scenario may not arise very often, but if it does, these steps may provide the means to work around the issue.

 

 

[{"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SS3JSW","label":"IBM Sterling B2B Integrator"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

UID

ibm11120719