1 | package ve.gob.cenditel.murachi; |
---|
2 | |
---|
3 | import java.sql.Connection; |
---|
4 | import java.sql.DriverManager; |
---|
5 | import java.sql.ResultSet; |
---|
6 | import java.sql.SQLException; |
---|
7 | import java.sql.Statement; |
---|
8 | |
---|
9 | import org.apache.log4j.Logger; |
---|
10 | |
---|
11 | /** |
---|
12 | * Clase para obtener estadisticas del servicio murachi. |
---|
13 | * |
---|
14 | * @author aaraujo |
---|
15 | * |
---|
16 | */ |
---|
17 | public class MURACHIStatistic { |
---|
18 | |
---|
19 | private String databaseName; |
---|
20 | |
---|
21 | private String databaseHost; |
---|
22 | |
---|
23 | private String databasePort; |
---|
24 | |
---|
25 | private String databaseLogin; |
---|
26 | |
---|
27 | private String databasePassword; |
---|
28 | |
---|
29 | private String databaseConnection; |
---|
30 | |
---|
31 | final static Logger logger = Logger.getLogger(MURACHIStatistic.class); |
---|
32 | |
---|
33 | /** |
---|
34 | * Constructor de la clase |
---|
35 | * |
---|
36 | * @param dbLogin login para la conexion de base de datos |
---|
37 | * @param dbPassword contrasena para la conexion de base de datos |
---|
38 | * |
---|
39 | */ |
---|
40 | MURACHIStatistic(String dbHost, String dbPort, String dbName, String dbLogin, String dbPassword) { |
---|
41 | databaseName = dbName; |
---|
42 | databaseHost = dbHost; |
---|
43 | databasePort = dbPort; |
---|
44 | databaseLogin = dbLogin; |
---|
45 | databasePassword = dbPassword; |
---|
46 | databaseConnection = "jdbc:postgresql://"+ |
---|
47 | databaseHost + |
---|
48 | ":" + |
---|
49 | databasePort + |
---|
50 | "/" + |
---|
51 | databaseName; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | public void createDatabaseTables() throws InstantiationException, IllegalAccessException { |
---|
56 | Connection conn = null; |
---|
57 | Statement stmt = null; |
---|
58 | try { |
---|
59 | Class.forName("org.postgresql.Driver").newInstance(); |
---|
60 | System.out.println("postgresql JDBC Driver Registered!"); |
---|
61 | |
---|
62 | conn = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword); |
---|
63 | |
---|
64 | stmt = conn.createStatement(); |
---|
65 | String sql = "CREATE TABLE SIGNATURES " + |
---|
66 | "(SIGNATURE_ID SERIAL PRIMARY KEY NOT NULL," + |
---|
67 | " TYPE CHAR(4) NOT NULL)"; |
---|
68 | stmt.executeUpdate(sql); |
---|
69 | System.out.println("table SIGNATURES created!"); |
---|
70 | |
---|
71 | stmt = conn.createStatement(); |
---|
72 | |
---|
73 | sql = "CREATE TABLE VERIFICATIONS " + |
---|
74 | "(VERIFICATION_ID SERIAL PRIMARY KEY NOT NULL,"+ |
---|
75 | "TYPE CHAR(4) NOT NULL)"; |
---|
76 | stmt.executeUpdate(sql); |
---|
77 | System.out.println("table VERIFICATIONS created!"); |
---|
78 | |
---|
79 | stmt = conn.createStatement(); |
---|
80 | |
---|
81 | sql = "CREATE TABLE SIGNATURES_ERROR " + |
---|
82 | "(SIGNATURE_ERROR_ID SERIAL PRIMARY KEY NOT NULL, "+ |
---|
83 | "TYPE CHAR(4) NOT NULL)"; |
---|
84 | stmt.executeUpdate(sql); |
---|
85 | System.out.println("table SIGNATURE_ERROR created!"); |
---|
86 | |
---|
87 | stmt.close(); |
---|
88 | //conn.close(); |
---|
89 | |
---|
90 | |
---|
91 | } catch (ClassNotFoundException e) { |
---|
92 | //Cannot register postgresql MySQL driver |
---|
93 | System.out.println("This is something you have not add in postgresql library to classpath!"); |
---|
94 | e.printStackTrace(); |
---|
95 | }catch (SQLException ex) { |
---|
96 | // handle any errors |
---|
97 | System.out.println("SQLException: " + ex.getMessage()); |
---|
98 | System.out.println("SQLState: " + ex.getSQLState()); |
---|
99 | System.out.println("VendorError: " + ex.getErrorCode()); |
---|
100 | }finally{ |
---|
101 | //After using connection, release the postgresql resource. |
---|
102 | try { |
---|
103 | conn.close(); |
---|
104 | } catch (SQLException e) { |
---|
105 | logger.error(e.getMessage()); |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Incrementa en una unidad la cuenta de las firmas realizadas con el servicio |
---|
112 | * |
---|
113 | * @param type tipo de firma: 0->PDF, 1->BDOC |
---|
114 | * |
---|
115 | */ |
---|
116 | public void incrementSignatures(int type) { |
---|
117 | Connection c = null; |
---|
118 | Statement stmt = null; |
---|
119 | try { |
---|
120 | Class.forName("org.postgresql.Driver"); |
---|
121 | |
---|
122 | //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword); |
---|
123 | c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword); |
---|
124 | |
---|
125 | c.setAutoCommit(false); |
---|
126 | //System.out.println("Opened database successfully"); |
---|
127 | |
---|
128 | stmt = c.createStatement(); |
---|
129 | String signatureType = ""; |
---|
130 | |
---|
131 | if (type == 0) { |
---|
132 | signatureType = "pdf"; |
---|
133 | }else if (type == 1){ |
---|
134 | signatureType = "bdoc"; |
---|
135 | }else{ |
---|
136 | signatureType = "unknown"; |
---|
137 | } |
---|
138 | |
---|
139 | String sql = "INSERT INTO SIGNATURES (TYPE) VALUES ('"+ signatureType +"');"; |
---|
140 | stmt.executeUpdate(sql); |
---|
141 | stmt.close(); |
---|
142 | c.commit(); |
---|
143 | c.close(); |
---|
144 | } catch (Exception e) { |
---|
145 | logger.error(e.getClass().getName()+": "+ e.getMessage()); |
---|
146 | System.err.println( e.getClass().getName()+": "+ e.getMessage() ); |
---|
147 | } |
---|
148 | //System.out.println("Records created successfully"); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Incrementa en una unidad la cuenta de las firmas incompletas realizadas con el servicio |
---|
153 | * |
---|
154 | * @param type tipo de firma: 0->PDF, 1->BDOC |
---|
155 | * |
---|
156 | */ |
---|
157 | public void incrementErrorSignatures(int type) { |
---|
158 | Connection c = null; |
---|
159 | Statement stmt = null; |
---|
160 | try { |
---|
161 | Class.forName("org.postgresql.Driver"); |
---|
162 | |
---|
163 | //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword); |
---|
164 | c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword); |
---|
165 | |
---|
166 | c.setAutoCommit(false); |
---|
167 | //System.out.println("Opened database successfully"); |
---|
168 | |
---|
169 | stmt = c.createStatement(); |
---|
170 | String signatureType = ""; |
---|
171 | |
---|
172 | if (type == 0) { |
---|
173 | signatureType = "pdf"; |
---|
174 | }else if (type == 1){ |
---|
175 | signatureType = "bdoc"; |
---|
176 | }else{ |
---|
177 | signatureType = "unknown"; |
---|
178 | } |
---|
179 | |
---|
180 | String sql = "INSERT INTO SIGNATURES_ERROR (TYPE) VALUES ('"+ signatureType +"');"; |
---|
181 | stmt.executeUpdate(sql); |
---|
182 | stmt.close(); |
---|
183 | c.commit(); |
---|
184 | c.close(); |
---|
185 | } catch (Exception e) { |
---|
186 | logger.error(e.getClass().getName()+": "+ e.getMessage()); |
---|
187 | System.err.println( e.getClass().getName()+": "+ e.getMessage() ); |
---|
188 | } |
---|
189 | //System.out.println("Records created successfully"); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | /** |
---|
194 | * Incrementa en una unidad la cuenta de las verificaciones de firmas realizadas con el servicio |
---|
195 | * |
---|
196 | * @param type tipo de firma: 0->PDF, 1->BDOC |
---|
197 | * |
---|
198 | */ |
---|
199 | public void incrementVerifications(int type) { |
---|
200 | Connection c = null; |
---|
201 | Statement stmt = null; |
---|
202 | try { |
---|
203 | Class.forName("org.postgresql.Driver"); |
---|
204 | |
---|
205 | //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword); |
---|
206 | c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword); |
---|
207 | |
---|
208 | c.setAutoCommit(false); |
---|
209 | //System.out.println("Opened database successfully"); |
---|
210 | |
---|
211 | stmt = c.createStatement(); |
---|
212 | String signatureType = ""; |
---|
213 | |
---|
214 | if (type == 0) { |
---|
215 | signatureType = "pdf"; |
---|
216 | }else if (type == 1){ |
---|
217 | signatureType = "bdoc"; |
---|
218 | }else{ |
---|
219 | signatureType = "unkn"; |
---|
220 | } |
---|
221 | |
---|
222 | String sql = "INSERT INTO VERIFICATIONS (TYPE) VALUES ('"+ signatureType +"');"; |
---|
223 | stmt.executeUpdate(sql); |
---|
224 | stmt.close(); |
---|
225 | c.commit(); |
---|
226 | c.close(); |
---|
227 | } catch (Exception e) { |
---|
228 | logger.error(e.getClass().getName()+": "+ e.getMessage()); |
---|
229 | System.err.println( e.getClass().getName()+": "+ e.getMessage() ); |
---|
230 | } |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | /** |
---|
237 | * Retorna el número de firmas realizadas exitosamente con el servicio. |
---|
238 | * |
---|
239 | * @return número de firmas realizadas exitosamente con el servicio. |
---|
240 | */ |
---|
241 | public int countOfSigantures() { |
---|
242 | |
---|
243 | Connection c = null; |
---|
244 | Statement stmt = null; |
---|
245 | int rowCount = 0; |
---|
246 | |
---|
247 | try { |
---|
248 | Class.forName("org.postgresql.Driver"); |
---|
249 | //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword); |
---|
250 | c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword); |
---|
251 | |
---|
252 | c.setAutoCommit(false); |
---|
253 | //System.out.println("Opened database successfully"); |
---|
254 | |
---|
255 | stmt = c.createStatement(); |
---|
256 | ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM SIGNATURES"); |
---|
257 | // get the number of rows from the result set |
---|
258 | rs.next(); |
---|
259 | rowCount = rs.getInt(1); |
---|
260 | //System.out.println(rowCount); |
---|
261 | |
---|
262 | stmt.close(); |
---|
263 | c.commit(); |
---|
264 | c.close(); |
---|
265 | } catch (Exception e) { |
---|
266 | logger.error(e.getClass().getName()+": "+ e.getMessage()); |
---|
267 | System.err.println( e.getClass().getName()+": "+ e.getMessage() ); |
---|
268 | rowCount = -1; |
---|
269 | } |
---|
270 | //System.out.println("Records created successfully"); |
---|
271 | |
---|
272 | return rowCount; |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * Retorna el número de firmas que no se completaron en el servicio. |
---|
277 | * |
---|
278 | * @return número de firmas que no se completaron en el servicio. |
---|
279 | */ |
---|
280 | public int countOfSiganturesFailed() { |
---|
281 | |
---|
282 | Connection c = null; |
---|
283 | Statement stmt = null; |
---|
284 | int rowCount = 0; |
---|
285 | |
---|
286 | try { |
---|
287 | Class.forName("org.postgresql.Driver"); |
---|
288 | //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword); |
---|
289 | c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword); |
---|
290 | |
---|
291 | c.setAutoCommit(false); |
---|
292 | //System.out.println("Opened database successfully"); |
---|
293 | |
---|
294 | stmt = c.createStatement(); |
---|
295 | ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM SIGNATURES_ERROR"); |
---|
296 | // get the number of rows from the result set |
---|
297 | rs.next(); |
---|
298 | rowCount = rs.getInt(1); |
---|
299 | //System.out.println(rowCount); |
---|
300 | |
---|
301 | stmt.close(); |
---|
302 | c.commit(); |
---|
303 | c.close(); |
---|
304 | } catch (Exception e) { |
---|
305 | logger.error(e.getClass().getName()+": "+ e.getMessage()); |
---|
306 | System.err.println( e.getClass().getName()+": "+ e.getMessage() ); |
---|
307 | rowCount = -1; |
---|
308 | } |
---|
309 | //System.out.println("Records created successfully"); |
---|
310 | |
---|
311 | return rowCount; |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | * Retorna el número de verificaciones de firma electrónica realizadas en el servicio |
---|
316 | * |
---|
317 | * @return número de verificaciones de firma electrónica realizadas en el servicio |
---|
318 | */ |
---|
319 | public int countOfVerifications() { |
---|
320 | |
---|
321 | Connection c = null; |
---|
322 | Statement stmt = null; |
---|
323 | int rowCount = 0; |
---|
324 | |
---|
325 | try { |
---|
326 | Class.forName("org.postgresql.Driver"); |
---|
327 | //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword); |
---|
328 | c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword); |
---|
329 | |
---|
330 | c.setAutoCommit(false); |
---|
331 | //System.out.println("Opened database successfully"); |
---|
332 | |
---|
333 | stmt = c.createStatement(); |
---|
334 | ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM VERIFICATIONS"); |
---|
335 | // get the number of rows from the result set |
---|
336 | rs.next(); |
---|
337 | rowCount = rs.getInt(1); |
---|
338 | //System.out.println(rowCount); |
---|
339 | |
---|
340 | stmt.close(); |
---|
341 | c.commit(); |
---|
342 | c.close(); |
---|
343 | } catch (Exception e) { |
---|
344 | logger.error(e.getClass().getName()+": "+ e.getMessage()); |
---|
345 | System.err.println( e.getClass().getName()+": "+ e.getMessage() ); |
---|
346 | rowCount = -1; |
---|
347 | } |
---|
348 | //System.out.println("Records created successfully"); |
---|
349 | |
---|
350 | return rowCount; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | } |
---|