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); }