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;
}
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());
}
}
Aggregations