/// <summary>Executes the pipeline block's code logic.</summary>
        /// <param name="arg">The pipeline argument.</param>
        /// <param name="context">The context.</param>
        /// <returns>The <see cref="EntityView"/>.</returns>
        public override async Task <EntityView> RunAsync(EntityView arg, CommercePipelineExecutionContext context)
        {
            if (string.IsNullOrEmpty(arg?.Action) ||
                !arg.Action.Equals(context.GetPolicy <KnownEntityVersionsActionsPolicy>().AddEntityVersion, StringComparison.OrdinalIgnoreCase))
            {
                return(arg);
            }

            var commerceEntity = context.CommerceContext.GetObjects <CommerceEntity>().FirstOrDefault(p => p.Id.Equals(arg.EntityId, StringComparison.OrdinalIgnoreCase));

            if (commerceEntity == null)
            {
                await context.CommerceContext.AddMessage(
                    context.GetPolicy <KnownResultCodes>().ValidationError,
                    "EntityNotFound",
                    new object[]
                {
                    arg.EntityId
                },
                    $"Entity {arg.EntityId} was not found.")
                .ConfigureAwait(false);

                return(arg);
            }

            var findArg  = new FindEntityArgument(typeof(VersioningEntity), VersioningEntity.GetIdBasedOnEntityId(commerceEntity.Id));
            var versions = await _commerceCommander.Pipeline <IFindEntityPipeline>()
                           .RunAsync(findArg, context)
                           .ConfigureAwait(false);

            var latestVersion = (versions as VersioningEntity)?.LatestVersion(context.CommerceContext) ?? 1;
            var newVersion    = latestVersion + 1;

            await _commerceCommander.ProcessWithTransaction(context.CommerceContext,
                                                            () => _commerceCommander.Pipeline <IAddEntityVersionPipeline>()
                                                            .RunAsync(new AddEntityVersionArgument(commerceEntity)
            {
                CurrentVersion = commerceEntity.EntityVersion,
                NewVersion     = newVersion
            },
                                                                      context.CommerceContext.PipelineContextOptions)).ConfigureAwait(false);

            context.CommerceContext.AddModel(
                new RedirectUrlModel($"/entityView/Master/{newVersion}/{arg.EntityId}")
                );

            return(arg);
        }
예제 #2
0
        public override async Task <ImportEntityArgument> Run(ImportEntityArgument arg, CommercePipelineExecutionContext context)
        {
            string entityId = arg.ImportHandler.EntityId;

            var entityToUpdate = arg.ImportHandler.GetCommerceEntity() ?? await _findEntityCommand
                                 .Process(context.CommerceContext, typeof(CommerceEntity), entityId)
                                 .ConfigureAwait(false);

            bool createNewVersion = entityToUpdate != null &&
                                    arg.CatalogImportPolicy.EntityVersioningScheme == EntityVersioningScheme.CreateNewVersion;

            if (entityToUpdate != null &&
                arg.CatalogImportPolicy.EntityVersioningScheme == EntityVersioningScheme.UpdateLatestUnpublished)
            {
                VersioningEntity versioningEntity = await _findEntityCommand.Process(context.CommerceContext, typeof(VersioningEntity), VersioningEntity.GetIdBasedOnEntityId(entityToUpdate.Id), new int?()).ConfigureAwait(false) as VersioningEntity;

                if (versioningEntity?.Versions != null && versioningEntity.Versions.Any())
                {
                    var unpublishedVersion = versioningEntity.Versions.Where(x => !x.Published).Max(x => x.Version);
                    if (unpublishedVersion <= 0 || entityToUpdate.EntityVersion != unpublishedVersion)
                    {
                        createNewVersion = true;
                    }
                }
            }

            if (createNewVersion)
            {
                var newVersion = entityToUpdate.EntityVersion + 1;
                await _addEntityVersionCommand.Process(context.CommerceContext, entityToUpdate, newVersion).ConfigureAwait(false);

                entityToUpdate = await _findEntityCommand.Process(context.CommerceContext, typeof(CommerceEntity), entityId, newVersion).ConfigureAwait(false);
            }

            if (entityToUpdate != null)
            {
                arg.IsNew = false;
                arg.ImportHandler.SetCommerceEntity(entityToUpdate);
            }

            return(arg);
        }