Online Book Reader

Home Category

Beautiful Code [306]

By Root 5289 0
);

as400response.put("Status", status);

String error = (String) st.nextToken().trim( );

as400response.put("ErrorCode", error);

String quantity = (String) st.nextToken().trim( );

as400response.put("Quantity",

String.valueOf(Integer.parseInt(quantity)));

if (status.toUpperCase( ).equals("ER")) {

if (error.equals("1")) {

as400response.put("ErrorMsg",

"Account not authorized for item availability.");

}

if (error.equals("2")) {

as400response.put("ErrorMsg", "Item not found.");

}

if (error.equals("3")) {

as400response.put("ErrorMsg", "Item is obsolete.");

as400response.put("Replacement",

(String) st.nextToken().trim( ));

}

if (error.equals("4")) {

as400response.put("ErrorMsg",

"Invalid quantity amount.");

}

if (error.equals("5")) {

as400response.put("ErrorMsg",

"Preference profile processing error.");

}

if (error.equals("6")) {

as400response.put("ErrorMsg",

"ATP processing error.");

}

}

}

}

catch (Exception e) {

System.out.println(e.toString( ));

}

return true;

}

This method begins by assembling a parameter string (pipe-delimited) that gets passed into the AS/400 program, where it parses the string, retrieves the requested data, and returns a pipe-delimited string with a status and error code as well as the result of the operation.

Assuming there isn't an error, the results of this AS/400 interaction get stored in another HashMap, which we'll use when constructing the XML response document. If there is an error, then that gets written to the response instead.

27.4.2. Assembling the XML Response

I've always enjoyed seeing the many ways people have tried to create XML documents programmatically. What I always tell people is that XML documents are just big text strings. Therefore, it's usually easier to just write one out using a StringBuffer rather than trying to build a DOM (Document Object Model) or using a special XML generator library.

For this project, I simply created a StringBuffer object and appended each individual line of the XML document following the Rosettanet standard. In the following code example, I omitted several lines of code, but this should give you an idea of how the response was constructed:

Code View: Scroll / Show All

public String getResponseXML() {

StringBuffer response = new StringBuffer();

response.append("").append("\n");

response.append(" ").append("\n");

response.append(" ").append(as400response.get("Quantity")).

append("").append("\n");

response.append(" ").append("\n");

response.append(" ").append("\n");

response.append(" ").append("\n");

response.append(" Manufacturer GlobalPartnerClassificationCode>").append("\n");

response.append(" ").append(requestValues.

get("prod_ProductIdentifier")).append("").append("\n");

response.append(" ").append("\n");

response.append(" ").append("\n");

response.append(" ").append("\n");

response.append("").append("\n");

return response.toString();

}

Integrating Business Partners the RESTful Way > Conclusion

27.5. Conclusion

In looking back on this code I wrote over two years ago, I think it's pretty normal to second-guess myself and think of better ways I could have written it. While I may have written some of the implementation code differently, I think I'd still design the overall solution the same way. This code has stood the test of time, as the client has since added new distributors and new request types all on its own, with minimal help from outside service providers like me.

Currently, as director of a bioinformatics department, I have used this code to demonstrate several things to

Return Main Page Previous Page Next Page

®Online Book Reader