| 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 | 0 | 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 | 0 | if (sqlCommand == null) { |
| 31 | 0 | sqlCommand = sql; |
| 32 | |
} else { |
| 33 | 0 | sqlCommand += sql; |
| 34 | |
} |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public Reader getReader() throws IOException { |
| 38 | |
|
| 39 | 0 | if (!isEmpty(sqlCommand)) { |
| 40 | 0 | return new StringReader(sqlCommand); |
| 41 | |
} |
| 42 | |
|
| 43 | |
|
| 44 | 0 | if (isEmpty(resourceLocation)) { |
| 45 | 0 | throw new IOException("Unable to locate the SQL for this transaction"); |
| 46 | |
} |
| 47 | |
|
| 48 | |
|
| 49 | 0 | File file = new File(resourceLocation); |
| 50 | 0 | if (file.exists()) { |
| 51 | 0 | return getReader(new FileInputStream(file)); |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
| 56 | 0 | Resource resource = loader.getResource(resourceLocation); |
| 57 | 0 | if (!resource.exists()) { |
| 58 | 0 | throw new IOException("Unable to locate the SQL for this transaction"); |
| 59 | |
} |
| 60 | 0 | return getReader(resource.getInputStream()); |
| 61 | |
} |
| 62 | |
|
| 63 | |
protected Reader getReader(InputStream in) throws IOException { |
| 64 | 0 | if (isEmpty(getEncoding())) { |
| 65 | 0 | return new InputStreamReader(in); |
| 66 | |
} else { |
| 67 | 0 | return new InputStreamReader(in, getEncoding()); |
| 68 | |
} |
| 69 | |
} |
| 70 | |
|
| 71 | |
public String getResourceLocation() { |
| 72 | 0 | return resourceLocation; |
| 73 | |
} |
| 74 | |
|
| 75 | |
public void setResourceLocation(String resourceLocation) { |
| 76 | 0 | this.resourceLocation = resourceLocation; |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
public String getSqlCommand() { |
| 80 | 0 | return sqlCommand; |
| 81 | |
} |
| 82 | |
|
| 83 | |
public void setSqlCommand(String sqlCommand) { |
| 84 | 0 | this.sqlCommand = sqlCommand; |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
public String getEncoding() { |
| 88 | 0 | return encoding; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public void setEncoding(String encoding) { |
| 92 | 0 | this.encoding = encoding; |
| 93 | 0 | } |
| 94 | |
|
| 95 | |
public String getDescription() { |
| 96 | 0 | return description; |
| 97 | |
} |
| 98 | |
|
| 99 | |
public void setDescription(String description) { |
| 100 | 0 | this.description = description; |
| 101 | 0 | } |
| 102 | |
} |