Search in sources :

Example 1 with JRDataSource

use of net.sf.jasperreports.engine.JRDataSource 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;
}
Also used : DJGroupLabel(ar.com.fdvs.dj.domain.DJGroupLabel) ClassicLayoutManager(ar.com.fdvs.dj.core.layout.ClassicLayoutManager) JRException(net.sf.jasperreports.engine.JRException) DynamicReport(ar.com.fdvs.dj.domain.DynamicReport) DecimalFormat(java.text.DecimalFormat) GroupBuilder(ar.com.fdvs.dj.domain.builders.GroupBuilder) AutoText(ar.com.fdvs.dj.domain.AutoText) JasperPrint(net.sf.jasperreports.engine.JasperPrint) ArrayList(java.util.ArrayList) JRDataSource(net.sf.jasperreports.engine.JRDataSource) DynamicReportBuilder(ar.com.fdvs.dj.domain.builders.DynamicReportBuilder) AbstractColumn(ar.com.fdvs.dj.domain.entities.columns.AbstractColumn) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRException(net.sf.jasperreports.engine.JRException) DJGroup(ar.com.fdvs.dj.domain.entities.DJGroup) Style(ar.com.fdvs.dj.domain.Style) JRTableModelDataSource(net.sf.jasperreports.engine.data.JRTableModelDataSource) ColumnBuilder(ar.com.fdvs.dj.domain.builders.ColumnBuilder) NumberFormat(java.text.NumberFormat)

Example 2 with JRDataSource

use of net.sf.jasperreports.engine.JRDataSource in project hmftools by hartwigmedical.

the class GenePanelDataSourceTest method canCreateGenePanelFor3Genes.

@Test
public void canCreateGenePanelFor3Genes() throws IOException {
    final HmfReporterData reporterData = testHmfReporterData();
    final JRDataSource dataSource = GenePanelDataSource.fromHmfReporterData(reporterData);
    assertNotNull(dataSource);
}
Also used : JRDataSource(net.sf.jasperreports.engine.JRDataSource) PatientReporterTestUtil.testHmfReporterData(com.hartwig.hmftools.patientreporter.PatientReporterTestUtil.testHmfReporterData) HmfReporterData(com.hartwig.hmftools.patientreporter.HmfReporterData) Test(org.junit.Test)

Example 3 with JRDataSource

use of net.sf.jasperreports.engine.JRDataSource in project java-examples by urvanov-ru.

the class App method generateReport.

public static void generateReport() throws JRException, IOException {
    JasperDesign jasperDesign = createDesign();
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    JRDataSource jrDataSource = prepareDataSource();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("startDate", new Date());
    params.put("endDate", new Date());
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrDataSource);
    try (FileOutputStream baos = new FileOutputStream("dynamicReport.xls")) {
        JRXlsExporter xlsExporter = new JRXlsExporter();
        xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos));
        SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
        configuration.setOnePagePerSheet(false);
        xlsExporter.setConfiguration(configuration);
        xlsExporter.exportReport();
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) JasperDesign(net.sf.jasperreports.engine.design.JasperDesign) HashMap(java.util.HashMap) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRDataSource(net.sf.jasperreports.engine.JRDataSource) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JasperReport(net.sf.jasperreports.engine.JasperReport) Date(java.util.Date) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) FileOutputStream(java.io.FileOutputStream)

Aggregations

JRDataSource (net.sf.jasperreports.engine.JRDataSource)3 JasperPrint (net.sf.jasperreports.engine.JasperPrint)2 ClassicLayoutManager (ar.com.fdvs.dj.core.layout.ClassicLayoutManager)1 AutoText (ar.com.fdvs.dj.domain.AutoText)1 DJGroupLabel (ar.com.fdvs.dj.domain.DJGroupLabel)1 DynamicReport (ar.com.fdvs.dj.domain.DynamicReport)1 Style (ar.com.fdvs.dj.domain.Style)1 ColumnBuilder (ar.com.fdvs.dj.domain.builders.ColumnBuilder)1 DynamicReportBuilder (ar.com.fdvs.dj.domain.builders.DynamicReportBuilder)1 GroupBuilder (ar.com.fdvs.dj.domain.builders.GroupBuilder)1 DJGroup (ar.com.fdvs.dj.domain.entities.DJGroup)1 AbstractColumn (ar.com.fdvs.dj.domain.entities.columns.AbstractColumn)1 HmfReporterData (com.hartwig.hmftools.patientreporter.HmfReporterData)1 PatientReporterTestUtil.testHmfReporterData (com.hartwig.hmftools.patientreporter.PatientReporterTestUtil.testHmfReporterData)1 FileOutputStream (java.io.FileOutputStream)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1