/
LookupColumnTransform.java
135 lines (114 loc) · 5.58 KB
/
LookupColumnTransform.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
/**
* 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.io.data.transform;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.db.sql.mapper.StringMapper;
import org.jumpmind.extension.IBuiltInExtensionPoint;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.io.data.DataContext;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.jumpmind.symmetric.model.Data;
import org.jumpmind.util.FormatUtils;
import org.jumpmind.util.LinkedCaseInsensitiveMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LookupColumnTransform implements ISingleNewAndOldValueColumnTransform, IBuiltInExtensionPoint {
protected final Logger log = LoggerFactory.getLogger(getClass());
public static final String NAME = "lookup";
protected static final StringMapper lookupColumnRowMapper = new StringMapper();
public String getName() {
return NAME;
}
public boolean isExtractColumnTransform() {
return true;
}
public boolean isLoadColumnTransform() {
return true;
}
public NewAndOldValue transform(IDatabasePlatform platform,
DataContext context,
TransformColumn column, TransformedData data, Map<String, String> sourceValues,
String newValue, String oldValue) throws IgnoreColumnException, IgnoreRowException {
String sql = doTokenReplacementOnSql(context, column.getTransformExpression());
String lookupValue = null;
if (StringUtils.isNotBlank(sql)) {
ISqlTransaction transaction = context.findTransaction();
List<String> values = null;
LinkedCaseInsensitiveMap<Object> namedParams = new LinkedCaseInsensitiveMap<Object>(sourceValues);
if (data.getOldSourceValues() != null && sql.contains(":OLD_")) {
for (Map.Entry<String, String> oldColumn : data.getOldSourceValues().entrySet()) {
namedParams.put("OLD_" + oldColumn.getKey().toUpperCase(), oldColumn.getValue());
}
}
if (data.getTargetValues() != null && sql.contains(":TRM_")) {
for (Map.Entry<String, String> transformedCol : data.getTargetValues().entrySet()) {
namedParams.put("TRM_" + transformedCol.getKey().toUpperCase(), transformedCol.getValue());
}
}
if (transaction != null) {
values = transaction.query(sql, lookupColumnRowMapper, namedParams);
} else {
values = platform.getSqlTemplate().query(sql, lookupColumnRowMapper, namedParams);
}
int rowCount = values.size();
if (rowCount == 1) {
lookupValue = values.get(0);
} else if (rowCount > 1) {
lookupValue = values.get(0);
log.warn(
"Expected a single row, but returned multiple rows from lookup for target column {} on transform {} ",
column.getTargetColumnName(), column.getTransformId());
} else if (values.size() == 0) {
log.info(
"Expected a single row, but returned no rows from lookup for target column {} on transform {}",
column.getTargetColumnName(), column.getTransformId());
}
} else {
log.warn(
"Expected SQL expression for lookup transform, but no expression was found for target column {} on transform {}",
column.getTargetColumnName(), column.getTransformId());
}
if (data.getTargetDmlType().equals(DataEventType.DELETE) && data.getOldSourceValues() != null) {
return new NewAndOldValue(null, lookupValue);
} else {
return new NewAndOldValue(lookupValue, null);
}
}
protected String doTokenReplacementOnSql(DataContext context, String sql) {
if (isNotBlank(sql)) {
Data csvData = (Data) context.get(Constants.DATA_CONTEXT_CURRENT_CSV_DATA);
if (csvData != null && csvData.getTriggerHistory() != null) {
sql = FormatUtils.replaceToken(sql, "sourceCatalogName", csvData
.getTriggerHistory().getSourceCatalogName(), true);
}
if (csvData != null && csvData.getTriggerHistory() != null) {
sql = FormatUtils.replaceToken(sql, "sourceSchemaName", csvData.getTriggerHistory()
.getSourceSchemaName(), true);
}
}
return sql;
}
}