001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.rice.test;
017:
018: import java.io.BufferedReader;
019: import java.io.InputStreamReader;
020: import java.sql.Connection;
021: import java.sql.Statement;
022:
023: import javax.sql.DataSource;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.log4j.Logger;
027: import org.springframework.core.io.DefaultResourceLoader;
028:
029: public class SQLDataLoader {
030:
031: private static final Logger LOG = Logger
032: .getLogger(SQLDataLoader.class);
033:
034: private String fileLoc;
035: private String seperatorChar;
036:
037: public SQLDataLoader(String fileLoc, String seperatorChar) {
038: this .fileLoc = fileLoc;
039: this .seperatorChar = seperatorChar;
040: }
041:
042: public void runSql() throws Exception {
043: String sqlStatementsContent = getContentsAsString(fileLoc);
044: String[] sqlStatements = sqlStatementsContent
045: .split(getSeperatorChar());
046: Connection conn = ((DataSource) TestHarnessServiceLocator
047: .getDataSource()).getConnection();
048: Statement statement = conn.createStatement();
049: LOG.info("################################");
050: LOG.info("#");
051: LOG.info("#");
052: for (String sqlStatement : sqlStatements) {
053: if (StringUtils.isNotBlank(sqlStatement)) {
054: LOG.info("# Executing sql statement ->" + sqlStatement
055: + "<-");
056: statement.execute(sqlStatement);
057: }
058:
059: }
060: LOG.info("#");
061: LOG.info("#");
062: LOG.info("################################");
063: try {
064: statement.close();
065: } catch (Exception e) {
066: LOG.error(e);
067: }
068: try {
069: conn.close();
070: } catch (Exception e) {
071: LOG.error(e);
072: }
073:
074: }
075:
076: private String getContentsAsString(String fileLoc) throws Exception {
077: DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
078: String data = "";
079: BufferedReader reader = null;
080: try {
081: reader = new BufferedReader(new InputStreamReader(
082: resourceLoader.getResource(fileLoc)
083: .getInputStream()));
084: String line = "";
085: while ((line = reader.readLine()) != null) {
086: data += line + " ";
087: }
088: } finally {
089: if (reader != null) {
090: try {
091: reader.close();
092: } catch (Exception e) {
093: LOG.error(e);
094: }
095: }
096:
097: }
098: return data;
099: }
100:
101: public String getSeperatorChar() {
102: if (this .seperatorChar == null) {
103: return ";";
104: }
105: return seperatorChar;
106: }
107:
108: }
|