Esempio n. 1
0
        public IHttpActionResult GetImportInformation([FromUri] string fileUrl)
        {
            var retVal = new webModel.ImportInfo();

            using (var stream = _blobStorageProvider.OpenReadOnly(fileUrl))
            {
                retVal.ExportManifest = _platformExportManager.ReadPlatformExportManifest(stream);
                //First check platform compatibility
                if (!SemanticVersion.Parse(retVal.ExportManifest.PlatformVersion).IsCompatibleWith(PlatformVersion.CurrentVersion))
                {
                    throw new InvalidDataException("Imported platform version " + retVal.ExportManifest.PlatformVersion + " not compatible with current " + PlatformVersion.CurrentVersion.ToString());
                }
                foreach (var exportModuleInfo in retVal.ExportManifest.Modules)
                {
                    var installedModule = _packageService.GetModules().FirstOrDefault(x => exportModuleInfo.ModuleId == x.Id);
                    if (installedModule != null)
                    {
                        var moduleDescriptor = installedModule.ToWebModel();
                        retVal.Modules.Add(moduleDescriptor);
                        //Check compatibility
                        if (!SemanticVersion.Parse(moduleDescriptor.Version).IsCompatibleWith(SemanticVersion.Parse(installedModule.Version)))
                        {
                            moduleDescriptor.ValidationErrors.Add("Imported module version " + moduleDescriptor.Version + " not compatible with installed " + installedModule.Version);
                        }
                    }
                }
            }
            return(Ok(retVal));
        }
        public IHttpActionResult LoadExportManifest([FromUri] string fileUrl)
        {
            PlatformExportManifest retVal = null;

            using (var stream = _blobStorageProvider.OpenReadOnly(fileUrl))
            {
                retVal = _platformExportManager.ReadExportManifest(stream);
            }
            return(Ok(retVal));
        }
        public IHttpActionResult GetMappingConfiguration([FromUri] string fileUrl, [FromUri] string delimiter = ";")
        {
            var retVal = CsvProductMappingConfiguration.GetDefaultConfiguration();

            retVal.Delimiter = delimiter;

            //Read csv headers and try to auto map fields by name
            using (var reader = new CsvReader(new StreamReader(_blobStorageProvider.OpenReadOnly(fileUrl))))
            {
                reader.Configuration.Delimiter = delimiter;
                if (reader.Read())
                {
                    retVal.AutoMap(reader.FieldHeaders);
                }
            }

            return(Ok(retVal));
        }
 public Stream GetReportFile(string reportFileName)
 {
     return(_blobStorageProvider.OpenReadOnly(reportFileName));
 }
        public IHttpActionResult GetMappingConfiguration([FromUri] string fileUrl, [FromUri] string delimiter = ";")
        {
            var retVal = new CsvImportConfiguration
            {
                Delimiter = delimiter,
                FileUrl   = fileUrl
            };
            var mappingItems = new List <CsvImportMappingItem>();

            mappingItems.AddRange(ReflectionUtility.GetPropertyNames <coreModel.CatalogProduct>(x => x.Name, x => x.Category).Select(x => new CsvImportMappingItem {
                EntityColumnName = x, IsRequired = true
            }));

            mappingItems.AddRange(new string[] { "Sku", "ParentSku", "Review", "PrimaryImage", "AltImage", "SeoUrl", "SeoDescription", "SeoTitle",
                                                 "PriceId", "Price", "SalePrice", "Currency", "AllowBackorder", "Quantity", "FulfilmentCenterId" }
                                  .Select(x => new CsvImportMappingItem {
                EntityColumnName = x, IsRequired = false
            }));

            mappingItems.AddRange(ReflectionUtility.GetPropertyNames <coreModel.CatalogProduct>(x => x.Id, x => x.MainProductId, x => x.CategoryId, x => x.IsActive, x => x.IsBuyable, x => x.TrackInventory,
                                                                                                x => x.ManufacturerPartNumber, x => x.Gtin, x => x.MeasureUnit, x => x.WeightUnit, x => x.Weight,
                                                                                                x => x.Height, x => x.Length, x => x.Width, x => x.TaxType, x => x.ProductType, x => x.ShippingType,
                                                                                                x => x.Vendor, x => x.DownloadType, x => x.DownloadExpiration, x => x.HasUserAgreement).Select(x => new CsvImportMappingItem {
                EntityColumnName = x, IsRequired = false
            }));



            retVal.MappingItems = mappingItems.ToArray();


            //Read csv headers and try to auto map fields by name
            using (var reader = new CsvReader(new StreamReader(_blobStorageProvider.OpenReadOnly(fileUrl))))
            {
                reader.Configuration.Delimiter = delimiter;
                while (reader.Read())
                {
                    var csvColumns = reader.FieldHeaders;
                    retVal.CsvColumns = csvColumns;
                    //default columns mapping
                    if (csvColumns.Any())
                    {
                        foreach (var mappingItem in retVal.MappingItems)
                        {
                            var entityColumnName     = mappingItem.EntityColumnName;
                            var betterMatchCsvColumn = csvColumns.Select(x => new { csvColumn = x, distance = x.ComputeLevenshteinDistance(entityColumnName) })
                                                       .Where(x => x.distance < 2)
                                                       .OrderBy(x => x.distance)
                                                       .Select(x => x.csvColumn)
                                                       .FirstOrDefault();
                            if (betterMatchCsvColumn != null)
                            {
                                mappingItem.CsvColumnName = betterMatchCsvColumn;
                                mappingItem.CustomValue   = null;
                            }
                        }
                    }
                }
            }
            //All not mapped properties may be a product property
            retVal.PropertyCsvColumns = retVal.CsvColumns.Except(retVal.MappingItems.Where(x => x.CsvColumnName != null).Select(x => x.CsvColumnName)).ToArray();
            //Generate ETag for identifying csv format
            retVal.ETag = string.Join(";", retVal.CsvColumns).GetMD5Hash();
            return(Ok(retVal));
        }
Esempio n. 6
0
 /// <summary>
 /// Open zip file before extract
 /// </summary>
 /// <param name="fileUrl"></param>
 public void OpenBackup(string fileUrl)
 {
     _zipArchive = new ZipArchive(_blobStorageProvider.OpenReadOnly(fileUrl), ZipArchiveMode.Read, true);
     LoadBackupMap();
 }
        private BackupObject GetBackupObject(Action <ExportImportProgressInfo> progressCallback, bool loadBinaryData)
        {
            var progressInfo = new ExportImportProgressInfo {
                Description = "loading data..."
            };

            progressCallback(progressInfo);


            const ResponseGroup responseGroup = ResponseGroup.WithCatalogs | ResponseGroup.WithCategories | ResponseGroup.WithProducts;
            var searchResponse = _catalogSearchService.Search(new SearchCriteria {
                Count = int.MaxValue, GetAllCategories = true, Start = 0, ResponseGroup = responseGroup
            });

            var retVal = new BackupObject();

            progressInfo.Description = String.Format("{0} catalogs loading", searchResponse.Catalogs.Count());
            progressCallback(progressInfo);

            //Catalogs
            retVal.Catalogs = searchResponse.Catalogs.Select(x => _catalogService.GetById(x.Id)).ToList();

            progressInfo.Description = String.Format("{0} categories loading", searchResponse.Categories.Count());
            progressCallback(progressInfo);

            //Categories
            retVal.Categories = searchResponse.Categories.Select(x => _categoryService.GetById(x.Id)).ToList();
            //Products
            for (int i = 0; i < searchResponse.Products.Count(); i += 50)
            {
                var products = _itemService.GetByIds(searchResponse.Products.Skip(i).Take(50).Select(x => x.Id).ToArray(), ItemResponseGroup.ItemMedium | ItemResponseGroup.Variations | ItemResponseGroup.Seo);
                retVal.Products.AddRange(products);

                progressInfo.Description = String.Format("{0} of {1} products loaded", Math.Min(searchResponse.TotalCount, i), searchResponse.TotalCount);
                progressCallback(progressInfo);
            }
            //Binary data
            if (loadBinaryData)
            {
                var allImages = retVal.Products.SelectMany(x => x.Images);
                allImages = allImages.Concat(retVal.Categories.SelectMany(x => x.Images));
                allImages = allImages.Concat(retVal.Products.SelectMany(x => x.Variations).SelectMany(x => x.Images));

                var index            = 0;
                var progressTemplate = "{0} of " + allImages.Count() + " images downloading";
                foreach (var image in allImages)
                {
                    progressInfo.Description = String.Format(progressTemplate, index);
                    progressCallback(progressInfo);
                    try
                    {
                        image.BinaryData = _blobStorageProvider.OpenReadOnly(image.Url).ReadFully();
                    }
                    catch (Exception ex)
                    {
                        progressInfo.Errors.Add(ex.ToString());
                        progressCallback(progressInfo);
                    }
                    index++;
                }
            }

            //Properties
            var catalogsPropertiesIds   = retVal.Catalogs.SelectMany(x => _propertyService.GetCatalogProperties(x.Id)).Select(x => x.Id).ToArray();
            var categoriesPropertiesIds = retVal.Categories.SelectMany(x => _propertyService.GetCategoryProperties(x.Id)).Select(x => x.Id).ToArray();
            var propertiesIds           = catalogsPropertiesIds.Concat(categoriesPropertiesIds).Distinct().ToArray();

            progressInfo.Description = String.Format("{0} properties loading", propertiesIds.Count());
            progressCallback(progressInfo);

            retVal.Properties = propertiesIds.Select(x => _propertyService.GetById(x)).ToList();
            return(retVal);
        }
Esempio n. 8
0
 public Stream OpenReadOnly(string blobKey)
 {
     return(BlobStorageProvider.OpenReadOnly(blobKey));
 }