String jpaQueryStr = statement.getQueryString();
String sqlQuery = statement.getSqlQueryString();
logger.debug("Executable query (HQL/JPQL): [" + jpaQueryStr+ "]");
//logger.debug("Executable query (SQL): [" + sqlQuery + "]");
UserProfile userProfile = (UserProfile)getEnv().get(EngineConstants.ENV_USER_PROFILE);
//auditlogger.info("[" + userProfile.getUserId() + "]:: HQL: " + hqlQuery);
//auditlogger.info("[" + userProfile.getUserId() + "]:: SQL: " + sqlQuery);
// STEP 3: transform the sql query
FilterQueryTransformer transformer = new FilterQueryTransformer();
List selectFields = SqlUtils.getSelectFields(sqlQuery);
List queryFields = query.getDataMartSelectFields(true);
for(int i = 0; i < queryFields.size(); i++) {
ISelectField queryField = (ISelectField)queryFields.get(i);
String[] f = (String[])selectFields.get(i);
transformer.addColumn(f[1]!=null? f[1]:f[0], f[1]!=null? f[1]:f[0]);
}
for(int i = 0; i < filters.length(); i++) {
JSONObject filter = filters.getJSONObject(i);
String columnName = filter.getString("columnName");
String value = filter.getString("value");
int fieldIndex = query.getSelectFieldIndex(columnName);
String[] f = (String[])selectFields.get(fieldIndex);
transformer.addFilter(f[1]!=null? f[1]:f[0], value);
}
sqlQuery = (String)transformer.transformQuery(sqlQuery);
// put the query into session
this.setAttributeInSession(LAST_DETAIL_QUERY, sqlQuery);
// STEP 4: execute the query
try {
logger.debug("Executing query: [" + sqlQuery + "]");
auditlogger.info("[" + userProfile.getUserId() + "]:: SQL: " + sqlQuery);
dataSet = new JDBCDataSet();
//Session session = getDatamartModel().getDataSource().getSessionFactory().openSession();
ConnectionDescriptor connection = (ConnectionDescriptor)getDataSource().getConfiguration().loadDataSourceProperties().get("connection");
DataSource dataSource = new DataSource();
dataSource.setJndi(connection.getJndiName());
dataSource.setHibDialectName(connection.getDialect());
dataSource.setUrlConnection(connection.getUrl());
dataSource.setDriver(connection.getDriverClass());
dataSource.setUser(connection.getUsername());
dataSource.setPwd(connection.getPassword());
dataSet.setDataSource(dataSource);
dataSet.setQuery(sqlQuery);
dataSet.loadData(start, limit, -1);
dataStore = dataSet.getDataStore();
IDataStoreMetaData dataStoreMetadata = dataStore.getMetaData();
for(int i = 0; i < dataStoreMetadata.getFieldCount(); i++) {
ISelectField queryField = (ISelectField)queryFields.get(i);
dataStoreMetadata.changeFieldAlias(i, queryField.getAlias());
}
} catch (Exception e) {
logger.debug("Query execution aborted because of an internal exceptian");
SpagoBIEngineServiceException exception;
String message;
message = "An error occurred in " + getActionName() + " service while executing query: [" + statement.getQueryString() + "]";
exception = new SpagoBIEngineServiceException(getActionName(), message, e);
exception.addHint("Check if the query is properly formed: [" + statement.getQueryString() + "]");
exception.addHint("Check connection configuration");
exception.addHint("Check the qbe jar file");
throw exception;
}
logger.debug("Query executed succesfully");
//dataStore.getMetaData().setProperty("resultNumber", new Integer( (int)dataStore.getRecordsCount() ));
resultNumber = (Integer)dataStore.getMetaData().getProperty("resultNumber");
Assert.assertNotNull(resultNumber, "property [resultNumber] of the dataStore returned by loadData method of the class [" + dataSet.getClass().getName()+ "] cannot be null");
logger.debug("Total records: " + resultNumber);
boolean overflow = maxSize != null && resultNumber >= maxSize;
if (overflow) {
logger.warn("Query results number [" + resultNumber + "] exceeds max result limit that is [" + maxSize + "]");
auditlogger.info("[" + userProfile.getUserId() + "]:: max result limit [" + maxSize + "] exceeded with SQL: " + sqlQuery);
}
dataSetWriter = new JSONDataWriter();
gridDataFeed = (JSONObject)dataSetWriter.write(dataStore);