// VideoLibrary DEMO - DaST Technology Demonstrator
// Copyright (C) 2011 Roman Gubarenko
// http://www.rgubarenko.net
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Licence terms for AspNetDaST.dll (in Bin folder) are below.
//
// ASP.NET DaST Rendering Engine (BETA version)
// Copyright (C) 2011 Roman Gubarenko
// http://www.rgubarenko.net
//
// Redistribution and use of BETA version of DaST Rendering Engine
// is permitted in binary form without modification. Only original and
// unmodified .NET dll (AspNetDaST.dll) can be used in your own projects and
// redistributed. You may not copy, decompile, reverse engineer, disassemble,
// modify, or create derivative works of the software or any part thereof.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.
//
// DaST technology dev site: http://www.rgubarenko.net
// My contact email for questions: rgubarenko@gmail.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Web.Hosting;
using System.IO;
/// <summary>
/// Summary description for DataLayer
/// </summary>
public static class DataLayer
{
private static XElement XmlDoc
{
get
{
var xmlDoc = HttpContext.Current.Session["Data.XmlDocument"] as XElement;
if (xmlDoc == null)
{
string dataPath = HostingEnvironment.MapPath("~/App_Data/Data/VideoLibrarySource.xml");
xmlDoc = XElement.Parse(File.ReadAllText(dataPath));
HttpContext.Current.Session["Data.XmlDocument"] = xmlDoc;
}
return xmlDoc;
}
}
public static int GetVideoItemCount(string videoCategory)
{
return (from video in XmlDoc.Elements("Video")
where videoCategory == null || video.Element("Category").Value == videoCategory
select video).Count();
}
public static object[] GetVideoItems(int startIdx, int count, string videoCategory)
{
return (from video in XmlDoc.Elements("Video")
where videoCategory == null || video.Element("Category").Value == videoCategory
let commElems = video.Element("Comments").Elements("Comment")
select new
{
ID = video.Element("ID").Value,
Name = video.Element("Name").Value,
Category = video.Element("Category").Value,
Description = video.Element("Description").Value,
CommCount = commElems.Count(),
Rating = commElems.Count() > 0 ? commElems.Average(elem => decimal.Parse(elem.Element("Rated").Value)) : 0.0m
}).Skip(startIdx).Take(count).ToArray();
}
public static object GetVideoItem(string videoID)
{
var videoElem = (from video in XmlDoc.Elements("Video")
where video.Element("ID").Value == videoID
select video).Single();
var commElems = videoElem.Element("Comments").Elements("Comment");
return new
{
ID = videoElem.Element("ID").Value,
Name = videoElem.Element("Name").Value,
Category = videoElem.Element("Category").Value,
Description = videoElem.Element("Description").Value,
CommCount = commElems.Count(),
Rating = commElems.Count() > 0 ? commElems.Average(elem => decimal.Parse(elem.Element("Rated").Value)) : 0.0m
};
}
public static object[] GetVideoComments(int startIdx, int count, string videoID)
{
var videoElem = (from video in XmlDoc.Elements("Video")
where video.Element("ID").Value == videoID
select video).Single();
return (from comm in videoElem.Element("Comments").Elements("Comment")
select new
{
CommID = comm.Element("ID").Value,
Date = comm.Element("Time").Value,
Author = comm.Element("Name").Value,
Rated = int.Parse(comm.Element("Rated").Value),
Text = comm.Element("Text").Value
}).Skip(startIdx).Take(count).ToArray();
}
public static void AddNewComment(string videoID, string name, int rated, string comment)
{
var videoElem = (from video in XmlDoc.Elements("Video")
where video.Element("ID").Value == videoID
select video).Single();
videoElem.Element("Comments").Add(new XElement("Comment",
new XElement("ID", Guid.NewGuid()),
new XElement("Time", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")),
new XElement("Name", name),
new XElement("Rated", rated),
new XElement("Text", comment)));
}
#region Playlist simulation
private static List<string> PlaylistVideoItems
{
get
{
var playlist = HttpContext.Current.Session["DaST.VideoLibrary.PlaylistVideoItems"] as List<string>;
if (playlist == null)
{
playlist = new List<string>();
HttpContext.Current.Session["DaST.VideoLibrary.PlaylistVideoItems"] = playlist;
}
return playlist;
}
}
public static int GetPlayItemCount()
{
return PlaylistVideoItems.Count;
}
public static object[] GetPlayItems(int startIdx, int count)
{
var videoIDs = PlaylistVideoItems.Skip(startIdx).Take(count);
return (from videoID in videoIDs
select GetVideoItem(videoID)).ToArray();
}
public static void AddToPlaylist(string videoID)
{
if (!PlaylistVideoItems.Contains(videoID))
{
PlaylistVideoItems.Add(videoID);
}
}
public static void RemoveFromPlaylist(string videoID)
{
if (PlaylistVideoItems.Contains(videoID))
{
PlaylistVideoItems.Remove(videoID);
}
}
public static bool CheckPlaylistContains(string videoID)
{
return PlaylistVideoItems.Contains(videoID);
}
#endregion
public static int[] GetAvailablePageSizes()
{
return new int[] { 3, 5, 10, 20, 40 };
}
public static string[] GetAvailableThemes()
{
//string strPath = HostingEnvironment.MapPath("~/App_Themes");
//string[] dirs = Directory.GetDirectories(strPath);
//return dirs.Select(str => Path.GetFileName(str)).ToArray();
return new string[] { "ui-lightness", "ui-darkness", "start", "sunny", "le-frog", "swanky-purse" };
}
}