package org.ch3ck3r.jgbx.requests;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import org.ch3ck3r.jgbx.error.JGBXException;
import org.ch3ck3r.jgbx.internal.TypeToXMLConverter;
import org.ch3ck3r.jgbx.responses.Response;
/**
* Abstract base class for all Requests.
*
* @author Frank Zechert
* @version 1
*/
public abstract class Request {
/**
* The class name part to replace to get the response class.
*/
private static final CharSequence CLASS_NAME_REQUEST = "Request";
/**
* The class name part to insert to get the response class.
*/
private static final CharSequence CLASS_NAME_RESPONSE = "Response";
/**
* The package name part to replace to get the response package.
*/
private static final CharSequence PACKAGE_NAME_REQUEST = "requests";
/**
* The package name part to insert to get the response package.
*/
private static final CharSequence PACKAGE_NAME_RESPONSE = "responses";
/**
* The method to call.
*/
protected String method;
/**
* The parameters to pass.
*/
protected List<Object> parameters;
/**
* The class of the response.
*/
private final Class<? extends Response> clazz;
/**
* Create a new request.
*
* @param procedureName
* The procedure to call.
* @throws JGBXException
* There is an error
*/
@SuppressWarnings("unchecked")
public Request(final String procedureName) throws JGBXException {
this.method = procedureName;
this.parameters = new ArrayList<>(5);
final String classname = this.getClass().getName().replace(CLASS_NAME_REQUEST, CLASS_NAME_RESPONSE)
.replace(PACKAGE_NAME_REQUEST, PACKAGE_NAME_RESPONSE);
try {
this.clazz = (Class<? extends Response>) Class.forName(classname);
}
catch (final ClassNotFoundException e) {
throw new JGBXException("Cannot find the given response class: " + classname, e);
}
}
/**
* Add a boolean parameter.
*
* @param booleanParameter
* The parameter.
*/
public void addParameter(final Boolean booleanParameter) {
this.parameters.add(booleanParameter);
}
/**
* Add an integer parameter.
*
* @param integerParameter
* The parameter.
*/
public void addParameter(final Integer integerParameter) {
this.parameters.add(integerParameter);
}
/**
* Add a double parameter.
*
* @param doubleParameter
* The parameter.
*/
public void addParameter(final Double doubleParameter) {
this.parameters.add(doubleParameter);
}
/**
* Add a float parameter.
*
* @param floatParameter
* The parameter.
*/
public void addParameter(final Float floatParameter) {
this.parameters.add(floatParameter);
}
/**
* Add a date parameter.
*
* @param dateParameter
* The parameter.
*/
public void addParameter(final Calendar dateParameter) {
this.parameters.add(dateParameter);
}
/**
* Add a base64 parameter.
*
* @param base64Parameter
* The parameter.
*/
public void addParameter(final Byte[] base64Parameter) {
this.parameters.add(base64Parameter);
}
/**
* Add a string parameter.
*
* @param stringParameter
* The parameter.
*/
public void addParameter(final String stringParameter) {
this.parameters.add(stringParameter);
}
/**
* Add an array parameter.
*
* @param arrayParameter
* The parameter.
*/
public void addParameter(final List<? extends Object> arrayParameter) {
this.parameters.add(arrayParameter);
}
/**
* Add a struct parameterr.
*
* @param structParameter
* The parameter.
*/
public void addParameter(final Map<String, ? extends Object> structParameter) {
this.parameters.add(structParameter);
}
public void addParameter(final Object commands) {
// TODO Auto-generated method stub
}
/**
* Generate the XML String that represents this call.
*
* @return The XML.
*/
public String toXML() {
final StringBuilder xml = new StringBuilder();
xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<methodCall>");
xml.append("<methodName>").append(this.method).append("</methodName>");
if (this.parameters.size() > 0) {
xml.append("<params>");
for (final Object parameter : this.parameters) {
xml.append("<param>");
xml.append("<value>").append(TypeToXMLConverter.getXML(parameter)).append("</value>");
xml.append("</param>");
}
xml.append("</params>");
}
xml.append("</methodCall>");
return xml.toString();
}
/**
* Return the class of the response class.
*
* @return the response class.
*/
public Class<? extends Response> getResponseClass() {
return this.clazz;
}
/**
* Get the parameter at position pos of type T
*
* @param pos
* The position to get.
* @return The parameter.
*/
@SuppressWarnings("unchecked")
public <T> T getParameter(final int pos) {
return (T) this.parameters.get(pos);
}
}