/// <summary> /// Looks up product by <paramref name="gtin"/> locally and merges information with products in <paramref name="productsToMerge"/>. /// </summary> /// <param name="gtin">Global Trade Item Number</param> /// <param name="productsToMerge"></param> /// <remarks>Information is not added up. If local product has data in "TableOfContent" property then this is used, otherwise ToC /// from next product and so on.</remarks> /// <returns></returns> public Product FindProductAndMerge(string gtin, IList<Product> productsToMerge) { Log.Debug("FindProductAndMerge - {0}.", gtin); var myProduct = FindProductByGtinInOwnDatabase(gtin, true); if (myProduct == null) { Log.Debug("No product for GTIN {0} found in own database.", gtin); //TODO should we add an empty product into our DB here? myProduct = new Product { GlobalTradeItemNumber = gtin, LastUpdated = DateTime.Now }; //_productRepository.Add(myProduct);_productRepository.Persist(); } else { Log.Debug("Product with id {0} found for GTIN {1}", myProduct.Id, gtin); } Product mergedProduct; if (productsToMerge != null && productsToMerge.Any()) { Log.Debug("Merging {0} products.", productsToMerge.Count); mergedProduct = new Product { Id = myProduct.Id, GlobalTradeItemNumber = myProduct.GlobalTradeItemNumber, LastUpdated = myProduct.LastUpdated }; productsToMerge.Insert(0, myProduct); PropertyInfo[] properties = typeof (Product).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo p in properties) { // Only work with strings if (p.PropertyType != typeof (string)) { continue; } // If not writable then cannot null it; if not readable then cannot check it's value if (!p.CanWrite || !p.CanRead) { continue; } MethodInfo mget = p.GetGetMethod(false); MethodInfo mset = p.GetSetMethod(false); // Get and set methods have to be public if (mget == null) { continue; } if (mset == null) { continue; } foreach (var product in productsToMerge.Where(prod => prod != null)) { if (!string.IsNullOrEmpty((string) p.GetValue(product, null))) { p.SetValue(mergedProduct, p.GetValue(product, null), null); } } } foreach (var product in productsToMerge.Where(p => p != null)) { if (mergedProduct.Quantity == null) { mergedProduct.Quantity = product.Quantity; } if (mergedProduct.Brand == null) { mergedProduct.Brand = product.Brand; } else if (string.IsNullOrEmpty(mergedProduct.Brand.BrandName) && product.Brand != null) { mergedProduct.Brand.BrandName = product.Brand.BrandName; } if (mergedProduct.Brand != null && product.Brand != null && mergedProduct.Brand.Owner == null) { mergedProduct.Brand.Owner = product.Brand.Owner; } if (mergedProduct.ProductAdvices.Count == 0) { mergedProduct.ProductAdvices = product.ProductAdvices.ToList(); } if (mergedProduct.AllergyInformation.Count == 0) { mergedProduct.AllergyInformation = product.AllergyInformation; } if (mergedProduct.CertificationMarks.Count == 0) { foreach (var certificationMark in product.CertificationMarks) { mergedProduct.AddCertificationMark(certificationMark); } } if (mergedProduct.Ingredients.Count == 0) { foreach (var ingredient in product.Ingredients) { mergedProduct.AddIngredient(ingredient); } } if (mergedProduct.ProductCategory == null) { mergedProduct.ProductCategory = product.ProductCategory; } } } else { Log.Debug("No products to merge."); mergedProduct = myProduct; } RemoveNonpublishedAdvices(mergedProduct); if (string.IsNullOrEmpty(mergedProduct.ImageUrlSmall)) { mergedProduct.ImageUrlSmall = @"http://shopgun.se/images/productimages/missing_image_66.jpg"; } if (string.IsNullOrEmpty(mergedProduct.ImageUrlMedium)) { mergedProduct.ImageUrlMedium = @"http://shopgun.se/images/productimages/missing_image_150.jpg"; } if (string.IsNullOrEmpty(mergedProduct.ImageUrlLarge)) { mergedProduct.ImageUrlLarge = @"http://shopgun.se/images/productimages/missing_image_300.jpg"; } return mergedProduct; }
/// <summary> /// Looks up product by <paramref name="gtin"/> locally and merges information with products in <paramref name="productsToMerge"/>. /// </summary> /// <param name="gtin">Global Trade Item Number</param> /// <param name="productsToMerge"></param> /// <remarks>Information is not added up. If local product has data in "TableOfContent" property then this is used, otherwise ToC /// from next product and so on.</remarks> /// <returns></returns> public Product FindProductAndMerge(string gtin, IList <Product> productsToMerge) { Log.Debug("FindProductAndMerge - {0}.", gtin); var myProduct = FindProductByGtinInOwnDatabase(gtin, true); if (myProduct == null) { Log.Debug("No product for GTIN {0} found in own database.", gtin); //TODO should we add an empty product into our DB here? myProduct = new Product { GlobalTradeItemNumber = gtin, LastUpdated = DateTime.Now }; //_productRepository.Add(myProduct);_productRepository.Persist(); } else { Log.Debug("Product with id {0} found for GTIN {1}", myProduct.Id, gtin); } Product mergedProduct; if (productsToMerge != null && productsToMerge.Any()) { Log.Debug("Merging {0} products.", productsToMerge.Count); mergedProduct = new Product { Id = myProduct.Id, GlobalTradeItemNumber = myProduct.GlobalTradeItemNumber, LastUpdated = myProduct.LastUpdated }; productsToMerge.Insert(0, myProduct); PropertyInfo[] properties = typeof(Product).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo p in properties) { // Only work with strings if (p.PropertyType != typeof(string)) { continue; } // If not writable then cannot null it; if not readable then cannot check it's value if (!p.CanWrite || !p.CanRead) { continue; } MethodInfo mget = p.GetGetMethod(false); MethodInfo mset = p.GetSetMethod(false); // Get and set methods have to be public if (mget == null) { continue; } if (mset == null) { continue; } foreach (var product in productsToMerge.Where(prod => prod != null)) { if (!string.IsNullOrEmpty((string)p.GetValue(product, null))) { p.SetValue(mergedProduct, p.GetValue(product, null), null); } } } foreach (var product in productsToMerge.Where(p => p != null)) { if (mergedProduct.Quantity == null) { mergedProduct.Quantity = product.Quantity; } if (mergedProduct.Brand == null) { mergedProduct.Brand = product.Brand; } else if (string.IsNullOrEmpty(mergedProduct.Brand.BrandName) && product.Brand != null) { mergedProduct.Brand.BrandName = product.Brand.BrandName; } if (mergedProduct.Brand != null && product.Brand != null && mergedProduct.Brand.Owner == null) { mergedProduct.Brand.Owner = product.Brand.Owner; } if (mergedProduct.ProductAdvices.Count == 0) { mergedProduct.ProductAdvices = product.ProductAdvices.ToList(); } if (mergedProduct.AllergyInformation.Count == 0) { mergedProduct.AllergyInformation = product.AllergyInformation; } if (mergedProduct.CertificationMarks.Count == 0) { foreach (var certificationMark in product.CertificationMarks) { mergedProduct.AddCertificationMark(certificationMark); } } if (mergedProduct.Ingredients.Count == 0) { foreach (var ingredient in product.Ingredients) { mergedProduct.AddIngredient(ingredient); } } if (mergedProduct.ProductCategory == null) { mergedProduct.ProductCategory = product.ProductCategory; } } } else { Log.Debug("No products to merge."); mergedProduct = myProduct; } RemoveNonpublishedAdvices(mergedProduct); if (string.IsNullOrEmpty(mergedProduct.ImageUrlSmall)) { mergedProduct.ImageUrlSmall = @"http://shopgun.se/images/productimages/missing_image_66.jpg"; } if (string.IsNullOrEmpty(mergedProduct.ImageUrlMedium)) { mergedProduct.ImageUrlMedium = @"http://shopgun.se/images/productimages/missing_image_150.jpg"; } if (string.IsNullOrEmpty(mergedProduct.ImageUrlLarge)) { mergedProduct.ImageUrlLarge = @"http://shopgun.se/images/productimages/missing_image_300.jpg"; } return(mergedProduct); }