Commit c8d1f4f9fe8a454f18635760ac7ce27dbaf11349

Authored by gibransodre
1 parent a646d246
Exists in master

Recuperação do plano de trabalho.

WebContent/WEB-INF/jsp/questionario/questionario.jsp
@@ -53,8 +53,9 @@ table, th, td { @@ -53,8 +53,9 @@ table, th, td {
53 </fieldset> 53 </fieldset>
54 </form> 54 </form>
55 <br> 55 <br>
  56 +
56 <label>Total de solicitações: <span>${totalSolicitacoes}</span></label><br> 57 <label>Total de solicitações: <span>${totalSolicitacoes}</span></label><br>
57 - <table> 58 + <table id="questionarios">
58 <tr> 59 <tr>
59 <th>Nº Linha</th> 60 <th>Nº Linha</th>
60 <th>Orgão</th> 61 <th>Orgão</th>
@@ -67,6 +68,7 @@ table, th, td { @@ -67,6 +68,7 @@ table, th, td {
67 <th>Data Relatório Respondido</th> 68 <th>Data Relatório Respondido</th>
68 <th>Relatório Respondido</th> 69 <th>Relatório Respondido</th>
69 <th>Plano de Trabalho Enviado</th> 70 <th>Plano de Trabalho Enviado</th>
  71 + <th>download Plano de Trabalho</th>
70 72
71 </tr> 73 </tr>
72 74
@@ -103,14 +105,14 @@ table, th, td { @@ -103,14 +105,14 @@ table, th, td {
103 <c:if test="${questionario.planoTrabalhoEnviado == false}"> 105 <c:if test="${questionario.planoTrabalhoEnviado == false}">
104 <td><input type="checkbox" disabled="disabled"> </td> 106 <td><input type="checkbox" disabled="disabled"> </td>
105 </c:if> 107 </c:if>
106 - 108 + <td><a href="${pageContext.request.contextPath}/recuperar-plano-trabalho/${questionario.idQuestionario}">Recuperar</a></td>
107 </tr> 109 </tr>
108 110
109 </c:forEach> 111 </c:forEach>
110 </c:if> 112 </c:if>
111 113
112 </table> 114 </table>
113 - 115 +<!-- <a href="${pageContext.request.contextPath}/recuperar-plano-trabalho">Recuperar Plano de Trabalho</a>-->
114 </body> 116 </body>
115 117
116 118
src/br/com/controller/PlanoTrabalhoController.java
1 package br.com.controller; 1 package br.com.controller;
2 2
  3 +import java.io.FileOutputStream;
  4 +import java.io.IOException;
  5 +import java.io.InputStream;
  6 +import java.sql.ResultSet;
  7 +
3 import javax.servlet.ServletContext; 8 import javax.servlet.ServletContext;
  9 +import javax.servlet.ServletOutputStream;
  10 +import javax.servlet.http.HttpServletResponse;
4 11
  12 +import br.com.caelum.vraptor.Get;
5 import br.com.caelum.vraptor.Path; 13 import br.com.caelum.vraptor.Path;
  14 +import br.com.caelum.vraptor.Post;
6 import br.com.caelum.vraptor.Resource; 15 import br.com.caelum.vraptor.Resource;
7 import br.com.caelum.vraptor.Result; 16 import br.com.caelum.vraptor.Result;
8 import br.com.caelum.vraptor.Validator; 17 import br.com.caelum.vraptor.Validator;
@@ -43,7 +52,8 @@ public class PlanoTrabalhoController { @@ -43,7 +52,8 @@ public class PlanoTrabalhoController {
43 String tipoArquivo = file.getContentType(); 52 String tipoArquivo = file.getContentType();
44 String mensagem = "Não foi possível enviar o plano de trabalho!"; 53 String mensagem = "Não foi possível enviar o plano de trabalho!";
45 boolean status = false; 54 boolean status = false;
46 - 55 + int idPlanoTrabalho = -1;
  56 +
47 if (!tipoArquivo.equalsIgnoreCase("application/msword") 57 if (!tipoArquivo.equalsIgnoreCase("application/msword")
48 && !tipoArquivo.equalsIgnoreCase("application/vnd.ms-excel") 58 && !tipoArquivo.equalsIgnoreCase("application/vnd.ms-excel")
49 && !tipoArquivo.equalsIgnoreCase("application/vnd.oasis.opendocument.text") 59 && !tipoArquivo.equalsIgnoreCase("application/vnd.oasis.opendocument.text")
@@ -64,15 +74,29 @@ public class PlanoTrabalhoController { @@ -64,15 +74,29 @@ public class PlanoTrabalhoController {
64 74
65 if (questionario != null && questionario.getDtInicioQuestionario() != null) { 75 if (questionario != null && questionario.getDtInicioQuestionario() != null) {
66 PlanoTrabalhoDao planoTrabalhoDao = new PlanoTrabalhoDao(); 76 PlanoTrabalhoDao planoTrabalhoDao = new PlanoTrabalhoDao();
67 -  
68 - if (planoTrabalhoDao.salvarPlanoTrabalho(valorHash, file.getFile(), questionario)) {  
69 - mensagem = "Plano de trabalho enviado com sucesso!";  
70 - status = true;  
71 - }  
72 -  
73 - else {  
74 - mensagem = "Não foi possível enviar o plano de trabalho!"; 77 +
  78 + idPlanoTrabalho = planoTrabalhoDao.verificaPlanoTrabalho(path, questionario.getIdQuestionario());
  79 +
  80 + if(idPlanoTrabalho == -1)
  81 + {
  82 + if (planoTrabalhoDao.salvarPlanoTrabalho(path, file.getFile(), questionario)) {
  83 + mensagem = "Plano de trabalho enviado com sucesso!";
  84 + status = true;
  85 + }
  86 + else {
  87 + mensagem = "Não foi possível enviar o plano de trabalho!";
  88 + }
  89 + }else
  90 + {
  91 + if (planoTrabalhoDao.atualizarPlanoTrabalho(path, file.getFile(), questionario, idPlanoTrabalho)) {
  92 + mensagem = "Plano de trabalho enviado com sucesso!";
  93 + status = true;
  94 + }
  95 + else {
  96 + mensagem = "Não foi possível enviar o plano de trabalho!";
  97 + }
75 } 98 }
  99 +
76 } 100 }
77 else 101 else
78 { 102 {
@@ -84,4 +108,35 @@ public class PlanoTrabalhoController { @@ -84,4 +108,35 @@ public class PlanoTrabalhoController {
84 this.validator.add(new ValidationMessage("", "")); 108 this.validator.add(new ValidationMessage("", ""));
85 this.validator.onErrorUsePageOf(PlanoTrabalhoController.class).planoTrabalhoConfirmacao(); 109 this.validator.onErrorUsePageOf(PlanoTrabalhoController.class).planoTrabalhoConfirmacao();
86 } 110 }
  111 +
  112 + @Get("/recuperar-plano-trabalho/{id}")
  113 + public void recuperarPlanoTrabalho(int id, HttpServletResponse response)throws IOException {
  114 +
  115 +Questionario questionario = new Questionario(path, id);
  116 +
  117 + PlanoTrabalhoDao planoTrabalhoDao = new PlanoTrabalhoDao();
  118 +
  119 + byte[] dados = null;
  120 +
  121 + if(questionario != null && questionario.getIdQuestionario() > 0)
  122 + {
  123 + dados = planoTrabalhoDao.recuperarPlanoTrabalho(path, questionario);
  124 + }
  125 +
  126 + String nome = "plano de trabalho";
  127 + //FileOutputStream fileOut = new FileOutputStream(nome);
  128 + //fileOut.write(dados);
  129 + //fileOut.flush();
  130 + //fileOut.close();
  131 + response.setContentType("inline/download");
  132 + String arq = "attachment;filename=" + nome;
  133 + response.setHeader("Content-Disposition", arq);
  134 + ServletOutputStream os = response.getOutputStream();
  135 + os.write(dados);
  136 + os.flush();
  137 + os.close();
  138 +
  139 +
  140 +
  141 + }
87 } 142 }
src/br/com/dao/PlanoTrabalhoDao.java
1 package br.com.dao; 1 package br.com.dao;
2 2
  3 +import java.io.FileNotFoundException;
  4 +import java.io.FileOutputStream;
  5 +import java.io.IOException;
3 import java.io.InputStream; 6 import java.io.InputStream;
  7 +import java.io.PrintStream;
4 import java.sql.Connection; 8 import java.sql.Connection;
5 import java.sql.PreparedStatement; 9 import java.sql.PreparedStatement;
6 import java.sql.ResultSet; 10 import java.sql.ResultSet;
7 import java.sql.SQLException; 11 import java.sql.SQLException;
8 import java.sql.Statement; 12 import java.sql.Statement;
  13 +import java.util.Properties;
9 14
10 import br.com.model.entity.Questionario; 15 import br.com.model.entity.Questionario;
  16 +import br.com.model.utilities.ManagerProperties;
11 17
12 public class PlanoTrabalhoDao { 18 public class PlanoTrabalhoDao {
13 19
14 20
15 - public boolean verificaPlanoTrabalho(String path, Integer idQuestionario) 21 + public int verificaPlanoTrabalho(String path, Integer idQuestionario)
16 { 22 {
17 23
18 24
@@ -21,6 +27,8 @@ public class PlanoTrabalhoDao { @@ -21,6 +27,8 @@ public class PlanoTrabalhoDao {
21 Statement estado; 27 Statement estado;
22 ResultSet rset = null; 28 ResultSet rset = null;
23 boolean existePlanoTrabalho = false; 29 boolean existePlanoTrabalho = false;
  30 + int idPlanoTrabalho = -1;
  31 +
24 try { 32 try {
25 33
26 estado = con.createStatement(); 34 estado = con.createStatement();
@@ -31,7 +39,7 @@ public class PlanoTrabalhoDao { @@ -31,7 +39,7 @@ public class PlanoTrabalhoDao {
31 rset = estado.executeQuery(sql); 39 rset = estado.executeQuery(sql);
32 40
33 while (rset.next()) { 41 while (rset.next()) {
34 - existePlanoTrabalho = true; 42 + idPlanoTrabalho = rset.getInt("id_plano_trabalho");
35 } 43 }
36 44
37 } catch (SQLException e) { 45 } catch (SQLException e) {
@@ -48,7 +56,7 @@ public class PlanoTrabalhoDao { @@ -48,7 +56,7 @@ public class PlanoTrabalhoDao {
48 } 56 }
49 } 57 }
50 58
51 - return existePlanoTrabalho; 59 + return idPlanoTrabalho;
52 } 60 }
53 61
54 public boolean salvarPlanoTrabalho(String path, InputStream file, Questionario questionario) 62 public boolean salvarPlanoTrabalho(String path, InputStream file, Questionario questionario)
@@ -86,5 +94,84 @@ public class PlanoTrabalhoDao { @@ -86,5 +94,84 @@ public class PlanoTrabalhoDao {
86 94
87 return salvoComSucesso; 95 return salvoComSucesso;
88 } 96 }
  97 +
  98 + public boolean atualizarPlanoTrabalho(String path, InputStream file, Questionario questionario, int idPlanoTrabalho)
  99 + {
  100 +
  101 + StringBuffer sql = new StringBuffer();
  102 + Connection con = Conexao.conecta(path);
  103 + PreparedStatement estado;
  104 + int resultado = 0;
  105 + boolean salvoComSucesso = false;
  106 +
  107 + try {
  108 +
  109 + sql = new StringBuffer();
  110 + //sql.append(" INSERT INTO public.plano_trabalho(id_questionario, arquivo_plano_trabalho) VALUES (?, ?); ");
  111 + sql.append(" UPDATE public.plano_trabalho");
  112 + sql.append(" SET arquivo_plano_trabalho=?");
  113 + sql.append(" WHERE id_plano_trabalho= "+ idPlanoTrabalho +";");
  114 +
  115 + estado = con.prepareStatement(sql.toString());
  116 +
  117 + //estado.setInt(1, questionario.getIdQuestionario());
  118 + estado.setBinaryStream(1, file);
  119 +
  120 + resultado = estado.executeUpdate();
  121 +
  122 + if (resultado == 0) {
  123 + salvoComSucesso = false;
  124 + } else {
  125 +
  126 + salvoComSucesso = true;
  127 + }
  128 +
  129 + } catch (SQLException e) {
  130 +
  131 + e.printStackTrace();
  132 + }
  133 +
  134 + return salvoComSucesso;
  135 + }
  136 +
  137 + public byte[] recuperarPlanoTrabalho(String path, Questionario questionario)
  138 + {
  139 +
  140 +
  141 + String sql = "";
  142 + Connection con = Conexao.conecta(path);
  143 + Statement estado;
  144 + ResultSet rset = null;
  145 + byte[] imgBytes = null;
  146 +
  147 + try {
  148 +
  149 +
  150 + estado = con.createStatement();
  151 +
  152 + estado.execute(sql.toString());
  153 + sql = "SELECT * FROM public.plano_trabalho where id_questionario =" + questionario.getIdQuestionario() + ";" ;
  154 +
  155 + rset = estado.executeQuery(sql);
  156 +
  157 + while (rset.next()) {
  158 + imgBytes = rset.getBytes("arquivo_plano_trabalho");
  159 + }
  160 +
  161 + } catch (SQLException e) {
  162 + System.out.println("Deu erro!");
  163 + e.printStackTrace();
  164 + } finally {
  165 + if (con != null) {
  166 + try {
  167 + con.close();
  168 + } catch (SQLException e) {
  169 + e.printStackTrace();
  170 + }
  171 + }
  172 + }
  173 +
  174 + return imgBytes;
  175 + }
89 176
90 } 177 }
src/br/com/dao/QuestionarioDao.java
@@ -68,7 +68,7 @@ public class QuestionarioDao { @@ -68,7 +68,7 @@ public class QuestionarioDao {
68 68
69 69
70 questionario.setDataRespostaQuestionario(respostaQuestionarioDao.verificaRespostaQuestionario(path, rset.getInt("id_questionario"))); 70 questionario.setDataRespostaQuestionario(respostaQuestionarioDao.verificaRespostaQuestionario(path, rset.getInt("id_questionario")));
71 - questionario.setPlanoTrabalhoEnviado(planoTrabalhoDao.verificaPlanoTrabalho(path, rset.getInt("id_questionario"))); 71 + questionario.setPlanoTrabalhoEnviado( planoTrabalhoDao.verificaPlanoTrabalho(path, rset.getInt("id_questionario")) > -1 ? true : false);
72 listaQuestionario.add(questionario); 72 listaQuestionario.add(questionario);
73 73
74 } 74 }