Ejemplo n.º 1
0
        public async Task <string> GetParentPath(CommerceContext commerceContext, string parentSitecoreId, string input)
        {
            ItemModel parent = await manager.GetItemByIdAsync(commerceContext, parentSitecoreId);

            if (parent?["ParentCategoryList"] == null || string.IsNullOrWhiteSpace(parent["ParentCategoryList"].ToString()))
            {
                return(input);
            }

            string output = $"/{parent["DisplayName"]}{input}";

            return(await GetParentPath(commerceContext, parent["ParentCategoryList"].ToString(), output));
        }
Ejemplo n.º 2
0
        private async Task <string> GetCategoryIdPath(string categoryId, string input)
        {
            //Place parent path before the current children output
            string output = $"/{categoryId}{input}";

            ItemModel item = await _manager.GetItemByIdAsync(_commerceContext, categoryId);

            object parentCategoryList = item["ParentCategoryList"];

            if (parentCategoryList == null)
            {
                return(output);
            }

            string parentCategoryId = parentCategoryList.ToString();

            if (string.IsNullOrEmpty(parentCategoryId))
            {
                return(output);
            }

            return(await GetCategoryIdPath(parentCategoryId, output));
        }
        public override async Task <MinionRunResultsModel> Run(MinionRunResultsModel arg, CommercePipelineExecutionContext context)
        {
            SitecoreConnectionManager connection = new SitecoreConnectionManager();
            var stiboImportSettingItem           = await connection.GetItemByPathAsync(context.CommerceContext, ProductContsants.StiboImportSettingPath);

            if (stiboImportSettingItem == null)
            {
                context.Logger.LogInformation($"Minion-ProductImport: Could not find stibo import setting at location: {ProductContsants.StiboImportSettingPath}");
                return(arg);
            }

            Host     = (string)stiboImportSettingItem.GetFieldValue(ProductContsants.SFTPHostField);
            Username = (string)stiboImportSettingItem.GetFieldValue(ProductContsants.SFTPUsernameField);
            Password = (string)stiboImportSettingItem.GetFieldValue(ProductContsants.SFTPPasswordField);

            BaseDirectory      = (string)stiboImportSettingItem.GetFieldValue(ProductContsants.BaseDirectoryField);
            CompletedDirectory = (string)stiboImportSettingItem.GetFieldValue(ProductContsants.CompletedDirectoryField);
            FailedDirectory    = (string)stiboImportSettingItem.GetFieldValue(ProductContsants.FailedDirectoryField);

            var BrandRegions = (string)stiboImportSettingItem.GetFieldValue(ProductContsants.BrandRegionMappingField);

            if (string.IsNullOrWhiteSpace(BrandRegions))
            {
                context.Logger.LogInformation($"Minion-ProductImport: Brand settings could not found on stibo import setting: {ProductContsants.StiboImportSettingPath}");
                return(arg);
            }

            foreach (var id in BrandRegions.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                var brandRegionImportSettingItem = await connection.GetItemByIdAsync(context.CommerceContext, id);

                if (brandRegionImportSettingItem == null)
                {
                    context.Logger.LogInformation($"Minion-ProductImport: Could not find brand settings for stibo import. ID: {id}");
                    continue;
                }

                var CatalogName           = (string)brandRegionImportSettingItem.GetFieldValue(ProductContsants.CatalogNameField);
                var BrandDirectory        = (string)brandRegionImportSettingItem.GetFieldValue(ProductContsants.BrandDirectoryField);
                var RegionLanguagePattern = (string)brandRegionImportSettingItem.GetFieldValue(ProductContsants.RegionLanguagePatternField);

                context.Logger.LogInformation($"Minion-ProductImport: Product import started for Brand: {BrandDirectory}, CatalogName: {CatalogName}, Region/Language: {RegionLanguagePattern}");

                using (SftpClient sftp = new SftpClient(Host, Username, Password))
                {
                    try
                    {
                        context.Logger.LogInformation($"Minion-ProductImport: Download started");

                        sftp.Connect();

                        var files = sftp.ListDirectory($"{BaseDirectory}/{BrandDirectory}");

                        CleanupDirectory(BrandDirectory, context);

                        foreach (var file in files)
                        {
                            if (file.IsDirectory || file.IsSymbolicLink)
                            {
                                continue;
                            }

                            //Ignore other region/language files for same brand
                            if (!file.Name.ToLower().Contains(RegionLanguagePattern.ToLower()))
                            {
                                continue;
                            }

                            ////Temp
                            //if (file.Name != "Grohe-Context1##1607423-1607845_0.xml")
                            //    continue;

                            try
                            {
                                using (Stream fileStream = File.OpenWrite(this.GetFullPath(BrandDirectory + "/" + file.Name)))
                                {
                                    sftp.DownloadFile(file.FullName, fileStream);
                                }
                            }
                            catch (Exception ex)
                            {
                                context.Logger.LogError($"Minion-ProductImport: Error while downloading file from SFTP {file.FullName}" + ex.Message);
                            }
                        }

                        sftp.Disconnect();

                        context.Logger.LogInformation($"Minion-ProductImport: Download completed");

                        var directoryPath = this.GetFullPath(BrandDirectory);
                        var filePaths     = Directory.GetFiles(directoryPath).ToList();
                        foreach (var fileFullName in filePaths)
                        {
                            try
                            {
                                context.Logger.LogInformation($"Minion-ProductImport: Import started");

                                var model = new ImportSellableItemPipelineArgument("");
                                //Call selleble item import pipeline
                                model.XMlFilePath        = fileFullName;
                                model.CatalogName        = CatalogName;
                                model.CatalogDisplayName = CatalogName;
                                var result = await _iImportProuctPipeline.Run(model, context);

                                context.Logger.LogInformation($"Minion-ProductImport: Import completed");

                                context.Logger.LogInformation($"Minion-ProductImport: Moving files to completed/failed started");
                                //Move file to completed/failed folder
                                sftp.Connect();

                                files = sftp.ListDirectory($"{BaseDirectory}/{BrandDirectory}");
                                var fileName = fileFullName.Substring(fileFullName.LastIndexOf("\\") + 1);
                                foreach (var f in files)
                                {
                                    if (f.Name != fileName)
                                    {
                                        continue;
                                    }

                                    if (result.FileImportSuccess)
                                    {
                                        f.MoveTo($"{BaseDirectory}/{BrandDirectory}/{CompletedDirectory}/{f.Name}");
                                    }
                                    else
                                    {
                                        f.MoveTo($"{BaseDirectory}/{BrandDirectory}/{FailedDirectory}/{f.Name}");
                                    }
                                }

                                sftp.Disconnect();

                                context.Logger.LogInformation($"Minion-ProductImport: Moving files to completed/failed completed");
                            }
                            catch (Exception ex)
                            {
                                context.Logger.LogError($"Minion-ProductImport: Error while importing/moving file: {fileFullName}, CatalogName: {CatalogName}" + ex.Message);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Logger.LogError($"Minion-ProductImport: Error while downloading/importing/moving " + ex.Message);
                    }
                }

                context.Logger.LogInformation($"Minion-ProductImport: Product import completed for Brand: {BrandDirectory}, CatalogName: {CatalogName}, Region/Language: {RegionLanguagePattern}");
            }

            return(arg);
        }