View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.jdbc.suppliers;
17  
18  import java.util.List;
19  
20  import org.kuali.common.jdbc.service.MetaDataUtils;
21  import org.kuali.common.jdbc.sql.model.SqlMetaData;
22  import org.kuali.common.util.Assert;
23  import org.kuali.common.util.CollectionUtils;
24  import org.kuali.common.util.ListUtils;
25  
26  /**
27   * Supply SQL from strings that have one SQL statement each
28   */
29  public final class SimpleStringSupplier extends AbstractSupplier {
30  
31  	private final SqlMetaData metaData;
32  	private final List<String> strings;
33  	private boolean open = false;
34  	private boolean done = false;
35  
36  	public SimpleStringSupplier(String sql) {
37  		this(CollectionUtils.singletonList(sql));
38  	}
39  
40  	public SimpleStringSupplier(List<String> strings) {
41  		Assert.notNull(strings);
42  		this.strings = ListUtils.newImmutableArrayList(strings);
43  		this.metaData = MetaDataUtils.getSqlMetaData(this);
44  	}
45  
46  	@Override
47  	public synchronized void open() {
48  		Assert.isFalse(open, "Already open");
49  		this.open = true;
50  		this.done = false;
51  	}
52  
53  	@Override
54  	public synchronized List<String> getSql() {
55  		Assert.isTrue(open, "Not open");
56  		if (done) {
57  			return null;
58  		} else {
59  			this.done = true;
60  			return strings;
61  		}
62  	}
63  
64  	@Override
65  	public synchronized void close() {
66  		Assert.isTrue(open, "Not open");
67  		this.open = false;
68  	}
69  
70  	@Override
71  	public SqlMetaData getMetaData() {
72  		return metaData;
73  	}
74  
75  	public List<String> getStrings() {
76  		return strings;
77  	}
78  
79  }