| | 1 | | using System; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using FakeXrmEasy.Extensions; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using Microsoft.Xrm.Sdk.Query; |
| | 6 | |
|
| | 7 | | namespace FakeXrmEasy.Services |
| | 8 | | { |
| | 9 | | public enum EntityInitializationLevel |
| | 10 | | { |
| | 11 | | Default = 0, //Minimal initialization of common attributes |
| | 12 | | PerEntity = 1 //More detailed initialization of entities, on an entity per entity basis |
| | 13 | | } |
| | 14 | | public class DefaultEntityInitializerService : IEntityInitializerService |
| | 15 | | { |
| | 16 | |
|
| | 17 | | public Dictionary<string, IEntityInitializerService> InitializerServiceDictionary; |
| | 18 | |
|
| 4276 | 19 | | public DefaultEntityInitializerService() |
| 4276 | 20 | | { |
| 4276 | 21 | | InitializerServiceDictionary = new Dictionary<string, IEntityInitializerService>() |
| 4276 | 22 | | { |
| 4276 | 23 | | { InvoiceDetailInitializerService.EntityLogicalName, new InvoiceDetailInitializerService() }, |
| 4276 | 24 | | { InvoiceInitializerService.EntityLogicalName, new InvoiceInitializerService() } |
| 4276 | 25 | | }; |
| 4276 | 26 | | } |
| | 27 | |
|
| | 28 | | public Entity Initialize(Entity e, Guid gCallerId, XrmFakedContext ctx, bool isManyToManyRelationshipEntity = fa |
| 28494 | 29 | | { |
| | 30 | | //Validate primary key for dynamic entities |
| 28494 | 31 | | var primaryKey = string.Format("{0}id", e.LogicalName); |
| 28494 | 32 | | if (!e.Attributes.ContainsKey(primaryKey)) |
| 22296 | 33 | | { |
| 22296 | 34 | | e[primaryKey] = e.Id; |
| 22296 | 35 | | } |
| | 36 | |
|
| 28494 | 37 | | if (isManyToManyRelationshipEntity) |
| 72 | 38 | | { |
| 72 | 39 | | return e; |
| | 40 | | } |
| | 41 | |
|
| 28422 | 42 | | var CallerId = new EntityReference("systemuser", gCallerId); //Create a new instance by default |
| | 43 | |
|
| 28422 | 44 | | var now = DateTime.UtcNow; |
| | 45 | |
|
| 28422 | 46 | | e.SetValueIfEmpty("createdon", now); |
| | 47 | |
|
| | 48 | | //Overriden created on should replace created on |
| 28422 | 49 | | if (e.Contains("overriddencreatedon")) |
| 6 | 50 | | { |
| 6 | 51 | | e["createdon"] = e["overriddencreatedon"]; |
| 6 | 52 | | } |
| | 53 | |
|
| 28422 | 54 | | e.SetValueIfEmpty("modifiedon", now); |
| 28422 | 55 | | e.SetValueIfEmpty("createdby", CallerId); |
| 28422 | 56 | | e.SetValueIfEmpty("modifiedby", CallerId); |
| 28422 | 57 | | e.SetValueIfEmpty("ownerid", CallerId); |
| 28422 | 58 | | e.SetValueIfEmpty("statecode", new OptionSetValue(0)); //Active by default |
| | 59 | |
|
| 28422 | 60 | | if (ctx.InitializationLevel == EntityInitializationLevel.PerEntity) |
| 360 | 61 | | { |
| 360 | 62 | | if (!string.IsNullOrEmpty(e.LogicalName) && InitializerServiceDictionary.ContainsKey(e.LogicalName)) |
| 144 | 63 | | InitializerServiceDictionary[e.LogicalName].Initialize(e, gCallerId, ctx, isManyToManyRelationshipEn |
| 360 | 64 | | } |
| | 65 | |
|
| 28422 | 66 | | return e; |
| 28494 | 67 | | } |
| | 68 | |
|
| | 69 | | public Entity Initialize(Entity e, XrmFakedContext ctx, bool isManyToManyRelationshipEntity = false) |
| 0 | 70 | | { |
| 0 | 71 | | return this.Initialize(e, Guid.NewGuid(), ctx, isManyToManyRelationshipEntity); |
| 0 | 72 | | } |
| | 73 | | } |
| | 74 | | } |