| 1 | |
package br.mia.test.model.dao; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.sql.Connection; |
| 5 | |
import java.sql.PreparedStatement; |
| 6 | |
import java.sql.ResultSet; |
| 7 | |
import java.sql.SQLException; |
| 8 | |
import java.util.ArrayList; |
| 9 | |
|
| 10 | |
import br.mia.test.seguranca.Vulnerabilidade; |
| 11 | |
import br.mia.test.seguranca.VulnerabilidadeValor; |
| 12 | |
|
| 13 | 0 | public class DAOSecurity { |
| 14 | |
|
| 15 | |
public void updateIDVulnerabilidadeValue(int id1, int id2) |
| 16 | |
throws InstantiationException, IllegalAccessException, |
| 17 | |
ClassNotFoundException, SQLException, IOException { |
| 18 | 0 | Connection con = Singleton.getConnection(); |
| 19 | |
|
| 20 | 0 | PreparedStatement st = con |
| 21 | 0 | .prepareStatement("update vulnerabilidades set id=0 where id=?"); |
| 22 | |
|
| 23 | 0 | st.setInt(1, id1); |
| 24 | 0 | st.executeUpdate(); |
| 25 | |
|
| 26 | 0 | st = con |
| 27 | 0 | .prepareStatement("update vulnerabilidades set id=? where id=?"); |
| 28 | |
|
| 29 | 0 | st.setInt(1, id1); |
| 30 | 0 | st.setInt(2, id2); |
| 31 | |
|
| 32 | 0 | st.executeUpdate(); |
| 33 | |
|
| 34 | 0 | st = con |
| 35 | 0 | .prepareStatement("update vulnerabilidades set id=? where id=0"); |
| 36 | |
|
| 37 | 0 | st.setInt(1, id2); |
| 38 | 0 | st.executeUpdate(); |
| 39 | |
|
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public void delete(Vulnerabilidade vulnerabilidade) |
| 43 | |
throws InstantiationException, IllegalAccessException, |
| 44 | |
ClassNotFoundException, SQLException, IOException { |
| 45 | 0 | Connection con = Singleton.getConnection(); |
| 46 | |
|
| 47 | 0 | PreparedStatement st = con |
| 48 | 0 | .prepareStatement("delete from tipovulnerabilidade where id=?"); |
| 49 | |
|
| 50 | 0 | st.setInt(1, vulnerabilidade.getId()); |
| 51 | 0 | st.executeUpdate(); |
| 52 | |
|
| 53 | 0 | } |
| 54 | |
|
| 55 | |
public void deleteValor(VulnerabilidadeValor vulnerabilidadeValor) |
| 56 | |
throws InstantiationException, IllegalAccessException, |
| 57 | |
ClassNotFoundException, SQLException, IOException { |
| 58 | 0 | Connection con = Singleton.getConnection(); |
| 59 | |
|
| 60 | 0 | PreparedStatement st = con |
| 61 | 0 | .prepareStatement("delete from vulnerabilidades where id=?"); |
| 62 | |
|
| 63 | 0 | st.setInt(1, vulnerabilidadeValor.getId()); |
| 64 | 0 | st.executeUpdate(); |
| 65 | |
|
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public Vulnerabilidade update(Vulnerabilidade vulnerabilidade) |
| 69 | |
throws SQLException, InstantiationException, |
| 70 | |
IllegalAccessException, ClassNotFoundException, |
| 71 | |
IOException { |
| 72 | 0 | Connection con = Singleton.getConnection(); |
| 73 | 0 | if (vulnerabilidade.getId() == 0) { |
| 74 | 0 | vulnerabilidade = this.insert(vulnerabilidade); |
| 75 | |
|
| 76 | |
} else { |
| 77 | 0 | PreparedStatement st = con |
| 78 | 0 | .prepareStatement("update tipovulnerabilidade set descricao=?,card=? where id=?"); |
| 79 | |
|
| 80 | 0 | st.setString(1, vulnerabilidade.getName()); |
| 81 | 0 | st.setString(2, vulnerabilidade.getCard()); |
| 82 | |
|
| 83 | 0 | st.setInt(3, vulnerabilidade.getId()); |
| 84 | |
|
| 85 | 0 | st.executeUpdate(); |
| 86 | |
|
| 87 | |
} |
| 88 | |
|
| 89 | 0 | return vulnerabilidade; |
| 90 | |
|
| 91 | |
} |
| 92 | |
|
| 93 | |
public VulnerabilidadeValor updateValor( |
| 94 | |
VulnerabilidadeValor vulnerabilidadeValor) throws SQLException, |
| 95 | |
InstantiationException, IllegalAccessException, |
| 96 | |
ClassNotFoundException, IOException { |
| 97 | 0 | Connection con = Singleton.getConnection(); |
| 98 | |
|
| 99 | 0 | if (vulnerabilidadeValor.getId() != 0) { |
| 100 | 0 | PreparedStatement st = con |
| 101 | 0 | .prepareStatement("update vulnerabilidades set descricao=?,observacao=? where id=?"); |
| 102 | |
|
| 103 | 0 | st.setString(1, vulnerabilidadeValor.getValor().toString()); |
| 104 | 0 | st.setString(2, vulnerabilidadeValor.getObservacao()); |
| 105 | |
|
| 106 | 0 | st.setInt(3, vulnerabilidadeValor.getId()); |
| 107 | |
|
| 108 | 0 | st.executeUpdate(); |
| 109 | |
|
| 110 | |
} |
| 111 | |
|
| 112 | 0 | return vulnerabilidadeValor; |
| 113 | |
|
| 114 | |
} |
| 115 | |
|
| 116 | |
public ArrayList<Vulnerabilidade> selectAll() throws InstantiationException, |
| 117 | |
IllegalAccessException, ClassNotFoundException, SQLException, |
| 118 | |
IOException { |
| 119 | 0 | Connection con = Singleton.getConnection(); |
| 120 | 0 | ArrayList<Vulnerabilidade> arrays = new ArrayList<Vulnerabilidade>(); |
| 121 | |
|
| 122 | 0 | PreparedStatement st1 = con |
| 123 | 0 | .prepareStatement("Select descricao,card,id from tipovulnerabilidade "); |
| 124 | |
|
| 125 | 0 | ResultSet rs1 = st1.executeQuery(); |
| 126 | |
|
| 127 | 0 | while (rs1.next()) { |
| 128 | 0 | String descricao = rs1.getString(1); |
| 129 | 0 | String card = rs1.getString(2); |
| 130 | 0 | int id = rs1.getInt(3); |
| 131 | 0 | Vulnerabilidade vulnerabilidade = new Vulnerabilidade(); |
| 132 | 0 | vulnerabilidade.setName(descricao); |
| 133 | 0 | vulnerabilidade.setCard(card); |
| 134 | 0 | vulnerabilidade.setId(id); |
| 135 | |
|
| 136 | 0 | ArrayList<VulnerabilidadeValor> valores = this.selectAllValores(vulnerabilidade); |
| 137 | |
|
| 138 | 0 | vulnerabilidade.getValues().addAll(valores); |
| 139 | |
|
| 140 | 0 | arrays.add(vulnerabilidade); |
| 141 | |
|
| 142 | |
} |
| 143 | |
|
| 144 | 0 | return arrays; |
| 145 | |
|
| 146 | |
} |
| 147 | |
|
| 148 | |
public ArrayList<VulnerabilidadeValor> selectAllValores(Vulnerabilidade vulnerabilidade) |
| 149 | |
throws InstantiationException, IllegalAccessException, |
| 150 | |
ClassNotFoundException, SQLException, IOException { |
| 151 | 0 | Connection con = Singleton.getConnection(); |
| 152 | 0 | ArrayList<VulnerabilidadeValor> arrays = new ArrayList<VulnerabilidadeValor>(); |
| 153 | |
|
| 154 | 0 | PreparedStatement st1 = con |
| 155 | 0 | .prepareStatement("Select descricao,observacao,id from vulnerabilidades where idtipo=?"); |
| 156 | |
|
| 157 | 0 | st1.setInt(1, vulnerabilidade.getId()); |
| 158 | |
|
| 159 | 0 | ResultSet rs1 = st1.executeQuery(); |
| 160 | |
|
| 161 | 0 | while (rs1.next()) { |
| 162 | 0 | String descricao = rs1.getString(1); |
| 163 | 0 | String observacao = rs1.getString(2); |
| 164 | 0 | int id = rs1.getInt(3); |
| 165 | 0 | VulnerabilidadeValor vulnerabilidadeValor = new VulnerabilidadeValor(); |
| 166 | 0 | vulnerabilidadeValor.setValor(descricao); |
| 167 | 0 | vulnerabilidadeValor.setObservacao(observacao); |
| 168 | 0 | vulnerabilidadeValor.setId(id); |
| 169 | |
|
| 170 | 0 | arrays.add(vulnerabilidadeValor); |
| 171 | |
|
| 172 | |
} |
| 173 | |
|
| 174 | 0 | return arrays; |
| 175 | |
|
| 176 | |
} |
| 177 | |
|
| 178 | |
public VulnerabilidadeValor insertValor(Vulnerabilidade vulnerabilidade, |
| 179 | |
VulnerabilidadeValor vulnerabilidadeValor) throws SQLException, |
| 180 | |
InstantiationException, IllegalAccessException, |
| 181 | |
ClassNotFoundException, IOException { |
| 182 | 0 | Connection con = Singleton.getConnection(); |
| 183 | 0 | if (vulnerabilidadeValor.getId() == 0) { |
| 184 | 0 | PreparedStatement st = con |
| 185 | 0 | .prepareStatement("Insert into vulnerabilidades(descricao,observacao,idtipo) values (?,?,?)"); |
| 186 | |
|
| 187 | 0 | st.setString(1, vulnerabilidadeValor.getValor().toString()); |
| 188 | 0 | st.setString(2, vulnerabilidadeValor.getObservacao()); |
| 189 | 0 | st.setInt(3, vulnerabilidade.getId()); |
| 190 | |
|
| 191 | 0 | st.executeUpdate(); |
| 192 | |
|
| 193 | 0 | st = con |
| 194 | 0 | .prepareStatement("select id from vulnerabilidades where descricao=? and observacao=? and idtipo=?"); |
| 195 | 0 | st.setString(1, vulnerabilidadeValor.getValor().toString()); |
| 196 | 0 | st.setString(2, vulnerabilidadeValor.getObservacao()); |
| 197 | 0 | st.setInt(3, vulnerabilidade.getId()); |
| 198 | |
|
| 199 | 0 | ResultSet rs = st.executeQuery(); |
| 200 | |
|
| 201 | 0 | rs.next(); |
| 202 | |
|
| 203 | 0 | int id = rs.getInt(1); |
| 204 | |
|
| 205 | 0 | vulnerabilidadeValor.setId(id); |
| 206 | |
|
| 207 | |
} |
| 208 | |
|
| 209 | 0 | return vulnerabilidadeValor; |
| 210 | |
} |
| 211 | |
|
| 212 | |
public Vulnerabilidade insert(Vulnerabilidade vulnerabilidade) |
| 213 | |
throws SQLException, InstantiationException, |
| 214 | |
IllegalAccessException, ClassNotFoundException, |
| 215 | |
IOException { |
| 216 | 0 | Connection con = Singleton.getConnection(); |
| 217 | 0 | if (vulnerabilidade.getId() == 0) { |
| 218 | 0 | PreparedStatement st = con |
| 219 | 0 | .prepareStatement("Insert into tipovulnerabilidade(descricao,card) values (?,?)"); |
| 220 | |
|
| 221 | 0 | st.setString(1, vulnerabilidade.getName()); |
| 222 | 0 | st.setString(2, vulnerabilidade.getCard()); |
| 223 | |
|
| 224 | 0 | st.executeUpdate(); |
| 225 | |
|
| 226 | 0 | st = con |
| 227 | 0 | .prepareStatement("select id from tipovulnerabilidade where descricao=? and card=?"); |
| 228 | 0 | st.setString(1, vulnerabilidade.getName()); |
| 229 | 0 | st.setString(2, vulnerabilidade.getCard()); |
| 230 | |
|
| 231 | 0 | ResultSet rs = st.executeQuery(); |
| 232 | |
|
| 233 | 0 | rs.next(); |
| 234 | |
|
| 235 | 0 | int id = rs.getInt(1); |
| 236 | |
|
| 237 | 0 | vulnerabilidade.setId(id); |
| 238 | |
|
| 239 | |
} |
| 240 | |
|
| 241 | 0 | return vulnerabilidade; |
| 242 | |
} |
| 243 | |
|
| 244 | |
} |