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.Arrays;
021 import java.util.List;
022
023 import org.apache.commons.io.IOUtils;
024 import org.kuali.common.jdbc.DefaultSqlReader;
025 import org.kuali.common.jdbc.JdbcUtils;
026 import org.kuali.common.jdbc.SqlMetaData;
027 import org.kuali.common.jdbc.SqlReader;
028 import org.kuali.common.util.LocationUtils;
029 import org.springframework.util.Assert;
030
031 /**
032 * Supply SQL from strings that may have more than one SQL statement each
033 *
034 * @deprecated
035 */
036 @Deprecated
037 public class ComplexStringSupplier extends AbstractSupplier {
038
039 protected int index = 0;
040 protected BufferedReader in;
041
042 List<String> strings;
043 SqlReader reader = new DefaultSqlReader();
044
045 public ComplexStringSupplier() {
046 this((String) null);
047 }
048
049 public ComplexStringSupplier(String sql) {
050 this(Arrays.asList(sql));
051 }
052
053 public ComplexStringSupplier(List<String> strings) {
054 super();
055 this.strings = strings;
056 }
057
058 @Override
059 public void open() {
060
061 // Make sure we've got something to work with
062 Assert.notNull(strings, "strings is null");
063
064 // Reset index to zero
065 index = 0;
066
067 // Open a reader to the first string in the list
068 in = getBufferedReader(strings, index);
069 }
070
071 @Override
072 public List<String> getSql() {
073 try {
074 // Have the reader produce a SQL statement
075 List<String> sql = reader.getSql(in);
076
077 if (sql != null) {
078 // We got a SQL statement we are done
079 return sql;
080 } else {
081 // We've exhausted the current string, move to the next one
082 index++;
083 }
084
085 // We've exhausted all of the strings, we are done
086 if (index == strings.size()) {
087 return null;
088 }
089
090 // Open a reader to the new string
091 in = getBufferedReader(strings, index);
092
093 // Get a SQL statement from the new string
094 return getSql();
095 } catch (IOException e) {
096 throw new IllegalStateException(e);
097 }
098 }
099
100 /**
101 * Extract a String from the list and open a BufferedReader that can read from it
102 */
103 protected BufferedReader getBufferedReader(List<String> strings, int index) {
104 String string = strings.get(index);
105 return LocationUtils.getBufferedReaderFromString(string);
106 }
107
108 @Override
109 public void close() {
110 // Reset index to zero
111 index = 0;
112
113 // Make sure the BufferedReader is closed
114 IOUtils.closeQuietly(in);
115 }
116
117 @Override
118 public void fillInMetaData() {
119 long count = 0;
120 long size = 0;
121 for (String string : strings) {
122 SqlMetaData smd = getMetaData(string);
123 count += smd.getCount();
124 size += smd.getSize();
125 }
126 this.metaData = new SqlMetaData(count, size);
127 }
128
129 protected SqlMetaData getMetaData(String sql) {
130 BufferedReader in = null;
131 try {
132 in = LocationUtils.getBufferedReaderFromString(sql);
133 return JdbcUtils.getSqlMetaData(in, reader);
134 } catch (IOException e) {
135 throw new IllegalStateException(e);
136 } finally {
137 IOUtils.closeQuietly(in);
138 }
139 }
140
141 public List<String> getStrings() {
142 return strings;
143 }
144
145 public void setStrings(List<String> strings) {
146 this.strings = strings;
147 }
148
149 public SqlReader getReader() {
150 return reader;
151 }
152
153 public void setReader(SqlReader reader) {
154 this.reader = reader;
155 }
156
157 }