-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueConversion.cs
166 lines (134 loc) · 6.95 KB
/
ValueConversion.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
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Extensions;
using Microsoft.Xrm.Sdk.Data.Mappings;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Data.Extensions;
using Microsoft.Xrm.Sdk.Messages;
namespace LoopbackDataProvider
{
/// <summary>
/// WIP
/// </summary>
class ValueConversion
{
internal IEnumerable<KeyValuePair<string, object>> ConvertSchema(IEnumerable<KeyValuePair<string, object>> kvps)
=> kvps.Select(ip => new KeyValuePair<string, object>(ip.Key, ConvertSchema(ip.Value)));
object ConvertSchema(object obj)
=> obj switch
{
EntityReference entityReference
=> ConvertSchema(entityReference),
Entity entity
=> ConvertSchema(entity),
EntityCollection entityCollection
=> ConvertSchema(entityCollection),
AttributeCollection attributeCollection
=> ConvertSchema(attributeCollection),
QueryExpression queryExpression
=> ConvertSchema(queryExpression),
_ => obj,
};
EntityCollection ConvertSchema(EntityCollection entityCollection)
{
throw new NotImplementedException();
//var retval = queryExpression.ConvertSchema(queryMap);
// return retval;
}
object ConvertSchema(object obj, string logicalName, string newLogicalName)
=> obj switch
{
EntityReference entityReference when entityReference.LogicalName == logicalName
=> ReplaceEntityLogicalNames(entityReference, logicalName, newLogicalName),
Entity entity when entity.LogicalName == logicalName
=> ReplaceEntityLogicalNames(entity, logicalName, newLogicalName),
EntityCollection entityCollection when entityCollection.EntityName == logicalName
=> ReplaceEntityLogicalNames(entityCollection, logicalName, newLogicalName),
AttributeCollection attributeCollection
=> ReplaceEntityLogicalNames(attributeCollection, logicalName, newLogicalName),
QueryExpression queryExpression
=> ReplaceEntityLogicalName(queryExpression, logicalName, newLogicalName),
_ => obj,
};
static QueryExpression ReplaceEntityLogicalName(QueryExpression queryExpression, string logicalName, string newLogicalName)
{
var retval = new QueryExpression()
{
ColumnSet = queryExpression.ColumnSet,
Criteria = queryExpression.Criteria,
EntityName = newLogicalName,
ExtensionData = queryExpression.ExtensionData,
NoLock = queryExpression.NoLock,
PageInfo = queryExpression.PageInfo,
QueryHints = queryExpression.QueryHints,
SubQueryExpression = queryExpression.SubQueryExpression,
Distinct = queryExpression.Distinct,
TopCount = queryExpression.TopCount,
};
retval.LinkEntities.AddRange(queryExpression.LinkEntities);
retval.Orders.AddRange(queryExpression.Orders);
return retval;
}
public static EntityReference ReplaceEntityLogicalNames(EntityReference entityReference, string logicalName, string newLogicalName)
=> new()
{
ExtensionData = entityReference.ExtensionData,
Id = entityReference.Id,
KeyAttributes = entityReference.KeyAttributes,
LogicalName = newLogicalName,
Name = entityReference.Name,
RowVersion = entityReference.RowVersion,
};
static Entity ReplaceEntityLogicalNames(Entity entityReference, string logicalName, string newLogicalName)
{
var retval = new Entity()
{
ExtensionData = entityReference.ExtensionData,
Id = entityReference.Id,
KeyAttributes = entityReference.KeyAttributes,
LogicalName = newLogicalName,
Attributes = ReplaceEntityLogicalNames(entityReference.Attributes, logicalName, newLogicalName),
RowVersion = entityReference.RowVersion,
EntityState = entityReference.EntityState,
HasLazyFileAttribute = entityReference.HasLazyFileAttribute,
LazyFileAttributeKey = entityReference.LazyFileAttributeKey,
LazyFileAttributeValue = entityReference.LazyFileAttributeValue,
LazyFileSizeAttributeKey = entityReference.LazyFileSizeAttributeKey,
LazyFileSizeAttributeValue = entityReference.LazyFileSizeAttributeValue,
};
retval.RelatedEntities.AddRange(ReplaceEntityLogicalNames(retval.RelatedEntities, logicalName, newLogicalName));
retval.FormattedValues.AddRange(retval.FormattedValues);
return retval;
}
static IEnumerable<Entity> ReplaceEntityLogicalNames(IEnumerable<Entity> entities, string logicalName, string newLogicalName)
=> entities.Select(e => ReplaceEntityLogicalNames(e, logicalName, newLogicalName));
static IEnumerable<KeyValuePair<Relationship, EntityCollection>> ReplaceEntityLogicalNames(RelatedEntityCollection kvps, string logicalName, string newLogicalName)
=> kvps.Select<KeyValuePair<Relationship, EntityCollection>, KeyValuePair<Relationship, EntityCollection>>(
_ => throw new NotImplementedException(nameof(RelatedEntityCollection)));
static AttributeCollection ReplaceEntityLogicalNames(AttributeCollection attributeCollection, string logicalName, string newLogicalName)
{
throw new NotImplementedException();
var retval = new AttributeCollection();
// retval.AddRange(
// ReplaceEntityLogicalNames(attributeCollection.AsEnumerable(), logicalName, newLogicalName));
return retval;
}
private static EntityCollection ReplaceEntityLogicalNames(EntityCollection entityCollection, string logicalName, string newLogicalName)
{
var retval = new EntityCollection()
{
EntityName = newLogicalName,
ExtensionData = entityCollection.ExtensionData,
MinActiveRowVersion = entityCollection.MinActiveRowVersion,
MoreRecords = entityCollection.MoreRecords,
PagingCookie = entityCollection.PagingCookie,
TotalRecordCount = entityCollection.TotalRecordCount,
TotalRecordCountLimitExceeded = entityCollection.TotalRecordCountLimitExceeded,
};
retval.Entities.AddRange(ReplaceEntityLogicalNames(entityCollection.Entities.AsEnumerable(), logicalName, newLogicalName));
return retval;
}
}
}