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.model.context;
17  
18  import java.util.List;
19  
20  import javax.sql.DataSource;
21  
22  import org.kuali.common.jdbc.listeners.LogSqlListener;
23  import org.kuali.common.jdbc.listeners.SqlListener;
24  import org.kuali.common.jdbc.model.enums.CommitMode;
25  import org.kuali.common.jdbc.sql.model.SqlContext;
26  import org.kuali.common.jdbc.suppliers.SqlSupplier;
27  import org.kuali.common.util.Assert;
28  
29  import com.google.common.base.Optional;
30  import com.google.common.collect.ImmutableList;
31  
32  public final class JdbcContext {
33  
34  	private final DataSource dataSource;
35  	private final List<SqlSupplier> suppliers;
36  
37  	// If true, no SQL is executed.
38  	// Everything leading up to SQL execution still takes place
39  	// Connecting to the database, parsing SQL, etc.
40  	private final boolean skipSqlExecution;
41  
42  	private final int threads;
43  	// Use this to enable multi-threaded SQL execution
44  	// When true, SQL from each supplier is executed sequentially, but the suppliers are not executed sequentially.
45  	// The suppliers are split up evenly across the threads and executed concurrently
46  	private final boolean multithreaded;
47  	private final SqlListener listener;
48  	private final CommitMode commitMode;
49  	private final Optional<String> message;
50  	private final boolean trackProgressByUpdateCount;
51  
52  	public boolean isSkipSqlExecution() {
53  		return skipSqlExecution;
54  	}
55  
56  	public int getThreads() {
57  		return threads;
58  	}
59  
60  	public boolean isMultithreaded() {
61  		return multithreaded;
62  	}
63  
64  	public SqlListener getListener() {
65  		return listener;
66  	}
67  
68  	public CommitMode getCommitMode() {
69  		return commitMode;
70  	}
71  
72  	public DataSource getDataSource() {
73  		return dataSource;
74  	}
75  
76  	public List<SqlSupplier> getSuppliers() {
77  		return suppliers;
78  	}
79  
80  	public Optional<String> getMessage() {
81  		return message;
82  	}
83  
84  	public boolean isTrackProgressByUpdateCount() {
85  		return trackProgressByUpdateCount;
86  	}
87  
88  	public static class Builder {
89  
90  		// Required
91  		private final DataSource dataSource;
92  		private final List<SqlSupplier> suppliers;
93  
94  		// Optional
95  		private boolean skipSqlExecution = false;
96  		private int threads = SqlContext.DEFAULT_THREADS;
97  		private boolean multithreaded = false;
98  		private SqlListener listener = new LogSqlListener();
99  		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 }