Search in sources :

Example 1 with PentahoReport

use of org.mifos.reports.pentaho.PentahoReport in project head by mifos.

the class PentahoReportsServiceImpl method getReport.

@Override
public PentahoReport getReport(Integer reportId, Integer outputTypeId, Map<String, AbstractPentahoParameter> params) {
    ByteArrayOutputStream baos = null;
    if (!checkAccessToReport(reportId)) {
        throw new AccessDeniedException("Access denied");
    }
    try {
        String reportFileName = getReportFilename(reportId);
        // load report definition
        ResourceManager manager = new ResourceManager();
        manager.registerDefaults();
        URL url = PentahoReportLocator.getURLForReport(reportFileName);
        Resource res = manager.createDirectly(url, MasterReport.class);
        MasterReport report = (MasterReport) res.getResource();
        PentahoReport result = new PentahoReport();
        List<PentahoValidationError> errors = new ArrayList<PentahoValidationError>();
        try {
            addParametersToReport(report, params);
            validate(report, errors);
        } catch (ReflectionException ex) {
            errors.add(new PentahoValidationError(ex.getMessage()));
        }
        result.setErrors(errors);
        if (errors.isEmpty()) {
            baos = new ByteArrayOutputStream();
            PentahoOutputType outputType = PentahoOutputType.findById(outputTypeId);
            switch(outputType) {
                case XLS:
                    ExcelReportUtil.createXLS(report, baos);
                    break;
                case RTF:
                    RTFReportUtil.createRTF(report, baos);
                    break;
                case HTML:
                    HtmlReportUtil.createStreamHTML(report, baos);
                    break;
                case CSV:
                    CSVReportUtil.createCSV(report, baos, "UTF-8");
                    break;
                case XML:
                    XmlTableReportUtil.createFlowXML(report, baos);
                    break;
                default:
                    // PDF
                    PdfReportUtil.createPDF(report, baos);
                    break;
            }
            result.setContentType(outputType.getContentType());
            result.setFileExtension(outputType.getFileExtension());
            result.setName(getReportName(reportId));
            result.setContent(baos.toByteArray());
        }
        return result;
    } catch (Exception e) {
        throw new MifosRuntimeException(e);
    } finally {
        closeStream(baos);
    }
}
Also used : PentahoReport(org.mifos.reports.pentaho.PentahoReport) ReflectionException(org.mifos.reports.pentaho.util.ReflectionException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Resource(org.pentaho.reporting.libraries.resourceloader.Resource) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceManager(org.pentaho.reporting.libraries.resourceloader.ResourceManager) URL(java.net.URL) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ReflectionException(org.mifos.reports.pentaho.util.ReflectionException) ReportProcessingException(org.pentaho.reporting.engine.classic.core.ReportProcessingException) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) PentahoOutputType(org.mifos.reports.pentaho.util.PentahoOutputType) MasterReport(org.pentaho.reporting.engine.classic.core.MasterReport) PentahoValidationError(org.mifos.reports.pentaho.PentahoValidationError) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Example 2 with PentahoReport

use of org.mifos.reports.pentaho.PentahoReport in project head by mifos.

the class PentahoReportingController method executeReport.

@RequestMapping(value = "/execPentahoReport.ftl", method = RequestMethod.POST)
public ModelAndView executeReport(final HttpServletRequest request, HttpServletResponse response, @RequestParam(value = CANCEL_PARAM, required = false) String cancel, @Valid @ModelAttribute("pentahoReportFormBean") PentahoReportFormBean pentahoReportFormBean, BindingResult bindingResult) throws IOException {
    if (!this.pentahoReportsService.checkAccessToReport(pentahoReportFormBean.getReportId())) {
        throw new AccessDeniedException("Access denied");
    }
    ModelAndView mav = null;
    Integer reportId = pentahoReportFormBean.getReportId();
    if (StringUtils.isNotBlank(cancel)) {
        mav = new ModelAndView("redirect:" + REPORTS_MAIN_URL);
    } else if (bindingResult.hasErrors()) {
        mav = new ModelAndView("viewPentahoReport");
        initFormBean(pentahoReportFormBean, reportId, request);
    } else {
        Integer outputType = Integer.parseInt(pentahoReportFormBean.getOutputType());
        Map<String, AbstractPentahoParameter> reportParams = pentahoReportFormBean.getAllParameteres();
        PentahoReport report = this.pentahoReportsService.getReport(reportId, outputType, reportParams);
        if (report.isInError()) {
            for (PentahoValidationError error : report.getErrors()) {
                addErrorToBindingResult(error, bindingResult);
            }
            mav = new ModelAndView("viewPentahoReport");
            initFormBean(pentahoReportFormBean, reportId, request);
        } else {
            if (report.getContentType().equalsIgnoreCase("text/html")) {
                HashMap<String, String> modelMap = new HashMap<String, String>();
                modelMap.put("reportContent", new String(report.getContent()));
                mav = new ModelAndView("viewHtmlReport", modelMap);
            } else {
                response.setHeader("Content-Disposition", "attachment; filename=\"" + report.getFilename() + "\"");
                response.setContentType(report.getContentType());
                response.setContentLength(report.getContentSize());
                response.getOutputStream().write(report.getContent());
            }
        }
    }
    return mav;
}
Also used : PentahoReport(org.mifos.reports.pentaho.PentahoReport) AccessDeniedException(org.springframework.security.access.AccessDeniedException) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) PentahoValidationError(org.mifos.reports.pentaho.PentahoValidationError) HashMap(java.util.HashMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with PentahoReport

use of org.mifos.reports.pentaho.PentahoReport in project head by mifos.

the class AdminDocumentController method executeAdminDocument.

@RequestMapping(value = "/executeAdminDocument.ftl", method = RequestMethod.GET)
public ModelAndView executeAdminDocument(final HttpServletRequest request, HttpServletResponse response, @RequestParam Integer adminDocumentId, @RequestParam String entityId, @RequestParam Integer outputTypeId) throws IOException {
    ModelAndView mav = null;
    String fileName = pentahoReportsService.getAdminReportFileName(adminDocumentId);
    if (fileName.endsWith(".rptdesign")) {
        response.sendRedirect(String.format(LEGACY_BIRT_ADMIN_DOCUMENT_LOAD_PATH, adminDocumentId.toString(), entityId));
    } else {
        Map<String, AbstractPentahoParameter> params = new HashMap<String, AbstractPentahoParameter>();
        PentahoInputParameter entityIdParameter = new PentahoInputParameter();
        entityIdParameter.setParamName("entity_id");
        entityIdParameter.setValue(entityId);
        params.put("entity_id", entityIdParameter);
        PentahoReport report = pentahoReportsService.getAdminReport(adminDocumentId, outputTypeId, params);
        if (!outputTypeId.equals(PENTAHO_OUTPUT_TYPE_HTML_ID)) {
            response.setHeader("Content-Disposition", "attachment; filename=\"" + report.getFilename() + "\"");
        }
        response.setContentType(report.getContentType());
        response.setContentLength(report.getContentSize());
        response.getOutputStream().write(report.getContent());
    }
    return mav;
}
Also used : PentahoReport(org.mifos.reports.pentaho.PentahoReport) AbstractPentahoParameter(org.mifos.reports.pentaho.params.AbstractPentahoParameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PentahoInputParameter(org.mifos.reports.pentaho.params.PentahoInputParameter) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with PentahoReport

use of org.mifos.reports.pentaho.PentahoReport in project head by mifos.

the class PentahoReportsServiceIntegrationTest method testGetAdminReport.

@Test
public void testGetAdminReport() throws Exception {
    String adminDocumentUploadPath = viewOrganizationSettingsServiceFacade.getAdminDocumentStorageDirectory();
    String adminDocumentPath = "IntegrationTest_Loan_template.prpt";
    File adminDocumentSrc = new File(this.getClass().getResource("/Loan_template.prpt").toString().replace("file:", ""));
    File adminDocumentDst = new File(adminDocumentUploadPath + "/" + adminDocumentPath);
    try {
        FileUtils.copyFile(adminDocumentSrc, adminDocumentDst);
        AdminDocumentBO adminDocumentBO = new AdminDocumentBO();
        adminDocumentBO.setAdmindocId(Short.valueOf("1"));
        adminDocumentBO.setAdminDocumentName("IntegrationTest_Loan_template");
        adminDocumentBO.setIsActive(Short.valueOf("1"));
        adminDocumentBO.setAdminDocumentIdentifier(adminDocumentPath);
        AdminDocAccStateMixBO adminDocAccStateMixBO = new AdminDocAccStateMixBO();
        AccountStateEntity accountStateEntity = new AccountStateEntity(AccountState.LOAN_APPROVED);
        adminDocAccStateMixBO.setAccountStateID(accountStateEntity);
        adminDocAccStateMixBO.setAdminDocumentID(adminDocumentBO);
        legacyAdminDocumentDao.createOrUpdate(adminDocumentBO);
        Map<String, AbstractPentahoParameter> params = new HashMap<String, AbstractPentahoParameter>();
        PentahoInputParameter entityIdParameter = new PentahoInputParameter();
        entityIdParameter.setParamName("entity_id");
        entityIdParameter.setValue("000100000000002");
        params.put("entity_id", entityIdParameter);
        Integer adminDocId = Integer.parseInt(adminDocumentBO.getAdmindocId().toString());
        for (PentahoOutputType outputType : PentahoOutputType.values()) {
            PentahoReport report = pentahoReportsServiceImpl.getAdminReport(adminDocId, outputType.getId(), params);
            Assert.assertTrue(report.getFileExtension() == outputType.getFileExtension());
            Assert.assertTrue(report.getContentType() == outputType.getContentType());
            Assert.assertTrue(report.getErrors().isEmpty());
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (adminDocumentDst.exists()) {
            adminDocumentDst.delete();
        }
    }
}
Also used : PentahoReport(org.mifos.reports.pentaho.PentahoReport) AbstractPentahoParameter(org.mifos.reports.pentaho.params.AbstractPentahoParameter) HashMap(java.util.HashMap) AdminDocAccStateMixBO(org.mifos.reports.admindocuments.business.AdminDocAccStateMixBO) AccountStateEntity(org.mifos.accounts.business.AccountStateEntity) PentahoOutputType(org.mifos.reports.pentaho.util.PentahoOutputType) AdminDocumentBO(org.mifos.reports.admindocuments.business.AdminDocumentBO) PentahoInputParameter(org.mifos.reports.pentaho.params.PentahoInputParameter) File(java.io.File) Test(org.junit.Test)

Example 5 with PentahoReport

use of org.mifos.reports.pentaho.PentahoReport in project head by mifos.

the class PentahoReportsServiceImpl method getAdminReport.

@Override
public PentahoReport getAdminReport(Integer adminReportId, Integer outputTypeId, Map<String, AbstractPentahoParameter> params) {
    ByteArrayOutputStream baos = null;
    try {
        // load report definition
        ResourceManager manager = new ResourceManager();
        manager.registerDefaults();
        String reportName = legacyAdminDocumentDao.getAdminDocumentById(adminReportId.shortValue()).getAdminDocumentName();
        String filename = legacyAdminDocumentDao.getAdminDocumentById(adminReportId.shortValue()).getAdminDocumentIdentifier();
        File file = new File(viewOrganizationSettingsServiceFacade.getAdminDocumentStorageDirectory(), filename);
        StringBuilder path = new StringBuilder("file:");
        path.append(file.getAbsolutePath());
        URL url = new URL(path.toString());
        Resource res = manager.createDirectly(url, MasterReport.class);
        MasterReport report = (MasterReport) res.getResource();
        PentahoReport result = new PentahoReport();
        List<PentahoValidationError> errors = new ArrayList<PentahoValidationError>();
        try {
            addParametersToReport(report, params);
            validate(report, errors);
        } catch (ReflectionException ex) {
            errors.add(new PentahoValidationError(ex.getMessage()));
        }
        result.setErrors(errors);
        if (errors.isEmpty()) {
            baos = new ByteArrayOutputStream();
            PentahoOutputType outputType = PentahoOutputType.findById(outputTypeId);
            switch(outputType) {
                case XLS:
                    ExcelReportUtil.createXLS(report, baos);
                    break;
                case RTF:
                    RTFReportUtil.createRTF(report, baos);
                    break;
                case HTML:
                    HtmlReportUtil.createStreamHTML(report, baos);
                    break;
                case CSV:
                    CSVReportUtil.createCSV(report, baos, "UTF-8");
                    break;
                case XML:
                    XmlTableReportUtil.createFlowXML(report, baos);
                    break;
                default:
                    // PDF
                    PdfReportUtil.createPDF(report, baos);
                    break;
            }
            result.setContentType(outputType.getContentType());
            result.setFileExtension(outputType.getFileExtension());
            result.setName(reportName);
            result.setContent(baos.toByteArray());
        }
        return result;
    } catch (Exception e) {
        throw new MifosRuntimeException(e);
    } finally {
        closeStream(baos);
    }
}
Also used : PentahoReport(org.mifos.reports.pentaho.PentahoReport) ReflectionException(org.mifos.reports.pentaho.util.ReflectionException) Resource(org.pentaho.reporting.libraries.resourceloader.Resource) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceManager(org.pentaho.reporting.libraries.resourceloader.ResourceManager) URL(java.net.URL) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ReflectionException(org.mifos.reports.pentaho.util.ReflectionException) ReportProcessingException(org.pentaho.reporting.engine.classic.core.ReportProcessingException) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) PentahoOutputType(org.mifos.reports.pentaho.util.PentahoOutputType) MasterReport(org.pentaho.reporting.engine.classic.core.MasterReport) PentahoValidationError(org.mifos.reports.pentaho.PentahoValidationError) File(java.io.File) MifosRuntimeException(org.mifos.core.MifosRuntimeException)

Aggregations

PentahoReport (org.mifos.reports.pentaho.PentahoReport)5 HashMap (java.util.HashMap)3 PentahoValidationError (org.mifos.reports.pentaho.PentahoValidationError)3 PentahoOutputType (org.mifos.reports.pentaho.util.PentahoOutputType)3 AccessDeniedException (org.springframework.security.access.AccessDeniedException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 MifosRuntimeException (org.mifos.core.MifosRuntimeException)2 PersistenceException (org.mifos.framework.exceptions.PersistenceException)2 AbstractPentahoParameter (org.mifos.reports.pentaho.params.AbstractPentahoParameter)2 PentahoInputParameter (org.mifos.reports.pentaho.params.PentahoInputParameter)2 ReflectionException (org.mifos.reports.pentaho.util.ReflectionException)2 MasterReport (org.pentaho.reporting.engine.classic.core.MasterReport)2 ReportProcessingException (org.pentaho.reporting.engine.classic.core.ReportProcessingException)2 Resource (org.pentaho.reporting.libraries.resourceloader.Resource)2 ResourceManager (org.pentaho.reporting.libraries.resourceloader.ResourceManager)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2