net.hexonet.apiconnector.Response Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk Show documentation
Show all versions of java-sdk Show documentation
A connector library for the insanely fast HEXONET backend API.
package net.hexonet.apiconnector;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Response covers all functionality to wrap a Backend API Response like accessing data
*
* @author Kai Schwarz
* @version %I%, %G%
* @since 2.0
*/
public class Response {
/** backend system plain response data */
private String raw;
/** backend system parsed response data (hash format) */
private Map hash;
/**
* The API Command used within this request
*/
private Map command;
/**
* Column names available in this responsse NOTE: this includes also FIRST, LAST, LIMIT, COUNT,
* TOTAL and maybe further specific columns in case of a list query
*/
private ArrayList columnkeys;
/**
* Container of Column Instances
*/
private ArrayList columns;
/**
* Record Index we currently point to in record list
*/
private int recordIndex;
/**
* Record List (List of rows)
*/
private ArrayList records;
/**
* Constructor
*
* @param raw API plain response
*/
public Response(String raw) {
this(raw, Map.ofEntries(), Map.ofEntries());
}
/**
* Constructor
*
* @param raw API plain response
* @param cmd API command used within this request
*/
public Response(String raw, Map cmd) {
this(raw, cmd, Map.ofEntries());
}
/**
* Constructor
*
* @param raw API plain response
* @param cmd API command used within this request
* @param ph place holder variables replacements
*/
@SuppressWarnings("unchecked") // not really a good way ...
public Response(String raw, Map cmd, Map ph) {
// secure password for output
if (cmd.containsKey("PASSWORD")) {
cmd.replace("PASSWORD", "***");
}
this.raw = ResponseTranslator.translate(raw, cmd, ph);
this.hash = ResponseParser.parse(this.raw);
this.command = new HashMap(cmd);
this.columnkeys = new ArrayList();
this.columns = new ArrayList();
this.recordIndex = 0;
this.records = new ArrayList();
Object property = this.hash.get("PROPERTY");
if (property != null) {
Map> p = (HashMap>) property;
Iterator>> it = p.entrySet().iterator();
int count = 0;
while (it.hasNext()) {
Map.Entry> pair = it.next();
this.addColumn(pair.getKey(), pair.getValue());
int len = pair.getValue().size();
if (len > count) {
count = len;
}
}
for (int i = 0; i < count; i++) {
Map d = new HashMap();
for (String key : this.columnkeys) {
Column col = this.getColumn(key);
if (col != null) {
String v = col.getDataByIndex(i);
if (v != null) {
d.put(key, v);
}
}
}
this.addRecord(d);
}
}
}
/**
* Get API response code
*
* @return API response code
*/
public int getCode() {
return Integer.parseInt((String) this.hash.get("CODE"));
}
/**
* Get API response description
*
* @return API response description
*/
public String getDescription() {
return (String) this.hash.get("DESCRIPTION");
}
/**
* Get Plain API response
*
* @return Plain API response
*/
public String getPlain() {
return this.raw;
}
/**
* Get Queuetime of API response
*
* @return Queuetime of API response
*/
public double getQueuetime() {
String rt = (String) this.hash.get("QUEUETIME");
if (rt != null) {
return Double.parseDouble((String) this.hash.get("QUEUETIME"));
}
return 0.00;
}
/**
* Get API response as Hash
*
* @return API response hash
*/
public Map getHash() {
return this.hash;
}
/**
* Get Runtime of API response
*
* @return Runtime of API response
*/
public double getRuntime() {
String rt = (String) this.hash.get("RUNTIME");
if (rt != null) {
return Double.parseDouble((String) this.hash.get("RUNTIME"));
}
return 0.00;
}
/**
* Check if current API response represents an error case API response code is an 5xx code
*
* @return boolean result
*/
public boolean isError() {
String code = (String) this.hash.get("CODE");
return code.charAt(0) == '5';
}
/**
* Check if current API response represents a success case API response code is an 2xx code
*
* @return boolean result
*/
public boolean isSuccess() {
String code = (String) this.hash.get("CODE");
return code.charAt(0) == '2';
}
/**
* Check if current API response represents a temporary error case API response code is an 4xx
* code
*
* @return boolean result
*/
public boolean isTmpError() {
String code = (String) this.hash.get("CODE");
return code.charAt(0) == '4';
}
/**
* Check if current operation is returned as pending
*
* @return boolean result
*/
public boolean isPending() {
String pending = (String) this.hash.get("PENDING");
if (pending != null) {
return pending.equals("1");
}
return false;
}
/**
* Add a column to the column list
*
* @param key column name
* @param data array of column data
* @return Current Response Instance for method chaining
*/
public Response addColumn(String key, ArrayList data) {
Column col = new Column(key, data);
this.columns.add(col);
this.columnkeys.add(key);
return this;
}
/**
* Add a record to the record list
*
* @param h row data
* @return Current Response Instance for method chaining
*/
public Response addRecord(Map h) {
this.records.add(new Record(h));
return this;
}
/**
* Get column by column name
*
* @param key column name
* @return column instance or null if column does not exist
*/
public Column getColumn(String key) {
if (this.hasColumn(key)) {
return this.columns.get(this.columnkeys.indexOf(key));
}
return null;
}
/**
* Get Data by Column Name and Index
*
* @param colkey column name
* @param index column data index
* @return column data at index or null if not found
*/
public String getColumnIndex(String colkey, int index) {
Column col = this.getColumn(colkey);
if (col != null) {
return col.getDataByIndex(index);
}
return null;
}
/**
* Get Column Names
*
* @return Array of Column Names
*/
public ArrayList getColumnKeys() {
return this.columnkeys;
}
/**
* Get List of Columns
*
* @return Array of Columns
*/
public ArrayList getColumns() {
return this.columns;
}
/**
* Get Command used in this request
*
* @return command
*/
public Map getCommand() {
return this.command;
}
/**
* Get Command used in this request in plain text
*
* @return plain text command
*/
public String getCommandPlain() {
StringBuilder tmp = new StringBuilder("");
Iterator> it = this.command.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = it.next();
tmp.append(pair.getKey());
tmp.append(" = ");
tmp.append(pair.getValue());
tmp.append("\n");
}
return tmp.toString();
}
/**
* Get Page Number of current List Query
*
* @return page number or -1 in case of a non-list response
*/
public int getCurrentPageNumber() {
int first = this.getFirstRecordIndex();
int limit = this.getRecordsLimitation();
if ((first != -1) && (limit > 0)) {
return (int) Math.floor((double) first / (double) limit) + 1;
}
return -1;
}
/**
* Get Record of current record index
*
* @return Record or null in case of a non-list response
*/
public Record getCurrentRecord() {
if (this.hasCurrentRecord()) {
return this.records.get(this.recordIndex);
}
return null;
}
/**
* Get Index of first row in this response
*
* @return first row index; -1 if record list is empty
*/
public int getFirstRecordIndex() {
Column col = this.getColumn("FIRST");
if (col != null) {
String f = col.getDataByIndex(0);
if (f != null) {
return Integer.parseInt(f);
}
}
if (this.records.size() > 0) {
return 0;
}
return -1;
}
/**
* Get last record index of the current list query
*
* @return record index or -1 for a non-list response
*/
public int getLastRecordIndex() {
Column col = this.getColumn("LAST");
if (col != null) {
String l = col.getDataByIndex(0);
if (l != null) {
return Integer.parseInt(l);
}
}
int len = this.getRecordsCount();
if (len > 0) {
return (len - 1);
}
return -1;
}
/**
* Get Response as List Hash including useful meta data for tables
*
* @return hash including list meta data and array of rows in hash notation
*/
public Map getListHash() {
ArrayList