private IEnumerable <Product> GetMatchingProducts(String brandCode, String modelName, String colorCode) { var brandName = String.Empty; if (!PricatBrandMapping.TryGetValue(brandCode, out brandName)) { TraceError("There is no brand with the alias '{0}'.", brandCode); return(Enumerable.Empty <Product>()); } var vendorItemNumber = String.Format("{0} {1}", modelName, colorCode); // Get all products that match the brand, color code attribute and model name var products = Unit.Scope .Repository <Product>() .Include(product => product.Brand) .Include(product => product.ProductAttributeValues) .Include(product => product.ProductDescriptions) .Include(product => product.ProductMedias) .Include(product => product.RelatedProductsRelated) .GetAll(product => !product.IsConfigurable && product.Brand.Name == brandName && product.VendorItemNumber == vendorItemNumber) .ToArray(); if (!products.Any()) { TraceInformation("There are no products with the brand '{0} - {1}'.", brandName, vendorItemNumber); } return(products); }
private void ProcessMediaFiles(FtpClient client) { TraceVerbose("Processing media files..."); var fileNameMatchGroups = client.Files .Select(file => FileNameRegex.Match(file.FileName)) .Where(match => match.Success) .GroupBy(match => new { BrandCode = match.Groups["Brand"].Value, ColorCode = match.Groups["Color"].Value, ModelName = match.Groups["Model"].Value }) .ToArray(); var productRepository = Unit.Scope.Repository <Product>().Include(product => product.Brand); foreach (var fileNameMatchGroup in fileNameMatchGroups) { TraceVerbose("Processing '{0}{1} {2}'..." , fileNameMatchGroup.Key.BrandCode , fileNameMatchGroup.Key.ModelName , fileNameMatchGroup.Key.ColorCode); var brandCode = fileNameMatchGroup.Key.BrandCode; string brandName; if (!PricatBrandMapping.TryGetValue(brandCode, out brandName)) { TraceError("There is no brand with the alias '{0}'.", brandCode); continue; } var vendorItemNumber = String.Join(" ", fileNameMatchGroup.Key.ModelName, fileNameMatchGroup.Key.ColorCode); var configurableProduct = productRepository.GetSingle(product => product.IsConfigurable && product.Brand.Name == brandName && product.VendorItemNumber == vendorItemNumber); if (configurableProduct == null) { TraceWarning("There is no configurable product with the brand '{0}' with the vendor item number '{1}'.", brandName, vendorItemNumber); continue; } foreach (var fileNameMatch in fileNameMatchGroup) { var colorCode = fileNameMatch.Groups["Color"].Value.PadLeft(3, '0'); var remoteFileName = CombineUri(fileNameMatch.Value); TraceVerbose("Processing '{0}'...", remoteFileName); // The Parse-method is safe to use, because the FileNameRegex only matches digits. var sequence = Int32.Parse(fileNameMatch.Groups["Sequence"].Value); var localFileName = String.Format("{0}_{1}_{2}_{3}{4}" , fileNameMatchGroup.Key.BrandCode , fileNameMatchGroup.Key.ModelName , colorCode + PricatColors .Where(pricatColor => pricatColor.ColorCode == colorCode) .Select(pricatColor => "_" + pricatColor.Filter) .FirstOrDefault() ?? String.Empty , sequence , Path.GetExtension(fileNameMatch.Value)) .ToLower(); var relativeLocalFilePath = Path.Combine(RsoConstants.SubDirectoryProductMedia, localFileName.Substring(0, 1), localFileName.Substring(1, 1), localFileName); var absoluteLocalFilePath = Path.Combine(ConcentratorMediaDirectory, relativeLocalFilePath); EnsureLocalDirectory(Path.GetDirectoryName(absoluteLocalFilePath)); if (DownloadFile(client, remoteFileName, absoluteLocalFilePath)) { var newRemoteFile = SynchronizeProductMedia( new[] { configurableProduct }, relativeLocalFilePath, ImageMediaType, sequence, localFileName) ? CombineUri(SuccessDirectoryName, fileNameMatch.Value) : CombineUri(ErrorDirectoryName, fileNameMatch.Value); try { client.RenameFile(remoteFileName, newRemoteFile); } catch (Exception exception) { TraceError("Unable to move the remote file '{0}' to the backup directory '{1}'. {2}" , remoteFileName , newRemoteFile , exception.Message); } } } } }
private void ProcessMediaDirectories(FtpClient client) { TraceVerbose("Processing media directories..."); var directoryNameMatchGroups = client.Directories .Select(directoryName => DirectoryNameRegex.Match(directoryName)) .Where(match => match.Success) .GroupBy(match => new { BrandCode = match.Groups["Brand"].Value, ColorCode = match.Groups["Color"].Value, ModelName = match.Groups["Model"].Value }) .ToArray(); var productRepository = Unit.Scope.Repository <Product>().Include(product => product.Brand); foreach (var directoryNameMatchGroup in directoryNameMatchGroups) { TraceVerbose("Processing '{0}{1} {2}'..." , directoryNameMatchGroup.Key.BrandCode , directoryNameMatchGroup.Key.ModelName , directoryNameMatchGroup.Key.ColorCode); var brandCode = directoryNameMatchGroup.Key.BrandCode; string brandName; if (!PricatBrandMapping.TryGetValue(brandCode, out brandName)) { TraceError("There is no brand with the alias '{0}'.", brandCode); continue; } var vendorItemNumber = String.Join(" ", directoryNameMatchGroup.Key.ModelName, directoryNameMatchGroup.Key.ColorCode); var configurableProduct = productRepository.GetSingle(product => product.IsConfigurable && product.Brand.Name == brandName && product.VendorItemNumber == vendorItemNumber); if (configurableProduct == null) { TraceWarning("There is no configurable product with the brand '{0}' with the vendor item number '{1}'.", brandName, vendorItemNumber); continue; } foreach (var directoryNameMatch in directoryNameMatchGroup) { var remoteDirectory = directoryNameMatch.Value; client.Update(CombineUri(remoteDirectory, "images")); var fileNameSequenceLookup = client.Files .Select(file => DirectoryFileNameRegex.Match(file.FileName)) .Where(match => match.Success) .Select(match => new { FileName = match.Value, Sequence = match.Groups["Sequence"].Value.ParseToInt() }) .Where(item => item.Sequence.HasValue) .OrderBy(item => item.Sequence.Value) .ToDictionary(item => CombineUri(remoteDirectory, "images", item.FileName), item => item.Sequence.Value); var allFilesSuccessfullyDownloaded = true; var colorCode = directoryNameMatchGroup.Key.ColorCode.PadLeft(3, '0'); foreach (var remoteFileName in fileNameSequenceLookup.Keys) { TraceVerbose("Processing '{0}'...", remoteFileName); var sequence = fileNameSequenceLookup[remoteFileName]; var localFileName = String.Format("{0}_{1}_{2}_360_{3}{4}" , directoryNameMatchGroup.Key.BrandCode , directoryNameMatchGroup.Key.ModelName , colorCode + PricatColors .Where(pricatColor => pricatColor.ColorCode == colorCode) .Select(pricatColor => "_" + pricatColor.Filter) .FirstOrDefault() ?? String.Empty , sequence , Path.GetExtension(remoteFileName)) .ToLower(); var relativeLocalFilePath = Path.Combine(RsoConstants.SubDirectoryProductMedia, localFileName.Substring(0, 1), localFileName.Substring(1, 1), localFileName); var absoluteLocalFilePath = Path.Combine(ConcentratorMediaDirectory, relativeLocalFilePath); EnsureLocalDirectory(Path.GetDirectoryName(absoluteLocalFilePath)); if (!DownloadFile(client, remoteFileName, absoluteLocalFilePath) || !SynchronizeProductMedia(new[] { configurableProduct }, relativeLocalFilePath, Image360MediaType, sequence, localFileName)) { allFilesSuccessfullyDownloaded = false; } } if (allFilesSuccessfullyDownloaded) { TraceVerbose("Saving the product media..."); string newRemoteDirectory; try { Unit.Save(); newRemoteDirectory = CombineUri(SuccessDirectoryName, remoteDirectory); } catch (DataException exception) { TraceError("Unable to save the database changes. {0}", exception.InnerException.Message); newRemoteDirectory = CombineUri(ErrorDirectoryName, remoteDirectory); } try { client.RenameFile(remoteDirectory, newRemoteDirectory); } catch (Exception exception) { TraceError("Unable to move the remote directory '{0}' to the backup directory '{1}'. {2}" , remoteDirectory , newRemoteDirectory , exception.Message); } } } } }