65fc5217f6690a35e9d8065d7f45699684699c04
[vid.git] / vid-app-common / src / main / java / org / onap / vid / dao / FnAppDoaImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.dao;
22
23 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
24
25 import java.sql.*;
26
27
28 public class FnAppDoaImpl {
29
30         /** The logger. */
31         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnAppDoaImpl.class);
32                 
33                 public int getProfileCount(String driver, String URL, String username, String password) {
34                         Connection dbc = null;
35                         PreparedStatement pst = null;
36                         ResultSet rs = null;
37                         String q = null;
38                         int count = 0;
39                         try {
40                                         dbc = getConnection(URL,username,password);
41                                    logger.debug(EELFLoggerDelegate.debugLogger, "getConnection:::"+ dbc);
42                                 q = "select count(*) from fn_app";
43                                         pst = dbc.prepareStatement(q);
44                                         rs = pst.executeQuery();
45                                         
46                                         if (rs.next())
47                                                 count = rs.getInt(1);
48                         } catch(Exception ex) {
49                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
50                         } finally {
51                                 cleanup(rs,pst,dbc);
52                         }
53                         logger.debug(EELFLoggerDelegate.debugLogger, "count:::"+ count);
54                         return count;
55                 }
56
57                 public static Connection getConnection(String url, String username, String password) throws SQLException {
58                         java.sql.Connection con=null;
59                 
60                         if( url!=null && username!=null && password!=null ){
61                             con = DriverManager.getConnection(url, username, password);
62                         }
63
64                         logger.info("Connection Successful");
65
66                         return con;
67                         
68                 }
69                 
70                 public static void cleanup(ResultSet rs, PreparedStatement st, Connection c) {
71                         if (rs != null) {
72                                 closeResultSet(rs);
73                         }
74                         if (st != null) {
75                                 closePreparedStatement(st);
76                         }
77                         if (c != null) {
78                                 rollbackAndCloseConnection(c);
79                         }
80                 }
81
82         private static void rollbackAndCloseConnection(Connection c) {
83                 try {
84             c.rollback();
85         } catch (Exception e) {
86             if (logger != null)
87                 logger.error("Error when trying to rollback connection", e);
88         }
89                 try {
90             c.close();
91         } catch (Exception e) {
92             if (logger != null)
93                 logger.error("Error when trying to close connection", e);
94         }
95         }
96
97         private static void closePreparedStatement(PreparedStatement st) {
98                 try {
99             st.close();
100         } catch (Exception e) {
101             if (logger != null)
102                 logger.error("Error when trying to close statement", e);
103         }
104         }
105
106         private static void closeResultSet(ResultSet rs) {
107                 try {
108             rs.close();
109         } catch (Exception e) {
110             if (logger != null)
111                 logger.error("Error when trying to close result set", e);
112         }
113         }
114 }