-
Notifications
You must be signed in to change notification settings - Fork 222
/
TriggerRouter.java
257 lines (205 loc) · 7.62 KB
/
TriggerRouter.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
/**
* 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 General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) 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.Date;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Defines the trigger via which a table will be synchronized.
*/
public class TriggerRouter implements Serializable {
private static final long serialVersionUID = 1L;
static final Logger logger = LoggerFactory.getLogger(TriggerRouter.class);
private boolean enabled = true;
/**
* This is the order in which the definitions will be processed.
*/
private int initialLoadOrder = 50;
private String initialLoadSelect;
private String initialLoadDeleteStmt;
private Trigger trigger;
private Router router;
private Date createTime;
private Date lastUpdateTime;
private String lastUpdateBy;
private boolean pingBackEnabled = false;
public TriggerRouter() {
this(new Trigger(), new Router());
}
public TriggerRouter(Trigger trigger, Router router) {
this.trigger = trigger;
this.router = router;
createTime = new Date();
lastUpdateTime = new Date();
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isEnabled() {
return enabled;
}
public Date getCreateTime() {
return createTime;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public String getLastUpdateBy() {
return lastUpdateBy;
}
public void setCreateTime(Date createdOn) {
this.createTime = createdOn;
}
public void setLastUpdateTime(Date lastModifiedOn) {
this.lastUpdateTime = lastModifiedOn;
}
public void setLastUpdateBy(String updatedBy) {
this.lastUpdateBy = updatedBy;
}
public int getInitialLoadOrder() {
return initialLoadOrder;
}
public void setInitialLoadOrder(int order) {
this.initialLoadOrder = order;
}
public void setRouter(Router router) {
this.router = router;
}
public Router getRouter() {
return router;
}
public void setTrigger(Trigger trigger) {
this.trigger = trigger;
}
public Trigger getTrigger() {
return trigger;
}
public void setInitialLoadSelect(String initialLoadSelect) {
this.initialLoadSelect = initialLoadSelect;
}
public String getInitialLoadSelect() {
return initialLoadSelect;
}
public String getInitialLoadDeleteStmt() {
return initialLoadDeleteStmt;
}
public void setInitialLoadDeleteStmt(String initialLoadDeleteStmt) {
this.initialLoadDeleteStmt = initialLoadDeleteStmt;
}
public boolean isRouted(DataEventType event) {
switch (event) {
case INSERT:
return router.isSyncOnInsert();
case DELETE:
return router.isSyncOnDelete();
case UPDATE:
return router.isSyncOnUpdate();
default:
return true;
}
}
public String getTargetSchema(String defaultSchema, TriggerHistory triggerHistory) {
if (router != null && !StringUtils.isBlank(router.getTargetSchemaName())) {
return router.getTargetSchemaName();
} else if (router != null && router.isUseSourceCatalogSchema() && triggerHistory != null) {
return triggerHistory.getSourceSchemaName();
} else {
return defaultSchema;
}
}
public String getTargetCatalog(String defaultCatalog, TriggerHistory triggerHistory) {
if (router != null && !StringUtils.isBlank(router.getTargetCatalogName())) {
return router.getTargetCatalogName();
} else if (router != null && router.isUseSourceCatalogSchema() && triggerHistory != null) {
return triggerHistory.getSourceCatalogName();
} else {
return defaultCatalog;
}
}
public String getTargetTable(TriggerHistory triggerHistory) {
if (router != null && !StringUtils.isBlank(router.getTargetTableName())) {
return router.getTargetTableName();
}
if (triggerHistory != null) {
return triggerHistory.getSourceTableName();
}
if (trigger != null && !StringUtils.isBlank(trigger.getSourceTableName())) {
return trigger.getSourceTableName();
} else {
return null;
}
}
public String qualifiedTargetTableName(TriggerHistory triggerHistory) {
String catalog = getTargetCatalog(null, triggerHistory);
String schema = getTargetSchema(null, triggerHistory);
String tableName = getTargetTable(triggerHistory);
if (!StringUtils.isBlank(schema)) {
tableName = schema + "." + tableName;
}
if (!StringUtils.isBlank(catalog)) {
tableName = catalog + "." + tableName;
}
return tableName;
}
public void setPingBackEnabled(boolean pingBackEnabled) {
this.pingBackEnabled = pingBackEnabled;
}
public boolean isPingBackEnabled() {
return pingBackEnabled;
}
public boolean isSame(TriggerRouter triggerRouter) {
return (this.trigger == null && triggerRouter.trigger == null)
|| (this.trigger != null && triggerRouter.trigger != null && this.trigger
.matches(triggerRouter.trigger))
&& (this.router == null && triggerRouter.router == null)
|| (this.router != null && triggerRouter.router != null && this.router
.equals(triggerRouter.router));
}
@Override
public String toString() {
return (trigger != null ? trigger.toString() : "") + ":"
+ (router != null ? router.toString() : "");
}
public String getIdentifier() {
return getTrigger().getTriggerId() + getRouter().getRouterId();
}
public String getTriggerId() {
return this.trigger != null ? this.trigger.getTriggerId() : null;
}
public String getRouterId() {
return this.router != null ? this.router.getRouterId() : null;
}
public void setTriggerId(String triggerId) {
if (this.trigger == null) {
this.trigger = new Trigger();
}
this.trigger.setTriggerId(triggerId);
}
public void setRouterId(String routerId) {
if (this.router == null) {
this.router = new Router();
}
this.router.setRouterId(routerId);
}
}