[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / database / LoadSchema.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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.dmaap.dbcapi.database;
22
23 import java.io.FileReader;
24 import java.io.LineNumberReader;
25 import java.sql.Connection;
26 import java.sql.Statement;
27 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
28
29 public class LoadSchema extends BaseLoggingClass {
30
31         private LoadSchema(){}
32
33         static void loadSchema() {
34                 ConnectionFactory cf = ConnectionFactory.getDefaultInstance();
35                 try (LineNumberReader lineReader = new LineNumberReader(new FileReader("/opt/app/dmaapbc/misc/schema_all.sql"));
36                         Connection c = cf.get(true);
37                         Statement stmt = c.createStatement()) {
38                         StringBuilder strBuilder = new StringBuilder();
39                         String line;
40                         while ((line = lineReader.readLine()) != null) {
41                                 if (!line.startsWith("--")) {
42                                         line = line.trim();
43                                         strBuilder.append(line);
44                                         if (line.endsWith(";")) {
45                                                 String sql = strBuilder.toString();
46                                                 strBuilder.setLength(0);
47                                                 stmt.execute(sql);
48                                                 appLogger.debug("SQL EXECUTE SUCCESS: " + sql);
49                                         }
50                                 }
51                         }
52                         strBuilder.setLength(0);
53                 } catch (Exception e) {
54                         errorLogger.error("Error when initializing table: " + e.getMessage(), e);
55                 }
56         }
57 }