private async Task <bool> ImportComposerViewsFields(CommerceEntity commerceEntity, Dictionary <string, string> entityFields, CommerceContext context) { //Get root/master view of the target entity, composer views, if any, will be included in Child views of this master view var masterView = await _commerceCommander.Command <GetEntityViewCommand>().Process( context, commerceEntity.Id, commerceEntity.EntityVersion, context.GetPolicy <KnownCatalogViewsPolicy>().Master, string.Empty, string.Empty); if (masterView == null) { Log.Error($"Master view not found on Commerce Entity, Entity ID={commerceEntity.Id}"); throw new ApplicationException($"Master view not found on Commerce Entity, Entity ID={commerceEntity.Id}"); } if (masterView.ChildViews == null || masterView.ChildViews.Count == 0) { Log.Error($"No composer-generated views found on Sellable Item entity, Entity ID={commerceEntity.Id}"); throw new ApplicationException($"No composer-generated views found on Sellable Item entity, Entity ID={commerceEntity.Id}"); } //Now iterate through child views and then their child fields, looking for matching names var isUpdated = false; foreach (EntityView view in masterView.ChildViews) { EntityView composerViewForEdit = null; foreach (var viewField in view.Properties) { //Found matching field that need to be updated if (entityFields.Keys.Contains(viewField.Name)) { //Retrieve the composer view to update... if (composerViewForEdit == null) { composerViewForEdit = Task.Run <EntityView>(async() => await commerceEntity.GetComposerView(view.ItemId, _commerceCommander, context)).Result; } //...and update the field value if (composerViewForEdit != null) { var composerProperty = composerViewForEdit.GetProperty(viewField.Name); if (composerViewForEdit != null) { composerProperty.ParseValueAndSetEntityView(entityFields[viewField.Name]); isUpdated = true; } } } } } if (isUpdated) { return(await _composerCommander.PersistEntity(context, commerceEntity)); } return(false); }
/// <summary> /// Import fields defined in Item's composer views /// </summary> /// <param name="commerceEntity"></param> /// <param name="jsonData"></param> /// <param name="mappingPolicy"></param> /// <param name="context"></param> /// <returns></returns> public async Task <SellableItem> ImportComposerViewsFields(SellableItem commerceEntity, Dictionary <string, string> composerFields, CommerceContext context) { var masterView = await _commerceCommander.Command <GetEntityViewCommand>().Process( context, commerceEntity.Id, commerceEntity.EntityVersion, context.GetPolicy <KnownCatalogViewsPolicy>().Master, string.Empty, string.Empty); if (masterView == null) { Log.Error($"Master view not found on Commerce Entity, Entity ID={commerceEntity.Id}"); throw new ApplicationException($"Master view not found on Commerce Entity, Entity ID={commerceEntity.Id}"); } if (masterView.ChildViews == null || masterView.ChildViews.Count == 0) { Log.Error($"No composer-generated views found on Sellable Item entity, Entity ID={commerceEntity.Id}"); throw new ApplicationException($"No composer-generated views found on Sellable Item entity, Entity ID={commerceEntity.Id}"); } var isUpdated = false; foreach (EntityView view in masterView.ChildViews) { EntityView composerViewForEdit = null; foreach (var viewField in view.Properties) { if (composerFields.Keys.Contains(viewField.Name)) { if (composerViewForEdit == null) { composerViewForEdit = Task.Run <EntityView>(async() => await commerceEntity.GetComposerView(view.ItemId, _commerceCommander, context)).Result; } if (composerViewForEdit != null) { var composerProperty = composerViewForEdit.GetProperty(viewField.Name); if (composerViewForEdit != null) { composerProperty.ParseValueAndSetEntityView(composerFields[viewField.Name]); isUpdated = true; } } } } } if (isUpdated) { await _composerCommander.PersistEntity(context, commerceEntity); //var persistResult = await _commerceCommander.Pipeline<IPersistEntityPipeline>().Run(new PersistEntityArgument(commerceEntity), context.PipelineContextOptions); return(await _commerceCommander.Command <FindEntityCommand>().Process(context, typeof(SellableItem), commerceEntity.Id) as SellableItem); } return(commerceEntity); }
private async Task UpdateSyncforceComposerTemplate(SynchronizeProductArgument arg, CommercePipelineExecutionContext context, SellableItem sellableItem, ComposerTemplate commonComposerTemplate, SyncForceClientPolicy syncForcePolicy) { var sellableEntityViewComponent = sellableItem.GetComponent <EntityViewComponent>(); if (sellableEntityViewComponent != null) { EntityView tempViewHolder = new EntityView(); tempViewHolder.SetPropertyValue("Template", syncForcePolicy.CommonComposerTemplateName); tempViewHolder.GetProperty(it => String.Equals(it.Name, "Template", StringComparison.InvariantCultureIgnoreCase)).Value = syncForcePolicy.CommonComposerTemplateName; EntityView templateView = !string.IsNullOrEmpty(tempViewHolder.ItemId) ? sellableItem.GetComponent <EntityViewComponent>().ChildViewWithItemId(tempViewHolder.ItemId) : sellableItem.GetComponent <EntityViewComponent>().View.ChildViews.OfType <EntityView>().FirstOrDefault <EntityView>(); if (templateView == null || !sellableItem.GetComponent <EntityViewComponent>().HasChildViews((Func <EntityView, bool>)(p => p.Name.Equals(templateView.Name, StringComparison.OrdinalIgnoreCase)))) { await _composerCommander.AddChildViewFromTemplate(context.CommerceContext, tempViewHolder, sellableItem); } var composerView = sellableItem.GetComponent <EntityViewComponent>()?.View.ChildViews?.OfType <EntityView>() ?.FirstOrDefault(v => v.Name == syncForcePolicy.CommonComposerTemplateName); if (composerView != null) { //Add SyncForce timestamp var timestampDateTime = DateTime.SpecifyKind(arg.MasterProduct.TimeStamp, DateTimeKind.Utc); var timestampDateTimeOffset = (DateTimeOffset)timestampDateTime; var timestampRawValue = timestampDateTimeOffset.ToString("yyyy-MM-dd'T'H:mm:ss.fffffffzzz"); //Ensure timestamp field on ComposerTemplate await EnsureComposerTemplateProperty(syncForcePolicy, commonComposerTemplate, context.CommerceContext, composerView, "Timestamp", "System.DateTimeOffset"); if (composerView.ContainsProperty("Timestamp")) { composerView.SetPropertyValue("Timestamp", timestampRawValue); } else { composerView.Properties.Add(new ViewProperty() { DisplayName = "Timestamp", RawValue = timestampRawValue, IsHidden = false, IsReadOnly = false, IsRequired = false, Name = "Timestamp", OriginalType = "System.DateTimeOffset" }); } } } }