Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initialization of pre operation for create and update #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/XrmMockupShared/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 25 additions & 17 deletions src/XrmMockupShared/Requests/CreateRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateRequest>(orgRequest);
Expand Down Expand Up @@ -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"))
{
Expand Down
3 changes: 3 additions & 0 deletions src/XrmMockupShared/Requests/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(OrganizationRequest req) where T : OrganizationRequest {
var typedReq = Activator.CreateInstance<T>();
Expand Down
16 changes: 16 additions & 0 deletions src/XrmMockupShared/Requests/UpdateRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Metadata;
using DG.Tools.XrmMockup.Database;
using System;

namespace DG.Tools.XrmMockup
{
Expand Down Expand Up @@ -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<UpdateRequest>(orgRequest);
Expand Down