org.onap migration
[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 java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.URI;
28 import java.net.URL;
29 import java.sql.Connection;
30 import java.sql.DriverManager;
31 import java.sql.PreparedStatement;
32 import java.sql.ResultSet;
33 import java.sql.SQLException;
34 import java.util.Properties;
35
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37
38
39 public class FnAppDoaImpl {
40
41         /** The logger. */
42         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnAppDoaImpl.class);
43                 
44                 public int getProfileCount(String driver, String URL, String username, String password) {
45                         Connection dbc = null;
46                         PreparedStatement pst = null;
47                         ResultSet rs = null;
48                         String q = null;
49                         int count = 0;
50                         try {
51                                         dbc = getConnection(driver,URL,username,password);
52                                    logger.debug(EELFLoggerDelegate.debugLogger, "getConnection:::"+ dbc);
53                                 q = "select count(*) from fn_app";
54                                         pst = dbc.prepareStatement(q);
55                                         rs = pst.executeQuery();
56                                         
57                                         if (rs.next())
58                                                 count = rs.getInt(1);
59                         } catch(Exception ex) {
60                                 logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
61                         } finally {
62                                 cleanup(rs,pst,dbc);
63                         }
64                         logger.debug(EELFLoggerDelegate.debugLogger, "count:::"+ count);
65                         return count;
66                 }
67
68                 public static Connection getConnection(String driver2, String url, String username, String password) throws IOException, SQLException, ClassNotFoundException{
69                         java.sql.Connection con=null;
70                 
71                         if( url!=null && username!=null && password!=null ){
72                             con = DriverManager.getConnection(url, username, password);
73                         }
74                         
75                            System.out.println("Connection Successful");                 
76                         return con;
77                         
78                 }
79                 
80                 public static void cleanup(ResultSet rs, PreparedStatement st, Connection c) {
81                         if (rs != null) {
82                                 try {
83                                         rs.close();
84                                 } catch (Exception e) {
85                                         if (logger != null)
86                                                 logger.error("Error when trying to close result set", e);
87                                 }
88                         }
89                         if (st != null) {
90                                 try {
91                                         st.close();
92                                 } catch (Exception e) {
93                                         if (logger != null)
94                                                 logger.error("Error when trying to close statement", e);
95                                 }
96                         }
97                         if (c != null) {
98                                 try {
99                                         c.rollback();
100                                 } catch (Exception e) {
101                                         if (logger != null)
102                                                 logger.error("Error when trying to rollback connection", e);
103                                 }
104                                 try {
105                                         c.close();
106                                 } catch (Exception e) {
107                                         if (logger != null)
108                                                 logger.error("Error when trying to close connection", e);
109                                 }
110                         }
111                 }
112 }