Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package br.com.ateliens.controller; import br.com.ateliens.util.jsf.FacesUtil; import br.com.ateliens.util.report.ExecutorRelatorio; import java.io.Serializable; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.faces.context.FacesContext; import javax.faces.view.ViewScoped; import javax.inject.Inject; import javax.inject.Named; import javax.persistence.EntityManager; import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.NotNull; import org.hibernate.Session; @Named @ViewScoped public class RelatorioServicosBean implements Serializable { private static final long serialVersionUID = 1L; private Date dataInicio; private Date dataFim; @Inject private FacesContext facesContext; @Inject private HttpServletResponse response; @Inject private EntityManager manager; public void emitir() { Map<String, Object> parametros = new HashMap<>(); parametros.put("data_inicio", this.dataInicio); parametros.put("data_fim", this.dataFim); ExecutorRelatorio executor = new ExecutorRelatorio("/relatorios/servicosrealizados.jasper", response, parametros, "Relatrios de Servios.pdf"); Session session = manager.unwrap(Session.class); session.doWork(executor); if (executor.isRelatorioGerado()) { facesContext.responseComplete(); } else { FacesUtil.addErrorMessage("A execuo do relatrio no retornou dados."); } } @NotNull public Date getDataInicio() { return dataInicio; } public void setDataInicio(Date dataInicio) { this.dataInicio = dataInicio; } @NotNull public Date getDataFim() { return dataFim; } public void setDataFim(Date dataFim) { this.dataFim = dataFim; } }