*/
private JSONArray serializeFields(Query query, IDataSource dataSource, Locale locale) throws SerializationException {
JSONArray result;
List fields;
ISelectField field;
String fieldUniqueName;
IModelField datamartField;
JSONObject fieldJSON;
Iterator it;
IModelProperties datamartLabels;
String label, longDescription;
logger.debug("IN");
try {
datamartLabels = null;
if(locale != null) {
//datamartLabels = QbeCacheManager.getInstance().getLabels( dataSource , locale );
datamartLabels = dataSource.getModelI18NProperties(locale);
}
fields = query.getSelectFields(false);
Assert.assertNotNull(fields, "Fields cannot be null");
logger.debug("Query [" + query.getId() + "] have [" + fields.size() + "] field/s to serialize");
result = new JSONArray();
it = fields.iterator();
while( it.hasNext() ) {
field = (ISelectField)it.next();
logger.debug("Serializing filed [" + field.getAlias() + "]");
try {
fieldJSON = new JSONObject();
fieldJSON.put(QuerySerializationConstants.FIELD_ALIAS, field.getAlias());
fieldJSON.put(QuerySerializationConstants.FIELD_VISIBLE, field.isVisible());
fieldJSON.put(QuerySerializationConstants.FIELD_INCLUDE, field.isIncluded());
// field nature can me "measure" or "attribute"
String nature = null;
if (field.isDataMartField()) {
DataMartSelectField dataMartSelectField = (DataMartSelectField)field;
fieldJSON.put(QuerySerializationConstants.FIELD_TYPE, field.DATAMART_FIELD);
fieldUniqueName = dataMartSelectField.getUniqueName();
datamartField = dataSource.getModelStructure().getField( fieldUniqueName );
Assert.assertNotNull(datamartField, "A filed named [" + fieldUniqueName + "] does not exist in the datamart model");
fieldJSON.put(QuerySerializationConstants.FIELD_ID, datamartField.getUniqueName());
// localize entity name
label = null;
if(datamartLabels != null) {
label = datamartLabels.getProperty(datamartField.getParent(), "label");
}
label = StringUtilities.isEmpty(label)? datamartField.getParent().getName(): label;
fieldJSON.put(QuerySerializationConstants.FIELD_ENTITY, label);
// localize field name
label = null;
if(datamartLabels != null) {
label = datamartLabels.getProperty(datamartField, "label");
}
label = StringUtilities.isEmpty(label)? datamartField.getName(): label;
fieldJSON.put(QuerySerializationConstants.FIELD_NAME, label);
longDescription = getFieldLongDescription(datamartField, datamartLabels);
fieldJSON.put(QuerySerializationConstants.FIELD_LONG_DESCRIPTION, longDescription);
if( dataMartSelectField.isGroupByField() ) {
fieldJSON.put(QuerySerializationConstants.FIELD_GROUP, "true");
} else {
fieldJSON.put(QuerySerializationConstants.FIELD_GROUP, "");
}
fieldJSON.put(QuerySerializationConstants.FIELD_ORDER, dataMartSelectField.getOrderType());
fieldJSON.put(QuerySerializationConstants.FIELD_AGGREGATION_FUNCTION, dataMartSelectField.getFunction().getName());
//DatamartProperties datamartProperties = dataSource.getDataMartProperties();
String iconCls = datamartField.getPropertyAsString("type");
fieldJSON.put(QuerySerializationConstants.FIELD_ICON_CLS, iconCls);
// if an aggregation function is defined or if the field is declared as "measure" into property file,
// then it is a measure, elsewhere it is an attribute
if (
(dataMartSelectField.getFunction() != null
&& !dataMartSelectField.getFunction().equals(AggregationFunctions.NONE_FUNCTION))
|| iconCls.equals("measure")) {
nature = QuerySerializationConstants.FIELD_NATURE_MEASURE;
} else {
nature = QuerySerializationConstants.FIELD_NATURE_ATTRIBUTE;
}
} else if (field.isCalculatedField()){
CalculatedSelectField calculatedSelectField = (CalculatedSelectField)field;
fieldJSON.put(QuerySerializationConstants.FIELD_TYPE, field.CALCULATED_FIELD);
JSONObject fieldClaculationDescriptor = new JSONObject();
fieldClaculationDescriptor.put(QuerySerializationConstants.FIELD_TYPE, calculatedSelectField.getType());
fieldClaculationDescriptor.put(QuerySerializationConstants.FIELD_EXPRESSION, calculatedSelectField.getExpression());
fieldJSON.put(QuerySerializationConstants.FIELD_ID, fieldClaculationDescriptor);
fieldJSON.put(QuerySerializationConstants.FIELD_ICON_CLS, "calculation");
nature = QuerySerializationConstants.FIELD_NATURE_POST_LINE_CALCULATED;
} else if (field.isInLineCalculatedField()) {
InLineCalculatedSelectField calculatedSelectField = (InLineCalculatedSelectField)field;
fieldJSON.put(QuerySerializationConstants.FIELD_TYPE, field.IN_LINE_CALCULATED_FIELD);
JSONObject fieldClaculationDescriptor = new JSONObject();
fieldClaculationDescriptor.put(QuerySerializationConstants.FIELD_ALIAS, calculatedSelectField.getAlias());
fieldClaculationDescriptor.put(QuerySerializationConstants.FIELD_TYPE, calculatedSelectField.getType());
fieldClaculationDescriptor.put(QuerySerializationConstants.FIELD_EXPRESSION, calculatedSelectField.getExpression());
fieldJSON.put(QuerySerializationConstants.FIELD_ID, fieldClaculationDescriptor);
fieldJSON.put(QuerySerializationConstants.FIELD_LONG_DESCRIPTION, calculatedSelectField.getExpression());
if ( calculatedSelectField.isGroupByField() ) {
fieldJSON.put(QuerySerializationConstants.FIELD_GROUP, "true");
} else {
fieldJSON.put(QuerySerializationConstants.FIELD_GROUP, "");
}
fieldJSON.put(QuerySerializationConstants.FIELD_AGGREGATION_FUNCTION, calculatedSelectField.getFunction().getName());
fieldJSON.put(QuerySerializationConstants.FIELD_ORDER, calculatedSelectField.getOrderType());
//fieldJSON.put(SerializationConstants.FIELD_GROUP, "");
fieldJSON.put(QuerySerializationConstants.FIELD_ORDER, "");
//fieldJSON.put(SerializationConstants.FIELD_AGGREGATION_FUNCTION, "");
fieldJSON.put(QuerySerializationConstants.FIELD_ICON_CLS, "calculation");
/*
* We should understand if the calculated field is an attribute (i.e. a composition of attributes)
* or a measure (i.e. a composition of measures).
* The easiest way to understand this it to see if it is a grouping field.
* TODO manage queries without any aggregation and grouping.
* At the time being this information is used only in crosstab definition, and crosstab base query SHOULD
* make aggregation.
*/
if ( calculatedSelectField.isGroupByField() ) {
nature = QuerySerializationConstants.FIELD_NATURE_ATTRIBUTE;
} else {
nature = QuerySerializationConstants.FIELD_NATURE_MEASURE;
}
}
fieldJSON.put(QuerySerializationConstants.FIELD_NATURE, nature);
} catch(Throwable t) {
throw new SerializationException("An error occurred while serializing field: " + field.getAlias(), t);
}
logger.debug("Filed [" + field.getAlias() + "] serialized succesfully: [" + fieldJSON.toString() + "]");
result.put(fieldJSON);
}
}catch (Throwable t) {
throw new SerializationException("An error occurred while serializing select clause of query: " + query.getId(), t);