-
Notifications
You must be signed in to change notification settings - Fork 6
/
InputHandler.bbj
377 lines (297 loc) · 12.3 KB
/
InputHandler.bbj
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use java.util.HashMap
use com.basiscomponents.db.DataRow
use com.basiscomponents.db.ResultSet
class public InputHandler
rem the request's HTTP method
field protected String requestMethod!
rem the request's body as string
field protected String requestBody!
rem the request's URI
field protected String requestURI!
rem the request's Parameters
field protected HashMap requestParams!
rem the request headers
field protected HashMap requestHeaders!
rem the requested BC's attribute record used for the filtering
field protected DataRow attributesRecord!
rem the allowed filter fields
field protected DataRow allowedFilterFields!
rem the requested custom method
field protected String requestedCustomMethod!
rem the requested custom method return value name
field protected String requestedCustomMethodReturnValueName!
rem The session handle string
field public String Session!
method public BBjVector getTrailingRoutesList()
declare BBjVector extensionList!
extensionList! = new BBjVector()
uri$ = #requestURI!
if uri$ = "" then
methodret extensionList!
endif
rem removing the leading /
uri$ = uri$(2)
if pos("/"=uri$) = 0 then
methodret extensionList!
endif
uriFilterString$ = uri$(pos("/"=uri$)+1)
extensionList!.addAll(java.util.Arrays.asList(new String(uriFilterString$).split("/",-1)))
methodret extensionList!
methodend
rem /**
rem * The filter coming from the URL extension automatically mapped to the
rem * BC's primary keys
rem */
method public DataRow getTrailingRoutesFilter()
declare DataRow filter!
filter! = new DataRow()
extensionList! = #getTrailingRoutesList()
if extensionList!.isEmpty() then
methodret filter!
endif
if #attributesRecord! = null() OR #attributesRecord!.isEmpty() then
methodret filter!
endif
rem attributesRecord!.getFieldsHavingAttributeValue("EDITABLE", "2")
fieldNameList! = #attributesRecord!.getFieldNames()
for i=0 to fieldNameList!.size() -1
fieldName! = fieldNameList!.get(i)
if fieldName!.startsWith("_") then
continue
endif
if #attributesRecord!.getFieldAttribute(fieldName!, "EDITABLE") = "2" then
filter!.setFieldValue(fieldName!, extensionList!.get(i))
endif
next i
methodret filter!
methodend
method public DataRow getFilter()
declare DataRow filter!
filter! = new DataRow()
rem Adding all the fields from the parameter map
if #requestParams! = null() OR #requestParams!.isEmpty() then
methodret filter!
endif
rem TODO - Check the AttributesRecord
rem What to do if the AttributesRecord is not available ?
allowedFilterFields! = #allowedFilterFields!
if #allowedFilterFields! = null() OR #allowedFilterFields!.isEmpty() then
allowedFilterFields! = #attributesRecord!
endif
rem creating a second vector with the field names to upper case for comparison reasons
allowedFieldNames! = allowedFilterFields!.getFieldNames()
allowedFieldNamesUpperCase! = new BBjVector()
it! = allowedFieldNames!.iterator()
while it!.hasNext()
allowedFieldNamesUpperCase!.addItem(cvs(str(it!.next()),4))
wend
it! = #requestParams!.entrySet().iterator()
while it!.hasNext()
entry! = it!.next()
fieldName$ = entry!.getKey()
fieldValue$ = entry!.getValue()
if (fieldName$ = "_FULLTEXT" OR fieldName$ = "_SEARCH")
filter!.setFieldValue("%SEARCH", fieldValue$)
continue
endif
rem ignoring the special fields starting with underscore or the percentage sign
if fieldName$(1,1) = "_" OR fieldName$(1,1) = "%" then
continue
endif
rem ignoring empty values in the filter
if fieldValue$ = "" then
continue
endif
rem ignoring fields not beeing part of the allowed filter fields
if !allowedFieldNamesUpperCase!.contains(fieldName$) AND #getBCName() <> "DataSourceData" then
continue
endif
rem todo: all BCs should accept filters with fields that are not part of the attributes record
rem and still honor all other fields in the filter correctly, instead of giving no result
if (allowedFieldNamesUpperCase!.contains(fieldName$)) then
fieldName! = allowedFieldNames!.getItem(allowedFieldNamesUpperCase!.indexOf(fieldName$))
endif
rem program variable approach for filter fields
rem if the value of the current field matches the variable pattern [[test]]
rem then the actual value of the filter field will be the value of the [[test]] parameter
if len(fieldValue$) > 4 AND fieldValue$(1,2)="[[" then
newValue! = #requestParams!.get(fieldValue$, err=*next)
filter!.setFieldValue(fieldName!, str(newValue!))
continue
endif
rem Adding the field into the filter row
filter!.setFieldValue(fieldName!, fieldValue$)
wend
methodret filter!
methodend
method public String getMethod()
methodret #requestMethod!
methodend
rem /**
rem * Returns the name of the BC to invoke.
rem *
rem * @return the name of the BC to invoke.
rem */
method public String getBCName()
uri$ = #requestURI!
if uri$ = "" then
methodret ""
endif
uri$=uri$(2)
if pos("/"=uri$)>0
methodret uri$(1,pos("/"=uri$)-1)
endif
methodret uri$
methodend
method public void setAttributesRecord(DataRow attributesRecord!)
#attributesRecord! = attributesRecord!
methodend
rem /**
rem * Returns a com.basiscomponents.db.ResultSet for the given request body String
rem */
method public ResultSet getBody()
if #requestBody! = null() OR #requestBody!.isEmpty() then
methodret new ResultSet()
endif
rem TODO - Determine the ResultSet and
rem don't just assume the body is in the JSON format
if #requestBody! > "" and #requestBody!.substring(0,1) <> "[" then
#requestBody! = "[" + #requestBody! + "]"
endif
rem create an result set from the data
rs! = ResultSet.fromJson(#requestBody!, err=*next)
if rs! = null() then
rs! = new ResultSet()
endif
methodret rs!
methodend
rem /**
rem * Returns a DataRow with the RestBridge specific commands
rem *
rem * The return DataRow can have the following values
rem * <ul>
rem * <li>_SCOPE</li>
rem * <li>_FIELDSEL</li>
rem * <li>_LOOKUP</li>
rem * <li>_META</li>
rem * <li>_OFFSET</li>
rem * <li>_LIMIT</li>
rem * <li>_PAGE</li>
rem * <li>_PER_PAGE</li>
rem * <li>_ATTACHMENT</li>
rem * </ul>
rem *
rem * @return the DataRow with the RestBridge commands
rem */
method public DataRow getRestBridgeCommands()
declare DataRow row!
row! = new DataRow()
rem The Scope to set on the BC level
scope! = #requestParams!.get("_SCOPE", err=*next)
if scope! = null() then
scope! = ""
endif
row!.setFieldValue("_SCOPE", scope!)
rem the field selection to be set on the BC level
fieldSelection! = new DataRow()
fieldSel! = #requestParams!.get("_FIELDSEL",err=*next)
if fieldSel! <> null() then
it! = java.util.Arrays.asList(fieldSel!.split(",")).iterator()
while it!.hasNext()
fieldSelection!.setFieldValue(it!.next(), "")
wend
endif
row!.setFieldValue("_FIELDSEL", fieldSelection!)
rem Pagging
if #requestParams!.containsKey("_PAGE")
row!.setFieldValue("_PAGE", num(#requestParams!.get("_PAGE"), err=*next))
endif
if #requestParams!.containsKey("_PER_PAGE") then
row!.setFieldValue("_PER_PAGE", num(#requestParams!.get("_PER_PAGE"), err=*next))
endif
rem Offset
if #requestParams!.containsKey("_OFFSET") then
row!.setFieldValue("_OFFSET", num(#requestParams!.get("_OFFSET"), err=*next))
endif
if #requestParams!.containsKey("_LIMIT") then
row!.setFieldValue("_LIMIT", num(#requestParams!.get("_LIMIT"), err=*next))
endif
rem _ATTACHMENT
if #requestParams!.containsKey("_ATTACHMENT") then
row!.setFieldValue("_ATTACHMENT", #requestParams!.get("_ATTACHMENT"))
endif
rem _META and _LOOKUP from the URL Extension
uri$ = #requestURI!
if uri$ <> "" then
rem removing the leading /
uri$ = uri$(2)
rem check that the uri still has other entries
if pos("/"=uri$) <> 0 then
uriFilterString$ = uri$(pos("/"=uri$)+1)
extensions! = java.util.Arrays.asList(new String(uriFilterString$).split("/",-1))
it! = extensions!.iterator()
while it!.hasNext()
extension! = it!.next()
if cvs(extension!,4) = "_META" then
row!.setFieldValue("_META", "")
break
endif
if cvs(extension!,4) = "_LOOKUP" then
lookupField! = ""
if it!.hasNext() then
lookupField! = it!.next()
endif
row!.setFieldValue("_LOOKUP", lookupField!)
break
endif
wend
endif
endif
methodret row!
methodend
method public void setRequestBody(String body!)
#requestBody! = body!
methodend
method public void setRequestMethod(String method!)
#requestMethod! = method!
methodend
method public void setRequestURI(String uri!)
#requestURI! = uri!
methodend
method public void setRequestParams(HashMap params!)
#requestParams! = params!
methodend
method public void setRequestHeaders(HashMap headers!)
#requestHeaders! = headers!
methodend
method public void setAllowedFilterFields(DataRow allowedFilterFields!)
#allowedFilterFields! = allowedFilterFields!
methodend
method public void setRequestedCustomMethod(String customMethod!)
#requestedCustomMethod! = customMethod!
methodend
method public void setRequestedCustomMethodReturnValueName(String returnValueName!)
#requestedCustomMethodReturnValueName! = returnValueName!
methodend
method public String getCustomMethodName()
methodret #requestedCustomMethod!
methodend
method public BBjVector getCustomMethodParams()
declare BBjVector params!
params! = new BBjVector()
rs! = #getBody()
if rs! = null() OR rs!.isEmpty() then
methodret params!
endif
row! = rs!.get(0)
fieldNames! = row!.getFieldNames()
it! = fieldNames!.iterator()
while it!.hasNext()
params!.add(row!.getField(it!.next()).getObject())
wend
methodret params!
methodend
method public String getCustomMethodReturnValueName()
methodret #requestedCustomMethodReturnValueName!
methodend
classend