-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRowset2JSON.peoplecode
More file actions
309 lines (258 loc) · 11.5 KB
/
Rowset2JSON.peoplecode
File metadata and controls
309 lines (258 loc) · 11.5 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
import PSXP_XMLGEN:*;
/* RowSet data source */
class RowsetJS
/* Constructor */
method RowsetJS();
/* Generates RowSet data in JSON from the RowSet object passed in.
&rsResult - input RowSet object. Assume caller has generated the rowset.
return - Generated RowSet data in XML string.
*/
method getJSONData(&rsResult As Rowset) Returns string;
/* Gets RowSet name. */
method getRowSetName(&rsResult As Rowset) Returns string;
method strEncode(&input As string) Returns string;
private
method addJSONDataImpl(&parentNode As JsonBuilder, &rsResult As Rowset);
method init();
instance string &UsedFieldTypes;
instance JavaObject &meta_chars_;
instance JavaObject &unsafe_chars_pattern_;
instance JavaObject &int_;
instance File &logFile_;
instance time &startTime;
Constant &Tag_Rowset = "rs_";
Constant &Tag_Row = "row_";
Constant &Tag_Field = "fld_";
Constant &PSXP_MsgSet = 235;
Constant &Unknown_Rowset = "Unknown RowSet";
Constant &Exception_RowSetEmpty = "Rowset object can not be empty.";
Constant &Msg_NoColumn = "No column found.";
end-class;
method RowsetJS
&UsedFieldTypes = "";
&logFile_ = GetFile("/tmp/LogFile_" | %Datetime | ".txt", "W", "A", %FilePath_Absolute);
end-method;
method getRowSetName
/+ &rsResult as Rowset +/
/+ Returns String +/
Local string &sRowsetName;
&sRowsetName = &rsResult.Name;
If &sRowsetName = "" Then
&sRowsetName = &rsResult.DBRecordName;
If &sRowsetName = "" Then
&sRowsetName = &Unknown_Rowset;
End-If;
End-If;
Return &sRowsetName;
end-method;
method getJSONData
/+ &rsResult as Rowset +/
/+ Returns String +/
Local XmlDoc &xmlDoc;
Local XmlNode &xmlDocNode;
Local string &sRowsetName, &sRowsetXsdName;
Local number &nActiveRowCount;
Local JsonBuilder &oJsonBldr_;
If &rsResult = Null Then
throw CreateException(&PSXP_MsgSet, 3222, &Exception_RowSetEmpty);
End-If;
&startTime = %PerfTime;
rem MessageBox(0, "", 0, 0, "&sRowsetName " | %This.getRowSetName(&rsResult));
&sRowsetName = %This.getRowSetName(&rsResult);
&sRowsetXsdName = &Tag_Rowset | &sRowsetName;
rem MessageBox(0, "", 0, 0, "sRowsetXsdName " | &sRowsetXsdName);
/* Create JSON : root */
&oJsonBldr_ = CreateJsonBuilder(); /* Create Payload */
Local integer &iBldMode = &oJsonBldr_.BuildMode;
Local JsonNode &jNode = &oJsonBldr_.GetRootNode();
rem MessageBox(0, "", 0, 0, "BuildMode " | &iBldMode);
If &oJsonBldr_.StartObjectReturnsTrue("") Then
&nActiveRowCount = &rsResult.ActiveRowCount;
&oJsonBldr_.AddProperty("@numrows", String(&nActiveRowCount));
&oJsonBldr_.AddProperty("@rowsetname", &sRowsetName);
%This.addJSONDataImpl(&oJsonBldr_, &rsResult);
&oJsonBldr_.EndObject("");
End-If;
&logFile_.Close();
/* Add rowset data */
rem %This.addJSONDataImpl(&oJsonBldr_, &rsResult);
Return &oJsonBldr_.ToString();
end-method;
method addJSONDataImpl
/+ &parentNode as JsonBuilder, +/
/+ &rsResult as Rowset +/
Local Rowset &oRowset;
Local Row &oRow;
Local Record &oRec;
Local Field &oFld;
Local PSXP_XMLGEN:OutputField &oFieldTmp;
Local XmlNode &baseNode, &thisNode;
Local string &sRowsetName, &sRowXsdName;
Local string &sRecName, &sFldName, &sFldNames;
Local string &sSubRowsetName, &sSubRowsetXsdName;
Local string &sTmp;
Local number &nRowCount, &nRecCount, &nColCount, &nRowsetCount, &nRowCount2;
Local number &nActiveRow, &nActiveRowCount;
Local number &i, &j, &k;
If &rsResult = Null Then
throw CreateException(&PSXP_MsgSet, 3222, &Exception_RowSetEmpty);
End-If;
&oFieldTmp = create PSXP_XMLGEN:OutputField("temp");
&nActiveRowCount = &rsResult.ActiveRowCount;
If &nActiveRowCount > 0 Then
&sRowsetName = %This.getRowSetName(&rsResult);
&sRowXsdName = &Tag_Row | &sRowsetName;
&nRowCount = &rsResult.RowCount;
&nActiveRow = 0;
For &i = 1 To &nRowCount
/* Break if all active rows are read */
If &nActiveRow >= &nActiveRowCount Then
Break;
End-If;
&oRow = &rsResult(&i);
/* Skip deleted rows */
If &oRow.IsDeleted Then
Continue;
End-If;
&nActiveRow = &nActiveRow + 1;
If &parentNode.StartObjectReturnsTrue(&sRowXsdName) Then
&parentNode.AddProperty("@rownumber", String(&nActiveRow));
&logFile_.WriteLine("Inside Object ||\/: " | &sRowXsdName | " : for the : " | &i | " time --> " | NumberToString("%6.3", Value(%PerfTime - &startTime)));
/* Create rowset row */
rem &baseNode = &parentNode.AddElement(&sRowXsdName);
rem &baseNode.AddAttribute("rownumber", String(&nActiveRow));
&nRecCount = &oRow.RecordCount;
&sFldNames = "";
/* Create field elements */
For &j = 1 To &nRecCount
&oRec = &oRow.GetRecord(&j);
&sRecName = &oRec.Name;
&nColCount = &oRec.FieldCount;
For &k = 1 To &nColCount
&oFld = &oRec.GetField(&k);
&sFldName = &Tag_Field | &oFld.Name;
If (Find(&sFldName | ",", &sFldNames) > 0) Then
&sFldName = &Tag_Field | &sRecName | "_" | &sFldName;
End-If;
&sFldNames = &sFldNames | &sFldName | ",";
rem &thisNode = &baseNode.AddElement(&sFldName);
&sTmp = &oFieldTmp.GetFieldXSDValue(&oFld);
&parentNode.AddProperty(&sFldName, %This.strEncode(&sTmp));
rem &thisNode.NodeValue = &sTmp;
End-For;
End-For;
/* Create sub-rowset elements */
&nRowsetCount = &oRow.ChildCount;
For &j = 1 To &nRowsetCount
&oRowset = &oRow.GetRowset(&j);
&sSubRowsetName = %This.getRowSetName(&oRowset);
&sSubRowsetXsdName = &Tag_Rowset | &sSubRowsetName;
rem &thisNode = &baseNode.AddElement(&sSubRowsetXsdName);
rem &thisNode.AddAttribute("rowsetname", &sSubRowsetName);
rem &thisNode.AddAttribute("numrows", String(&nRowCount2));
rem %This.addXMLDataImpl(&parentNode, &oRowset);
&nRowCount2 = &oRowset.RowCount;
/*
If &parentNode.StartObjectReturnsTrue(&sSubRowsetXsdName) Then
&parentNode.AddProperty("numrows", String(&nRowCount2));
rem &parentNode.AddProperty("" | &sSubRowsetXsdName | "", &sSubRowsetName);
&parentNode.AddProperty("rowsetname", &sSubRowsetName);
%This.addJSONDataImpl(&parentNode, &oRowset);
&parentNode.EndObject(&sSubRowsetXsdName);
End-If;
*/
/*
If &parentNode.StartArrayReturnsTrue(&sSubRowsetXsdName) Then
&parentNode.StartObject(&sSubRowsetXsdName);
&parentNode.AddProperty("numrows", String(&nRowCount2));
rem &parentNode.AddProperty("" | &sSubRowsetXsdName | "", &sSubRowsetName);
&parentNode.AddProperty("rowsetname", &sSubRowsetName);
%This.addJSONDataImpl(&parentNode, &oRowset);
&parentNode.EndObject(&sSubRowsetXsdName);
&parentNode.EndArray(&sSubRowsetXsdName);
End-If;
*/
If &parentNode.StartObjectReturnsTrue(&sSubRowsetXsdName) Then
&parentNode.AddProperty("@numrows", String(&nRowCount2));
&parentNode.AddProperty("@rowsetname", &sSubRowsetName);
If &parentNode.StartArrayReturnsTrue(&sSubRowsetXsdName) Then
%This.addJSONDataImpl(&parentNode, &oRowset);
&parentNode.AddNullElement();
rem &parentNode.EndArray(&sSubRowsetXsdName);
&logFile_.WriteLine("Inside StartArray ||/\: " | &sSubRowsetXsdName | " : for the : " | &j | " time --> " | NumberToString("%6.3", Value(%PerfTime - &startTime)));
End-If;
&parentNode.EndArray(&sSubRowsetXsdName);
rem &parentNode.EndObject(&sSubRowsetXsdName);
End-If;
&parentNode.EndObject(&sSubRowsetXsdName);
End-For;
&parentNode.EndObject(&sRowXsdName);
Else
&logFile_.WriteLine("Returned FALSE");
End-If;
End-For;
End-If;
end-method;
method strEncode
/+ &input as String +/
/+ Returns String +/
Local JavaObject &matcher;
Local string &output = &input;
Local string &replacement;
Local string &match;
Local number &offset = 1;
REM ** Run lazy init if needed;
REM ** Protects against Stateless PeopleCode/Stateful JVM;
%This.init();
&matcher = &unsafe_chars_pattern_.matcher(CreateJavaObject("java.lang.String", &input));
While &matcher.find()
&match = &matcher.group();
If (&meta_chars_.containsKey(&match)) Then
REM ** replace meta characters first;
&replacement = &meta_chars_.get(&match).toString();
Else
REM ** not meta, so convert to a unicode escape sequence;
&replacement = "\u" | Right("0000" | &int_.toHexString(Code(&match)), 4);
End-If;
&output = Replace(&output, &matcher.start() + &offset, (&matcher.end() - &matcher.start()), &replacement);
REM ** move the starting position based on the size of the string after replacement;
&offset = &offset + Len(&replacement) - (&matcher.end() - &matcher.start());
End-While;
Return &output;
end-method;
method init
REM ** None only works on local vars, so get a pointer;
Local JavaObject &int = &int_;
REM ** if &int has no value, then initialize all JavaObject vars;
/*
* JavaObject vars will have no value in two scenarios:
*
* 1. First use, never initialized
* 2. Think time function, global variable, anything that causes state
* serialization.
*
* The first case is obvious. The second case, however, is not. PeopleSoft
* allows you to make App Classes Global and Component scoped objects, but
* not JavaObject variables. By using JavaObject variables in Component and
* Global scope, you can get into a bit of trouble. Retesting these values
* on each use ensures they are always initialized. The same will happen if
* you use a think-time function like Prompt or a Yes/No/Cancel MessageBox.
*/
If (None(&int)) Then
REM ** Lazy initialize Integer class;
&int_ = GetJavaClass("java.lang.Integer");
REM ** Lazy initialize the regular expression;
REM ** List other unsafe characters;
&unsafe_chars_pattern_ = GetJavaClass("java.util.regex.Pattern").compile("[\\""\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]");
REM ** Lazy initialize the hashtable;
&meta_chars_ = CreateJavaObject("java.util.Hashtable");
REM ** setup meta characters;
&meta_chars_.put(Char(8), "\b");
&meta_chars_.put(Char(9), "\t");
&meta_chars_.put(Char(10), "\n");
&meta_chars_.put(Char(12), "\f");
&meta_chars_.put(Char(13), "\r");
&meta_chars_.put("\", "\\");
&meta_chars_.put("""", "\""");
End-If;
end-method;