/
OData4CodeGenerator.cs
937 lines (858 loc) · 40 KB
/
OData4CodeGenerator.cs
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
using Microsoft.Xrm.Sdk.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cinteros.Xrm.FetchXmlBuilder.AppCode
{
public class OData4CodeGenerator
{
public static string GetOData4Query(FetchType fetch, string organizationServiceUrl, FetchXmlBuilder sender)
{
if (sender.Service == null)
{
throw new Exception("Must have an active connection to CRM to compose OData query.");
}
var url = organizationServiceUrl;
var entity = fetch.Items.Where(i => i is FetchEntityType).FirstOrDefault() as FetchEntityType;
if (entity == null)
{
throw new Exception("Fetch must contain entity definition");
}
url += "/" + LogicalToCollectionName(entity.name, sender);
var query = "";
if (!string.IsNullOrEmpty(fetch.top))
{
query = AppendQuery(query, "$top", fetch.top);
}
if (entity.Items != null)
{
if (fetch.aggregate)
{
var aggregate = GetAggregate(entity, sender);
var filter = GetFilter(entity, sender, null);
var apply = aggregate;
if (!String.IsNullOrEmpty(filter))
apply = $"filter({filter})/{aggregate}";
query = AppendQuery(query, "$apply", apply);
}
else
{
var select = GetSelect(entity, sender);
var order = GetOrder(entity, sender);
var expandFilter = "";
var expand = GetExpand(entity, sender, ref expandFilter);
var filter = GetFilter(entity, sender, expandFilter);
query = AppendQuery(query, "$select", select);
query = AppendQuery(query, "$orderby", order);
query = AppendQuery(query, "$expand", expand);
query = AppendQuery(query, "$filter", filter);
}
}
if (!string.IsNullOrEmpty(query))
{
url += "?" + query;
}
return url;
}
private static string AppendQuery(string query, string paramname, string append)
{
var result = new StringBuilder(query);
if (!string.IsNullOrEmpty(append))
{
if (!string.IsNullOrEmpty(query))
{
result.Append("&");
}
result.Append(paramname + "=" + append);
}
return result.ToString();
}
private static string GetSelect(FetchEntityType entity, FetchXmlBuilder sender)
{
var attributeitems = entity.Items
.OfType<FetchAttributeType>()
.Where(i => i.name != null);
var result = GetAttributeNames(entity.name, attributeitems, sender);
return string.Join(",", result);
}
private static IEnumerable<string> GetAttributeNames(string entityName, IEnumerable<FetchAttributeType> attributeitems, FetchXmlBuilder sender)
{
GetEntityMetadata(entityName, sender);
var entityMeta = sender.entities[entityName];
foreach (FetchAttributeType attributeitem in attributeitems)
{
if (!String.IsNullOrEmpty(attributeitem.alias))
throw new ApplicationException($"OData queries do not support aliasing columns except for aggregate queries");
var attrMeta = entityMeta.Attributes.SingleOrDefault(a => a.LogicalName == attributeitem.name);
if (attrMeta == null)
throw new ApplicationException($"Unknown attribute {entityName}.{attributeitem.name}");
yield return GetPropertyName(attrMeta);
}
}
private static string GetExpand(FetchEntityType entity, FetchXmlBuilder sender, ref string filterString)
{
var resultList = new List<string>();
var linkitems = entity.Items.Where(i => i is FetchLinkEntityType).ToList();
if (linkitems.Count > 0)
{
foreach (FetchLinkEntityType linkitem in linkitems)
{
var navigationProperty = LinkItemToNavigationProperty(entity.name, linkitem, sender, out var child);
if (linkitem.Items != null)
{
if (!child)
{
if (linkitem.Items.Where(i => i is filter).ToList().Count > 0)
{
foreach (var filter in linkitem.Items.OfType<filter>())
{
foreach (var condition in filter.Items.OfType<condition>())
{
var targetLogicalName = linkitem.name;
GetEntityMetadata(targetLogicalName, sender);
if (!String.IsNullOrEmpty(filterString))
filterString += $" {filter.type} ";
filterString += navigationProperty + "/" + GetCondition(linkitem.name, condition, sender);
}
}
}
if (linkitem.Items.Where(i => i is FetchOrderType).ToList().Count > 0)
{
throw new Exception("OData queries do not support sorting on parent link entities");
}
}
}
var expandedSelect = GetExpandedSelect(linkitem, sender);
var childFilter = child ? linkitem.Items?.OfType<filter>().FirstOrDefault() : null;
var expandedFilter = childFilter == null ? null : GetFilter(linkitem.name, childFilter, sender);
var childOrders = child ? linkitem.Items?.OfType<FetchOrderType>().ToList() : null;
var expandedOrder = childOrders == null ? null : GetOrder(linkitem.name, sender, childOrders);
if (String.IsNullOrEmpty(expandedSelect) && String.IsNullOrEmpty(expandedFilter) && String.IsNullOrEmpty(expandedOrder))
{
resultList.Add(navigationProperty);
}
else
{
var options = new List<string>();
if (!String.IsNullOrEmpty(expandedSelect))
options.Add("$select=" + expandedSelect);
if (!String.IsNullOrEmpty(expandedFilter))
options.Add("$filter=" + expandedFilter);
if (!String.IsNullOrEmpty(expandedOrder))
options.Add("$orderby=" + expandedOrder);
resultList.Add(navigationProperty + "(" + String.Join(";", options) + ")");
}
}
}
return string.Join(",", resultList);
}
private static string GetExpandedSelect(FetchLinkEntityType linkitem, FetchXmlBuilder sender, string entityname = "")
{
if (linkitem.Items == null)
{
return "";
}
var linkentity = linkitem.name;
if (linkentity == null)
return null;
var attributeitems = linkitem.Items
.OfType<FetchAttributeType>()
.Where(i => i.name != null);
if (linkitem.intersect)
{
var linkitems = linkitem.Items.Where(i => i is FetchLinkEntityType).ToList();
if (linkitems.Count > 1)
{
throw new Exception("Invalid M:M-relation definition for OData");
}
if (linkitems.Count == 1)
{
var nextlink = (FetchLinkEntityType)linkitems[0];
linkentity = nextlink.name;
if (nextlink.Items == null)
return null;
attributeitems = nextlink.Items
.OfType<FetchAttributeType>()
.Where(i => i.name != null);
}
}
var resultList = GetAttributeNames(linkentity, attributeitems, sender);
return string.Join(",", resultList) + GetNestedExpand(linkitem, sender, linkitem.name);
}
private static string GetNestedExpand(FetchLinkEntityType linkitem, FetchXmlBuilder sender, string entityname)
{
var resultList = new List<string>();
var linkitems = linkitem.Items.Where(i => i is FetchLinkEntityType).ToList();
if (linkitem.intersect || linkitems.Count == 0)
{
return "";
}
foreach (FetchLinkEntityType linkentityitem in linkitems)
{
var navigationProperty = LinkItemToNavigationProperty(entityname, linkentityitem, sender, out var child);
if (child)
{
throw new Exception($"The navigation property '{navigationProperty}' can't be expanded. Only many-to-one relationships are supported for nested expansion in OData queries.");
}
resultList.Add(navigationProperty + "($select=" + GetExpandedSelect(linkentityitem, sender, linkentityitem.name) + ")");
}
return ";$expand=" + string.Join(",", resultList);
}
private static string GetFilter(FetchEntityType entity, FetchXmlBuilder sender, string expandFilter)
{
var resultList = new StringBuilder();
var filteritems = entity.Items.Where(i => i is filter && ((filter)i).Items != null && ((filter)i).Items.Length > 0).ToList();
if (filteritems.Count > 0)
{
var and = true;
foreach (filter filteritem in filteritems)
{
resultList.Append(GetFilter(entity.name, filteritem, sender));
if (filteritem.type == filterType.or)
and = false;
}
var result = resultList.ToString();
if (result.StartsWith("(") && result.EndsWith(")"))
{
result = result.Substring(1, result.Length - 2);
}
if (!String.IsNullOrEmpty(expandFilter))
{
if (!and)
result = "(" + result + ")";
result += " and " + expandFilter;
}
return result;
}
return expandFilter;
}
private static string GetFilter(string entity, filter filteritem, FetchXmlBuilder sender)
{
var result = "";
if (filteritem.Items == null || filteritem.Items.Length == 0)
{
return "";
}
var logical = filteritem.type == filterType.or ? " or " : " and ";
if (filteritem.Items.Length > 1)
{
result = "(";
}
foreach (var item in filteritem.Items)
{
if (item is condition)
{
result += GetCondition(entity, item as condition, sender);
}
else if (item is filter)
{
result += GetFilter(entity, item as filter, sender);
}
result += logical;
}
if (result.EndsWith(logical))
{
result = result.Substring(0, result.Length - logical.Length);
}
if (filteritem.Items.Length > 1)
{
result += ")";
}
return result;
}
private static string GetCondition(string entityName, condition condition, FetchXmlBuilder sender)
{
var result = "";
if (!string.IsNullOrEmpty(condition.attribute))
{
if (!String.IsNullOrEmpty(condition.entityname))
throw new ApplicationException($"OData queries do not support filtering on link entities. If filtering on the primary key of an N:1 related entity, please add the filter to the link entity itself");
GetEntityMetadata(entityName, sender);
var attrMeta = sender.GetAttribute(entityName, condition.attribute);
if (attrMeta == null)
{
throw new Exception($"No metadata for attribute: {entityName}.{condition.attribute}");
}
result = GetPropertyName(attrMeta);
string function = null;
var functionParameters = 1;
var functionParameterType = typeof(string);
switch (condition.@operator)
{
case @operator.eq:
case @operator.ne:
case @operator.lt:
case @operator.le:
case @operator.gt:
case @operator.ge:
result += $" {condition.@operator} ";
break;
case @operator.neq:
result += " ne ";
break;
case @operator.@null:
result += " eq null";
break;
case @operator.notnull:
result += " ne null";
break;
case @operator.like:
result = $"contains({attrMeta.LogicalName}, '{condition.value}')";
break;
case @operator.notlike:
result = $"not contains({attrMeta.LogicalName}, '{condition.value}')";
break;
case @operator.beginswith:
result = $"startswith({attrMeta.LogicalName}, '{condition.value}')";
break;
case @operator.endswith:
result = $"endswith({attrMeta.LogicalName}, '{condition.value}')";
break;
case @operator.above:
function = "Above";
break;
case @operator.eqorabove:
function = "AboveOrEqual";
break;
case @operator.between:
function = "Between";
functionParameters = Int32.MaxValue;
break;
case @operator.containvalues:
function = "ContainsValues";
functionParameters = Int32.MaxValue;
break;
case @operator.notcontainvalues:
function = "DoesNotContainValues";
functionParameters = Int32.MaxValue;
break;
case @operator.eqbusinessid:
function = "EqualBusinessId";
functionParameters = 0;
break;
case @operator.equserid:
function = "EqualUserId";
functionParameters = 0;
break;
case @operator.equserlanguage:
function = "EqualUserLanguage";
functionParameters = 0;
break;
case @operator.equseroruserhierarchy:
function = "EqualUserOrUserHierarchy";
functionParameters = 0;
break;
case @operator.equseroruserhierarchyandteams:
function = "EqualUserOrUserHierarchyAndTeams";
functionParameters = 0;
break;
case @operator.equseroruserteams:
function = "EqualUserOrUserTeams";
functionParameters = 0;
break;
case @operator.equserteams:
function = "EqualUserTeams";
functionParameters = 0;
break;
case @operator.@in:
function = "In";
functionParameters = Int32.MaxValue;
break;
case @operator.infiscalperiod:
function = "InFiscalPeriod";
functionParameterType = typeof(long);
break;
case @operator.infiscalperiodandyear:
function = "InFiscalPeriodAndYear";
functionParameters = 2;
functionParameterType = typeof(long);
break;
case @operator.infiscalyear:
function = "InFiscalYear";
functionParameterType = typeof(long);
break;
case @operator.inorafterfiscalperiodandyear:
function = "InOrAfterFiscalPeriodAndYear";
functionParameters = 2;
functionParameterType = typeof(long);
break;
case @operator.inorbeforefiscalperiodandyear:
function = "InOrBeforeFiscalPeriodAndYear";
functionParameters = 2;
functionParameterType = typeof(long);
break;
case @operator.lastsevendays:
function = "Last7Days";
functionParameters = 0;
break;
case @operator.lastfiscalperiod:
function = "LastFiscalPeriod";
functionParameters = 0;
break;
case @operator.lastfiscalyear:
function = "LastFiscalYear";
functionParameters = 0;
break;
case @operator.lastmonth:
function = "LastMonth";
functionParameters = 0;
break;
case @operator.lastweek:
function = "LastWeek";
functionParameters = 0;
break;
case @operator.lastxdays:
function = "LastXDays";
functionParameterType = typeof(long);
break;
case @operator.lastxfiscalperiods:
function = "LastXFiscalPeriods";
functionParameterType = typeof(long);
break;
case @operator.lastxfiscalyears:
function = "LastXFiscalYears";
functionParameterType = typeof(long);
break;
case @operator.lastxhours:
function = "LastXHours";
functionParameterType = typeof(long);
break;
case @operator.lastxmonths:
function = "LastXMonths";
functionParameterType = typeof(long);
break;
case @operator.lastxweeks:
function = "LastXWeeks";
functionParameterType = typeof(long);
break;
case @operator.lastxyears:
function = "LastXYears";
functionParameterType = typeof(long);
break;
case @operator.lastyear:
function = "LastYear";
functionParameters = 0;
break;
case @operator.nextsevendays:
function = "Next7Days";
functionParameters = 0;
break;
case @operator.nextfiscalperiod:
function = "NextFiscalPeriod";
functionParameters = 0;
break;
case @operator.nextfiscalyear:
function = "NextFiscalYear";
functionParameters = 0;
break;
case @operator.nextmonth:
function = "NextMonth";
functionParameters = 0;
break;
case @operator.nextweek:
function = "NextWeek";
functionParameters = 0;
break;
case @operator.nextxdays:
function = "NextXDays";
functionParameterType = typeof(long);
break;
case @operator.nextxfiscalperiods:
function = "NextXFiscalPeriods";
functionParameterType = typeof(long);
break;
case @operator.nextxfiscalyears:
function = "NextXFiscalYears";
functionParameterType = typeof(long);
break;
case @operator.nextxhours:
function = "NextXHours";
functionParameterType = typeof(long);
break;
case @operator.nextxmonths:
function = "NextXMonths";
functionParameterType = typeof(long);
break;
case @operator.nextxweeks:
function = "NextXWeeks";
functionParameterType = typeof(long);
break;
case @operator.nextxyears:
function = "NextXYears";
functionParameterType = typeof(long);
break;
case @operator.nextyear:
function = "NextYear";
functionParameters = 0;
break;
case @operator.notbetween:
function = "NotBetween";
functionParameters = Int32.MaxValue;
break;
case @operator.nebusinessid:
function = "NotEqualBusinessId";
functionParameters = 0;
break;
case @operator.neuserid:
function = "NotEqualUserId";
functionParameters = 0;
break;
case @operator.notin:
function = "NotIn";
functionParameters = Int32.MaxValue;
break;
case @operator.notunder:
function = "NotUnder";
break;
case @operator.olderthanxdays:
function = "OlderThanXDays";
functionParameterType = typeof(long);
break;
case @operator.olderthanxhours:
function = "OlderThanXHours";
functionParameterType = typeof(long);
break;
case @operator.olderthanxminutes:
function = "OlderThanXMinutes";
functionParameterType = typeof(long);
break;
case @operator.olderthanxmonths:
function = "OlderThanXMonths";
functionParameterType = typeof(long);
break;
case @operator.olderthanxweeks:
function = "OlderThanXWeeks";
functionParameterType = typeof(long);
break;
case @operator.olderthanxyears:
function = "OlderThanXYears";
functionParameterType = typeof(long);
break;
case @operator.on:
function = "On";
break;
case @operator.onorafter:
function = "OnOrAfter";
break;
case @operator.onorbefore:
function = "OnOrBefore";
break;
case @operator.thisfiscalperiod:
function = "ThisFiscalPeriod";
functionParameters = 0;
break;
case @operator.thisfiscalyear:
function = "ThisFiscalYear";
functionParameters = 0;
break;
case @operator.thismonth:
function = "ThisMonth";
functionParameters = 0;
break;
case @operator.thisweek:
function = "ThisWeek";
functionParameters = 0;
break;
case @operator.thisyear:
function = "ThisYear";
functionParameters = 0;
break;
case @operator.today:
function = "Today";
functionParameters = 0;
break;
case @operator.tomorrow:
function = "Tomorrow";
functionParameters = 0;
break;
case @operator.under:
function = "Under";
break;
case @operator.eqorunder:
function = "UnderOrEqual";
break;
case @operator.yesterday:
function = "Yesterday";
functionParameters = 0;
break;
default:
throw new Exception($"Unsupported OData condition operator '{condition.@operator}'");
}
if (!String.IsNullOrEmpty(function))
{
if (functionParameters == Int32.MaxValue)
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{attrMeta.LogicalName}',PropertyValues=[{String.Join(",", condition.Items.Select(i => FormatValue(functionParameterType, i.Value)))}])";
else if (functionParameters == 0)
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{attrMeta.LogicalName}')";
else if (functionParameters == 1)
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{attrMeta.LogicalName}',PropertyValue={FormatValue(functionParameterType, condition.value)})";
else
return $"Microsoft.Dynamics.CRM.{function}(PropertyName='{attrMeta.LogicalName}',{String.Join(",", condition.Items.Select((i, idx) => $"Property{idx + 1}={FormatValue(functionParameterType, i.Value)}"))})";
}
if (!string.IsNullOrEmpty(condition.value) && !result.Contains("("))
{
var valueType = typeof(string);
switch (attrMeta.AttributeType)
{
case AttributeTypeCode.Money:
case AttributeTypeCode.Decimal:
valueType = typeof(decimal);
break;
case AttributeTypeCode.BigInt:
valueType = typeof(long);
break;
case AttributeTypeCode.Boolean:
valueType = typeof(bool);
break;
case AttributeTypeCode.Double:
valueType = typeof(double);
break;
case AttributeTypeCode.Integer:
case AttributeTypeCode.State:
case AttributeTypeCode.Status:
case AttributeTypeCode.Picklist:
valueType = typeof(int);
break;
case AttributeTypeCode.Uniqueidentifier:
case AttributeTypeCode.Lookup:
case AttributeTypeCode.Customer:
case AttributeTypeCode.Owner:
valueType = typeof(Guid);
break;
case AttributeTypeCode.DateTime:
valueType = typeof(DateTime);
break;
}
result += FormatValue(valueType, condition.value);
}
else if (!string.IsNullOrEmpty(condition.valueof))
{
result += condition.valueof;
}
}
return result;
}
private static string GetPropertyName(AttributeMetadata attr)
{
if (attr is LookupAttributeMetadata)
return $"_{attr.LogicalName}_value";
return attr.LogicalName;
}
private static string FormatValue(Type type, string s)
{
if (type == typeof(string))
return "'" + s.Replace("'", "''") + "'";
if (type == typeof(DateTime))
{
var date = DateTimeOffset.Parse(s);
var datestr = string.Empty;
if (date.Equals(date.Date))
return date.ToString("yyyy-MM-dd");
else
return date.ToString("u").Replace(' ', 'T');
}
if (type == typeof(bool))
return s == "1" ? "true" : "false";
if (type == typeof(Guid))
return Guid.Parse(s).ToString();
return Convert.ChangeType(s, type).ToString();
}
private static string GetOrder(FetchEntityType entity, FetchXmlBuilder sender)
{
var orderitems = entity.Items
.OfType<FetchOrderType>()
.Where(i => i.attribute != null);
return GetOrder(entity.name, sender, orderitems);
}
private static string GetOrder(string entityName, FetchXmlBuilder sender, IEnumerable<FetchOrderType> orderitems)
{
var results = new List<string>();
foreach (FetchOrderType orderitem in orderitems)
{
if (!String.IsNullOrEmpty(orderitem.alias))
throw new ApplicationException($"OData queries do not support ordering on link entities. Please remove the sort on {orderitem.alias}.{orderitem.attribute}");
var attrMetadata = sender.entities[entityName].Attributes.SingleOrDefault(a => a.LogicalName == orderitem.attribute);
if (attrMetadata == null)
throw new ApplicationException($"No metadata for attribute {entityName}.{orderitem.attribute}");
var result = GetPropertyName(attrMetadata);
if (orderitem.descending)
{
result += " desc";
}
results.Add(result);
}
return String.Join(",", results);
}
private static string GetAggregate(FetchEntityType entity, FetchXmlBuilder sender)
{
var groups = entity.Items.OfType<FetchAttributeType>()
.Where(a => a.groupbySpecified)
.Select(a => a.name)
.ToList();
var aggregates = entity.Items.OfType<FetchAttributeType>()
.Where(a => a.aggregateSpecified && a.aggregate != AggregateType.count)
.Select(a => $"{a.name} with {GetAggregateType(a.aggregate)} as {a.alias}")
.ToList();
aggregates.AddRange(entity.Items.OfType<FetchAttributeType>()
.Where(a => a.aggregateSpecified && a.aggregate == AggregateType.count)
.Select(a => $"$count as {a.alias}"));
var aggregateText = "aggregate(" + String.Join(",", aggregates) + ")";
if (groups.Count > 0)
{
var result = "groupby((" + String.Join(",", groups) + ")";
if (aggregates.Count > 0)
result += "," + aggregateText;
result += ")";
return result;
}
return aggregateText;
}
private static string GetAggregateType(AggregateType aggregate)
{
switch (aggregate)
{
case AggregateType.avg:
return "average";
case AggregateType.countcolumn:
return "countdistinct";
case AggregateType.max:
case AggregateType.min:
case AggregateType.sum:
return aggregate.ToString();
}
throw new ApplicationException("Unknown aggregate type " + aggregate);
}
private static string LogicalToCollectionName(string entity, FetchXmlBuilder sender)
{
GetEntityMetadata(entity, sender);
var entityMeta = sender.entities[entity];
return entityMeta.LogicalCollectionName;
}
private static void GetEntityMetadata(string entity, FetchXmlBuilder sender)
{
if (sender.NeedToLoadEntity(entity))
{
sender.LoadEntityDetails(entity, null, false);
}
if (!sender.entities.ContainsKey(entity))
{
throw new Exception($"No metadata for entity: {entity}");
}
}
private static string LinkItemToNavigationProperty(string entityname, FetchLinkEntityType linkitem, FetchXmlBuilder sender, out bool child)
{
GetEntityMetadata(entityname, sender);
var entity = sender.entities[entityname];
foreach (var relation in entity.OneToManyRelationships
.Where(r =>
r.ReferencedEntity == entityname &&
r.ReferencedAttribute == linkitem.to &&
r.ReferencingEntity == linkitem.name &&
r.ReferencingAttribute == linkitem.from))
{
if (linkitem.linktype != "outer")
throw new ApplicationException($"OData queries do not support inner joins on 1:N relationships. Try changing link to {linkitem.name} to an outer join");
child = true;
return relation.ReferencedEntityNavigationPropertyName;
}
foreach (var relation in entity.ManyToOneRelationships
.Where(r =>
r.ReferencingEntity == entityname &&
r.ReferencingAttribute == linkitem.to &&
r.ReferencedEntity == linkitem.name &&
r.ReferencedAttribute == linkitem.from))
{
// OData $expand is equivalent to an outer join. Replicate inner join behaviour by adding a not-null filter on primary key
// of related record type
if (linkitem.linktype != "outer")
{
if (linkitem.Items == null)
{
linkitem.Items = new object[0];
}
var filter = linkitem.Items.OfType<filter>().FirstOrDefault(f => f.type == filterType.and);
if (filter == null)
{
filter = new filter
{
type = filterType.and,
Items = new object[0]
};
var items = new List<object>(linkitem.Items);
items.Add(filter);
linkitem.Items = items.ToArray();
}
GetEntityMetadata(linkitem.name, sender);
var linkedEntity = sender.entities[linkitem.name];
var condition = filter.Items.OfType<condition>()
.FirstOrDefault(c => c.attribute == linkedEntity.PrimaryIdAttribute && c.@operator == @operator.notnull);
if (condition == null)
{
condition = new condition
{
attribute = linkedEntity.PrimaryIdAttribute,
@operator = @operator.notnull
};
var items = new List<object>(filter.Items);
items.Add(condition);
filter.Items = items.ToArray();
}
}
child = false;
return relation.ReferencingEntityNavigationPropertyName;
}
foreach (var relation in entity.ManyToManyRelationships
.Where(r =>
r.Entity1LogicalName == entityname &&
r.Entity1IntersectAttribute == linkitem.from))
{
var linkitems = linkitem.Items.Where(i => i is FetchLinkEntityType).ToList();
if (linkitems.Count > 1)
{
throw new Exception("Invalid M:M-relation definition for OData");
}
if (linkitems.Count == 1)
{
var nextlink = (FetchLinkEntityType)linkitems[0];
if (nextlink.linktype != "outer")
{
throw new Exception($"OData queries do not support inner joins on N:N relationships. Try changing link to {nextlink.name} to an outer join");
}
if (relation.Entity2LogicalName == nextlink.name &&
relation.Entity2IntersectAttribute == nextlink.to)
{
child = true;
return relation.Entity1NavigationPropertyName;
}
}
}
foreach (var relation in entity.ManyToManyRelationships
.Where(r =>
r.Entity2LogicalName == entityname &&
r.Entity2IntersectAttribute == linkitem.from))
{
var linkitems = linkitem.Items.Where(i => i is FetchLinkEntityType).ToList();
if (linkitems.Count > 1)
{
throw new Exception("Invalid M:M-relation definition for OData");
}
if (linkitems.Count == 1)
{
var nextlink = (FetchLinkEntityType)linkitems[0];
if (nextlink.linktype != "outer")
{
throw new Exception($"OData queries do not support inner joins on N:N relationships. Try changing link to {nextlink.name} to an outer join");
}
if (relation.Entity1LogicalName == nextlink.name &&
relation.Entity1IntersectAttribute == nextlink.from)
{
child = true;
return relation.Entity2NavigationPropertyName;
}
}
}
throw new Exception($"Cannot find metadata for relation {entityname}.{linkitem.to} => {linkitem.name}.{linkitem.from}");
}
}
}