HavingField.Operand operand;
JSONObject havingJSON;
IModelField datamartFilter;
String fieldUniqueName;
Iterator it;
IModelProperties datamartLabels;
IModelField datamartField;
havings = query.getHavingFields();
Assert.assertNotNull(havings, "Filters cannot be null");
datamartLabels = null;
if(locale != null) {
//datamartLabels = QbeCacheManager.getInstance().getLabels( dataSource , locale );
datamartLabels = dataSource.getModelI18NProperties(locale);
}
it = havings.iterator();
while( it.hasNext() ) {
filter = (HavingField)it.next();
havingJSON = new JSONObject();
try {
havingJSON.put(QuerySerializationConstants.FILTER_ID, filter.getName());
havingJSON.put(QuerySerializationConstants.FILTER_DESCRIPTION, filter.getDescription());
havingJSON.put(QuerySerializationConstants.FILTER_PROMPTABLE, filter.isPromptable());
operand = filter.getLeftOperand();
havingJSON.put(QuerySerializationConstants.FILTER_LO_VALUE, operand.values[0]);
if(operand.type.equalsIgnoreCase("Field Content")) {
if(operand.values[0].contains("\"expression\":\"")){
havingJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, operand.description );
String description = operand.values[0].substring(operand.values[0].indexOf("\"expression\":\"")+14);
description.substring(0, description.indexOf("\""));
havingJSON.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;
havingJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, labelE + " : " + labelF );
String loLongDescription = getFieldLongDescription(datamartField, datamartLabels);
havingJSON.put(QuerySerializationConstants.FILTER_LO_LONG_DESCRIPTION, loLongDescription);
}
} else if(operand.type.equalsIgnoreCase("Subquery")) {
String loLongDescription = "Subquery " + operand.description;
havingJSON.put(QuerySerializationConstants.FILTER_LO_LONG_DESCRIPTION, loLongDescription);
havingJSON.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;
havingJSON.put(QuerySerializationConstants.FILTER_LO_LONG_DESCRIPTION, loLongDescription);
havingJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, operand.description);
} else {
havingJSON.put(QuerySerializationConstants.FILTER_LO_DESCRIPTION, operand.description);
}
havingJSON.put(QuerySerializationConstants.FILTER_LO_TYPE, operand.type);
havingJSON.put(QuerySerializationConstants.FILTER_LO_FUNCTION, operand.function.getName());
havingJSON.put(QuerySerializationConstants.FILTER_LO_DEFAULT_VALUE, operand.defaulttValues[0]);
havingJSON.put(QuerySerializationConstants.FILTER_LO_LAST_VALUE, operand.lastValues[0]);
havingJSON.put(QuerySerializationConstants.FILTER_OPERATOR, filter.getOperator());
operand = filter.getRightOperand();
havingJSON.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;
havingJSON.put(QuerySerializationConstants.FILTER_RO_DESCRIPTION, labelE + " : " + labelF );