use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.
the class JasperTemplateService method saveTemplate.
/**
* Saves a template with given name.
* If template already exists, only description and required rights are updated.
*
* @param file report file
* @param name name of report
* @param description report's description
* @return saved report template
*/
public JasperTemplate saveTemplate(MultipartFile file, String name, String description) throws ReportingException {
JasperTemplate jasperTemplate = jasperTemplateRepository.findByName(name);
if (jasperTemplate == null) {
jasperTemplate = JasperTemplate.builder().name(name).description(description).build();
} else {
jasperTemplate.setDescription(description);
}
validateFileAndSaveTemplate(jasperTemplate, file);
return jasperTemplate;
}
use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.
the class JasperTemplateController method createJasperReportTemplate.
/**
* Adding report templates with ".jrxml" format to database.
*
* @param file File in ".jrxml" format to upload
* @param name Name of file in database
* @param description Description of the file
*/
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void createJasperReportTemplate(@RequestPart("file") MultipartFile file, String name, String description) throws ReportingException {
LOGGER.debug("Saving template with name: " + name);
JasperTemplate template = jasperTemplateService.saveTemplate(file, name, description);
LOGGER.debug("Saved template with id: " + template.getId());
}
use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.
the class JasperTemplateController method generateReport.
/**
* Generate a report based on the template, the format and the request parameters.
*
* @param request request (to get the request parameters)
* @param templateId report template ID
* @param format report format to generate, default is PDF
* @return the generated report
*/
@RequestMapping(value = "/{id}/{format}", method = RequestMethod.GET)
@ResponseBody
public ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") UUID templateId, @PathVariable("format") String format) throws JasperReportViewException {
JasperTemplate template = jasperTemplateRepository.findOne(templateId);
if (template == null) {
throw new EntityNotFoundException(ERROR_JASPER_TEMPLATE_NOT_FOUND, templateId);
}
Map<String, Object> map = jasperTemplateService.mapRequestParametersToTemplate(request, template);
map.put("format", format);
JasperReportsMultiFormatView jasperView = jasperReportsViewService.getJasperReportsView(template, request);
String fileName = template.getName().replaceAll("\\s+", "_");
String contentDisposition = "inline; filename=" + fileName + "." + format;
jasperView.getContentDispositionMappings().setProperty(format, contentDisposition.toLowerCase(Locale.ENGLISH));
return new ModelAndView(jasperView, map);
}
use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.
the class JasperTemplateService method validateFileAndSaveTemplate.
/**
* Validate ".jrmxl" file and insert if template not exist. If this name of template already
* exist, remove older template and insert new.
*/
void validateFileAndSaveTemplate(JasperTemplate jasperTemplate, MultipartFile file) throws ReportingException {
JasperTemplate templateTmp = jasperTemplateRepository.findByName(jasperTemplate.getName());
if (templateTmp != null) {
jasperTemplateRepository.delete(templateTmp.getId());
}
validateFileAndSetData(jasperTemplate, file);
saveWithParameters(jasperTemplate);
}
use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.
the class JasperTemplateRepositoryIntegrationTest method shouldBindParametersToTemplateOnSave.
@Test
public void shouldBindParametersToTemplateOnSave() {
// given
JasperTemplateParameter templateParameter = new JasperTemplateParameter();
templateParameter.setName("parameter");
templateParameter.setRequired(true);
JasperTemplate template = generateInstance();
template.setTemplateParameters(Collections.singletonList(templateParameter));
// when
JasperTemplate result = jasperTemplateRepository.save(template);
// then
assertEquals(templateParameter.getTemplate().getId(), result.getId());
}
Aggregations