private ImportNotification CreateImportNotification(Guid id, UKCompetentAuthority competentAuthority, string notificationNumber)
        {
            var importNotification = new ImportNotification(NotificationType.Recovery, competentAuthority, notificationNumber);

            EntityHelper.SetEntityId(importNotification, id);
            return(importNotification);
        }
        // Only public methods can be invoked in the background. (Hangfire)
        public void BackgroundImport(CsvImportInfo importInfo, ImportNotification notifyEvent)
        {
            Action <ExportImportProgressInfo> progressCallback = x =>
            {
                notifyEvent.InjectFrom(x);
                _notifier.Upsert(notifyEvent);
            };

            using (var stream = _blobStorageProvider.OpenRead(importInfo.FileUrl))
            {
                try
                {
                    _csvImporter.DoImport(stream, importInfo, progressCallback);
                }
                catch (Exception ex)
                {
                    notifyEvent.Description = "Export error";
                    notifyEvent.ErrorCount++;
                    notifyEvent.Errors.Add(ex.ToString());
                }
                finally
                {
                    notifyEvent.Finished    = DateTime.UtcNow;
                    notifyEvent.Description = "Import finished" + (notifyEvent.Errors.Any() ? " with errors" : " successfully");
                    _notifier.Upsert(notifyEvent);
                }
            }
        }
        public WasteOperationTests()
        {
            var recoveryImportNotification = new ImportNotification(NotificationType.Recovery, UKCompetentAuthority.England, "FR0001");
            EntityHelper.SetEntityId(recoveryImportNotification, importNotificationId);

            validRCodesList = OperationCodesList.CreateForNotification(recoveryImportNotification, new[] { OperationCode.R1, OperationCode.R2 });
        }
        public void BackgroundImport(ImportRequest request, ImportNotification notification)
        {
            Action <ExportImportProgressInfo> progressCallback = c =>
            {
                notification.InjectFrom(c);
                _notifier.Upsert(notification);
            };

            using (var stream = _blobStorageProvider.OpenRead(request.FileUrl))
            {
                try
                {
                    _csvCouponImporter.DoImport(stream, request.Delimiter, request.PromotionId, request.ExpirationDate, progressCallback);
                }
                catch (Exception exception)
                {
                    notification.Description = "Import error";
                    notification.ErrorCount++;
                    notification.Errors.Add(exception.ToString());
                }
                finally
                {
                    notification.Finished    = DateTime.UtcNow;
                    notification.Description = "Import finished" + (notification.Errors.Any() ? " with errors" : " successfully");
                    _notifier.Upsert(notification);
                }
            }
        }
예제 #5
0
 public OperationCodesListTests()
 {
     recoveryImportNotification = new ImportNotification(NotificationType.Recovery, UKCompetentAuthority.England,
                                                         "FR0001");
     disposalImportNotification = new ImportNotification(NotificationType.Disposal, UKCompetentAuthority.England,
                                                         "FR0001");
 }
예제 #6
0
        public WasteOperationTests()
        {
            var recoveryImportNotification = new ImportNotification(NotificationType.Recovery, UKCompetentAuthority.England, "FR0001");

            EntityHelper.SetEntityId(recoveryImportNotification, importNotificationId);

            validRCodesList        = OperationCodesList.CreateForNotification(recoveryImportNotification, new[] { OperationCode.R1, OperationCode.R2 });
            anotherValidRCodesList = OperationCodesList.CreateForNotification(recoveryImportNotification, new[] { OperationCode.R10, OperationCode.R13 });
        }
        private async Task CheckCompetentAuthority(ImportNotification notification)
        {
            var userCompetentAuthority = await context.GetUsersCompetentAuthority(userContext);

            if (notification.CompetentAuthority != userCompetentAuthority)
            {
                throw new SecurityException(string.Format("Access denied to this notification {0} for user {1} for competent authority {2}",
                    notification.Id, userContext.UserId, userCompetentAuthority));
            }
        }
        private async Task CheckCompetentAuthority(ImportNotification notification)
        {
            var userCompetentAuthority = await context.GetUsersCompetentAuthority(userContext);

            if (notification.CompetentAuthority != userCompetentAuthority)
            {
                throw new SecurityException(string.Format("Access denied to this notification {0} for user {1} for competent authority {2}",
                                                          notification.Id, userContext.UserId, userCompetentAuthority));
            }
        }
        public async Task Add(ImportNotification notification)
        {
            if (await context.ImportNotifications
                .AnyAsync(n => n.NotificationNumber == notification.NotificationNumber))
            {
                throw new InvalidOperationException("Cannot add an import notification with the duplicate number: " + notification.NotificationNumber);
            }

            context.ImportNotifications.Add(notification);
        }
        public async Task Add(ImportNotification notification)
        {
            if (await context.ImportNotifications
                .AnyAsync(n => n.NotificationNumber == notification.NotificationNumber))
            {
                throw new InvalidOperationException("Cannot add an import notification with the duplicate number: " + notification.NotificationNumber);
            }

            context.ImportNotifications.Add(notification);
        }
예제 #11
0
        private ImportNotification GetTestImportNotification(bool addToAddressBook)
        {
            ImportNotification importNotification = new ImportNotification()
            {
                Exporter   = GetValidExporter(addToAddressBook),
                Importer   = GetValidImporter(addToAddressBook),
                Producer   = GetValidProducer(addToAddressBook),
                Facilities = GetValidFacilities(addToAddressBook)
            };

            return(importNotification);
        }
        public async Task <ActionResult <ImportNotification> > ImportCouponsAsync([FromBody] ImportRequest request)
        {
            var notification = new ImportNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Import coupons from CSV",
                Description = "Starting import..."
            };

            await _notifier.SendAsync(notification);

            BackgroundJob.Enqueue(() => BackgroundImportAsync(request, notification));

            return(Ok(notification));
        }
        public IHttpActionResult DoImport(CsvImportInfo importInfo)
        {
            var notification = new ImportNotification(CurrentPrincipal.GetCurrentUserName())
            {
                Title       = "Import catalog from CSV",
                Description = "starting import...."
            };

            _notifier.Upsert(notification);

            BackgroundJob.Enqueue(() => BackgroundImport(importInfo, notification));

            return(Ok(notification));
        }
        public IHttpActionResult ImportCoupons(ImportRequest request)
        {
            var notification = new ImportNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Import coupons from CSV",
                Description = "Starting import..."
            };

            _notifier.Upsert(notification);

            BackgroundJob.Enqueue(() => BackgroundImport(request, notification));

            return(Ok(notification));
        }
        private async Task<decimal> GetPrice(ImportNotification notification, int numberOfShipments, bool isInterim)
        {
             var pricingStructures = await pricingStructureRepository.Get();

            var correspondingPricingStructure =
                pricingStructures.Single(p => p.CompetentAuthority == notification.CompetentAuthority
                                              && p.Activity.TradeDirection == TradeDirection.Import
                                              && p.Activity.NotificationType == notification.NotificationType
                                              && p.Activity.IsInterim == isInterim
                                              && p.ShipmentQuantityRange.RangeFrom <= numberOfShipments
                                              && (p.ShipmentQuantityRange.RangeTo == null
                                                  || p.ShipmentQuantityRange.RangeTo >= numberOfShipments));

            return correspondingPricingStructure.Price;
        } 
예제 #16
0
        private int AddressesToUpdate(ImportNotification draft)
        {
            int x = 0;

            x += draft.Exporter.IsAddedToAddressBook ? 1 : 0;
            x += draft.Importer.IsAddedToAddressBook ? 1 : 0;
            x += draft.Producer.IsAddedToAddressBook ? 1 : 0;

            foreach (var facility in draft.Facilities.Facilities)
            {
                x += facility.IsAddedToAddressBook ? 1 : 0;
            }

            return(x);
        }
        private async Task <decimal> GetPrice(ImportNotification notification, int numberOfShipments, bool isInterim)
        {
            var pricingStructures = await pricingStructureRepository.Get();

            var correspondingPricingStructure =
                pricingStructures.Single(p => p.CompetentAuthority == notification.CompetentAuthority &&
                                         p.Activity.TradeDirection == TradeDirection.Import &&
                                         p.Activity.NotificationType == notification.NotificationType &&
                                         p.Activity.IsInterim == isInterim &&
                                         p.ShipmentQuantityRange.RangeFrom <= numberOfShipments &&
                                         (p.ShipmentQuantityRange.RangeTo == null ||
                                          p.ShipmentQuantityRange.RangeTo >= numberOfShipments));

            return(correspondingPricingStructure.Price);
        }
        public IHttpActionResult DoImport(CsvImportConfiguration importConfiguration)
        {
            var notification = new ImportNotification(CurrentPrincipal.GetCurrentUserName())
            {
                Title       = "Import catalog from CSV",
                Description = "starting import...."
            };

            _notifier.Upsert(notification);

            var importJob = new CsvCatalogImportJob();

            BackgroundJob.Enqueue(() => importJob.DoImport(importConfiguration, notification));

            return(Ok(notification));
        }
        public IHttpActionResult DoImport(CsvImportInfo importInfo)
        {
            CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Import, importInfo);

            var notification = new ImportNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Import catalog from CSV",
                Description = "starting import...."
            };

            _notifier.Upsert(notification);

            BackgroundJob.Enqueue(() => BackgroundImport(importInfo, notification));

            return(Ok(notification));
        }
예제 #20
0
        private async Task AddToAddressBookForType(AddressRecordType type, ImportNotification draft)
        {
            AddressBook addressBook = await addressBookRepository.GetAddressBookForUser(userContext.UserId, type);

            switch (type)
            {
            case AddressRecordType.Facility:
                foreach (var facility in draft.Facilities.Facilities.Where(p => p.IsAddedToAddressBook))
                {
                    var facilityAddress = await FacilityAddressBookRecord(facility);

                    addressBook.Add(facilityAddress);
                }
                if (draft.Importer.IsAddedToAddressBook)
                {
                    var importerAddress = await ImporterAddressBookRecord(draft.Importer);

                    addressBook.Add(importerAddress);
                }
                break;

            case AddressRecordType.Producer:
                if (draft.Exporter.IsAddedToAddressBook)
                {
                    var exporter = await ExporterAddressBookRecord(draft.Exporter);

                    addressBook.Add(exporter);
                }

                if (draft.Producer.IsAddedToAddressBook)
                {
                    var producer = await ProducerAddressBookRecord(draft.Producer);

                    addressBook.Add(producer);
                }

                break;

            case AddressRecordType.Carrier:
            default:
                break;
            }
            await addressBookRepository.Update(addressBook);
        }
예제 #21
0
        public async Task <ActionResult <ImportNotification> > DoImport([FromBody] CsvImportInfo importInfo)
        {
            var hasPermissions = true;

            if (!importInfo.CatalogId.IsNullOrEmpty())
            {
                var catalogs = await _catalogService.GetByIdsAsync(new[] { importInfo.CatalogId }, CategoryResponseGroup.Info.ToString());

                if (!catalogs.IsNullOrEmpty())
                {
                    hasPermissions = await CheckCatalogPermission(catalogs.First(), CatalogModuleConstants.Security.Permissions.Update);
                }
            }

            if (!hasPermissions)
            {
                return(Unauthorized());
            }

            var criteria = AbstractTypeFactory <CatalogSearchCriteria> .TryCreateInstance();

            criteria.CatalogIds = new[] { importInfo.CatalogId };

            var authorizationResult = await _authorizationService.AuthorizeAsync(User, criteria, new CatalogAuthorizationRequirement(CatalogModuleConstants.Security.Permissions.Update));

            if (!authorizationResult.Succeeded)
            {
                return(Unauthorized());
            }


            var notification = new ImportNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Import catalog from CSV",
                Description = "starting import...."
            };
            await _notifier.SendAsync(notification);

            BackgroundJob.Enqueue(() => BackgroundImport(importInfo, notification));

            return(Ok(notification));
        }
예제 #22
0
        public void CanCreateImportNotification()
        {
            var import = new ImportNotification(NotificationType.Recovery, UKCompetentAuthority.England, "DE 001 12345");

            Assert.NotNull(import);
        }
예제 #23
0
        public virtual void DoImport(StoreImportConfiguration importConfiguration, ImportNotification notification)
        {
            if (string.IsNullOrEmpty(importConfiguration.FileUrl))
            {
                throw new Exception("FileUrl is null or empty. Can't import.");
            }
            //Notification
            notification.Description = "loading ...";
            _notifier.Upsert(notification);

            try
            {
                var backup = new Backup(_blobStorageProvider, null);
                backup.OpenBackup(importConfiguration.FileUrl);

                // Extract objects from backup
                var store = backup.LoadObject <Store>(_stopeXmlName);
                if (store != null)
                {
                    store.Languages       = backup.LoadObject <string[]>(_storeLanguagesXmlName);
                    store.Currencies      = backup.LoadObject <CurrencyCodes[]>(_storeCurrenciesXmlName);
                    store.SeoInfos        = backup.LoadObject <SeoInfo[]>(_storeSeoXmlName);
                    store.PaymentMethods  = backup.LoadObjectsByMask <PaymentMethod>(_paymentMethodXmlNamePrefix).ToArray();
                    store.ShippingMethods = backup.LoadObjectsByMask <ShippingMethod>(_shippingMethodXmlNamePrefix).ToArray();
                    var settings = backup.LoadObject <SettingEntry[]>(_settingsXmlName);

                    var storeSettings = settings.Where(x => x.ObjectId == store.Id).ToArray();

                    // Clear ids of collections to prevent dublicate ids
                    //todo check exists or not added payment and shipping modules
                    if (store.PaymentMethods != null)
                    {
                        store.PaymentMethods.ForEach(x => x.Id = null);
                    }
                    if (store.ShippingMethods != null)
                    {
                        store.ShippingMethods.ForEach(x => x.Id = null);
                    }
                    if (store.SeoInfos != null)
                    {
                        store.SeoInfos.ForEach(x => x.Id = null);
                    }


                    if (!string.IsNullOrEmpty(importConfiguration.NewStoreName))
                    {
                        store.Name = importConfiguration.NewStoreName;
                    }
                    if (!string.IsNullOrEmpty(importConfiguration.NewStoreId))
                    {
                        store.Id = importConfiguration.NewStoreId;
                    }
                    if (_storeService.GetById(store.Id) != null)
                    {
                        //todo change generation code
                        store.Id = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture).GenerateSlug();
                    }
                    SaveStore(store);

                    storeSettings.ForEach(x => x.ObjectId = store.Id);
                    _settingsManager.SaveSettings(storeSettings);
                }

                backup.CloseBackup();
            }
            catch (Exception ex)
            {
                notification.Description = "Export error";
                notification.ErrorCount++;
                notification.Errors.Add(ex.ToString());
            }
            finally
            {
                notification.Finished    = DateTime.UtcNow;
                notification.Description = "Import finished"
                                           + (notification.Errors.Any() ? " with errors" : " successfully");
                _notifier.Upsert(notification);
            }
        }
예제 #24
0
        private void SaveCategoryTree(coreModel.Catalog catalog, IEnumerable <coreModel.CatalogProduct> csvProducts, ImportNotification notification)
        {
            var categories = new List <coreModel.Category>();

            notification.ProcessedCount = 0;
            var cachedCategoryMap = new Dictionary <string, Category>();

            foreach (var csvProduct in csvProducts.Where(x => x.Category != null && !String.IsNullOrEmpty(x.Category.Path)))
            {
                var    outline = "";
                var    productCategoryNames = csvProduct.Category.Path.Split(_categoryDelimiters);
                string parentCategoryId     = null;
                foreach (var categoryName in productCategoryNames)
                {
                    outline += "\\" + categoryName;
                    Category category;
                    if (!cachedCategoryMap.TryGetValue(outline, out category))
                    {
                        var searchCriteria = new SearchCriteria
                        {
                            CatalogId     = catalog.Id,
                            CategoryId    = parentCategoryId,
                            ResponseGroup = ResponseGroup.WithCategories
                        };
                        category = _searchService.Search(searchCriteria).Categories.FirstOrDefault(x => x.Name == categoryName);
                    }

                    if (category == null)
                    {
                        category = _categoryService.Create(new coreModel.Category()
                        {
                            Name = categoryName, Code = categoryName.GenerateSlug(), CatalogId = catalog.Id, ParentId = parentCategoryId
                        });
                        //Raise notification each notifyCategorySizeLimit category
                        notification.Description = string.Format("Creating categories: {0} created", ++notification.ProcessedCount);
                        _notifier.Upsert(notification);
                    }
                    csvProduct.CategoryId      = category.Id;
                    csvProduct.Category        = category;
                    parentCategoryId           = category.Id;
                    cachedCategoryMap[outline] = category;
                }
            }
        }
예제 #25
0
        private void SaveProducts(coreModel.Catalog catalog, IEnumerable <coreModel.CatalogProduct> csvProducts, ImportNotification notification)
        {
            int counter = 0;
            var notifyProductSizeLimit = 10;

            notification.TotalCount = csvProducts.Count();

            var defaultFulfilmentCenter = _commerceService.GetAllFulfillmentCenters().FirstOrDefault();
            var options = new ParallelOptions()
            {
                MaxDegreeOfParallelism = 10
            };

            DetectParents(csvProducts);

            //First need create main products, second will be created variations
            foreach (var group in new IEnumerable <coreModel.CatalogProduct>[] { csvProducts.Where(x => x.MainProduct == null), csvProducts.Where(x => x.MainProduct != null) })
            {
                Parallel.ForEach(group, options, csvProduct =>
                {
                    try
                    {
                        SaveProduct(catalog, defaultFulfilmentCenter, csvProduct);
                    }
                    catch (Exception ex)
                    {
                        lock (_lockObject)
                        {
                            notification.ErrorCount++;
                            notification.Errors.Add(ex.ToString());
                            _notifier.Upsert(notification);
                        }
                    }
                    finally
                    {
                        lock (_lockObject)
                        {
                            //Raise notification each notifyProductSizeLimit category
                            counter++;
                            notification.ProcessedCount = counter;
                            notification.Description    = string.Format("Creating products: {0} of {1} created", notification.ProcessedCount, notification.TotalCount);
                            if (counter % notifyProductSizeLimit == 0)
                            {
                                _notifier.Upsert(notification);
                            }
                        }
                    }
                });
            }
            ;
        }
예제 #26
0
        public virtual void DoImport(webModel.CsvImportConfiguration configuration, ImportNotification notification)
        {
            var csvProducts = new List <coreModel.CatalogProduct>();
            var catalog     = _catalogService.GetById(configuration.CatalogId);

            try
            {
                using (var reader = new CsvReader(new StreamReader(_blobStorageProvider.OpenReadOnly(configuration.FileUrl))))
                {
                    reader.Configuration.Delimiter = configuration.Delimiter;
                    var initialized = false;
                    while (reader.Read())
                    {
                        if (!initialized)
                        {
                            //Notification
                            notification.Description = "Configuration...";
                            _notifier.Upsert(notification);

                            //set import configuration based on mapping info
                            var productMap = new ProductMap(reader.FieldHeaders, configuration, catalog);
                            reader.Configuration.RegisterClassMap(productMap);
                            initialized = true;

                            //Notification
                            notification.Description = "Reading products from csv...";
                            _notifier.Upsert(notification);
                        }

                        try
                        {
                            var csvProduct = reader.GetRecord <coreModel.CatalogProduct>();
                            csvProducts.Add(csvProduct);
                        }
                        catch (Exception ex)
                        {
                            var error = ex.Message;
                            if (ex.Data.Contains("CsvHelper"))
                            {
                                error += ex.Data["CsvHelper"];
                            }
                            notification.ErrorCount++;
                            notification.Errors.Add(error);
                        }
                    }
                }

                SaveCategoryTree(catalog, csvProducts, notification);
                SaveProducts(catalog, csvProducts, notification);
            }
            catch (Exception ex)
            {
                notification.Description = "Export error";
                notification.ErrorCount++;
                notification.Errors.Add(ex.ToString());
            }
            finally
            {
                notification.Finished    = DateTime.UtcNow;
                notification.Description = "Import finished" + (notification.Errors.Any() ? " with errors" : " successfully");
                _notifier.Upsert(notification);
            }
        }
        public void CanCreateImportNotification()
        {
            var import = new ImportNotification(NotificationType.Recovery, UKCompetentAuthority.England, "DE 001 12345");

            Assert.NotNull(import);
        }