context: //
  // DO NOT REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  //
  // @Authors:
  //       christiank
  //
  // Copyright 2004-2012 by OM International
  //
  // This file is part of OpenPetra.org.
  //
  // OpenPetra.org is free software: you can redistribute it and/or modify
  // it under the terms of the GNU General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // OpenPetra.org is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // You should have received a copy of the GNU General Public License
  // along with OpenPetra.org.  If not, see <http://www.gnu.org/licenses/>.
  //
  using System;
  using System.Windows.Forms;
  using System.Diagnostics;
  using Ict.Common;
  using Ict.Common.Exceptions;
  using Ict.Common.Data;
  using Ict.Common.Data.Exceptions;
  using Ict.Common.Verification;
  using Ict.Petra.Client.App.Core;
  using Ict.Petra.Shared;
  using Ict.Petra.Shared.Security;
  using Ict.Petra.Shared.MPartner;
  using GNU.Gettext;
  
  namespace Ict.Petra.Client.App.Gui
  {
      /// <summary>
      /// Contains Messages which can be used anywhere in the Petra Client. They are not
      /// specific for a certain Petra Module.
      /// </summary>
      public class TMessages
      {
          #region Private Message Strings
  
          #region General
  
          /// <summary>Message Number</summary>
          private static readonly string StrMessageNumber = Catalog.GetString("Message Number: ");
  
          /// <summary>Context</summary>
          private static readonly string StrContext = Catalog.GetString("Context: ");
  
          /// <summary>Release</summary>
          private static readonly string StrRelease = Catalog.GetString("Release: ");
  
          /// <summary>Shown when the Form contains invalid data.</summary>
          private static readonly string StrInvalidDataNeedsCorrectingTitle = Catalog.GetString("Form Contains Invalid Data!");
  
          /// <summary>Shown when there are Warnings associated with a Form's data.</summary>
          private static readonly string StrFormDataWarningTitle = Catalog.GetString("Form Data Warning");
  
          /// <summary>Shown when invalid data was entered.</summary>
          private static readonly string StrInvalidDataTitle = Catalog.GetString("Invalid Data");
  
          /// <summary>Shown when the Form contains deleted records and at least one of them is referenced in the DB (hence it cannot be deleted).</summary>
          private static readonly string StrDeletionNotPossibleBecauseOfReferencesTitle = Catalog.GetString("Deletion of Record Not Possible!");
  
          /// <summary>Shown when the Form contains invalid data at a certain point when Data Validation runs (e.g. saving of data, change of context [e.g. switching of Tab]).</summary>
          private static readonly string StrInvalidDataNeedsCorrecting = Catalog.GetString(
              "The operation cannot be performed because the form contains invalid data:");
  
          /// <summary>Shown when a record contains warnings at the point of changing to a different record.</summary>
          private static readonly string StrRecordChangeInvalidDataWarning = Catalog.GetString(
              "There are warning messages associated with the data in the currently edited record\r\n(you can move away from that record, though):");
  
          /// <summary>Shown when a record contains invalid data at the point of changing to a different record.</summary>
          private static readonly string StrRecordChangeInvalidDataNeedsCorrecting = Catalog.GetString(
              "The currently edited record contains invalid data:");
  
          /// <summary>Shown when the Form contains deleted records and at least one of them is referenced in the DB (hence it cannot be deleted).</summary>
          private static readonly string StrDeletionNotPossibleBecauseOfReferences = Catalog.GetString(
              "You tried to delete a record that is referenced from somewhere else; that record can therefore not be deleted.\r\n" +
              "Saving of data is not possible anymore from this screen - please close it to continue your work.");
  
          /// <summary>Shown when the Form contains invalid data at the point of saving data.</summary>
          private static readonly string StrRecordChangeInvalidDataNeedsCorrectingTitle = Catalog.GetString("Record Contains Invalid Data!");
  
          #endregion
  
          #region Database Concurrency
  
          // Notice to translators: when translating the following resourcestrings which are about database concurrency,
          // be very careful that the message that the user gets displayed does still make sense and is still correct!!!
          // (The strings are replaced in quite different ways depending on the situation.)
  
          /// <summary>Database Concurrency Message Title.</summary>
          private static readonly string StrDBConcurrencyTitle = Catalog.GetString("Unable to Save - Concurrent Activity Detected");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyOtherUser = Catalog.GetString(
              "Another user {0} has {1} the same record in Table '{2}' after you have" + "\r\n" +
              "{3} it.{4}" + "\r\n" + "\r\n" +
              "None of your changes can be saved, since the other user's changes could be overwritten." +
              "\r\n" + "\r\n");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencySelf = Catalog.GetString(
              "You have tried to {1} the same record in Table\r\n" + "'{2}' after you have {3}{4}." +
              "\r\n" + "\r\n" +
              "None of your current changes can be saved, since these changes" + "\r\n" +
              "could potentially conflict with each other." +
              "\r\n" + "\r\n");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyActionsRequired = Catalog.GetString("Actions required:\r\n" +
              "  * If you were in a screen: please close the screen and re-open it again.\r\n" +
              "  * You will need to repeat your changes and save them again.\r\n" +
              "  * In case the error keeps occuring: please contact your OpenPetra support team.");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyWrittenSelfAction = Catalog.GetString("modified");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyWrittenOthersAction = Catalog.GetString("modified and saved");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyWriteOthersAction = Catalog.GetString("modify and save");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyDeletedSelfAction1 = Catalog.GetString("opened and before you deleted");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyDeletedSelfAction2 = Catalog.GetString("deleted it somewhere else");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyDeletedOthersAction = Catalog.GetString("deleted");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyDeleteOthersAction = Catalog.GetString("delete");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyDateInfoOthersAction = Catalog.GetString(" The other user has done this on {0}.");
  
          /// <summary>Part of a Database Concurrency Message.</summary>
          private static readonly string StrDBConcurrencyDateInfoSelfAction = Catalog.GetString(" it in another place on {0}");
          #endregion
  
          #endregion
  
  
          /// <summary>
          /// Displays a general warning message in a MessageBox.
          /// </summary>
          /// <param name="AErrorText">Error text to be displayed.</param>
          /// <param name="ATitle">Title of the MessageBox.</param>
          /// <param name="AMessageNumber">Message Number.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgGeneralWarning(String AErrorText, String ATitle, String AMessageNumber, System.Type ATypeWhichRaisesError)
          {
              MsgGeneralError(AErrorText, ATitle, AMessageNumber, ATypeWhichRaisesError, MessageBoxIcon.Warning);
          }
  
          /// <summary>
          /// Displays a general error message in a MessageBox.
          /// </summary>
          /// <param name="AVerificationResult">TVerificationResult which contains all the information.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgGeneralError(TVerificationResult AVerificationResult, System.Type ATypeWhichRaisesError)
          {
              MessageBoxIcon MsgBoxIcon = GetMbxIconFromVerificationResult(AVerificationResult);
  
              MsgGeneralError(AVerificationResult.ResultText,
                  AVerificationResult.ResultTextCaption,
                  AVerificationResult.ResultCode,
                  ATypeWhichRaisesError, MsgBoxIcon);
          }
  
          /// <summary>
          /// Displays a general error message in a MessageBox.
          /// </summary>
          /// <param name="AErrorText">Error text to be displayed.</param>
          /// <param name="ATitle">Title of the MessageBox.</param>
          /// <param name="AMessageNumber">Message Number.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgGeneralError(String AErrorText, String ATitle, String AMessageNumber, System.Type ATypeWhichRaisesError)
          {
              MsgGeneralError(AErrorText, ATitle, AMessageNumber, ATypeWhichRaisesError, MessageBoxIcon.Error);
          }
  
          /// <summary>
          /// Displays a general error message in a MessageBox.
          /// </summary>
          /// <param name="AErrorText">Error text to be displayed.</param>
          /// <param name="ATitle">Title of the MessageBox.</param>
          /// <param name="AMessageNumber">Message Number.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          /// <param name="AIcon">Icon to show in the MessageBox.</param>
          public static void MsgGeneralError(String AErrorText, String ATitle, String AMessageNumber, System.Type ATypeWhichRaisesError,
              MessageBoxIcon AIcon)
          {
              if (ATypeWhichRaisesError == null)
              {
                  ATypeWhichRaisesError = new StackTrace(false).GetFrame(2).GetMethod().DeclaringType;
              }
  
              // Remove possible trailing double Environment.NewLines
              // (Double Environment.NewLines happen if several error messages are concatenated and they are each separated by double Environment.NewLines)
              if (AErrorText.EndsWith(Environment.NewLine + Environment.NewLine))
              {
                  AErrorText = AErrorText.Substring(0, AErrorText.Length - Environment.NewLine.Length);
              }
  
              string message = AErrorText + BuildMessageFooter(AMessageNumber,
                  ATypeWhichRaisesError.Name);
  
              TLogging.LogAtLevel(1, ATitle + ": " + message);
              MessageBox.Show(message, ATitle, MessageBoxButtons.OK, AIcon);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox.
          /// </summary>
          /// <param name="AVerificationResult">TVerificationResult which contains all the information.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgVerificationError(TVerificationResult AVerificationResult, System.Type ATypeWhichRaisesError)
          {
              MsgVerificationError(AVerificationResult.ResultText,
                  AVerificationResult.ResultTextCaption,
                  AVerificationResult.ResultCode,
                  ATypeWhichRaisesError);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="AMessageNumber">Message Number.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgVerificationError(String AVerificationError, String AMessageNumber, System.Type ATypeWhichRaisesError)
          {
              MsgVerificationError(AVerificationError, String.Empty, AMessageNumber, ATypeWhichRaisesError);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="ATitle">Title of the MessageBox.</param>
          /// <param name="AMessageNumber">Message Number.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgVerificationError(String AVerificationError, String ATitle, String AMessageNumber, System.Type ATypeWhichRaisesError)
          {
              string Title = ATitle;
  
              if (Title == String.Empty)
              {
                  Title = StrInvalidDataTitle;
              }
  
              if (ATypeWhichRaisesError == null)
              {
                  ATypeWhichRaisesError = new StackTrace(false).GetFrame(2).GetMethod().DeclaringType;
              }
  
              MsgGeneralError(AVerificationError, Title, AMessageNumber, ATypeWhichRaisesError);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox. Use this for the data verification check before a record in a Grid is changed.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgRecordChangeVerificationError(String AVerificationError, System.Type ATypeWhichRaisesError)
          {
              MsgRecordChangeVerificationError(AVerificationError, String.Empty, false, ATypeWhichRaisesError);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox. Use this for the data verification check before a record in a Grid is changed.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="AOnlyWarnings">Set this to true if the items in <paramref name="AVerificationError" />
          /// are only warnings.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgRecordChangeVerificationError(String AVerificationError, bool AOnlyWarnings, System.Type ATypeWhichRaisesError)
          {
              MsgRecordChangeVerificationError(AVerificationError, String.Empty, AOnlyWarnings, ATypeWhichRaisesError);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox. Use this for the data verification check before a record in a Grid is changed.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="AMessageNumber">Message Number.</param>
          /// <param name="AOnlyWarnings">Set this to true if the items in <paramref name="AVerificationError" />
          /// are only warnings.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgRecordChangeVerificationError(String AVerificationError,
              String AMessageNumber,
              bool AOnlyWarnings,
              System.Type ATypeWhichRaisesError)
          {
              string Heading;
              MessageBoxIcon Icon;
  
              if (AMessageNumber == String.Empty)
              {
                  AMessageNumber = PetraErrorCodes.ERR_GENERAL_VERIFICATION_ERROR;
              }
  
              if (ATypeWhichRaisesError == null)
              {
                  ATypeWhichRaisesError = new StackTrace(false).GetFrame(2).GetMethod().DeclaringType;
              }
  
              if (AOnlyWarnings)
              {
                  Heading = StrRecordChangeInvalidDataWarning;
                  Icon = MessageBoxIcon.Warning;
              }
              else
              {
                  Heading = StrRecordChangeInvalidDataNeedsCorrecting;
                  Icon = MessageBoxIcon.Error;
              }
  
              MsgGeneralError(Heading + Environment.NewLine + Environment.NewLine +
                  AVerificationError, Catalog.GetString(
                      TMessages.StrRecordChangeInvalidDataNeedsCorrectingTitle), AMessageNumber, ATypeWhichRaisesError, Icon);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox. Use this for the final data verification before a Form's data is saved.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgFormSaveVerificationError(String AVerificationError, System.Type ATypeWhichRaisesError)
          {
              MsgFormSaveVerificationError(AVerificationError, String.Empty, false, ATypeWhichRaisesError);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox. Use this for the final data verification before a Form's data is saved.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="AOnlyWarnings">Set this to true if the items in <paramref name="AVerificationError" />
          /// are only warnings.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          public static void MsgFormSaveVerificationError(String AVerificationError, bool AOnlyWarnings, System.Type ATypeWhichRaisesError)
          {
              MsgFormSaveVerificationError(AVerificationError, String.Empty, AOnlyWarnings, ATypeWhichRaisesError);
          }
  
          /// <summary>
          /// Displays a verification error message in a MessageBox. Use this for the final data verification before a Form's data is saved.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="AMessageNumber">Message Number.</param>
          /// <param name="AOnlyWarnings">Set this to true if the items in <paramref name="AVerificationError" />
          /// are only warnings.</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          /// <param name="AOnlyRecordDeletionErrorsBecauseOfReference">Set to true if the message only pertains to
          /// the inability to delete (a) record(s) because they are referenced from within the DB.</param>
          public static void MsgFormSaveVerificationError(String AVerificationError,
              String AMessageNumber,
              bool AOnlyWarnings,
              System.Type ATypeWhichRaisesError,
              bool AOnlyRecordDeletionErrorsBecauseOfReference = false)
          {
              string Title;
              string Heading;
              MessageBoxIcon Icon;
  
              if (AMessageNumber == String.Empty)
              {
                  AMessageNumber = PetraErrorCodes.ERR_GENERAL_VERIFICATION_ERROR;
              }
  
              if (ATypeWhichRaisesError == null)
              {
                  ATypeWhichRaisesError = new StackTrace(false).GetFrame(2).GetMethod().DeclaringType;
              }
  
              if (AOnlyWarnings)
              {
                  Title = StrFormDataWarningTitle;
                  Heading = Messages.StrWarningsAttention;
                  Icon = MessageBoxIcon.Warning;
              }
              else
              {
                  if (!AOnlyRecordDeletionErrorsBecauseOfReference)
                  {
                      Title = StrInvalidDataNeedsCorrectingTitle;
                      Heading = StrInvalidDataNeedsCorrecting;
                  }
                  else
                  {
                      Title = StrDeletionNotPossibleBecauseOfReferencesTitle;
                      Heading = StrDeletionNotPossibleBecauseOfReferences;
                  }
  
                  Icon = MessageBoxIcon.Error;
              }
  
              MsgGeneralError(Heading + Environment.NewLine + Environment.NewLine +
                  AVerificationError, Title, AMessageNumber,
                  ATypeWhichRaisesError, Icon);
          }
  
          /// <summary>
          /// Displays a verification warning message in a MessageBox. Use this for the final data verification before a Form's data is saved
          /// but after the message has been shown about errors.  This box should only appear if there are no errors but only warnings.
          /// </summary>
          /// <param name="AVerificationError">Verification error to be displayed.</param>
          /// <param name="AMessageNumber">Message Number.  Usually PetraErrorCodes.ERR_GENERAL_VERIFICATION_WARNING</param>
          /// <param name="ATypeWhichRaisesError">Instance of an object which raises the Error.</param>
          /// <param name="AQuestionToAppend">This message box offers the user a Yes/No choice.  Phrase an appropriate question in this parameter.</param>
          /// <param name="ADefaultToAnswerYes">Set to true if the default answer should be Yes.  Set to false if the default answer should be No.</param>
          /// <returns>The DialogResult value - either Yes or No.</returns>
          public static DialogResult MsgFormSaveVerificationWarning(String AVerificationError,
              String AMessageNumber,
              System.Type ATypeWhichRaisesError,
              String AQuestionToAppend,
              bool ADefaultToAnswerYes)
          {
              if (AMessageNumber == String.Empty)
              {
                  AMessageNumber = PetraErrorCodes.ERR_GENERAL_VERIFICATION_WARNING;
              }
  
              if (ATypeWhichRaisesError == null)
              {
                  ATypeWhichRaisesError = new StackTrace(false).GetFrame(2).GetMethod().DeclaringType;
              }
  
              // Remove possible trailing double Environment.NewLines
              // (Double Environment.NewLines happen if several error messages are concatenated and they are each separated by double Environment.NewLines)
              if (AVerificationError.EndsWith(Environment.NewLine + Environment.NewLine))
              {
                  AVerificationError = AVerificationError.Substring(0, AVerificationError.Length - Environment.NewLine.Length);
              }
  
              // Assemble the message
              string message = String.Format("{0}{1}{1}{2}{1}{3}{4}",
                  Messages.StrWarningsAttention,
                  Environment.NewLine,
                  AVerificationError,
                  AQuestionToAppend,
                  BuildMessageFooter(AMessageNumber, ATypeWhichRaisesError.Name));
  
              // Show the message box and return the result
              return MessageBox.Show(message,
                  ErrorCodes.GetErrorInfo(PetraErrorCodes.ERR_GENERAL_VERIFICATION_WARNING).ErrorMessageTitle,
                  MessageBoxButtons.YesNo,
                  MessageBoxIcon.Question,
                  ADefaultToAnswerYes ? MessageBoxDefaultButton.Button1 : MessageBoxDefaultButton.Button2);
          }
  
          /// <summary>
          /// todoComment
          /// </summary>
          /// <param name="AException"></param>
          /// <param name="ATypeWhichRaisesError"></param>
          public static void MsgSecurityException(ESecurityGroupAccessDeniedException AException, System.Type ATypeWhichRaisesError)
          {
              string Context = DetermineExceptionContext(AException, ATypeWhichRaisesError);
  
              MessageBox.Show(AException.Message +
                  BuildMessageFooter(PetraErrorCodes.ERR_NOPERMISSIONTOACCESSGROUP,
                      Context), Catalog.GetString("Security Violation"), MessageBoxButtons.OK,
                  MessageBoxIcon.Information);
          }
  
          /// <summary>
          /// todoComment
          /// </summary>
          /// <param name="AException"></param>
          /// <param name="ATypeWhichRaisesError"></param>
          public static void MsgSecurityException(ESecurityAccessDeniedException AException, System.Type ATypeWhichRaisesError)
          {
              string Context = DetermineExceptionContext(AException, ATypeWhichRaisesError);
  
              MessageBox.Show(AException.Message +
                  BuildMessageFooter(PetraErrorCodes.ERR_NOPERMISSIONTOACCESSMODULE,
                      Context), Catalog.GetString("Security Violation"), MessageBoxButtons.OK,
                  MessageBoxIcon.Information);
          }
  
          /// <summary>
          /// todoComment
          /// </summary>
          /// <param name="AException"></param>
          /// <param name="ATypeWhichRaisesError"></param>
          public static void MsgSecurityException(ESecurityDBTableAccessDeniedException AException, System.Type ATypeWhichRaisesError)
          {
              String TableLabelName = StringHelper.UpperCamelCase(AException.DBTable);
              string Context = DetermineExceptionContext(AException, ATypeWhichRaisesError);
  
              MessageBox.Show(String.Format(Catalog.GetString("You do not have permission to {0} {1} records."), AException.AccessRight,
                      TableLabelName) + BuildMessageFooter(PetraErrorCodes.ERR_NOPERMISSIONTOACCESSTABLE,
                      Context), Catalog.GetString("Security Violation"), MessageBoxButtons.OK,
                  MessageBoxIcon.Information);
          }
  
          /// <summary>
          /// todoComment
          /// </summary>
          /// <param name="AException"></param>
          public static void MsgSecurityException(ESecurityPartnerAccessDeniedException AException)
          {
              MessageBox.Show(MsgSecurityExceptionString(AException), Catalog.GetString(
                      "Security Violation"), MessageBoxButtons.OK,
                  MessageBoxIcon.Information);
          }
  
          private static string DetermineExceptionContext(ESecurityAccessDeniedException AException, System.Type ATypeWhichRaisesError)
          {
              if (AException == null)
              {
                  throw new ArgumentNullException("AException");
              }
  
              if (AException.Context != String.Empty)
              {
                  return AException.Context;
              }
  
gt:             else if (ATypeWhichRaisesError != null)