StringBuffer buffer;
List selectFields;
List allSelectFields;
List<InLineCalculatedSelectField> selectInLineCalculatedFields = new ArrayList<InLineCalculatedSelectField>();
AbstractSelectField selectAbstractField;
DataMartSelectField selectField;
InLineCalculatedSelectField selectInLineField;
IModelEntity rootEntity;
IModelField datamartField;
String queryName;
String rootEntityAlias;
String selectClauseElement; // rootEntityAlias.queryName
Map entityAliases;
logger.debug("IN");
buffer = new StringBuffer();
try {
selectFields = query.getSelectFields(true);
if(selectFields == null ||selectFields.size() == 0) {
return "";
}
entityAliases = (Map)entityAliasesMaps.get(query.getId());
buffer.append(SELECT);
if (query.isDistinctClauseEnabled()) {
buffer.append(" " + DISTINCT);
}
Iterator it = selectFields.iterator();
if(it.hasNext()){
selectAbstractField = (AbstractSelectField)it.next();
String[] idsForQuery = new String[selectFields.size()-query.getCalculatedSelectFields(true).size()];
int index=0;
do{
if(selectAbstractField.isDataMartField()){
selectField = (DataMartSelectField)selectAbstractField;
logger.debug("select field unique name [" + selectField.getUniqueName() + "]");
datamartField = getDataSource().getModelStructure().getField(selectField.getUniqueName());
Couple queryNameAndRoot = datamartField.getQueryName();
queryName = (String) queryNameAndRoot.getFirst();
logger.debug("select field query name [" + queryName + "]");
if(queryNameAndRoot.getSecond()!=null){
rootEntity = (IModelEntity)queryNameAndRoot.getSecond();
}else{
rootEntity = datamartField.getParent().getRoot();
}
logger.debug("select field root entity unique name [" + rootEntity.getUniqueName() + "]");
rootEntityAlias = (String)entityAliases.get(rootEntity.getUniqueName());
if(rootEntityAlias == null) {
rootEntityAlias = getNextAlias(entityAliasesMaps);
entityAliases.put(rootEntity.getUniqueName(), rootEntityAlias);
}
logger.debug("select field root entity alias [" + rootEntityAlias + "]");
selectClauseElement = rootEntityAlias + "." + queryName.substring(0,1).toLowerCase()+queryName.substring(1);
//selectClauseElement = rootEntityAlias + "." + queryName;
logger.debug("select clause element before aggregation [" + selectClauseElement + "]");
selectClauseElement = selectField.getFunction().apply(selectClauseElement);
logger.debug("select clause element after aggregation [" + selectClauseElement + "]");
idsForQuery[index] = " " + selectClauseElement;
index++;