1 package org.kuali.db;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.io.Reader;
9 import java.io.StringReader;
10
11 import org.springframework.core.io.DefaultResourceLoader;
12 import org.springframework.core.io.Resource;
13 import org.springframework.core.io.ResourceLoader;
14 import static org.apache.commons.lang.StringUtils.*;
15
16
17
18
19
20 public class Transaction {
21 String resourceLocation;
22 String sqlCommand;
23 String encoding;
24 String description;
25
26
27
28
29 public void addText(String sql) {
30 if (sqlCommand == null) {
31 sqlCommand = sql;
32 } else {
33 sqlCommand += sql;
34 }
35 }
36
37 public Reader getReader() throws IOException {
38
39 if (!isEmpty(sqlCommand)) {
40 return new StringReader(sqlCommand);
41 }
42
43
44 if (isEmpty(resourceLocation)) {
45 throw new IOException("Unable to locate the SQL for this transaction");
46 }
47
48
49 File file = new File(resourceLocation);
50 if (file.exists()) {
51 return getReader(new FileInputStream(file));
52 }
53
54
55 ResourceLoader loader = new DefaultResourceLoader();
56 Resource resource = loader.getResource(resourceLocation);
57 if (!resource.exists()) {
58 throw new IOException("Unable to locate the SQL for this transaction");
59 }
60 return getReader(resource.getInputStream());
61 }
62
63 protected Reader getReader(InputStream in) throws IOException {
64 if (isEmpty(getEncoding())) {
65 return new InputStreamReader(in);
66 } else {
67 return new InputStreamReader(in, getEncoding());
68 }
69 }
70
71 public String getResourceLocation() {
72 return resourceLocation;
73 }
74
75 public void setResourceLocation(String resourceLocation) {
76 this.resourceLocation = resourceLocation;
77 }
78
79 public String getSqlCommand() {
80 return sqlCommand;
81 }
82
83 public void setSqlCommand(String sqlCommand) {
84 this.sqlCommand = sqlCommand;
85 }
86
87 public String getEncoding() {
88 return encoding;
89 }
90
91 public void setEncoding(String encoding) {
92 this.encoding = encoding;
93 }
94
95 public String getDescription() {
96 return description;
97 }
98
99 public void setDescription(String description) {
100 this.description = description;
101 }
102 }