Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Xml.Linq;
public class Data {
const int PopularCountriesCount = 20;
const string SearchQuerySID = "SQ";
const string AllHotelsSID = "AH";
public static Data Instance {
get {
if(HttpContext.Current.Items["DG"] == null)
HttpContext.Current.Items["DG"] = new Data();
return (Data)HttpContext.Current.Items["DG"];
}
}
public Data() {
FakeDataHelper = new FakeDataHelper();
LoadXmlData();
}
public SearchQuery CurrentQuery {
get { return Session[SearchQuerySID] as SearchQuery; }
set { Session[SearchQuerySID] = value; }
}
// Data
public List<Country> Countries { get; protected set; }
public List<string> HotelServices { get; protected set; }
public List<string> RoomServices { get; protected set; }
public List<string> RoomTypes { get; protected set; }
public List<string> CommonDescriptions { get; protected set; }
public List<string> HotelDescriptions { get; protected set; }
public List<string> RoomDescriptions { get; protected set; }
public List<int> RoomTypesMaxChildrenCount { get; protected set; }
public List<int> HotelStars { get; protected set; }
public List<Hotel> Hotels {
get {
if(Session[AllHotelsSID] == null)
Session[AllHotelsSID] = new List<Hotel>();
return (List<Hotel>)Session[AllHotelsSID];
}
}
public IEnumerable<Country> GetPopularCountries() {
return FakeDataHelper.GetPopularCountries(Countries, PopularCountriesCount);
}
public List<Hotel> GetExclusiveHotels() {
if(!Hotels.Any(hotel => hotel.IsExclusive))
Hotels.AddRange(FakeDataHelper.GetHotels(SearchQuery.MostExclusive, TitleGroups, 10));
return Hotels.Where(hotel => hotel.IsExclusive).ToList();
}
public IEnumerable<Hotel> GetSearchResults() {
FakeDataHelper.UpdateHotelsList(TitleGroups);
return Hotels.Where(h => CurrentQuery.IsMatch(h));
}
public void ClearMemory() {
Session.Clear();
Session.Timeout = 3 * 24 * 60;
}
protected List<XElement> TitleGroups { get; set; }
protected FakeDataHelper FakeDataHelper { get; set; }
protected HttpSessionState Session {
get { return HttpContext.Current.Session; }
}
// Xml load
void LoadXmlData() {
Countries = LoadXmlDataInternal<Country>("Countries", "HotelDestinations",
el => new Country(
el.Attribute("Title").Value,
el.Attribute("Image").Value,
el.Elements().Select(c => c.Value).OrderBy(oc => oc),
FakeDataHelper.GetPopularity(el.Attribute("MostPopular") != null)
));
HotelServices = LoadXmlDataInternal("HotelParameters", "HotelServices");
RoomServices = LoadXmlDataInternal("HotelParameters", "RoomServices");
RoomTypes = LoadXmlDataInternal("HotelParameters", "RoomTypes");
RoomTypesMaxChildrenCount = LoadXmlDataInternal("HotelParameters", "RoomTypes", "Children").ConvertAll<int>(v => Convert.ToInt32(v));
HotelStars = new List<int>(new int[] { 1, 2, 3, 4, 5 });
TitleGroups = LoadXmlDataInternal("TitleWordsDictionary", "Dictionary", el => el);
CommonDescriptions = LoadXmlDataInternal("HotelParameters", "CommonDesc", "Text");
HotelDescriptions = LoadXmlDataInternal("HotelParameters", "HotelDesc", "Text");
RoomDescriptions = LoadXmlDataInternal("HotelParameters", "RoomDesc", "Text");
}
List<string> LoadXmlDataInternal(string fileName, string rootNodeName) {
return LoadXmlDataInternal(fileName, rootNodeName, "Title");
}
List<string> LoadXmlDataInternal(string fileName, string rootNodeName, string attrName) {
return LoadXmlDataInternal(fileName, rootNodeName, el => el.Attribute(attrName).Value);
}
List<T> LoadXmlDataInternal<T>(string fileName, string rootNodeName, Func<XElement, T> getElement) {
XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(string.Format("~\\App_Data\\{0}.xml", fileName)));
return (from node in doc.Root.Element(rootNodeName).Elements()
select getElement(node)).ToList();
}
}