1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40 private final boolean skipSqlExecution;
41
42 private final int threads;
43
44
45
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
91 private final DataSource dataSource;
92 private final List<SqlSupplier> suppliers;
93
94
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 }