1 package ecar.servlet.componente;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.apache.log4j.Logger;
15
16 import comum.util.Pagina;
17
18 import ecar.dao.AcompRealFisicoDao;
19 import ecar.dao.AcompRealFisicoLocalDao;
20 import ecar.dao.LocalItemDao;
21 import ecar.pojo.AcompRealFisicoArf;
22 import ecar.pojo.AcompRealFisicoLocalArfl;
23 import ecar.pojo.LocalItemLit;
24
25 public class SalvarAcompRealFisicoLocalServlet extends HttpServlet{
26
27
28 private static final long serialVersionUID = 8006183415879658218L;
29
30 private Logger logger = Logger.getLogger(this.getClass());
31
32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33 logger.info("request :: GET:: Classe:: " + this.getClass().getName() );
34 StringBuilder conteudo = new StringBuilder();
35 conteudo.append("false");
36 logger.error("Acesso nao autorizado:: acesso GET ao servico de salvar indicador por local");
37 responseToHTML( response, conteudo.toString());
38 }
39
40 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41 logger.info("request :: POST:: Classe:: " + this.getClass().getName() );
42 execute(request,response);
43
44 }
45
46 private void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47
48 StringBuilder conteudo = new StringBuilder();
49
50 try {
51
52 Long codArfLong = Pagina.getParamLong(request, "codARF");
53 Long paramGrupo = Long.valueOf(Pagina.getParamStr(request, "subgrupo"));
54
55
56 AcompRealFisicoDao arfDAO = new AcompRealFisicoDao(request);
57 AcompRealFisicoArf arf = arfDAO.buscar(codArfLong);
58
59
60 LocalItemDao litDAO = new LocalItemDao(request);
61 List listaLocais = litDAO.getLocalItemPorLocalGrupo(paramGrupo);
62
63
64
65 List listaArfl = new ArrayList();
66 AcompRealFisicoLocalDao arflDAO = new AcompRealFisicoLocalDao(request);
67 for (Iterator iterator = listaLocais.iterator(); iterator.hasNext();) {
68 LocalItemLit local = (LocalItemLit) iterator.next();
69
70
71 AcompRealFisicoLocalArfl arfl = arflDAO.getAcompRealFisicoLocalByLocal(arf, local);
72
73
74 String nomeCampo = "cmp|" + local.getCodLit().toString();
75 String vlr = request.getParameter(nomeCampo);
76
77
78 if ( vlr!=null & (!"".equalsIgnoreCase(vlr))){
79 StringBuilder strValor = new StringBuilder(vlr);
80 char charValor [] = new char [strValor.length()];
81 int j = 0;
82 for (int i = 0; i < strValor.length(); i++) {
83 char posicao = strValor.charAt(i);
84
85 if ( posicao == ','){
86
87 charValor[j] = '.';
88 j++;
89 }
90 else{
91 if (posicao != '.'){
92 charValor[j] = posicao;
93 j++;
94 }
95 }
96 }
97
98 Double vlrDouble = Double.parseDouble(String.valueOf(charValor));
99
100
101 arfl.setQuantidadeArfl(vlrDouble);
102 listaArfl.add(arfl);
103 }
104 }
105
106
107 arflDAO.salvar(listaArfl, arf);
108
109 conteudo.append("true");
110 responseToHTML(response, conteudo.toString() );
111 } catch(Exception e){
112 logger.error(e);
113 StringBuilder responseHTML = new StringBuilder();
114 responseHTML.append("false");
115 responseToHTML(response, responseHTML.toString());
116 }
117 }
118
119 private void responseToHTML ( HttpServletResponse response, String conteudo ) throws IOException {
120 response.setContentType("text/html");
121 PrintWriter writer = response.getWriter();
122 writer.append(conteudo);
123 writer.flush();
124 writer.close();
125 }
126
127
128 }