View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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   * This class provides an implementation of the SqlSupplier interface using an Mpx resource as the data source
37   * 
38   * @author andrewlubbers
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       * Data model provider
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      * Parses the next line in the given reader and returns a MpxHeaderData built from header information
102      *
103      * This method assumes the given reader is at the beginning of an mpx file
104      *
105      * @param reader the mpx file reader
106      * @return the next parsed header data
107      * @throws IllegalArgumentException when the next non-blank line from the reader is not a header line
108      *      IOException if a read error is thrown from the reader
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 }