diff --git a/src/XrmMockupShared/Core.cs b/src/XrmMockupShared/Core.cs index 28af7642..53b9383b 100644 --- a/src/XrmMockupShared/Core.cs +++ b/src/XrmMockupShared/Core.cs @@ -626,6 +626,9 @@ internal OrganizationResponse Execute(OrganizationRequest request, EntityReferen //perform security checks for the request CheckRequestSecurity(request, userRef); + //perform initialization of preoperation + InitializePreOperation(request, userRef, preImage); + if (settings.TriggerProcesses && entityInfo != null) { // Shared variables should be moved to parent context when transitioning from 10 to 20. @@ -843,6 +846,18 @@ private void CheckRequestSecurity(OrganizationRequest request, EntityReference u throw new NotImplementedException($"CheckRequestSecurity for the request '{request.RequestName}' has not been implemented yet."); } + private void InitializePreOperation(OrganizationRequest request, EntityReference userRef, Entity preImage) + { + var handler = RequestHandlers.FirstOrDefault(x => x.HandlesRequest(request.RequestName)); + if(handler != null) + { + handler.InitializePreOperation(request, userRef, preImage); + } + return; + + throw new NotImplementedException($"InitializePreOperation for the request '{request.RequestName}' has not been implemented yet."); + } + private string RequestNameToMessageName(string requestName) { switch (requestName) diff --git a/src/XrmMockupShared/Requests/CreateRequestHandler.cs b/src/XrmMockupShared/Requests/CreateRequestHandler.cs index 119c8021..ea2af49f 100644 --- a/src/XrmMockupShared/Requests/CreateRequestHandler.cs +++ b/src/XrmMockupShared/Requests/CreateRequestHandler.cs @@ -74,6 +74,31 @@ internal override void CheckSecurity(OrganizationRequest orgRequest, EntityRefer } } + internal override void InitializePreOperation(OrganizationRequest orgRequest, EntityReference userRef, Entity preImage) + { + var entity = orgRequest["Target"] as Entity; + + if (Utility.IsValidAttribute("createdon", metadata.EntityMetadata.GetMetadata(entity.LogicalName))) + { + entity["createdon"] = DateTime.UtcNow.Add(core.TimeOffset); + } + + if (Utility.IsValidAttribute("createdby", metadata.EntityMetadata.GetMetadata(entity.LogicalName))) + { + entity["createdby"] = userRef; + } + + if (Utility.IsValidAttribute("modifiedon", metadata.EntityMetadata.GetMetadata(entity.LogicalName))) + { + entity["modifiedon"] = entity["createdon"]; + } + + if (Utility.IsValidAttribute("modifiedby", metadata.EntityMetadata.GetMetadata(entity.LogicalName))) + { + entity["modifiedby"] = entity["createdby"]; + } + } + internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) { var request = MakeRequest(orgRequest); @@ -175,23 +200,6 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E } } - if (Utility.IsValidAttribute("createdon", entityMetadata)) - { - clonedEntity["createdon"] = DateTime.UtcNow.Add(core.TimeOffset); - } - - if (Utility.IsValidAttribute("createdby", entityMetadata)) - { - clonedEntity["createdby"] = userRef; - } - - if (Utility.IsValidAttribute("modifiedon", entityMetadata) && - Utility.IsValidAttribute("modifiedby", entityMetadata)) - { - clonedEntity["modifiedon"] = clonedEntity["createdon"]; - clonedEntity["modifiedby"] = clonedEntity["createdby"]; - } - var owner = userRef; if (clonedEntity.Attributes.ContainsKey("ownerid")) { diff --git a/src/XrmMockupShared/Requests/RequestHandler.cs b/src/XrmMockupShared/Requests/RequestHandler.cs index 8da05393..f30ff96e 100644 --- a/src/XrmMockupShared/Requests/RequestHandler.cs +++ b/src/XrmMockupShared/Requests/RequestHandler.cs @@ -29,6 +29,9 @@ internal Boolean HandlesRequest(string RequestName) { virtual internal void CheckSecurity(OrganizationRequest orgRequest, EntityReference userRef) { } + virtual internal void InitializePreOperation(OrganizationRequest orgRequest, EntityReference userRef, Entity preImage = null) + { + } public static T MakeRequest(OrganizationRequest req) where T : OrganizationRequest { var typedReq = Activator.CreateInstance(); diff --git a/src/XrmMockupShared/Requests/UpdateRequestHandler.cs b/src/XrmMockupShared/Requests/UpdateRequestHandler.cs index 55b95d37..38d73625 100644 --- a/src/XrmMockupShared/Requests/UpdateRequestHandler.cs +++ b/src/XrmMockupShared/Requests/UpdateRequestHandler.cs @@ -6,6 +6,7 @@ using System.ServiceModel; using Microsoft.Xrm.Sdk.Metadata; using DG.Tools.XrmMockup.Database; +using System; namespace DG.Tools.XrmMockup { @@ -67,6 +68,21 @@ internal override void CheckSecurity(OrganizationRequest orgRequest, EntityRefer } } + internal override void InitializePreOperation(OrganizationRequest orgRequest, EntityReference userRef, Entity preImage) + { + var entity = orgRequest["Target"] as Entity; + + if (Utility.IsValidAttribute("modifiedon", metadata.EntityMetadata.GetMetadata(entity.LogicalName))) + { + entity["modifiedon"] = preImage["modifiedon"]; + } + + if (Utility.IsValidAttribute("modifiedby", metadata.EntityMetadata.GetMetadata(entity.LogicalName))) + { + entity["modifiedby"] = preImage["modifiedby"]; + } + } + internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) { var request = MakeRequest(orgRequest);