private int ProcessSlugs(ICollection <ImportRow <Product> > batch, ImportResult result) { var slugMap = new Dictionary <string, UrlRecord>(100); Func <string, UrlRecord> slugLookup = ((s) => { if (slugMap.ContainsKey(s)) { return(slugMap[s]); } return((UrlRecord)null); }); var entityName = typeof(Product).Name; foreach (var row in batch) { if (row.IsNew || row.NameChanged || row.ContainsKey("SeName")) { try { string seName = row.GetValue <string>("SeName"); seName = row.Entity.ValidateSeName(seName, row.Entity.Name, true, _urlRecordService, _seoSettings, extraSlugLookup: slugLookup); UrlRecord urlRecord = null; if (row.IsNew) { // dont't bother validating SeName for new entities. urlRecord = new UrlRecord { EntityId = row.Entity.Id, EntityName = entityName, Slug = seName, LanguageId = 0, IsActive = true, }; _rsUrlRecord.Insert(urlRecord); } else { urlRecord = _urlRecordService.SaveSlug(row.Entity, seName, 0); } if (urlRecord != null) { // a new record was inserted to the store: keep track of it for this batch. slugMap[seName] = urlRecord; } } catch (Exception ex) { result.AddWarning(ex.Message, row.GetRowInfo(), "SeName"); } } } // commit whole batch at once return(_rsUrlRecord.Context.SaveChanges()); }
private int ProcessProductManufacturers(ICollection <ImportRow <Product> > batch, ImportResult result) { _rsProductManufacturer.AutoCommitEnabled = false; ProductManufacturer lastInserted = null; foreach (var row in batch) { string manufacturerIds = row.GetValue <string>("ManufacturerIds"); if (manufacturerIds.HasValue()) { try { foreach (var id in manufacturerIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x.Trim()))) { if (_rsProductManufacturer.TableUntracked.Where(x => x.ProductId == row.Entity.Id && x.ManufacturerId == id).FirstOrDefault() == null) { // ensure that manufacturer exists var manufacturer = _manufacturerService.GetManufacturerById(id); if (manufacturer != null) { var productManufacturer = new ProductManufacturer() { ProductId = row.Entity.Id, ManufacturerId = manufacturer.Id, IsFeaturedProduct = false, DisplayOrder = 1 }; _rsProductManufacturer.Insert(productManufacturer); lastInserted = productManufacturer; } } } } catch (Exception ex) { result.AddWarning(ex.Message, row.GetRowInfo(), "ManufacturerIds"); } } } // commit whole batch at once var num = _rsProductManufacturer.Context.SaveChanges(); // Perf: notify only about LAST insertion and update if (lastInserted != null) { _eventPublisher.EntityInserted(lastInserted); } return(num); }
private int ProcessLocalizations(ICollection <ImportRow <Product> > batch, ImportResult result) { //_rsProductManufacturer.AutoCommitEnabled = false; //string lastInserted = null; var languages = _languageService.GetAllLanguages(true); foreach (var row in batch) { Product product = null; //get product try { product = _productService.GetProductById(row.Entity.Id); } catch (Exception ex) { result.AddWarning(ex.Message, row.GetRowInfo(), "ProcessLocalizations Product"); } foreach (var lang in languages) { string localizedName = row.GetValue <string>("Name[" + lang.UniqueSeoCode + "]"); string localizedShortDescription = row.GetValue <string>("ShortDescription[" + lang.UniqueSeoCode + "]"); string localizedFullDescription = row.GetValue <string>("FullDescription[" + lang.UniqueSeoCode + "]"); if (localizedName.HasValue()) { _localizedEntityService.SaveLocalizedValue(product, x => x.Name, localizedName, lang.Id); } if (localizedShortDescription.HasValue()) { _localizedEntityService.SaveLocalizedValue(product, x => x.ShortDescription, localizedShortDescription, lang.Id); } if (localizedFullDescription.HasValue()) { _localizedEntityService.SaveLocalizedValue(product, x => x.FullDescription, localizedFullDescription, lang.Id); } } } // commit whole batch at once var num = _rsProductManufacturer.Context.SaveChanges(); // Perf: notify only about LAST insertion and update //if (lastInserted != null) // _eventPublisher.EntityInserted(lastInserted); return(num); }
private async Task <int> ProcessProductCategories(ICollection <ImportRow <Product> > batch, ImportResult result) { _rsProductCategory.AutoCommitEnabled = false; ProductCategory lastInserted = null; foreach (var row in batch) { string categoryIds = row.GetValue <string>("CategoryIds"); if (categoryIds.HasValue()) { try { foreach (var id in categoryIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x.Trim()))) { if (_rsProductCategory.TableUntracked.Where(x => x.ProductId == row.Entity.Id && x.CategoryId == id).FirstOrDefault() == null) { // ensure that category exists var category = _categoryService.GetCategoryById(id); if (category != null) { var productCategory = new ProductCategory() { ProductId = row.Entity.Id, CategoryId = category.Id, IsFeaturedProduct = false, DisplayOrder = 1 }; _rsProductCategory.Insert(productCategory); lastInserted = productCategory; } } } } catch (Exception ex) { result.AddWarning(ex.Message, row.GetRowInfo(), "CategoryIds"); } } } // commit whole batch at once var t = await _rsProductCategory.Context.SaveChangesAsync(); // Perf: notify only about LAST insertion and update if (lastInserted != null) { _eventPublisher.EntityInserted(lastInserted); } return(t); }
public bool SetProperty <TProp>(ImportResult result, T target, Expression <Func <T, TProp> > prop, TProp defaultValue = default(TProp), Func <object, TProp> converter = null) { // TBD: (MC) do not check for perf reason? //CheckInitialized(); var pi = prop.ExtractPropertyInfo(); var propName = pi.Name; try { object value; if (this.TryGetValue(propName, out value)) { // source contains field value. Set it. TProp converted; if (converter != null) { converted = converter(value); } else { if (value.ToString().ToUpper().Equals("NULL")) { // prop is set "explicitly" to null. converted = default(TProp); } else { converted = value.Convert <TProp>(); } } return(target.TrySetPropertyValue(propName, converted)); } else { // source does not contain field data or it's empty... if (IsTransient && defaultValue != null) { // ...but the entity is new. In this case // set the default value if given. return(target.TrySetPropertyValue(propName, defaultValue)); } } } catch (Exception ex) { result.AddWarning("Conversion failed: " + ex.Message, this.GetRowInfo(), propName); } return(false); }
private void ProcessSlugs(ICollection <ImportRow <Product> > batch, ImportResult result) { foreach (var row in batch) { if (row.IsNew || row.NameChanged || row.ContainsKey("SeName")) { try { string seName = row.GetValue <string>("SeName"); _urlRecordService.SaveSlug(row.Entity, row.Entity.ValidateSeName(seName, row.Entity.Name, true), 0); } catch (Exception ex) { result.AddWarning(ex.Message, row.GetRowInfo(), "SeName"); } } } }
private void ProcessProductPictures(ICollection <ImportRow <Product> > batch, ImportResult result) { // true, cause pictures must be saved and assigned an id // prior adding a mapping. _rsProductPicture.AutoCommitEnabled = true; ProductPicture lastInserted = null; int equalPictureId = 0; foreach (var row in batch) { var pictures = new string[] { row.GetValue <string>("Picture1"), row.GetValue <string>("Picture2"), row.GetValue <string>("Picture3") }; int i = 0; try { for (i = 0; i < pictures.Length; i++) { var picture = pictures[i]; if (picture.IsEmpty() || !File.Exists(picture)) { continue; } var currentPictures = _rsProductPicture.TableUntracked.Expand(x => x.Picture).Where(x => x.ProductId == row.Entity.Id).Select(x => x.Picture).ToList(); var pictureBinary = _pictureService.FindEqualPicture(picture, currentPictures, out equalPictureId); if (pictureBinary != null && pictureBinary.Length > 0) { // no equal picture found in sequence var newPicture = _pictureService.InsertPicture(pictureBinary, "image/jpeg", _pictureService.GetPictureSeName(row.EntityDisplayName), true, true); if (newPicture != null) { var mapping = new ProductPicture() { ProductId = row.Entity.Id, PictureId = newPicture.Id, DisplayOrder = 1, }; _rsProductPicture.Insert(mapping); lastInserted = mapping; } } else { result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "Picture" + (i + 1).ToString()); } } } catch (Exception ex) { result.AddWarning(ex.Message, row.GetRowInfo(), "Picture" + (i + 1).ToString()); } } // Perf: notify only about LAST insertion and update if (lastInserted != null) { _eventPublisher.EntityInserted(lastInserted); } }
public ImportResult ImportSubscribers(Stream stream) { Guard.ArgumentNotNull(() => stream); var result = new ImportResult(); var toAdd = new List <NewsLetterSubscription>(); var toUpdate = new List <NewsLetterSubscription>(); var autoCommit = _subscriptionRepository.AutoCommitEnabled; var validateOnSave = _subscriptionRepository.Context.ValidateOnSaveEnabled; var autoDetectChanges = _subscriptionRepository.Context.AutoDetectChangesEnabled; var proxyCreation = _subscriptionRepository.Context.ProxyCreationEnabled; try { using (var reader = new StreamReader(stream)) { _subscriptionRepository.Context.ValidateOnSaveEnabled = false; _subscriptionRepository.Context.AutoDetectChangesEnabled = false; _subscriptionRepository.Context.ProxyCreationEnabled = false; while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line.IsEmpty()) { continue; } string[] tmp = line.Split(','); var email = ""; bool isActive = true; // parse if (tmp.Length == 1) { // "email" only email = tmp[0].Trim(); } else if (tmp.Length == 2) { // "email" and "active" fields specified email = tmp[0].Trim(); isActive = Boolean.Parse(tmp[1].Trim()); } else { throw new InSearchException("Wrong file format (expected comma separated entries 'Email' and optionally 'IsActive')"); } result.TotalRecords++; if (email.Length > 255) { result.AddWarning("The email address '{0}' exceeds the maximum allowed length of 255.".FormatInvariant(email)); continue; } if (!email.IsEmail()) { result.AddWarning("'{0}' is not a valid email address.".FormatInvariant(email)); continue; } // import var subscription = (from nls in _subscriptionRepository.Table where nls.Email == email orderby nls.Id select nls).FirstOrDefault(); if (subscription != null) { subscription.Active = isActive; toUpdate.Add(subscription); result.ModifiedRecords++; } else { subscription = new NewsLetterSubscription() { Active = isActive, CreatedOnUtc = DateTime.UtcNow, Email = email, NewsLetterSubscriptionGuid = Guid.NewGuid() }; toAdd.Add(subscription); result.NewRecords++; } } } // insert new subscribers _subscriptionRepository.AutoCommitEnabled = true; _subscriptionRepository.InsertRange(toAdd, 500); toAdd.Clear(); // update modified subscribers _subscriptionRepository.AutoCommitEnabled = false; toUpdate.Each(x => { _subscriptionRepository.Update(x); }); _subscriptionRepository.Context.SaveChanges(); toUpdate.Clear(); } catch (Exception ex) { throw ex; } finally { _subscriptionRepository.AutoCommitEnabled = autoCommit; _subscriptionRepository.Context.ValidateOnSaveEnabled = validateOnSave; _subscriptionRepository.Context.AutoDetectChangesEnabled = autoDetectChanges; _subscriptionRepository.Context.ProxyCreationEnabled = proxyCreation; } return(result); }