/
LookupTableDataRouter.java
154 lines (136 loc) · 6.43 KB
/
LookupTableDataRouter.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
/*
* 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.route;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.model.DataMetaData;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.Router;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
/**
* A data router that uses a lookup table to map data to nodes
*
*
*/
public class LookupTableDataRouter extends AbstractDataRouter implements IDataRouter {
final static ILog log = LogFactory.getLog(LookupTableDataRouter.class);
public final static String PARAM_TABLE = "LOOKUP_TABLE";
public final static String PARAM_KEY_COLUMN = "KEY_COLUMN";
public final static String PARAM_MAPPED_KEY_COLUMN = "LOOKUP_KEY_COLUMN";
public final static String PARAM_EXTERNAL_ID_COLUMN = "EXTERNAL_ID_COLUMN";
final static String EXPRESSION_KEY = String.format("%s.Expression.",
LookupTableDataRouter.class.getName());
final static String LOOKUP_TABLE_KEY = String.format("%s.Table.", LookupTableDataRouter.class
.getName());
private JdbcTemplate jdbcTemplate;
public Set<String> routeToNodes(IRouterContext routingContext,
DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad) {
Set<String> nodeIds = null;
Router router = dataMetaData.getTriggerRouter().getRouter();
Map<String, String> params = getParams(router, routingContext);
Map<String, String> dataMap = getDataMap(dataMetaData);
boolean validExpression = params.containsKey(PARAM_TABLE)
&& params.containsKey(PARAM_KEY_COLUMN) && params.containsKey(PARAM_MAPPED_KEY_COLUMN)
&& params.containsKey(PARAM_EXTERNAL_ID_COLUMN);
if (validExpression) {
Map<String, Set<String>> lookupTable = getLookupTable(params, router, routingContext);
String column = params.get(PARAM_KEY_COLUMN);
String keyData = dataMap.get(column);
Set<String> externalIds = lookupTable.get(keyData);
if (externalIds != null) {
for (Node node : nodes) {
if (externalIds.contains(node.getExternalId())) {
nodeIds = addNodeId(node.getNodeId(), nodeIds, nodes);
}
}
}
} else {
log.warn("RouterIllegalLookupTableExpression", router.getRouterExpression());
}
return nodeIds;
}
/**
* Cache parsed expressions in the context to minimize the amount of parsing
* we have to do when we have lots of throughput.
*/
@SuppressWarnings("unchecked")
protected Map<String, String> getParams(Router router, IRouterContext routingContext) {
final String KEY = EXPRESSION_KEY + router.getRouterId();
Map<String, String> params = (Map<String, String>) routingContext.getContextCache()
.get(KEY);
if (params == null) {
params = new HashMap<String, String>();
routingContext.getContextCache().put(KEY, params);
String routerExpression = router.getRouterExpression();
if (!StringUtils.isBlank(routerExpression)) {
String[] expTokens = routerExpression.split("\r\n|\r|\n");
if (expTokens != null) {
for (String t : expTokens) {
if (!StringUtils.isBlank(t)) {
String[] tokens = t.split("=");
if (tokens.length >= 2) {
params.put(tokens[0], tokens[1]);
}
}
}
}
}
}
return params;
}
@SuppressWarnings("unchecked")
protected Map<String, Set<String>> getLookupTable(Map<String, String> params, Router router,
IRouterContext routingContext) {
final String CTX_CACHE_KEY = LOOKUP_TABLE_KEY + "." + params.get("TABLENAME");
Map<String, Set<String>> lookupMap = (Map<String, Set<String>>) routingContext.getContextCache().get(
CTX_CACHE_KEY);
if (lookupMap == null) {
final Map<String, Set<String>> fillMap = new HashMap<String, Set<String>>();
jdbcTemplate.query(String.format("select %s, %s from %s", params.get(PARAM_MAPPED_KEY_COLUMN),
params.get(PARAM_EXTERNAL_ID_COLUMN), params.get(PARAM_TABLE)),
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
String key = rs.getString(1);
Set<String> set = fillMap.get(key);
if (set == null) {
set = new HashSet<String>();
fillMap.put(key, set);
}
set.add(rs.getString(2));
}
});
lookupMap = fillMap;
routingContext.getContextCache().put(CTX_CACHE_KEY, lookupMap);
}
return lookupMap;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}