Search in sources :

Example 1 with ReportingException

use of org.motechproject.mots.exception.ReportingException in project mots by motech-implementations.

the class JasperTemplateService method createParameter.

/**
 * Create new report parameter of report which is not defined in Jasper system.
 */
private JasperTemplateParameter createParameter(JRParameter jrParameter) throws ReportingException {
    String displayName = jrParameter.getPropertiesMap().getProperty("displayName");
    if (isBlank(displayName)) {
        throw new ReportingException(ERROR_REPORTING_PARAMETER_MISSING, "displayName");
    }
    String dataType = jrParameter.getValueClassName();
    if (isNotBlank(dataType)) {
        try {
            Class.forName(dataType);
        } catch (ClassNotFoundException err) {
            throw new ReportingException(err, ERROR_REPORTING_PARAMETER_INCORRECT_TYPE, jrParameter.getName(), dataType);
        }
    }
    // Set parameters.
    JasperTemplateParameter jasperTemplateParameter = new JasperTemplateParameter();
    jasperTemplateParameter.setName(jrParameter.getName());
    jasperTemplateParameter.setDisplayName(displayName);
    jasperTemplateParameter.setDescription(jrParameter.getDescription());
    jasperTemplateParameter.setDataType(dataType);
    jasperTemplateParameter.setSelectExpression(jrParameter.getPropertiesMap().getProperty("selectExpression"));
    jasperTemplateParameter.setSelectProperty(jrParameter.getPropertiesMap().getProperty("selectProperty"));
    jasperTemplateParameter.setDisplayProperty(jrParameter.getPropertiesMap().getProperty("displayProperty"));
    String required = jrParameter.getPropertiesMap().getProperty("required");
    if (required != null) {
        jasperTemplateParameter.setRequired(Boolean.parseBoolean(jrParameter.getPropertiesMap().getProperty("required")));
    }
    if (jrParameter.getDefaultValueExpression() != null) {
        jasperTemplateParameter.setDefaultValue(jrParameter.getDefaultValueExpression().getText().replace("\"", "").replace("\'", ""));
    }
    jasperTemplateParameter.setOptions(extractOptions(jrParameter));
    return jasperTemplateParameter;
}
Also used : ReportingException(org.motechproject.mots.exception.ReportingException) JasperTemplateParameter(org.motechproject.mots.domain.JasperTemplateParameter)

Example 2 with ReportingException

use of org.motechproject.mots.exception.ReportingException in project mots by motech-implementations.

the class JasperTemplateService method validateFileAndSetData.

/**
 * Validate ".jrxml" report file with JasperCompileManager. If report is valid create additional
 * report parameters. Save additional report parameters as JasperTemplateParameter list. Save
 * report file as ".jasper" in byte array in Template class. If report is not valid throw
 * exception.
 */
private void validateFileAndSetData(JasperTemplate jasperTemplate, MultipartFile file) throws ReportingException {
    ReportingValidationHelper.throwIfFileIsNull(file);
    ReportingValidationHelper.throwIfIncorrectFileType(file, ALLOWED_FILETYPES);
    ReportingValidationHelper.throwIfFileIsEmpty(file);
    try {
        JasperReport report = JasperCompileManager.compileReport(file.getInputStream());
        String reportType = report.getProperty(REPORT_TYPE_PROPERTY);
        if (reportType != null) {
            jasperTemplate.setType(reportType);
        }
        String formats = report.getProperty(SUPPORTED_FORMATS_PROPERTY);
        if (formats != null) {
            jasperTemplate.setSupportedFormats(extractListProperties(formats));
        }
        JRParameter[] jrParameters = report.getParameters();
        if (jrParameters != null && jrParameters.length > 0) {
            processJrParameters(jasperTemplate, jrParameters);
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(report);
        jasperTemplate.setData(bos.toByteArray());
    } catch (JRException ex) {
        throw new ReportingException(ex, ERROR_REPORTING_FILE_INVALID);
    } catch (IOException ex) {
        throw new ReportingException(ex, ex.getMessage());
    }
}
Also used : JRParameter(net.sf.jasperreports.engine.JRParameter) JRException(net.sf.jasperreports.engine.JRException) ReportingException(org.motechproject.mots.exception.ReportingException) JasperReport(net.sf.jasperreports.engine.JasperReport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ReportingException (org.motechproject.mots.exception.ReportingException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 JRException (net.sf.jasperreports.engine.JRException)1 JRParameter (net.sf.jasperreports.engine.JRParameter)1 JasperReport (net.sf.jasperreports.engine.JasperReport)1 JasperTemplateParameter (org.motechproject.mots.domain.JasperTemplateParameter)1