Beautiful Code [305]
public static HashMap xpathmappings = new HashMap();
static {
xpathmappings.put("from_ContactName",
"//Pip3A2PriceAndAvailabilityRequest/fromRole/PartnerRoleDescription/
ContactInformation/contactName/FreeFormText");
xpathmappings.put("from_EmailAddress", "//Pip3A2PriceAndAvailabilityRequest/
fromRole/PartnerRoleDescription/ContactInformation/EmailAddress");
}
// Remaining xpath mappings omitted for brevity...
public HotkeyAdaptorRosProdAvailImpl() {
this.requestValues = new HashMap();
this.as400response = new HashMap();
}
public void setXML(String _xml) {
this.inputFile = _xml;
}
public boolean parseXML() {
try {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader r = new StringReader(this.inputFile);
org.xml.sax.InputSource is = new org.xml.sax.InputSource(r);
doc = db.parse(is);
Element root = doc.getDocumentElement();
Node node = null;
Iterator xpathvals = xpathmappings.values().iterator( );
Iterator xpathvars = xpathmappings.keySet().iterator( );
while (xpathvals.hasNext() && xpathvars.hasNext( )) {
node = XPathAPI.selectSingleNode(root, String)xpathvals.next( ));
requestValues.put((String)xpathvars.next( ),
node.getChildNodes().item(0).getNodeValue( ));
}
}
catch (Exception e) {
System.out.println(e.toString( ));
}
return true;
}
public boolean executeQuery() {
// Code omitted...
}
public String getResponseXML() {
// Code omitted...
}
}
The executeQuery method contains all of the code necessary to access the RPG code running on the AS/400 systems, in order to get the necessary response data we'll use later to construct the response XML document. Many years ago, I worked on a project that integrated a MAPICS system (RPG on the AS/400) with a new system that I wrote using Visual Basic. I had written code for both sides of the exchange, in RPG and CL on the AS/400, and Visual Basic on the PC. This led to several speaking engagements where I attempted to show legions of RPG programmers how to integrate their legacy systems with modern client/server software. At the time, it really was a complicated and almost mystical thing to do.
Since then, IBM has made it very easy and provided us with a library of Java functions that do all the work for us. (So much for all the consulting gigs and book deals I could have had with that one!) Here's the code, using the much better Java library from IBM:
Code View: Scroll / Show All
public boolean executeQuery() {
StringBuffer sb = new StringBuffer();
sb.append(requestValues.get("from_ContactName")).append("|");
sb.append(requestValues.get("from_EmailAddress")).append("|");
sb.append(requestValues.get("from_TelephoneNumber")).append("|");
sb.append(requestValues.get("from_BusinessIdentifier")).append("|");
sb.append(requestValues.get("prod_BeginAvailDate")).append("|");
sb.append(requestValues.get("prod_EndAvailDate")).append("|");
sb.append(requestValues.get("prod_Quantity")).append("|");
sb.append(requestValues.get("prod_ProductIdentifier")).append("|");
try {
AS400 sys = new AS400("SS100044", "ACME", "HOUSE123");
CharConverter ch = new CharConverter();
byte[] as = ch.stringToByteArray(sb.toString( ));
ProgramParameter[] parmList = new ProgramParameter[2];
parmList[0] = new ProgramParameter(as);
parmList[1] = new ProgramParameter(255);
ProgramCall pgm = new ProgramCall(sys,
"/QSYS.LIB/DEVOBJ.LIB/J551231.PGM", parmList);
if (pgm.run( ) != true) {
AS400Message[] msgList = pgm.getMessageList();
for (int i=0; i < msgList.length; i++) {
System.out.println(msgList[i].getID( ) + " : " +
msgList[i].getText());
}
}
else {
CharConverter chconv = new CharConverter();
String response =
chconv.byteArrayToString(parmList[1].getOutputData( ));
StringTokenizer st = new StringTokenizer(response, "|");
String status = (String) st.nextToken().trim(