public ActionResult BulkCreate(int? templateId, int? callForProposalId, string bulkLoadEmails) { var emailsCreatedCount = 0; var notAddedCount = 0; Template template = null; CallForProposal callforProposal = null; var existingList = new List<string>(); var notAddedSb = new StringBuilder(); if (!_accessService.HasAccess(templateId, callForProposalId, CurrentUser.Identity.Name)) { Message = string.Format(StaticValues.Message_NoAccess, "that"); return this.RedirectToAction<HomeController>(a => a.Index()); } if (templateId.HasValue && templateId != 0) { template = Repository.OfType<Template>().GetNullableById(templateId.Value); existingList = _emailsforcallRepository.Queryable.Where(a => a.Template != null && a.Template == template).Select(a => a.Email).ToList(); } else if (callForProposalId.HasValue && callForProposalId != 0) { callforProposal = Repository.OfType<CallForProposal>().GetNullableById(callForProposalId.Value); existingList = _emailsforcallRepository.Queryable.Where(a => a.CallForProposal != null && a.CallForProposal == callforProposal).Select(a => a.Email).ToList(); } const string regexPattern = @"\b[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}\b"; // Find matches System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(bulkLoadEmails, regexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); // add each match foreach (System.Text.RegularExpressions.Match match in matches) { if (existingList.Contains(match.ToString().ToLower())) { notAddedCount++; notAddedSb.AppendLine(match.ToString().ToLower() + " == Email already exists in list"); } else { ModelState.Clear(); var emailsforcallToCreate = new EmailsForCall(match.ToString().ToLower()); emailsforcallToCreate.Template = template; emailsforcallToCreate.CallForProposal = callforProposal; emailsforcallToCreate.TransferValidationMessagesTo(ModelState); if (ModelState.IsValid) { _emailsforcallRepository.EnsurePersistent(emailsforcallToCreate); emailsCreatedCount++; existingList.Add(emailsforcallToCreate.Email); } else { notAddedCount++; var sbErr = new StringBuilder(); foreach (var result in ModelState.Values) { foreach (var errs in result.Errors) { sbErr.Append(errs.ErrorMessage); } } notAddedSb.AppendLine(string.Format("{0} == {1} \n", match.ToString().ToLower(), sbErr)); } } } if (notAddedCount > 0) { ModelState.Clear(); Message = string.Format("{0} EmailsForCall Created Successfully == {1} EmailsForCall Not Created", emailsCreatedCount, notAddedCount); var viewModel = EmailsForCallViewModel.Create(Repository, template, callforProposal); viewModel.BulkLoadEmails = notAddedSb.ToString(); return View(viewModel); } else { Message = string.Format("{0} EmailsForCall Created Successfully", emailsCreatedCount); return this.RedirectToAction(a => a.Index(templateId, callForProposalId)); } }
public ActionResult Create(int? templateId, int? callForProposalId, EmailsForCall emailsforcall) { Template template = null; CallForProposal callforProposal = null; if (!_accessService.HasAccess(templateId, callForProposalId, CurrentUser.Identity.Name)) { Message = string.Format(StaticValues.Message_NoAccess, "that"); return this.RedirectToAction<HomeController>(a => a.Index()); } if (templateId.HasValue && templateId != 0) { template = Repository.OfType<Template>().GetNullableById(templateId.Value); if(_emailsforcallRepository.Queryable.Where(a => a.Template != null && a.Template == template && a.Email == emailsforcall.Email).Any()) { ModelState.AddModelError("Email", string.Format(StaticValues.ModelError_AlreadyExists, "Email")); } } else if (callForProposalId.HasValue && callForProposalId != 0) { callforProposal = Repository.OfType<CallForProposal>().GetNullableById(callForProposalId.Value); if (_emailsforcallRepository.Queryable.Where(a => a.CallForProposal != null && a.CallForProposal == callforProposal && a.Email == emailsforcall.Email).Any()) { ModelState.AddModelError("Email", string.Format(StaticValues.ModelError_AlreadyExists, "Email")); } } var emailsforcallToCreate = new EmailsForCall(emailsforcall.Email.ToLower()); //TransferValues(emailsforcall, emailsforcallToCreate); emailsforcallToCreate.Template = template; emailsforcallToCreate.CallForProposal = callforProposal; emailsforcallToCreate.TransferValidationMessagesTo(ModelState); if (ModelState.IsValid) { _emailsforcallRepository.EnsurePersistent(emailsforcallToCreate); Message = string.Format(StaticValues.Message_CreatedSuccessfully, "Emails For Call"); return this.RedirectToAction(a => a.Index(templateId, callForProposalId)); } else { var viewModel = EmailsForCallViewModel.Create(Repository, template, callforProposal); viewModel.EmailsForCall = emailsforcallToCreate; Message = string.Format(StaticValues.Message_NotCreated, "Emails For Call"); return View(viewModel); } }