private EmailsDocumentCollection GetEmailsCollectionFromCustomer(Emails emails, DeleteHistoryDataset history, Token lastToken, string crmUser) { EmailsDocumentCollection emailCollection = new EmailsDocumentCollection(); EmailDocument email; foreach (DeleteHistoryDataset.DeleteHistoryRow dr in history.DeleteHistory) { if (dr.DeleteUser == crmUser) continue; if (dr.DeleteID <= lastToken.SequenceNumber) continue; email = new EmailDocument(); email.Id = dr.Identity; email.LogState = LogState.Deleted; emailCollection.Add(email); } int index = 0; foreach (Emails.CustomerEmailsRow er in emails.CustomerEmails) { email = new EmailDocument(); if ((er.CreateUser != crmUser) && (er.CreateID > lastToken.SequenceNumber)) email.LogState = LogState.Created; else if ((er.ModifyUser != crmUser) && (er.ModifyID > lastToken.SequenceNumber)) email.LogState = LogState.Updated; email.Id = er.ID.ToString(); try { email.emailaddress.Value = er.Email; if (index == 0) email.type.Value = "Business"; if (index == 1) email.type.Value = "Private"; index++; } catch (StrongTypingException) { email.emailaddress.Value = null; } emailCollection.Add(email); } return emailCollection; }
private void UpdateEmailsCollectionFromCustomer(string customerId, EmailsDocumentCollection emailCollection, NorthwindConfig config, ref List<TransactionResult> result) { #region declarations int recordCount; Emails emails = new Emails(); CustomerEmailsTableAdapter tableAdapter; DeleteHistoryTableAdapter history = new DeleteHistoryTableAdapter(); Emails.CustomerEmailsRow row; OleDbCommand Cmd; object lastid; int newEmailID; #endregion using (OleDbConnection connection = new OleDbConnection(config.ConnectionString)) { connection.Open(); tableAdapter = new CustomerEmailsTableAdapter(); tableAdapter.Connection = connection; recordCount = tableAdapter.FillByCustomerId(emails.CustomerEmails, customerId); foreach (EmailDocument emailDocument in emailCollection) { if (emailDocument.HasNoLogStatus) continue; #region process deleted if (emailDocument.LogState == LogState.Deleted) { try { if (tableAdapter.Delete(Identity.GetId(emailDocument.Id)) > 0) { history.Connection = connection; history.LogCustomerEmails(emailDocument.Id, config.SequenceNumber, config.CrmUser, customerId); } result.Add(emailDocument.SetTransactionStatus(TransactionStatus.Success)); } catch (Exception deleteException) { #warning todo: check this result.Add(emailDocument.SetTransactionStatus(TransactionStatus.UnRecoverableError, deleteException.Message)); } continue; } #endregion if (emailDocument.emailaddress.IsNull) { emailDocument.ClearTransactionStatus(); continue; } #region process created if (emailDocument.LogState == LogState.Created) { if ((emailDocument.Id != null) && (emailDocument.Id.Length > 0)) continue; row = emails.CustomerEmails.NewCustomerEmailsRow(); row.CustomerID = customerId; row.Email = (string)emailDocument.emailaddress.Value; row.CreateID = config.SequenceNumber; row.CreateUser = config.CrmUser; row.ModifyID = config.SequenceNumber; row.ModifyUser = config.CrmUser; emails.CustomerEmails.AddCustomerEmailsRow(row); try { tableAdapter.Update(emails.CustomerEmails); Cmd = new OleDbCommand("SELECT @@IDENTITY", connection); lastid = Cmd.ExecuteScalar(); newEmailID = (int)lastid; emailDocument.Id = newEmailID.ToString(); result.Add(emailDocument.SetTransactionStatus(TransactionStatus.Success)); } catch (Exception addException) { #warning todo: check this result.Add(emailDocument.SetTransactionStatus(TransactionStatus.UnRecoverableError, addException.Message)); } continue; } #endregion #region process changed if (emailDocument.LogState == LogState.Updated) { row = emails.CustomerEmails.FindByID(Identity.GetId(emailDocument.Id)); if (row == null) { result.Add(emailDocument.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_EmailNotFound)); continue; } row.Email = (string)emailDocument.emailaddress.Value; row.ModifyID = config.SequenceNumber; row.ModifyUser = config.CrmUser; try { tableAdapter.Update(emails.CustomerEmails); result.Add(emailDocument.SetTransactionStatus(TransactionStatus.Success)); } catch (Exception updateException) { #warning todo: check this result.Add(emailDocument.SetTransactionStatus(TransactionStatus.UnRecoverableError, updateException.Message)); } continue; } #endregion emailDocument.ClearTransactionStatus(); } } }