Search in sources :

Example 6 with JRException

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

the class ReportInfo method processReport.

protected File processReport(String reportFile) {
    log.finest("ReportInfo.processReport - " + reportFile);
    File JasperDesignFile = new File(reportFile);
    String JasperReportFile = reportFile.replaceAll(".jrxml", ".jasper").replaceAll(".xml", ".jasper");
    File jasperFile = new File(JasperReportFile);
    // Marco LOMBARDO: this is the local one, there is the same 
    // field at class level. There was a bug on it about subreports.
    JasperReport jasperReport = null;
    if (jasperFile.exists()) {
        // test time
        if (JasperDesignFile.lastModified() <= jasperFile.lastModified()) {
            try {
                // Marco LOMBARDO: should refer to local.
                jasperReport = (JasperReport) JRLoader.loadObject(jasperFile.getAbsolutePath());
            } catch (JRException e) {
                // Marco LOMBARDO: on error reset global report.
                this.jasperReport = null;
                log.saveError("ReportServer.processReport: Can not load report - ", e);
            }
        } else {
            // Marco LOMBARDO: should refer to local.
            jasperReport = compileReport(JasperDesignFile, jasperFile);
        }
    } else {
        // create new jasper file
        // Marco LOMBARDO: should refer to local.
        jasperReport = compileReport(JasperDesignFile, jasperFile);
    }
    if (hasError)
        return null;
    // Marco LOMBARDO: this should happen only after compile the main report.
    if (this.jasperReport == null)
        this.jasperReport = jasperReport;
    if (// Marco LOMBARDO: should refer to local.
    jasperReport != null) {
        String[] extension = { ".xml", "jrxml" };
        File[] subreports = JasperDesignFile.getParentFile().listFiles(new FileFilter(JasperReportFile.replaceAll(".jasper", "") + "Subreport", JasperDesignFile.getParentFile(), extension));
        for (int i = 0; i < subreports.length; i++) {
            log.finest("The subreport file @ " + subreports[i].getAbsolutePath());
            File sub = processReport(subreports[i].getAbsolutePath());
            String subName = sub.getName();
            int pos = sub.getName().indexOf('.');
            if (pos != -1)
                subName = subName.substring(0, pos);
            subReport.put(subName, sub.getAbsolutePath());
            this.hasSubReport = true;
        }
    }
    if (hasError)
        return null;
    return jasperFile;
}
Also used : JRException(net.sf.jasperreports.engine.JRException) JasperReport(net.sf.jasperreports.engine.JasperReport) File(java.io.File)

Example 7 with JRException

use of net.sf.jasperreports.engine.JRException 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 JRException

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

the class MidPointAbstractDataSource method getFieldValue.

@Override
public Object getFieldValue(JRField jrField) throws JRException {
    // TODO Auto-generated method stub
    String fieldName = jrField.getName();
    if (fieldName.equals("oid")) {
        return currentObject.getOid();
    }
    Item i = currentObject.findItem(new QName(fieldName));
    if (i == null) {
        return null;
    }
    if (i instanceof PrismProperty) {
        if (i.isSingleValue()) {
            return ((PrismProperty) i).getRealValue();
        }
        return ((PrismProperty) i).getRealValues();
    } else if (i instanceof PrismReference) {
        if (i.isSingleValue()) {
            return ((PrismReference) i).getValue().asReferencable();
        }
        List<Referencable> refs = new ArrayList<Referencable>();
        for (PrismReferenceValue refVal : ((PrismReference) i).getValues()) {
            refs.add(refVal.asReferencable());
        }
        return refs;
    } else if (i instanceof PrismContainer) {
        if (i.isSingleValue()) {
            return ((PrismContainer) i).getValue().asContainerable();
        }
        List<Containerable> containers = new ArrayList<Containerable>();
        for (Object pcv : i.getValues()) {
            if (pcv instanceof PrismContainerValue) {
                containers.add(((PrismContainerValue) pcv).asContainerable());
            }
        }
        return containers;
    } else
        throw new JRException("Could not get value of the fileld: " + fieldName);
}
Also used : Referencable(com.evolveum.midpoint.prism.Referencable) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) JRException(net.sf.jasperreports.engine.JRException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Item(com.evolveum.midpoint.prism.Item) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) PrismReference(com.evolveum.midpoint.prism.PrismReference) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) ArrayList(java.util.ArrayList) List(java.util.List) Containerable(com.evolveum.midpoint.prism.Containerable) PrismObject(com.evolveum.midpoint.prism.PrismObject)

Example 9 with JRException

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

the class ReportCreateTaskHandler method prepareReportParameters.

//	private JasperReport loadJasperReport(ReportType reportType) throws SchemaException{
//		
//			if (reportType.getTemplate() == null) {
//				throw new IllegalStateException("Could not create report. No jasper template defined.");
//			}
//			try	 {
//		    	 	byte[] reportTemplate = Base64.decodeBase64(reportType.getTemplate());
//		    	 	
//		    	 	InputStream inputStreamJRXML = new ByteArrayInputStream(reportTemplate);
//		    	 	JasperDesign jasperDesign = JRXmlLoader.load(inputStreamJRXML);
//		    	 	LOGGER.trace("load jasper design : {}", jasperDesign);
//				 
//				 if (reportType.getTemplateStyle() != null){
//					JRDesignReportTemplate templateStyle = new JRDesignReportTemplate(new JRDesignExpression("$P{" + PARAMETER_TEMPLATE_STYLES + "}"));
//					jasperDesign.addTemplate(templateStyle);
//					JRDesignParameter parameter = new JRDesignParameter();
//					parameter.setName(PARAMETER_TEMPLATE_STYLES);
//					parameter.setValueClass(JRTemplate.class);
//					parameter.setForPrompting(false);
//					jasperDesign.addParameter(parameter);
//				 } 
//				 JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
//				 return jasperReport;
//			 } catch (JRException ex){ 
//				 LOGGER.error("Couldn't create jasper report design {}", ex.getMessage());
//				 throw new SchemaException(ex.getMessage(), ex.getCause());
//			 }
//			 
//			 
//	}
private Map<String, Object> prepareReportParameters(ReportType reportType, OperationResult parentResult) {
    Map<String, Object> params = new HashMap<String, Object>();
    if (reportType.getTemplateStyle() != null) {
        byte[] reportTemplateStyleBase64 = reportType.getTemplateStyle();
        byte[] reportTemplateStyle = Base64.decodeBase64(reportTemplateStyleBase64);
        try {
            LOGGER.trace("Style template string {}", new String(reportTemplateStyle));
            InputStream inputStreamJRTX = new ByteArrayInputStream(reportTemplateStyle);
            JRTemplate templateStyle = JRXmlTemplateLoader.load(inputStreamJRTX);
            params.put(PARAMETER_TEMPLATE_STYLES, templateStyle);
            LOGGER.trace("Style template parameter {}", templateStyle);
        } catch (Exception ex) {
            LOGGER.error("Error create style template parameter {}", ex.getMessage());
            throw new SystemException(ex);
        }
    }
    // for our special datasource
    params.put(PARAMETER_REPORT_OID, reportType.getOid());
    params.put(PARAMETER_OPERATION_RESULT, parentResult);
    params.put(ReportService.PARAMETER_REPORT_SERVICE, reportService);
    return params;
}
Also used : SystemException(com.evolveum.midpoint.util.exception.SystemException) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JRTemplate(net.sf.jasperreports.engine.JRTemplate) PrismObject(com.evolveum.midpoint.prism.PrismObject) 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)

Example 10 with JRException

use of net.sf.jasperreports.engine.JRException in project bamboobsc by billchen198318.

the class JReportUtils method deployReport.

public static void deployReport(TbSysJreport report) throws Exception {
    String reportDeployDirName = Constants.getDeployJasperReportDir() + "/";
    File reportDeployDir = new File(reportDeployDirName);
    try {
        if (!reportDeployDir.exists()) {
            logger.warn("no exists dir, force mkdir " + reportDeployDirName);
            FileUtils.forceMkdir(reportDeployDir);
        }
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage().toString());
    }
    logger.info("REPORT-ID : " + report.getReportId());
    File reportFile = null;
    File reportZipFile = null;
    OutputStream os = null;
    try {
        String reportFileFullPath = reportDeployDirName + report.getReportId() + "/" + report.getFile();
        String reportZipFileFullPath = reportDeployDirName + report.getReportId() + ".zip";
        reportZipFile = new File(reportZipFileFullPath);
        if (reportZipFile.exists()) {
            logger.warn("delete " + reportZipFileFullPath);
            FileUtils.forceDelete(reportZipFile);
        }
        os = new FileOutputStream(reportZipFile);
        IOUtils.write(report.getContent(), os);
        os.flush();
        ZipFile zipFile = new ZipFile(reportZipFileFullPath);
        zipFile.extractAll(reportDeployDirName);
        reportFile = new File(reportFileFullPath);
        if (!reportFile.exists()) {
            logger.warn("report file is missing : " + reportFileFullPath);
            return;
        }
        if (YesNo.YES.equals(report.getIsCompile()) && report.getFile().endsWith("jrxml")) {
            logger.info("compile report...");
            File d = new File(reportDeployDirName + report.getReportId());
            String outJasper = compileReportToJasperFile(d.listFiles(), reportDeployDirName + report.getReportId() + "/");
            logger.info("out first : " + outJasper);
        }
    } catch (JRException re) {
        re.printStackTrace();
        logger.error(re.getMessage().toString());
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage().toString());
    } finally {
        if (os != null) {
            os.close();
        }
        os = null;
        reportFile = null;
        reportZipFile = null;
    }
    reportDeployDir = null;
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) JRException(net.sf.jasperreports.engine.JRException) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File)

Aggregations

JRException (net.sf.jasperreports.engine.JRException)33 JasperReport (net.sf.jasperreports.engine.JasperReport)8 File (java.io.File)7 InputStream (java.io.InputStream)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 JasperPrint (net.sf.jasperreports.engine.JasperPrint)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)5 ArrayList (java.util.ArrayList)4 JRBeanCollectionDataSource (net.sf.jasperreports.engine.data.JRBeanCollectionDataSource)4 Containerable (com.evolveum.midpoint.prism.Containerable)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 JasperDesign (net.sf.jasperreports.engine.design.JasperDesign)3 BusinessServiceException (sic.service.BusinessServiceException)3 ServiceException (sic.service.ServiceException)3 Item (com.evolveum.midpoint.prism.Item)2 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)2