01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package edu.iu.uis.eden.test;
17:
18: import java.io.BufferedReader;
19: import java.io.InputStreamReader;
20: import java.sql.Connection;
21: import java.sql.Statement;
22:
23: import org.apache.commons.lang.StringUtils;
24: import org.apache.log4j.Logger;
25:
26: import edu.iu.uis.eden.KEWServiceLocator;
27:
28: public class SQLDataLoader {
29:
30: private static final Logger LOG = Logger
31: .getLogger(SQLDataLoader.class);
32:
33: private String fileLoc;
34: private Class fileLocationDeterminer;
35:
36: public SQLDataLoader(String fileLoc, Class fileLocationDeterminer) {
37: this .fileLoc = fileLoc;
38: this .fileLocationDeterminer = fileLocationDeterminer;
39: }
40:
41: public void runSql() throws Exception {
42: String sqlStatementsContent = getContentsAsString(fileLoc);
43: String[] sqlStatements = sqlStatementsContent.split(";");
44: Connection conn = KEWServiceLocator.getEdenDataSource()
45: .getConnection();
46: Statement statement = conn.createStatement();
47: LOG.info("################################");
48: LOG.info("#");
49: LOG.info("#");
50: for (String sqlStatement : sqlStatements) {
51: if (StringUtils.isNotBlank(sqlStatement)) {
52: LOG.info("# Executing sql statement ->" + sqlStatement
53: + "<-");
54: statement.execute(sqlStatement);
55: }
56:
57: }
58: LOG.info("#");
59: LOG.info("#");
60: LOG.info("################################");
61: try {
62: statement.close();
63: } catch (Exception e) {
64: LOG.error(e);
65: }
66: try {
67: conn.close();
68: } catch (Exception e) {
69: LOG.error(e);
70: }
71:
72: }
73:
74: private String getContentsAsString(String fileLoc) throws Exception {
75: String data = "";
76: BufferedReader reader = null;
77: try {
78: reader = new BufferedReader(
79: new InputStreamReader(fileLocationDeterminer
80: .getResourceAsStream(fileLoc)));
81: String line = "";
82: while ((line = reader.readLine()) != null) {
83: data += line + " ";
84: }
85: } finally {
86: if (reader != null) {
87: try {
88: reader.close();
89: } catch (Exception e) {
90: LOG.error(e);
91: }
92: }
93:
94: }
95: return data;
96: }
97:
98: }
|