1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  package org.kuali.rice.test;
14  
15  import java.io.BufferedReader;
16  import java.io.InputStreamReader;
17  import java.sql.Connection;
18  import java.sql.SQLException;
19  import java.sql.Statement;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.springframework.core.io.DefaultResourceLoader;
24  import org.springframework.jdbc.core.ConnectionCallback;
25  import org.springframework.jdbc.core.JdbcTemplate;
26  import org.springframework.transaction.TransactionStatus;
27  import org.springframework.transaction.support.TransactionCallback;
28  import org.springframework.transaction.support.TransactionTemplate;
29  
30  public class SQLDataLoader {
31  
32      private static final Logger LOG = Logger.getLogger(SQLDataLoader.class);
33      public static final String SQL_LINE_COMMENT_PREFIX = "--";
34  
35      private String fileLoc;
36      private String seperatorChar;
37      private String statement;
38  
39      public SQLDataLoader(String statement) {
40          this.fileLoc = null;
41          this.seperatorChar = null;
42          this.statement = statement;
43      }
44  
45      public SQLDataLoader(String fileLoc, String seperatorChar) {
46          this.fileLoc = fileLoc;
47          this.seperatorChar = seperatorChar;
48          this.statement = null;
49      }
50  
51      public void runSql() throws Exception {
52          String[] sqlStatements = null;
53          if (statement == null) {
54              String sqlStatementsContent = getContentsAsString(fileLoc);
55              
56              
57              sqlStatements = sqlStatementsContent.split("(?m)" + getSeperatorChar() + "\\s*$");
58          } else {
59              sqlStatements = new String[]{statement};
60          }
61          final String[] finalSqlStatements = sqlStatements;
62          new TransactionTemplate(TestHarnessServiceLocator.getJtaTransactionManager()).execute(new TransactionCallback() {
63              public Object doInTransaction(TransactionStatus status) {
64                  return new JdbcTemplate(TestHarnessServiceLocator.getDataSource()).execute(new ConnectionCallback() {
65                      public Object doInConnection(Connection connection) throws SQLException {
66                          Statement statement = connection.createStatement();
67                          LOG.info("################################");
68                          LOG.info(fileLoc != null ? "#" + fileLoc : "#");
69                          LOG.info("#");
70                          for (String sqlStatement : finalSqlStatements) {
71                              if (StringUtils.isNotBlank(sqlStatement)) {
72                                  LOG.info("# Executing sql statement ->" + sqlStatement + "<-");
73                                  statement.execute(sqlStatement);
74                              }
75                          }
76                          LOG.info("#");
77                          LOG.info("#");
78                          LOG.info("################################");
79                          statement.close();
80                          return null;
81                      }
82                  });
83              }
84          });
85      }
86  
87      private String getContentsAsString(String fileLoc) throws Exception {
88          DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
89          String data = "";
90          BufferedReader reader = null;
91          try {
92              reader = new BufferedReader(new InputStreamReader(resourceLoader.getResource(fileLoc).getInputStream()));
93              String line = "";
94              while ((line = reader.readLine()) != null) {
95                  
96                  
97                  
98                  if (!line.trim().startsWith(SQL_LINE_COMMENT_PREFIX)) {
99                      data += line + "\r\n ";
100                 }
101             }
102         } finally {
103             if (reader != null) {
104                 try {
105                     reader.close();
106                 } catch (Exception e) {
107                     LOG.error(e);
108                 }
109             }
110 
111         }
112         return data;
113     }
114 
115     public String getSeperatorChar() {
116         if (this.seperatorChar == null) {
117             return ";";
118         }
119         return seperatorChar;
120     }
121 
122 }