View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
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 org.kuali.rice.test;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.springframework.core.io.DefaultResourceLoader;
21  import org.springframework.jdbc.core.ConnectionCallback;
22  import org.springframework.jdbc.core.JdbcTemplate;
23  import org.springframework.transaction.TransactionStatus;
24  import org.springframework.transaction.support.TransactionCallback;
25  import org.springframework.transaction.support.TransactionTemplate;
26  
27  import java.io.BufferedReader;
28  import java.io.InputStreamReader;
29  import java.sql.Connection;
30  import java.sql.SQLException;
31  import java.sql.Statement;
32  
33  public class SQLDataLoader {
34  
35      private static final Logger LOG = Logger.getLogger(SQLDataLoader.class);
36      public static final String SQL_LINE_COMMENT_PREFIX = "--";
37  
38      private String fileLoc;
39      private String seperatorChar;
40      private String statement;
41  
42      public SQLDataLoader(String statement) {
43          this.fileLoc = null;
44          this.seperatorChar = null;
45          this.statement = statement;
46      }
47  
48      public SQLDataLoader(String fileLoc, String seperatorChar) {
49          this.fileLoc = fileLoc;
50          this.seperatorChar = seperatorChar;
51          this.statement = null;
52      }
53  
54      public void runSql() throws Exception {
55          String[] sqlStatements = null;
56          if (statement == null) {
57              String sqlStatementsContent = getContentsAsString(fileLoc);
58              // separator char must be the last non-whitespace char on the line
59              // to avoid splitting in the middle of data that might contain the separator char
60              sqlStatements = sqlStatementsContent.split("(?m)" + getSeperatorChar() + "\\s*$");
61          } else {
62              sqlStatements = new String[]{statement};
63          }
64          final String[] finalSqlStatements = sqlStatements;
65          new TransactionTemplate(TestHarnessServiceLocator.getJtaTransactionManager()).execute(new TransactionCallback() {
66              public Object doInTransaction(TransactionStatus status) {
67                  return new JdbcTemplate(TestHarnessServiceLocator.getDataSource()).execute(new ConnectionCallback() {
68                      public Object doInConnection(Connection connection) throws SQLException {
69                          Statement statement = connection.createStatement();
70                          LOG.info("################################");
71                          LOG.info(fileLoc != null ? "#" + fileLoc : "#");
72                          LOG.info("#");
73                          for (String sqlStatement : finalSqlStatements) {
74                              if (StringUtils.isNotBlank(sqlStatement)) {
75                                  LOG.info("# Executing sql statement ->" + sqlStatement + "<-");
76                                  statement.execute(sqlStatement);
77                              }
78                          }
79                          LOG.info("#");
80                          LOG.info("#");
81                          LOG.info("################################");
82                          statement.close();
83                          return null;
84                      }
85                  });
86              }
87          });
88      }
89  
90      private String getContentsAsString(String fileLoc) throws Exception {
91          DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
92          String data = "";
93          BufferedReader reader = null;
94          try {
95              reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource(fileLoc).getInputStream()));
96              String line = "";
97              while ((line = reader.readLine()) != null) {
98                  // discard comments...commented single line statements
99                  // will result in errors when executed because there are no
100                 // results
101                 if (!line.trim().startsWith(SQL_LINE_COMMENT_PREFIX)) {
102                     data += line + "\r\n ";
103                 }
104             }
105         } finally {
106             if (reader != null) {
107                 try {
108                     reader.close();
109                 } catch (Exception e) {
110                     LOG.error(e);
111                 }
112             }
113 
114         }
115         return data;
116     }
117 
118     public String getSeperatorChar() {
119         if (this.seperatorChar == null) {
120             return ";";
121         }
122         return seperatorChar;
123     }
124 
125 }