| | 1 | | using FakeXrmEasy.Metadata; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using Microsoft.Xrm.Sdk.Query; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | |
|
| | 8 | | namespace FakeXrmEasy.Extensions |
| | 9 | | { |
| | 10 | | public static class EntityExtensions |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Extension method to add an attribute and return the entity itself |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="e"></param> |
| | 16 | | /// <param name="key"></param> |
| | 17 | | /// <param name="value"></param> |
| | 18 | | /// <returns></returns> |
| | 19 | | public static Entity AddAttribute(this Entity e, string key, object value) |
| 0 | 20 | | { |
| 0 | 21 | | e.Attributes.Add(key, value); |
| 0 | 22 | | return e; |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Projects the attributes of entity e so that only the attributes specified in the columnSet are returned |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="e"></param> |
| | 29 | | /// <param name="columnSet"></param> |
| | 30 | | /// <param name="alias"></param> |
| | 31 | | /// <returns></returns> |
| | 32 | | public static Entity ProjectAttributes(this Entity e, ColumnSet columnSet, XrmFakedContext context) |
| 152 | 33 | | { |
| 152 | 34 | | return ProjectAttributes(e, new QueryExpression() { ColumnSet = columnSet }, context); |
| 152 | 35 | | } |
| | 36 | |
|
| | 37 | | public static void ApplyDateBehaviour(this Entity e, XrmFakedContext context) |
| 16458 | 38 | | { |
| | 39 | | #if FAKE_XRM_EASY || FAKE_XRM_EASY_2013 |
| 5440 | 40 | | return; //Do nothing... DateBehavior wasn't available for versions <= 2013 |
| | 41 | | #else |
| | 42 | |
|
| 11018 | 43 | | if (context.DateBehaviour.Count == 0 || e.LogicalName == null || !context.DateBehaviour.ContainsKey(e.Logica |
| 9636 | 44 | | { |
| 9636 | 45 | | return; |
| | 46 | | } |
| | 47 | |
|
| 1382 | 48 | | var entityDateBehaviours = context.DateBehaviour[e.LogicalName]; |
| 9178 | 49 | | foreach (var attribute in entityDateBehaviours.Keys) |
| 2516 | 50 | | { |
| 2516 | 51 | | if (!e.Attributes.ContainsKey(attribute)) |
| 2356 | 52 | | { |
| 2356 | 53 | | continue; |
| | 54 | | } |
| | 55 | |
|
| 160 | 56 | | switch (entityDateBehaviours[attribute]) |
| | 57 | | { |
| | 58 | | case DateTimeAttributeBehavior.DateOnly: |
| 152 | 59 | | var currentValue = (DateTime)e[attribute]; |
| 152 | 60 | | e[attribute] = new DateTime(currentValue.Year, currentValue.Month, currentValue.Day, 0, 0, 0, Da |
| 152 | 61 | | break; |
| | 62 | |
|
| | 63 | | default: |
| 8 | 64 | | break; |
| | 65 | | } |
| 160 | 66 | | } |
| | 67 | | #endif |
| 16458 | 68 | | } |
| | 69 | |
|
| | 70 | | public static void ProjectAttributes(Entity e, Entity projected, LinkEntity le, XrmFakedContext context) |
| 13778 | 71 | | { |
| 13778 | 72 | | var sAlias = string.IsNullOrWhiteSpace(le.EntityAlias) ? le.LinkToEntityName : le.EntityAlias; |
| | 73 | |
|
| 13778 | 74 | | if (le.Columns.AllColumns) |
| 12 | 75 | | { |
| 696 | 76 | | foreach (var attKey in e.Attributes.Keys) |
| 330 | 77 | | { |
| 330 | 78 | | if (attKey.StartsWith(sAlias + ".")) |
| 132 | 79 | | { |
| 132 | 80 | | projected[attKey] = e[attKey]; |
| 132 | 81 | | } |
| 330 | 82 | | } |
| | 83 | |
|
| 36 | 84 | | foreach (var attKey in e.FormattedValues.Keys) |
| 0 | 85 | | { |
| 0 | 86 | | if (attKey.StartsWith(sAlias + ".")) |
| 0 | 87 | | { |
| 0 | 88 | | projected.FormattedValues[attKey] = e.FormattedValues[attKey]; |
| 0 | 89 | | } |
| 0 | 90 | | } |
| 12 | 91 | | } |
| | 92 | | else |
| 13766 | 93 | | { |
| 42842 | 94 | | foreach (var attKey in le.Columns.Columns) |
| 772 | 95 | | { |
| 772 | 96 | | var linkedAttKey = sAlias + "." + attKey; |
| 772 | 97 | | if (e.Attributes.ContainsKey(linkedAttKey)) |
| 712 | 98 | | projected[linkedAttKey] = e[linkedAttKey]; |
| | 99 | |
|
| 772 | 100 | | if (e.FormattedValues.ContainsKey(linkedAttKey)) |
| 12 | 101 | | projected.FormattedValues[linkedAttKey] = e.FormattedValues[linkedAttKey]; |
| 772 | 102 | | } |
| | 103 | |
|
| 13766 | 104 | | } |
| | 105 | |
|
| 41466 | 106 | | foreach (var nestedLinkedEntity in le.LinkEntities) |
| 66 | 107 | | { |
| 66 | 108 | | ProjectAttributes(e, projected, nestedLinkedEntity, context); |
| 66 | 109 | | } |
| 13778 | 110 | | } |
| | 111 | |
|
| | 112 | | public static Entity ProjectAttributes(this Entity e, QueryExpression qe, XrmFakedContext context) |
| 31180 | 113 | | { |
| 31180 | 114 | | if (qe.ColumnSet == null || qe.ColumnSet.AllColumns) |
| 1729 | 115 | | { |
| 1729 | 116 | | return RemoveNullAttributes(e); //return all the original attributes |
| | 117 | | } |
| | 118 | | else |
| 29451 | 119 | | { |
| | 120 | | //Return selected list of attributes in a projected entity |
| 29451 | 121 | | Entity projected = null; |
| | 122 | |
|
| | 123 | | //However, if we are using proxy types, we must create a instance of the appropiate class |
| 29451 | 124 | | if (context.ProxyTypesAssembly != null) |
| 2353 | 125 | | { |
| 2353 | 126 | | var subClassType = context.FindReflectedType(e.LogicalName); |
| 2353 | 127 | | if (subClassType != null) |
| 2353 | 128 | | { |
| 2353 | 129 | | var instance = Activator.CreateInstance(subClassType); |
| 2353 | 130 | | projected = (Entity)instance; |
| 2353 | 131 | | projected.Id = e.Id; |
| 2353 | 132 | | } |
| | 133 | | else |
| 0 | 134 | | projected = new Entity(e.LogicalName) { Id = e.Id }; //fallback to generic type if type not foun |
| 2353 | 135 | | } |
| | 136 | | else |
| 27098 | 137 | | projected = new Entity(e.LogicalName) { Id = e.Id }; |
| | 138 | |
|
| | 139 | |
|
| 97923 | 140 | | foreach (var attKey in qe.ColumnSet.Columns) |
| 4791 | 141 | | { |
| | 142 | | //Check if attribute really exists in metadata |
| 4791 | 143 | | if (!context.AttributeExistsInMetadata(e.LogicalName, attKey)) |
| 12 | 144 | | { |
| 12 | 145 | | FakeOrganizationServiceFault.Throw(ErrorCodes.QueryBuilderNoAttribute, string.Format("The attrib |
| 0 | 146 | | } |
| | 147 | |
|
| 4779 | 148 | | if (e.Attributes.ContainsKey(attKey) && e.Attributes[attKey] != null) |
| 2835 | 149 | | { |
| 2835 | 150 | | projected[attKey] = CloneAttribute(e[attKey], context); |
| | 151 | |
|
| 2835 | 152 | | string formattedValue = ""; |
| | 153 | |
|
| 2835 | 154 | | if (e.FormattedValues.TryGetValue(attKey, out formattedValue)) |
| 6 | 155 | | { |
| 6 | 156 | | projected.FormattedValues[attKey] = formattedValue; |
| 6 | 157 | | } |
| 2835 | 158 | | } |
| 4779 | 159 | | } |
| | 160 | |
|
| | 161 | |
|
| | 162 | | //Plus attributes from joins |
| 115741 | 163 | | foreach (var le in qe.LinkEntities) |
| 13712 | 164 | | { |
| 13712 | 165 | | ProjectAttributes(RemoveNullAttributes(e), projected, le, context); |
| 13712 | 166 | | } |
| 29439 | 167 | | return RemoveNullAttributes(projected); |
| | 168 | | } |
| 31168 | 169 | | } |
| | 170 | |
|
| | 171 | | public static Entity RemoveNullAttributes(Entity entity) |
| 44880 | 172 | | { |
| 44880 | 173 | | IList<string> nullAttributes = entity.Attributes |
| 294987 | 174 | | .Where(attribute => attribute.Value == null || |
| 294987 | 175 | | (attribute.Value is AliasedValue && (attribute.Value as AliasedValue).Value == null)) |
| 45067 | 176 | | .Select(attribute => attribute.Key).ToList(); |
| 135014 | 177 | | foreach (var nullAttribute in nullAttributes) |
| 187 | 178 | | { |
| 187 | 179 | | entity.Attributes.Remove(nullAttribute); |
| 187 | 180 | | } |
| 44880 | 181 | | return entity; |
| 44880 | 182 | | } |
| | 183 | |
|
| | 184 | | public static object CloneAttribute(object attributeValue, XrmFakedContext context = null) |
| 1988945 | 185 | | { |
| 1988945 | 186 | | if (attributeValue == null) |
| 555892 | 187 | | return null; |
| | 188 | |
|
| 1433053 | 189 | | var type = attributeValue.GetType(); |
| 1433053 | 190 | | if (type == typeof(string)) |
| 20708 | 191 | | return new string((attributeValue as string).ToCharArray()); |
| 1412345 | 192 | | else if (type == typeof(EntityReference) |
| 1412345 | 193 | | #if FAKE_XRM_EASY |
| 1412345 | 194 | | || type == typeof(Microsoft.Xrm.Client.CrmEntityReference) |
| 1412345 | 195 | | #endif |
| 1412345 | 196 | | ) |
| 556072 | 197 | | { |
| 556072 | 198 | | var original = (attributeValue as EntityReference); |
| 556072 | 199 | | var clone = new EntityReference(original.LogicalName, original.Id); |
| | 200 | |
|
| 556072 | 201 | | if (context != null && !string.IsNullOrEmpty(original.LogicalName) && context.EntityMetadata.ContainsKey |
| 556072 | 202 | | context.Data.ContainsKey(original.LogicalName) && context.Data[original.LogicalName].ContainsKey(ori |
| 54 | 203 | | { |
| 54 | 204 | | clone.Name = context.Data[original.LogicalName][original.Id].GetAttributeValue<string>(context.Entit |
| 54 | 205 | | } |
| | 206 | | else |
| 556018 | 207 | | { |
| 556018 | 208 | | clone.Name = CloneAttribute(original.Name) as string; |
| 556018 | 209 | | } |
| | 210 | |
|
| | 211 | | #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015 |
| 278814 | 212 | | if (original.KeyAttributes != null) |
| 278814 | 213 | | { |
| 278814 | 214 | | clone.KeyAttributes = new KeyAttributeCollection(); |
| 278826 | 215 | | clone.KeyAttributes.AddRange(original.KeyAttributes.Select(kvp => new KeyValuePair<string, object>(C |
| 278814 | 216 | | } |
| | 217 | | #endif |
| 556072 | 218 | | return clone; |
| | 219 | | } |
| 856273 | 220 | | else if (type == typeof(BooleanManagedProperty)) |
| 18 | 221 | | { |
| 18 | 222 | | var original = (attributeValue as BooleanManagedProperty); |
| 18 | 223 | | return new BooleanManagedProperty(original.Value); |
| | 224 | | } |
| 856255 | 225 | | else if (type == typeof(OptionSetValue)) |
| 170085 | 226 | | { |
| 170085 | 227 | | var original = (attributeValue as OptionSetValue); |
| 170085 | 228 | | return new OptionSetValue(original.Value); |
| | 229 | | } |
| 686170 | 230 | | else if (type == typeof(AliasedValue)) |
| 121696 | 231 | | { |
| 121696 | 232 | | var original = (attributeValue as AliasedValue); |
| 121696 | 233 | | return new AliasedValue(original.EntityLogicalName, original.AttributeLogicalName, CloneAttribute(origin |
| | 234 | | } |
| 564474 | 235 | | else if (type == typeof(Money)) |
| 1125 | 236 | | { |
| 1125 | 237 | | var original = (attributeValue as Money); |
| 1125 | 238 | | return new Money(original.Value); |
| | 239 | | } |
| 563349 | 240 | | else if (attributeValue.GetType() == typeof(EntityCollection)) |
| 12 | 241 | | { |
| 12 | 242 | | var collection = attributeValue as EntityCollection; |
| 24 | 243 | | return new EntityCollection(collection.Entities.Select(e => e.Clone(e.GetType())).ToList()); |
| | 244 | | } |
| 563337 | 245 | | else if (attributeValue is IEnumerable<Entity>) |
| 6 | 246 | | { |
| 6 | 247 | | var enumerable = attributeValue as IEnumerable<Entity>; |
| 18 | 248 | | return enumerable.Select(e => e.Clone(e.GetType())).ToArray(); |
| | 249 | | } |
| | 250 | | #if !FAKE_XRM_EASY |
| 470085 | 251 | | else if (type == typeof(byte[])) |
| 5 | 252 | | { |
| 5 | 253 | | var original = (attributeValue as byte[]); |
| 5 | 254 | | var copy = new byte[original.Length]; |
| 5 | 255 | | original.CopyTo(copy, 0); |
| 5 | 256 | | return copy; |
| | 257 | | } |
| | 258 | | #endif |
| | 259 | | #if FAKE_XRM_EASY_9 |
| 94841 | 260 | | else if (attributeValue is OptionSetValueCollection) |
| 292 | 261 | | { |
| 292 | 262 | | var original = (attributeValue as OptionSetValueCollection); |
| 292 | 263 | | var copy = new OptionSetValueCollection(original.ToArray()); |
| 292 | 264 | | return copy; |
| | 265 | | } |
| | 266 | | #endif |
| 563034 | 267 | | else if (type == typeof(int) || type == typeof(Int64)) |
| 4773 | 268 | | return attributeValue; //Not a reference type |
| 558261 | 269 | | else if (type == typeof(decimal)) |
| 327 | 270 | | return attributeValue; //Not a reference type |
| 557934 | 271 | | else if (type == typeof(double)) |
| 270 | 272 | | return attributeValue; //Not a reference type |
| 557664 | 273 | | else if (type == typeof(float)) |
| 36 | 274 | | return attributeValue; //Not a reference type |
| 557628 | 275 | | else if (type == typeof(byte)) |
| 0 | 276 | | return attributeValue; //Not a reference type |
| 557628 | 277 | | else if (type == typeof(float)) |
| 0 | 278 | | return attributeValue; //Not a reference type |
| 557628 | 279 | | else if (type == typeof(bool)) |
| 44620 | 280 | | return attributeValue; //Not a reference type |
| 513008 | 281 | | else if (type == typeof(Guid)) |
| 174642 | 282 | | return attributeValue; //Not a reference type |
| 338366 | 283 | | else if (type == typeof(DateTime)) |
| 338282 | 284 | | return attributeValue; //Not a reference type |
| 84 | 285 | | else if (attributeValue is Enum) |
| 84 | 286 | | return attributeValue; //Not a reference type |
| | 287 | |
|
| 0 | 288 | | throw new Exception(string.Format("Attribute type not supported when trying to clone attribute '{0}'", type. |
| 1988945 | 289 | | } |
| | 290 | |
|
| | 291 | | public static Entity Clone(this Entity e, XrmFakedContext context = null) |
| 56112 | 292 | | { |
| 56112 | 293 | | var cloned = new Entity(e.LogicalName); |
| 56112 | 294 | | cloned.Id = e.Id; |
| 56112 | 295 | | cloned.LogicalName = e.LogicalName; |
| | 296 | |
|
| 56112 | 297 | | if (e.FormattedValues != null) |
| 56112 | 298 | | { |
| 56112 | 299 | | var formattedValues = new FormattedValueCollection(); |
| 168360 | 300 | | foreach (var key in e.FormattedValues.Keys) |
| 12 | 301 | | formattedValues.Add(key, e.FormattedValues[key]); |
| | 302 | |
|
| 56112 | 303 | | cloned.Inject("FormattedValues", formattedValues); |
| 56112 | 304 | | } |
| | 305 | |
|
| 1047062 | 306 | | foreach (var attKey in e.Attributes.Keys) |
| 439363 | 307 | | { |
| 439363 | 308 | | cloned[attKey] = e[attKey] != null ? CloneAttribute(e[attKey], context) : null; |
| 439363 | 309 | | } |
| | 310 | | #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015 |
| 84258 | 311 | | foreach (var attKey in e.KeyAttributes.Keys) |
| 0 | 312 | | { |
| 0 | 313 | | cloned.KeyAttributes[attKey] = e.KeyAttributes[attKey] != null ? CloneAttribute(e.KeyAttributes[attKey]) |
| 0 | 314 | | } |
| | 315 | | #endif |
| 56112 | 316 | | return cloned; |
| 56112 | 317 | | } |
| | 318 | |
|
| | 319 | | public static T Clone<T>(this Entity e) where T : Entity |
| 0 | 320 | | { |
| 0 | 321 | | return (T)e.Clone(typeof(T)); |
| 0 | 322 | | } |
| | 323 | |
|
| | 324 | | public static Entity Clone(this Entity e, Type t, XrmFakedContext context = null) |
| 98371 | 325 | | { |
| 98371 | 326 | | if (t == null) |
| 431 | 327 | | return e.Clone(context); |
| | 328 | |
|
| 97940 | 329 | | var cloned = Activator.CreateInstance(t) as Entity; |
| 97940 | 330 | | cloned.Id = e.Id; |
| 97940 | 331 | | cloned.LogicalName = e.LogicalName; |
| | 332 | |
|
| 97940 | 333 | | if (e.FormattedValues != null) |
| 97940 | 334 | | { |
| 97940 | 335 | | var formattedValues = new FormattedValueCollection(); |
| 294048 | 336 | | foreach (var key in e.FormattedValues.Keys) |
| 114 | 337 | | formattedValues.Add(key, e.FormattedValues[key]); |
| | 338 | |
|
| 97940 | 339 | | cloned.Inject("FormattedValues", formattedValues); |
| 97940 | 340 | | } |
| | 341 | |
|
| 2034972 | 342 | | foreach (var attKey in e.Attributes.Keys) |
| 870576 | 343 | | { |
| 870576 | 344 | | cloned[attKey] = e[attKey] != null ? CloneAttribute(e[attKey], context) : null; |
| 870576 | 345 | | } |
| | 346 | |
|
| | 347 | | #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015 |
| 147846 | 348 | | foreach (var attKey in e.KeyAttributes.Keys) |
| 9 | 349 | | { |
| 9 | 350 | | cloned.KeyAttributes[attKey] = e.KeyAttributes[attKey] != null ? CloneAttribute(e.KeyAttributes[attKey]) |
| 9 | 351 | | } |
| | 352 | | #endif |
| 97940 | 353 | | return cloned; |
| 98371 | 354 | | } |
| | 355 | |
|
| | 356 | | /// <summary> |
| | 357 | | /// Extension method to join the attributes of entity e and otherEntity |
| | 358 | | /// </summary> |
| | 359 | | /// <param name="e"></param> |
| | 360 | | /// <param name="otherEntity"></param> |
| | 361 | | /// <param name="attributes"></param> |
| | 362 | | /// <returns></returns> |
| | 363 | | public static Entity JoinAttributes(this Entity e, Entity otherEntity, ColumnSet columnSet, string alias, XrmFak |
| 15019 | 364 | | { |
| 15196 | 365 | | if (otherEntity == null) return e; //Left Join where otherEntity was not matched |
| | 366 | |
|
| 14842 | 367 | | otherEntity = otherEntity.Clone(); //To avoid joining entities from/to the same entities, which would cause |
| | 368 | |
|
| 14842 | 369 | | if (columnSet.AllColumns) |
| 14842 | 370 | | { |
| 286960 | 371 | | foreach (var attKey in otherEntity.Attributes.Keys) |
| 121217 | 372 | | { |
| 121217 | 373 | | e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, otherEntity[attKey]); |
| 121217 | 374 | | } |
| | 375 | |
|
| 44550 | 376 | | foreach (var attKey in otherEntity.FormattedValues.Keys) |
| 12 | 377 | | { |
| 12 | 378 | | e.FormattedValues[alias + "." + attKey] = otherEntity.FormattedValues[attKey]; |
| 12 | 379 | | } |
| 14842 | 380 | | } |
| | 381 | | else |
| 0 | 382 | | { |
| | 383 | | //Return selected list of attributes |
| 0 | 384 | | foreach (var attKey in columnSet.Columns) |
| 0 | 385 | | { |
| 0 | 386 | | if (!context.AttributeExistsInMetadata(otherEntity.LogicalName, attKey)) |
| 0 | 387 | | { |
| 0 | 388 | | FakeOrganizationServiceFault.Throw(ErrorCodes.QueryBuilderNoAttribute, string.Format("The attrib |
| 0 | 389 | | } |
| | 390 | |
|
| 0 | 391 | | if (otherEntity.Attributes.ContainsKey(attKey)) |
| 0 | 392 | | { |
| 0 | 393 | | e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, otherEntity[attKey]) |
| 0 | 394 | | } |
| | 395 | | else |
| 0 | 396 | | { |
| 0 | 397 | | e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, null); |
| 0 | 398 | | } |
| | 399 | |
|
| 0 | 400 | | if (otherEntity.FormattedValues.ContainsKey(attKey)) |
| 0 | 401 | | { |
| 0 | 402 | | e.FormattedValues[alias + "." + attKey] = otherEntity.FormattedValues[attKey]; |
| 0 | 403 | | } |
| 0 | 404 | | } |
| 0 | 405 | | } |
| 14842 | 406 | | return e; |
| 15019 | 407 | | } |
| | 408 | |
|
| | 409 | | public static Entity JoinAttributes(this Entity e, IEnumerable<Entity> otherEntities, ColumnSet columnSet, strin |
| 0 | 410 | | { |
| 0 | 411 | | foreach (var otherEntity in otherEntities) |
| 0 | 412 | | { |
| 0 | 413 | | var otherClonedEntity = otherEntity.Clone(); //To avoid joining entities from/to the same entities, whic |
| | 414 | |
|
| 0 | 415 | | if (columnSet.AllColumns) |
| 0 | 416 | | { |
| 0 | 417 | | foreach (var attKey in otherClonedEntity.Attributes.Keys) |
| 0 | 418 | | { |
| 0 | 419 | | e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, otherClonedEntity[at |
| 0 | 420 | | } |
| | 421 | |
|
| 0 | 422 | | foreach (var attKey in otherEntity.FormattedValues.Keys) |
| 0 | 423 | | { |
| 0 | 424 | | e.FormattedValues[alias + "." + attKey] = otherEntity.FormattedValues[attKey]; |
| 0 | 425 | | } |
| 0 | 426 | | } |
| | 427 | | else |
| 0 | 428 | | { |
| | 429 | | //Return selected list of attributes |
| 0 | 430 | | foreach (var attKey in columnSet.Columns) |
| 0 | 431 | | { |
| 0 | 432 | | if (!context.AttributeExistsInMetadata(otherEntity.LogicalName, attKey)) |
| 0 | 433 | | { |
| 0 | 434 | | FakeOrganizationServiceFault.Throw(ErrorCodes.QueryBuilderNoAttribute, string.Format("The at |
| 0 | 435 | | } |
| | 436 | |
|
| 0 | 437 | | if (otherClonedEntity.Attributes.ContainsKey(attKey)) |
| 0 | 438 | | { |
| 0 | 439 | | e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, otherClonedEntit |
| 0 | 440 | | } |
| | 441 | | else |
| 0 | 442 | | { |
| 0 | 443 | | e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, null); |
| 0 | 444 | | } |
| | 445 | |
|
| 0 | 446 | | if (otherEntity.FormattedValues.ContainsKey(attKey)) |
| 0 | 447 | | { |
| 0 | 448 | | e.FormattedValues[alias + "." + attKey] = otherEntity.FormattedValues[attKey]; |
| 0 | 449 | | } |
| 0 | 450 | | } |
| 0 | 451 | | } |
| 0 | 452 | | } |
| 0 | 453 | | return e; |
| 0 | 454 | | } |
| | 455 | |
|
| | 456 | | /// <summary> |
| | 457 | | /// Returns the key for the attribute name selected (could an entity reference or a primary key or a guid) |
| | 458 | | /// </summary> |
| | 459 | | /// <param name="e"></param> |
| | 460 | | /// <param name="sAttributeName"></param> |
| | 461 | | /// <returns></returns> |
| | 462 | | public static object KeySelector(this Entity e, string sAttributeName, XrmFakedContext context) |
| 30077 | 463 | | { |
| 30077 | 464 | | if (sAttributeName.Contains(".")) |
| 192 | 465 | | { |
| | 466 | | //Do not lowercase the alias prefix |
| 192 | 467 | | var splitted = sAttributeName.Split('.'); |
| 192 | 468 | | sAttributeName = string.Format("{0}.{1}", splitted[0], splitted[1].ToLower()); |
| 192 | 469 | | } |
| | 470 | | else |
| 29885 | 471 | | { |
| 29885 | 472 | | sAttributeName = sAttributeName.ToLower(); |
| 29885 | 473 | | } |
| | 474 | |
|
| 30077 | 475 | | if (!e.Attributes.ContainsKey(sAttributeName)) |
| 174 | 476 | | { |
| | 477 | | //Check if it is the primary key |
| 174 | 478 | | if (sAttributeName.Contains("id") && |
| 174 | 479 | | e.LogicalName.ToLower().Equals(sAttributeName.Substring(0, sAttributeName.Length - 2))) |
| 0 | 480 | | { |
| 0 | 481 | | return e.Id; |
| | 482 | | } |
| 174 | 483 | | return Guid.Empty; //Atrribute is null or doesn´t exists so it can´t be joined |
| | 484 | | } |
| | 485 | |
|
| 29903 | 486 | | object keyValue = null; |
| | 487 | | AliasedValue aliasedValue; |
| 29903 | 488 | | if ((aliasedValue = e[sAttributeName] as AliasedValue) != null) |
| 192 | 489 | | { |
| 192 | 490 | | keyValue = aliasedValue.Value; |
| 192 | 491 | | } |
| | 492 | | else |
| 29711 | 493 | | { |
| 29711 | 494 | | keyValue = e[sAttributeName]; |
| 29711 | 495 | | } |
| | 496 | |
|
| 29903 | 497 | | EntityReference entityReference = keyValue as EntityReference; |
| 29903 | 498 | | if (entityReference != null) |
| 14494 | 499 | | return entityReference.Id; |
| | 500 | |
|
| 15409 | 501 | | OptionSetValue optionSetValue = keyValue as OptionSetValue; |
| 15409 | 502 | | if (optionSetValue != null) |
| 0 | 503 | | return optionSetValue.Value; |
| | 504 | |
|
| 15409 | 505 | | Money money = keyValue as Money; |
| 15409 | 506 | | if (money != null) |
| 0 | 507 | | return money.Value; |
| | 508 | |
|
| 15409 | 509 | | return keyValue; |
| 30077 | 510 | | } |
| | 511 | |
|
| | 512 | | /// <summary> |
| | 513 | | /// Extension method to "hack" internal set properties on sealed classes via reflection |
| | 514 | | /// </summary> |
| | 515 | | /// <param name="e"></param> |
| | 516 | | /// <param name="property"></param> |
| | 517 | | /// <param name="value"></param> |
| | 518 | | public static void Inject(this Entity e, string property, object value) |
| 154088 | 519 | | { |
| 154088 | 520 | | e.GetType().GetProperty(property).SetValue(e, value, null); |
| 154088 | 521 | | } |
| | 522 | |
|
| | 523 | | public static void SetValueIfEmpty(this Entity e, string property, object value) |
| 170550 | 524 | | { |
| 170550 | 525 | | var containsKey = e.Attributes.ContainsKey(property); |
| 170550 | 526 | | if (!containsKey || containsKey && e[property] == null) |
| 170261 | 527 | | { |
| 170261 | 528 | | e[property] = value; |
| 170261 | 529 | | } |
| 170550 | 530 | | } |
| | 531 | |
|
| | 532 | | /// <summary> |
| | 533 | | /// ToEntityReference implementation which converts an entity into an entity reference with key attribute info a |
| | 534 | | /// </summary> |
| | 535 | | /// <param name="e">Entity to convert to an Entity Reference</param> |
| | 536 | | /// <returns></returns> |
| | 537 | | public static EntityReference ToEntityReferenceWithKeyAttributes(this Entity e) |
| 342 | 538 | | { |
| 342 | 539 | | var result = e.ToEntityReference(); |
| | 540 | | #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 && !FAKE_XRM_EASY_2015 |
| 184 | 541 | | result.KeyAttributes = e.KeyAttributes; |
| | 542 | | #endif |
| 342 | 543 | | return result; |
| 342 | 544 | | } |
| | 545 | |
|
| | 546 | |
|
| | 547 | | } |
| | 548 | | } |