Exemple #1
0
        /// <summary>
        /// The main method of this class.  It deletes one or more rows selected in a grid on the caller form
        /// </summary>
        /// <param name="ACallerFormOrControl">The form or user control that is making the call.  The form must implement the IDeleteGridRows interface</param>
        /// <param name="AGrid">A reference to the grid object</param>
        /// <param name="APetraUtilsObject">A reference to the PetraUtilsObject associated with the form or control making the call</param>
        /// <param name="AButtonPanel">A reference a form or control that implements the IButtonPanel interface.  This parameter can be null.</param>
        /// <returns>True if any rows were actually deleted</returns>
        public static bool DeleteRows(IDeleteGridRows ACallerFormOrControl,
                                      TSgrdDataGrid AGrid,
                                      TFrmPetraEditUtils APetraUtilsObject,
                                      IButtonPanel AButtonPanel)
        {
            DataRow currentDataRow  = ACallerFormOrControl.GetSelectedDataRow();
            Int32   currentRowIndex = ACallerFormOrControl.GetSelectedRowIndex();

            if ((currentDataRow == null) || (currentRowIndex == -1))
            {
                return(false);
            }

            string CompletionMessage = String.Empty;

            DataRowView[] HighlightedRows = AGrid.SelectedDataRowsAsDataRowView;

            if (HighlightedRows.Length == 1)
            {
                // Single row deletion

                TVerificationResultCollection VerificationResults = null;

                if (TVerificationHelper.IsNullOrOnlyNonCritical(APetraUtilsObject.VerificationResultCollection))
                {
                    ACallerFormOrControl.GetReferenceCount(currentDataRow, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults);
                }

                if ((VerificationResults != null) &&
                    (VerificationResults.Count > 0))
                {
                    TCascadingReferenceCountHandler countHandler = new TCascadingReferenceCountHandler();
                    TFrmExtendedMessageBox.TResult  result       = countHandler.HandleReferences(APetraUtilsObject, VerificationResults, true);

                    if (result == TFrmExtendedMessageBox.TResult.embrYes)
                    {
                        // repeat the count but with no limit to the number of references
                        ACallerFormOrControl.GetReferenceCount(currentDataRow, 0, out VerificationResults);
                        countHandler.HandleReferences(APetraUtilsObject, VerificationResults, false);
                    }

                    return(false);
                }

                string DeletionQuestion  = ACallerFormOrControl.GetDefaultDeletionQuestion();
                bool   AllowDeletion     = true;
                bool   DeletionPerformed = false;

                ACallerFormOrControl.HandlePreDelete(currentDataRow, ref AllowDeletion, ref DeletionQuestion);

                if (AllowDeletion)
                {
                    if ((MessageBox.Show(DeletionQuestion,
                                         MCommonResourcestrings.StrConfirmDeleteTitle,
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Question,
                                         MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes))
                    {
                        try
                        {
                            if (!ACallerFormOrControl.HandleDeleteRow(currentDataRow, ref DeletionPerformed, ref CompletionMessage))
                            {
                                currentDataRow.Delete();
                                DeletionPerformed = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(String.Format(MCommonResourcestrings.StrErrorWhileDeleting,
                                                          Environment.NewLine, ex.Message),
                                            MCommonResourcestrings.StrGenericError,
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Warning);
                        }

                        if (DeletionPerformed)
                        {
                            APetraUtilsObject.SetChangedFlag();
                        }

                        // Select and display the details of the nearest row to the one previously selected
                        ACallerFormOrControl.SelectRowInGrid(currentRowIndex);
                        // Clear any errors left over from  the deleted row
                        APetraUtilsObject.VerificationResultCollection.Clear();

                        if (AButtonPanel != null)
                        {
                            AButtonPanel.UpdateRecordNumberDisplay();
                        }
                    }
                }

                if (!ACallerFormOrControl.HandlePostDelete(currentDataRow, AllowDeletion, DeletionPerformed, CompletionMessage))
                {
                    if (DeletionPerformed && (CompletionMessage.Length > 0))
                    {
                        MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle);
                    }
                }

                return(DeletionPerformed);
            }
            else
            {
                // Multi-row deletion

                int recordsDeleted = 0;

                string DeletionQuestion = String.Format(MCommonResourcestrings.StrMultiRowDeletionQuestion,
                                                        HighlightedRows.Length,
                                                        Environment.NewLine);
                DeletionQuestion += MCommonResourcestrings.StrMultiRowDeletionCheck;

                if (MessageBox.Show(DeletionQuestion,
                                    MCommonResourcestrings.StrConfirmDeleteTitle,
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    int recordsUndeletable                   = 0;
                    int recordsDeleteDisallowed              = 0;
                    List <TMultiDeleteResult> listConflicts  = new List <TMultiDeleteResult>();
                    List <TMultiDeleteResult> listExceptions = new List <TMultiDeleteResult>();

                    APetraUtilsObject.GetForm().Cursor = Cursors.WaitCursor;

                    foreach (DataRowView drv in HighlightedRows)
                    {
                        DataRow rowToDelete = drv.Row;

                        if (rowToDelete.RowState == DataRowState.Detached)
                        {
                            continue;
                        }

                        string rowDetails = MakePKValuesString(rowToDelete);

                        if (!ACallerFormOrControl.IsRowDeletable(rowToDelete))
                        {
                            recordsUndeletable++;
                            continue;
                        }

                        TVerificationResultCollection VerificationResults = null;
                        ACallerFormOrControl.GetReferenceCount(rowToDelete, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults);

                        if ((VerificationResults != null) && (VerificationResults.Count > 0))
                        {
                            TMultiDeleteResult result = new TMultiDeleteResult(rowDetails,
                                                                               Messages.BuildMessageFromVerificationResult(String.Empty, VerificationResults));
                            listConflicts.Add(result);
                            continue;
                        }

                        bool AllowDeletion     = true;
                        bool DeletionPerformed = false;

                        ACallerFormOrControl.HandlePreDelete(rowToDelete, ref AllowDeletion, ref DeletionQuestion);

                        if (AllowDeletion)
                        {
                            try
                            {
                                if (!ACallerFormOrControl.HandleDeleteRow(rowToDelete, ref DeletionPerformed, ref CompletionMessage))
                                {
                                    rowToDelete.Delete();
                                    DeletionPerformed = true;
                                }
                            }
                            catch (Exception ex)
                            {
                                TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, ex.Message);
                                listExceptions.Add(result);
                            }
                        }
                        else
                        {
                            recordsDeleteDisallowed++;
                        }

                        if (DeletionPerformed)
                        {
                            APetraUtilsObject.SetChangedFlag();
                            recordsDeleted++;
                        }

                        ACallerFormOrControl.HandlePostDelete(rowToDelete, AllowDeletion, DeletionPerformed, String.Empty);
                    }

                    APetraUtilsObject.GetForm().Cursor = Cursors.Default;
                    ACallerFormOrControl.SelectRowInGrid(currentRowIndex);

                    if (AButtonPanel != null)
                    {
                        AButtonPanel.UpdateRecordNumberDisplay();
                    }

                    if ((recordsDeleted > 0) && (CompletionMessage.Length > 0))
                    {
                        MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle);
                    }

                    //  Show the results of the multi-deletion
                    string results = null;

                    if (recordsDeleted > 0)
                    {
                        results = String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRecordSuccessfullyDeleted,
                                                    MCommonResourcestrings.StrRecordsSuccessfullyDeleted, recordsDeleted),
                            recordsDeleted);
                    }
                    else
                    {
                        results = MCommonResourcestrings.StrNoRecordsWereDeleted;
                    }

                    if (recordsUndeletable > 0)
                    {
                        results += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseNonDeletable,
                                                    MCommonResourcestrings.StrRowsNotDeletedBecauseNonDeletable,
                                                    recordsUndeletable),
                            Environment.NewLine,
                            recordsUndeletable);
                    }

                    if (recordsDeleteDisallowed > 0)
                    {
                        results += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseDeleteNotAllowed,
                                                    MCommonResourcestrings.StrRowsNotDeletedBecauseDeleteNotAllowed,
                                                    recordsDeleteDisallowed),
                            Environment.NewLine,
                            recordsDeleteDisallowed);
                    }

                    bool showCancel = false;

                    if (listConflicts.Count > 0)
                    {
                        showCancel = true;
                        results   += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseReferencedElsewhere,
                                                    MCommonResourcestrings.StrRowsNotDeletedBecauseReferencedElsewhere,
                                                    listConflicts.Count),
                            Environment.NewLine,
                            listConflicts.Count);
                    }

                    if (listExceptions.Count > 0)
                    {
                        showCancel = true;
                        results   += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException,
                                                    MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException,
                                                    listExceptions.Count),
                            Environment.NewLine,
                            listExceptions.Count);
                    }

                    if (showCancel)
                    {
                        results += String.Format(MCommonResourcestrings.StrClickToReviewDeletionOrCancel, Environment.NewLine);

                        if (MessageBox.Show(results,
                                            MCommonResourcestrings.StrDeleteActionSummaryTitle,
                                            MessageBoxButtons.OKCancel,
                                            MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK)
                        {
                            ReviewMultiDeleteResults(listConflicts, MCommonResourcestrings.StrRowsReferencedByOtherTables);
                            ReviewMultiDeleteResults(listExceptions, MCommonResourcestrings.StrExceptions);
                        }
                    }
                    else
                    {
                        MessageBox.Show(results,
                                        MCommonResourcestrings.StrDeleteActionSummaryTitle,
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                }

                return(recordsDeleted > 0);
            }
        }
        /// <summary>
        /// The main method of this class.  It deletes one or more rows selected in a grid on the caller form
        /// </summary>
        /// <param name="ACallerFormOrControl">The form or user control that is making the call.  The form must implement the IDeleteGridRows interface</param>
        /// <param name="AGrid">A reference to the grid object</param>
        /// <param name="APetraUtilsObject">A reference to the PetraUtilsObject associated with the form or control making the call</param>
        /// <param name="AButtonPanel">A reference a form or control that implements the IButtonPanel interface.  This parameter can be null.</param>
        /// <returns>True if any rows were actually deleted</returns>
        public static bool DeleteRows(IDeleteGridRows ACallerFormOrControl,
            TSgrdDataGrid AGrid,
            TFrmPetraEditUtils APetraUtilsObject,
            IButtonPanel AButtonPanel)
        {
            DataRow currentDataRow = ACallerFormOrControl.GetSelectedDataRow();
            Int32 currentRowIndex = ACallerFormOrControl.GetSelectedRowIndex();

            if ((currentDataRow == null) || (currentRowIndex == -1))
            {
                return false;
            }

            string CompletionMessage = String.Empty;
            DataRowView[] HighlightedRows = AGrid.SelectedDataRowsAsDataRowView;

            if (HighlightedRows.Length == 1)
            {
                // Single row deletion

                TVerificationResultCollection VerificationResults = null;

                if (TVerificationHelper.IsNullOrOnlyNonCritical(APetraUtilsObject.VerificationResultCollection))
                {
                    ACallerFormOrControl.GetReferenceCount(currentDataRow, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults);
                }

                if ((VerificationResults != null)
                    && (VerificationResults.Count > 0))
                {
                    TCascadingReferenceCountHandler countHandler = new TCascadingReferenceCountHandler();
                    TFrmExtendedMessageBox.TResult result = countHandler.HandleReferences(APetraUtilsObject, VerificationResults, true);

                    if (result == TFrmExtendedMessageBox.TResult.embrYes)
                    {
                        // repeat the count but with no limit to the number of references
                        ACallerFormOrControl.GetReferenceCount(currentDataRow, 0, out VerificationResults);
                        countHandler.HandleReferences(APetraUtilsObject, VerificationResults, false);
                    }

                    return false;
                }

                string DeletionQuestion = ACallerFormOrControl.GetDefaultDeletionQuestion();
                bool AllowDeletion = true;
                bool DeletionPerformed = false;

                ACallerFormOrControl.HandlePreDelete(currentDataRow, ref AllowDeletion, ref DeletionQuestion);

                if (AllowDeletion)
                {
                    if ((MessageBox.Show(DeletionQuestion,
                             MCommonResourcestrings.StrConfirmDeleteTitle,
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question,
                             MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes))
                    {
                        try
                        {
                            if (!ACallerFormOrControl.HandleDeleteRow(currentDataRow, ref DeletionPerformed, ref CompletionMessage))
                            {
                                currentDataRow.Delete();
                                DeletionPerformed = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(String.Format(MCommonResourcestrings.StrErrorWhileDeleting,
                                    Environment.NewLine, ex.Message),
                                MCommonResourcestrings.StrGenericError,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                        }

                        if (DeletionPerformed)
                        {
                            APetraUtilsObject.SetChangedFlag();
                        }

                        // Select and display the details of the nearest row to the one previously selected
                        ACallerFormOrControl.SelectRowInGrid(currentRowIndex);
                        // Clear any errors left over from  the deleted row
                        APetraUtilsObject.VerificationResultCollection.Clear();

                        if (AButtonPanel != null)
                        {
                            AButtonPanel.UpdateRecordNumberDisplay();
                        }
                    }
                }

                if (!ACallerFormOrControl.HandlePostDelete(currentDataRow, AllowDeletion, DeletionPerformed, CompletionMessage))
                {
                    if (DeletionPerformed && (CompletionMessage.Length > 0))
                    {
                        MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle);
                    }
                }

                return DeletionPerformed;
            }
            else
            {
                // Multi-row deletion

                int recordsDeleted = 0;

                string DeletionQuestion = String.Format(MCommonResourcestrings.StrMultiRowDeletionQuestion,
                    HighlightedRows.Length,
                    Environment.NewLine);
                DeletionQuestion += MCommonResourcestrings.StrMultiRowDeletionCheck;

                if (MessageBox.Show(DeletionQuestion,
                        MCommonResourcestrings.StrConfirmDeleteTitle,
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question,
                        MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    int recordsUndeletable = 0;
                    int recordsDeleteDisallowed = 0;
                    List <TMultiDeleteResult>listConflicts = new List <TMultiDeleteResult>();
                    List <TMultiDeleteResult>listExceptions = new List <TMultiDeleteResult>();

                    APetraUtilsObject.GetForm().Cursor = Cursors.WaitCursor;

                    foreach (DataRowView drv in HighlightedRows)
                    {
                        DataRow rowToDelete = drv.Row;
                        string rowDetails = MakePKValuesString(rowToDelete);

                        if (!ACallerFormOrControl.IsRowDeletable(rowToDelete))
                        {
                            recordsUndeletable++;
                            continue;
                        }

                        TVerificationResultCollection VerificationResults = null;
                        ACallerFormOrControl.GetReferenceCount(rowToDelete, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults);

                        if ((VerificationResults != null) && (VerificationResults.Count > 0))
                        {
                            TMultiDeleteResult result = new TMultiDeleteResult(rowDetails,
                                Messages.BuildMessageFromVerificationResult(String.Empty, VerificationResults));
                            listConflicts.Add(result);
                            continue;
                        }

                        bool AllowDeletion = true;
                        bool DeletionPerformed = false;

                        ACallerFormOrControl.HandlePreDelete(rowToDelete, ref AllowDeletion, ref DeletionQuestion);

                        if (AllowDeletion)
                        {
                            try
                            {
                                if (!ACallerFormOrControl.HandleDeleteRow(rowToDelete, ref DeletionPerformed, ref CompletionMessage))
                                {
                                    rowToDelete.Delete();
                                    DeletionPerformed = true;
                                }
                            }
                            catch (Exception ex)
                            {
                                TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, ex.Message);
                                listExceptions.Add(result);
                            }
                        }
                        else
                        {
                            recordsDeleteDisallowed++;
                        }

                        if (DeletionPerformed)
                        {
                            APetraUtilsObject.SetChangedFlag();
                            recordsDeleted++;
                        }

                        ACallerFormOrControl.HandlePostDelete(rowToDelete, AllowDeletion, DeletionPerformed, String.Empty);
                    }

                    APetraUtilsObject.GetForm().Cursor = Cursors.Default;
                    ACallerFormOrControl.SelectRowInGrid(currentRowIndex);

                    if (AButtonPanel != null)
                    {
                        AButtonPanel.UpdateRecordNumberDisplay();
                    }

                    if ((recordsDeleted > 0) && (CompletionMessage.Length > 0))
                    {
                        MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle);
                    }

                    //  Show the results of the multi-deletion
                    string results = null;

                    if (recordsDeleted > 0)
                    {
                        results = String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRecordSuccessfullyDeleted,
                                MCommonResourcestrings.StrRecordsSuccessfullyDeleted, recordsDeleted),
                            recordsDeleted);
                    }
                    else
                    {
                        results = MCommonResourcestrings.StrNoRecordsWereDeleted;
                    }

                    if (recordsUndeletable > 0)
                    {
                        results += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseNonDeletable,
                                MCommonResourcestrings.StrRowsNotDeletedBecauseNonDeletable,
                                recordsUndeletable),
                            Environment.NewLine,
                            recordsUndeletable);
                    }

                    if (recordsDeleteDisallowed > 0)
                    {
                        results += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseDeleteNotAllowed,
                                MCommonResourcestrings.StrRowsNotDeletedBecauseDeleteNotAllowed,
                                recordsDeleteDisallowed),
                            Environment.NewLine,
                            recordsDeleteDisallowed);
                    }

                    bool showCancel = false;

                    if (listConflicts.Count > 0)
                    {
                        showCancel = true;
                        results += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseReferencedElsewhere,
                                MCommonResourcestrings.StrRowsNotDeletedBecauseReferencedElsewhere,
                                listConflicts.Count),
                            Environment.NewLine,
                            listConflicts.Count);
                    }

                    if (listExceptions.Count > 0)
                    {
                        showCancel = true;
                        results += String.Format(
                            Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException,
                                MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException,
                                listExceptions.Count),
                            Environment.NewLine,
                            listExceptions.Count);
                    }

                    if (showCancel)
                    {
                        results += String.Format(MCommonResourcestrings.StrClickToReviewDeletionOrCancel, Environment.NewLine);

                        if (MessageBox.Show(results,
                                MCommonResourcestrings.StrDeleteActionSummaryTitle,
                                MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK)
                        {
                            ReviewMultiDeleteResults(listConflicts, MCommonResourcestrings.StrRowsReferencedByOtherTables);
                            ReviewMultiDeleteResults(listExceptions, MCommonResourcestrings.StrExceptions);
                        }
                    }
                    else
                    {
                        MessageBox.Show(results,
                            MCommonResourcestrings.StrDeleteActionSummaryTitle,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                    }
                }

                return recordsDeleted > 0;
            }
        }