*/
private JSONArray serializeFilters(Query query, IDataSource dataSource, Locale locale) throws SerializationException {
JSONArray filtersJOSN = new JSONArray();
List filters;
WhereField filter;
WhereField.Operand operand;
JSONObject filterJSON;
IModelField datamartFilter;
String fieldUniqueName;
Iterator it;
IModelProperties datamartLabels;
IModelField datamartField;
filters = query.getWhereFields();
Assert.assertNotNull(filters, "Filters cannot be null");
datamartLabels = null;
if(locale != null) {
//datamartLabels = QbeCacheManager.getInstance().getLabels( dataSource , locale );
datamartLabels = dataSource.getModelI18NProperties(locale);
}
it = filters.iterator();
while( it.hasNext() ) {
filter = (WhereField)it.next();
filterJSON = new JSONObject();
try {
filterJSON.put(QuerySerializationConstants.FILTER_ID, filter.getName());
filterJSON.put(QuerySerializationConstants.FILTER_DESCRIPTION, filter.getDescription());
filterJSON.put(QuerySerializationConstants.FILTER_PROMPTABLE, filter.isPromptable());
operand = filter.getLeftOperand();
filterJSON.put(QuerySerializationConstants.FILTER_LO_VALUE, operand.values[0]);
if(operand.type.equalsIgnoreCase("Field Content")) {
if(operand.values[0].contains("\"expression\":\"")){
filterJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, operand.description );
String description = operand.values[0].substring(operand.values[0].indexOf("\"expression\":\"")+14);
description.substring(0, description.indexOf("\""));
filterJSON.put(QuerySerializationConstants.FILTER_LO_LONG_DESCRIPTION, description);
}else{
datamartField = dataSource.getModelStructure().getField( operand.values[0] );
String labelF, labelE;
labelE = null;
if(datamartLabels != null) {
labelE = datamartLabels.getProperty(datamartField.getParent(), "label");
}
labelE = StringUtilities.isEmpty(labelE)? datamartField.getParent().getName(): labelE;
labelF = null;
if(datamartLabels != null) {
labelF = datamartLabels.getProperty(datamartField, "label");
}
labelF = StringUtilities.isEmpty(labelF)? datamartField.getName(): labelF;
filterJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, labelE + " : " + labelF );
String loLongDescription = getFieldLongDescription(datamartField, datamartLabels);
filterJSON.put(QuerySerializationConstants.FILTER_LO_LONG_DESCRIPTION, loLongDescription);
}
} else if(operand.type.equalsIgnoreCase("Subquery")) {
String loLongDescription = "Subquery " + operand.description;
filterJSON.put(QuerySerializationConstants.FILTER_LO_LONG_DESCRIPTION, loLongDescription);
filterJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, operand.description);
} else if(operand.type.equalsIgnoreCase("Parent Field Content")) {
String[] chunks = operand.values[0].split(" ");
String parentQueryId = chunks[0];
String fieldName = chunks[1];
datamartField = dataSource.getModelStructure().getField( fieldName );
String datamartFieldLongDescription = getFieldLongDescription(datamartField, datamartLabels);
String loLongDescription = "Query " + parentQueryId + ", " + datamartFieldLongDescription;
filterJSON.put(QuerySerializationConstants.FILTER_LO_LONG_DESCRIPTION, loLongDescription);
filterJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, operand.description);
} else {
filterJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, operand.description);
}
filterJSON.put(QuerySerializationConstants.FILTER_LO_TYPE, operand.type);
filterJSON.put(QuerySerializationConstants.FILTER_LO_DEFAULT_VALUE, operand.defaulttValues[0]);
filterJSON.put(QuerySerializationConstants.FILTER_LO_LAST_VALUE, operand.lastValues[0]);
filterJSON.put(QuerySerializationConstants.FILTER_OPERATOR, filter.getOperator());
operand = filter.getRightOperand();
filterJSON.put(QuerySerializationConstants.FILTER_RO_VALUE, JSONUtils.asJSONArray(operand.values));
if(operand.type.equalsIgnoreCase("Field Content")) {
datamartField = dataSource.getModelStructure().getField( operand.values[0] );
String labelF, labelE;
labelE = null;
if(datamartLabels != null) {
labelE = datamartLabels.getProperty(datamartField.getParent(), "label");
}
labelE = StringUtilities.isEmpty(labelE)? datamartField.getParent().getName(): labelE;
labelF = null;
if(datamartLabels != null) {
labelF = datamartLabels.getProperty(datamartField, "label");
}
labelF = StringUtilities.isEmpty(labelF)? datamartField.getName(): labelF;
filterJSON.put(QuerySerializationConstants.FILTER_RO_DESCRIPTION, labelE + " : " + labelF );
String roLongDescription = getFieldLongDescription(datamartField, datamartLabels);
filterJSON.put(QuerySerializationConstants.FILTER_RO_LONG_DESCRIPTION, roLongDescription);
} else if(operand.type.equalsIgnoreCase("Subquery")) {
String roLongDescription = "Subquery " + operand.description;
filterJSON.put(QuerySerializationConstants.FILTER_RO_LONG_DESCRIPTION, roLongDescription);
filterJSON.put(QuerySerializationConstants.FILTER_RO_DESCRIPTION, operand.description);
} else if(operand.type.equalsIgnoreCase("Parent Field Content")) {
String[] chunks = operand.values[0].split(" ");
String parentQueryId = chunks[0];
String fieldName = chunks[1];
datamartField = dataSource.getModelStructure().getField( fieldName );
String datamartFieldLongDescription = getFieldLongDescription(datamartField, datamartLabels);
String loLongDescription = "Query " + parentQueryId + ", " + datamartFieldLongDescription;
filterJSON.put(QuerySerializationConstants.FILTER_RO_LONG_DESCRIPTION, loLongDescription);
filterJSON.put(QuerySerializationConstants.FILTER_RO_DESCRIPTION, operand.description);
} else {
filterJSON.put(QuerySerializationConstants.FILTER_RO_DESCRIPTION, operand.description);
}
filterJSON.put(QuerySerializationConstants.FILTER_RO_TYPE, operand.type);
filterJSON.put(QuerySerializationConstants.FILTER_RO_DEFAULT_VALUE, JSONUtils.asJSONArray(operand.defaulttValues));
filterJSON.put(QuerySerializationConstants.FILTER_RO_LAST_VALUE, JSONUtils.asJSONArray(operand.lastValues));
filterJSON.put(QuerySerializationConstants.FILTER_BOOLEAN_CONNETOR, filter.getBooleanConnector());
} catch(JSONException e) {
throw new SerializationException("An error occurred while serializing filter: " + filter.getName(), e);
}
filtersJOSN.put(filterJSON);
}
return filtersJOSN;