A pipeline block modifying the views for the order and customer entity
namespace Sitecore.Commerce.Connectors.MYERP.Plugin.Pipelines.Blocks | |
{ | |
using System.Globalization; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Sitecore.Commerce.Connectors.MYERP.Plugin.Components; | |
using Sitecore.Commerce.Core; | |
using Sitecore.Commerce.EntityViews; | |
using Sitecore.Commerce.Plugin.Customers; | |
using Sitecore.Commerce.Plugin.Orders; | |
using Sitecore.Framework.Conditions; | |
using Sitecore.Framework.Pipelines; | |
/// <summary> | |
/// The process orders minion block. | |
/// </summary> | |
public class ModifyViewsBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext> | |
{ | |
/// <summary> | |
/// The _pipeline. | |
/// </summary> | |
private readonly IPersistEntityPipeline _pipeline; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ModifyViewsBlock"/> class. | |
/// </summary> | |
/// <param name="persistEntityPipeline"> | |
/// The persist entity pipeline. | |
/// </param> | |
public ModifyViewsBlock(IPersistEntityPipeline persistEntityPipeline) | |
{ | |
this._pipeline = persistEntityPipeline; | |
} | |
/// <summary> | |
/// The run. | |
/// </summary> | |
/// <param name="entityView"> | |
/// The entity view. | |
/// </param> | |
/// <param name="context"> | |
/// The context. | |
/// </param> | |
/// <returns> | |
/// The <see cref="Task"/>. | |
/// </returns> | |
public override Task<EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context) | |
{ | |
Condition.Requires(entityView).IsNotNull("The argument can not be null"); | |
var argument = context.CommerceContext.GetObjects<EntityViewArgument>().FirstOrDefault(); | |
if (argument == null) | |
{ | |
return Task.FromResult(entityView); | |
} | |
if (argument.ViewName != context.GetPolicy<KnownOrderViewsPolicy>().Summary && | |
argument.ViewName != context.GetPolicy<KnownOrderViewsPolicy>().Master && | |
argument.ViewName != context.GetPolicy<KnownOrderViewsPolicy>().Preview && | |
argument.ViewName != context.GetPolicy<KnownCustomerViewsPolicy>().Preview && | |
argument.ViewName != context.GetPolicy<KnownCustomerViewsPolicy>().Master) | |
{ | |
// Do nothing if this request is for a different view | |
return Task.FromResult(entityView); | |
} | |
if (argument.Entity == null) | |
{ | |
// Do nothing if there is no entity loaded | |
return Task.FromResult(entityView); | |
} | |
// Only do something if the Entity is an order or customer | |
if (!(argument.Entity is Order) && !(argument.Entity is Customer)) | |
{ | |
return Task.FromResult(entityView); | |
} | |
EntityView entityViewToProcess; | |
if (argument.ViewName == context.GetPolicy<KnownOrderViewsPolicy>().Master && argument.Entity is Order) | |
{ | |
entityViewToProcess = entityView.ChildViews.FirstOrDefault(p => p.Name == "Summary") as EntityView; | |
} | |
else if (argument.ViewName == context.GetPolicy<KnownCustomerViewsPolicy>().Master && argument.Entity is Customer) | |
{ | |
entityViewToProcess = entityView.ChildViews.FirstOrDefault(p => p.Name == "Details") as EntityView; | |
} | |
else | |
{ | |
entityViewToProcess = entityView; | |
} | |
if (entityViewToProcess == null) | |
{ | |
return Task.FromResult(entityView); | |
} | |
// Orders | |
if ((argument.ViewName == context.GetPolicy<KnownOrderViewsPolicy>().Master | |
|| argument.ViewName == context.GetPolicy<KnownOrderViewsPolicy>().Summary) && argument.Entity is Order) | |
{ | |
OrderSummaryAndDetail(entityViewToProcess, (Order)argument.Entity); | |
} | |
else if (argument.ViewName == context.GetPolicy<KnownOrderViewsPolicy>().Summary && argument.Entity is Order) | |
{ | |
OrderPreview(entityViewToProcess, (Order)argument.Entity); | |
} | |
// Customers | |
else if (argument.ViewName == context.GetPolicy<KnownCustomerViewsPolicy>().Preview && argument.Entity is Customer) | |
{ | |
CustomerPreview(entityViewToProcess, (Customer)argument.Entity); | |
} | |
else if (argument.ViewName == context.GetPolicy<KnownCustomerViewsPolicy>().Master && argument.Entity is Customer) | |
{ | |
CustomerSummaryAndDetail(entityViewToProcess, (Customer)argument.Entity); | |
} | |
return Task.FromResult(entityView); | |
} | |
/// <summary> | |
/// The order preview. | |
/// </summary> | |
/// <param name="entityViewToProcess"> | |
/// The entity view to process. | |
/// </param> | |
/// <param name="order"> | |
/// The order. | |
/// </param> | |
private static void OrderPreview(EntityView entityViewToProcess, Order order) | |
{ | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP order no.", | |
DisplayName = "ERP order no.", | |
IsReadOnly = true, | |
RawValue = order.GetComponent<ErpOrderStatusComponent>().ErpOrderNumber, | |
Value = order.GetComponent<ErpOrderStatusComponent>().ErpOrderNumber | |
}); | |
} | |
/// <summary> | |
/// The customer preview. | |
/// </summary> | |
/// <param name="entityViewToProcess"> | |
/// The entity view to process. | |
/// </param> | |
/// <param name="customer"> | |
/// The customer. | |
/// </param> | |
private static void CustomerPreview(EntityView entityViewToProcess, Customer customer) | |
{ | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP customer no.", | |
DisplayName = "ERP customer no.", | |
IsReadOnly = true, | |
RawValue = customer.GetComponent<ErpCustomerStatusComponent>().ErpCustomerNo, | |
Value = customer.GetComponent<ErpCustomerStatusComponent>().ErpCustomerNo | |
}); | |
} | |
/// <summary> | |
/// The order summary and detail. | |
/// </summary> | |
/// <param name="entityViewToProcess"> | |
/// The entity view to process. | |
/// </param> | |
/// <param name="order"> | |
/// The order. | |
/// </param> | |
private static void OrderSummaryAndDetail(EntityView entityViewToProcess, Order order) | |
{ | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP order no.", | |
DisplayName = "ERP order no.", | |
IsReadOnly = true, | |
RawValue = order.GetComponent<ErpOrderStatusComponent>().ErpOrderNumber, | |
Value = order.GetComponent<ErpOrderStatusComponent>().ErpOrderNumber | |
}); | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP status date", | |
DisplayName = "ERP status date", | |
IsReadOnly = true, | |
RawValue = order.GetComponent<ErpOrderStatusComponent>().StatusDate, | |
Value = | |
order.GetComponent<ErpOrderStatusComponent>() | |
.StatusDate.ToString(CultureInfo.InvariantCulture) | |
}); | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "Is integrated", | |
DisplayName = "Is integrated", | |
IsReadOnly = true, | |
RawValue = order.GetComponent<ErpOrderStatusComponent>().Integrated, | |
Value = order.GetComponent<ErpOrderStatusComponent>().Integrated.ToString() | |
}); | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP status message", | |
DisplayName = "ERP status message", | |
IsReadOnly = true, | |
RawValue = order.GetComponent<ErpOrderStatusComponent>().StatusMessage, | |
Value = order.GetComponent<ErpOrderStatusComponent>().StatusMessage | |
}); | |
} | |
/// <summary> | |
/// The customer summary and detail. | |
/// </summary> | |
/// <param name="entityViewToProcess"> | |
/// The entity view to process. | |
/// </param> | |
/// <param name="customer"> | |
/// The customer. | |
/// </param> | |
private static void CustomerSummaryAndDetail(EntityView entityViewToProcess, Customer customer) | |
{ | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP customer no.", | |
DisplayName = "ERP customer no.", | |
IsReadOnly = true, | |
RawValue = customer.GetComponent<ErpCustomerStatusComponent>().ErpCustomerNo, | |
Value = customer.GetComponent<ErpCustomerStatusComponent>().ErpCustomerNo | |
}); | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP status date", | |
DisplayName = "ERP status date", | |
IsReadOnly = true, | |
RawValue = customer.GetComponent<ErpCustomerStatusComponent>().StatusDate, | |
Value = | |
customer.GetComponent<ErpCustomerStatusComponent>() | |
.StatusDate.ToString(CultureInfo.InvariantCulture) | |
}); | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "Is integrated", | |
DisplayName = "Is integrated", | |
IsReadOnly = true, | |
RawValue = customer.GetComponent<ErpCustomerStatusComponent>().Integrated, | |
Value = customer.GetComponent<ErpCustomerStatusComponent>().Integrated.ToString() | |
}); | |
entityViewToProcess.Properties.Add( | |
new ViewProperty | |
{ | |
Name = "ERP status message", | |
DisplayName = "ERP status message", | |
IsReadOnly = true, | |
RawValue = customer.GetComponent<ErpCustomerStatusComponent>().StatusMessage, | |
Value = customer.GetComponent<ErpCustomerStatusComponent>().StatusMessage | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment