|
| 1 | +using Microsoft.Xrm.Sdk; |
| 2 | +using Microsoft.Xrm.Sdk.Extensions; |
| 3 | +using Microsoft.Xrm.Sdk.Data.Mappings; |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.Xrm.Sdk.Query; |
| 9 | +using Microsoft.Xrm.Sdk.Data.Extensions; |
| 10 | +using Microsoft.Xrm.Sdk.Messages; |
| 11 | + |
| 12 | +namespace LoopbackDataProvider |
| 13 | +{ |
| 14 | + sealed class Mapping |
| 15 | + { |
| 16 | + internal static Mapping Create(IOrganizationService service, string primaryEntityName) |
| 17 | + { |
| 18 | + var typeMapFactory = new DefaultTypeMapFactory(); |
| 19 | + var queryMapFactory = new QueryMapFactory(service, typeMapFactory); |
| 20 | + return new(queryMapFactory); |
| 21 | + } |
| 22 | + |
| 23 | + readonly QueryMapFactory queryMapFactory; |
| 24 | + |
| 25 | + Mapping(QueryMapFactory queryMapFactory) |
| 26 | + { |
| 27 | + this.queryMapFactory = queryMapFactory; |
| 28 | + } |
| 29 | + |
| 30 | + internal IEnumerable<KeyValuePair<string, object>> ConvertSchema(IEnumerable<KeyValuePair<string, object>> kvps) |
| 31 | + => kvps.Select(ip => new KeyValuePair<string, object>(ip.Key, ConvertSchema(ip.Value))); |
| 32 | + |
| 33 | + object ConvertSchema(object obj) |
| 34 | + => obj switch |
| 35 | + { |
| 36 | + EntityReference entityReference |
| 37 | + => ConvertSchema(entityReference), |
| 38 | + |
| 39 | + Entity entity |
| 40 | + => ConvertSchema(entity), |
| 41 | + |
| 42 | + EntityCollection entityCollection |
| 43 | + => ConvertSchema(entityCollection), |
| 44 | + |
| 45 | + AttributeCollection attributeCollection |
| 46 | + => ConvertSchema(attributeCollection), |
| 47 | + |
| 48 | + QueryExpression queryExpression |
| 49 | + => ConvertSchema(queryExpression), |
| 50 | + |
| 51 | + _ => obj, |
| 52 | + }; |
| 53 | + |
| 54 | + EntityCollection ConvertSchema(EntityCollection entityCollection) |
| 55 | + { |
| 56 | + throw new NotImplementedException(); |
| 57 | + //var retval = queryExpression.ConvertSchema(queryMap); |
| 58 | + // return retval; |
| 59 | + } |
| 60 | + |
| 61 | + QueryExpression ConvertSchema(QueryExpression queryExpression) |
| 62 | + { |
| 63 | + var queryMap = queryMapFactory.Create(queryExpression); |
| 64 | + var retval = queryExpression.ConvertSchema(queryMap); |
| 65 | + return retval; |
| 66 | + } |
| 67 | + |
| 68 | + object ConvertSchema(object obj, string logicalName, string newLogicalName) |
| 69 | + => obj switch |
| 70 | + { |
| 71 | + EntityReference entityReference when entityReference.LogicalName == logicalName |
| 72 | + => ReplaceEntityLogicalNames(entityReference, logicalName, newLogicalName), |
| 73 | + |
| 74 | + Entity entity when entity.LogicalName == logicalName |
| 75 | + => ReplaceEntityLogicalNames(entity, logicalName, newLogicalName), |
| 76 | + |
| 77 | + EntityCollection entityCollection when entityCollection.EntityName == logicalName |
| 78 | + => ReplaceEntityLogicalNames(entityCollection, logicalName, newLogicalName), |
| 79 | + |
| 80 | + AttributeCollection attributeCollection |
| 81 | + => ReplaceEntityLogicalNames(attributeCollection, logicalName, newLogicalName), |
| 82 | + |
| 83 | + QueryExpression queryExpression |
| 84 | + => ReplaceEntityLogicalName(queryExpression, logicalName, newLogicalName), |
| 85 | + |
| 86 | + _ => obj, |
| 87 | + }; |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + static QueryExpression ReplaceEntityLogicalName(QueryExpression queryExpression, string logicalName, string newLogicalName) |
| 92 | + { |
| 93 | + var retval = new QueryExpression() |
| 94 | + { |
| 95 | + ColumnSet = queryExpression.ColumnSet, |
| 96 | + Criteria = queryExpression.Criteria, |
| 97 | + EntityName = newLogicalName, |
| 98 | + ExtensionData = queryExpression.ExtensionData, |
| 99 | + NoLock = queryExpression.NoLock, |
| 100 | + PageInfo = queryExpression.PageInfo, |
| 101 | + QueryHints = queryExpression.QueryHints, |
| 102 | + SubQueryExpression = queryExpression.SubQueryExpression, |
| 103 | + Distinct = queryExpression.Distinct, |
| 104 | + TopCount = queryExpression.TopCount, |
| 105 | + }; |
| 106 | + |
| 107 | + retval.LinkEntities.AddRange(queryExpression.LinkEntities); |
| 108 | + retval.Orders.AddRange(queryExpression.Orders); |
| 109 | + |
| 110 | + return retval; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + public static EntityReference ReplaceEntityLogicalNames(EntityReference entityReference, string logicalName, string newLogicalName) |
| 115 | + => new() |
| 116 | + { |
| 117 | + ExtensionData = entityReference.ExtensionData, |
| 118 | + Id = entityReference.Id, |
| 119 | + KeyAttributes = entityReference.KeyAttributes, |
| 120 | + LogicalName = newLogicalName, |
| 121 | + Name = entityReference.Name, |
| 122 | + RowVersion = entityReference.RowVersion, |
| 123 | + }; |
| 124 | + |
| 125 | + static Entity ReplaceEntityLogicalNames(Entity entityReference, string logicalName, string newLogicalName) |
| 126 | + { |
| 127 | + var retval = new Entity() |
| 128 | + { |
| 129 | + ExtensionData = entityReference.ExtensionData, |
| 130 | + Id = entityReference.Id, |
| 131 | + KeyAttributes = entityReference.KeyAttributes, |
| 132 | + LogicalName = newLogicalName, |
| 133 | + Attributes = ReplaceEntityLogicalNames(entityReference.Attributes, logicalName, newLogicalName), |
| 134 | + RowVersion = entityReference.RowVersion, |
| 135 | + EntityState = entityReference.EntityState, |
| 136 | + HasLazyFileAttribute = entityReference.HasLazyFileAttribute, |
| 137 | + LazyFileAttributeKey = entityReference.LazyFileAttributeKey, |
| 138 | + LazyFileAttributeValue = entityReference.LazyFileAttributeValue, |
| 139 | + LazyFileSizeAttributeKey = entityReference.LazyFileSizeAttributeKey, |
| 140 | + LazyFileSizeAttributeValue = entityReference.LazyFileSizeAttributeValue, |
| 141 | + }; |
| 142 | + |
| 143 | + retval.RelatedEntities.AddRange(ReplaceEntityLogicalNames(retval.RelatedEntities, logicalName, newLogicalName)); |
| 144 | + retval.FormattedValues.AddRange(retval.FormattedValues); |
| 145 | + return retval; |
| 146 | + } |
| 147 | + |
| 148 | + static IEnumerable<Entity> ReplaceEntityLogicalNames(IEnumerable<Entity> entities, string logicalName, string newLogicalName) |
| 149 | + => entities.Select(e => ReplaceEntityLogicalNames(e, logicalName, newLogicalName)); |
| 150 | + |
| 151 | + static IEnumerable<KeyValuePair<Relationship, EntityCollection>> ReplaceEntityLogicalNames(RelatedEntityCollection kvps, string logicalName, string newLogicalName) |
| 152 | + => kvps.Select<KeyValuePair<Relationship, EntityCollection>, KeyValuePair<Relationship, EntityCollection>>( |
| 153 | + _ => throw new NotImplementedException(nameof(RelatedEntityCollection))); |
| 154 | + |
| 155 | + static AttributeCollection ReplaceEntityLogicalNames(AttributeCollection attributeCollection, string logicalName, string newLogicalName) |
| 156 | + { |
| 157 | + throw new NotImplementedException(); |
| 158 | + var retval = new AttributeCollection(); |
| 159 | + // retval.AddRange( |
| 160 | + // ReplaceEntityLogicalNames(attributeCollection.AsEnumerable(), logicalName, newLogicalName)); |
| 161 | + return retval; |
| 162 | + } |
| 163 | + |
| 164 | + private static EntityCollection ReplaceEntityLogicalNames(EntityCollection entityCollection, string logicalName, string newLogicalName) |
| 165 | + { |
| 166 | + var retval = new EntityCollection() |
| 167 | + { |
| 168 | + EntityName = newLogicalName, |
| 169 | + ExtensionData = entityCollection.ExtensionData, |
| 170 | + MinActiveRowVersion = entityCollection.MinActiveRowVersion, |
| 171 | + MoreRecords = entityCollection.MoreRecords, |
| 172 | + PagingCookie = entityCollection.PagingCookie, |
| 173 | + TotalRecordCount = entityCollection.TotalRecordCount, |
| 174 | + TotalRecordCountLimitExceeded = entityCollection.TotalRecordCountLimitExceeded, |
| 175 | + }; |
| 176 | + |
| 177 | + retval.Entities.AddRange(ReplaceEntityLogicalNames(entityCollection.Entities.AsEnumerable(), logicalName, newLogicalName)); |
| 178 | + return retval; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments