// TODO Remove redundant code
private DataTable buildDataTableFromSQL(String natSQL, String headerFromSQL) {
Session session = Helper.getHibernateSession();
// creating header row from headerSQL (gets all columns in one row
DataRow headerRow = null;
if (headerFromSQL != null) {
headerRow = new DataRow(null);
SQLQuery headerQuery = session.createSQLQuery(headerFromSQL);
// needs to be there otherwise an exception is thrown
headerQuery.addScalar("stepCount", StandardBasicTypes.DOUBLE);
headerQuery.addScalar("stepName", StandardBasicTypes.STRING);
headerQuery.addScalar("stepOrder", StandardBasicTypes.DOUBLE);
headerQuery.addScalar("intervall", StandardBasicTypes.STRING);
@SuppressWarnings("rawtypes")
List headerList = headerQuery.list();
for (Object obj : headerList) {
Object[] objArr = (Object[]) obj;
try {
headerRow.setName(new Converter(objArr[3]).getString() + "");
headerRow.addValue(new Converter(new Converter(objArr[2]).getInteger()).getString() + " (" + new Converter(objArr[1]).getString()
+ ")", (new Converter(objArr[0]).getDouble()));
} catch (Exception e) {
headerRow.addValue(e.getMessage(), new Double(0));
}
}
}
SQLQuery query = session.createSQLQuery(natSQL);
// needs to be there otherwise an exception is thrown
query.addScalar("stepCount", StandardBasicTypes.DOUBLE);
query.addScalar("stepName", StandardBasicTypes.STRING);
query.addScalar("stepOrder", StandardBasicTypes.DOUBLE);
query.addScalar("intervall", StandardBasicTypes.STRING);
@SuppressWarnings("rawtypes")
List list = query.list();
DataTable dtbl = new DataTable("");
// if headerRow is set then add it to the DataTable to set columns
// needs to be removed later
if (headerRow != null) {
dtbl.addDataRow(headerRow);
}
DataRow dataRow = null;
// each data row comes out as an Array of Objects
// the only way to extract the data is by knowing
// in which order they come out
// checks if intervall has changed which then triggers the start for a
// new row
// intervall here is the timeGroup Expression (e.g. "2006/05" or
// "2006-10-05")
String observeIntervall = "";
for (Object obj : list) {
Object[] objArr = (Object[]) obj;
try {
// objArr[3]
if (!observeIntervall.equals(new Converter(objArr[3]).getString())) {
observeIntervall = new Converter(objArr[3]).getString();
// row cannot be added before it is filled because the add
// process triggers
// a testing for header alignement -- this is where we add
// it after iterating it first
if (dataRow != null) {
dtbl.addDataRow(dataRow);
}
dataRow = new DataRow(null);
// setting row name with localized time group and the
// date/time extraction based on the group
dataRow.setName(new Converter(objArr[3]).getString() + "");
}
dataRow.addValue(
new Converter(new Converter(objArr[2]).getInteger()).getString() + " (" + new Converter(objArr[1]).getString() + ")",
(new Converter(objArr[0]).getDouble()));
} catch (Exception e) {
dataRow.addValue(e.getMessage(), new Double(0));
}
}
// to add the last row
if (dataRow != null) {
dtbl.addDataRow(dataRow);