Search in sources :

Example 1 with Report

use of org.hisp.dhis.report.Report in project dhis2-core by dhis2.

the class DashboardTest method testGetAvailableItemByType.

@Test
public void testGetAvailableItemByType() {
    Dashboard dashboard = new Dashboard();
    DashboardItem diA = new DashboardItem();
    DashboardItem diB = new DashboardItem();
    DashboardItem diC = new DashboardItem();
    diA.setUid("A");
    diB.setUid("B");
    diC.setUid("C");
    diA.setChart(new Chart("A"));
    diB.getReports().add(new Report("A", null, null, null));
    diB.getReports().add(new Report("B", null, null, null));
    diC.getResources().add(new Document("A", null, false, null));
    diC.getResources().add(new Document("B", null, false, null));
    diC.getResources().add(new Document("C", null, false, null));
    dashboard.getItems().add(diA);
    dashboard.getItems().add(diB);
    dashboard.getItems().add(diC);
    assertEquals(diB, dashboard.getAvailableItemByType(DashboardItemType.REPORTS));
    assertEquals(diC, dashboard.getAvailableItemByType(DashboardItemType.RESOURCES));
    assertNull(dashboard.getAvailableItemByType(DashboardItemType.MAP));
}
Also used : Report(org.hisp.dhis.report.Report) Document(org.hisp.dhis.document.Document) Chart(org.hisp.dhis.chart.Chart) Test(org.junit.Test)

Example 2 with Report

use of org.hisp.dhis.report.Report 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)

Example 3 with Report

use of org.hisp.dhis.report.Report in project dhis2-core by dhis2.

the class GetReportParamsAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
    if (mode == null || uid == null) {
        return SUCCESS;
    }
    RelativePeriods relatives = null;
    if (MODE_REPORT_TABLE.equals(mode)) {
        ReportTable reportTable = reportTableService.getReportTable(uid);
        if (reportTable != null) {
            reportParams = reportTable.getReportParams();
            relatives = reportTable.getRelatives();
        }
    } else if (MODE_REPORT.equals(mode)) {
        Report report = reportService.getReport(uid);
        if (report != null && report.isTypeReportTable()) {
            reportParams = report.getReportTable().getReportParams();
            relatives = report.getReportTable().getRelatives();
        } else if (report != null && (report.isTypeJdbc() || report.isTypeHtml())) {
            reportParams = report.getReportParams();
            relatives = report.getRelatives();
        }
        if (type == null && report != null) {
            // Set type based on report
            type = report.getType();
        }
    }
    if (reportParams != null && reportParams.isParamReportingMonth() && relatives != null) {
        CalendarPeriodType periodType = (CalendarPeriodType) relatives.getPeriodType();
        List<Period> periods = periodType.generateLast5Years(new Date());
        Collections.reverse(periods);
        FilterUtils.filter(periods, new PastAndCurrentPeriodFilter());
        Calendar calendar = PeriodType.getCalendar();
        for (Period period_ : periods) {
            BaseIdentifiableObject period = new BaseIdentifiableObject();
            if (calendar.isIso8601()) {
                period.setUid(period_.getIsoDate());
                period.setDisplayName(format.formatPeriod(period_));
            } else {
                DateTimeUnit dateTimeUnit = calendar.fromIso(period_.getStartDate());
                period.setUid(period_.getPeriodType().getIsoDate(dateTimeUnit));
                period.setDisplayName(format.formatPeriod(period_));
            }
            this.periods.add(period);
        }
    }
    return SUCCESS;
}
Also used : RelativePeriods(org.hisp.dhis.period.RelativePeriods) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Report(org.hisp.dhis.report.Report) Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) ReportTable(org.hisp.dhis.reporttable.ReportTable) Period(org.hisp.dhis.period.Period) CalendarPeriodType(org.hisp.dhis.period.CalendarPeriodType) PastAndCurrentPeriodFilter(org.hisp.dhis.system.filter.PastAndCurrentPeriodFilter) Date(java.util.Date)

Example 4 with Report

use of org.hisp.dhis.report.Report in project dhis2-core by dhis2.

the class ReportController method getReportDesign.

@RequestMapping(value = "/{uid}/design", method = RequestMethod.GET)
public void getReportDesign(@PathVariable("uid") String uid, HttpServletResponse response) throws Exception {
    Report report = reportService.getReport(uid);
    if (report == null) {
        throw new WebMessageException(WebMessageUtils.notFound("Report not found for identifier: " + uid));
    }
    if (report.getDesignContent() == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Report has no design content: " + uid));
    }
    if (report.isTypeHtml()) {
        contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_HTML, CacheStrategy.NO_CACHE, filenameEncode(report.getName()) + ".html", true);
    } else {
        contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_XML, CacheStrategy.NO_CACHE, filenameEncode(report.getName()) + ".jrxml", true);
    }
    response.getWriter().write(report.getDesignContent());
}
Also used : Report(org.hisp.dhis.report.Report) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Report

use of org.hisp.dhis.report.Report in project dhis2-core by dhis2.

the class ReportController method updateReportDesign.

// -------------------------------------------------------------------------
// CRUD
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/design", method = RequestMethod.PUT)
@PreAuthorize("hasRole('ALL')")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateReportDesign(@PathVariable("uid") String uid, @RequestBody String designContent, HttpServletResponse response) throws Exception {
    Report report = reportService.getReport(uid);
    if (report == null) {
        throw new WebMessageException(WebMessageUtils.notFound("Report not found for identifier: " + uid));
    }
    report.setDesignContent(designContent);
    reportService.saveReport(report);
}
Also used : Report(org.hisp.dhis.report.Report) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Report (org.hisp.dhis.report.Report)8 Period (org.hisp.dhis.period.Period)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)3 ReportTable (org.hisp.dhis.reporttable.ReportTable)3 Date (java.util.Date)2 JasperPrint (net.sf.jasperreports.engine.JasperPrint)2 JasperReport (net.sf.jasperreports.engine.JasperReport)2 Calendar (org.hisp.dhis.calendar.Calendar)2 TextUtils.getCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString)2 I18nFormat (org.hisp.dhis.i18n.I18nFormat)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 VelocityContext (org.apache.velocity.VelocityContext)1 DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)1 Chart (org.hisp.dhis.chart.Chart)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 Grid (org.hisp.dhis.common.Grid)1