package br.com.centralit.controller; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Collection; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.xml.JRXmlLoader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import br.com.centralit.api.model.EventoAditivo; import br.com.centralit.api.service.DominioService; import br.com.centralit.api.service.EventoAditivoService; import br.com.centralit.api.service.InternacionalizacaoService; import br.com.centralit.api.viewHelper.ViewsContrato; import br.com.centralit.framework.controller.GenericController; import br.com.centralit.framework.controller.ReportController; import br.com.centralit.framework.exception.BusinessException; import br.com.centralit.framework.json.ResponseBodyWrapper; import br.com.centralit.framework.model.Dominio; import br.com.centralit.framework.model.Usuario; import br.com.centralit.framework.util.UtilDataBase; /** * *

* *

Company: Central IT - Governança Corporativa -

* *

Title:

* *

Description:

* *

Iniciativa(s): NUMERO_INICIATIVA

* *

Regra(s) de negócio: NUMERO_REGRA_DE_NEGOCIO

* * @since 15/03/2016 - 18:30:22 * * @version 1.0.0 * * @author geovane.filho * */ @Controller @RequestMapping("/rest/eventoAditivo") public class EventoAditivoController extends GenericController{ private EventoAditivoService eventoAditivoService; @Autowired private DominioService dominioService; @Autowired private InternacionalizacaoService internacionalizacaoService; @Autowired private ReportController reportController; public EventoAditivoController() { super(); } @Autowired public EventoAditivoController(EventoAditivoService eventoAditivoService) { super(eventoAditivoService); this.eventoAditivoService = eventoAditivoService; } @Override public Class getEditView() { return ViewsContrato.EventoAditivoEditView.class; } @RequestMapping(value = "/findAllByContrato", method = RequestMethod.POST) @ResponseBody public ResponseBodyWrapper findAllByContrato(@RequestBody Long contratoId) { Collection aditivos = this.eventoAditivoService.findAllByContrato(contratoId); ResponseBodyWrapper responseBody = new ResponseBodyWrapper(aditivos, ViewsContrato.EventoAditivoEditView.class); return responseBody; } @RequestMapping(method = RequestMethod.GET, value = "/gerarAditivo") @ResponseBody public void generatePdfRMB(@RequestParam(value="idAditivo") Long idAditivo, @RequestParam(value="download") boolean download, HttpServletResponse response, HttpServletRequest request) throws SQLException, JRException, IOException { Dominio idioma = this.dominioService.findByChaveAndCodigo(Dominio.TIPO_IDIOMA, Dominio.TIPO_IDIOMA_PT_BR_CODIGO); Usuario user = (Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Connection conn = null; ByteArrayOutputStream baos = null; InputStream input = null; InputStream inputSub = null; try { conn = reportController.getConnection(); input = this.getClass().getResourceAsStream("/reports/templateRetrato.jrxml"); inputSub = this.getClass().getResourceAsStream("/reports/eventoAditivo.jrxml"); JasperDesign jasperDesign = JRXmlLoader.load(input); JasperDesign jasperDesignSub = JRXmlLoader.load(inputSub); JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); JasperReport jasperReportSub = JasperCompileManager.compileReport(jasperDesignSub); Map subReportParameters = new HashMap(); subReportParameters.put("ADITIVO_ID", idAditivo); subReportParameters.put("REPORT_CONNECTION", conn); String titulo = this.internacionalizacaoService.getTranslate("CONTRATOS.LABEL.TITULO_RELATORIO_ADITIVO", idioma); Map parameters = new HashMap(); parameters.put("ORGANIZACAO_ID", user.getOrganizacao().getId()); parameters.put("TITULO", titulo); parameters.put("SUBREPORT_DIR", jasperReportSub); parameters.put("SUBREPORT_PARAMETERS_MAP", subReportParameters); parameters.put("REPORT_CONNECTION", conn); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn); // Responsavel por verificar a presenca de dados no relatorio reportController.gerarRelatorio(download, "pdf", jasperPrint, response, baos, titulo); } catch (JRException jre) { throw new RuntimeException(jre); } catch (Exception e) { throw new BusinessException(); }finally { UtilDataBase.closeResources(conn, input, inputSub, baos); } } }