source: murachi/murachi/src/main/java/ve/gob/cenditel/murachi/MURACHIStatistic.java @ fc4d3bb

Last change on this file since fc4d3bb was fc4d3bb, checked in by antonioaraujob <aaraujo@…>, 9 years ago

Creada clase MURACHIStatistic para gestionar estadísticas básicas de uso del servicio Murachi (firmas y verificaciones realizadas). Creado recurso /estadisticas para obtener reporte básico del número de firmas y verificaciones realizadas en el servicio.

  • Property mode set to 100644
File size: 8.8 KB
Line 
1package ve.gob.cenditel.murachi;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.SQLException;
7import java.sql.Statement;
8
9import org.apache.log4j.Logger;
10 
11/**
12 * Clase para obtener estadisticas del servicio murachi.
13 *
14 * @author aaraujo
15 *
16 */
17public 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            stmt.executeUpdate(sql);                       
76            System.out.println("table VERIFICATIONS created!");
77           
78            stmt = conn.createStatement();
79           
80            sql = "CREATE TABLE SIGNATURES_ERROR " +
81                    "(SIGNATURE_ERROR_ID SERIAL PRIMARY KEY NOT NULL)";
82            stmt.executeUpdate(sql);
83            System.out.println("table SIGNATURE_ERROR created!");
84           
85            stmt.close();
86            //conn.close();
87           
88           
89        } catch (ClassNotFoundException e) {
90            //Cannot register postgresql MySQL driver
91            System.out.println("This is something you have not add in postgresql library to classpath!");
92            e.printStackTrace();
93        }catch (SQLException ex) {
94            // handle any errors
95            System.out.println("SQLException: " + ex.getMessage());
96            System.out.println("SQLState: " + ex.getSQLState());
97            System.out.println("VendorError: " + ex.getErrorCode());
98        }finally{
99            //After using connection, release the postgresql resource.
100            try {
101                conn.close();
102            } catch (SQLException e) {
103                logger.error(e.getMessage());
104            }
105        }
106    }
107   
108    /**
109     * Incrementa en una unidad la cuenta de las firmas realizadas con el servicio
110     *
111     * @param type tipo de firma: 0->PDF, 1->BDOC
112     *
113     */
114    public void incrementSignatures(int type) {
115        Connection c = null;
116        Statement stmt = null;
117        try {
118           Class.forName("org.postgresql.Driver");
119           
120           //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword);
121           c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword);
122           
123           c.setAutoCommit(false);
124           //System.out.println("Opened database successfully");
125
126           stmt = c.createStatement();
127           String signatureType = "";
128           
129           if (type == 0) {
130                   signatureType = "pdf";
131           }else if (type == 1){
132                   signatureType = "bdoc";
133           }else{
134                   signatureType = "unknown";
135           }
136                   
137           String sql = "INSERT INTO SIGNATURES (TYPE) VALUES ('"+ signatureType +"');";
138           stmt.executeUpdate(sql);
139           stmt.close();
140           c.commit();
141           c.close();
142        } catch (Exception e) {
143                logger.error(e.getClass().getName()+": "+ e.getMessage());
144            System.err.println( e.getClass().getName()+": "+ e.getMessage() );
145        }
146        //System.out.println("Records created successfully");
147    }
148   
149    /**
150     * Retorna el número de firmas realizadas exitosamente con el servicio.
151     *
152     * @return número de firmas realizadas exitosamente con el servicio.
153     */
154    public int countOfSigantures() {
155       
156        Connection c = null;
157        Statement stmt = null;
158        int rowCount = 0;
159       
160        try {
161           Class.forName("org.postgresql.Driver");
162           //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword);
163           c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword);
164           
165           c.setAutoCommit(false);
166           //System.out.println("Opened database successfully");
167
168           stmt = c.createStatement();
169           ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM SIGNATURES");
170           // get the number of rows from the result set
171           rs.next();
172           rowCount = rs.getInt(1);
173           //System.out.println(rowCount);
174         
175           stmt.close();
176           c.commit();
177           c.close();
178        } catch (Exception e) {
179                logger.error(e.getClass().getName()+": "+ e.getMessage());
180            System.err.println( e.getClass().getName()+": "+ e.getMessage() );
181            rowCount = -1;
182        }
183        //System.out.println("Records created successfully");
184               
185                return rowCount;       
186    }
187   
188    /**
189     * Retorna el número de firmas que no se completaron en el servicio.
190     *
191     * @return número de firmas que no se completaron en el servicio.
192     */
193    public int countOfSiganturesFailed() {
194       
195        Connection c = null;
196        Statement stmt = null;
197        int rowCount = 0;
198       
199        try {
200           Class.forName("org.postgresql.Driver");
201           //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword);
202           c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword);
203           
204           c.setAutoCommit(false);
205           //System.out.println("Opened database successfully");
206
207           stmt = c.createStatement();
208           ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM SIGNATURES_ERROR");
209           // get the number of rows from the result set
210           rs.next();
211           rowCount = rs.getInt(1);
212           //System.out.println(rowCount);
213         
214           stmt.close();
215           c.commit();
216           c.close();
217        } catch (Exception e) {
218                logger.error(e.getClass().getName()+": "+ e.getMessage());
219            System.err.println( e.getClass().getName()+": "+ e.getMessage() );
220            rowCount = -1;
221        }
222        //System.out.println("Records created successfully");
223               
224                return rowCount;       
225    }
226 
227    /**
228     * Retorna el número de verificaciones de firma electrónica realizadas en el servicio
229     *
230     * @return número de verificaciones de firma electrónica realizadas en el servicio
231     */
232    public int countOfVerifications() {
233       
234        Connection c = null;
235        Statement stmt = null;
236        int rowCount = 0;
237       
238        try {
239           Class.forName("org.postgresql.Driver");
240           //c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/databasemurachi", databaseLogin, databasePassword);
241           c = DriverManager.getConnection(databaseConnection, databaseLogin, databasePassword);
242           
243           c.setAutoCommit(false);
244           //System.out.println("Opened database successfully");
245
246           stmt = c.createStatement();
247           ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM VERIFICATIONS");
248           // get the number of rows from the result set
249           rs.next();
250           rowCount = rs.getInt(1);
251           //System.out.println(rowCount);
252         
253           stmt.close();
254           c.commit();
255           c.close();
256        } catch (Exception e) {
257                logger.error(e.getClass().getName()+": "+ e.getMessage());
258            System.err.println( e.getClass().getName()+": "+ e.getMessage() );
259            rowCount = -1;
260        }
261        //System.out.println("Records created successfully");
262               
263                return rowCount;       
264    }
265   
266   
267}
Note: See TracBrowser for help on using the repository browser.