Skip to content

Instantly share code, notes, and snippets.

@chrahunt
Created October 4, 2012 20:17
Show Gist options
  • Save chrahunt/3836148 to your computer and use it in GitHub Desktop.
Save chrahunt/3836148 to your computer and use it in GitHub Desktop.
RequestInterface handles the basic communication with the web service class, Request has all the individual actions
public class Connect extends ConnectInterface {
...
/*
* Function parameters come in a few flavors: many arguments with all-optional,
* 1 or a few arguments with all-optional, variable number of arguments with
* some required and some optional, set number of arguments with all required,
* and arguments that can pertain to multiple records (requiring us to delineate which
* of the arguments for a parameter of the same name pertains to which record).
*
* Information on how each type is handled can be found above the first occurrence
* of that type below.
*/
// This is a function with many all-optional arguments
public ConnectReturn getOrders(String args) {
return new ConnectReturn(Request("getOrders", args));
}
// Functions with all-optional arguments get overloaded like below
public ConnectReturn getOrders() {
return getOrders(null);
}
// This function takes no arguments
public ConnectReturn getRaces() {
return new ConnectReturn(Request("getRaces"));
}
// This function has a single, required argument
public ConnectReturn getEducationHistory(String tempId) {
return new ConnectReturn(Request("getEducationHistory", "tempId:" + tempId));
}
// This is an example of a function whose arguments need to be formatted differently (under
// an additional parent and child element). Have not yet implemented multiple-record argument capability
public ConnectReturn insertEducationHistory(String tempId,
String institution, String courseOfStudy, String graduationDate,
String degree, String args) {
Hashtable<Integer, String> properties = new Hashtable<Integer, String>();
properties.put(ConnectInterface.PARENT_ELEMENT, "educationHistoryRecords");
properties.put(ConnectInterface.CHILD_ELEMENT, "educationHistoryRecord");
String arguments = String.format("tempId:%s;institution:%s;courseOfStudy:%s;graduationDate:%s;degree:%s;",
tempId, institution, courseOfStudy, graduationDate, degree);
if (args != null) arguments += args;
return new ConnectReturn(Request("insertEducationHistory", arguments, properties));
}
// This is a function with a single required argument and multiple optional arguments
public ConnectReturn getStateLicenses(String tempId, String args) {
String arguments = String.format("tempId:%s", tempId);
if (args != null) arguments += args;
return new ConnectReturn(Request("getStateLicenses", arguments));
}
// Functions like the one above get overloaded as seen below
public ConnectReturn getStateLicenses(String tempId) {
return getStateLicenses(tempId, null);
}
}
public abstract class ConnectInterface {
...
// Provide an interface for the Request method
final protected String Request(String action, String args, Hashtable<Integer, String> properties) {
String data = RequestBuilder(action, args, properties);
return service.Request(data);
}
final protected String Request(String action, String args) {
return Request(action, args, null);
}
final protected String Request(String action) {
return Request(action, null, null);
}
private String RequestBuilder(String action, String args, Hashtable<Integer, String> properties) {
String data = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element rootElement = doc.createElement("Request");
doc.appendChild(rootElement);
Element sessionKeyElement = doc.createElement("sessionKey");
sessionKeyElement.appendChild(doc.createTextNode(getSessionKey()));
rootElement.appendChild(sessionKeyElement);
Element actionElement = doc.createElement("action");
actionElement.appendChild(doc.createTextNode(action));
rootElement.appendChild(actionElement);
Element resultTypeElement = doc.createElement("resultType");
resultTypeElement.appendChild(doc.createTextNode(resultType));
rootElement.appendChild(resultTypeElement);
OutputFormat format = new OutputFormat(doc);
format.setOmitXMLDeclaration(true);
StringWriter writer = new StringWriter();
XMLSerializer serializer = new XMLSerializer(writer, format);
serializer.serialize(doc);
data = writer.toString();
if (args != null) {
args = ArgumentParse(args, properties);
data = data.replaceFirst("</Request>", args + "</Request>");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
private String ArgumentParse(String args, Hashtable<Integer, String> properties) {
String[] arguments = args.split(";");
String returned = "";
for (int i = 0; i < arguments.length; i++) {
String arg = arguments[i].split(":")[0];
String value = arguments[i].split(":")[1];
returned += "<" + arg + ">" + value + "</" + arg + ">";
}
if (properties != null) {
String startTag = "<" + properties.get(ConnectInterface.PARENT_ELEMENT) + "><"
+ properties.get(ConnectInterface.CHILD_ELEMENT) + ">";
String endTag = "</" + properties.get(ConnectInterface.CHILD_ELEMENT) + "></"
+ properties.get(ConnectInterface.PARENT_ELEMENT);
returned = startTag + returned + endTag;
}
return returned;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment