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 extends ResponseTemplate {
/**
* 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
* @param cmd API command used within this request
*/
@SuppressWarnings("unchecked") // not really a good way ...
public Response(String raw, Map cmd) {
super(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);
}
}
}
/**
* 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 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