Integer maxSize = null;
boolean isMaxResultsLimitBlocking = false;
IDataStore dataStore = null;
IDataSet dataSet = null;
JSONDataWriter serializer;
LookupStoreJSONSerializer serializer2;
JSONObject filtersJSON = null;
Query query = null;
IStatement statement = null;
Integer resultNumber = null;
JSONObject gridDataFeed = new JSONObject();
Monitor totalTimeMonitor = null;
Monitor errorHitsMonitor = null;
logger.debug("IN");
try {
super.service(request, response);
totalTimeMonitor = MonitorFactory.start("QbeEngine.GetValuesForQbeFilterLookup.totalTime");
entityId = getAttributeAsString( ENTITY_ID );
if(this.requestContainsAttribute( FILTERS ) ) {
filtersJSON = getAttributeAsJSONObject( FILTERS );
}
query = buildQuery(entityId, filtersJSON);
statement = getDataSource().createStatement( query );
statement.setParameters( getEnv() );
String jpaQueryStr = statement.getQueryString();
// String sqlQuery = statement.getSqlQueryString();
logger.debug("Executable query (HQL/JPQL): [" + jpaQueryStr+ "]");
// logger.debug("Executable query (SQL): [" + sqlQuery + "]");
start = getAttributeAsInteger( START );
limit = getAttributeAsInteger( LIMIT );
logger.debug("Parameter [" + ENTITY_ID + "] is equals to [" + entityId + "]");
logger.debug("Parameter [" + START + "] is equals to [" + start + "]");
logger.debug("Parameter [" + LIMIT + "] is equals to [" + limit + "]");
Assert.assertNotNull(entityId, "Parameter [" + ENTITY_ID + "] cannot be null" );
try {
logger.debug("Executing query ...");
dataSet = QbeDatasetFactory.createDataSet(statement);
dataSet.setAbortOnOverflow(true);
Map userAttributes = new HashMap();
UserProfile profile = (UserProfile)this.getEnv().get(EngineConstants.ENV_USER_PROFILE);
Iterator it = profile.getUserAttributeNames().iterator();
while(it.hasNext()) {
String attributeName = (String)it.next();
Object attributeValue = profile.getUserAttribute(attributeName);
userAttributes.put(attributeName, attributeValue);
}
dataSet.addBinding("attributes", userAttributes);
dataSet.addBinding("parameters", this.getEnv());
dataSet.loadData(start, limit, (maxSize == null? -1: maxSize.intValue()));
dataStore = dataSet.getDataStore();
Assert.assertNotNull(dataStore, "The dataStore returned by loadData method of the class [" + dataSet.getClass().getName()+ "] cannot be null");
} 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");
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);
//serializer = new DataStoreJSONSerializer();
//gridDataFeed = (JSONObject)serializer.serialize(dataStore);
serializer2 = new LookupStoreJSONSerializer();
gridDataFeed = (JSONObject)serializer2.serialize(dataStore);
try {
writeBackToClient( new JSONSuccess(gridDataFeed) );
} catch (IOException e) {
String message = "Impossible to write back the responce to the client";