/// <summary> /// /// </summary> /// <param name="accDoc"></param> /// <param name="accountRow"></param> /// <param name="config"></param> private void StoreCustomer(AccountDocument accDoc, AccountDataset.AccountsRow accountRow, NorthwindConfig config, ref List<TransactionResult> result) { #region declaration AccountDataset.CustomersRow customerRow; AccountDataset account; CustomersTableAdapter tableAdapter; string columnName; int recordCount; bool newCustomer; string customerId; #endregion newCustomer = ((accDoc.Id == null) || (accDoc.Id == "")); try { if (newCustomer) customerId = GetNewCustomerID((string)accDoc.name.Value, config); else if (accDoc.Id.StartsWith(Constants.CustomerIdPrefix, StringComparison.InvariantCultureIgnoreCase)) customerId = accDoc.Id.Substring(Constants.CustomerIdPrefix.Length); else { result.Add(accDoc.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_AccountNotFound)); return; } } catch (Exception) { result.Add(accDoc.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_AccountNotFound)); return; } try { using (OleDbConnection connection = new OleDbConnection(config.ConnectionString)) { account = new AccountDataset(); tableAdapter = new CustomersTableAdapter(); tableAdapter.Connection = connection; if (newCustomer) { customerRow = account.Customers.NewCustomersRow(); customerRow.CustomerID = customerId; } else { recordCount = tableAdapter.FillBy(account.Customers, customerId); if (recordCount == 0) { accDoc.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_AccountNotFound); return; } customerRow = (AccountDataset.CustomersRow)account.Customers.Rows[0]; } for (int index = 0; index < accountRow.Table.Columns.Count; index++) { columnName = accountRow.Table.Columns[index].ColumnName; if (!customerRow.Table.Columns.Contains(columnName)) continue; if (columnName.StartsWith("Create", StringComparison.InvariantCultureIgnoreCase)) if ((accountRow[columnName].GetType().Equals(typeof(DBNull)))) continue; customerRow[columnName] = accountRow[columnName]; } if (newCustomer) account.Customers.AddCustomersRow(customerRow); tableAdapter.Update(account.Customers); accDoc.SetTransactionStatus(TransactionStatus.Success); SetIdentitiesForAccounts(Constants.CustomerIdPrefix + customerId, accDoc); accDoc.GetTransactionResult(ref result); } } catch (Exception addCustomerException) { result.Add(accDoc.SetTransactionStatus(TransactionStatus.FatalError, addCustomerException.ToString())); throw; } UpdateEmailsCollectionFromCustomer(customerId, accDoc.emails, config, ref result); }
/// <summary> /// The Delete verb deletes existing account and all subentities in the underlying data-store. /// /// Should no account instance be found in the data-store that matches the id of the document, /// the transaction result will set accordingly. /// </summary> /// <param name="doc">Document (incl. ID) containing the data to be stored</param> /// <param name="config">The configuration object</param> /// <returns>The transactionResult contais status information of the single transaction an also the nesed transactions of the subentities</returns> public override void Delete(Document doc, NorthwindConfig config, ref List<TransactionResult> result) { #region Declarations CustomersTableAdapter customers; SuppliersTableAdapter suppliers; DeleteHistoryTableAdapter deleteHistory; int sequenceId; string accountId ; string customerId = ""; int supplierID = 0; #endregion // get the account Id from the given document accountId = doc.Id == null ? "" : doc.Id; // get the northwind Supplier or customer id if possible if (doc.Id.StartsWith(Constants.CustomerIdPrefix)) customerId = accountId.Substring(Constants.CustomerIdPrefix.Length); else if (doc.Id.StartsWith(Constants.SupplierIdPrefix)) supplierID = Identity.GetId(accountId.Substring(Constants.SupplierIdPrefix.Length)); else { result.Add(doc.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_AccountTypeNotSupported)); return; } // get the logging sequence number sequenceId = config.SequenceNumber; using (OleDbConnection connection = new OleDbConnection(config.ConnectionString)) { try { // get Table Adpater and set the connecion deleteHistory = new DeleteHistoryTableAdapter(); deleteHistory.Connection = connection; if (supplierID > 0) { // get Table Adpater and set the connecion suppliers = new SuppliersTableAdapter(); suppliers.Connection = connection; // delete the supplier suppliers.DeleteQuery(supplierID); // logg the deleted supplier in the delete history deleteHistory.LogSuppliers(supplierID.ToString(), sequenceId, config.CrmUser); // set transaction status to success result.Add(doc.SetTransactionStatus(TransactionStatus.Success)); } else if (customerId.Length > 0) { // get Table Adpater and set the connecion customers = new CustomersTableAdapter(); customers.Connection = connection; // delete The customer customers.DeleteQuery(customerId); // logg the deleted Customer in the delete history deleteHistory.LogCustomers(customerId, sequenceId, config.CrmUser); // set transaction status to success result.Add(doc.SetTransactionStatus(TransactionStatus.Success)); } else // set transaction status result.Add(doc.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_AccountTypeNotSupported)); } catch (Exception deleteException) { #warning Check the error status. this occours if the account is not deletable, because of having orders // set transaction status and logg the error message result.Add(doc.SetTransactionStatus(TransactionStatus.UnRecoverableError, deleteException.ToString())); } } }