forked from haeusler/tMongoDBInput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtMongoDBInput_begin.javajet
80 lines (76 loc) · 2.83 KB
/
tMongoDBInput_begin.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
<%@ jet
imports="
org.talend.core.model.process.INode
org.talend.core.model.process.ElementParameterParser
org.talend.core.model.metadata.IMetadataTable
org.talend.core.model.metadata.IMetadataColumn
org.talend.core.model.process.IConnection
org.talend.core.model.process.IConnectionCategory
org.talend.designer.codegen.config.CodeGeneratorArgument
org.talend.core.model.metadata.types.JavaTypesManager
org.talend.core.model.metadata.types.JavaType
java.util.List
java.util.Map
"
%>
<%
CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
INode node = (INode)codeGenArgument.getArgument();
boolean useExistingConnection = "true".equalsIgnoreCase(ElementParameterParser.getValue(node,"__USE_EXISTING_CONNECTION__"));
String cid = node.getUniqueName();
String collection = ElementParameterParser.getValue(node,"__COLLECTION__");
String query = ElementParameterParser.getValue(node,"__QUERY__");
String sort = ElementParameterParser.getValue(node,"__SORT__");
String limit = ElementParameterParser.getValue(node,"__LIMIT__");
/*
* Retrieve internal schema : build columnList
*/
List<IMetadataColumn> columnList = null;
List<IMetadataTable> metadatas = node.getMetadataList();
if(metadatas != null && metadatas.size() > 0)
{
IMetadataTable metadata = metadatas.get(0);
if(metadata != null)
{
columnList = metadata.getListColumns();
}
}
// Actually, the only mode currently available
if (useExistingConnection)
{
String connection = ElementParameterParser.getValue(node, "__CONNECTION__");
String dbconn = "db_" + connection;
%>
com.mongodb.DB db_<%=cid%> = (com.mongodb.DB) globalMap.get("<%=dbconn%>");
com.mongodb.DBCollection coll_<%=cid%> = db_<%=cid%>.getCollection(<%=collection%>);
com.mongodb.DBObject myQuery = (com.mongodb.DBObject) com.mongodb.util.JSON.parse(<%=query%>);
com.mongodb.DBObject fields = new com.mongodb.BasicDBObject();
<%
for (IMetadataColumn colStmt : columnList) {
%>
fields.put("<%=colStmt%>", 1);
<%
}
%>
com.mongodb.DBCursor cursor_<%=cid%> = coll_<%=cid%>.find(myQuery, fields);
<%
// Limit the number of results to retrieve if specified
if (limit != null && limit.length() > 0)
{
%>
cursor_<%=cid%> = cursor_<%=cid%>.limit(new Integer(<%=limit%>));
<%
}
// Sort results if sort criteria has been given
if (sort != null && sort.length() > 0)
{
%>
com.mongodb.DBObject orderBy = (com.mongodb.DBObject) com.mongodb.util.JSON.parse(<%=sort%>);
cursor_<%=cid%>.sort(orderBy);
<%
}
}
%>
int nb_line_<%=cid %> = 0;
while (cursor_<%=cid%>.hasNext())
{