/
BshColumnTransform.java
184 lines (173 loc) · 9.29 KB
/
BshColumnTransform.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
/**
* 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.jumpmind.symmetric.common.Constants.DATA_CONTEXT_ENGINE;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_SOURCE_NODE;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_SOURCE_NODE_EXTERNAL_ID;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_SOURCE_NODE_GROUP_ID;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_SOURCE_NODE_ID;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_TARGET_NODE;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_TARGET_NODE_EXTERNAL_ID;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_TARGET_NODE_GROUP_ID;
import static org.jumpmind.symmetric.common.Constants.DATA_CONTEXT_TARGET_NODE_ID;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.extension.IBuiltInExtensionPoint;
import org.jumpmind.symmetric.SymmetricException;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.io.data.DataContext;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.jumpmind.symmetric.model.Data;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.util.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import bsh.Interpreter;
import bsh.TargetError;
public class BshColumnTransform implements ISingleNewAndOldValueColumnTransform, IBuiltInExtensionPoint {
protected final Logger log = LoggerFactory.getLogger(getClass());
final String INTERPRETER_KEY = String.format("%d.BshInterpreter", hashCode());
public static final String NAME = "bsh";
IParameterService parameterService;
/*
* Static context object used to maintain objects in memory for reference between BSH transforms.
*/
private static Map<String, Object> bshContext = new HashMap<String, Object>();
public BshColumnTransform(IParameterService parameterService) {
this.parameterService = parameterService;
}
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 {
try {
Interpreter interpreter = getInterpreter(context);
interpreter.set("currentValue", newValue);
interpreter.set("oldValue", oldValue);
interpreter.set("channelId", context.getBatch().getChannelId());
interpreter.set("includeOn", column.getIncludeOn());
interpreter.set("sourceDmlType", data.getSourceDmlType());
interpreter.set("sourceDmlTypeString", data.getSourceDmlType().toString());
interpreter.set("transformedData", data);
interpreter.set("transformColumn", column);
Data csvData = (Data) context.get(Constants.DATA_CONTEXT_CURRENT_CSV_DATA);
if (csvData != null && csvData.getTriggerHistory() != null) {
interpreter.set("sourceSchemaName", csvData.getTriggerHistory().getSourceSchemaName());
interpreter.set("sourceCatalogName", csvData.getTriggerHistory().getSourceCatalogName());
interpreter.set("sourceTableName", csvData.getTriggerHistory().getSourceTableName());
}
for (String columnName : sourceValues.keySet()) {
interpreter.set(columnName.toUpperCase(), sourceValues.get(columnName));
}
String transformExpression = column.getTransformExpression();
if (StringUtils.isEmpty(transformExpression)) {
throw new SymmetricException("transformExpression cannot be empty. Check "
+ "configuration for transform '" + column.getTransformId() + "'");
}
String globalScript = parameterService.getString(ParameterConstants.BSH_TRANSFORM_GLOBAL_SCRIPT);
String methodName = String.format("transform_%d()",
Math.abs(transformExpression.hashCode() + (globalScript == null ? 0 : globalScript.hashCode())));
if (context.get(methodName) == null) {
interpreter.set("log", log);
interpreter.set("sqlTemplate", platform.getSqlTemplate());
interpreter.set("context", context);
interpreter.set("bshContext", bshContext);
interpreter.set(DATA_CONTEXT_ENGINE, context.get(DATA_CONTEXT_ENGINE));
interpreter.set(DATA_CONTEXT_TARGET_NODE, context.get(DATA_CONTEXT_TARGET_NODE));
interpreter.set(DATA_CONTEXT_TARGET_NODE_ID, context.get(DATA_CONTEXT_TARGET_NODE_ID));
interpreter.set(DATA_CONTEXT_TARGET_NODE_GROUP_ID, context.get(DATA_CONTEXT_TARGET_NODE_GROUP_ID));
interpreter.set(DATA_CONTEXT_TARGET_NODE_EXTERNAL_ID, context.get(DATA_CONTEXT_TARGET_NODE_EXTERNAL_ID));
interpreter.set(DATA_CONTEXT_SOURCE_NODE, context.get(DATA_CONTEXT_SOURCE_NODE));
interpreter.set(DATA_CONTEXT_SOURCE_NODE_ID, context.get(DATA_CONTEXT_SOURCE_NODE_ID));
interpreter.set(DATA_CONTEXT_SOURCE_NODE_GROUP_ID, context.get(DATA_CONTEXT_SOURCE_NODE_GROUP_ID));
interpreter.set(DATA_CONTEXT_SOURCE_NODE_EXTERNAL_ID, context.get(DATA_CONTEXT_SOURCE_NODE_EXTERNAL_ID));
if (StringUtils.isNotBlank(globalScript)) {
interpreter.eval(globalScript);
}
interpreter.eval(String.format("%s {\n%s\n}", methodName, transformExpression));
context.put(methodName, Boolean.TRUE);
}
Object result = interpreter.eval(methodName);
if (csvData != null && csvData.getTriggerHistory() != null) {
interpreter.unset("sourceSchemaName");
interpreter.unset("sourceCatalogName");
interpreter.unset("sourceTableName");
}
for (String columnName : sourceValues.keySet()) {
interpreter.unset(columnName.toUpperCase());
}
if (result instanceof String) {
if (data.getTargetDmlType().equals(DataEventType.DELETE) && data.getOldSourceValues() != null) {
return new NewAndOldValue(null, (String) result);
} else {
return new NewAndOldValue((String) result, null);
}
} else if (result instanceof NewAndOldValue) {
return (NewAndOldValue) result;
} else if (result != null) {
return new NewAndOldValue(result.toString(), null);
} else {
return null;
}
} catch (TargetError evalEx) {
Throwable ex = evalEx.getTarget();
if (ex instanceof IgnoreColumnException) {
throw (IgnoreColumnException) ex;
} else if (ex instanceof IgnoreRowException) {
throw (IgnoreRowException) ex;
} else {
throw new TransformColumnException(String.format("Beanshell script error on line %d for target column %s on transform %s", evalEx
.getErrorLineNumber(), column.getTargetColumnName(),
column.getTransformId()), ex);
}
} catch (Exception ex) {
if (ex instanceof IgnoreColumnException) {
throw (IgnoreColumnException) ex;
} else if (ex instanceof IgnoreRowException) {
throw (IgnoreRowException) ex;
} else {
log.error(String.format("Beanshell script error for target column %s on transform %s", column.getTargetColumnName(),
column.getTransformId()), ex);
throw new TransformColumnException(ex);
}
}
}
protected Interpreter getInterpreter(Context context) {
Interpreter interpreter = (Interpreter) context.get(INTERPRETER_KEY);
if (interpreter == null) {
interpreter = new Interpreter();
context.put(INTERPRETER_KEY, interpreter);
}
return interpreter;
}
}