1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.jumpmind.symmetric.load;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.jumpmind.symmetric.common.ParameterConstants;
25 import org.jumpmind.symmetric.common.TableConstants;
26 import org.jumpmind.symmetric.model.IncomingBatchHistory;
27 import org.jumpmind.symmetric.service.IBootstrapService;
28 import org.jumpmind.symmetric.service.IParameterService;
29
30 /***
31 * An out of the box filter that checks to see if the SymmetricDS trigger
32 * configuration has changed. If it has, it will synchronize triggers.
33 */
34 public class SyncTriggersRequiredFilter implements IDataLoaderFilter, IBatchListener {
35
36 static final Log logger = LogFactory.getLog(SyncTriggersRequiredFilter.class);
37
38 final String CTX_KEY_RESYNC_NEEDED = SyncTriggersRequiredFilter.class.getSimpleName() + hashCode();
39
40 private IBootstrapService bootstrapService;
41
42 private IParameterService parameterService;
43
44 private String tablePrefix;
45
46 public boolean filterDelete(IDataLoaderContext context, String[] keyValues) {
47 recordSyncNeeded(context);
48 return true;
49 }
50
51 public boolean filterInsert(IDataLoaderContext context, String[] columnValues) {
52 recordSyncNeeded(context);
53 return true;
54 }
55
56 public boolean filterUpdate(IDataLoaderContext context, String[] columnValues, String[] keyValues) {
57 recordSyncNeeded(context);
58 return true;
59 }
60
61 private void recordSyncNeeded(IDataLoaderContext context) {
62 if (isSyncTriggersNeeded(context)) {
63 context.getContextCache().put(CTX_KEY_RESYNC_NEEDED, true);
64 }
65 }
66
67 private boolean isSyncTriggersNeeded(IDataLoaderContext context) {
68 return matchesTable(context, TableConstants.SYM_TRIGGER);
69 }
70
71 private boolean matchesTable(IDataLoaderContext context, String tableSuffix) {
72 return context.getTableName().equalsIgnoreCase(TableConstants.getTableName(tablePrefix, tableSuffix));
73 }
74
75 public boolean isAutoRegister() {
76 return true;
77 }
78
79 public void batchComplete(IDataLoader loader, IncomingBatchHistory history) {
80 if (loader.getContext().getContextCache().get(CTX_KEY_RESYNC_NEEDED) != null
81 && parameterService.is(ParameterConstants.AUTO_SYNC_CONFIGURATION)) {
82 logger.info("About to syncTriggers because new configuration came through the dataloader.");
83 bootstrapService.syncTriggers();
84 }
85 }
86
87 public void setBootstrapService(IBootstrapService bootstrapService) {
88 this.bootstrapService = bootstrapService;
89 }
90
91 public void setParameterService(IParameterService parameterService) {
92 this.parameterService = parameterService;
93 }
94
95 public void setTablePrefix(String tablePrefix) {
96 this.tablePrefix = tablePrefix;
97 }
98
99 }