/
Trigger.java
594 lines (465 loc) · 17.7 KB
/
Trigger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Table;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.util.FormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Defines the trigger via which a table will be synchronized.
*/
public class Trigger implements Serializable {
private static final long serialVersionUID = 1L;
static final Logger log = LoggerFactory.getLogger(Trigger.class);
private static int maxTriggerId;
private static final String DEFAULT_CONDITION = "1=1";
private String triggerId;
private String sourceTableName;
private String sourceSchemaName;
private String sourceCatalogName;
private String channelId = Constants.CHANNEL_DEFAULT;
private boolean syncOnUpdate = true;
private boolean syncOnInsert = true;
private boolean syncOnDelete = true;
private boolean syncOnIncomingBatch = false;
private boolean useStreamLobs = false;
private boolean useCaptureLobs = false;
private boolean useCaptureOldData = true;
private boolean useHandleKeyUpdates = false;
private String nameForInsertTrigger;
private String nameForUpdateTrigger;
private String nameForDeleteTrigger;
private String syncOnUpdateCondition = DEFAULT_CONDITION;
private String syncOnInsertCondition = DEFAULT_CONDITION;
private String syncOnDeleteCondition = DEFAULT_CONDITION;
private String excludedColumnNames = null;
private String syncKeyNames = null;
/**
* This is a SQL expression that creates a unique id which the sync process
* can use to 'group' events together and commit together.
*/
private String txIdExpression = null;
private String externalSelect = null;
private Date createTime;
private Date lastUpdateTime;
private String lastUpdateBy;
public Trigger() {
triggerId = Integer.toString(maxTriggerId++);
}
public Trigger(String tableName, String channelId) {
this.triggerId = tableName;
this.sourceTableName = tableName;
this.channelId = channelId;
}
final public String qualifiedSourceTableName() {
return qualifiedSourceTablePrefix() + sourceTableName;
}
final public String qualifiedSourceTablePrefix() {
String schemaPlus = (getSourceSchemaName() != null ? getSourceSchemaName() + "." : "");
String catalogPlus = (getSourceCatalogName() != null ? getSourceCatalogName() + "." : "")
+ schemaPlus;
return catalogPlus;
}
public void nullOutBlankFields() {
if (StringUtils.isBlank(sourceCatalogName)) {
sourceCatalogName = null;
}
if (StringUtils.isBlank(sourceSchemaName)) {
sourceSchemaName = null;
}
}
public Column[] filterExcludedColumns(Column[] src) {
if (src != null) {
List<String> excludedColumnNames = getExcludedColumnNamesAsList();
List<Column> filtered = new ArrayList<Column>(src.length);
for (int i = 0; i < src.length; i++) {
Column col = src[i];
if (!excludedColumnNames.contains(col.getName().toLowerCase())) {
filtered.add(col);
}
}
return filtered.toArray(new Column[filtered.size()]);
} else {
return new Column[0];
}
}
public Column[] getSyncKeysColumnsForTable(Table table) {
List<String> syncKeys = getSyncKeyNamesAsList();
if (syncKeys.size() > 0) {
List<Column> columns = new ArrayList<Column>();
for (String syncKey : syncKeys) {
Column col = table.getColumnWithName(syncKey);
if (col != null) {
columns.add(col);
} else {
log.error("The sync key column '{}' was specified for the '{}' trigger but was not found in the table", syncKey, triggerId);
}
}
if (columns.size() > 0) {
return columns.toArray(new Column[columns.size()]);
} else {
return table.getPrimaryKeyColumns();
}
} else {
return table.getPrimaryKeyColumns();
}
}
/**
* When dealing with columns, always use this method to order the columns so
* that the primary keys are first.
*/
public Column[] orderColumnsForTable(Table table) {
if (table != null) {
Column[] pks = getSyncKeysColumnsForTable(table);
Column[] cols = table.getColumns();
List<Column> orderedColumns = new ArrayList<Column>(cols.length);
for (int i = 0; i < pks.length; i++) {
orderedColumns.add(pks[i]);
}
for (int i = 0; i < cols.length; i++) {
boolean syncKey = false;
for (int j = 0; j < pks.length; j++) {
if (cols[i].getName().equals(pks[j].getName())) {
syncKey = true;
break;
}
}
if (!syncKey) {
orderedColumns.add(cols[i]);
}
}
Column[] result = orderedColumns.toArray(new Column[orderedColumns.size()]);
return filterExcludedColumns(result);
} else {
return new Column[0];
}
}
@SuppressWarnings("unchecked")
private List<String> getExcludedColumnNamesAsList() {
if (excludedColumnNames != null && excludedColumnNames.length() > 0) {
StringTokenizer tokenizer = new StringTokenizer(excludedColumnNames, ",");
List<String> columnNames = new ArrayList<String>(tokenizer.countTokens());
while (tokenizer.hasMoreTokens()) {
columnNames.add(tokenizer.nextToken().toLowerCase().trim());
}
return columnNames;
} else {
return Collections.EMPTY_LIST;
}
}
public boolean hasChangedSinceLastTriggerBuild(Date lastTriggerBuildTime) {
return lastTriggerBuildTime == null || getLastUpdateTime() == null
|| lastTriggerBuildTime.before(getLastUpdateTime());
}
public String getTriggerId() {
return triggerId;
}
public void setTriggerId(String triggerId) {
this.triggerId = triggerId;
if (StringUtils.isNotBlank(triggerId) && StringUtils.isNumeric(triggerId)) {
int id = Integer.parseInt(triggerId);
if (id >= maxTriggerId) {
maxTriggerId = id + 1;
}
}
}
public String getSourceTableName() {
return sourceTableName;
}
public boolean isSourceTableNameWildCarded() {
return sourceTableName != null && sourceTableName.contains(FormatUtils.WILDCARD);
}
public void setSourceTableName(String sourceTableName) {
this.sourceTableName = sourceTableName;
}
public String getSourceSchemaName() {
return sourceSchemaName;
}
public void setSourceSchemaName(String sourceSchemaName) {
this.sourceSchemaName = sourceSchemaName;
}
public String getSourceCatalogName() {
return sourceCatalogName;
}
public void setSourceCatalogName(String sourceCatalogName) {
this.sourceCatalogName = sourceCatalogName;
}
public String getChannelId() {
return channelId;
}
public void setChannelId(String channelId) {
this.channelId = channelId;
}
public boolean isSyncOnUpdate() {
return syncOnUpdate;
}
public void setSyncOnUpdate(boolean syncOnUpdate) {
this.syncOnUpdate = syncOnUpdate;
}
public boolean isSyncOnInsert() {
return syncOnInsert;
}
public void setSyncOnInsert(boolean syncOnInsert) {
this.syncOnInsert = syncOnInsert;
}
public boolean isSyncOnDelete() {
return syncOnDelete;
}
public void setSyncOnDelete(boolean syncOnDelete) {
this.syncOnDelete = syncOnDelete;
}
public boolean isSyncOnIncomingBatch() {
return syncOnIncomingBatch;
}
public void setSyncOnIncomingBatch(boolean syncOnIncomingBatch) {
this.syncOnIncomingBatch = syncOnIncomingBatch;
}
public String getNameForInsertTrigger() {
return nameForInsertTrigger;
}
public void setNameForInsertTrigger(String nameForInsertTrigger) {
this.nameForInsertTrigger = nameForInsertTrigger;
}
public String getNameForUpdateTrigger() {
return nameForUpdateTrigger;
}
public void setNameForUpdateTrigger(String nameForUpdateTrigger) {
this.nameForUpdateTrigger = nameForUpdateTrigger;
}
public String getNameForDeleteTrigger() {
return nameForDeleteTrigger;
}
public void setNameForDeleteTrigger(String nameForDeleteTrigger) {
this.nameForDeleteTrigger = nameForDeleteTrigger;
}
public String getSyncOnUpdateCondition() {
return syncOnUpdateCondition;
}
public void setSyncOnUpdateCondition(String syncOnUpdateCondition) {
this.syncOnUpdateCondition = syncOnUpdateCondition;
}
public String getSyncOnInsertCondition() {
return syncOnInsertCondition;
}
public void setSyncOnInsertCondition(String syncOnInsertCondition) {
this.syncOnInsertCondition = syncOnInsertCondition;
}
public String getSyncOnDeleteCondition() {
return syncOnDeleteCondition;
}
public void setSyncOnDeleteCondition(String syncOnDeleteCondition) {
this.syncOnDeleteCondition = syncOnDeleteCondition;
}
public String getExcludedColumnNames() {
return excludedColumnNames;
}
public void setExcludedColumnNames(String excludedColumnNames) {
this.excludedColumnNames = excludedColumnNames;
}
public String getTxIdExpression() {
return txIdExpression;
}
public void setTxIdExpression(String txIdExpression) {
this.txIdExpression = txIdExpression;
}
public String getExternalSelect() {
return externalSelect;
}
public void setExternalSelect(String externalSelect) {
this.externalSelect = externalSelect;
}
public void setLastUpdateBy(String updatedBy) {
this.lastUpdateBy = updatedBy;
}
public String getLastUpdateBy() {
return lastUpdateBy;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastModifiedOn) {
this.lastUpdateTime = lastModifiedOn;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createdOn) {
this.createTime = createdOn;
}
public void setUseStreamLobs(boolean useStreamLobs) {
this.useStreamLobs = useStreamLobs;
}
public boolean isUseStreamLobs() {
return useStreamLobs;
}
public void setUseCaptureLobs(boolean useCaptureLobs) {
this.useCaptureLobs = useCaptureLobs;
}
public boolean isUseCaptureLobs() {
return useCaptureLobs;
}
public boolean isUseHandleKeyUpdates() {
return useHandleKeyUpdates;
}
public void setUseHandleKeyUpdates(boolean useHandleKeyUpdates) {
this.useHandleKeyUpdates = useHandleKeyUpdates;
}
public void setUseCaptureOldData(boolean useCaptureOldData) {
this.useCaptureOldData = useCaptureOldData;
}
public boolean isUseCaptureOldData() {
return useCaptureOldData;
}
public void setSyncKeyNames(String syncKeys) {
this.syncKeyNames = syncKeys;
}
public String getSyncKeyNames() {
return syncKeyNames;
}
@SuppressWarnings("unchecked")
private List<String> getSyncKeyNamesAsList() {
if (syncKeyNames != null && syncKeyNames.length() > 0) {
StringTokenizer tokenizer = new StringTokenizer(syncKeyNames, ",");
List<String> columnNames = new ArrayList<String>(tokenizer.countTokens());
while (tokenizer.hasMoreTokens()) {
columnNames.add(tokenizer.nextToken().toLowerCase().trim());
}
return columnNames;
} else {
return Collections.EMPTY_LIST;
}
}
public String getFullyQualifiedSourceTableName() {
return Table.getFullyQualifiedTableName(sourceCatalogName, sourceSchemaName, sourceTableName, null);
}
public long toHashedValue() {
long hashedValue = triggerId != null ? triggerId.hashCode() : 0;
if (null != sourceTableName) {
hashedValue += sourceTableName.hashCode();
}
if (null != channelId) {
hashedValue += channelId.hashCode();
}
if (null != sourceSchemaName) {
hashedValue += sourceSchemaName.hashCode();
}
if (null != sourceCatalogName) {
hashedValue += sourceCatalogName.hashCode();
}
hashedValue += syncOnUpdate ? "syncOnUpdate".hashCode() : 0;
hashedValue += syncOnInsert ? "syncOnInsert".hashCode() : 0;
hashedValue += syncOnDelete ? "syncOnDelete".hashCode() : 0;
hashedValue += syncOnIncomingBatch ? "syncOnIncomingBatch".hashCode() : 0;
hashedValue += useStreamLobs ? "useStreamLobs".hashCode() : 0;
hashedValue += useCaptureLobs ? "useCaptureLobs".hashCode() : 0;
hashedValue += useCaptureOldData ? "useCaptureOldData".hashCode() : 0;
hashedValue += useHandleKeyUpdates ? "useHandleKeyUpdates".hashCode() : 0;
if (null != nameForInsertTrigger) {
hashedValue += nameForInsertTrigger.hashCode();
}
if (null != nameForUpdateTrigger) {
hashedValue += nameForUpdateTrigger.hashCode();
}
if (null != nameForDeleteTrigger) {
hashedValue += nameForDeleteTrigger.hashCode();
}
if (null != syncOnUpdateCondition) {
hashedValue += syncOnUpdateCondition.hashCode();
}
if (null != syncOnInsertCondition) {
hashedValue += syncOnInsertCondition.hashCode();
}
if (null != syncOnDeleteCondition) {
hashedValue += syncOnDeleteCondition.hashCode();
}
if (null != excludedColumnNames) {
hashedValue += excludedColumnNames.hashCode();
}
if (null != externalSelect) {
hashedValue += externalSelect.hashCode();
}
if (null != txIdExpression) {
hashedValue += txIdExpression.hashCode();
}
if (null != syncKeyNames) {
hashedValue += syncKeyNames.hashCode();
}
return hashedValue;
}
public boolean matches(Table table, String defaultCatalog, String defaultSchema,
boolean ignoreCase) {
boolean schemaAndCatalogMatch = (StringUtils.equals(sourceCatalogName, table.getCatalog()) || (StringUtils
.isBlank(sourceCatalogName) && StringUtils.equals(defaultCatalog,
table.getCatalog())))
&& (StringUtils.equals(sourceSchemaName, table.getSchema()) || (StringUtils
.isBlank(sourceSchemaName) && StringUtils.equals(defaultSchema,
table.getSchema())));
boolean tableMatches = ignoreCase ? table.getName().equalsIgnoreCase(sourceTableName)
: table.getName().equals(sourceTableName);
if (!tableMatches && isSourceTableNameWildCarded()) {
String[] wildcardTokens = sourceTableName.split(",");
for (String wildcardToken : wildcardTokens) {
if (FormatUtils.isWildCardMatch(table.getName(), wildcardToken, ignoreCase)) {
if (!wildcardToken.startsWith(FormatUtils.NEGATE_TOKEN)) {
tableMatches = true;
} else {
tableMatches = false;
}
}
}
}
return schemaAndCatalogMatch && tableMatches;
}
public boolean matches(Trigger trigger) {
return StringUtils.equals(sourceCatalogName, trigger.sourceCatalogName)
&& StringUtils.equals(sourceSchemaName, trigger.sourceSchemaName)
&& trigger.sourceTableName.equalsIgnoreCase(sourceTableName);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Trigger && triggerId != null) {
return triggerId.equals(((Trigger) obj).triggerId);
} else {
return super.equals(obj);
}
}
@Override
public int hashCode() {
return triggerId != null ? triggerId.hashCode() : super.hashCode();
}
@Override
public String toString() {
if (triggerId != null) {
return triggerId;
} else {
return super.toString();
}
}
}