View Javadoc

1   package ecar.servlet.relatorio.PPA_funcao;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.util.HashMap;
5   import java.util.Map;
6   import java.util.TreeSet;
7   
8   import javax.servlet.ServletContext;
9   import javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  
12  import net.sf.jasperreports.engine.JasperPrint;
13  import net.sf.jasperreports.engine.JasperReport;
14  import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
15  
16  import org.apache.log4j.Logger;
17  
18  import ecar.servlet.relatorio.PPA_funcao.bean.FuncaoAreaAreBean;
19  import ecar.util.jasper.JasperService;
20  import ecar.util.jasper.servlet.RelatorioServlet;
21  
22  /**
23   * Servlet responsavel por interagir com os eventos de geracao de relatorio PPA
24   * @author gabrielsolana
25   *
26   */
27  public class RelatorioPPAFuncaoServlet extends RelatorioServlet {
28  	
29  
30  	private static final long serialVersionUID = -2928712623369347910L;
31  	
32  	private Logger logger = Logger.getLogger(this.getClass());
33  
34  	public RelatorioPPAFuncaoServlet(){
35  		super();
36  	}
37  	
38  	public void doRelatorioGet(HttpServletRequest request, HttpServletResponse response) {
39  		doRelatorioPost(request,response);
40  	}
41  
42  	public void doRelatorioPost(HttpServletRequest request, HttpServletResponse response) {
43  
44  		logger.info("Gerando relatorio PPA Por Funcao...");
45  		
46  		final ServletContext context = getServletContext();
47  
48  		byte[] bytes = null;
49  		Map parametros = new HashMap();
50  		try {
51  
52  			String titulo = request.getParameter("titulo");
53  			String pagina = request.getParameter("pagina");
54  			String data = request.getParameter("data");
55  			
56  			Integer paginaInt = null;
57  			
58  			try {
59  				if( pagina ==null || pagina.equals("") ){
60  					paginaInt = Integer.valueOf(1);
61  				}else{
62  					paginaInt = Integer.valueOf( pagina );
63  				}
64  			} catch (Exception e) {
65  				paginaInt = Integer.valueOf( 1 );
66  			}
67  			
68  			try {
69  				if( data ==null || data.equals("") ){
70  					data = "";
71  				}
72  			} catch (Exception e) {
73  				data = "";
74  			}			
75  			
76  			try {
77  				if( titulo ==null || "".equals( titulo ) ){
78  					titulo = "ESTADO DO PARANÁ - PLANO PLURIANUAL 2008/2011 - Demonstrativo por Função / Subfunção";	
79  				}
80  			} catch (Exception e) {
81  				titulo = "ESTADO DO PARANÁ - PLANO PLURIANUAL 2008/2011 - Demonstrativo por Função / Subfunção";
82  			}			
83  			
84  			
85  			final RelatorioPPAFuncaoService service = RelatorioPPAFuncaoService.getInstance(request);
86  						
87  			// path absoluto do relatorio PPA
88  			final String pathRelPPA = context.getRealPath( JasperService.PATH_RELATORIO + "/relatorioPPAFuncao.jasper" );
89  			final String pathRelPPASub = context.getRealPath( JasperService.PATH_RELATORIO + "/relatorioPPAFuncaoSub.jasper" );
90  			final String pathRelPPACapa = context.getRealPath( JasperService.PATH_RELATORIO + "/relatorioPPA_Demonstrativos_capa.jasper" );					
91  
92  			JasperReport relatorioJasper = JasperService.loadReport( pathRelPPA );
93  			JasperReport relatorioCapaJasper = JasperService.loadReport( pathRelPPACapa );	
94  			this.redirectDefaultErrorIfNull(request, response, relatorioJasper);
95  			
96  			TreeSet<FuncaoAreaAreBean> relatorioDS = service.generatePPA();
97  			this.redirectDefaultErrorIfNull(request, response, relatorioDS);
98  			
99  			
100 			
101 			parametros.put("pagina", paginaInt );
102 			parametros.put( "data" , data );
103 			parametros.put( "titulo" , titulo );	
104 			parametros.put( "relatorioPPA_demonstrativo_funcao_PATH" , pathRelPPASub );
105 			parametros.put( "total1" , service.getTotalGeral1() );
106 			parametros.put( "total2" , service.getTotalGeral2() );
107 			parametros.put( "total3" , service.getTotalGeral3() );
108 			parametros.put( "total4" , service.getTotalGeral4() );
109 			parametros.put( "totalGeral" , service.getTotalGeral() );			
110 			
111 			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(relatorioDS);
112 			
113 			JasperPrint capa = JasperService.fillReport(relatorioCapaJasper,parametros );
114 			JasperPrint rel = JasperService.fillReport(relatorioJasper,parametros, ds);
115 			
116 			ByteArrayOutputStream out = new ByteArrayOutputStream();
117 			JasperService.addRelatorio( capa, rel, out );			
118 			this.responseToPDF(response, out.toByteArray());
119 			
120 		} catch (Exception e) {
121 			logger.error( "Nao foi possivel gerar relatorio PPA por Função",e);
122 		}
123 		
124 	}
125 	
126 
127 
128 	
129 }