-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtMongoDBXMLOutput_main.javajet
executable file
·297 lines (284 loc) · 13.8 KB
/
tMongoDBXMLOutput_main.javajet
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<%@ jet
imports="
org.talend.designer.codegen.config.CodeGeneratorArgument
org.talend.core.model.process.INode
org.talend.core.model.process.EConnectionType
org.talend.core.model.process.ElementParameterParser
org.talend.core.model.process.IConnection
org.talend.core.model.metadata.IMetadataColumn
org.talend.core.model.metadata.IMetadataTable
org.talend.core.model.process.IConnectionCategory
java.util.List
"
%>
<%
CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
INode node = (INode)codeGenArgument.getArgument();
String cid = node.getUniqueName();
String dataAction = ElementParameterParser.getValue(node,"__DATA_ACTION__");
boolean dieOnError = "true".equalsIgnoreCase(ElementParameterParser.getValue(node, "__DIE_ON_ERROR__"));
List<? extends IConnection> inConns = node.getIncomingConnections(EConnectionType.FLOW_MAIN);
boolean isLog4jEnabled = ("true").equals(ElementParameterParser.getValue(node.getProcess(), "__LOG4J_ACTIVATE__"));
String dbversion = ElementParameterParser.getValue(node, "__DB_VERSION__");
java.util.List<java.util.Map<String, String>> mappings = (java.util.List<java.util.Map<String, String>>)ElementParameterParser.getObjectValueXML(node, "__MAPPING__");
boolean setBulkWrite = "true".equalsIgnoreCase(ElementParameterParser.getValue(node, "__SET_BULK_WRITE__"));
String bulkWriteOperationSize = ElementParameterParser.getValue(node, "__BULK_WRITE_SIZE__");
String bulkWriteType = ElementParameterParser.getValue(node, "__BULK_WRITE_TYPE__");
// BulkWrite is only supportted on Mongo 2.6+ drivers
final boolean bulkWrite = setBulkWrite & (!"MONGODB_2_5_X".equalsIgnoreCase(dbversion));
IConnection inConn = null;
IMetadataTable metadata = null;
if(inConns!=null && inConns.size()> 0) {
inConn = inConns.get(0);
metadata = inConn.getMetadataTable();
}
if (metadata!=null) {
List< ? extends IConnection> conns = node.getIncomingConnections();
for (IConnection conn : conns) {
if (conn.getLineStyle().hasConnectionCategory(IConnectionCategory.DATA)) {
List<IMetadataColumn> columnList = metadata.getListColumns();
int sizeColumns = columnList.size();
// Create the BasicDBObject
%>
com.mongodb.WriteResult resultMessage_<%=cid%>=null;
updateObjectUtil_<%=cid%>.setObject(new com.mongodb.BasicDBObject());
<%
if(!"INSERT".equalsIgnoreCase(dataAction)){
%>
queryObjectUtil_<%=cid%>.setObject(new com.mongodb.BasicDBObject());
int countKey_<%=cid%>=0;
<%
}
// Set key/values of the BasicObject
for (int i = 0; i < sizeColumns; i++) {
IMetadataColumn column = columnList.get(i);
for (java.util.Map<String, String> mapping: mappings) {
if (column.getLabel().equals(mapping.get("SCHEMA_COLUMN"))) {
if ("true".equals(mapping.get("REMOVE_NULL_FIELD"))) {
%>
if (<%=conn.getName() %>.<%=column.getLabel() %> != null) {
<%
if (!"INSERT".equalsIgnoreCase(dataAction) && column.isKey()){
%>
queryObjectUtil_<%=cid%>.putkeyNode(pathMap_<%=cid%>.get("<%=column.getLabel() %>"),"<%=column.getOriginalDbColumnName() %>", <%=conn.getName() %>.<%=column.getLabel() %>);
countKey_<%=cid%>++;
<%
}
%>
updateObjectUtil_<%=cid%>.put(pathMap_<%=cid%>.get("<%=column.getLabel() %>"),"<%=column.getOriginalDbColumnName() %>", <%=conn.getName() %>.<%=column.getLabel() %>);
}
<%
} else {
if (!"INSERT".equalsIgnoreCase(dataAction) && column.isKey()){
%>
queryObjectUtil_<%=cid%>.putkeyNode(pathMap_<%=cid%>.get("<%=column.getLabel() %>"),"<%=column.getOriginalDbColumnName() %>", <%=conn.getName() %>.<%=column.getLabel() %>);
countKey_<%=cid%>++;
<%
}
%>
updateObjectUtil_<%=cid%>.put(pathMap_<%=cid%>.get("<%=column.getLabel() %>"),"<%=column.getOriginalDbColumnName() %>", <%=conn.getName() %>.<%=column.getLabel() %>);
<%
}
break;
}
}
}
// Get the BasicDBObject
%>
com.mongodb.BasicDBObject updateObj_<%=cid%> = updateObjectUtil_<%=cid%>.getObject();
<%
// INSERT operation
if ("INSERT".equalsIgnoreCase(dataAction)) {
// Bulk Writes
if(bulkWrite){
// Add inserts untill bulksize is reached then execute and re-initialze the bulk operation
%>
if(bulkWriteOperationCounter_<%=cid%> < bulkWriteOperationSize_<%=cid%>){
bulkWriteOperation_<%=cid%>.insert(updateObj_<%=cid%>);
bulkWriteOperationCounter_<%=cid%>++;
} else {
bulkWriteOperation_<%=cid%>.insert(updateObj_<%=cid%>);
bulkWriteOperation_<%=cid%>.execute();
bulkWriteOperationCounter_<%=cid%>=1;
bulkWriteOperation_<%=cid%> = coll_<%=cid%>.initialize<%=bulkWriteType%>BulkOperation();
}
<%
// Single Writes
}else{
%>
resultMessage_<%=cid%>=coll_<%=cid%>.insert(updateObj_<%=cid%>);
<%
}
// Other operations (UPDATE, UPSERT, DELETE)
}else{
%>
if(countKey_<%=cid%> <=0){
<%
if(dieOnError){
%>
throw new Exception("Must have at least one key in schema");
<%
}else{
if(isLog4jEnabled){
%>
log.error("<%=cid%> - Must have at least one key in schema");
<%
}
%>
System.err.println("Must have at least one key in schema");
<%
}
%>
}else{
com.mongodb.BasicDBObject queryObj_<%=cid%> = queryObjectUtil_<%=cid%>.getObject();
<%
// UPDATE
if("UPDATE".equalsIgnoreCase(dataAction)){
// Bulk Updates
if(bulkWrite){
%>
if(bulkWriteOperationCounter_<%=cid%> < bulkWriteOperationSize_<%=cid%>){
bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>).replaceOne(updateObj_<%=cid%>);
bulkWriteOperationCounter_<%=cid%>++;
}else{
bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>).replaceOne(updateObj_<%=cid%>);
bulkWriteOperation_<%=cid%>.execute();
bulkWriteOperationCounter_<%=cid%>=1;
bulkWriteOperation_<%=cid%> = coll_<%=cid%>.initialize<%=bulkWriteType%>BulkOperation();
}
<%
}else{
%>
resultMessage_<%=cid%>=coll_<%=cid%>.update(queryObj_<%=cid%>, updateObj_<%=cid%>);
<%
}
// SET
} else if("SET".equalsIgnoreCase(dataAction)){
// Bulk Updates
if(bulkWrite){
%>
if(bulkWriteOperationCounter_<%=cid%> < bulkWriteOperationSize_<%=cid%>){
bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>).update(new com.mongodb.BasicDBObject("$set", updateObj_<%=cid%>));
bulkWriteOperationCounter_<%=cid%>++;
}else{
bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>).update(new com.mongodb.BasicDBObject("$set", updateObj_<%=cid%>));
bulkWriteOperation_<%=cid%>.execute();
bulkWriteOperationCounter_<%=cid%>=1;
bulkWriteOperation_<%=cid%> = coll_<%=cid%>.initialize<%=bulkWriteType%>BulkOperation();
}
<%
}else{
%>
resultMessage_<%=cid%>=coll_<%=cid%>.update(queryObj_<%=cid%>,new com.mongodb.BasicDBObject("$set", updateObj_<%=cid%>));
<%
}
// UPSERT
} else if("UPSERT".equalsIgnoreCase(dataAction)){
// Bulk Upsert
if(bulkWrite){
%>
if(bulkWriteOperationCounter_<%=cid%> < bulkWriteOperationSize_<%=cid%>){
com.mongodb.BulkWriteRequestBuilder bulkWriteRequestBuilder_<%=cid%> = bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>);
com.mongodb.BulkUpdateRequestBuilder bulkUpdateRequestBuilder_<%=cid%> = bulkWriteRequestBuilder_<%=cid%>.upsert();
bulkUpdateRequestBuilder_<%=cid%>.replaceOne(updateObj_<%=cid%>);
bulkWriteOperationCounter_<%=cid%>++;
}else{
com.mongodb.BulkWriteRequestBuilder bulkWriteRequestBuilder_<%=cid%> = bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>);
com.mongodb.BulkUpdateRequestBuilder bulkUpdateRequestBuilder_<%=cid%> = bulkWriteRequestBuilder_<%=cid%>.upsert();
bulkUpdateRequestBuilder_<%=cid%>.replaceOne(updateObj_<%=cid%>);
bulkWriteOperation_<%=cid%>.execute();
bulkWriteOperationCounter_<%=cid%>=1;
bulkWriteOperation_<%=cid%> = coll_<%=cid%>.initialize<%=bulkWriteType%>BulkOperation();
}
<%
}else{
%>
resultMessage_<%=cid%>=coll_<%=cid%>.update(queryObj_<%=cid%>,updateObj_<%=cid%>,true,false);
<%
}
// UPSERT WITH SET
} else if("UPSERT_WITH_SET".equalsIgnoreCase(dataAction)){
// Bulk Upsert
if(bulkWrite){
%>
if(bulkWriteOperationCounter_<%=cid%> < bulkWriteOperationSize_<%=cid%>){
com.mongodb.BulkWriteRequestBuilder bulkWriteRequestBuilder_<%=cid%> = bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>);
com.mongodb.BulkUpdateRequestBuilder bulkUpdateRequestBuilder_<%=cid%> = bulkWriteRequestBuilder_<%=cid%>.upsert();
bulkUpdateRequestBuilder_<%=cid%>.update(new com.mongodb.BasicDBObject("$set", updateObj_<%=cid%>));
bulkWriteOperationCounter_<%=cid%>++;
}else{
com.mongodb.BulkWriteRequestBuilder bulkWriteRequestBuilder_<%=cid%> = bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>);
com.mongodb.BulkUpdateRequestBuilder bulkUpdateRequestBuilder_<%=cid%> = bulkWriteRequestBuilder_<%=cid%>.upsert();
bulkUpdateRequestBuilder_<%=cid%>.update(new com.mongodb.BasicDBObject("$set", updateObj_<%=cid%>));
bulkWriteOperation_<%=cid%>.execute();
bulkWriteOperationCounter_<%=cid%>=1;
bulkWriteOperation_<%=cid%> = coll_<%=cid%>.initialize<%=bulkWriteType%>BulkOperation();
}
<%
}else{
%>
resultMessage_<%=cid%>=coll_<%=cid%>.update(queryObj_<%=cid%>,new com.mongodb.BasicDBObject("$set", updateObj_<%=cid%>),true,false);
<%
}
// DELETE
} else if("DELETE".equalsIgnoreCase(dataAction)){
// Bulk Delete
if(bulkWrite){
%>
if(bulkWriteOperationCounter_<%=cid%> < bulkWriteOperationSize_<%=cid%>){
bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>).remove();
bulkWriteOperationCounter_<%=cid%>++;
}else{
bulkWriteOperation_<%=cid%>.find(queryObj_<%=cid%>).remove();
bulkWriteOperation_<%=cid%>.execute();
bulkWriteOperationCounter_<%=cid%>=1;
bulkWriteOperation_<%=cid%> = coll_<%=cid%>.initialize<%=bulkWriteType%>BulkOperation();
}
<%
}else{
%>
resultMessage_<%=cid%>=coll_<%=cid%>.remove(queryObj_<%=cid%>);
<%
}
}
%>
}
<%
}
// Die on Error
// Used only with MONGODB 2.5
// 2.5 doesn't support BulkWrites => No BulkWrites Die and Error
if (dbversion.equals("MONGODB_2_5_X")) {
%>
if(resultMessage_<%=cid%>!=null && resultMessage_<%=cid%>.getError()!=null){
<%
if(dieOnError){
%>
throw new Exception(resultMessage_<%=cid%>.getError());
<%
}else{
if(isLog4jEnabled){
%>
log.error("<%=cid%> - " + resultMessage_<%=cid%>.getError());
<%
}
%>
System.err.println(resultMessage_<%=cid%>.getError());
<%
}
%>
}
<%
}
%>
nb_line_<%=cid %> ++;
<%
if(isLog4jEnabled){
%>
log.debug("<%=cid%> - [<%=dataAction%>] - Writing the record " + nb_line_<%=cid %> + " into database.");
<%
}
}
}
}
%>