-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sdk.FetchXml.js
3191 lines (3044 loc) · 120 KB
/
Sdk.FetchXml.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
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
/*
The MIT License (MIT)
Copyright (c) 2016 Jim Daly
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var Sdk = window.Sdk || {};
Sdk.FetchXml = Sdk.FetchXml || {};
(function () {
//Variable to allow changing of namespace references in error messages
var n = "Sdk.FetchXml";
/** @description Contains the data for a fetchXml fetch element.
* @param {Sdk.FetchXml.entity} [entity] An Sdk.FetchXML.entity
* @property {boolean} aggregate Gets or sets the aggregate attribute of the fetch element.
* @property {number} count Gets or sets the count attribute of the fetch element.
* @property {boolean} distinct Gets or sets the distinct attribute of the fetch element.
* @property {Sdk.FetchXML.entity} entity Gets or sets the entity attribute of the fetch element.
* @property {Sdk.FetchXml.Mapping} mapping Gets or sets the mapping attribute of the fetch element.
* @property {boolean} minActiveRowVersion Gets or sets the minActiveRowVersion attribute of the fetch element.
* @property {boolean} noLock Gets or sets the noLock attribute of the fetch element.
* @property {Sdk.FetchXml.order} order Gets or sets the order attribute of the fetch element.
* @property {Sdk.FetchXml.OutputFormat} outputFormat Gets or sets the outputFormat attribute of the fetch element.
* @property {number} page Gets or sets the page attribute of the fetch element.
* @property {string} pagingCookie Gets or sets the pagingCookie attribute of the fetch element.
* @property {boolean} returnTotalRecordCount Gets or sets the returnTotalRecordCount attribute of the fetch element.
* @property {number} top Gets or sets the top attribute of the fetch element.
* @property {number} utcOffset Gets or sets the utcOffset attribute of the fetch element.
* @property {string} version Gets or sets the version attribute of the fetch element.
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch = function (entity) {
if (!(this instanceof Sdk.FetchXml.fetch))
{ return new Sdk.FetchXml.fetch(entity) }
var _count, _order, _page, _pagingCookie, _top, _utcOffset, _entity;
var _aggregate = false;
var _minActiveRowVersion = null;
var _noLock = null;
var _returnTotalRecordCount = null;
var _mapping = Sdk.FetchXml.Mapping.Logical; //Default value
var _version = null;
var _outputFormat = null;
var _distinct = false;
Object.defineProperties(this, {
"aggregate": {
get: function () { return _aggregate; },
set: function (value) {
if (!isBoolean(value)) {
throw new Error(n + ".fetch aggregate property must be a boolean value;")
}
_aggregate = value;
},
enumerable: true
},
"count": {
get: function () { return _count; },
set: function (value) {
if (!isNumberOrNull(value)) {
throw new Error(n + ".fetch count property must be a number value or null.");
}
_count = value;
},
enumerable: true
},
"distinct": {
get: function () { return _distinct; },
set: function (value) {
if (!isBoolean) {
throw new Error(n + ".fetch distinct property must be a boolean value;")
}
_distinct = value;
},
enumerable: true
},
"entity": {
get: function () { return _entity; },
set: function (value) {
if (!isEntity(value)) {
throw new Error(n + ".fetch entity property must be a " + n + ".entity value or null.");
}
_entity = value;
},
enumerable: true
},
"mapping": {
get: function () { return _mapping; },
set: function (value) {
if (!isEnumMember(Sdk.FetchXml.Mapping, value)) {
throw new Error(n + ".fetch mapping property must be a " + n + ".Mapping value.");
}
_mapping = value;
},
enumerable: true
},
"minActiveRowVersion": {
get: function () { return _minActiveRowVersion; },
set: function (value) {
if (!isBooleanOrNull(value)) {
throw new Error(n + ".fetch minActiveRowVersion property must be a boolean value or null.");
}
_minActiveRowVersion = value;
},
enumerable: true
},
"noLock": {
get: function () { return _noLock; },
set: function (value) {
if (!isBooleanOrNull(value)) {
throw new Error(n + ".fetch noLock property must be a boolean value or null.");
}
_noLock = value;
},
enumerable: true
},
"order": {
get: function () { return _order; },
set: function (value) {
if (!isOrder(value)) {
throw new Error(n + ".fetch order property must be an Sdk.FetchXml.order value.");
}
_order = value;
},
enumerable: true
},
"outputFormat": {
get: function () { return _outputFormat; },
set: function (value) {
if (!isEnumMemberOrNull(Sdk.FetchXml.OutputFormat, value)) {
throw new Error(n + ".fetch outputFormat property must be a " + n + ".OutputFormatType value or null.");
}
_outputFormat = value;
},
enumerable: true
},
"page": {
get: function () { return _page; },
set: function (value) {
if (!isNumberOrNull(value)) {
throw new Error(n + ".fetch page property must be a number value or null.");
}
_page = value;
},
enumerable: true
},
"pagingCookie": {
get: function () { return _pagingCookie; },
set: function (value) {
if (!isStringOrNull(value)) {
throw new Error(n + ".fetch pagingCookie property must be a string value or null.");
}
_pagingCookie = value;
},
enumerable: true
},
"returnTotalRecordCount": {
get: function () { return _returnTotalRecordCount; },
set: function (value) {
if (!isBooleanOrNull(value)) {
throw new Error(n + ".fetch returnTotalRecordCount property must be a boolean value or null.");
}
_returnTotalRecordCount = value;
},
enumerable: true
},
"top": {
get: function () { return _top; },
set: function (value) {
if (!isNumberOrNull(value)) {
throw new Error(n + ".fetch top property must be a number value or null.");
}
_top = value;
},
enumerable: true
},
"utcOffset": {
get: function () { return _utcOffset; },
set: function (value) {
if (!isNumberOrNull(value)) {
throw new Error(n + ".fetch utcOffset property must be a number value or null.");
}
_utcOffset = value;
},
enumerable: true
},
"version": {
get: function () { return _version; },
set: function (value) {
if (!isStringOrNull(value)) {
throw new Error(n + ".fetch version property must be a string value or null.");
}
_version = value;
},
enumerable: true
}
});
//Constructor
if (entity) {
this.entity = entity;
}
return Object.seal(this);
}
/** @description Sets the aggregate attribute of the fetch element
* @param {boolean} aggregate The aggregate value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setAggregate = function (aggregate) {
this.aggregate = aggregate;
return this;
}
/** @description Sets the count attribute of the fetch element
* @param {Number} count The count value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setCount = function (count) {
this.count = count;
return this;
}
/** @description Sets the distinct attribute of the fetch element
* @param {boolean} distinct The distinct value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setDistinct = function (distinct) {
this.distinct = distinct;
return this;
}
/** @description Sets the aggregate entity of the fetch element
* @param {Sdk.FetchXml.entity} entity The entity value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setEntity = function (entity) {
this.entity = entity;
return this;
}
/** @description Sets the mapping attribute of the fetch element
* @param {Sdk.FetchXml.Mapping} mapping The mapping value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setMapping = function (mapping) {
this.mapping = mapping;
return this;
}
/** @description Sets the minActiveRowVersion attribute of the fetch element
* @param {boolean} minActiveRowVersion The minActiveRowVersion value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setMinActiveRowVersion = function (minActiveRowVersion) {
this.minActiveRowVersion = minActiveRowVersion;
return this;
}
/** @description Sets the noLock attribute of the fetch element
* @param {boolean | null} noLock The noLock value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setNoLock = function (noLock) {
this.noLock = noLock;
return this;
}
/** @description Sets the order attribute of the fetch element
* @param {Sdk.FetchXml.order} order The order value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setOrder = function (order) {
this.order = order;
return this;
}
/** @description Sets the outputFormat attribute of the fetch element
* @param {Sdk.FetchXml.OutputFormat} outputFormat The outputFormat value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setOutputFormat = function (outputFormat) {
this.outputFormat = outputFormat;
return this;
}
/** @description Sets the page attribute of the fetch element
* @param {number | null} page The page value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setPage = function (page) {
this.page = page;
return this;
}
/** @description Sets the pagingCookie attribute of the fetch element
* @param {string} pagingCookie The pagingCookie value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setPagingCookie = function (pagingCookie) {
this.pagingCookie = pagingCookie;
return this;
}
/** @description Sets the returnTotalRecordCount attribute of the fetch element
* @param {boolean | null} returnTotalRecordCount The returnTotalRecordCount value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setReturnTotalRecordCount = function (returnTotalRecordCount) {
this.returnTotalRecordCount = returnTotalRecordCount;
return this;
}
/** @description Sets the top attribute of the fetch element
* @param {number} top The top value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setTop = function (top) {
this.top = top;
return this;
}
/** @description Sets the utcOffset attribute of the fetch element
* @param {number | null} utcOffset The utcOffset value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setUtcOffset = function (utcOffset) {
this.utcOffset = utcOffset;
return this;
}
/** @description Sets the version attribute of the fetch element
* @param {string | null} version The version value to apply to the fetch element
* @returns {Sdk.FetchXml.fetch}
*/
this.fetch.prototype.setVersion = function (version) {
this.version = version;
return this;
}
//Internal use only
this.fetch.prototype.toXml = function () {
var root = "<fetch />";
var doc = new DOMParser().parseFromString(root, "text/xml");
if (this.version) {
doc.documentElement.setAttribute("version", this.version)
}
if (this.outputFormat) {
doc.documentElement.setAttribute("output-format", this.outputFormat)
}
if (this.mapping) {
doc.documentElement.setAttribute("mapping", this.mapping)
}
if (this.aggregate) {
doc.documentElement.setAttribute("aggregate", (this.aggregate) ? "true" : "false")
}
if (this.count) {
doc.documentElement.setAttribute("count", this.count)
}
if (!isNullOrUndefined(this.distinct)) {
doc.documentElement.setAttribute("distinct", (this.distinct) ? "true" : "false")
}
if (this.entity) {
doc.documentElement.appendChild(this.entity.toXml(doc));
}
if (isBoolean(this.minActiveRowVersion)) {
doc.documentElement.setAttribute("min-active-row-version", (this.minActiveRowVersion) ? "true" : "false")
}
if (isBoolean(this.noLock)) {
doc.documentElement.setAttribute("no-lock", (this.noLock) ? "true" : "false")
}
if (this.order) {
doc.documentElement.appendChild(this.order.toXml(doc));
}
if (this.page) {
doc.documentElement.setAttribute("page", this.page)
}
if (this.pagingCookie) {
doc.documentElement.setAttribute("paging-cookie", this.pagingCookie) //Does this need special handling?
}
if (isBoolean(this.returnTotalRecordCount)) {
doc.documentElement.setAttribute("returntotalrecordcount", (this.returnTotalRecordCount) ? "true" : "false")
}
if (this.top) {
doc.documentElement.setAttribute("top", this.top)
}
if (this.utcOffset) {
doc.documentElement.setAttribute("utc-offset", this.utcOffset)
}
return new XMLSerializer().serializeToString(doc);
}
this.fetch.fromXml = fetchFromXml; //A static method
function fetchFromXml(xml) {
var doc = new DOMParser().parseFromString(xml, "text/xml");
var fetchObj = new Sdk.FetchXml.fetch();
var errorMessage = "The XML string is not a valid fetchXML document";
if (doc.documentElement.localName == "fetch") {
parseAttributes(doc.documentElement, fetchObj, fetchSwitch);
for (var i = 0; i < doc.documentElement.childNodes.length; i++) {
if (doc.documentElement.childNodes[i].nodeName == "entity") {
fetchObj.entity = entityFromXml(doc.documentElement.childNodes[i]);
}
if (doc.documentElement.childNodes[i].nodeName == "order") {
fetchObj.order = orderFromXml(doc.documentElement.childNodes[i]);
}
}
}
else {
throw new Error(errorMessage);
}
return fetchObj;
}
//switchfunction for fetchFromXml
function fetchSwitch(fetchObj, name, value) {
switch (name) {
case "count":
case "top":
case "page":
fetchObj[name] = parseInt(value, 10);
break;
case "utc-offset":
if (!isNaN(value)) {
fetchObj.utcOffset = parseInt(value, 10);
}
else {
fetchObj.utcOffset = null;
}
break;
case "paging-cookie":
fetchObj.pagingCookie = value;
break;
case "min-active-row-version":
fetchObj.minActiveRowVersion = (value == "true") ? true : false;
break;
case "output-format":
fetchObj.outputFormat = value;
break;
case "returntotalrecordcount":
fetchObj.returnTotalRecordCount = (value == "true") ? true : false;
break;
case "no-lock":
fetchObj.noLock = (value == "true") ? true : false;
break;
case "aggregate": //xs:boolean
case "distinct":
fetchObj[name] = (value == "true") ? true : false;
break;
default:
fetchObj[name] = value;
break;
}
}
function entityFromXml(xml) {
var entityObj = new Sdk.FetchXml.entity(xml.attributes.getNamedItem("name").nodeValue);
entityObj.allAttributes = (xml.getElementsByTagName("all-attributes").length == 1);
for (var i = 0; i < xml.childNodes.length; i++) {
if (xml.childNodes[i].nodeName == "attribute") {
entityObj.addAttribute(attributeFromXml(xml.childNodes[i]));
}
if (xml.childNodes[i].nodeName == "order") {
entityObj.addOrder(orderFromXml(xml.childNodes[i]));
}
if (xml.childNodes[i].nodeName == "link-entity") {
entityObj.addLinkEntity(linkEntityFromXml(xml.childNodes[i]));
}
if (xml.childNodes[i].nodeName == "filter") {
entityObj.addFilter(filterFromXml(xml.childNodes[i]));
}
}
return entityObj;
}
function orderFromXml(xml) {
var orderObj = new Sdk.FetchXml.order();
parseAttributes(xml, orderObj, orderSwitch);
return orderObj;
}
//switchfunction for orderFromXml
function orderSwitch(orderObj, name, value) {
switch (name) {
case "attribute":
orderObj.attribute = value;
break;
case "alias":
orderObj.alias = value;
break;
case "descending":
orderObj.descending = (value == "true") ? true : false;
break;
default:
break;
}
}
function attributeFromXml(xml) {
var attObj = new Sdk.FetchXml.attribute(xml.attributes.getNamedItem("name").nodeValue);
parseAttributes(xml, attObj, attributeSwitch)
return attObj;
}
//switchfunction for attributeFromXml
function attributeSwitch(attObj, name, value) {
switch (name) {
case "build":
case "alias":
case "aggregate":
attObj[name] = value;
break;
case "addedby":
attObj.addedBy = value;
break;
case "dategrouping":
attObj.dateGrouping = value;
break;
case "groupby":
attObj.groupBy = convertFetchBoolType(value);
case "usertimezone":
attObj.userTimeZone = convertFetchBoolType(value);
break;
default:
break;
}
}
function linkEntityFromXml(xml) {
var linkEntityObj = new Sdk.FetchXml.linkEntity(xml.attributes.getNamedItem("name").nodeValue);
linkEntityObj.allAttributes = (xml.getElementsByTagName("all-attributes").length == 1);
parseAttributes(xml, linkEntityObj, LinkEntitySwitch);
for (var i = 0; i < xml.childNodes.length; i++) {
if (xml.childNodes[i].nodeName == "attribute") {
linkEntityObj.addAttribute(attributeFromXml(xml.childNodes[i]));
}
if (xml.childNodes[i].nodeName == "order") {
linkEntityObj.addOrder(orderFromXml(xml.childNodes[i]));
}
if (xml.childNodes[i].nodeName == "filter") {
linkEntityObj.addFilter(filterFromXml(xml.childNodes[i]));
}
if (xml.childNodes[i].nodeName == "link-entity") {
linkEntityObj.addLinkEntity(linkEntityFromXml(xml.childNodes[i]));
}
}
return linkEntityObj;
}
//switchfunction for linkEntityFromXml
function LinkEntitySwitch(linkEntityObj, name, value) {
switch (name) {
case "to":
case "from":
case "alias":
linkEntityObj[name] = value;
break;
case "link-type":
linkEntityObj.linktype = value;
break;
case "visible":
case "intersect":
linkEntityObj[name] = (value == "true") ? true : false;
break;
default:
break;
}
}
function filterFromXml(xml) {
var filterObj = new Sdk.FetchXml.filter();
parseAttributes(xml, filterObj, filterSwitch);
for (var i = 0; i < xml.childNodes.length; i++) {
if (xml.childNodes[i].nodeName == "condition") {
filterObj.addCondition(conditionFromXml(xml.childNodes[i]));
}
if (xml.childNodes[i].nodeName == "filter") {
filterObj.addFilter(filterFromXml(xml.childNodes[i]));
}
}
return filterObj;
}
//switchfunction for filterFromXml
function filterSwitch(filterObj, name, value) {
switch (name) {
case "type":
filterObj.type = value;
break;
case "isquickfindfields":
filterObj.isQuickFindFields = (value == "true" || value == "1") ? true : false;
break;
default:
break;
}
}
function conditionFromXml(xml) {
var conditionObj = new Sdk.FetchXml.condition(
xml.attributes.getNamedItem("attribute").nodeValue,
xml.attributes.getNamedItem("operator").nodeValue
);
parseAttributes(xml, conditionObj, conditionSwitch)
for (var i = 0; i < xml.childNodes.length; i++) {
if (xml.childNodes[i].nodeName == "value") {
conditionObj.addValue(valueFromXml(xml.childNodes[i]));
}
}
return conditionObj;
}
//switchfunction for conditionFromXml
function conditionSwitch(conditionObj, name, value) {
switch (name) {
case "column":
case "entityname":
case "aggregate":
case "alias":
case "uiname":
case "uitype":
conditionObj[name] = value;
break;
case "rowaggregate":
conditionObj.rowAggregate = value;
break;
case "uihidden":
conditionObj.uihidden = convertFetchBoolType(value);
break;
case "value":
conditionObj.addValue(value);
break;
default:
break;
}
}
function valueFromXml(xml) {
var valueObj = new Sdk.FetchXml.value(xml.textContent);
parseAttributes(xml, valueObj, valueSwitch);
return valueObj;
}
//switch function for valueFromXml
function valueSwitch(object, name, value) {
switch (name) {
case "uiname":
case "uitype":
object[name] = value;
break;
}
}
//Loops through the attributes of a node and applies switch function to set attributes
function parseAttributes(xml, object, switchFunction) {
var atts = xml.attributes;
for (var i = 0; i < atts.length; i++) {
var name = atts[i].nodeName;
var value = atts[i].nodeValue;
switchFunction(object, name, value)
}
}
/** @description Contains the data for a fetchXml entity element.
* @param {string} name The logical name of the attribute
* @param {Array} [attributes] An array of Sdk.FetchXML.attribute
* @param {Array} [orders] An array of Sdk.FetchXML.order
* @param {Array} [linkEntities] An array of Sdk.FetchXML.linkEntity
* @param {Array} [filters] The filter to apply to the entity
* @property {boolean} allAttributes Gets or sets whether all attributes for the entity should be returned
* @property {Array} attributes Gets or sets the attributes to be returned by the query
* @property {Array} orders Gets or sets the orders to be applied to the query
* @property {Array} linkEntities Gets or sets the linked entities for the query
* @property {Array} filters Gets or sets the filters for the query
* @property {string} name Gets or sets logical name of the entity
* @returns {Sdk.FetchXml.entity}
*/
this.entity = function (name, attributes, orders, linkEntities, filters) {
if (!(isEntity(this)))
{ return new Sdk.FetchXml.entity(name, attributes, orders, linkEntities, filters) }
var _name;
var _allAttributes = false;
var _attributes = [];
var _orders = [];
var _filters = [];
var _linkEntities = [];
Object.defineProperties(this, {
"allAttributes": {
get: function () { return _allAttributes; },
set: function (value) {
if (!isBoolean(value)) {
throw new Error(n + ".entity allAttributes property must be a boolean value.");
}
_allAttributes = value;
},
enumerable: true
},
"attributes": {
get: function () { return _attributes; },
set: function (value) {
if (!isAttributeArrayOrNull(value)) {
throw new Error(n + ".entity attributes property must be an array of " + n + ".attribute or null.");
}
if (isNull(value))
{ _attributes = []; }
else {
_attributes = value;
}
},
enumerable: true
},
"orders": {
get: function () { return _orders; },
set: function (value) {
if (!isOrderArrayOrNull(value)) {
throw new Error(n + ".entity orders property must be an array of " + n + ".order or null.");
}
if (isNull(value)) {
_orders = [];
}
else {
_orders = value;
}
},
enumerable: true
},
"linkEntities": {
get: function () { return _linkEntities; },
set: function (value) {
if (!isLinkEntityArrayOrNull(value)) {
throw new Error(n + ".entity linkEntities property must be an array of " + n + ".linkEntity or null.");
}
if (isNull(value)) {
_linkEntities = [];
}
else {
_linkEntities = value;
}
},
enumerable: true
},
"filters": {
get: function () { return _filters; },
set: function (value) {
if (!isFilterArrayOrNull(value)) {
throw new Error(n + ".entity filter property must be an array of " + n + ".filter or null.");
}
if (isNull(value)) {
_filters = [];
}
else {
_filters = value;
}
},
enumerable: true
},
"name": {
get: function () { return _name; },
set: function (value) {
if (!isString(value)) {
throw new Error(n + ".entity name property must be an string.");
}
_name = value;
},
enumerable: true
}
});
//Constructor
if (name) {
this.name = name;
}
else {
throw new Error(n + ".entity name constructor parameter is required.");
}
if (attributes) {
this.attributes = attributes;
}
if (orders) {
this.orders = orders;
}
if (linkEntities) {
this.linkEntities = linkEntities;
}
if (filters) {
this.filters = filters;
}
return Object.seal(this);
}
/** @description Adds an attribute to the attribute collection
* @param {Sdk.FetchXml.attribute | string} attributeOrAttributeName the attribute or attribute name to add
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.addAttribute = function (attributeOrAttributeName) {
if (isString(attributeOrAttributeName)) {
// Some saved queries have duplicate attributes defined, so this is a bit more strict than it has to be.
var exists = false;
this.attributes.forEach(function (a) {
if (a.name == attributeOrAttributeName) {
exists = true;
}
});
if (!exists) {
this.attributes.push(new Sdk.FetchXml.attribute(attributeOrAttributeName))
}
return this;
}
if (isAttribute(attributeOrAttributeName)) {
var exists = false;
this.attributes.forEach(function (a) {
if (a.name == attributeOrAttributeName.name) {
exists = true;
}
});
if (!exists) {
this.attributes.push(attributeOrAttributeName);
}
}
else {
throw new Error(n + ".entity addAttribute method attributeOrAttributeName parameter must be an " + n + ".attribute or a string value.");
}
return this;
}
/** @description Removes an attribute from the attributes collection by name
* @param {string} attributeName The name of the attribute to remove
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.removeAttributeByName = function (attributeName) {
removeCollectionValueByProperty(this.attributes, "name", attributeName)
}
/** @description Removes an attribute from the attributes collection by reference
* @param {Sdk.FetchXml.attribute} attribute The attribute to remove
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.removeAttributeByRef = function (attribute) {
removeCollectionValueByRef(this.attributes, attribute);
}
/** @description Removes an attribute from the attributes collection
* @param {Sdk.FetchXml.attribute} attribute The attribute to remove
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.removeAttribute = function (attribute) {
removeCollectionValue(this.attributes, attribute);
}
/** @description Adds an order to the orders collection
* @param {Sdk.FetchXml.order | string} orderOrAttribute The order to add to the attributes collection or the Attribute property of a new order to create
* @param {boolean} descending Whether the order is descending. True if descending, otherwise false
* @param {string} alias The alias to set for the order
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.addOrder = function (orderOrAttribute, descending, alias) {
if (isOrder(orderOrAttribute)) {
this.orders.push(orderOrAttribute);
return this;
}
if (isString(orderOrAttribute)) {
this.orders.push(new Sdk.FetchXml.order(orderOrAttribute, descending, alias));
}
else {
throw new Error(n + ".entity addOrder method orderOrAttribute parameter must be a " + n + ".order value or a string.");
}
return this;
}
/** @description Removes a order from the orders collection by reference
* @param {Sdk.FetchXml.order} order The order to remove from the orders collection
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.removeOrderByRef = function (order) {
if (!isOrder(order)) {
throw new Error(n + ".entity removeOrderByRef method order parameter must be an " + n + ".order.")
}
removeCollectionValueByRef(this.orders, order);
return this;
}
/** @description Removes a order from the orders collection by value
* @param {Sdk.FetchXml.order} order The order to remove from the orders collection
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.removeOrder = function (order) {
if (!isOrder(order)) {
throw new Error(n + ".entity removeOrder method order parameter must be an " + n + ".order.")
}
removeCollectionValue(this.orders, order);
return this;
}
/** @description Sets the name property of the entity element
* @param {string} name The name value to apply to the entity
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.setName = function (name) {
this.name = name;
return this;
}
/** @description Sets the allAttributes property of the entity
* @param {boolean} [allAttributes] The allAttributes value to apply to the entity. The default is true;
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.setAllAttributes = function (allAttributes) {
if (isBoolean(allAttributes)) {
this.allAttributes = allAttributes;
}
else {
if (isNullOrUndefined(allAttributes)) {
this.allAttributes = true;
}
else {
throw new Error(n + ".entity setAllAttributes method allAttributes parameter must be a boolean value.");
}
}
return this;
}
/** @description Adds an linkEntity to the linkEntities collection
* @param {Sdk.FetchXml.linkEntity} linkEntity The linkEntity to add to the entity
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.addLinkEntity = function (linkEntity) {
if (isLinkEntity(linkEntity)) {
this.linkEntities.push(linkEntity);
return this;
}
else {
throw new Error(n + ".entity addLinkEntity method linkEntity parameter must be a " + n + ".linkEntity value.");
}
return this;
}
/** @description Removes a linkEntity from the linkEntities collection by reference
* @param {Sdk.FetchXml.linkEntity} linkEntity The linkEntity to remove from the linkEntities collection
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.removeLinkEntityByRef = function (linkEntity) {
if (!isLinkEntity(linkEntity)) {
throw new Error(n + ".entity removeLinkEntityByRef method linkEntity parameter must be an " + n + ".linkEntity.")
}
removeCollectionValueByRef(this.linkEntities, linkEntity);
return this;
}
/** @description Removes a linkEntity from the linkEntities collection by value
* @param {Sdk.FetchXml.linkEntity} linkEntity The linkEntity to remove from the linkEntities collection
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.removeLinkEntity = function (linkEntity) {
if (!isLinkEntity(linkEntity)) {
throw new Error(n + ".entity removeLinkEntity method linkEntity parameter must be an " + n + ".linkEntity.")
}
removeCollectionValue(this.linkEntities, linkEntity);
return this;
}
/** @description Adds an filter to the filters collection
* @param {Sdk.FetchXml.filter} filter The filter to add to the entity
* @returns {Sdk.FetchXml.entity}
*/
this.entity.prototype.addFilter = function (filter) {
if (isFilter(filter)) {
this.filters.push(filter);
return this;
}
else {
throw new Error(n + ".entity addFilter method filter parameter must be a " + n + ".filter value.");
}
return this;
}