super();
}
public IDataStore read( Object data ) {
DataStore dataStore;
DataStoreMetaData dataStoreMeta;
JSONObject parsedData;
logger.debug("IN");
dataStore = null;
parsedData = null;
dataStore = new DataStore();
dataStoreMeta = new DataStoreMetaData();
dataStore.setMetaData(dataStoreMeta);
try {
if (!(data instanceof JSONObject)) {
parsedData = new JSONObject( (String)data );
}
else{
parsedData = (JSONObject)data;
}
JSONObject parsedMeta = parsedData.optJSONObject("metaData");
// init meta
if(parsedMeta == null) {
throw new RuntimeException("Malformed data. Impossible to find attribute [" + "metaData" + "]");
}
String root = parsedMeta.optString("root");
if(root == null) {
throw new RuntimeException("Malformed meta data. Impossible to find attribute [" + "root" + "]");
}
JSONArray parsedFieldsMeta = parsedMeta.optJSONArray("fields");
if(parsedFieldsMeta == null) {
throw new RuntimeException("Malformed meta data. Impossible to find attribute [" + "fields" + "]");
}
for(int i = 1; i < parsedFieldsMeta.length(); i++) { // skip "recNo"
JSONObject parsedFieldMeta = parsedFieldsMeta.getJSONObject(i);
String columnName = parsedFieldMeta.getString("dataIndex");
String columnHeader = parsedFieldMeta.getString("header");
String columnType = parsedFieldMeta.getString("type");
FieldMetadata fieldMeta = new FieldMetadata();
fieldMeta.setName( columnName );
fieldMeta.setAlias(columnHeader);
if(columnType.equalsIgnoreCase("string")) {
fieldMeta.setType( String.class );
}else if(columnType.equalsIgnoreCase("int")) {
fieldMeta.setType( BigInteger.class );
} else if(columnType.equalsIgnoreCase("float")) {
fieldMeta.setType( Double.class );
} else if(columnType.equalsIgnoreCase("date")) {
String subtype = parsedFieldMeta.optString("subtype");
if(subtype != null && subtype.equalsIgnoreCase("timestamp")) {
fieldMeta.setType( Timestamp.class );
} else {
fieldMeta.setType( Date.class );
}
} else if(columnType.equalsIgnoreCase("boolean")) {
fieldMeta.setType( Boolean.class );
} else {
throw new RuntimeException("Impossible to resolve column type [" + columnType + "]");
}
dataStoreMeta.addFiedMeta(fieldMeta);
}
// init results
JSONArray parsedRows = parsedData.optJSONArray(root);
if(parsedRows == null) {
throw new RuntimeException("Malformed data. Impossible to find attribute [" + root + "]");
}
int rowNumber = parsedRows.length();
for (int i = 0; i < rowNumber; i++) {
JSONObject parsedRow = parsedRows.getJSONObject(i);
IRecord record = new Record(dataStore);
for(int j = 0; j < dataStoreMeta.getFieldCount(); j++) {
String columnName = dataStoreMeta.getFieldName(j);
String columnValue = parsedRow.getString(columnName);
IFieldMetaData fieldMeta = dataStoreMeta.getFieldMeta(j);
Class fieldType = fieldMeta.getType();
Object value = null;
if(fieldType == String.class) {