Commit 3a822e2acaf9deb995602f281b41c80eef16bd81
1 parent
f2b40aa4
Exists in
master
Responsáveis pelas consultas e inserções.
Showing
5 changed files
with
307 additions
and
97 deletions
Show diff stats
src/br/com/dao/HashDao.java
@@ -6,42 +6,53 @@ import java.sql.SQLException; | @@ -6,42 +6,53 @@ import java.sql.SQLException; | ||
6 | import java.sql.Statement; | 6 | import java.sql.Statement; |
7 | 7 | ||
8 | import br.com.model.entity.Questionario; | 8 | import br.com.model.entity.Questionario; |
9 | +import br.com.util.DateUtil; | ||
9 | 10 | ||
10 | public class HashDao { | 11 | public class HashDao { |
11 | 12 | ||
12 | - public Questionario verificarHash(String valorHash) { | ||
13 | - | ||
14 | - StringBuffer sql = new StringBuffer(); | ||
15 | - String orgao = ""; | ||
16 | - Connection con = Conexao.conecta(); | ||
17 | - Statement estado; | ||
18 | - ResultSet rset = null; | ||
19 | - Questionario questionario = new Questionario(); | ||
20 | - | ||
21 | - try { | ||
22 | - estado = con.createStatement(); | ||
23 | - | ||
24 | - estado.execute(sql.toString()); | ||
25 | - sql.append("SELECT id_questionario, dt_inicio_questionario, dt_fim_questionario,"); | ||
26 | - sql.append("no_orgao, hash_autenticacao"); | ||
27 | - sql.append(" FROM questionario where hash_autenticacao = '" + valorHash + "'"); | ||
28 | - | ||
29 | - rset = estado.executeQuery(sql.toString()); | ||
30 | - | ||
31 | - while (rset.next()) { | ||
32 | - questionario.setIdQuestionario(rset.getInt("id_questionario")); | ||
33 | - questionario.setDtFimQuestionario(rset.getString("dt_fim_questionario")); | ||
34 | - questionario.setDtInicioQuestionario(rset.getString("dt_inicio_questionario")); | ||
35 | - questionario.setHashAutenticacao(rset.getString("hash_autenticacao")); | ||
36 | - questionario.setNoOrgao(rset.getString("no_orgao")); | ||
37 | - } | ||
38 | - | ||
39 | - } catch (SQLException e) { | ||
40 | - System.out.println("Deu errooooooooooooooooooo: "); | ||
41 | - e.printStackTrace(); | ||
42 | - | ||
43 | - } | ||
44 | - | ||
45 | - return questionario; | ||
46 | - } | 13 | + public Questionario verificarHash(String valorHash) { |
14 | + | ||
15 | + StringBuffer sql = new StringBuffer(); | ||
16 | + Connection con = Conexao.conecta(); | ||
17 | + Statement estado; | ||
18 | + ResultSet rset = null; | ||
19 | + Questionario questionario = new Questionario(); | ||
20 | + | ||
21 | + try { | ||
22 | + estado = con.createStatement(); | ||
23 | + | ||
24 | + estado.execute(sql.toString()); | ||
25 | + sql.append("SELECT id_questionario, dt_inicio_questionario, dt_fim_questionario,"); | ||
26 | + sql.append("no_orgao, hash_autenticacao"); | ||
27 | + sql.append(" FROM questionario where hash_autenticacao = '" + valorHash + "'"); | ||
28 | + sql.append(" and (dt_inicio_questionario <= '" + DateUtil.dataHoraAtual() + "')"); | ||
29 | + sql.append(" and (dt_fim_questionario >= '" + DateUtil.dataHoraAtual() + "');"); | ||
30 | + | ||
31 | + rset = estado.executeQuery(sql.toString()); | ||
32 | + | ||
33 | + while (rset.next()) { | ||
34 | + questionario.setIdQuestionario(rset.getInt("id_questionario")); | ||
35 | + questionario.setDtFimQuestionario(rset.getString("dt_fim_questionario")); | ||
36 | + questionario.setDtInicioQuestionario(rset.getString("dt_inicio_questionario")); | ||
37 | + questionario.setHashAutenticacao(rset.getString("hash_autenticacao")); | ||
38 | + questionario.setNoOrgao(rset.getString("no_orgao")); | ||
39 | + questionario.setHashAutenticacao(rset.getString("hash_autenticacao")); | ||
40 | + } | ||
41 | + | ||
42 | + } catch (SQLException e) { | ||
43 | + System.out.println("Deu erro!"); | ||
44 | + e.printStackTrace(); | ||
45 | + | ||
46 | + } finally { | ||
47 | + if (con != null) { | ||
48 | + try { | ||
49 | + con.close(); | ||
50 | + } catch (SQLException e) { | ||
51 | + e.printStackTrace(); | ||
52 | + } | ||
53 | + } | ||
54 | + } | ||
55 | + | ||
56 | + return questionario; | ||
57 | + } | ||
47 | } | 58 | } |
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
1 | +package br.com.dao; | ||
2 | + | ||
3 | +import java.sql.Connection; | ||
4 | +import java.sql.ResultSet; | ||
5 | +import java.sql.SQLException; | ||
6 | +import java.sql.Statement; | ||
7 | + | ||
8 | +public class OpcaoRespostaDao { | ||
9 | + | ||
10 | + public ResultSet retornarOpcaoResposta(int idOpcaoResposta) { | ||
11 | + | ||
12 | + StringBuffer sql = new StringBuffer(); | ||
13 | + Connection con = Conexao.conecta(); | ||
14 | + Statement estado; | ||
15 | + ResultSet rset = null; | ||
16 | + | ||
17 | + try { | ||
18 | + estado = con.createStatement(); | ||
19 | + | ||
20 | + estado.execute(sql.toString()); | ||
21 | + sql.append(" SELECT id_opcao_resposta, desc_opcao_resposta FROM public.opcao_resposta where id_opcao_resposta = "+ idOpcaoResposta +"; "); | ||
22 | + | ||
23 | + rset = estado.executeQuery(sql.toString()); | ||
24 | + | ||
25 | + return rset; | ||
26 | + } catch (SQLException e) { | ||
27 | + e.printStackTrace(); | ||
28 | + return null; | ||
29 | + | ||
30 | + }finally { | ||
31 | + if (con != null) { | ||
32 | + try { | ||
33 | + con.close(); | ||
34 | + } catch (SQLException e) { | ||
35 | + e.printStackTrace(); | ||
36 | + } | ||
37 | + } | ||
38 | + } | ||
39 | + | ||
40 | + } | ||
41 | +} |
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
1 | +package br.com.dao; | ||
2 | + | ||
3 | +import java.sql.Connection; | ||
4 | +import java.sql.ResultSet; | ||
5 | +import java.sql.SQLException; | ||
6 | +import java.sql.Statement; | ||
7 | + | ||
8 | +public class PerguntaDao { | ||
9 | + | ||
10 | + public ResultSet retornarPergunta(int idPergunta) { | ||
11 | + | ||
12 | + StringBuffer sql = new StringBuffer(); | ||
13 | + Connection con = Conexao.conecta(); | ||
14 | + Statement estado; | ||
15 | + ResultSet rset = null; | ||
16 | + | ||
17 | + try { | ||
18 | + estado = con.createStatement(); | ||
19 | + | ||
20 | + estado.execute(sql.toString()); | ||
21 | + sql.append(" SELECT id_pergunta, desc_pergunta FROM public.pergunta where id_pergunta = "+ idPergunta +"; "); | ||
22 | + | ||
23 | + rset = estado.executeQuery(sql.toString()); | ||
24 | + | ||
25 | + return rset; | ||
26 | + } catch (SQLException e) { | ||
27 | + e.printStackTrace(); | ||
28 | + return null; | ||
29 | + | ||
30 | + }finally { | ||
31 | + if (con != null) { | ||
32 | + try { | ||
33 | + con.close(); | ||
34 | + } catch (SQLException e) { | ||
35 | + e.printStackTrace(); | ||
36 | + } | ||
37 | + } | ||
38 | + } | ||
39 | + | ||
40 | + } | ||
41 | +} |
@@ -0,0 +1,42 @@ | @@ -0,0 +1,42 @@ | ||
1 | +package br.com.dao; | ||
2 | + | ||
3 | +import java.sql.Connection; | ||
4 | +import java.sql.ResultSet; | ||
5 | +import java.sql.SQLException; | ||
6 | +import java.sql.Statement; | ||
7 | + | ||
8 | +public class QuestionarioDao { | ||
9 | + | ||
10 | + public ResultSet retornarQuestionario(int idQuestionario) { | ||
11 | + | ||
12 | + StringBuffer sql = new StringBuffer(); | ||
13 | + Connection con = Conexao.conecta(); | ||
14 | + Statement estado; | ||
15 | + ResultSet rset = null; | ||
16 | + | ||
17 | + try { | ||
18 | + estado = con.createStatement(); | ||
19 | + | ||
20 | + estado.execute(sql.toString()); | ||
21 | + sql.append(" SELECT id_questionario, dt_inicio_questionario, dt_fim_questionario, no_orgao, hash_autenticacao "); | ||
22 | + sql.append(" FROM public.questionario where id_questionario = "+ idQuestionario +";"); | ||
23 | + | ||
24 | + rset = estado.executeQuery(sql.toString()); | ||
25 | + | ||
26 | + return rset; | ||
27 | + } catch (SQLException e) { | ||
28 | + e.printStackTrace(); | ||
29 | + return null; | ||
30 | + | ||
31 | + }finally { | ||
32 | + if (con != null) { | ||
33 | + try { | ||
34 | + con.close(); | ||
35 | + } catch (SQLException e) { | ||
36 | + e.printStackTrace(); | ||
37 | + } | ||
38 | + } | ||
39 | + } | ||
40 | + | ||
41 | + } | ||
42 | +} |
src/br/com/dao/RespostaQuestionarioDao.java
@@ -5,74 +5,149 @@ import java.sql.DriverManager; | @@ -5,74 +5,149 @@ import java.sql.DriverManager; | ||
5 | import java.sql.ResultSet; | 5 | import java.sql.ResultSet; |
6 | import java.sql.SQLException; | 6 | import java.sql.SQLException; |
7 | import java.sql.Statement; | 7 | import java.sql.Statement; |
8 | - | 8 | +import java.util.ArrayList; |
9 | import java.util.List; | 9 | import java.util.List; |
10 | 10 | ||
11 | import br.com.controller.confirmacaoController; | 11 | import br.com.controller.confirmacaoController; |
12 | +import br.com.model.entity.Pergunta; | ||
13 | +import br.com.model.entity.Questionario; | ||
12 | import br.com.model.entity.RespostaQuestionario; | 14 | import br.com.model.entity.RespostaQuestionario; |
15 | +import br.com.util.DateUtil; | ||
13 | 16 | ||
14 | public class RespostaQuestionarioDao { | 17 | public class RespostaQuestionarioDao { |
15 | 18 | ||
16 | - public boolean salvar(List<RespostaQuestionario> lsRespostaQuestionario) { | ||
17 | - | ||
18 | - Connection con = null; | ||
19 | - int resultado = 0; | ||
20 | - StringBuffer sql; | ||
21 | - Statement estado; | ||
22 | - boolean salvoComSucesso = false; | ||
23 | - | ||
24 | - try { | ||
25 | - | ||
26 | - con = Conexao.conecta(); | ||
27 | - if (con != null) { | ||
28 | - con.setAutoCommit(false); | ||
29 | - // cria statement para executar a query | ||
30 | - estado = con.createStatement(); | ||
31 | - for (RespostaQuestionario respostaQuestionario : lsRespostaQuestionario) { | ||
32 | - sql = new StringBuffer(); | ||
33 | - | ||
34 | - sql.append("INSERT INTO resposta_questionario(id_pergunta, id_opcao_resposta, texto_resposta, dt_resposta_questionario, id_questionario)"); | ||
35 | - sql.append("VALUES (" | ||
36 | - + respostaQuestionario.getPergunta() | ||
37 | - .getIdPergunta() | ||
38 | - + "," | ||
39 | - + respostaQuestionario.getOpcaoResposta() | ||
40 | - .getIdOpcaoResposta() + ",'"); | ||
41 | - sql.append(respostaQuestionario.getOpcaoResposta() | ||
42 | - .getDescOpcaoResposta() + "','2016-08-17',"); | ||
43 | - sql.append(respostaQuestionario.getQuestionario() | ||
44 | - .getIdQuestionario() + ");"); | ||
45 | - | ||
46 | - resultado = estado.executeUpdate(sql.toString()); | ||
47 | - | ||
48 | - if (resultado == 0) { | ||
49 | - con.rollback(); | ||
50 | - break; | ||
51 | - } | ||
52 | - } | ||
53 | - if (resultado > 0) { | ||
54 | - con.commit(); | ||
55 | - salvoComSucesso = true; | ||
56 | - } | ||
57 | - } | ||
58 | - | ||
59 | - } | ||
60 | - | ||
61 | - catch (SQLException e) { | ||
62 | - System.err.print(e.getMessage()); | ||
63 | - } finally { | ||
64 | - if (con != null) { | ||
65 | - try { | ||
66 | - con.close(); | ||
67 | - lsRespostaQuestionario = null; | ||
68 | - } catch (SQLException e) { | ||
69 | - // TODO Auto-generated catch block | ||
70 | - e.printStackTrace(); | ||
71 | - } | ||
72 | - } | ||
73 | - } | ||
74 | - | ||
75 | - return salvoComSucesso; | ||
76 | - } | 19 | + |
20 | + public ResultSet retornarRespostaQuestionario(int id_resposta_questionario) { | ||
21 | + | ||
22 | + StringBuffer sql = new StringBuffer(); | ||
23 | + Connection con = Conexao.conecta(); | ||
24 | + Statement estado; | ||
25 | + ResultSet rset = null; | ||
26 | + | ||
27 | + try { | ||
28 | + estado = con.createStatement(); | ||
29 | + | ||
30 | + estado.execute(sql.toString()); | ||
31 | + sql.append(" SELECT id_resposta_questionario, id_pergunta, id_opcao_resposta, texto_resposta, dt_resposta_questionario, id_questionario"); | ||
32 | + sql.append(" FROM public.resposta_questionario where id_resposta_questionario = "+ id_resposta_questionario +";"); | ||
33 | + | ||
34 | + rset = estado.executeQuery(sql.toString()); | ||
35 | + | ||
36 | + return rset; | ||
37 | + } catch (SQLException e) { | ||
38 | + e.printStackTrace(); | ||
39 | + return null; | ||
40 | + | ||
41 | + }finally { | ||
42 | + if (con != null) { | ||
43 | + try { | ||
44 | + con.close(); | ||
45 | + } catch (SQLException e) { | ||
46 | + e.printStackTrace(); | ||
47 | + } | ||
48 | + } | ||
49 | + } | ||
50 | + | ||
51 | + } | ||
52 | + | ||
53 | + public List<RespostaQuestionario> retornarRespostaQuestionarioPorQuestionario(int idQuestionario) { | ||
54 | + | ||
55 | + StringBuffer sql = new StringBuffer(); | ||
56 | + Connection con = Conexao.conecta(); | ||
57 | + Statement estado; | ||
58 | + ResultSet rset = null; | ||
59 | + | ||
60 | + RespostaQuestionario respostaQuestionario; | ||
61 | + List<RespostaQuestionario> lsRespostaQuestionario = new ArrayList<RespostaQuestionario>(); | ||
62 | + | ||
63 | + try { | ||
64 | + estado = con.createStatement(); | ||
65 | + | ||
66 | + estado.execute(sql.toString()); | ||
67 | + sql.append(" SELECT id_resposta_questionario "); | ||
68 | + sql.append(" FROM public.resposta_questionario where dt_resposta_questionario = (SELECT max(dt_resposta_questionario) as dt_resposta_questionario "); | ||
69 | + sql.append(" FROM public.resposta_questionario where id_questionario = "+ idQuestionario +") and id_questionario = "+ idQuestionario +";"); | ||
70 | + | ||
71 | + rset = estado.executeQuery(sql.toString()); | ||
72 | + con.close(); | ||
73 | + | ||
74 | + while (rset.next()) { | ||
75 | + respostaQuestionario = new RespostaQuestionario(Integer.parseInt(rset.getString("id_resposta_questionario"))); | ||
76 | + lsRespostaQuestionario.add(respostaQuestionario); | ||
77 | + } | ||
78 | + | ||
79 | + } catch (SQLException e) { | ||
80 | + System.out.println("Deu erro!"); | ||
81 | + e.printStackTrace(); | ||
82 | + | ||
83 | + }finally { | ||
84 | + if (con != null) { | ||
85 | + try { | ||
86 | + con.close(); | ||
87 | + } catch (SQLException e) { | ||
88 | + e.printStackTrace(); | ||
89 | + } | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | + return lsRespostaQuestionario; | ||
94 | +} | ||
95 | + | ||
96 | + public boolean salvar(List<RespostaQuestionario> lsRespostaQuestionario) { | ||
97 | + | ||
98 | + Connection con = null; | ||
99 | + int resultado = 0; | ||
100 | + StringBuffer sql; | ||
101 | + Statement estado; | ||
102 | + boolean salvoComSucesso = false; | ||
103 | + | ||
104 | + try { | ||
105 | + | ||
106 | + con = Conexao.conecta(); | ||
107 | + if (con != null) { | ||
108 | + con.setAutoCommit(false); | ||
109 | + // cria statement para executar a query | ||
110 | + estado = con.createStatement(); | ||
111 | + for (RespostaQuestionario respostaQuestionario : lsRespostaQuestionario) { | ||
112 | + sql = new StringBuffer(); | ||
113 | + | ||
114 | + sql.append("INSERT INTO resposta_questionario(id_pergunta, id_opcao_resposta, texto_resposta, dt_resposta_questionario, id_questionario)"); | ||
115 | + sql.append("VALUES (" + respostaQuestionario.getPergunta().getIdPergunta() + "," | ||
116 | + + respostaQuestionario.getOpcaoResposta().getIdOpcaoResposta() + ",'"); | ||
117 | + sql.append(respostaQuestionario.getTextoResposta() + "','" | ||
118 | + + DateUtil.dataHoraAtual() + "',"); | ||
119 | + sql.append(respostaQuestionario.getQuestionario().getIdQuestionario() + ");"); | ||
120 | + | ||
121 | + resultado = estado.executeUpdate(sql.toString()); | ||
122 | + | ||
123 | + if (resultado == 0) { | ||
124 | + con.rollback(); | ||
125 | + break; | ||
126 | + } | ||
127 | + } | ||
128 | + if (resultado > 0) { | ||
129 | + con.commit(); | ||
130 | + salvoComSucesso = true; | ||
131 | + } | ||
132 | + } | ||
133 | + | ||
134 | + } | ||
135 | + | ||
136 | + catch (SQLException e) { | ||
137 | + System.err.print(e.getMessage()); | ||
138 | + } finally { | ||
139 | + if (con != null) { | ||
140 | + try { | ||
141 | + con.close(); | ||
142 | + lsRespostaQuestionario = null; | ||
143 | + } catch (SQLException e) { | ||
144 | + // TODO Auto-generated catch block | ||
145 | + e.printStackTrace(); | ||
146 | + } | ||
147 | + } | ||
148 | + } | ||
77 | 149 | ||
150 | + return salvoComSucesso; | ||
151 | + } | ||
152 | + | ||
78 | } | 153 | } |