Search in sources :

Example 1 with ClassicLayoutManager

use of ar.com.fdvs.dj.core.layout.ClassicLayoutManager in project Asqatasun by Asqatasun.

the class ExportService method export.

/**
     * Processes the download for Excel format
     * @param response
     * @param resourceId
     * @param auditStatistics
     * @param dataSource
     * @param locale
     * @param format
     * @throws ColumnBuilderException
     * @throws ClassNotFoundException
     * @throws JRException
     * @throws NotSupportedExportFormatException 
     */
@SuppressWarnings("unchecked")
public void export(HttpServletResponse response, long resourceId, AuditStatistics auditStatistics, Collection<?> dataSource, Locale locale, String format) throws ColumnBuilderException, ClassNotFoundException, JRException, NotSupportedExportFormatException {
    if (!exportFormatMap.containsKey(format)) {
        throw new NotSupportedExportFormatException(format);
    }
    ExportFormat exportFormat = exportFormatMap.get(format);
    DynamicReport dr = LayoutFactory.getInstance().buildReportLayout(locale, auditStatistics, format);
    // Retrieve our data source
    JRDataSource ds = new JRBeanCollectionDataSource(dataSource);
    // params is used for passing extra parameters
    JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
    // Create our output byte stream
    // This is the stream where the data will be written
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JRExporter exporter;
    try {
        exporter = (JRExporter) Class.forName(exportFormat.getExporterClassName()).newInstance();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
        response.setHeader(CONTENT_DISPOSITION, INLINE_FILENAME + getFileName(resourceId, exportFormat.getFileExtension()));
        // Make sure to set the correct content type
        // Each format has its own content type
        response.setContentType(exportFormat.getFileType());
        response.setContentLength(baos.size());
        // Write to reponse stream
        writeReportToResponseStream(response, baos);
    } catch (InstantiationException | IllegalAccessException ex) {
        LOGGER.warn(ex);
    }
}
Also used : ClassicLayoutManager(ar.com.fdvs.dj.core.layout.ClassicLayoutManager) DynamicReport(ar.com.fdvs.dj.domain.DynamicReport) JRBeanCollectionDataSource(net.sf.jasperreports.engine.data.JRBeanCollectionDataSource) NotSupportedExportFormatException(org.asqatasun.webapp.report.service.exception.NotSupportedExportFormatException) ExportFormat(org.asqatasun.webapp.report.format.ExportFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with ClassicLayoutManager

use of ar.com.fdvs.dj.core.layout.ClassicLayoutManager 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)

Aggregations

ClassicLayoutManager (ar.com.fdvs.dj.core.layout.ClassicLayoutManager)2 DynamicReport (ar.com.fdvs.dj.domain.DynamicReport)2 AutoText (ar.com.fdvs.dj.domain.AutoText)1 DJGroupLabel (ar.com.fdvs.dj.domain.DJGroupLabel)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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 JRDataSource (net.sf.jasperreports.engine.JRDataSource)1 JRException (net.sf.jasperreports.engine.JRException)1 JasperPrint (net.sf.jasperreports.engine.JasperPrint)1 JRBeanCollectionDataSource (net.sf.jasperreports.engine.data.JRBeanCollectionDataSource)1 JRTableModelDataSource (net.sf.jasperreports.engine.data.JRTableModelDataSource)1 ExportFormat (org.asqatasun.webapp.report.format.ExportFormat)1