Merge 1806 code of vid-common
[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                            System.out.println("Connection Successful");                 
65                         return con;
66                         
67                 }
68                 
69                 public static void cleanup(ResultSet rs, PreparedStatement st, Connection c) {
70                         if (rs != null) {
71                                 closeResultSet(rs);
72                         }
73                         if (st != null) {
74                                 closePreparedStatement(st);
75                         }
76                         if (c != null) {
77                                 rollbackAndCloseConnection(c);
78                         }
79                 }
80
81         private static void rollbackAndCloseConnection(Connection c) {
82                 try {
83             c.rollback();
84         } catch (Exception e) {
85             if (logger != null)
86                 logger.error("Error when trying to rollback connection", e);
87         }
88                 try {
89             c.close();
90         } catch (Exception e) {
91             if (logger != null)
92                 logger.error("Error when trying to close connection", e);
93         }
94         }
95
96         private static void closePreparedStatement(PreparedStatement st) {
97                 try {
98             st.close();
99         } catch (Exception e) {
100             if (logger != null)
101                 logger.error("Error when trying to close statement", e);
102         }
103         }
104
105         private static void closeResultSet(ResultSet rs) {
106                 try {
107             rs.close();
108         } catch (Exception e) {
109             if (logger != null)
110                 logger.error("Error when trying to close result set", e);
111         }
112         }
113 }