Beispiel #1
0
        public async override Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
        {
            Condition.Requires(entityView).IsNotNull($"{Name}: The argument cannot be null.");

            // Only proceed if the right action was invoked
            if (string.IsNullOrEmpty(entityView.Action) || !entityView.Action.StartsWith("Edit-", StringComparison.OrdinalIgnoreCase))
            {
                return(entityView);
            }

            // Get the commerce entity from the context
            var commerceEntity = context.CommerceContext.GetObject <CommerceEntity>(x => x.Id.Equals(entityView.EntityId));

            if (commerceEntity == null)
            {
                return(entityView);
            }

            KnownResultCodes errorCodes = context.CommerceContext.GetPolicy <KnownResultCodes>();

            if (context.CommerceContext.AnyMessage(msg => msg.Code == errorCodes.ValidationError))
            {   // We found an error
                return(entityView);
            }

            var components = commerceEntity.Components;

            if (!string.IsNullOrWhiteSpace(entityView.ItemId) && commerceEntity is SellableItem)
            {
                var variation = ((SellableItem)commerceEntity).GetVariation(entityView.ItemId);
                if (variation != null)
                {
                    components = variation.ChildComponents;
                }
            }

            var allComponentTypes   = catalogSchemaCommander.GetAllComponentTypes();
            var editedComponentType = allComponentTypes.SingleOrDefault(compType => entityView.Action == $"Edit-{compType.FullName}");
            var editedComponent     = components.SingleOrDefault(comp => entityView.Action == $"Edit-{comp.GetType().FullName}");

            if (editedComponent == null)
            {
                editedComponent = (Sitecore.Commerce.Core.Component)Activator.CreateInstance(editedComponentType);
                components.Add(editedComponent);
            }

            if (editedComponent != null)
            {
                catalogSchemaCommander.SetPropertyValuesOnEditedComponent(entityView.Properties, editedComponent.GetType(), editedComponent, context.CommerceContext);

                // Persist changes
                await this.catalogSchemaCommander.Pipeline <IPersistEntityPipeline>().Run(new PersistEntityArgument(commerceEntity), context);
            }

            return(await Task.FromResult(entityView));
        }
Beispiel #2
0
        public async override Task <EntityView> Run(EntityView arg, CommercePipelineExecutionContext context)
        {
            Condition.Requires(arg).IsNotNull($"{Name}: The argument cannot be null.");
            var request = this.viewCommander.CurrentEntityViewArgument(context.CommerceContext);

            // Only proceed if the current entity is a sellable item
            if (!IsSupportedEntity(request.Entity))
            {
                return(arg);
            }

            var isConnectView = arg.Name.Equals(GetConnectViewName(context), StringComparison.OrdinalIgnoreCase);

            // Make sure that we target the correct views
            if (!isConnectView)
            {
                return(arg);
            }

            List <Type> allComponentTypes = catalogSchemaCommander.GetAllComponentTypes();

            var targetView = arg;

            foreach (var componentType in allComponentTypes)
            {
                System.Attribute[] attrs = System.Attribute.GetCustomAttributes(componentType);

                if (attrs.SingleOrDefault(attr => attr is EntityViewAttribute) is EntityViewAttribute entityViewAttribute &&
                    ComponentShouldBeAddedToDataTemplate(attrs))
                {
                    // Create a new view and add it to the current entity view.
                    var view = new EntityView
                    {
                        Name          = componentType.FullName.Replace(".", "_"),
                        DisplayName   = entityViewAttribute?.ViewName ?? componentType.Name,
                        EntityId      = arg.EntityId,
                        EntityVersion = arg.EntityVersion
                    };

                    arg.ChildViews.Add(view);

                    targetView = view;

                    var props = componentType.GetProperties();

                    foreach (var prop in props)
                    {
                        System.Attribute[] propAttributes = System.Attribute.GetCustomAttributes(prop);

                        if (propAttributes.SingleOrDefault(attr => attr is PropertyAttribute) is PropertyAttribute propAttr)
                        {
                            targetView.Properties.Add(new ViewProperty
                            {
                                Name         = prop.Name,
                                DisplayName  = propAttr.DisplayName,
                                OriginalType = prop.PropertyType.FullName,
                                IsReadOnly   = propAttr.IsReadOnly,
                                IsRequired   = propAttr.IsRequired
                            });
                        }
                    }
                }
            }

            return(await Task.FromResult(arg));
        }