Inheritance: global::System.ComponentModel.Component
Ejemplo n.º 1
0
        /// <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()));
                }
            }
        }
Ejemplo n.º 2
0
        private void StoreSupplier(AccountDocument accDoc, AccountDataset.AccountsRow accountRow, NorthwindConfig config, ref List<TransactionResult> result)
        {
            AccountDataset.SuppliersRow suppliersRow;
            AccountDataset account;
            SuppliersTableAdapter tableAdapter;
            string columnName;
            int recordCount;
            bool newSupplier = ((accDoc.Id == null) || (accDoc.Id == ""));

            int supplierId;

            if (newSupplier)
                supplierId = 0;
            else if (accDoc.Id.StartsWith(Constants.SupplierIdPrefix, StringComparison.InvariantCultureIgnoreCase))
                try
                {
                    supplierId = Convert.ToInt32(accDoc.Id.Substring(Constants.SupplierIdPrefix.Length));
                }
                catch (Exception)
                { supplierId = 0; }
            else
            {
                accDoc.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_AccountNotFound);
                return;
            }

            try
            {
                using (OleDbConnection connection = new OleDbConnection(config.ConnectionString))
                {

                        connection.Open();
                        account = new AccountDataset();

                        tableAdapter = new SuppliersTableAdapter();
                        tableAdapter.Connection = connection;
                        if (newSupplier)
                            suppliersRow = account.Suppliers.NewSuppliersRow();
                        else
                        {
                            recordCount = tableAdapter.FillBy(account.Suppliers, supplierId);
                            if (recordCount == 0)
                            {
                                accDoc.SetTransactionStatus(TransactionStatus.UnRecoverableError, Resources.ErrorMessages_AccountNotFound);
                                return;
                            }
                            suppliersRow = (AccountDataset.SuppliersRow)account.Suppliers.Rows[0];

                        }

                        for (int index = 0; index < accountRow.Table.Columns.Count; index++)
                        {

                            columnName = accountRow.Table.Columns[index].ColumnName;

                            if (!suppliersRow.Table.Columns.Contains(columnName))
                                continue;

                            suppliersRow[columnName] = accountRow[columnName];
                        }

                        if (newSupplier)
                            account.Suppliers.AddSuppliersRow(suppliersRow);

                        tableAdapter.Update(account.Suppliers);

                        if (newSupplier)
                        {
                            OleDbCommand Cmd = new OleDbCommand("SELECT @@IDENTITY", connection);

                            object lastid = Cmd.ExecuteScalar();
                            supplierId = (int)lastid;
                        }

                        accDoc.SetTransactionStatus(TransactionStatus.Success);

                        SetIdentitiesForAccounts(Constants.SupplierIdPrefix + supplierId.ToString(), accDoc);
                        accDoc.GetTransactionResult(ref result);

                }
            }
            catch (Exception addCustomerException)
            {
                accDoc.SetTransactionStatus(TransactionStatus.FatalError, addCustomerException.ToString());
                throw;
            }
        }