public static int IdRoku(string userName)
        {
            LataObrotoweRepository LataObrotoweRepository = new LataObrotoweRepository();
            //return (int)System.Web.HttpContext.Current.Cache.Get("YearId");

            int?YearId = LataObrotoweRepository.WybraneIdRoku(userName);

            //if ((YearId = (int?)System.Web.HttpContext.Current.Cache.Get("YearId")).HasValue)
            //{
            //    return YearId.Value;
            //}

            if (YearId.HasValue)
            {
                return(YearId.Value);
            }
            else
            {
                int ParsedYearId;

                if (Int32.TryParse(WebConfigurationManager.AppSettings["idRoku"], out ParsedYearId))
                {
                    return(ParsedYearId);
                }
                else
                {
                    throw new Exception("Niepowodzenie odczytu id roku");
                }
            }
        }
        public void ShouldBeAbleToCallMultipleTimesRepository()
        {
            var initialRepository = new LataObrotoweRepository();

            int ustawianeIdRoku = 22;

            initialRepository.WybierzIdRoku(ustawianeIdRoku, "vn");
            initialRepository.Save();

            for (int i = 0; i < 30; i++)
            {
                var repository = new LataObrotoweRepository();
                (new TaskFactory()).StartNew(() =>
                {
                    repository.WybierzIdRoku(ustawianeIdRoku, "vn");
                    repository.Save();
                });
                var odczytaneIdRoku = (new LataObrotoweRepository()).WybraneIdRokuOrExcepion("vn");

                Assert.IsTrue(ustawianeIdRoku == odczytaneIdRoku, String.Format("Rozne id roku dla {0} wywolania ({1} != {2})", i, ustawianeIdRoku, odczytaneIdRoku));

                //if (i % 3 == 0)
                //{
                //    ustawianeIdRoku = 17;
                //}
                //else
                //if (i % 3 == 1)
                //{
                //    ustawianeIdRoku = 22;
                //}
                //else
                //if (i % 3 == 2)
                //{
                //    ustawianeIdRoku = 23;
                //}
            }
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                int?CompanyId = null;
                int?YearId    = null;
                int ParsedCompanyId;
                int ParsedYearId;

                // do not make this repositories as a class properties, they has to be instantiated in every request (or you wont see new results)
                FirmyRepository        FirmyRepository        = new FirmyRepository();
                LataObrotoweRepository LataObrotoweRepository = new LataObrotoweRepository();

                // jesli w AppSettings ustawimy idFirmy to zapisujemy je w cache'u
                if (Int32.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["idFirmy"], out ParsedCompanyId))
                {
                    CompanyId = ParsedCompanyId;
                    //System.Web.HttpContext.Current.Cache.Insert("CompanyId", ParsedCompanyId);
                }

                // jesli w AppSettings ustawimy idRoku to zapisujemy je w cache'u
                if (Int32.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["idRoku"], out ParsedYearId))
                {
                    YearId = ParsedYearId;
                    //System.Web.HttpContext.Current.Cache.Insert("YearId", ParsedYearId);
                }

                //if (!(CompanyId = (int?)System.Web.HttpContext.Current.Cache.Get("CompanyId")).HasValue)
                if (!CompanyId.HasValue && !(CompanyId = FirmyRepository.WybraneIdFirmy(filterContext.HttpContext.User.Identity.Name)).HasValue)
                {
                    string returnUrl = filterContext.HttpContext.Request.Url.ToString();
                    string debug     = filterContext.HttpContext.Request.Params["returnUrl"];
                    filterContext.Controller.TempData["Message"] = String.Format("Nie wybrano firmy.");
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                        { "action", "Wybor" }, { "controller", "Firmy" }, { "returnUrl", returnUrl }
                    });
                }
                else
                {
                    if (!YearId.HasValue && !(YearId = LataObrotoweRepository.WybraneIdRoku(filterContext.HttpContext.User.Identity.Name)).HasValue)
                    {
                        string returnUrl = filterContext.HttpContext.Request.Url.ToString();
                        string debug     = filterContext.HttpContext.Request.Params["returnUrl"];
                        filterContext.Controller.TempData["Message"] = String.Format("Nie wybrano roku.");
                        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                            { "action", "Wybor" }, { "controller", "LataObrotowe" }, { "idFirmy", CompanyId }, { "returnUrl", returnUrl }
                        });
                    }
                    else
                    {
                        filterContext.Controller.ViewBag.YearId     = YearId;
                        filterContext.Controller.TempData["YearId"] = YearId;

                        //string YearName = (string)System.Web.HttpContext.Current.Cache.Get("YearName");
                        string YearName = LataObrotoweRepository.RokObrotowy(YearId.Value).NazwaRoku;

                        if (!String.IsNullOrEmpty(YearName))
                        {
                            filterContext.Controller.ViewBag.YearName     = YearName;
                            filterContext.Controller.TempData["YearName"] = YearName;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                filterContext.Result = new RedirectToRouteResult("Firma", null);
            }

            base.OnActionExecuting(filterContext);
        }