Search in sources :

Example 6 with JasperTemplate

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;
}
Also used : JasperTemplate(org.motechproject.mots.domain.JasperTemplate)

Example 7 with 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());
}
Also used : JasperTemplate(org.motechproject.mots.domain.JasperTemplate) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with JasperTemplate

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);
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) JasperReportsMultiFormatView(org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with JasperTemplate

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);
}
Also used : JasperTemplate(org.motechproject.mots.domain.JasperTemplate)

Example 10 with 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());
}
Also used : JasperTemplateParameter(org.motechproject.mots.domain.JasperTemplateParameter) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) Test(org.junit.Test)

Aggregations

JasperTemplate (org.motechproject.mots.domain.JasperTemplate)18 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)7 Matchers.anyString (org.mockito.Matchers.anyString)4 MultipartFile (org.springframework.web.multipart.MultipartFile)4 InputStream (java.io.InputStream)3 JRParameter (net.sf.jasperreports.engine.JRParameter)3 JRPropertiesMap (net.sf.jasperreports.engine.JRPropertiesMap)3 JasperReport (net.sf.jasperreports.engine.JasperReport)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 JRExpression (net.sf.jasperreports.engine.JRExpression)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 UUID (java.util.UUID)1 JasperTemplateParameter (org.motechproject.mots.domain.JasperTemplateParameter)1 EntityNotFoundException (org.motechproject.mots.exception.EntityNotFoundException)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1