-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcb_script.js
141 lines (128 loc) · 4.24 KB
/
cb_script.js
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
var DataSrcImpl = Java.type("org.xresloader.core.data.src.DataSrcImpl");
var dataSrcImplInstance = DataSrcImpl.getOurInstance();
var SchemeConf = Java.type("org.xresloader.core.scheme.SchemeConf");
var schemeConfInstance = SchemeConf.getInstance();
var SqlTimestamp = Java.type("java.sql.Timestamp");
var ProgramOptions = Java.type("org.xresloader.core.ProgramOptions");
var PBType = Java.type("com.google.protobuf.Descriptors.FieldDescriptor.Type");
var ArrayList = Java.type("java.util.ArrayList");
var Integer = Java.type("java.lang.Integer");
parseInt = Integer.valueOf;
var G = {};
var mapSource2ID = {
"资源转换示例.xlsx|process_by_script1": 1,
"资源转换示例.xlsx|process_by_script2": 2,
"资源转换示例-大文件.xlsx|process_by_script1": 1,
"资源转换示例-大文件.xlsx|process_by_script2": 2,
"资源转换示例-大文件.xlsx|process_by_script3": 3,
"资源转换示例-大文件.xlsx|process_by_script4": 4,
"资源转换示例-大文件.xlsx|process_by_script5": 5,
"资源转换示例-大文件.xlsx|process_by_script6": 6,
"资源转换示例-大文件.xlsx|process_by_script7": 7,
"资源转换示例-大文件.xlsx|process_by_script8": 8,
};
function initDataSource() {
var key =
dataSrcImplInstance.getCurrentFileName() +
"|" +
dataSrcImplInstance.getCurrentTableName();
if (mapSource2ID[key]) {
G.currentID = mapSource2ID[key];
} else {
G.currentID = mapSource2ID["./" + key];
}
var curFileName = gOurInstance.getCurrentFileName();
var curSheetName = gOurInstance.getCurrentTableName();
print(" > Check source : " + curFileName + ", " + curSheetName);
}
function extractRealKey(keyWithPrefix) {
var transIDRegex = /id_([A-Za-z0-9]+)/;
var arrRegRes = transIDRegex.exec(keyWithPrefix);
var actualKey = null;
if (arrRegRes != null && arrRegRes.length > 1) {
actualKey = arrRegRes[1];
}
return actualKey;
}
function toCombineID(prefix, id) {
var cid;
if (prefix == null) {
prefix = G.currentID;
}
if (prefix != null && id != null) {
if (prefix < 100000 && id < 100000) {
cid = parseInt(prefix * 100000 + (id % 100000));
}
}
return cid;
}
var generateCombineID = function (curMsg, msgDesc) {
var newKV = {};
for (var key in curMsg) {
var fd = msgDesc.findFieldByName(key);
if (fd == null || fd.getType() != PBType.MESSAGE) {
continue;
}
var fieldMessageType = fd.getMessageType();
var value = curMsg[key];
var actualKey = extractRealKey(key);
if (fd.isMapField()) {
var mapKVDesc = fd.getMessageType();
var mapValueDesc = mapKVDesc.findFieldByName("value");
if (mapValueDesc.getType() == PBType.MESSAGE) {
if (fieldMessageType.getName() == "combine_id") {
newKV[actualKey] = {};
for (var subKey in value) {
newKV[actualKey][subKey] = toCombineID(
value[subKey].prefix,
value[subKey].id
);
}
} else {
generateCombineID(curMsg[key], fieldMessageType);
}
}
} else if (fd.isRepeated()) {
if (fieldMessageType.getName() == "combine_id") {
newKV[actualKey] = new ArrayList();
for (var subKey in value) {
newKV[actualKey].add(
subKey,
toCombineID(value[subKey].prefix, value[subKey].id)
);
}
} else {
for (var subKey in value) {
generateCombineID(curMsg[key], fieldMessageType);
}
}
} else {
if (fieldMessageType.getName() == "combine_id") {
var id_CombineID = curMsg[key];
newKV[actualKey] = toCombineID(id_CombineID.prefix, id_CombineID.id);
}
}
}
for (var key in newKV) {
if (newKV[key] != null) {
curMsg[key] = newKV[key];
}
}
};
/**
*
* @param {HashMap<String, Object>} curMsg
* @param {org.xresloader.core.data.dst.DataDstWriterNode.DataDstTypeDescriptor} typeDesc
* @returns boolean
*/
function currentMessageCallback(curMsg, typeDesc) {
generateCombineID(curMsg, typeDesc.getRawDescriptor());
if (curMsg.human_readable_date != null) {
var stamp = SqlTimestamp.valueOf(curMsg.human_readable_date);
curMsg.date = {
seconds: parseInt(stamp.getTime() / 1000),
nanos: stamp.getNanos(),
};
}
return true;
}