using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sleis.Models; using Sleis.ViewModels; using Sleis.Service; namespace Sleis.Utility { public static class SessionUtility { private const string FILTERED_RELEASE_POINT_IDS= @"FilteredReleasePointIds"; private const string LAST_RELEASE_POINT_FILTER_VALUE = @"LastReleasePointListFilterValue"; private const string CURRENT_REPORT = "CurrentReport"; public static AgencyReport CurrentReport { get { return Get(CURRENT_REPORT); } set { SetSessionVar(CURRENT_REPORT, value); } } /// /// Will return agency report year if available. Otherwise will return current year /// public static int GetCurrentReportYear() { return CurrentReport != null ? CurrentReport.Number : DateTime.Now.Year; } public static int[] FilteredReleasePointIds { get { return Get(FILTERED_RELEASE_POINT_IDS); } set { SetSessionVar(FILTERED_RELEASE_POINT_IDS, value); } } public static string LastReleasePointListFilterValue { get { return Get(LAST_RELEASE_POINT_FILTER_VALUE); } set { SetSessionVar(LAST_RELEASE_POINT_FILTER_VALUE, value); } } public static T Get(string key) { object obj = HttpContext.Current.Session[key]; if (obj == null) { return default(T); } return (T)obj; } public static void SetSessionVar(string key, T value) { if (value == null) { HttpContext.Current.Session.Remove(key); } else { HttpContext.Current.Session[key] = value; } } public static UserModel CurrentUser { get { //if not null return, otherwise we will try to restore session... if (CurrentUserView != null) { return CurrentUserView.User; } return null; } } public static UserView CurrentUserView { get { return HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; } set { HttpContext.Current.Session[UserModel.SESSION_USER_KEY] = value; } } public static bool RestoreUserSession(IUserService userService) { UserView AppUser = new UserView(); SleisVisit vist = AppUtility.GetVisit(); if (vist != null && vist.IsAuthenticated) { AppUser.User = userService.GetUser(vist.Id); CurrentUserView = AppUser; return true; } return false; } } }