1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.impex.data.impl;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.apache.commons.io.IOUtils;
23 import org.apache.commons.lang3.StringUtils;
24 import org.kuali.common.impex.data.MpxHeaderData;
25 import org.kuali.common.impex.data.SqlProducer;
26 import org.kuali.common.impex.model.Schema;
27 import org.kuali.common.impex.model.Table;
28 import org.kuali.common.jdbc.SqlMetaData;
29 import org.kuali.common.jdbc.supplier.AbstractSupplier;
30 import org.kuali.common.jdbc.supplier.LocationSupplier;
31 import org.kuali.common.util.CollectionUtils;
32 import org.kuali.common.util.LocationUtils;
33 import org.kuali.common.util.TextMetaData;
34
35
36
37
38
39
40 public class MpxLocationSupplier extends AbstractSupplier implements LocationSupplier {
41
42 public static final String DEFAULT_MPX_EXTENSION = ".mpx";
43 public static final String UTF8 = "UTF-8";
44
45 protected BufferedReader reader;
46 protected Table table;
47 protected MpxHeaderData headerData;
48
49 String extension = DEFAULT_MPX_EXTENSION;
50 String encoding = UTF8;
51 SqlProducer producer;
52 String location;
53
54
55
56
57 Schema schema;
58
59 @Override
60 public void open() throws IOException {
61 this.table = getTable();
62 this.reader = LocationUtils.getBufferedReader(location, encoding);
63 this.headerData = getHeader(reader);
64 }
65
66 @Override
67 public List<String> getSql() throws IOException {
68 return producer.getSql(table, headerData, reader);
69 }
70
71 @Override
72 public void close() {
73 IOUtils.closeQuietly(reader);
74 this.table = null;
75 }
76
77 protected Table getTable() {
78 String filename = LocationUtils.getFilename(location);
79 if (!StringUtils.endsWithIgnoreCase(filename, extension)) {
80 throw new IllegalArgumentException(location + " does not end with " + extension);
81 }
82 int end = filename.length() - extension.length();
83 String tableName = StringUtils.substring(filename, 0, end);
84
85 for (Table t : schema.getTables()) {
86 if(t.getName().equalsIgnoreCase(tableName)) {
87 return t;
88 }
89 }
90
91 throw new IllegalArgumentException("Unable to locate table [" + tableName + "]");
92 }
93
94 @Override
95 public void fillInMetaData() {
96 TextMetaData tmd = LocationUtils.getTextMetaData(location);
97 this.metaData = new SqlMetaData(tmd.getLines() - 1, tmd.getSize());
98 }
99
100
101
102
103
104
105
106
107
108
109
110 protected MpxHeaderData getHeader(BufferedReader reader) throws IOException {
111 String line = reader.readLine();
112 while(org.codehaus.plexus.util.StringUtils.isBlank(line)) {
113 line = reader.readLine();
114 }
115
116 if (ParseUtils.isHeaderLine(line)) {
117 MpxHeaderData header = new MpxHeaderData();
118 header.getColumnNames().addAll(CollectionUtils.getTrimmedListFromCSV(line));
119
120 return header;
121 }
122 else {
123 throw new IllegalArgumentException("Could not parse mpx header, next non-blank in given reader is not a header line. Reached line was: " + line);
124 }
125 }
126
127 public String getEncoding() {
128 return encoding;
129 }
130
131 public void setEncoding(String encoding) {
132 this.encoding = encoding;
133 }
134
135 @Override
136 public String getLocation() {
137 return location;
138 }
139
140 @Override
141 public void setLocation(String location) {
142 this.location = location;
143 }
144
145 public SqlProducer getProducer() {
146 return producer;
147 }
148
149 public void setProducer(SqlProducer producer) {
150 this.producer = producer;
151 }
152
153 public String getExtension() {
154 return extension;
155 }
156
157 public void setExtension(String extension) {
158 this.extension = extension;
159 }
160
161 public Schema getSchema() {
162 return schema;
163 }
164
165 public void setSchema(Schema schema) {
166 this.schema = schema;
167 }
168 }