Ejemplo n.º 1
0
 private static void ValidateContext(BulkActionContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
 }
Ejemplo n.º 2
0
        public async Task <Property[]> GetPropertiesAsync(BulkActionContext context)
        {
            var result      = new List <Property>();
            var propertyIds = new HashSet <string>();
            var dataSource  = _dataSourceFactory.Create(context);

            result.AddRange(GetStandardProperties());

            while (await dataSource.FetchAsync())
            {
                var productIds = dataSource.Items.Select(item => item.Id).ToArray();
                var products   = await _itemService.GetByIdsAsync(productIds, (ItemResponseGroup.ItemInfo | ItemResponseGroup.ItemProperties).ToString());

                // using only product inherited properties from categories,
                // own product props (only from PropertyValues) are not set via bulk update action
                var newProperties = products.SelectMany(CollectionSelector())
                                    .Distinct(AnonymousComparer.Create <Property, string>(property => property.Id))
                                    .Where(property => !propertyIds.Contains(property.Id)).ToArray();

                propertyIds.AddRange(newProperties.Select(property => property.Id));
                result.AddRange(newProperties);
            }

            foreach (var property in result)
            {
                await FillOwnerName(property);
            }

            return(result.ToArray());
        }
        public IBulkAction Create(BulkActionContext context)
        {
            IBulkAction result = null;

            switch (context)
            {
            case CategoryChangeBulkActionContext changeCategoryActionContext:
                result = new CategoryChangeBulkAction(_lazyLazyServiceProvider, changeCategoryActionContext);
                break;

            case PropertiesUpdateBulkActionContext updatePropertiesActionContext:
                result = new PropertiesUpdateBulkAction(_lazyLazyServiceProvider, updatePropertiesActionContext);
                break;
            }

            return(result ?? throw new ArgumentException($"Unsupported action type: {context.GetType().Name}"));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> GetActionData([FromBody] BulkActionContext context)
        {
            ValidateContext(context);

            var actionProvider = _bulkActionProviderStorage.Get(context.ActionName);

            if (!await IsAuthorizedUserHasPermissionsAsync(actionProvider.Permissions))
            {
                return(Unauthorized());
            }

            var factory    = actionProvider.BulkActionFactory;
            var action     = factory.Create(context);
            var actionData = await action.GetActionDataAsync();

            return(Ok(actionData));
        }
        public IBulkAction Create(BulkActionContext context)
        {
            IBulkAction result = null;

            switch (context)
            {
            case CategoryChangeBulkActionContext changeCategoryActionContext:
                result = new CategoryChangeBulkAction(changeCategoryActionContext, _catalogService, _categoryListEntryMover, _productListEntryMover);
                break;

            case PropertiesUpdateBulkActionContext updatePropertiesActionContext:
                result = new PropertiesUpdateBulkAction(updatePropertiesActionContext, _itemService, _bulkPropertyUpdateManager);
                break;
            }

            return(result ?? throw new ArgumentException($"Unsupported action type: {context.GetType().Name}"));
        }
        public IDataSource Create(BulkActionContext context)
        {
            IDataSource result = null;

            switch (context)
            {
            case CategoryChangeBulkActionContext categoryChangeContext:
                result = new BaseDataSource(_listEntrySearchService, categoryChangeContext.DataQuery);
                break;

            case PropertiesUpdateBulkActionContext propertiesUpdateContext:
                result = new ProductDataSource(_listEntrySearchService, propertiesUpdateContext.DataQuery);
                break;
            }

            var message = $"Unsupported bulk action query type: {context.GetType().Name}";

            return(result ?? throw new ArgumentException(message));
        }
Ejemplo n.º 7
0
        public async Task <ActionResult <BulkActionPushNotification> > Run([FromBody] BulkActionContext context)
        {
            ValidateContext(context);

            var actionProvider = _bulkActionProviderStorage.Get(context.ActionName);

            if (!await IsAuthorizedUserHasPermissionsAsync(actionProvider.Permissions))
            {
                return(Unauthorized());
            }

            var creator      = _userNameResolver.GetCurrentUserName();
            var notification = new BulkActionPushNotification(creator)
            {
                Title       = $"{context.ActionName}",
                Description = "Starting…"
            };

            notification.JobId = _backgroundJobExecutor.Enqueue <BulkActionJob>(job => job.ExecuteAsync(context, notification, JobCancellationToken.Null, null));

            return(Ok(notification));
        }
Ejemplo n.º 8
0
        public async Task ExecuteAsync(
            BulkActionContext bulkActionContext,
            BulkActionPushNotification notification,
            IJobCancellationToken cancellationToken,
            PerformContext performContext)
        {
            Validate(bulkActionContext);
            Validate(performContext);

            try
            {
                var tokenWrapper = new JobCancellationTokenWrapper(cancellationToken);
                await _bulkActionExecutor.ExecuteAsync(
                    bulkActionContext,
                    context =>
                {
                    notification.Patch(context);
                    notification.JobId = performContext.BackgroundJob.Id;
                    _pushNotificationManager.Send(notification);
                },
                    tokenWrapper);
            }
            catch (JobAbortedException)
            {
                // idle
            }
            catch (Exception exception)
            {
                notification.Errors.Add(exception.ExpandExceptionMessage());
            }
            finally
            {
                notification.Description = "Job finished";
                notification.Finished    = DateTime.UtcNow;
                _pushNotificationManager.Send(notification);
            }
        }
        public virtual async Task ExecuteAsync(BulkActionContext context,
                                               Action <BulkActionProgressContext> progressAction,
                                               ICancellationToken token)
        {
            // initialize the common context
            _progressAction  = progressAction;
            _token           = token;
            _progressContext = new BulkActionProgressContext {
                Description = "Validation has started…"
            };
            var totalCount     = 0;
            var processedCount = 0;

            // begin
            ValidateContext(context);
            SendFeedback();

            try
            {
                var action = GetAction(context);
                await ValidateActionAsync(action);

                SendFeedback();

                var dataSource = GetDataSource(context);
                totalCount = await dataSource.GetTotalCountAsync();

                SetProcessedCount(processedCount);
                SetTotalCount(totalCount);
                SetDescription("The process has been started…");
                SendFeedback();

                while (await dataSource.FetchAsync())
                {
                    ThrowIfCancellationRequested();

                    var result = await action.ExecuteAsync(dataSource.Items);

                    if (result.Succeeded)
                    {
                        // idle
                    }
                    else
                    {
                        SetErrors(result.Errors);
                    }

                    processedCount += dataSource.Items.Count();
                    SetProcessedCount(processedCount);

                    if (processedCount == totalCount)
                    {
                        continue;
                    }

                    SetDescription($"{processedCount} out of {totalCount} have been updated.");
                    SendFeedback();
                }
            }
            catch (Exception exception)
            {
                SetError(exception.Message);
            }
            finally
            {
                const string ErrorsMessage   = "The process has been completed with errors";
                const string CompleteMessage = "Process is completed";
                var          message         = IsContainsErrors() ? ErrorsMessage : CompleteMessage;
                SetDescription($"{message}: {processedCount} out of {totalCount} have been updated.");
                SendFeedback();
            }
        }
        private IDataSource GetDataSource(BulkActionContext context)
        {
            var actionProvider = _bulkActionProviderStorage.Get(context.ActionName);

            return(actionProvider.DataSourceFactory.Create(context));
        }
        private IBulkAction GetAction(BulkActionContext context)
        {
            var actionProvider = _bulkActionProviderStorage.Get(context.ActionName);

            return(actionProvider.BulkActionFactory.Create(context));
        }