Exemple #1
0
        private void PopulateLedgerDictionaries()
        {
            bool LedgerDictPrePopulated = (FLedgerNamesDict.Count > 0);

            if (LedgerDictPrePopulated && FLedgerNamesDict.ContainsKey(FLedgerNumber))
            {
                return;
            }

            //Prepare a temp dictionaries for minimum time in lock
            Dictionary <int, string> LedgerNamesDictTemp        = new Dictionary <int, string>();
            Dictionary <int, string> LedgerCountryCodesDictTemp = new Dictionary <int, string>();
            Dictionary <int, string> LedgerBaseCurrencyDictTemp = new Dictionary <int, string>();

            //Take a backup for reversion purposes if error occurs
            Dictionary <int, string> LedgerNamesDictBackup        = null;
            Dictionary <int, string> LedgerCountryCodesDictBackup = null;
            Dictionary <int, string> LedgerBaseCurrencyDictBackup = null;

            TDBTransaction Transaction = new TDBTransaction();
            TDataBase      db          = DBAccess.Connect("PopulateLedgerDictionaries", FDataBase);

            try
            {
                db.ReadTransaction(
                    ref Transaction,
                    delegate
                {
                    String strSql = "SELECT a_ledger_number_i, p_partner_short_name_c, a_country_code_c, a_base_currency_c" +
                                    " FROM PUB_a_ledger, PUB_p_partner" +
                                    " WHERE PUB_a_ledger.p_partner_key_n = PUB_p_partner.p_partner_key_n;";

                    DataTable ledgerData = db.SelectDT(strSql, "GetLedgerName_TempTable", Transaction);

                    #region Validate Data

                    if ((ledgerData == null) || (ledgerData.Rows.Count == 0))
                    {
                        throw new EFinanceSystemDataTableReturnedNoDataException(String.Format(Catalog.GetString(
                                                                                                   "Function:{0} - Ledger and Partner data does not exist or could not be accessed!"),
                                                                                               Utilities.GetMethodName(true)));
                    }

                    #endregion Validate Data

                    int currentLedger                = 0;
                    string currentLedgerName         = string.Empty;
                    string currentLedgerCountryCode  = string.Empty;
                    string currentLedgerBaseCurrency = string.Empty;

                    for (int i = 0; i < ledgerData.Rows.Count; i++)
                    {
                        currentLedger             = Convert.ToInt32(ledgerData.Rows[i][ALedgerTable.GetLedgerNumberDBName()]);
                        currentLedgerName         = Convert.ToString(ledgerData.Rows[i][PPartnerTable.GetPartnerShortNameDBName()]);
                        currentLedgerCountryCode  = Convert.ToString(ledgerData.Rows[i][ALedgerTable.GetCountryCodeDBName()]);
                        currentLedgerBaseCurrency = Convert.ToString(ledgerData.Rows[i][ALedgerTable.GetBaseCurrencyDBName()]);

                        LedgerNamesDictTemp.Add(currentLedger, currentLedgerName);
                        LedgerCountryCodesDictTemp.Add(currentLedger, currentLedgerCountryCode);
                        LedgerBaseCurrencyDictTemp.Add(currentLedger, currentLedgerBaseCurrency);
                    }

                    bool lockEntered = false;

                    try
                    {
                        if (FReadWriteLock.TryEnterWriteLock(10))
                        {
                            lockEntered = true;

                            if (LedgerDictPrePopulated)
                            {
                                //Backup dictionaries
                                LedgerNamesDictBackup        = new Dictionary <int, string>(FLedgerNamesDict);
                                LedgerCountryCodesDictBackup = new Dictionary <int, string>(FLedgerCountryCodeDict);
                                LedgerBaseCurrencyDictBackup = new Dictionary <int, string>(FLedgerBaseCurrencyDict);

                                FLedgerNamesDict.Clear();
                                FLedgerCountryCodeDict.Clear();
                                FLedgerBaseCurrencyDict.Clear();
                            }

                            FLedgerNamesDict        = new Dictionary <int, string>(LedgerNamesDictTemp);
                            FLedgerCountryCodeDict  = new Dictionary <int, string>(LedgerCountryCodesDictTemp);
                            FLedgerBaseCurrencyDict = new Dictionary <int, string>(LedgerBaseCurrencyDictTemp);
                        }
                    }
                    finally
                    {
                        if (lockEntered)
                        {
                            FReadWriteLock.ExitWriteLock();
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                if (LedgerNamesDictBackup != null)
                {
                    FLedgerNamesDict        = new Dictionary <int, string>(LedgerNamesDictBackup);
                    FLedgerCountryCodeDict  = new Dictionary <int, string>(LedgerCountryCodesDictBackup);
                    FLedgerBaseCurrencyDict = new Dictionary <int, string>(LedgerBaseCurrencyDictBackup);
                }

                TLogging.LogException(ex, Utilities.GetMethodSignature());
                throw;
            }

            if (FDataBase == null)
            {
                db.CloseDBConnection();
            }
        }
        private void PopulateLedgerDictionaries()
        {
            bool LedgerDictPrePopulated = (FLedgerNamesDict.Count > 0);

            if (LedgerDictPrePopulated && FLedgerNamesDict.ContainsKey(FLedgerNumber))
            {
                return;
            }

            if (LedgerDictPrePopulated)
            {
                FLedgerNamesDict.Clear();
                FLedgerCountryCodeDict.Clear();
                FLedgerBaseCurrencyDict.Clear();
            }

            TDBTransaction Transaction = new TDBTransaction();
            TDataBase      db          = DBAccess.Connect("PopulateLedgerDictionaries", FDataBase);

            db.ReadTransaction(
                ref Transaction,
                delegate
            {
                String strSql = "SELECT a_ledger_number_i, p_partner_short_name_c, a_country_code_c, a_base_currency_c" +
                                " FROM PUB_a_ledger, PUB_p_partner" +
                                " WHERE PUB_a_ledger.p_partner_key_n = PUB_p_partner.p_partner_key_n;";

                DataTable ledgerData = db.SelectDT(strSql, "GetLedgerName_TempTable", Transaction);

                #region Validate Data

                if ((ledgerData == null) || (ledgerData.Rows.Count == 0))
                {
                    throw new EFinanceSystemDataTableReturnedNoDataException(String.Format(Catalog.GetString(
                                                                                               "Function:{0} - Ledger and Partner data does not exist or could not be accessed!"),
                                                                                           Utilities.GetMethodName(true)));
                }

                #endregion Validate Data

                int currentLedger                = 0;
                string currentLedgerName         = string.Empty;
                string currentLedgerCountryCode  = string.Empty;
                string currentLedgerBaseCurrency = string.Empty;

                for (int i = 0; i < ledgerData.Rows.Count; i++)
                {
                    currentLedger             = Convert.ToInt32(ledgerData.Rows[i][ALedgerTable.GetLedgerNumberDBName()]);
                    currentLedgerName         = Convert.ToString(ledgerData.Rows[i][PPartnerTable.GetPartnerShortNameDBName()]);
                    currentLedgerCountryCode  = Convert.ToString(ledgerData.Rows[i][ALedgerTable.GetCountryCodeDBName()]);
                    currentLedgerBaseCurrency = Convert.ToString(ledgerData.Rows[i][ALedgerTable.GetBaseCurrencyDBName()]);

                    FLedgerNamesDict.Add(currentLedger, currentLedgerName);
                    FLedgerCountryCodeDict.Add(currentLedger, currentLedgerCountryCode);
                    FLedgerBaseCurrencyDict.Add(currentLedger, currentLedgerBaseCurrency);
                }
            });

            if (FDataBase == null)
            {
                db.CloseDBConnection();
            }
        }