Search in sources :

Example 6 with Report

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

the class ReportController method getReport.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private void getReport(HttpServletRequest request, HttpServletResponse response, String uid, String organisationUnitUid, String isoPeriod, Date date, String type, String contentType, boolean attachment) throws Exception {
    Report report = reportService.getReport(uid);
    if (report == null) {
        throw new WebMessageException(WebMessageUtils.notFound("Report not found for identifier: " + uid));
    }
    if (organisationUnitUid == null && report.hasReportTable() && report.getReportTable().hasReportParams() && report.getReportTable().getReportParams().isOrganisationUnitSet()) {
        organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
    }
    if (report.isTypeHtml()) {
        contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_HTML, report.getCacheStrategy());
        reportService.renderHtmlReport(response.getWriter(), uid, date, organisationUnitUid);
    } else {
        date = date != null ? date : new DateTime().minusMonths(1).toDate();
        Period period = isoPeriod != null ? PeriodType.getPeriodFromIsoString(isoPeriod) : new MonthlyPeriodType().createPeriod(date);
        String filename = CodecUtils.filenameEncode(report.getName()) + "." + type;
        contextUtils.configureResponse(response, contentType, report.getCacheStrategy(), filename, attachment);
        JasperPrint print = reportService.renderReport(response.getOutputStream(), uid, period, organisationUnitUid, type);
        if ("html".equals(type)) {
            request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
        }
    }
}
Also used : Report(org.hisp.dhis.report.Report) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) JasperPrint(net.sf.jasperreports.engine.JasperPrint) Period(org.hisp.dhis.period.Period) DateTime(org.joda.time.DateTime)

Example 7 with Report

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

the class AddReportAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    // ---------------------------------------------------------------------
    // New report or update existing object?
    // ---------------------------------------------------------------------
    boolean isNewReport = id == null;
    if (fileName == null && currentDesign != null) {
        fileName = currentDesign;
    }
    if (id == null && (fileName == null || fileName.trim().length() == 0)) {
        return ERROR;
    }
    // ---------------------------------------------------------------------
    // Create report
    // ---------------------------------------------------------------------
    Report report = isNewReport ? new Report() : reportService.getReport(id);
    ReportTable reportTable = reportTableService.getReportTable(reportTableId);
    ReportParams reportParams = new ReportParams(paramReportingMonth, false, false, paramOrganisationUnit);
    report.setName(name);
    report.setType(type);
    report.setReportTable(reportTable);
    report.setRelatives(getRelativePeriods());
    report.setReportParams(reportParams);
    log.info("Upload file name: " + fileName + ", content type: " + contentType);
    if (file != null) {
        report.setDesignContent(FileUtils.readFileToString(file));
    }
    if (cacheStrategy != null) {
        CacheStrategy strategy = EnumUtils.getEnum(CacheStrategy.class, cacheStrategy);
        report.setCacheStrategy(strategy != null ? strategy : Report.DEFAULT_CACHE_STRATEGY);
    } else if (isNewReport) {
        report.setCacheStrategy(CacheStrategy.RESPECT_SYSTEM_SETTING);
    }
    reportService.saveReport(report);
    return SUCCESS;
}
Also used : Report(org.hisp.dhis.report.Report) ReportTable(org.hisp.dhis.reporttable.ReportTable) ReportParams(org.hisp.dhis.reporttable.ReportParams) CacheStrategy(org.hisp.dhis.common.cache.CacheStrategy)

Example 8 with Report

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

the class DefaultReportService method renderHtmlReport.

@Override
public void renderHtmlReport(Writer writer, String uid, Date date, String ou) {
    Report report = getReport(uid);
    OrganisationUnit organisationUnit = null;
    List<OrganisationUnit> organisationUnitHierarchy = new ArrayList<>();
    List<OrganisationUnit> organisationUnitChildren = new ArrayList<>();
    List<String> periods = new ArrayList<>();
    I18nFormat format = i18nManager.getI18nFormat();
    if (ou != null) {
        organisationUnit = organisationUnitService.getOrganisationUnit(ou);
        if (organisationUnit != null) {
            organisationUnitHierarchy.add(organisationUnit);
            OrganisationUnit parent = organisationUnit;
            while (parent.getParent() != null) {
                parent = parent.getParent();
                organisationUnitHierarchy.add(parent);
            }
            organisationUnitChildren.addAll(organisationUnit.getChildren());
        }
    }
    Calendar calendar = PeriodType.getCalendar();
    if (report != null && report.hasRelativePeriods()) {
        if (calendar.isIso8601()) {
            for (Period period : report.getRelatives().getRelativePeriods(date, format, true)) {
                periods.add(period.getIsoDate());
            }
        } else {
            periods = IdentifiableObjectUtils.getLocalPeriodIdentifiers(report.getRelatives().getRelativePeriods(date, format, true), calendar);
        }
    }
    String dateString = DateUtils.getMediumDateString(date);
    if (date != null && !calendar.isIso8601()) {
        dateString = calendar.formattedDate(calendar.fromIso(date));
    }
    final VelocityContext context = new VelocityContext();
    context.put("report", report);
    context.put("organisationUnit", organisationUnit);
    context.put("organisationUnitHierarchy", organisationUnitHierarchy);
    context.put("organisationUnitChildren", organisationUnitChildren);
    context.put("date", dateString);
    context.put("periods", periods);
    context.put("format", format);
    context.put("encoder", ENCODER);
    new VelocityManager().getEngine().getTemplate("html-report.vm").merge(context, writer);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) JasperReport(net.sf.jasperreports.engine.JasperReport) Report(org.hisp.dhis.report.Report) VelocityContext(org.apache.velocity.VelocityContext) VelocityManager(org.hisp.dhis.system.velocity.VelocityManager) Calendar(org.hisp.dhis.calendar.Calendar) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) I18nFormat(org.hisp.dhis.i18n.I18nFormat)

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