001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.jdbc.supplier;
017    
018    import java.io.BufferedReader;
019    import java.io.IOException;
020    import java.util.List;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.kuali.common.jdbc.DefaultSqlReader;
024    import org.kuali.common.jdbc.JdbcUtils;
025    import org.kuali.common.jdbc.SqlReader;
026    import org.kuali.common.util.LocationUtils;
027    import org.springframework.util.Assert;
028    
029    /**
030     * Supply SQL from a location containing pre-generated SQL statements
031     * 
032     * @deprecated
033     */
034    @Deprecated
035    public class SqlLocationSupplier extends AbstractSupplier implements LocationSupplier {
036    
037            private final static String DEFAULT_ENCODING = "UTF-8";
038    
039            protected BufferedReader in;
040    
041            String location;
042            String encoding = DEFAULT_ENCODING;
043            SqlReader reader = new DefaultSqlReader();
044    
045            public SqlLocationSupplier() {
046                    this(null);
047            }
048    
049            public SqlLocationSupplier(String location) {
050                    super();
051                    this.location = location;
052            }
053    
054            @Override
055            public void open() throws IOException {
056                    Assert.hasText(location, "location has no text");
057                    Assert.notNull(reader, "reader is null");
058                    in = getLocationReader();
059            }
060    
061            private BufferedReader getLocationReader() throws IOException {
062                    return LocationUtils.getBufferedReader(LocationSupplierUtils.getLocationFromContextLocation(location), encoding);
063            }
064    
065            @Override
066            public List<String> getSql() throws IOException {
067                    return reader.getSql(in);
068            }
069    
070            @Override
071            public void close() {
072                    IOUtils.closeQuietly(in);
073            }
074    
075            @Override
076            public void fillInMetaData() {
077                    Assert.hasText(location, "location has no text");
078                    BufferedReader in = null;
079                    try {
080                            in = getLocationReader();
081                            this.metaData = JdbcUtils.getSqlMetaData(in, reader);
082                    } catch (IOException e) {
083                            throw new IllegalStateException(e);
084                    } finally {
085                            IOUtils.closeQuietly(in);
086                    }
087            }
088    
089            @Override
090            public String getLocation() {
091                    return location;
092            }
093    
094            @Override
095            public void setLocation(String location) {
096                    this.location = location;
097            }
098    
099            public String getEncoding() {
100                    return encoding;
101            }
102    
103            public void setEncoding(String encoding) {
104                    this.encoding = encoding;
105            }
106    
107            public SqlReader getReader() {
108                    return reader;
109            }
110    
111            public void setReader(SqlReader reader) {
112                    this.reader = reader;
113            }
114    }