use of org.jfree.data.general.Dataset in project adempiere by adempiere.
the class MChartDatasource method addData.
public void addData(MChart parent) {
String value = getValueColumn();
String category;
String unit = "D";
if (!parent.isTimeSeries())
category = getCategoryColumn();
else {
if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Week)) {
unit = "W";
} else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Month)) {
unit = "MM";
} else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Quarter)) {
unit = "Q";
} else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Year)) {
unit = "Y";
}
category = " TRUNC(" + getDateColumn() + ", '" + unit + "') ";
}
String series = DB.TO_STRING(getName());
boolean hasSeries = false;
if (getSeriesColumn() != null) {
series = getSeriesColumn();
hasSeries = true;
}
String where = getWhereClause();
if (!Util.isEmpty(where)) {
where = Env.parseContext(getCtx(), parent.getWindowNo(), where, true);
}
boolean hasWhere = false;
String sql = "SELECT " + value + ", " + category + ", " + series + " FROM " + getFromClause();
if (!Util.isEmpty(where)) {
sql += " WHERE " + where;
hasWhere = true;
}
Date currentDate = Env.getContextAsDate(getCtx(), "#Date");
Date startDate = null;
Date endDate = null;
int scope = parent.getTimeScope();
int offset = getTimeOffset();
if (parent.isTimeSeries() && scope != 0) {
offset += -scope;
startDate = increment(currentDate, parent.getTimeUnit(), offset);
endDate = increment(startDate, parent.getTimeUnit(), scope);
}
if (startDate != null && endDate != null) {
sql += hasWhere ? " AND " : " WHERE ";
sql += category + ">=TRUNC(" + DB.TO_DATE(new Timestamp(startDate.getTime())) + ", '" + unit + "') AND ";
sql += category + "<=TRUNC(" + DB.TO_DATE(new Timestamp(endDate.getTime())) + ", '" + unit + "') ";
}
MRole role = MRole.getDefault(getCtx(), false);
sql = role.addAccessSQL(sql, null, true, false);
if (hasSeries)
sql += " GROUP BY " + series + ", " + category + " ORDER BY " + series + ", " + category;
else
sql += " GROUP BY " + category + " ORDER BY " + category;
log.log(Level.FINE, sql);
PreparedStatement pstmt = null;
ResultSet rs = null;
TimeSeries tseries = null;
Dataset dataset = parent.getDataset();
try {
pstmt = DB.prepareStatement(sql, null);
rs = pstmt.executeQuery();
while (rs.next()) {
String key = rs.getString(2);
String seriesName = rs.getString(3);
if (seriesName == null)
seriesName = getName();
String queryWhere = "";
if (hasWhere)
queryWhere += where + " AND ";
queryWhere += series + " = " + DB.TO_STRING(seriesName) + " AND " + category + " = ";
if (parent.isTimeSeries() && dataset instanceof TimeSeriesCollection) {
if (tseries == null || !tseries.getKey().equals(seriesName)) {
if (tseries != null)
((TimeSeriesCollection) dataset).addSeries(tseries);
tseries = new TimeSeries(seriesName);
}
Date date = rs.getDate(2);
RegularTimePeriod period = null;
if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Day))
period = new Day(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Week))
period = new Week(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Month))
period = new Month(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Quarter))
period = new Quarter(date);
else if (parent.getTimeUnit().equals(MChart.TIMEUNIT_Year))
period = new Year(date);
tseries.add(period, rs.getBigDecimal(1));
key = period.toString();
queryWhere += DB.TO_DATE(new Timestamp(date.getTime()));
} else {
queryWhere += DB.TO_STRING(key);
}
MQuery query = new MQuery(getAD_Table_ID());
String keyCol = MTable.get(getCtx(), getAD_Table_ID()).getKeyColumns()[0];
String whereClause = keyCol + " IN (SELECT " + getKeyColumn() + " FROM " + getFromClause() + " WHERE " + queryWhere + " )";
query.addRestriction(whereClause.toString());
query.setRecordCount(1);
HashMap<String, MQuery> map = parent.getQueries();
if (dataset instanceof DefaultPieDataset) {
((DefaultPieDataset) dataset).setValue(key, rs.getBigDecimal(1));
map.put(key, query);
} else if (dataset instanceof DefaultCategoryDataset) {
((DefaultCategoryDataset) dataset).addValue(rs.getBigDecimal(1), seriesName, key);
map.put(seriesName + "__" + key, query);
} else if (dataset instanceof TimeSeriesCollection) {
map.put(seriesName + "__" + key, query);
}
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
if (tseries != null)
((TimeSeriesCollection) dataset).addSeries(tseries);
}
Aggregations