Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public async Task <bool> ImportComposerViewsFields(CommerceEntity 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)
            {
                return(await _composerCommander.PersistEntity(context, commerceEntity));
            }

            return(false);
        }