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.model.context;
017
018 import java.util.List;
019
020 import javax.sql.DataSource;
021
022 import org.kuali.common.jdbc.listeners.LogSqlListener;
023 import org.kuali.common.jdbc.listeners.SqlListener;
024 import org.kuali.common.jdbc.model.enums.CommitMode;
025 import org.kuali.common.jdbc.sql.model.SqlContext;
026 import org.kuali.common.jdbc.suppliers.SqlSupplier;
027 import org.kuali.common.util.Assert;
028
029 import com.google.common.base.Optional;
030 import com.google.common.collect.ImmutableList;
031
032 public final class JdbcContext {
033
034 private final DataSource dataSource;
035 private final List<SqlSupplier> suppliers;
036
037 // If true, no SQL is executed.
038 // Everything leading up to SQL execution still takes place
039 // Connecting to the database, parsing SQL, etc.
040 private final boolean skipSqlExecution;
041
042 private final int threads;
043 // Use this to enable multi-threaded SQL execution
044 // When true, SQL from each supplier is executed sequentially, but the suppliers are not executed sequentially.
045 // The suppliers are split up evenly across the threads and executed concurrently
046 private final boolean multithreaded;
047 private final SqlListener listener;
048 private final CommitMode commitMode;
049 private final Optional<String> message;
050 private final boolean trackProgressByUpdateCount;
051
052 public boolean isSkipSqlExecution() {
053 return skipSqlExecution;
054 }
055
056 public int getThreads() {
057 return threads;
058 }
059
060 public boolean isMultithreaded() {
061 return multithreaded;
062 }
063
064 public SqlListener getListener() {
065 return listener;
066 }
067
068 public CommitMode getCommitMode() {
069 return commitMode;
070 }
071
072 public DataSource getDataSource() {
073 return dataSource;
074 }
075
076 public List<SqlSupplier> getSuppliers() {
077 return suppliers;
078 }
079
080 public Optional<String> getMessage() {
081 return message;
082 }
083
084 public boolean isTrackProgressByUpdateCount() {
085 return trackProgressByUpdateCount;
086 }
087
088 public static class Builder {
089
090 // Required
091 private final DataSource dataSource;
092 private final List<SqlSupplier> suppliers;
093
094 // Optional
095 private boolean skipSqlExecution = false;
096 private int threads = SqlContext.DEFAULT_THREADS;
097 private boolean multithreaded = false;
098 private SqlListener listener = new LogSqlListener();
099 private CommitMode commitMode = CommitMode.PER_SUPPLIER;
100 private Optional<String> message = Optional.absent();
101 private boolean trackProgressByUpdateCount = false;
102
103 public JdbcContext build() {
104 Assert.noNulls(dataSource, suppliers, listener, commitMode, message);
105 if (multithreaded) {
106 Assert.isTrue(threads > 0, "threads must be a positive integer");
107 }
108 return new JdbcContext(this);
109 }
110
111 public Builder(DataSource dataSource, SqlSupplier supplier) {
112 this(dataSource, ImmutableList.of(supplier));
113 }
114
115 public Builder(DataSource dataSource, List<SqlSupplier> suppliers) {
116 this.dataSource = dataSource;
117 this.suppliers = suppliers;
118 }
119
120 public Builder skipSqlExecution(boolean skipSqlExecution) {
121 this.skipSqlExecution = skipSqlExecution;
122 return this;
123 }
124
125 public Builder multithreaded(boolean multithreaded) {
126 this.multithreaded = multithreaded;
127 return this;
128 }
129
130 public Builder listener(SqlListener listener) {
131 this.listener = listener;
132 return this;
133 }
134
135 public Builder commitMode(CommitMode commitMode) {
136 this.commitMode = commitMode;
137 return this;
138 }
139
140 public Builder trackProgressByUpdateCount(boolean trackProgressByUpdateCount) {
141 this.trackProgressByUpdateCount = trackProgressByUpdateCount;
142 return this;
143 }
144
145 public Builder message(String message) {
146 this.message = Optional.fromNullable(message);
147 return this;
148 }
149
150 public Builder threads(int threads) {
151 this.threads = threads;
152 return this;
153 }
154
155 }
156
157 private JdbcContext(Builder builder) {
158 this.dataSource = builder.dataSource;
159 this.suppliers = builder.suppliers;
160 this.skipSqlExecution = builder.skipSqlExecution;
161 this.threads = builder.threads;
162 this.multithreaded = builder.multithreaded;
163 this.listener = builder.listener;
164 this.commitMode = builder.commitMode;
165 this.message = builder.message;
166 this.trackProgressByUpdateCount = builder.trackProgressByUpdateCount;
167 }
168
169 }