-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathprogress.js
More file actions
7646 lines (6676 loc) · 319 KB
/
progress.js
File metadata and controls
7646 lines (6676 loc) · 319 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012-2019 Progress Software Corporation and/or its subsidiaries or affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*global $, progress:true*/
(function () {
// "use strict";
var PROGRESS_JSDO_PCT_MAX_EMPTY_BLOCKS = 20,
PROGRESS_JSDO_OP_STRING = ["none", "create", "read", "update", "delete", "submit"],
PROGRESS_JSDO_ROW_STATE_STRING = ["", "created", "", "modified", "deleted"];
/* define these if not defined yet - they may already be defined if
progress.session.js was included first */
if (typeof progress === 'undefined') {
progress = {};
}
if (typeof progress.data === 'undefined') {
progress.data = {};
}
progress.data._nextid = 0;
progress.data._uidprefix = "" + ( Date.now ? Date.now() : (new Date().getTime()));
/* 15 - 9 */
var UID_MAX_VALUE = 999999999999999;
progress.data._getNextId = function () {
var uid = ++progress.data._nextid;
if (uid >= UID_MAX_VALUE) {
progress.data._nextid = uid = 1;
progress.data._uidprefix = "" + ( Date.now ? Date.now() : (new Date().getTime()));
}
return progress.data._uidprefix + "-" + uid;
};
var msg = {};
msg.msgs = {};
// msg numbers 0 - 99 are related to use of the API (methods and properties we expose to developers)
// 100 - 109 relate to network errors
// 110 - 998 are for miscellaneous errors
msg.msgs.jsdoMSG000 = "JSDO, Internal Error: {1}";
msg.msgs.jsdoMSG001 = "JSDO: JSDO has multiple tables. Please use {1} at the table reference level.";
msg.msgs.jsdoMSG002 = "JSDO: Working record for '{1}' is undefined.";
msg.msgs.jsdoMSG003 = "JSDO: {1} function requires a function as a parameter.";
msg.msgs.jsdoMSG004 = "JSDO: Unable to find resource '{1}' in the catalog.";
msg.msgs.jsdoMSG005 = "JSDO: Data for table '{1}' was not specified in addRecords() call.";
msg.msgs.jsdoMSG006 = "JSDO: Data for JSDO was not specified in addRecords() call.";
msg.msgs.jsdoMSG007 = "JSDO: Test function in {1} must return a boolean.";
msg.msgs.jsdoMSG008 = "JSDO: Invalid keyFields parameter in addRecords() call.";
msg.msgs.jsdoMSG009 = "JSDO: KeyField '{1}' in addRecords() call was not found in the schema.";
msg.msgs.jsdoMSG010 = "JSDO: Field '{1}' in relationship was not found in the schema.";
msg.msgs.jsdoMSG011 = "UIHelper: JSDO has multiple tables. " +
"Please use {1} at the table reference level.";
msg.msgs.jsdoMSG012 = "UIHelper: Invalid {2} parameter in {1} call.";
msg.msgs.jsdoMSG020 = "JSDO: tableName parameter must be a string in addRecords() call.";
msg.msgs.jsdoMSG021 = "JSDO: addMode parameter must be specified in addRecords() call.";
msg.msgs.jsdoMSG022 = "JSDO: Invalid addMode specified in addRecords() call.";
msg.msgs.jsdoMSG023 = "JSDO: Duplicate found in addRecords() call using APPEND mode.";
msg.msgs.jsdoMSG024 = "{1}: Unexpected signature in call to {2} function.";
msg.msgs.jsdoMSG025 = "{1}: Invalid parameters in call to {2} function.";
msg.msgs.jsdoMSG026 = "JSDO: saveChanges requires a " +
"CREATE, UPDATE, DELETE or SUBMIT operation to be defined.";
msg.msgs.jsdoMSG030 = "JSDO: Invalid {1}, expected {2}.";
msg.msgs.jsdoMSG031 = "JSDO: Specified sort field name '{1}' was not found in the schema.";
msg.msgs.jsdoMSG032 = "JSDO: Before-image data already exists for record in addRecords() call.";
msg.msgs.jsdoMSG033 = "{1}: Invalid signature for {2}. {3}";
msg.msgs.jsdoMSG034 = "JSDO: In '{1}' function, JSON data is missing _id";
msg.msgs.jsdoMSG035 = "JSDO: In '{1}' function, before-image JSON data is missing prods:clientId";
msg.msgs.jsdoMSG036 = "JSDO: '{1}' can only be called for a dataset";
msg.msgs.jsdoMSG037 = "{1}: Event name must be provided for {2}.";
msg.msgs.jsdoMSG038 = "Too few arguments. There must be at least {1}.";
msg.msgs.jsdoMSG039 = "The name of the event is not a string.";
msg.msgs.jsdoMSG040 = "The event listener is not a function.";
msg.msgs.jsdoMSG041 = "The event listener scope is not an object.";
msg.msgs.jsdoMSG042 = "'{1}' is not a defined event for this object.";
msg.msgs.jsdoMSG043 = "{1}: A session object was requested to check the status of a Mobile " +
"Service named '{2}', but it has not loaded the definition of that service.";
msg.msgs.jsdoMSG044 = "JSDO: In '{1}' function, {2} is missing {3} property.";
msg.msgs.jsdoMSG045 = "JSDO: {1} function: {2} is missing {3} property.";
msg.msgs.jsdoMSG046 = "JSDO: {1} operation is not defined.";
msg.msgs.jsdoMSG047 = "{1} timeout expired.";
msg.msgs.jsdoMSG048 = "{1}: {2} method has argument '{3}' that is missing property '{4}'.";
msg.msgs.jsdoMSG049 = "{1}: Unexpected error calling {2}: {3}";
msg.msgs.jsdoMSG050 = "No token returned from server";
msg.msgs.jsdoMSG051 = "{1} The login method was not executed because the AuthenticationProvider is already logged in.";
msg.msgs.jsdoMSG052 = "{1}: The login method was not executed because no credentials were supplied.";
msg.msgs.jsdoMSG053 = "{1}: {2} was not executed because the AuthenticationProvider is not logged in.";
msg.msgs.jsdoMSG054 = "{1}: Token refresh was not executed because the AuthenticationProvider does not have a refresh token.";
msg.msgs.jsdoMSG055 = "{1}: Token refresh was not executed because the authentication model is not sso.";
msg.msgs.jsdoMSG056 = "{1}: Already logged in.";
msg.msgs.jsdoMSG057 = "{1}: Cannot call {2} when authenticationModel is SSO. Please use the AuthenticationProvider object instead.";
msg.msgs.jsdoMSG058 = "{1}: Cannot pass username and password to addCatalog when authenticationModel " +
"is sso. Pass an AuthenticationProvider instead.";
msg.msgs.jsdoMSG059 = "{1}: Error in constructor. The authenticationModels of the " +
"AuthenticationProvider ({2}) and the JSDOSession ({3}) were not compatible.";
msg.msgs.jsdoMSG060 = "AuthenticationProvider: AuthenticationProvider is no longer logged in. " +
"Tried to refresh SSO token but failed due to authentication error at token server.";
msg.msgs.jsdoMSG061 = "{1}: Attempted to set {2} property to an invalid value.";
msg.msgs.jsdoMSG062 = "{1}: Cannot call {2} when an AuthenticationProvider is already available and logged in.";
// 100 - 109 relate to network errors
msg.msgs.jsdoMSG100 = "JSDO: Unexpected HTTP response. Too many records.";
msg.msgs.jsdoMSG101 = "Network error while executing HTTP request.";
// 110 - 499 are for miscellaneous errors
msg.msgs.jsdoMSG110 = "Catalog error: idProperty not specified for resource '{1}'. " +
"idProperty is required {2}.";
msg.msgs.jsdoMSG111 = "Catalog error: Schema '{1}' was not found in catalog.";
msg.msgs.jsdoMSG112 = "Catalog error: Output parameter '{1}' was not found for operation '{2}'.";
msg.msgs.jsdoMSG113 = "Catalog error: Found xType '{1}' for output parameter '{2}' " +
"for operation '{3}' but xType DATASET, TABLE or ARRAY was expected.";
msg.msgs.jsdoMSG114 = "JSDO: idProperty '{1}' is missing from '{2}' record.";
msg.msgs.jsdoMSG115 = "JSDO: Invalid option specified in {1}() call.";
msg.msgs.jsdoMSG116 = "JSDO: {1} parameter must be a string in {2} call.";
msg.msgs.jsdoMSG117 = "JSDO: Schema from storage area '{1}' does not match JSDO schema";
msg.msgs.jsdoMSG118 = "JSDO: Plugin '{1}' was not found.";
msg.msgs.jsdoMSG119 = "JSDO: A mappingType is expected when 'capabilities' is set." +
" Please specify a plugin (ex: JFP).";
msg.msgs.jsdoMSG120 = "JSDO: Parameter '{2}' requires capability '{1}' in the catalog.";
msg.msgs.jsdoMSG121 = "{1}: Argument {2} must be of type {3} in {4} call.";
msg.msgs.jsdoMSG122 = "{1}: Incorrect number of arguments in {2} call. There should be {3}.";
msg.msgs.jsdoMSG123 = "{1}: A server response included an invalid '{2}' header.";
msg.msgs.jsdoMSG124 = "JSDO: autoApplyChanges is not supported for saveChanges(true) " +
"with a temp-table. Use jsdo.autoApplyChanges = false.";
msg.msgs.jsdoMSG125 = "{1}: The AuthenticationProvider is not managing valid credentials.";
msg.msgs.jsdoMSG126 = "{1}: No support for {2}.";
msg.msgs.jsdoMSG127 = "JSDO: acceptRowChanges() cannot be called for record with _rejected === true.";
// 500 - 998 are for generic errors
msg.msgs.jsdoMSG500 = "{1}: '{2}' objects must contain a '{3}' property.";
msg.msgs.jsdoMSG501 = "{1}: '{2}' in '{3}' function cannot be an empty string.";
msg.msgs.jsdoMSG502 = "{1}: The '{2}' parameter passed to the '{3}' function has an invalid value for " +
"its '{4}' property.";
msg.msgs.jsdoMSG503 = "{1}: '{2}' must be of type '{3}'.";
msg.msgs.jsdoMSG504 = "{1}: {2} has an invalid value for the '{3}' property.";
msg.msgs.jsdoMSG505 = "{1}: '{2}' objects must have a '{3}' method.";
// use message below if invalid parameter value is an object
msg.msgs.jsdoMSG506 = "{1}: Invalid argument for the {2} parameter in {3} call.";
// use message below if invalid parameter value is a primitive
msg.msgs.jsdoMSG507 = "{1}: '{2}' is an invalid value for the {3} parameter in {4} call.";
msg.msgs.jsdoMSG508 = "JSDOSession: If a JSDOSession object is using the SSO authentication model, " +
"the options object passed to its constructor must include an authProvider property.";
msg.msgs.jsdoMSG509 = "progress.data.getSession: If the authenticationModel is AUTH_TYPE_SSO, " +
"authenticationURI and authProviderAuthenticationModel are required parameters.";
msg.msgs.jsdoMSG510 = "{1}: This session has been invalidated and cannot be used.";
msg.msgs.jsdoMSG511 = "JSDOSession: addCatalog() can only be called if an AuthenticationProvider was passed as an argument or " +
"connect() has been successfully called.";
msg.msgs.jsdoMSG512 = "JSDOSession: Error while loading multiple catalogs.";
msg.msgs.jsdoMSG998 = "JSDO: JSON object in addRecords() must be DataSet or Temp-Table data.";
msg.getMsgText = function (n, args) {
var text = msg.msgs[n],
i;
if (!text) {
throw new Error("Message text was not found by getMsgText()");
}
for (i = 1; i < arguments.length; i += 1) {
text = text.replace(new RegExp('\\{' + i + '\\}', 'g'), arguments[i]);
}
return text;
};
progress.data._getMsgText = msg.getMsgText;
progress.data.PluginManager = {};
progress.data.PluginManager._plugins = {};
progress.data.PluginManager.addPlugin = function(name, plugin) {
if (progress.data.PluginManager._plugins[name] === undefined) {
progress.data.PluginManager._plugins[name] = plugin;
}
else {
throw new Error("A plugin named '" + name + "' is already registered.");
}
};
progress.data.PluginManager.getPlugin = function (name) {
return progress.data.PluginManager._plugins[name];
};
progress.data.JSIndexEntry = function JSIndexEntry(index) {
this.index = index;
};
progress.data.JSTableRef = function JSTableRef(jsdo, tableName) {
this._jsdo = jsdo;
this._name = tableName;
this._schema = null;
this._primaryKeys = null;
this._fields = null;
this._processed = {};
this._visited = false;
// record is used to represent the current record for a table reference
this.record = null;
// Data structure
this._data = [];
this._index = {};
this._tmpIndex = {};
this._hasEmptyBlocks = false;
// Arrays to keep track of changes
this._beforeImage = {};
this._added = [];
this._changed = {};
this._deleted = [];
this._lastErrors = [];
this._convertForServer;
this._createIndex = function () {
var i, block, id, idProperty;
this._index = {};
this._tmpIndex = {};
this._hasEmptyBlocks = false;
for (i = 0; i < this._data.length; i += 1) {
block = this._data[i];
if (!block) {
this._hasEmptyBlocks = true;
continue;
}
id = this._data[i]._id;
if (!id) {
idProperty = this._jsdo._resource.idProperty;
if (typeof(idProperty) == "string") {
id = this._data[i][idProperty];
if (id === undefined) {
throw new Error(msg.getMsgText("jsdoMSG114", idProperty, this._name));
}
id += "";
}
else {
id = progress.data._getNextId();
}
id += ""; // ID Property
this._data[i]._id = id;
}
this._index[id] = new progress.data.JSIndexEntry(i);
}
this._needCompaction = false;
};
this._compact = function () {
var newDataArray = [], i, block;
for (i = 0; i < this._data.length; i += 1) {
block = this._data[i];
if (block) {
newDataArray.push(block);
}
}
this._data = newDataArray;
this._createIndex();
};
this._loadBeforeImageData = function (jsonObject, beforeImageJsonIndex, keyFields) {
var prodsBeforeData = jsonObject[this._jsdo._dataSetName]["prods:before"],
tmpIndex = {},
record,
record2,
recordId,
key,
tmpKeyIndex,
id,
jsrecord,
tmpDataIndex,
tmpDeletedIndex,
i,
j;
if (prodsBeforeData && prodsBeforeData[this._name]) {
if ((Object.keys(this._beforeImage).length !== 0) && keyFields && (keyFields.length !== 0)) {
tmpKeyIndex = {};
for (id in this._beforeImage) {
jsrecord = this._findById(id, false);
if (jsrecord) {
key = this._getKey(jsrecord.data, keyFields);
tmpKeyIndex[key] = jsrecord.data;
}
}
}
for (i = 0; i < prodsBeforeData[this._name].length; i++) {
record = prodsBeforeData[this._name][i];
tmpIndex[record["prods:id"]] = record;
if (record["prods:rowState"] == "deleted") {
key = undefined;
if (keyFields && (keyFields.length !== 0)) {
key = this._getKey(record, keyFields);
}
if (tmpKeyIndex) {
if (tmpKeyIndex[key] !== undefined) {
throw new Error(msg.getMsgText("jsdoMSG032"));
}
}
if ((tmpDataIndex === undefined) && keyFields && (keyFields.length !== 0)) {
tmpDataIndex = {};
tmpDeletedIndex = {};
for (var j = 0; j < this._data.length; j++) {
record2 = this._data[j];
if (!record2) continue;
var key2 = this._getKey(record2, keyFields);
tmpDataIndex[key2] = record2;
}
// We also want to check if _deleted record already exists
for (j = 0; j < this._deleted.length; j++) {
record2 = this._deleted[j].data;
if (!record2) continue;
var key2 = this._getKey(record2, keyFields);
tmpDeletedIndex[key2] = record2;
}
}
// First check to see if this deleted record is already in _deleted array
if (key !== undefined) {
record2 = tmpDeletedIndex[key];
if (record2 !== undefined) {
// If record is already in _deleted array, then nothing more to do here
continue;
}
}
if (key !== undefined) {
record2 = tmpDataIndex[key];
if (record2 !== undefined) {
var jsrecord = this._findById(record2._id, false);
if (jsrecord) jsrecord._remove(false);
record._id = record2._id;
}
}
if (record._id === undefined)
record._id = progress.data._getNextId();
var copy = {};
this._jsdo._copyRecord(
this._tableRef, record, copy);
this._jsdo._deleteProdsProperties(copy);
this._beforeImage[record._id] = copy;
var jsrecord = new progress.data.JSRecord(this, copy);
this._deleted.push(jsrecord);
}
}
}
// Process data using jsonObject instead of _data
// First check if there is after-data for table. Can be called with just before-image data
var tableObject = jsonObject[this._jsdo._dataSetName][this._name];
if (tableObject) {
for (i = 0; i < jsonObject[this._jsdo._dataSetName][this._name].length; i++) {
record = jsonObject[this._jsdo._dataSetName][this._name][i];
recordId = undefined;
if (beforeImageJsonIndex && record["prods:id"]) {
recordId = beforeImageJsonIndex[record["prods:id"]];
}
switch (record["prods:rowState"]) {
case "created":
if (recordId === undefined) {
recordId = record._id;
}
// If recordId and record._id are undefined, the record was not processed
if (recordId !== undefined) {
this._beforeImage[recordId] = null;
this._added.push(recordId);
}
break;
case "modified":
var beforeRecord = tmpIndex[record["prods:id"]];
if (beforeRecord === undefined) {
beforeRecord = {};
}
if (recordId === undefined) {
recordId = record._id;
}
// If recordId and record._id are undefined, the record was not processed
if (recordId !== undefined) {
beforeRecord._id = record._id;
var copy = {};
this._jsdo._copyRecord(
this._tableRef, beforeRecord, copy);
this._jsdo._deleteProdsProperties(copy);
this._beforeImage[recordId] = copy;
this._changed[recordId] = record;
this._beforeImage[beforeRecord._id] = copy;
this._changed[beforeRecord._id] = record;
}
break;
case undefined:
break; // rowState is only specified for records that have changed
default:
throw new Error(msg.getMsgText("jsdoMSG030",
"rowState value in before-image data", "'created' or 'modified'"));
}
}
}
// Process prods:errors
var prodsErrors = jsonObject[this._jsdo._dataSetName]["prods:errors"];
if (prodsErrors) {
for (i = 0; i < prodsErrors[this._name].length; i++) {
var item = prodsErrors[this._name][i];
var recordId = beforeImageJsonIndex[item["prods:id"]];
var jsrecord = this._findById(recordId, false);
if (jsrecord) {
jsrecord.data._errorString = item["prods:error"];
}
}
}
tmpIndex = null;
};
/*
* Clears all data (including any pending changes) in buffer
*/
this._clearData = function () {
this._setRecord(null);
// Data structure
this._data = [];
this._index = {};
this._tmpIndex = {};
this._createIndex();
// Arrays to keep track of changes
this._beforeImage = {};
this._added = [];
this._changed = {};
this._deleted = [];
};
this.hasData = function () {
var data;
// Check if we should return this table with its nested child table's data as nested
if (this._jsdo._nestChildren) {
data = this._getDataWithNestedChildren(this._data);
}
else {
data = this._getRelatedData();
}
if (this._hasEmptyBlocks) {
for (var i = 0; i < data.length; i++) {
var block = data[i];
if (!block) {
return true;
}
}
}
return data.length !== 0;
};
// Public method that returns data. Before returning data,
// a compaction is performed (removal of null record entries)
this.getData = function (params) {
if (this._needCompaction || this._hasEmptyBlocks) {
this._compact();
}
return this._getData(params);
}
// Private method that returns data. It optimizes compaction (removal of null record entries)
this._getData = function (params) {
var i,
data,
numEmptyBlocks,
newDataArray,
block,
field;
if (this._needCompaction) {
this._compact();
}
if (params && params.filter) {
throw new Error("Not implemented in current version");
}
// Check if we should return this table with its nested child table's data as nested
else if (this._jsdo._nestChildren) {
data = this._getDataWithNestedChildren(this._data);
}
else {
data = this._getRelatedData();
}
if (this._hasEmptyBlocks) {
numEmptyBlocks = 0;
newDataArray = [];
for (i = 0; i < data.length; i += 1) {
block = data[i];
if (block) {
newDataArray.push(block);
}
else {
numEmptyBlocks++;
}
}
if ((numEmptyBlocks * 100 / this._data.length) >= PROGRESS_JSDO_PCT_MAX_EMPTY_BLOCKS)
this._needCompaction = true;
data = newDataArray;
}
else {
// Creates a copy of the data if sort and top are specified
// so that the sorting does not happen in the JSDO memory but
// in a copy of the records
if (params && (params.sort || params.top)) {
newDataArray = [];
for (i = 0; i < data.length; i += 1) {
newDataArray.push(data[i]);
}
data = newDataArray;
}
}
if (params && (params.sort || params.top)) {
if (params.sort) {
// Converts sort option from Kendo UI to sort option used by the JSDO
var sortFields = [];
for (i = 0; i < params.sort.length; i += 1) {
field = params.sort[i].field;
if (params.sort[i].dir == "desc") {
field += ":DESC";
}
sortFields.push(field);
}
// Obtain sortObject from sort options to get compare functions
var sortObject = this._processSortFields(sortFields);
if (sortObject.sortFields && sortObject.sortFields.length > 0) {
sortObject.tableRef = this;
data.sort(this._getCompareFn(sortObject));
}
}
if (params.top) {
if (typeof(params.skip) == "undefined") {
params.skip = 0;
}
data = data.splice(params.skip, params.top);
}
}
return data;
};
this._recToDataObject = function (record, includeChildren) {
var array = [record];
var dataObject = array;
if (typeof(includeChildren) == 'undefined') {
includeChildren = false;
}
if (this._jsdo._dataSetName) {
dataObject = {};
dataObject[this._jsdo._dataSetName] = {};
dataObject[this._jsdo._dataSetName][this._name] = array;
if (includeChildren && this._children.length > 0) {
var jsrecord = this._findById(record._id, false);
if (jsrecord) {
for (var i = 0; i < this._children.length; i++) {
var tableName = this._children[i];
dataObject[this._jsdo._dataSetName][tableName] =
this._jsdo._buffers[tableName]._getRelatedData(jsrecord);
}
}
}
}
else {
if (this._jsdo._dataProperty) {
dataObject = {};
dataObject[this._jsdo._dataProperty] = array;
}
}
return dataObject;
};
this._recFromDataObject = function (dataObject) {
var data = {};
if (dataObject) {
if (this._jsdo._dataSetName) {
if (dataObject[this._jsdo._dataSetName])
data = dataObject[this._jsdo._dataSetName][this._name];
}
else {
if (this._jsdo._dataProperty) {
if (dataObject[this._jsdo._dataProperty])
data = dataObject[this._jsdo._dataProperty];
}
else if (dataObject.data) {
data = dataObject.data;
}
else {
data = dataObject;
}
}
}
return data instanceof Array ? data[0] : data;
};
// Property: schema
this.getSchema = function () {
return this._schema;
};
this.setSchema = function (schema) {
this._schema = schema;
};
// Private method that returns the ABL data type for the specified field
this._getABLType = function (fieldName) {
var i, schema;
schema = this.getSchema();
for (i = 0; i < schema.length; i++) {
if (schema[i].name == fieldName) {
return schema[i].ablType;
}
}
return undefined;
};
// Private method that returns format property (from catalog) for the specified field
this._getFormat = function (fieldName) {
var i, schema;
schema = this.getSchema();
for (i = 0; i < schema.length; i++) {
if (schema[i].name == fieldName) {
return schema[i].format;
}
}
return undefined;
};
this.add = function (values) {
return this._add(values, true, true);
};
// Alias for add() method
this.create = this.add;
this._add = function (values, trackChanges, setWorkingRecord) {
if (typeof(trackChanges) == 'undefined') {
trackChanges = true;
}
if (typeof(setWorkingRecord) == 'undefined') {
setWorkingRecord = true;
}
var record = {},
i,
j,
value,
prefixElement,
name;
if (typeof values === "undefined") {
values = {};
}
// Assign values from the schema
var schema = this.getSchema();
for (i = 0; i < schema.length; i++) {
var fieldName = schema[i].name;
if (schema[i].type == "array") {
record[fieldName] = [];
if (schema[i].maxItems) {
for (j = 0; j < schema[i].maxItems; j++) {
record[fieldName][j] = this._jsdo._getDefaultValue(schema[i]);
}
}
// Assign array values from object parameter
value = values[fieldName];
if (typeof value != "undefined") {
record[fieldName] = value;
delete values[fieldName];
}
// Assign values from individual fields from flattened arrays
prefixElement = this._jsdo._getArrayField(fieldName);
if (!record[fieldName]) {
record[fieldName] = [];
}
for (j = 0; j < schema[i].maxItems; j += 1) {
name = prefixElement.name + (j+1);
value = values[name];
if (typeof value != "undefined") {
if (!this._fields[name.toLowerCase()]) {
// Skip element if a field with the same name exists
// Remove property from object for element since it is not part of the actual schema
delete values[prefixElement.name + (j+1)];
if (typeof value == 'string' && schema[i].items.type != 'string') {
value = this._jsdo._convertType(value,
schema[i].items.type,
null);
}
record[fieldName][j] = value;
}
}
}
}
else {
record[fieldName] = this._jsdo._getDefaultValue(schema[i]);
}
}
// Assign values based on a relationship
if (this._jsdo.useRelationships && this._relationship && this._parent) {
if (this._jsdo._buffers[this._parent].record) {
for (j = 0; j < this._relationship.length; j++) {
record[this._relationship[j].childFieldName] =
this._jsdo._buffers[this._parent].record.data[this._relationship[j].parentFieldName];
}
}
else
throw new Error(msg.getMsgText("jsdoMSG002", this._parent));
}
// Assign values from object parameter
for (var v in values) {
record[v] = values[v];
}
// Specify _id field - do not use schema default
var id;
var idProperty;
if ((idProperty = this._jsdo._resource.idProperty) !== undefined) {
id = record[idProperty];
}
if (!id) {
id = progress.data._getNextId();
}
else {
id += "";
}
id += ""; // ID Property
record._id = id;
if (this.autoSort
&& this._sortRecords
&& (this._sortFn !== undefined || this._sortObject.sortFields !== undefined)) {
if (this._needsAutoSorting) {
this._data.push(record);
this._sort();
}
else {
// Find position of new record in _data and use splice
for (i = 0; i < this._data.length; i++) {
if (this._data[i] === null) continue; // Skip null elements
var ret = this._sortFn ?
this._sortFn(record, this._data[i]) :
this._compareFields(record, this._data[i]);
if (ret == -1) break;
}
this._data.splice(i, 0, record);
}
this._createIndex();
}
else {
this._data.push(record);
this._index[record._id] = new progress.data.JSIndexEntry(this._data.length - 1);
}
var jsrecord = new progress.data.JSRecord(this, record);
// Set record property ignoring relationships
if (setWorkingRecord)
this._setRecord(jsrecord, true);
if (trackChanges) {
// Save before image
this._beforeImage[record._id] = null;
// End - Save before image
this._added.push(record._id);
}
return jsrecord;
};
/*
* Returns records related to the specified jsrecord.
* If jsrecord is not specified the parent working record is used.
*/
this._getRelatedData = function (jsrecord) {
var data = [];
if (this._data.length === 0) return data;
if (typeof(jsrecord) == 'undefined') {
if (this._jsdo.useRelationships && this._relationship && this._parent) {
jsrecord = this._jsdo._buffers[this._parent].record;
if (!jsrecord)
throw new Error(msg.getMsgText("jsdoMSG002", this._parent));
}
}
if (jsrecord) {
// Filter records using relationship
for (var i = 0; i < this._data.length; i++) {
var block = this._data[i];
if (!block) continue;
var match = false;
for (var j = 0; j < this._relationship.length; j++) {
match = (jsrecord.data[this._relationship[j].parentFieldName] ==
this._data[i][this._relationship[j].childFieldName]);
if (!match) break;
}
if (match)
data.push(this._data[i]);
}
}
else
data = this._data;
return data;
};
// This method is called on a parent table that has child tables
// where the relationship is specified as NESTED.
// It returns a json array that contains the parent rows.
// If a parent row is involved in nested relationship,
// then references to the child rows are added
// to the parent row in a child table array (providing the nested format)
// We are using the internal jsdo _data arrays,
// and adding a child table array to each parent row that has children.
// Once the caller is done with the nested data, they can call jsdo._unnestData()
// which removes these child table references
this._getDataWithNestedChildren = function (data) {
// Walk through all the rows and determine if any of its child tables
// should be associated (nested) with the current record
for (var i = 0; i < data.length; i++) {
var parentRecord = data[i];
// Now walk thru the parent's children to find any nested children
if (this._children && this._children.length > 0) {
for (var j = 0; j < this._children.length; j++) {
var childBuf = this._jsdo._buffers[this._children[j]];
if (childBuf._isNested) {
// If child is nested, then we should walk child records to find matches
for (var k = 0; k < childBuf._data.length; k++) {
var childRecord = childBuf._data[k];
if (!childRecord) continue;
var match = false;
for (var m = 0; m < childBuf._relationship.length; m++) {
match = (parentRecord[childBuf._relationship[m].parentFieldName] ==
childRecord[childBuf._relationship[m].childFieldName]);
if (!match) break;
}
if (match) {
// Make sure that this parentRecord has an array for its child rows
if (!parentRecord[childBuf._name]) {
parentRecord[childBuf._name] = [];
}
parentRecord[childBuf._name].push(childRecord);
}
} // end for; finished adding all child rows for parentRecord
// The child table may have its own nested children so call recursively
// Use child row array in current parentRecord
if (childBuf._hasNestedChild()) {
childBuf._getDataWithNestedChildren(parentRecord[childBuf._name]);
}
} // end if (childBuf._isNested)
}
}
}
return data;
};
this._findFirst = function () {
if (this._jsdo.useRelationships && this._relationship && this._parent) {
if (this._jsdo._buffers[this._parent].record) {
// Filter records using relationship
for (var i = 0; i < this._data.length; i++) {
var block = this._data[i];
if (!block) continue;
var match = false;
var parentFieldName, childFieldName;
for (var j = 0; j < this._relationship.length; j++) {
parentFieldName = this._relationship[j].parentFieldName;
childFieldName = this._relationship[j].childFieldName;
match = (this._jsdo._buffers[this._parent].record.data[parentFieldName] ==
this._data[i][childFieldName]);
if (!match) break;
}
if (match) {
return new progress.data.JSRecord(this, this._data[i]);
}
}
}
}
else {
for (var i = 0; i < this._data.length; i++) {
var block = this._data[i];
if (!block) continue;
return new progress.data.JSRecord(this, this._data[i]);
}
}
return undefined;
};
this._setRecord = function (jsrecord, ignoreRelationships) {
if (jsrecord) {
this.record = jsrecord;
}
else {
this.record = undefined;
}
// Set child records only if useRelationships is true
if (this._jsdo.useRelationships) {
ignoreRelationships = ((typeof(ignoreRelationships) == 'boolean') && ignoreRelationships);
if (this._children && this._children.length > 0) {
for (var i = 0; i < this._children.length; i++) {
var childTable = this._jsdo._buffers[this._children[i]];
if (!ignoreRelationships && this.record && childTable._relationship) {
childTable._setRecord(childTable._findFirst());
}
else {
childTable._setRecord(undefined, ignoreRelationships);
}
}
}
}
if (this._jsdo._defaultTableRef) {
this._jsdo.record = this.record;
}
};
this.assign = function (values) {
if (this.record) {
return this.record.assign(values);
}
else
throw new Error(msg.getMsgText("jsdoMSG002", this._name));
};
// Alias for assign() method
this.update = this.assign;
this.remove = function () {