use of ar.com.fdvs.dj.domain.entities.DJGroup in project jgnash by ccavanaugh.
the class BaseDynamicJasperReport method createJasperPrint.
@SuppressWarnings("ConstantConditions")
protected final JasperPrint createJasperPrint(final AbstractReportTableModel model, final boolean formatForCSV) {
logger.info(rb.getString("Message.ProcessingReportData"));
DynamicReportBuilder drb = new DynamicReportBuilder();
try {
if (formatForCSV) {
drb.setIgnorePagination(true);
}
final List<AbstractColumn> columns = new ArrayList<>();
final int[] columnsToHide = model.getColumnsToHide();
assignPageFormat(drb);
drb.setHeaderHeight(getStyle(model.getColumnHeaderStyle(0), formatForCSV).getFont().getFontSize() * 2);
drb.setDetailHeight(getBaseFontSize() * 2);
logger.info("Creating column model for report");
// create columns and add to the list
for (int i = 0; i < model.getColumnCount(); i++) {
if (Arrays.binarySearch(columnsToHide, i) >= 0) {
// don't add any hidden columns
continue;
}
if (model.getColumnStyle(i) != ColumnStyle.GROUP && model.getColumnStyle(i) != ColumnStyle.GROUP_NO_HEADER) {
Style columnTypeStyle = getStyle(model.getColumnStyle(i), formatForCSV);
Style columnHeaderStyle = getStyle(model.getColumnHeaderStyle(i), formatForCSV);
int width = AWTFontUtilities.getStringWidth(model.getColumnPrototypeValueAt(i), getTypeFooterStyle());
int hWidth = AWTFontUtilities.getStringWidth(model.getColumnName(i), columnHeaderStyle);
if (hWidth > width) {
width = hWidth;
}
ColumnBuilder builder = ColumnBuilder.getNew();
builder.setColumnProperty(COLUMN_PROPERTY + i, model.getColumnClass(i).getName());
builder.setTitle(model.getColumnName(i));
builder.setWidth(width);
builder.setStyle(columnTypeStyle);
builder.setHeaderStyle(columnHeaderStyle);
builder.setTruncateSuffix("…");
// set the format pattern for decimal values
if (model.getColumnStyle(i) == ColumnStyle.AMOUNT_SUM || model.getColumnStyle(i) == ColumnStyle.BALANCE || model.getColumnStyle(i) == ColumnStyle.BALANCE_WITH_SUM || model.getColumnStyle(i) == ColumnStyle.BALANCE_WITH_SUM_AND_GLOBAL || model.getColumnStyle(i) == ColumnStyle.CROSSTAB_TOTAL) {
String pattern = CommodityFormat.getFullNumberPattern(model.getCurrency());
builder.setPattern(pattern);
} else if (model.getColumnStyle(i) == ColumnStyle.PERCENTAGE) {
NumberFormat nf = ReportFactory.getPercentageFormat();
String pattern = ((DecimalFormat) nf).toPattern();
builder.setPattern(pattern);
} else if (model.getColumnStyle(i) == ColumnStyle.QUANTITY) {
NumberFormat nf = ReportFactory.getQuantityFormat();
String pattern = ((DecimalFormat) nf).toPattern();
builder.setPattern(pattern);
} else if (model.getColumnStyle(i) == ColumnStyle.SHORT_DATE) {
builder.setTextFormatter(DateUtils.getShortDateFormatter().toFormat());
} else if (model.getColumnStyle(i) == ColumnStyle.TIMESTAMP) {
builder.setTextFormatter(DateUtils.getShortDateTimeFormatter().toFormat());
} else if (model.getColumnStyle(i) == ColumnStyle.SHORT_AMOUNT) {
String pattern = CommodityFormat.getShortNumberPattern(model.getCurrency());
builder.setPattern(pattern);
}
if (model.isColumnFixedWidth(i) && !formatForCSV) {
builder.setFixedWidth(true);
}
columns.add(builder.build());
} else if (model.getColumnStyle(i) == ColumnStyle.GROUP || model.getColumnStyle(i) == ColumnStyle.GROUP_NO_HEADER) {
ColumnBuilder builder = ColumnBuilder.getNew();
builder.setColumnProperty(COLUMN_PROPERTY + i, model.getColumnClass(i).getName());
builder.setTitle(model.getColumnName(i));
builder.setStyle(getTypeHeaderStyle());
columns.add(builder.build());
}
}
boolean group = false;
boolean header = true;
logger.info("Searching for report groups");
// determine if a group needs to be created
for (int i = 0; i < model.getColumnCount(); i++) {
if (model.getColumnStyle(i) == ColumnStyle.GROUP) {
group = true;
break;
} else if (model.getColumnStyle(i) == ColumnStyle.GROUP_NO_HEADER) {
group = true;
header = false;
break;
}
}
if (group) {
// group columns
logger.info("Building report groups");
GroupBuilder gb = new GroupBuilder();
gb.setDefaultHeaderVariableStyle(getTypeHeaderStyle());
gb.setDefaultFooterVariableStyle(getTypeFooterStyle());
for (int i = 0; i < columns.size(); i++) {
if (model.getColumnStyle(i) == ColumnStyle.GROUP || model.getColumnStyle(i) == ColumnStyle.GROUP_NO_HEADER) {
gb.setCriteriaColumn((PropertyColumn) columns.get(i));
} else if (model.getColumnStyle(i) == ColumnStyle.AMOUNT_SUM || model.getColumnStyle(i) == ColumnStyle.BALANCE_WITH_SUM || model.getColumnStyle(i) == ColumnStyle.BALANCE_WITH_SUM_AND_GLOBAL || model.getColumnStyle(i) == ColumnStyle.CROSSTAB_TOTAL) {
gb.addFooterVariable(columns.get(i), DJCalculation.SUM);
}
}
if (header) {
gb.setGroupLayout(GroupLayout.VALUE_IN_HEADER);
} else {
gb.setGroupLayout(GroupLayout.EMPTY);
}
// adds a group footer label if it is not null or zero length
if (getGroupFooterLabel() != null && !getGroupFooterLabel().isEmpty()) {
DJGroupLabel label = new DJGroupLabel(getGroupFooterLabel(), getTypeFooterStyle(), LabelPosition.LEFT);
gb.setFooterLabel(label);
}
DJGroup group1 = gb.build();
boolean global = false;
for (int i = 0; i < columns.size(); i++) {
final AbstractColumn c = columns.get(i);
drb.addColumn(c);
if (model.getColumnStyle(i) == ColumnStyle.BALANCE_WITH_SUM_AND_GLOBAL || model.getColumnStyle(i) == ColumnStyle.CROSSTAB_TOTAL) {
drb.addGlobalFooterVariable(c, DJCalculation.SUM, getTypeFooterStyle());
global = true;
}
}
drb.addGroup(group1);
if (global) {
drb.setGrandTotalLegendStyle(getGlobalFooterStyle());
drb.setGrandTotalLegend(getGrandTotalLegend());
}
} else {
// no groups exist, just add the columns
for (final AbstractColumn column : columns) {
drb.addColumn(column);
}
}
} catch (Exception e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
buildTitle(drb);
// use the full page width
drb.setUseFullPageWidth(true);
if (!formatForCSV) {
final Style footerStyle = getPageFooterStyle();
final int halfWidth = (int) (getPageFormat().getWidth() * .5);
final AutoText date = new AutoText(AutoText.AUTOTEXT_CREATED_ON, AutoText.POSITION_FOOTER, HorizontalBandAlignment.LEFT, AutoText.PATTERN_DATE_DATE_TIME, halfWidth);
date.setStyle(footerStyle);
final AutoText pageNum = new AutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, HorizontalBandAlignment.RIGHT, (byte) 0, 80, 50);
pageNum.setStyle(footerStyle);
drb.addAutoText(date);
drb.addAutoText(pageNum);
}
logger.info(rb.getString("Message.CompilingReport"));
DynamicReport dr = drb.build();
logger.info(rb.getString("Message.ReportCompileComplete"));
JRDataSource ds = new JRTableModelDataSource(model);
JasperPrint jp = null;
try {
logger.info(rb.getString("Message.ReportCreateView"));
jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
} catch (final JRException e) {
logger.log(Level.WARNING, "Exception", e);
logger.warning(rb.getString("Message.ReduceFont"));
displayError(rb.getString("Message.ReduceFont"));
}
return jp;
}
Aggregations