/// <summary>
        /// Fills the cache with Exchange Rates for the given parameters
        /// </summary>
        /// <param name="startYearMonth">The start yearmonth</param>
        /// <param name="endYearMonth">The endyearmonth</param>
        /// <param name="currencyFrom">The CurrencyFrom</param>
        /// <param name="currencyTo">The CurrencyTo</param>
        public void FillExchangeRateCache(YearMonth startYearMonth, YearMonth endYearMonth, int currencyFrom, int currencyTo)
        {
            try
            {
                this._StartYearMonth = startYearMonth;
                this._EndYearMonth   = endYearMonth;
                this._CurrencyFrom   = currencyFrom;
                this._CurrencyTo     = currencyTo;

                //TODO: Unit test this method
                DataTable tableExchangeRates = (this.GetEntity().GetCustomDataSet("FillExchangeRateCache", this)).Tables[0];

                foreach (DataRow row in tableExchangeRates.Rows)
                {
                    //Gets the exchange rate from the datarow
                    BudgetExchangeRate exchangeRate = new BudgetExchangeRate();
                    exchangeRate.CurrencyFrom      = currencyFrom;
                    exchangeRate.CurrencyTo        = currencyTo;
                    exchangeRate.YearMonth         = new YearMonth((int)row["YearMonth"]);
                    exchangeRate.ExchangeRateValue = (decimal)row["ExchangeRateValue"];
                    //Add the exchange rate to cache
                    ExchangeRateCache.InsertExchangeRateToCache(exchangeRate);
                }
            }
            catch (Exception ex)
            {
                throw new IndException(ex);
            }
        }
        /// <summary>
        /// Get the new value expressed in the new currency
        /// </summary>
        /// <param name="originalValue">the giveen amount expressd in the currencyFrom currency</param>
        /// <param name="currencyFrom">The currency for the received amount of money</param>
        /// <param name="yearmonth">The yearmonth that will be used the get the new value</param>
        /// <param name="currencyTo">The currency that will be used to return the new amount of money</param>
        /// <returns>the new amount of money expressed in the currencyTo currency</returns>
        internal decimal GetConversionValue(decimal originalValue, int currencyFrom, YearMonth yearmonth, int currencyTo)
        {
            if (originalValue == 0)
            {
                return(0);
            }

            if (originalValue == ApplicationConstants.DECIMAL_NULL_VALUE)
            {
                return(ApplicationConstants.DECIMAL_NULL_VALUE);
            }

            //Constructs a new exhcange rate object with the given logical key
            BudgetExchangeRate exchangeRate = new BudgetExchangeRate();

            exchangeRate.CurrencyFrom = currencyFrom;
            exchangeRate.CurrencyTo   = currencyTo;
            exchangeRate.YearMonth    = yearmonth;

            //Get the currency rate
            decimal currencyRate = this.ExchangeRateCache.GetExchangeRate(exchangeRate);

            //Get the converted value
            decimal convertedValue = Decimal.Multiply(originalValue, currencyRate);

            //return the converted value
            return(convertedValue);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the exchange Rate value from the cache. If it does not exist it gets it from the database
        /// and inset it into cache
        /// </summary>
        /// <param name="exchangeRate">The exchange rate containing the logical key</param>
        /// <returns>The exchange rate value</returns>
        internal decimal GetExchangeRate(BudgetExchangeRate exchangeRate)
        {
            //Search the cache to see if the exchange rate exists in cache
            foreach (BudgetExchangeRate currentExchangeRate in exchangeRatesCache)
            {
                //If it is found return the value from the cache
                if (currentExchangeRate.CompareWithObject(exchangeRate))
                {
                    //the object exists in cache but we don't know yet in what order
                    if (currentExchangeRate.CurrencyFrom == exchangeRate.CurrencyFrom)
                    {
                        return(currentExchangeRate.ExchangeRateValue);
                    }
                    else
                    {
                        return(Decimal.Divide(1, currentExchangeRate.ExchangeRateValue));
                    }
                }
            }
            //If the code reaches this point, it means that the exchange rate doesn't exists in the cache
            //so it should be load from the dabase
            decimal exRateValue = (new ExchangeRateReader(this.connectionManager)).GetExchangeRate(exchangeRate);

            //After received from the database insert into the cache before return it
            exchangeRate.ExchangeRateValue = exRateValue;
            this.InsertExchangeRateToCache(exchangeRate);
            //Return the exchange Rate value
            return(exRateValue);
        }
Exemplo n.º 4
0
        /// <summary>
        /// This method returns the exchange rate from the base based on to parameters: Currency and
        /// YearMonth; To be known that inside stored procedure two other rules for selecting this ExchangeRate
        /// are used. For more information look after Stored Procedure bgtSelectExchangeRateForConverter
        /// </summary>
        /// <param name="exchangeRate"></param>
        /// <returns></returns>
        internal decimal GetExchangeRate(BudgetExchangeRate exchangeRate)
        {
            _CurrencyFrom = exchangeRate.CurrencyFrom;
            _CurrencyTo   = exchangeRate.CurrencyTo;
            _YearMonth    = exchangeRate.YearMonth;

            DBExchangeRateReader dbc = new DBExchangeRateReader(this.CurrentConnectionManager);

            //If for a random reason the specified cast cannot be made, an IndException is thrown
            try
            {
                object returnedER = dbc.ExecuteScalar("bgtSelectExchangeRateForConverter", this);
                //TODO: Modifiy error message - use names instead of ids
                if (returnedER == DBNull.Value)
                {
                    throw new IndException(ApplicationMessages.MessageWithParameters(ApplicationMessages.EXCEPTION_EXCHANGE_RATE_NOT_FOUND, exchangeRate.CurrencyFrom.ToString(), exchangeRate.CurrencyTo.ToString()));
                }
                ExchangeRate = (decimal)returnedER;
            }
            catch (Exception exc)
            {
                throw new IndException(exc);
            }
            return(ExchangeRate);
        }
        /// <summary>
        /// Compares the current object with another object
        /// </summary>
        /// <param name="secondExchangeRate">The object to compare with</param>
        /// <returns></returns>
        internal bool CompareWithObject(BudgetExchangeRate secondExchangeRate)
        {
            //All fields except ExchangeRateValue should be the same (no matter in what order)
            if (this.YearMonth.GetMonthsDiffrence(secondExchangeRate.YearMonth) != 0)
            {
                return(false);
            }
            if ((this.CurrencyFrom == secondExchangeRate.CurrencyFrom) && (this.CurrencyTo == secondExchangeRate.CurrencyTo))
            {
                return(true);
            }
            if ((this.CurrencyFrom == secondExchangeRate.CurrencyTo) && (this.CurrencyTo == secondExchangeRate.CurrencyFrom))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a given exchange rate into cache
        /// </summary>
        /// <param name="exRate">The exchange rate object</param>
        internal void InsertExchangeRateToCache(BudgetExchangeRate exRate)
        {
            bool found = false;

            //Insert the budget exchange rate into the cache only if it is not already in the cache
            foreach (BudgetExchangeRate rate in exchangeRatesCache)
            {
                if (exRate.CompareWithObject(rate))
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                exchangeRatesCache.Add(exRate);
            }
        }