I'm trying to generate a simple report with three filters: datainicial, datefinal, and reservoir. The problem is that clicking the button to generate the report nothing happens, and I do not get any messages on the console. I have already put prints and I saw that the method is being called. I also noticed that when I remove the filters from the method my report in pdf is generated smoothly. Here are the codes. xhtml:
<h:form id="formGrid">
Data Inicial: <p:calendar id="popup" value="#{relatorioHistoricoMB.dataInicial}" />
Data Final: <p:calendar id="popup2" value="#{relatorioHistoricoMB.dataFinal}" />
<p:selectOneMenu value="#{relatorioHistoricoMB.reservatorio.idReservatorio}"
style="width:170px" required="true"
requiredMessage="Selecione algum.">
<f:selectItem itemLabel="Selecione o Reservatório" itemValue="0" />
<f:selectItems value="#{relatorioHistoricoMB.listaReservatorio}"
var="reservatorio" itemLabel="#{reservatorio.nomeReservatorio}"
itemValue="#{reservatorio.idReservatorio}" />
</p:selectOneMenu>
<p:commandButton value="Gerar relatório" update="formGrid"
actionListener="#{relatorioHistoricoMB.executar}" />
<p:dataTable id="Grid" var="historico"
value="#{HistoricoBean.listaHistorico}"
selection="#{HistoricoBean.historico}" selectionMode="single"
rowKey="#{historico.idHistorico}" paginator="true" rows="5">
<f:facet name="header">
<h:outputText value="Historico" />
</f:facet>
<p:column headerText="Reservatorio">
<f:facet name="header">
<h:outputText value="Reservatorio" />
</f:facet>
<h:outputText value="#{historico.reservatorio.nomeReservatorio}" />
</p:column>
<p:column headerText="Bomba">
<f:facet name="header">
<h:outputText value="Bomba" />
</f:facet>
<h:outputText value="#{historico.bomba.nomeBomba}" />
</p:column>
<p:column headerText="Data">
<f:facet name="header">
<h:outputText value="Data" />
</f:facet>
<h:outputText value="#{historico.dataHistorico}" />
</p:column>
<p:column headerText="Evento">
<f:facet name="header">
<h:outputText value="Evento" />
</f:facet>
<h:outputText value="#{historico.novoStatusHistorico}" />
</p:column>
</p:dataTable>
</h:form>
</sec:ifAnyGranted>
</div>
</div>
</ui:define>
Bean:
@ManagedBean(name = "relatorioHistoricoMB")
@SessionScoped
public class RelatorioHistoricoMB {
private Date dataInicial;
private Date dataFinal;
private Reservatorio reservatorio = new Reservatorio();
private HistoricoCrudAnnotations historicoDAO = new HistoricoCrudAnnotations();
private List<Reservatorio> listaReservatorio = new ArrayList<>();
private ReservatorioCrudAnnotations reservatorioDAO = new ReservatorioCrudAnnotations();
public void executar() throws Exception {
List<Historico> ListaHistorico = new ArrayList<Historico>();
HistoricoCrudAnnotations HistoricoDAO = new HistoricoCrudAnnotations();
ListaHistorico = HistoricoDAO.listar();
int x = ListaHistorico.size();
Historico2[] ListaHistorico2 = new Historico2[x];
List<Historico2> Historico = new ArrayList<Historico2>();
GregorianCalendar dataCal = new GregorianCalendar();
dataCal.setTime(dataInicial);
int mesInicial = (dataCal.get(Calendar.MONTH))+1;
dataCal.setTime(dataFinal);
int mesFinal = (dataCal.get(Calendar.MONTH))+1;
System.out.println("MÉTODO EXECUTAR");
for (int i = 0; i < ListaHistorico.size(); i++) {
String mesBanco = historicoDAO.listar().get(i).getDataHistorico()
.substring(3, 5);
int mesBancoi = Integer.parseInt(mesBanco);
System.out.println("Mês inicial: "+mesInicial);
System.out.println("Mês Final: "+mesFinal);
System.out.println("Mês Banco: "+mesBancoi);
System.out.println("Reservatório: "+reservatorio.getIdReservatorio());
System.out.println("Reservatório da lista: "+ListaHistorico.get(i).getReservatorio().getIdReservatorio());
if ((mesBancoi >= mesInicial)
&& (mesBancoi <= mesFinal)
&& (reservatorio.getIdReservatorio() == ListaHistorico
.get(i).getReservatorio().getIdReservatorio()))
{
ListaHistorico2[i] = new Historico2();
ListaHistorico2[i].setIdHistorico(ListaHistorico.get(i)
.getIdHistorico());
ListaHistorico2[i].setNomeBomba(ListaHistorico.get(i)
.getBomba().getNomeBomba());
ListaHistorico2[i].setNomeReservatorio(ListaHistorico.get(i)
.getReservatorio().getNomeReservatorio());
ListaHistorico2[i].setNovoStatusHistorico(ListaHistorico.get(i)
.getNovoStatusHistorico());
ListaHistorico2[i].setDataHistorico(ListaHistorico.get(i)
.getDataHistorico());
ListaHistorico2[i].setObsReservatorio(ListaHistorico.get(i)
.getReservatorio().getObsReservatorio());
Historico.add(ListaHistorico2[i]);
}
}
System.out.println("HISTORICO"+Historico.get(0).getNovoStatusHistorico());
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(
Historico, false);
Map<String, Object> parameterMap = new HashMap<String, Object>();
Image logo = new ImageIcon(getClass().getResource("Logo.png"))
.getImage();
parameterMap.put("logo", logo);
ReportUtil.executarRelatorio("/WEB-INF/jasper/Rhistorico2.jasper",
parameterMap, "RelatorioHistorico", beanCollectionDataSource);
}
Bean: @ManagedBean (name="MBReportUtil") @SessionScoped
public class ReportUtil {
private static String ERRO_MENSAGEM_REPORT = "Um erro ocorreu quando o relatório estava sendo executado.";
//executa o relatório através de um JRBeanCollectionDataSource
public static void executarRelatorio(String caminhoRelatorio, Map<String, Object> parametros, String nomeRel, JRBeanCollectionDataSource fonteDados) throws Exception{
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
//pega o caminho do arquivo .jasper da aplicação
InputStream reportStream = (InputStream) context.getExternalContext().getResourceAsStream(caminhoRelatorio);
response.setHeader("Content-Disposition", "attachment; filename="+ nomeRel +".pdf");
response.setContentType("application/download");
response.setHeader("Pragma", "no-cache");
try {
System.out.println("------Inicio Try!-------");
ServletOutputStream servletOutputStream = response.getOutputStream();
//envia parametros para o relatório
if (parametros == null){
parametros = new HashMap<String, Object>();
}
//envia para o navegador o PDF gerado
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parametros, fonteDados);
servletOutputStream.flush();
servletOutputStream.close();
System.out.println("-----Passou Flush e Close!-----");
} catch (JRException e) {
e.printStackTrace();
try {
throw new Exception(ERRO_MENSAGEM_REPORT);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
try {
throw new Exception(ERRO_MENSAGEM_REPORT);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
context.responseComplete();
}
}
//executa o relatório através de uma Conexao
public static void executarRelatorioConexao(String caminhoRelatorio, Map<String, Object> parametros, String nomeRel, java.sql.Connection connection) throws Exception{
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
//pega o caminho do arquivo .jasper da aplicação
InputStream reportStream = (InputStream) context.getExternalContext().getResourceAsStream(caminhoRelatorio);
response.setHeader("Content-Disposition", "attachment; filename="+ nomeRel +".pdf");
response.setContentType("application/download");
response.setHeader("Pragma", "no-cache");
try {
System.out.println("------Inicio Try!-------");
ServletOutputStream servletOutputStream = response.getOutputStream();
//envia parametros para o relatório
if (parametros == null){
parametros = new HashMap<String, Object>();
}
//envia para o navegador o PDF gerado
JasperRunManager.runReportToPdf(caminhoRelatorio, parametros, connection);
servletOutputStream.flush();
servletOutputStream.close();
System.out.println("-----Passou Flush e Close!-----");
} catch (JRException e) {
e.printStackTrace();
try {
throw new Exception(ERRO_MENSAGEM_REPORT);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
try {
throw new Exception(ERRO_MENSAGEM_REPORT);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
context.responseComplete();
}
}
}