CellDataSet execute(String queryName, @NotNull ICellSetFormatter formatter) {
String runId = "runId:" + ID_GENERATOR.getAndIncrement();
try {
//System.out.println("Execute: ID " + Thread.currentThread().getId() + " Name: " +
// Thread.currentThread().getName());
IQuery query = getIQuery(queryName);
OlapConnection con = olapDiscoverService.getNativeConnection(query.getSaikuCube().getConnection());
Long start = new Date().getTime();
if (query.getScenario() != null) {
LOG.info(runId + "\tQuery: " + query.getName() + " Setting scenario:" + query.getScenario().getId());
con.setScenario(query.getScenario());
}
if (query.getTag() != null) {
query = applyTag(query, con, query.getTag());
}
String mdx = query.getMdx();
LOG.info(runId + "\tType:" + query.getType() + ":\n" + mdx);
CellSet cellSet = query.execute();
Long exec = new Date().getTime();
if (query.getScenario() != null) {
LOG.info("Query (" + queryName + ") removing scenario:" + query.getScenario().getId());
con.setScenario(null);
}
CellDataSet result = OlapResultSetUtil.cellSet2Matrix(cellSet, formatter);
Long format = new Date().getTime();
result.setRuntime(new Double(format - start).intValue());
getIQuery(queryName).storeCellset(cellSet);
getIQuery(queryName).storeFormatter(formatter);
// we could do a check if query.getTotalFunctions() actually includes a total function and if not
// dont execute the following
if (QueryType.QM.equals(query.getType()) && formatter instanceof FlattenedCellSetFormatter) {
QueryDimension queryDimension = query.getDimension("Measures");
Measure[] selectedMeasures = new Measure[queryDimension.getInclusions().size()];
for (int i = 0; i < selectedMeasures.length; i++) {
selectedMeasures[i] = (Measure) queryDimension.getInclusions().get(i).getRootElement();
}
result.setSelectedMeasures(selectedMeasures);
int rowsIndex = 0;
if (!cellSet.getAxes().get(0).getAxisOrdinal().equals(Axis.ROWS)) {
rowsIndex = rowsIndex + 1 & 1;
}
// TODO - refactor this using axis ordinals etc.
//@formatter:off
final AxisInfo[] axisInfos = new AxisInfo[] { new AxisInfo(cellSet.getAxes().get(rowsIndex)),
new AxisInfo(cellSet.getAxes().get(rowsIndex + 1 & 1)) };
//@formatter:on
List<TotalNode>[][] totals = new List[2][];
TotalsListsBuilder builder = null;
for (int index = 0; index < 2; index++) {
final int second = index + 1 & 1;
TotalAggregator[] aggregators = new TotalAggregator[axisInfos[second].maxDepth + 1];
for (int i = 1; i < aggregators.length - 1; i++) {
String totalFunctionName = query.getTotalFunction(axisInfos[second].uniqueLevelNames.get(i - 1));
aggregators[i] = TotalAggregator.newInstanceByFunctionName(totalFunctionName);
}
String totalFunctionName = query.getTotalFunction(axisInfos[second].axis.getAxisOrdinal().name());
aggregators[0] =
totalFunctionName != null ? TotalAggregator.newInstanceByFunctionName(totalFunctionName) : null;
builder = new TotalsListsBuilder(selectedMeasures, aggregators, cellSet, axisInfos[index], axisInfos[second]);
totals[index] = builder.buildTotalsLists();
}