View Javadoc

1   package ecar.servlet.componente;
2   
3   import java.io.IOException;
4   import java.io.PrintWriter;
5   import java.text.DecimalFormat;
6   import java.text.DecimalFormatSymbols;
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.Locale;
11  import java.util.Set;
12  
13  import javax.servlet.ServletException;
14  import javax.servlet.http.HttpServlet;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  import org.apache.log4j.Logger;
19  
20  import comum.util.Pagina;
21  
22  import ecar.dao.ExercicioDao;
23  import ecar.dao.ItemEstrtIndResultLocalIettirlDao;
24  import ecar.dao.ItemEstruturaDao;
25  import ecar.dao.LocalItemDao;
26  import ecar.pojo.ExercicioExe;
27  import ecar.pojo.ItemEstrtIndResulLocalIettirl;
28  import ecar.pojo.ItemEstrutLocalIettl;
29  import ecar.pojo.ItemEstruturaIett;
30  import ecar.pojo.LocalGrupoLgp;
31  import ecar.pojo.LocalItemLit;
32  
33  public class PesquisaLocalServlet extends HttpServlet{
34  
35  	private static final long serialVersionUID = 2244076567158750862L;
36  	private Logger logger = Logger.getLogger(this.getClass());
37  	
38  	//	constante de base de dados - Periodo de 2008 - 2011
39  	//private final Long CONSTANTE_PERIODO_EXERCICIO = Long.valueOf(2);
40  	
41  	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42  		logger.info("request :: GET:: Classe:: " + this.getClass().getName() );
43  		execute(request,response);
44  	}
45  	
46  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47  		logger.info("request :: POST:: Classe:: " + this.getClass().getName() );
48  		execute(request,response);
49  				
50  	}
51  	
52  		
53  	private void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
54  		
55  		final ItemEstrtIndResultLocalIettirlDao dao = new ItemEstrtIndResultLocalIettirlDao(request);
56  		final DecimalFormat formatBrasil = new DecimalFormat ("#,##0.00", new DecimalFormatSymbols (new Locale ("pt", "BR")));
57  		try{
58  			StringBuilder responseHTML = new StringBuilder();
59  			Long paramGrupo = Pagina.getParamLong(request, "grupo");
60  			Long codIettir = Pagina.getParamLong(request, "codIettir");
61  			Long codIett = Pagina.getParamLong(request, "codIett");
62  			Long itemSelecionado = Pagina.getParamLong(request, "itemSelecionado");
63  			
64  			final LocalItemDao daoItem = new LocalItemDao(request);
65  			final ExercicioDao exercicioDao = new ExercicioDao(request);
66  			
67  			List localList = daoItem.getLocalItemPorLocalGrupo(paramGrupo);
68  			List listaExercicios = exercicioDao.getExerciciosValidos(Long.valueOf( codIett ));
69  
70  			ItemEstruturaDao itemDao = new ItemEstruturaDao(request);
71  
72  			ItemEstruturaIett item = (ItemEstruturaIett)itemDao.buscar(ItemEstruturaIett.class, Long.valueOf( codIett ));
73  			List locaisItem = new ArrayList(item.getItemEstrutLocalIettls());
74  			LocalGrupoLgp localGrupoItem = ((ItemEstrutLocalIettl)locaisItem.get(0)).getLocalItemLit().getLocalGrupoLgp();
75  			
76  			// Se a lista de locais não estiver vazia
77  			if (!localList.isEmpty() && !localList.isEmpty()){
78  				final int larguraLocal = 250;
79  				final int larguraCampo = 150;
80  				final int larguraTotal = 200;
81  				final int cont=listaExercicios.size();
82  				final int larguraCaixaTexto;
83  				if (80 / cont > 15) {
84  					larguraCaixaTexto = 15;	
85  				}
86  				else{
87  					larguraCaixaTexto = (80 / cont );
88  				}
89  				
90  				
91  				// Criando a tabela
92  				responseHTML.append( "<table width=" + (larguraLocal + cont*larguraCampo + larguraTotal) + "class='layoutFixo' bgcolor='#EAEEF4' style='table-layout: fixed; text-align: left; margin-left: auto; margin-right: auto;' border='0' cellpadding='0' cellspacing='0' > ");
93  
94  				// Inserindo a checkbox superior
95  				responseHTML.append( "	<tr> ");
96  				responseHTML.append( "		<td colspan=" + (cont + 2) + "valign='top' height='25px'> "); 
97  				responseHTML.append( "			<input type='checkbox' name='copiarQtde1' id='copiarQtde1' value='true' onchange='checkboxSincroniza(\"copiarQtde1\",\"copiarQtde2\");' > Copiar Quantidade(s) para o(s) exercício(s) posterior(es)? ");
98  				responseHTML.append( "			<label ");
99  				responseHTML.append( "				id=\"label|-1|-1\"");
100 				responseHTML.append( " 				class=\"dica\" ");
101 				responseHTML.append( "				onmouseover=\"" );
102 				responseHTML.append( "					montaTooltipMouseOver(this,\'-1\', \'\', \'-1\', \'\');" );
103 				responseHTML.append( "				\"" );
104 				responseHTML.append( "				onmouseout=\"" );
105 				responseHTML.append( "					montaTooltipMouseOut(\'-1\', \'-1\');" );
106 				responseHTML.append( "				\"" );
107 				responseHTML.append( "			/>" );
108 				responseHTML.append( "				<img " );
109 				responseHTML.append( "					src=\"" + request.getContextPath() + "/images/dica.png\" " );
110 				responseHTML.append( "					align=\"absmiddle\" " );
111 				responseHTML.append( "					border=\"0\" " );
112 				responseHTML.append( "					onclick=\"" );
113 				responseHTML.append( "						montaTooltipMouseClick(\'-1\', \'\', \'-1\', \'\');" );
114 				responseHTML.append( "					\" " );
115 				responseHTML.append( "				/> " );
116 				responseHTML.append( "			" );
117 				responseHTML.append( "				<span id=\"span|-1|-1\" ></span>" );
118 				responseHTML.append( "			" );
119 				responseHTML.append( "			</label>" );
120 				responseHTML.append( "		</td> ");
121 				responseHTML.append( "	</tr> ");
122 
123 				// Inserindo o título Exercício
124 				responseHTML.append( "	<tr> ");
125 				responseHTML.append( "		<td width=" + larguraLocal + " style='font-size: 14px;color: #003366;font-weight: bold;'  align='center' >&nbsp; </td>");
126 				responseHTML.append( "		<td  colspan=\"" + (cont + 1) + "\" style=\"font-size: 14px;color: #003366;font-weight: bold;\"  align=\"center\" >Exercicio</td>"); 
127 				responseHTML.append( "	</tr> ");
128 
129 				// Inserindo os títulos Local, exercícios e Total por Local	
130 				responseHTML.append( "	<tr> ");
131 				responseHTML.append( "		<td width=" + larguraLocal + " style='font-size: 14px;color: #003366;font-weight: bold;'  align='center' >Local</td> ");					
132 				for (Iterator iterator = listaExercicios.iterator(); iterator.hasNext();) {
133 					ExercicioExe exe = (ExercicioExe) iterator.next();
134 					responseHTML.append( "	<td width=" + larguraCampo + " style=\"font-size: 14px;color: #003366;font-weight: bold;\"  align=\"center\" > ");
135 					responseHTML.append(exe.getDescricaoExe().toUpperCase() );
136 					responseHTML.append( "	</td> ");
137 				}	
138 				responseHTML.append( "		<td width=" + larguraTotal + " style='font-size: 14px;color: #003366;font-weight: bold;'  align='center' >Total por Local</td> ");
139 				responseHTML.append( "	<tr> ");
140 
141 				// Inserindo a caixa de seleção
142 				Set subgrupo = localGrupoItem.getLocalGrupoHierarquiaLgphsByCodLgpPai();
143 				if ( subgrupo!=null && !subgrupo.isEmpty()){
144 					responseHTML.append( "	<tr> ");
145 					responseHTML.append( "		<td width=" + larguraLocal + " style='font-size: 12px;color: #003366;'  align='left' > ");
146 					responseHTML.append( "		<table width='90%'> ");
147 					responseHTML.append( "			<tr>" );
148 					responseHTML.append( "				<td width='25%' align='right'>" );
149 					responseHTML.append( "					<img src='../../images/icon_seta_ident.gif'/>" );
150 					responseHTML.append( "				</td>" );
151 					responseHTML.append( "				<td width='75%'>" );
152 					responseHTML.append( "					<select name='subgrupo' id='subgrupo' onchange='carregarlocal();'  > ");
153 					
154 					int indice = 1;
155 					responseHTML.append( "						<option value=" + localGrupoItem.getCodLgp() + " >Selecione</option> ");
156 					for (Iterator itGrupo = subgrupo.iterator(); itGrupo.hasNext();) {
157 						LocalGrupoLgp grupo = (LocalGrupoLgp) itGrupo.next();
158 						if(indice == itemSelecionado){
159 							responseHTML.append( "				<option value='" + grupo.getCodLgp() + "' selected='true'>" + grupo.getIdentificacaoLgp() + "</option>" );
160 						}
161 						else {
162 							responseHTML.append( "				<option value='" + grupo.getCodLgp() + "' >" + grupo.getIdentificacaoLgp() + "</option>" );
163 						}
164 						indice++;
165 					}
166 					responseHTML.append( "					</select> ");
167 					responseHTML.append( "					</td>" );
168 					responseHTML.append( "				</tr>" );
169 					responseHTML.append( "			</table>" );
170 					
171 					responseHTML.append( "		</td> ");					
172 					responseHTML.append( "   	<td colspan=" + (cont +1) + "valign='top' height='25px'> &nbsp; </td> ");
173 					responseHTML.append( "	</tr> ");
174 				}
175 		
176 				
177 				// Inserindo o conteúdo da tabela
178 				String strTdSeta = null; 
179 				String strTdCampo = null;
180 				String strTdTotalPorLocal = null;
181 				
182 				Double totalLocal = 0.0;
183 				Double totalExercicio[] = new Double [listaExercicios.size()];
184 				Double totalGeral = 0.0;
185 				int i = 0;
186 				for(i = 0; i < totalExercicio.length; i++){
187 					totalExercicio[i] = 0.0;
188 				}
189 				
190 				
191 				// Para cada local
192 				for (Iterator iter = localList.iterator(); iter.hasNext();) {
193 					LocalItemLit local = (LocalItemLit) iter.next();
194 					
195 					// Criar uma célula com a seta e o nome do local
196 					strTdSeta = new String (
197 								"<td align='rigth' width='" + larguraLocal + "' height='25px' valign='top'>" +
198 								"	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
199 								"	<table width='100%'> " +
200 								"		<tr>" +
201 								"			<td width='25%' align='right'>" +
202 								"				<img src='../../images/icon_seta_ident.gif'/>" +
203 								"			</td>" +
204 								"			<td width='75%'>" +
205 								"				" + local.getIdentificacaoLit() +
206 								"				&nbsp;" + 
207 								"			</td>" +
208 								"		</tr>" +
209 								"	</table>" +
210 								"</td>"
211 					);
212 					responseHTML.append("<tr>");
213 					responseHTML.append(strTdSeta);
214 					
215 					totalLocal = 0.0;
216 					i = 0;
217 					// Para cada exercício
218 					for (Iterator iterator = listaExercicios.iterator(); iterator.hasNext();) {
219 						ExercicioExe exe = (ExercicioExe) iterator.next();
220 						ItemEstrtIndResulLocalIettirl itemEstrtIndResulLocalIettirl = dao.getQtdePrevistaByLocal( codIettir, exe.getCodExe(), local.getCodLit() );
221 		
222 						totalLocal += itemEstrtIndResulLocalIettirl.getQtdPrevistaIettirl().doubleValue();
223 						totalExercicio [i] += itemEstrtIndResulLocalIettirl.getQtdPrevistaIettirl().doubleValue();
224 						i++;
225 						
226 						//if (local.getCodLit().intValue() == 418){
227 							
228 							
229 						// Criar uma célula contendo um campo e uma imagem de informação.
230 						strTdCampo = new String (
231 									"<td width=\"" + larguraCampo + "\" align=\"center\" >" +
232 									"	<input " +
233 									"		type=\"text\"" +
234 									"		size=\"" + (larguraCaixaTexto - 3) + "\"" +
235 									"		onblur=\"" +
236 									"			copiarExercicio('" + local.getCodLit().toString() + "'); " +
237 									"			calcularTotalLocal('" + local.getCodLit().toString() + "');" +
238 									"			calcularTotalExercicio('"+exe.getCodExe().toString() + "');" +
239 									"		\"" +
240 									"		id=\"cmp|" + local.getCodLit().toString() + "|" + exe.getCodExe().toString() + "\"" +
241 									"		name=\"cmp|" + local.getCodLit().toString() + "|" + exe.getCodExe().toString() + "\"" +
242 									"		value=\"" + formatBrasil.format(itemEstrtIndResulLocalIettirl.getQtdPrevistaIettirl().doubleValue()) + "\"" +
243 									"	/>" +
244 									"	" +
245 									"	<label " +
246 									"		id=\"label|" + local.getCodLit().toString() + "|" + exe.getCodExe().toString() + "\"" +
247 									" 		class=\"dica\" " +
248 									"		onmouseover=\"" +
249 									"			montaTooltipMouseOver(this,\'"+ exe.getCodExe().toString() +  "\',\'" + exe.getDescricaoExe() +"\',\'"+ local.getCodLit().toString() +  "\',\'" + local.getIdentificacaoLit() +"\');" +
250 									"		\"" +
251 									"		onmouseout=\"" +
252 									"			montaTooltipMouseOut(\'"+ exe.getCodExe().toString() +  "\',\'" + local.getCodLit().toString() +"\');" +
253 									"		\"" +
254 									"	/>" +
255 									"		<img " +
256 									"			src=\"" + request.getContextPath() + "/images/dica.png\" " +
257 									"			align=\"absmiddle\" " +
258 									"			border=\"0\" " +
259 									"			onclick=\"" +
260 									"				montaTooltipMouseClick(\'"+ exe.getCodExe().toString() +  "\',\'" + exe.getDescricaoExe() +"\',\'"+ local.getCodLit().toString() +  "\',\'" + local.getIdentificacaoLit() +"\');" +
261 									"			\" " +
262 									"		/> " +
263 									"		" +
264 									"		<span id=\"span|" + local.getCodLit().toString() + "|" + exe.getCodExe().toString() + "\" ></span>" +
265 									"		" +
266 									"	</label>" +
267 									"</td>"
268 						);
269 						responseHTML.append(strTdCampo);
270 					}
271 					
272 					// Criar uma célula de total por local
273 					strTdTotalPorLocal = new String(
274 							"<td align=\"center\" width=\"" + larguraTotal + "\" >" +
275 							"	<input " +
276 							"		type=\"text\"" +
277 							"		size=\"12\"" +
278 							"		readonly=\"true\"" +
279 							"		id=\"cmp|" + local.getCodLit().toString() + "|total\"" +
280 							"		name=\"cmp|" + local.getCodLit().toString() + "|total\"" +
281 							"		value=\"" + formatBrasil.format(totalLocal) + "\"" + 
282 							"       style=\"background-color:#FFF9DC;\"" +
283 							"	/>" +
284 							"</td>"
285 					);
286 					responseHTML.append(strTdTotalPorLocal);
287 					responseHTML.append("</tr>");
288 					
289 					//}// if 418
290 
291 				}
292 
293 				responseHTML.append( "	<tr> ");
294 				responseHTML.append( "		<td width=" + larguraLocal + " style='font-size: 14px;color: #003366;font-weight: bold;'  align='center' >Total por Exercício</td> ");		
295 
296 				// Inserindo os totais por exercício e o total geral
297 				i = 0;
298 				for (Iterator iterator = listaExercicios.iterator(); iterator.hasNext();) {
299 					ExercicioExe exe = (ExercicioExe) iterator.next();
300 					//responseHTML.append( "	<td width=" + larguraCampo + " align=\"center\"><input type=\"text\" style=\"background-color:#FFF9DC;\"  size=\"" + larguraCaixaTexto + "\" id=\"total"+exe.getCodExe().toString() +"\" name=\"total"+exe.getCodExe()+"\" value=" + formatBrasil.format(totalExercicio[i]) + " readonly=\"true\" /></td>");
301 					responseHTML.append( "	<td width=" + larguraCampo + " align=\"center\"><input type=\"text\" style=\"background-color:#FFF9DC;\"  size=\"" + larguraCaixaTexto + "\" id=\"qtdPrevistaIettf"+exe.getCodExe().toString() +"\" name=\"qtdPrevistaIettf"+exe.getCodExe()+"\" value=" + formatBrasil.format(totalExercicio[i]) + " readonly=\"true\" /></td>");
302 					
303 					totalGeral += totalExercicio[i]; 
304 					i++;
305 				}	
306 				responseHTML.append( "		<td widht=" + larguraTotal + " align='center'><input type=\"text\" style=\"background-color:#FFF9DC;\"    size='12'  id='totalGeral' style='background-color:#FFF9DC;' name='totalGeral' value='" + formatBrasil.format(totalGeral) + "' readonly='true' /></td> ");
307 				responseHTML.append( "	</tr> ");
308 
309 				// Inserindo a checkbox inferior
310 				responseHTML.append( "	<tr> ");
311 				responseHTML.append( "		<td colspan=" + (cont + 2) + "valign='top' height='25px'> "); 
312 				responseHTML.append( "			<input type='checkbox' name='copiarQtde2' id='copiarQtde2' value='true' onchange='checkboxSincroniza(\"copiarQtde2\",\"copiarQtde1\");' > Copiar Quantidade(s) para o(s) exercício(s) posterior(es)? ");
313 				responseHTML.append( "			<label ");
314 				responseHTML.append( "				id=\"label|-2|-2\"");
315 				responseHTML.append( " 				class=\"dica\" ");
316 				responseHTML.append( "				onmouseover=\"" );
317 				responseHTML.append( "					montaTooltipMouseOver(this,\'-2\', \'\', \'-2\', \'\');" );
318 				responseHTML.append( "				\"" );
319 				responseHTML.append( "				onmouseout=\"" );
320 				responseHTML.append( "					montaTooltipMouseOut(\'-2\', \'-2\');" );
321 				responseHTML.append( "				\"" );
322 				responseHTML.append( "			/>" );
323 				responseHTML.append( "				<img " );
324 				responseHTML.append( "					src=\"" + request.getContextPath() + "/images/dica.png\" " );
325 				responseHTML.append( "					align=\"absmiddle\" " );
326 				responseHTML.append( "					border=\"0\" " );
327 				responseHTML.append( "					onclick=\"" );
328 				responseHTML.append( "						montaTooltipMouseClick(\'-1\', \'\', \'-1\', \'\');" );
329 				responseHTML.append( "					\" " );
330 				responseHTML.append( "				/> " );
331 				responseHTML.append( "			" );
332 				responseHTML.append( "				<span id=\"span|-2|-2\" ></span>" );
333 				responseHTML.append( "			" );
334 				responseHTML.append( "			</label>" );
335 				responseHTML.append( "		</td> ");
336 				responseHTML.append( "	</tr> ");	
337 				responseHTML.append( "</table> ");
338 			}else{
339 				responseHTML.append("Nenhum registro encontrado");
340 			}
341 			responseToHTML(response, responseHTML.toString());
342 		}catch(Exception e){
343 			e.printStackTrace(System.out);
344 			logger.error(e);
345 			StringBuilder responseHTML = new StringBuilder();
346 			responseHTML.append("Nenhum registro encontrado");
347 			responseToHTML(response, responseHTML.toString());
348 		}
349 	}
350 
351 		
352 	private void responseToHTML ( HttpServletResponse response, String conteudo ) throws IOException {
353 		response.setContentType("text/html");
354 		PrintWriter writer = response.getWriter();
355 		writer.append(conteudo);
356 		writer.flush();
357 		writer.close();
358 	}
359 	
360 	
361 }