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.suppliers;
017    
018    import java.util.List;
019    
020    import org.kuali.common.jdbc.service.MetaDataUtils;
021    import org.kuali.common.jdbc.sql.model.SqlMetaData;
022    import org.kuali.common.util.Assert;
023    import org.kuali.common.util.CollectionUtils;
024    import org.kuali.common.util.ListUtils;
025    
026    /**
027     * Supply SQL from strings that have one SQL statement each
028     */
029    public final class SimpleStringSupplier extends AbstractSupplier {
030    
031            private final SqlMetaData metaData;
032            private final List<String> strings;
033            private boolean open = false;
034            private boolean done = false;
035    
036            public SimpleStringSupplier(String sql) {
037                    this(CollectionUtils.singletonList(sql));
038            }
039    
040            public SimpleStringSupplier(List<String> strings) {
041                    Assert.notNull(strings);
042                    this.strings = ListUtils.newImmutableArrayList(strings);
043                    this.metaData = MetaDataUtils.getSqlMetaData(this);
044            }
045    
046            @Override
047            public synchronized void open() {
048                    Assert.isFalse(open, "Already open");
049                    this.open = true;
050                    this.done = false;
051            }
052    
053            @Override
054            public synchronized List<String> getSql() {
055                    Assert.isTrue(open, "Not open");
056                    if (done) {
057                            return null;
058                    } else {
059                            this.done = true;
060                            return strings;
061                    }
062            }
063    
064            @Override
065            public synchronized void close() {
066                    Assert.isTrue(open, "Not open");
067                    this.open = false;
068            }
069    
070            @Override
071            public SqlMetaData getMetaData() {
072                    return metaData;
073            }
074    
075            public List<String> getStrings() {
076                    return strings;
077            }
078    
079    }