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