Search in sources :

Example 6 with JasperReport

use of net.sf.jasperreports.engine.JasperReport in project adempiere by adempiere.

the class JasperViewer method main.

// End of variables declaration//GEN-END:variables
/**
	* @param args the command line arguments
	*/
public static void main(String[] args) {
    try {
        JasperReport myjasperReport = (JasperReport) JasperCompileManager.compileReport(args[0]);
        JasperPrint myjasperPrint = JasperFillManager.fillReport(myjasperReport, new HashMap(), getConnection());
        JasperViewer.viewReport(myjasperPrint);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JasperReport(net.sf.jasperreports.engine.JasperReport) JRException(net.sf.jasperreports.engine.JRException) SQLException(java.sql.SQLException)

Example 7 with JasperReport

use of net.sf.jasperreports.engine.JasperReport in project adempiere by adempiere.

the class ReportStarter method processReport.

/**
     * @author rlemeill
     * @param reportFile
     * @return
     */
protected JasperData processReport(File reportFile) {
    log.info("reportFile.getAbsolutePath() = " + reportFile.getAbsolutePath());
    JasperReport jasperReport = null;
    String jasperName = reportFile.getName();
    int pos = jasperName.indexOf('.');
    if (pos != -1)
        jasperName = jasperName.substring(0, pos);
    File reportDir = reportFile.getParentFile();
    //test if the compiled report exists
    File jasperFile = new File(reportDir.getAbsolutePath(), jasperName + ".jasper");
    if (jasperFile.exists()) {
        // test time
        if (reportFile.lastModified() == jasperFile.lastModified()) {
            log.info(" no need to compile use " + jasperFile.getAbsolutePath());
            try {
                jasperReport = (JasperReport) JRLoader.loadObject(jasperFile.getAbsolutePath());
            } catch (JRException e) {
                jasperReport = null;
                log.severe("Can not load report - " + e.getMessage());
            }
        } else {
            jasperReport = compileReport(reportFile, jasperFile);
        }
    } else {
        // create new jasper file
        jasperReport = compileReport(reportFile, jasperFile);
    }
    return new JasperData(jasperReport, reportDir, jasperName, jasperFile);
}
Also used : JRException(net.sf.jasperreports.engine.JRException) JasperReport(net.sf.jasperreports.engine.JasperReport) DigestOfFile(org.compiere.util.DigestOfFile) File(java.io.File) JasperPrint(net.sf.jasperreports.engine.JasperPrint)

Example 8 with JasperReport

use of net.sf.jasperreports.engine.JasperReport in project midpoint by Evolveum.

the class ReportCreateTaskHandler method getSubreportParameters.

private Map<String, Object> getSubreportParameters(SubreportType subreportType, Task task, OperationResult subResult) throws SchemaException, ObjectNotFoundException {
    Map<String, Object> reportParams = new HashMap<String, Object>();
    ReportType reportType = objectResolver.resolve(subreportType.getReportRef(), ReportType.class, null, "resolve subreport", task, subResult);
    Map<String, Object> parameters = prepareReportParameters(reportType, subResult);
    reportParams.putAll(parameters);
    JasperReport jasperReport = ReportTypeUtil.loadJasperReport(reportType);
    reportParams.put(subreportType.getName(), jasperReport);
    Map<String, Object> subReportParams = processSubreportParameters(reportType, task, subResult);
    reportParams.putAll(subReportParams);
    return reportParams;
}
Also used : HashMap(java.util.HashMap) PrismObject(com.evolveum.midpoint.prism.PrismObject) JasperReport(net.sf.jasperreports.engine.JasperReport) ReportType(com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType)

Example 9 with JasperReport

use of net.sf.jasperreports.engine.JasperReport in project midpoint by Evolveum.

the class ReportCreateTaskHandler method run.

@Override
public TaskRunResult run(Task task) {
    // TODO Auto-generated method stub
    OperationResult parentResult = task.getResult();
    OperationResult result = parentResult.createSubresult(ReportCreateTaskHandler.class.getSimpleName() + ".run");
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(result);
    recordProgress(task, 0, result);
    long progress = task.getProgress();
    JRSwapFile swapFile = null;
    // http://community.jaspersoft.com/wiki/virtualizers-jasperreports
    JRAbstractLRUVirtualizer virtualizer = null;
    try {
        ReportType parentReport = objectResolver.resolve(task.getObjectRef(), ReportType.class, null, "resolving report", task, result);
        Map<String, Object> parameters = completeReport(parentReport, task, result);
        JasperReport jasperReport = ReportTypeUtil.loadJasperReport(parentReport);
        LOGGER.trace("compile jasper design, create jasper report : {}", jasperReport);
        PrismContainer<ReportParameterType> reportParams = (PrismContainer) task.getExtensionItem(ReportConstants.REPORT_PARAMS_PROPERTY_NAME);
        if (reportParams != null) {
            PrismContainerValue<ReportParameterType> reportParamsValues = reportParams.getValue();
            List<Item<?, ?>> items = reportParamsValues.getItems();
            if (items != null) {
                for (Item item : items) {
                    PrismProperty pp = (PrismProperty) item;
                    String paramName = ItemPath.getName(pp.getPath().lastNamed()).getLocalPart();
                    Object value = null;
                    if (isSingleValue(paramName, jasperReport.getParameters())) {
                        value = pp.getRealValues().iterator().next();
                    } else {
                        value = pp.getRealValues();
                    }
                    parameters.put(paramName, value);
                }
            }
        }
        String virtualizerS = parentReport.getVirtualizer();
        Integer virtualizerKickOn = parentReport.getVirtualizerKickOn();
        Integer maxPages = parentReport.getMaxPages();
        Integer timeout = parentReport.getTimeout();
        if (maxPages != null && maxPages > 0) {
            LOGGER.trace("Setting hardlimit on number of report pages: " + maxPages);
            jasperReport.setProperty(MaxPagesGovernor.PROPERTY_MAX_PAGES_ENABLED, Boolean.TRUE.toString());
            jasperReport.setProperty(MaxPagesGovernor.PROPERTY_MAX_PAGES, String.valueOf(maxPages));
        }
        if (timeout != null && timeout > 0) {
            LOGGER.trace("Setting timeout on report execution [ms]: " + timeout);
            jasperReport.setProperty(TimeoutGovernor.PROPERTY_TIMEOUT_ENABLED, Boolean.TRUE.toString());
            jasperReport.setProperty(TimeoutGovernor.PROPERTY_TIMEOUT, String.valueOf(timeout));
        }
        if (virtualizerS != null && virtualizerKickOn != null && virtualizerKickOn > 0) {
            String virtualizerClassName = JASPER_VIRTUALIZER_PKG + "." + virtualizerS;
            try {
                Class<?> clazz = Class.forName(virtualizerClassName);
                if (clazz.equals(JRSwapFileVirtualizer.class)) {
                    swapFile = new JRSwapFile(TEMP_DIR, 4096, 200);
                    virtualizer = new JRSwapFileVirtualizer(virtualizerKickOn, swapFile);
                } else if (clazz.equals(JRGzipVirtualizer.class)) {
                    virtualizer = new JRGzipVirtualizer(virtualizerKickOn);
                } else if (clazz.equals(JRFileVirtualizer.class)) {
                    virtualizer = new JRFileVirtualizer(virtualizerKickOn, TEMP_DIR);
                } else {
                    throw new ClassNotFoundException("No support for virtualizer class: " + clazz.getName());
                }
                LOGGER.trace("Setting explicit Jasper virtualizer: " + virtualizer);
                virtualizer.setReadOnly(false);
                parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
            } catch (ClassNotFoundException e) {
                LOGGER.error("Cannot find Jasper virtualizer: " + e.getMessage());
            }
        }
        LOGGER.trace("All Report parameters : {}", parameters);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
        LOGGER.trace("fill report : {}", jasperPrint);
        String reportFilePath = generateReport(parentReport, jasperPrint);
        LOGGER.trace("generate report : {}", reportFilePath);
        saveReportOutputType(reportFilePath, parentReport, task, result);
        LOGGER.trace("create report output type : {}", reportFilePath);
        result.computeStatus();
    } catch (Exception ex) {
        LOGGER.error("CreateReport: {}", ex.getMessage(), ex);
        result.recordFatalError(ex.getMessage(), ex);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        runResult.setProgress(progress);
        return runResult;
    } finally {
        if (swapFile != null) {
            swapFile.dispose();
        }
        if (virtualizer != null) {
            virtualizer.cleanup();
        }
    }
    // This "run" is finished. But the task goes on ...
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    runResult.setProgress(progress);
    LOGGER.trace("CreateReportTaskHandler.run stopping");
    return runResult;
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) JasperReport(net.sf.jasperreports.engine.JasperReport) Item(com.evolveum.midpoint.prism.Item) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) JRFileVirtualizer(net.sf.jasperreports.engine.fill.JRFileVirtualizer) ReportType(com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType) ReportParameterType(com.evolveum.midpoint.xml.ns._public.common.common_3.ReportParameterType) JRSwapFileVirtualizer(net.sf.jasperreports.engine.fill.JRSwapFileVirtualizer) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRSwapFile(net.sf.jasperreports.engine.util.JRSwapFile) JRAbstractLRUVirtualizer(net.sf.jasperreports.engine.fill.JRAbstractLRUVirtualizer) JRGzipVirtualizer(net.sf.jasperreports.engine.fill.JRGzipVirtualizer) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SystemException(com.evolveum.midpoint.util.exception.SystemException) JRException(net.sf.jasperreports.engine.JRException) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PrismObject(com.evolveum.midpoint.prism.PrismObject)

Example 10 with JasperReport

use of net.sf.jasperreports.engine.JasperReport in project dhis2-core by dhis2.

the class DefaultReportService method renderReport.

// -------------------------------------------------------------------------
// ReportService implementation
// -------------------------------------------------------------------------
@Override
public JasperPrint renderReport(OutputStream out, String reportUid, Period period, String organisationUnitUid, String type) {
    I18nFormat format = i18nManager.getI18nFormat();
    Report report = getReport(reportUid);
    Map<String, Object> params = new HashMap<>();
    params.putAll(constantService.getConstantParameterMap());
    Date reportDate = new Date();
    if (period != null) {
        params.put(PARAM_PERIOD_NAME, format.formatPeriod(period));
        reportDate = period.getStartDate();
    }
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(organisationUnitUid);
    if (orgUnit != null) {
        int level = orgUnit.getLevel();
        params.put(PARAM_ORGANISATIONUNIT_COLUMN_NAME, orgUnit.getName());
        params.put(PARAM_ORGANISATIONUNIT_LEVEL, level);
        params.put(PARAM_ORGANISATIONUNIT_LEVEL_COLUMN, ORGUNIT_LEVEL_COLUMN_PREFIX + level);
        params.put(PARAM_ORGANISATIONUNIT_UID_LEVEL_COLUMN, ORGUNIT_UID_LEVEL_COLUMN_PREFIX + level);
    }
    JasperPrint print = null;
    try {
        JasperReport jasperReport = JasperCompileManager.compileReport(IOUtils.toInputStream(report.getDesignContent(), StandardCharsets.UTF_8));
        if (// Use JR data source
        report.hasReportTable()) {
            ReportTable reportTable = report.getReportTable();
            Grid grid = reportTableService.getReportTableGrid(reportTable.getUid(), reportDate, organisationUnitUid);
            print = JasperFillManager.fillReport(jasperReport, params, grid);
        } else // Use JDBC data source
        {
            if (report.hasRelativePeriods()) {
                List<Period> relativePeriods = report.getRelatives().getRelativePeriods(reportDate, null, false);
                String periodString = getCommaDelimitedString(getIdentifiers(periodService.reloadPeriods(relativePeriods)));
                String isoPeriodString = getCommaDelimitedString(IdentifiableObjectUtils.getUids(relativePeriods));
                params.put(PARAM_RELATIVE_PERIODS, periodString);
                params.put(PARAM_RELATIVE_ISO_PERIODS, isoPeriodString);
            }
            if (report.hasReportParams() && report.getReportParams().isParamOrganisationUnit() && orgUnit != null) {
                params.put(PARAM_ORG_UNITS, String.valueOf(orgUnit.getId()));
                params.put(PARAM_ORG_UNITS_UID, String.valueOf(orgUnit.getUid()));
            }
            Connection connection = DataSourceUtils.getConnection(dataSource);
            try {
                print = JasperFillManager.fillReport(jasperReport, params, connection);
            } finally {
                DataSourceUtils.releaseConnection(connection, dataSource);
            }
        }
        if (print != null) {
            JRExportUtils.export(type, out, print);
        }
    } catch (Exception ex) {
        throw new RuntimeException("Failed to render report", ex);
    }
    return print;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) JasperReport(net.sf.jasperreports.engine.JasperReport) Report(org.hisp.dhis.report.Report) HashMap(java.util.HashMap) JasperPrint(net.sf.jasperreports.engine.JasperPrint) Grid(org.hisp.dhis.common.Grid) Connection(java.sql.Connection) ReportTable(org.hisp.dhis.reporttable.ReportTable) Period(org.hisp.dhis.period.Period) I18nFormat(org.hisp.dhis.i18n.I18nFormat) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) JasperReport(net.sf.jasperreports.engine.JasperReport) Date(java.util.Date) JasperPrint(net.sf.jasperreports.engine.JasperPrint)

Aggregations

JasperReport (net.sf.jasperreports.engine.JasperReport)18 JRException (net.sf.jasperreports.engine.JRException)11 JasperPrint (net.sf.jasperreports.engine.JasperPrint)10 HashMap (java.util.HashMap)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)4 File (java.io.File)4 Connection (java.sql.Connection)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 JasperDesign (net.sf.jasperreports.engine.design.JasperDesign)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 ReportType (com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType)2 SQLException (java.sql.SQLException)2 JRDesignExpression (net.sf.jasperreports.engine.design.JRDesignExpression)2 DigestOfFile (org.compiere.util.DigestOfFile)2 ReportException (org.opennms.api.reporting.ReportException)2 ModelState (com.evolveum.midpoint.model.api.context.ModelState)1 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)1 Item (com.evolveum.midpoint.prism.Item)1 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)1 PrismProperty (com.evolveum.midpoint.prism.PrismProperty)1