JSONObject result = null;
JSONObject metadata;
IField field;
JSONArray fieldsMetaDataJSON;
JSONObject fieldMetaDataJSON;
IRecord record;
JSONObject recordJSON;
int recNo;
String valueField;
String displayField;
String descriptionField;
JSONArray recordsJSON;
int resultNumber;
Object propertyRawValue;
Assert.assertNotNull(dataStore, "Object to be serialized connot be null");
try {
IDataStoreMetaData dataStoreMeta = dataStore.getMetaData();
valueField = "Values";
displayField = "Values";
descriptionField = "Values";
result = new JSONObject();
metadata = new JSONObject();
metadata.put("root", ROOT);
metadata.put("totalProperty", TOTAL_PROPERTY);
metadata.put("valueField", valueField);
metadata.put("displayField", displayField);
metadata.put("descriptionField", descriptionField);
result.put("metaData", metadata);
propertyRawValue = dataStore.getMetaData().getProperty("resultNumber");
Assert.assertNotNull(propertyRawValue, "DataStore property [resultNumber] cannot be null");
Assert.assertTrue(propertyRawValue instanceof Integer, "DataStore property [resultNumber] must be of type [Integer]");
resultNumber = ((Integer)propertyRawValue).intValue();
Assert.assertTrue(resultNumber >= 0, "DataStore property [resultNumber] cannot be equal to [" + resultNumber + "]. It must be greater or equal to zero");
result.put(TOTAL_PROPERTY, resultNumber);
recordsJSON = new JSONArray();
result.put(ROOT, recordsJSON);
// field's meta
fieldsMetaDataJSON = new JSONArray();
fieldsMetaDataJSON.put("recNo"); // counting column
for(int i = 0; i < dataStore.getMetaData().getFieldCount(); i++) {
IFieldMetaData fieldMetaData = dataStore.getMetaData().getFieldMeta(i);
propertyRawValue = fieldMetaData.getProperty("visible");
if(propertyRawValue != null
&& (propertyRawValue instanceof Boolean)
&& ((Boolean)propertyRawValue).booleanValue() == false) {
continue;
}
fieldMetaDataJSON = new JSONObject();
fieldMetaDataJSON.put("header", "Values");
fieldMetaDataJSON.put("name", "Values" );
fieldMetaDataJSON.put("dataIndex", "Values");
Class clazz = fieldMetaData.getType();
logger.debug("Column [" + (i+1) + "] class is equal to [" + clazz.getName() + "]");
if( Number.class.isAssignableFrom(clazz) ) {
//BigInteger, Integer, Long, Short, Byte
if(Integer.class.isAssignableFrom(clazz)
|| BigInteger.class.isAssignableFrom(clazz)
|| Long.class.isAssignableFrom(clazz)
|| Short.class.isAssignableFrom(clazz)
|| Byte.class.isAssignableFrom(clazz)) {
logger.debug("Column [" + (i+1) + "] type is equal to [" + "INTEGER" + "]");
fieldMetaDataJSON.put("type", "int");
} else {
logger.debug("Column [" + (i+1) + "] type is equal to [" + "FLOAT" + "]");
fieldMetaDataJSON.put("type", "float");
}
} else if( String.class.isAssignableFrom(clazz) ) {
logger.debug("Column [" + (i+1) + "] type is equal to [" + "STRING" + "]");
fieldMetaDataJSON.put("type", "string");
} else if( Date.class.isAssignableFrom(clazz) ) {
logger.debug("Column [" + (i+1) + "] type is equal to [" + "DATE" + "]");
fieldMetaDataJSON.put("type", "date");
fieldMetaDataJSON.put("dateFormat", "d/m/Y H:i:s");
} else if( Boolean.class.isAssignableFrom(clazz) ) {
logger.debug("Column [" + (i+1) + "] type is equal to [" + "BOOLEAN" + "]");
fieldMetaDataJSON.put("type", "boolean");
} else {
logger.warn("Column [" + (i+1) + "] type is equal to [" + "???" + "]");
fieldMetaDataJSON.put("type", "string");
}
Boolean calculated = (Boolean)fieldMetaData.getProperty("calculated");
if(calculated.booleanValue() == true) {
DataSetVariable variable = (DataSetVariable)fieldMetaData.getProperty("variable");
if(variable.getType().equalsIgnoreCase(DataSetVariable.HTML)) {
fieldMetaDataJSON.put("type", "auto");
fieldMetaDataJSON.remove("type");
fieldMetaDataJSON.put("subtype", "html");
}
}
fieldsMetaDataJSON.put(fieldMetaDataJSON);
}
fieldsMetaDataJSON.put("recCk");
metadata.put("fields", fieldsMetaDataJSON);
// records
recNo = 0;
Iterator records = dataStore.iterator();
while(records.hasNext()) {
record = (IRecord)records.next();
recordJSON = new JSONObject();
for(int i = 0; i < dataStore.getMetaData().getFieldCount(); i++) {
IFieldMetaData fieldMetaData = dataStore.getMetaData().getFieldMeta(i);
propertyRawValue = fieldMetaData.getProperty("visible");
if(propertyRawValue != null
&& (propertyRawValue instanceof Boolean)
&& ((Boolean)propertyRawValue).booleanValue() == false) {
continue;
}
String key = fieldMetaData.getAlias() != null ? fieldMetaData.getAlias() : fieldMetaData.getName();
field = record.getFieldAt( dataStore.getMetaData().getFieldIndex( key ) );
String fieldValue = "";
if(field.getValue() != null && !field.getValue().equals("")) {
if(Date.class.isAssignableFrom(fieldMetaData.getType())) {
fieldValue = DATE_FORMATTER.format( field.getValue() );