Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Wrap Panel Virtualization

, 2 Jan 2012 CPOL
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
MediaAssistant_bin.zip
MediaAssistant_src
DokanNet.dll
Id3Lib.dll
Media Assistant.exe
Media Assistant.exe.manifest
MediaAssistant.DAL.dll
MediaFS.dll
MefBasic.dll
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.MefExtensions.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.ServiceLocation.dll
Mp3Lib.dll
Newtonsoft.Json.Net35.dll
SharpZipLib.dll
Tags.dll
MediaAssistant_src.zip
DokanNet
Properties
Lib
CommonServiceLocation
Desktop
Microsoft.Practices.ServiceLocation.dll
CompositeApplicationLibrary
Desktop
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.MefExtensions.dll
Microsoft.Practices.Composite.Presentation.dll
Dokan
dokan.lib
Moq
Desktop
Moq.dll
Mp3Lib
Id3Lib.dll
Mp3Lib.dll
SharpZipLib.dll
Newtonsoft
Newtonsoft.Json.Net35.dll
NUnit
nunit.framework.dll
MediaAssistant
MediaAssistant.csproj.user
MediaAssistant.ico
MusicAssistant_TemporaryKey.pfx
Constants
Controls
About
AddMovie
AlternativeLocation
BottomPanel
Dialog
LeftPanel
Library
MessageBox
MovieDetail
MovieList
MovieMiddlePanel
MovieResult
MovieSearchControl
MovieThumbnails
MusicList
MusicMiddlePanel
MusicSearchControl
PlayerControl
PlayingMusicInfo
PlaylistSelector
ProfileSelector
SendFeedback
SmartDJPreferance
SplashScreen
SplashPage.png
StatusMessageBar
TagEditor
TopPanel
WaitScreen
Converters
Data
EventArguments
HeaderImages
Close_act.png
Close_inact.png
Close_pr.png
Maximize_act.png
Maximize_inact.png
Maximize_pr.png
Minimize_act.png
Minimize_inact.png
Minimize_pr.png
Helper
Images
AcceptFolder.png
Actor.png
ActorLibrary.png
AddFolder.png
Album.png
Artist.png
Close.png
Composer.png
Default.png
DeleteFolder.png
Director.png
DrivesMovies.png
eye16.png
FailedToPlayMusic.png
Genre.png
IMDB.png
Library.png
ListView.png
Logo.png
Maximize.png
Menu.png
Mount.png
MovieBackground.jpg
MovieBackground.png
MovieFolder.png
MovieGenre.png
MovieLibrary.png
MoviePoster.png
Music.png
MusicAssistant.png
MusicLibrary.png
Mute.png
New.png
NewMovies.png
NoDJ.png
NonPlayingMusic.png
NormalShuffle.png
NoShuffle.png
NowPlayingMusic.png
PlayingMusic.png
playlist.png
PlayMovie.png
ProcessDone.png
ProcessFailed.png
Processing.png
Rated.png
Recent.png
RecommendedMovie.png
Restore.png
Runtime.png
SmartShuffle.png
Song.png
Star.png
Swap.png
ThumbnailView.png
Tools.png
TreeCollapse.png
TreeExpand.png
Unread.png
Volume.png
WatchList.png
WishList.png
Writer.png
Year.png
Management
MusicDJ
Properties
Settings.settings
Resources
MediaAssistant.DAL
MediaAssistant.sdf
MediaAssistantModels.edmx
Constants
DBUpdateScript
Helper
Properties
Settings.settings
DataSources
MusicAssistantEntities.datasource
TasteKidEntities.datasource
Virtualization
MediaAssistant.Setup
MusicAssistant.Setup.vdproj
MediaFS
DokanInstall_0.6.0.exe
Properties
MefBasic
Behaviors
Commans
Controls
Converters
Data
Extensions
Helper
Properties
Settings.settings
Themes
Threading
UserControls
Properties
TagClass
ASF Classes
Frames Classes
ID3
Properties
Text Files
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
using System.Linq;
using MediaAssistant.DAL.Constants;
using MediaAssistant.DAL.Helper;
using Newtonsoft.Json.Linq;
using Tags.ASF;
using Tags.ID3;

namespace MediaAssistant.DAL
{
    public partial class MediaAssistantEntities
    {
        public static bool IsUpdatedVersion { get; private set; }
        public const string Unknown = "Unknown";

        private static Music CreateMusic(string fileName)
        {
            var fileInfo = new FileInfo(fileName);
            var music = new Music
            {
                FileName = fileInfo.Name,
                FullPath = fileName,
                Size = fileInfo.Length,
                SizeText = Utility.GetFileSize(fileInfo.Length),
                Score = 0
            };
            UpdateTags(music);
            return music;
        }

        private static void UpdateTags(Music music)
        {
            try
            {
                var filePath = music.FullPath;
                var extension = Path.GetExtension(filePath);
                if (extension == null)
                    return;
                switch (extension.ToLower())
                {
                    case ".mp3":
                        ReadMp3FileTags(music, filePath);
                        break;
                    case ".wma":
                        ReadWmaFileTags(music, filePath);
                        break;
                }

            }
            catch (Exception)
            {
                //ID3 tag not found
            }
        }

        private static void ReadWmaFileTags(Music music, string filePath)
        {
            //WMA File: Title:Title;Album:WM/AlbumTitle;Artist:WM/AlbumArtist;Mood:WM/Mood;Genre:WM/Genre;Initial Key:WM/InitialKey;Language:WM/Language;Category:WM/Category;Conductor:WM/Conductor;Description:Description;Track Number:WM/TrackNumber;Part Of Set:WM/PartOfSet;Playlist Delay:WM/PlaylistDelay;Sub Title:WM/SubTitle;Sub Title description:WM/SubTitleDescription;Rating:Rating;Author:Author;Writer:WM/Writer;Director:WM/Director;Original Album Title:WM/OriginalAlbumTitle;Original Artist:WM/OriginalArtist;Original FileName:WM/OriginalFilename;Original Lyricist:WM/OriginalLyricist;Original Release Time:WM/OriginalReleaseTime;Original Release Year:WM/OriginalReleaseYear;Publisher:WM/Publisher;Producer:WM/Producer;Modified By:WM/ModifiedBy;Encoded By:WM/EncodedBy;ISRC:WM/ISRC;Copyright:Copyright;Radio Station Name:WM/RadioStationName;Radio Station Owner:WM/RadioStationOwner;Tool Name:WM/ToolName;Tool Version:WM/ToolVersion;FileName:;Path:
            var wmaFile = new ASFTagInfo(filePath, true);
            if (String.IsNullOrWhiteSpace(wmaFile.ContentDescription.Title) == false)
                music.Title = wmaFile.ContentDescription.Title;
            else
                music.Title = wmaFile.ExContentDescription["Title"] as string;
            music.Album = wmaFile.ExContentDescription["WM/AlbumTitle"] as string;
            music.Genre = wmaFile.ExContentDescription["WM/Genre"] as string;
            music.Artist = wmaFile.ExContentDescription["WM/AlbumArtist"] as string;
        }

        private static void ReadMp3FileTags(Music music, string filePath)
        {
            var id3 = new ID3Info(filePath, true);
            if (id3.ID3v2Info.HaveTag)
            {
                //MP3 File: Album:TALB;BPM ( Beats Per Minutes):TBPM;Composer:TCOM;Genre:TCON;Copyright Message:TCOP;Date:TDAT;Encoding Time:TDEN;Playlist Delay:TDLY;Orginal Release Time:TDOR;Recording Time:TDRC;Release Time:TDRL;Tagging Time:TDTG;Encoded By:TENC;Lyric/Text Writer:TEXT;File Type:TFLT;Time:TIME;Involved People List:TIPL;Content Group Description:TIT1;Title:TIT2;Subtitle/Desripction:TIT3;Initial Key:TKEY;Language:TLAN;Length:TLEN;Musician Credits List:TMCL;Media Type:TMED;Mood:TMOO;Orginal Title:TOAL;Orginal Filename:TOFN;Orginal Lyricist:TOLY;Orginal Artist:TOPE;Orginal Release Year:TORY;File Owner:TOWN;Lead Artist:TPE1;Band Artist:TPE2;Conductor:TPE3;Interpreted:TPE4;Part of set:TPOS;Produced Notice:TPRO;Publisher:TPUB;Track Number:TRCK;Recording Date:TRDA;Internet Radio Station Name:TRSN;Internet Radio Station Owner:TRSO;Size:TSIZ;Album Sort Order:TSOA;Preformer Sort Order:TSOP;Title Sort Order:TSOT;ISRC:TSRC;Software/Hardware Used For Encoding:TSSE;Set Subtitle:TSST;Year:TYER;File Name:;Tag Size:;ID3v1:;ID3v2:;Path:
                music.Album = id3.ID3v2Info.GetTextFrame("TALB");
                music.Artist = id3.ID3v2Info.GetTextFrame("TPE1");
                music.Genre = id3.ID3v2Info.GetTextFrame("TCON");
                music.Composer = id3.ID3v2Info.GetTextFrame("TCOM");
                music.Title = id3.ID3v2Info.GetTextFrame("TIT2");
            }
            else if (id3.ID3v1Info.HaveTag)
            {
                music.Album = id3.ID3v1Info.Album;
                music.Artist = id3.ID3v1Info.Artist;
                music.Title = id3.ID3v1Info.Title;
            }
        }
        public void UpdateMusic(Music music)
        {
            UnlinkMusic(music);
            LinkMusic(music);
            UpdateFileTag(music);
        }

        private static void UpdateFileTag(Music music)
        {
            if (File.Exists(music.FullPath))
            {
                var extension = Path.GetExtension(music.FileName);
                if (extension == null)
                    return;
                if (extension.ToLower() == ".mp3")
                {
                    UpdateMp3FileTag(music);
                }
                else if (extension.ToLower() == ".wma")
                {
                    UpdateWmaFileTag(music);
                }
            }
        }

        private static void UpdateWmaFileTag(Music music)
        {
            var wmaFile = new ASFTagInfo(music.FullPath, true);
            if (String.IsNullOrWhiteSpace(wmaFile.ContentDescription.Title) == false)
                wmaFile.ContentDescription.Title = music.Title ?? string.Empty;
            else
                wmaFile.ExContentDescription["Title"] = music.Title ?? string.Empty;
            wmaFile.ExContentDescription["WM/AlbumTitle"] = music.Album ?? string.Empty;
            wmaFile.ExContentDescription["WM/Genre"] = music.Genre ?? string.Empty;
            wmaFile.ExContentDescription["WM/AlbumArtist"] = music.Artist ?? string.Empty;
            wmaFile.Save();
        }

        private static void UpdateMp3FileTag(Music music)
        {
            var id3 = new ID3Info(music.FullPath, true);
            id3.ID3v2Info.SetTextFrame("TALB", music.Album ?? string.Empty);
            id3.ID3v2Info.SetTextFrame("TPE1", music.Artist ?? string.Empty);
            id3.ID3v2Info.SetTextFrame("TCON", music.Genre ?? string.Empty);
            id3.ID3v2Info.SetTextFrame("TCOM", music.Composer ?? string.Empty);
            id3.ID3v2Info.SetTextFrame("TIT2", music.Title ?? string.Empty);
            id3.ID3v2Info.Save();
        }

        public void CreateFixedLibraryItems()
        {
            var index = 1;
            CreateLibraryItem(LibraryItemType.MovieLibrary, LibraryItemType.MovieLibrary, "Movies", index++);
            CreateLibraryItem(LibraryItemType.MovieGenreLibrary, LibraryItemType.MovieGenreLibrary, "Genres", index++);
            CreateLibraryItem(LibraryItemType.ActorLibrary, LibraryItemType.ActorLibrary, "Stars", index++);
            CreateLibraryItem(LibraryItemType.DirectorLibrary, LibraryItemType.DirectorLibrary, "Directors", index++);
            CreateLibraryItem(LibraryItemType.WriterLibrary, LibraryItemType.WriterLibrary, "Writers", index++);
            CreateLibraryItem(LibraryItemType.YearLibrary, LibraryItemType.YearLibrary, "Years", index++);
            CreateLibraryItem(LibraryItemType.RatedLibrary, LibraryItemType.RatedLibrary, "Rated", index++);

            CreateLibraryItemForType(LibraryItemType.UnreadMovieLibrary, LibraryItemType.UnreadMovieLibrary, "New", index++);
            CreateLibraryItemForType(LibraryItemType.StaredMovieLibrary, LibraryItemType.StaredMovieLibrary, "Favorit", index++);
            CreateLibraryItemForType(LibraryItemType.RecommendedMovieLibrary, LibraryItemType.RecommendedMovieLibrary, "Recommended", index++);
            CreateLibraryItemForType(LibraryItemType.WatchListLibrary, LibraryItemType.WatchListLibrary, "Watched", index++);
            CreateLibraryItemForType(LibraryItemType.WishListLibrary, LibraryItemType.WishListLibrary, "Wish List", index++);
            CreateLibraryItemForType(LibraryItemType.IMDbMustWatchList, LibraryItemType.IMDbMustWatchList, "Must Watch", index++);
            CreateLibraryItemForType(LibraryItemType.RecentlyPlayedMovieLibrary, LibraryItemType.RecentlyPlayedMovieLibrary, "Recently Played", index++);
            CreateLibraryItemForType(LibraryItemType.ProcessingLibrary, LibraryItemType.ProcessingLibrary, "Processing", index++);
            CreateLibraryItemForType(LibraryItemType.FailedLibrary, LibraryItemType.FailedLibrary, "Failed", index++);

            CreateLibraryItem(LibraryItemType.MusicLibrary, LibraryItemType.MusicLibrary, "Musics", index++);
            CreateLibraryItem(LibraryItemType.GenreLibrary, LibraryItemType.GenreLibrary, "Genres", index++);
            CreateLibraryItem(LibraryItemType.AlbumLibrary, LibraryItemType.AlbumLibrary, "Albums", index++);
            CreateLibraryItem(LibraryItemType.ArtistLibrary, LibraryItemType.ArtistLibrary, "Artists", index++);
            CreateLibraryItem(LibraryItemType.ComposerLibrary, LibraryItemType.ComposerLibrary, "Composers", index++);

            CreateLibraryItem(LibraryItemType.PlayListLibrary, LibraryItemType.PlayListLibrary, "Playlists", index++);
            CreateLibraryItem(LibraryItemType.NowPlayingPlaylist, LibraryItemType.Playlist, LibraryItemType.NowPlayingPlaylist, index++);
            CreateLibraryItem(LibraryItemType.LastImportedPlaylist, LibraryItemType.Playlist, LibraryItemType.LastImportedPlaylist, index);
        }
        public Movie UpdateMovie(Movie movie, JObject jsonObject)
        {
            var imdbId = (string)jsonObject.SelectToken("ID");
            var exisingMovie = Movies.FirstOrDefault(m => m.IMDBId == imdbId);    
            var resultMovie = movie;
            if (exisingMovie != null && exisingMovie!=movie)
            {
                if (string.IsNullOrWhiteSpace(movie.FullPath)==false)
                {
                    UpdateExistingMovie(exisingMovie, movie);
                }
                DeleteMovie(movie);
                resultMovie = exisingMovie;
            }
            else
            {
                UpdateNewMovie(jsonObject, imdbId, movie);
            }
            
            SaveChanges();
            return resultMovie;
        }

        private void UpdateNewMovie(JObject jsonObject, string imdbId, Movie movie)
        {
            movie.UpdateMovie(jsonObject);
            movie.IMDBId = imdbId;
            movie.PosterImage = null;
            LinkMovie(movie, jsonObject);
            movie.Status = MovieStatus.Done;
        }

        private void DeleteMovie(Movie movie)
        {
            if (movie.EntityState == EntityState.Detached)
                return;
            UnLinkMovie(movie);
            foreach (var profileMovie in movie.ProfileMovies.ToArray())
            {
                ProfileMovies.DeleteObject(profileMovie);
            }
            foreach (var location in movie.AlternativeLocations.ToArray())
            {
                AlternativeLocations.DeleteObject(location);
            }
            Movies.DeleteObject(movie);
        }

        private void UpdateExistingMovie(Movie exisingMovie, Movie movie)
        {
            var fileInfo = new FileInfo(movie.FullPath);
            if (string.IsNullOrWhiteSpace(exisingMovie.FullPath))
            {
                exisingMovie.FullPath = movie.FullPath;
                exisingMovie.FileName = movie.FileName;
                exisingMovie.Size = fileInfo.Length;
                exisingMovie.SizeText = Utility.GetFileSize(fileInfo.Length);
            }
            else if (exisingMovie.AlternativeLocations.Any(l => l.Location == movie.FullPath) == false)
            {
                var alternativeLocation = new AlternativeLocation
                {
                    Id = AlternativeLocations.GetNextId(),
                    Movie = exisingMovie,
                    Location = movie.FullPath
                };
                AlternativeLocations.AddObject(alternativeLocation);
            }
            exisingMovie.Index = movie.Index;
        }

        public void AddFile(string fileName)
        {
            if (Utility.IsMusicFile(fileName))
            {
                AddMusicFile(fileName);
            }
            else if (Utility.IsMovieFile(fileName))
            {
                AddMovieFile(fileName);
            }
        }

        public void AddMovieFile(string fileName)
        {
            if (IsMovieFileAdded(fileName))
                return;
            var movie = CreateMovie(fileName);
            if (movie.Size < Utility.MinMovieFileSize)
                return;
            movie.Id = Movies.GetNextId();
            Movies.AddObject(movie);
            SaveChanges();
            
        }

        public bool IsMovieFileAdded(string fileName)
        {
            return Movies.Any(o => String.Compare(o.FullPath, fileName, true) == 0);
        }

        public IEnumerable<Movie> UpdateTitles(bool showDrivesMoviesOnly, Profile selectedProfile)
        {
            var processingMovies=UpdateProcessingTitle(showDrivesMoviesOnly);

            UpdateFailedTitle(showDrivesMoviesOnly);

            UpdateNewMovieTitle(showDrivesMoviesOnly, selectedProfile);

            UpdateFevoriteMovieTitle(showDrivesMoviesOnly, selectedProfile);

            UpdateWatchedMovieTitle(showDrivesMoviesOnly, selectedProfile);

            UpdateWishListTitle(showDrivesMoviesOnly, selectedProfile);

            UpdateRecommendedMovieTitle(showDrivesMoviesOnly, selectedProfile);
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.IMDbMustWatchList);
            libraryItem.Title = string.Format("Must Watch ({0})", libraryItem.Movies.Where(m=>m.CanShow(showDrivesMoviesOnly)).Count());
            return processingMovies;
        }

        public void UpdateRecommendedMovieTitle(bool showDrivesMoviesOnly, Profile selectedProfile)
        {
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.RecommendedMovieLibrary);
            libraryItem.Title = string.Format("Recommended ({0})", GetRecommendedMovies(selectedProfile).Where(m => m.CanShow(showDrivesMoviesOnly)).Count());
        }

        public void UpdateWishListTitle(bool showDrivesMoviesOnly, Profile selectedProfile)
        {
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.WishListLibrary);
            libraryItem.Title = string.Format("Wish List ({0})", GetWishListMovie(selectedProfile).Where(m => m.CanShow(showDrivesMoviesOnly)).Count());
        }

        public void UpdateWatchedMovieTitle(bool showDrivesMoviesOnly, Profile selectedProfile)
        {
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.WatchListLibrary);
            libraryItem.Title = string.Format("Watched ({0})", GetWatchedMovies(selectedProfile).Where(m => m.CanShow(showDrivesMoviesOnly)).Count());
        }

        public void UpdateFevoriteMovieTitle(bool showDrivesMoviesOnly, Profile selectedProfile)
        {
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.StaredMovieLibrary);
            libraryItem.Title = string.Format("Favorite ({0})", GetStarMovies(selectedProfile).Where(m => m.CanShow(showDrivesMoviesOnly)).Count());
        }

        public void UpdateNewMovieTitle(bool showDrivesMoviesOnly, Profile selectedProfile)
        {
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.UnreadMovieLibrary);
            libraryItem.Title = string.Format("New ({0})", GetNewMovies(selectedProfile).Where(m => m.CanShow(showDrivesMoviesOnly)).Count());
        }

        public void UpdateFailedTitle(bool showDrivesMoviesOnly)
        {
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.FailedLibrary);
            libraryItem.Title = string.Format("Failed ({0})", GetFailedMovies().Where(m => m.CanShow(showDrivesMoviesOnly)).Count());
        }

        public IEnumerable<Movie> UpdateProcessingTitle(bool showDrivesMoviesOnly)
        {
            var libraryItem = GetOrCreateFixedLibraryItem(LibraryItemType.ProcessingLibrary);
            var movies = GetProcessingMovies().Where(m=> m.CanShow(showDrivesMoviesOnly));
            libraryItem.Title = string.Format("Processing ({0})", movies.Count());
            return movies;
        }

        public IEnumerable<Movie> GetRecommendedMovies(Profile selectedProfile)
        {
            var profileMovieIds = selectedProfile.ProfileMovies.Where(pm => pm.Watched==false && pm.Recommendation != null && pm.Recommendation > 0).OrderByDescending(pm=>pm.Recommendation).Select(pm => pm.MovieId).ToList();
            return Movies.Where(m => profileMovieIds.Contains(m.Id)).ToList();
        }
        public IEnumerable<Movie> GetProcessingMovies()
        {
            return Movies.Where(m => m.Status == MovieStatus.Pending || m.Status == MovieStatus.Processing);
        }


        private static Movie CreateMovie(string fileName)
        {
            var fileInfo = new FileInfo(fileName);
            var movie = new Movie
            {
                FileName = fileInfo.Name,
                FullPath = fileName,
                Size = fileInfo.Length,
                SizeText = Utility.GetFileSize(fileInfo.Length),
                CreateDate = DateTime.Now,
                Status = MovieStatus.Pending
            };
            return movie;
        }

        public void AddMusicFile(string fileName)
        {
            if (IsMusicFileAdded(fileName))
                return;
            var music = CreateMusic(fileName);
            music.Id = Musics.GetNextId();
            Musics.AddObject(music);
            LinkMusic(music);
            AddMusicToLibrary(music, GetOrCreateFixedLibraryItem(LibraryItemType.LastImportedPlaylist));
            SaveChanges();
        }

        public bool IsMusicFileAdded(string fileName)
        {
            return Musics.Any(o => String.Compare(o.FullPath, fileName, true) == 0);
        }

        public void LinkMovie(Movie movie, JObject jsonObject)
        {
            UnLinkMovie(movie);
            AddMovieToLibrary(jsonObject, movie, "Genre", LibraryItemType.MovieGenreLibrary, LibraryItemType.MovieGenre);
            AddMovieToLibrary(jsonObject, movie, "Director", LibraryItemType.DirectorLibrary, LibraryItemType.Director);
            AddMovieToLibrary(jsonObject, movie, "Writer", LibraryItemType.WriterLibrary, LibraryItemType.Writer);
            AddMovieToLibrary(jsonObject, movie, "Actors", LibraryItemType.ActorLibrary, LibraryItemType.Actor);
            if (string.IsNullOrWhiteSpace(movie.Rated) == false)
                AddMovieToLibrary(movie.Rated, LibraryItemType.RatedLibrary, LibraryItemType.Rated, movie);
            if (movie.Year.HasValue)
            {
                AddMovieToLibrary(movie.Year.ToString(), LibraryItemType.YearLibrary, LibraryItemType.Year, movie);
            }
            movie.UpdateLibraryInformation();
            SaveChanges();
        }

        private static void UnLinkMovie(Movie movie)
        {
            foreach (var libraryItem in movie.LibraryItems.ToArray())
            {
                libraryItem.Movies.Remove(movie);
            }
        }

        private void AddMovieToLibrary(JObject jsonObject, Movie movie, string tokenName, string parentLibraryType, string libraryItemType)
        {
            var tokens = (string)jsonObject.SelectToken(tokenName);
            foreach (var token in tokens.Split(',').Select(s => s.Trim(' ')))
            {
                AddMovieToLibrary(token, parentLibraryType, libraryItemType, movie);
            }
        }

        public void AddMovieToLibrary(string title, string parentLibraryType, string libraryItemType, Movie movie)
        {
            var tokenTitle = GetTitle(title);
            var libraryItem = CreateLibraryItem(GetOrCreateFixedLibraryItem(parentLibraryType), tokenTitle, libraryItemType, null);    
            if (libraryItem.Movies.Contains(movie)) return;
            libraryItem.Movies.Add(movie);
        }

        public void LinkMusic(Music music)
        {
            AddToLibrary(music, music.Genre, LibraryItemType.Genre);
            AddToLibrary(music, music.Artist, LibraryItemType.Artist);
            AddToLibrary(music, music.Album, LibraryItemType.Album);
            AddToLibrary(music, music.Composer, LibraryItemType.Composer);
            SaveChanges();
        }

        private void AddToLibrary(Music music, string title, string type)
        {
            var genreTitle = GetTitle(title);
            var genre = CreateLibraryItem(GetOrCreateFixedLibraryItem(LibraryItemType.GetParentType(type)), genreTitle, type, null);
            AddMusicToLibrary(music, genre);
        }

        public void UnlinkMusic(Music music)
        {
            foreach (var libraryItem in music.LibraryItems.Where(o=>o.Type!=LibraryItemType.Playlist).ToArray())
            {
                music.LibraryItems.Remove(libraryItem);
                libraryItem.Musics.Remove(music);
                if(libraryItem.Musics.Count==0)
                {
                    RemoveLibraryItem(libraryItem);
                }
                SaveChanges();
            }
        }

        public void RemoveLibraryItem(LibraryItem libraryItem)
        {
            foreach (var music in libraryItem.Musics.ToArray())
            {
                libraryItem.Musics.Remove(music);
            }
            foreach (var movie in libraryItem.Movies.ToArray())
            {
                libraryItem.Movies.Remove(movie);
            }
            foreach (var parent in libraryItem.Parents.ToArray())
            {
                parent.Children.Remove(libraryItem);
            }
            foreach (var child in libraryItem.Children.ToArray())
            {
                child.Parents.Remove(libraryItem);
            }
            LibraryItems.DeleteObject(libraryItem);
        }

        private static string GetTitle(string actualTitle)
        {
            return String.IsNullOrWhiteSpace(actualTitle) ? Unknown : actualTitle.Trim();
        }

        private void AddMusicToLibrary(Music music, LibraryItem album)
        {
            if (album.Musics.Contains(music) == false)
            {
                album.Musics.Add(music);
                SaveChanges();
            }
        }

        private LibraryItem CreateLibraryItem(LibraryItem parent, string title, string childType, string mainLibraryType)
        {
            var child = parent.Children.FirstOrDefault(a =>  string.Compare(a.Title,title,true)==0);
            if (child == null)
            {
                child = new LibraryItem { Id = Guid.NewGuid().ToString(), Title = title, Type = childType, Index = -1, HasChildren = false};
                parent.Children.Add(child);
                parent.HasChildren = true;
                if (mainLibraryType != null)
                {
                    var mainLibrary = GetOrCreateFixedLibraryItem(mainLibraryType);
                    mainLibrary.Children.Add(child);
                    mainLibrary.HasChildren = true;
                }
                LibraryItems.AddObject(child);
                SaveChanges();
            }
            return child;
        }

        public LibraryItem CreateLibraryItem(string type, string title)
        {
            return CreateLibraryItem(Guid.NewGuid().ToString(), type, title, -1);
        }
        private LibraryItem CreateLibraryItem(string id, string type, string title, int index)
        {
            var item = LibraryItems.FirstOrDefault(o => o.Type == type && string.Compare(o.Title,title,true)==0);
            if (item == null)
            {
                item = new LibraryItem { Id = id, Title = title, Type = type, Index = index, HasChildren = false};
                if (LibraryItemType.IsRootItem(type)==false)
                {
                    var parent = GetOrCreateFixedLibraryItem(LibraryItemType.GetParentType(type));
                    item.Parents.Add(parent);
                    parent.HasChildren = true;
                }
                LibraryItems.AddObject(item);
                SaveChanges();
            }
            return item;
        }
        private void CreateLibraryItemForType(string id, string type, string title, int index)
        {
            var item = LibraryItems.FirstOrDefault(o => o.Type == type);
            if (item == null)
            {
                item = new LibraryItem { Id = id, Title = title, Type = type, Index = index, HasChildren = false };
                if (LibraryItemType.IsRootItem(type)==false)
                {
                    var parent = GetOrCreateFixedLibraryItem(LibraryItemType.GetParentType(type));
                    item.Parents.Add(parent);
                    parent.HasChildren = true;
                }
                LibraryItems.AddObject(item);
                SaveChanges();
            }
            else
            {
                item.Index = index;
                SaveChanges();
            }
            return;
        }
        public LibraryItem GetOrCreateFixedLibraryItem(string type)
        {
            var item = LibraryItems.FirstOrDefault(o => o.Id == type);
            if (item == null)
            {
                item = new LibraryItem { Id = type, Title = type, Type = type, Index = -1, HasChildren = false };
                LibraryItems.AddObject(item);
                if (LibraryItemType.IsRootItem(type)==false)
                {
                    var parent = GetOrCreateFixedLibraryItem(type == LibraryItemType.Playlist ? LibraryItemType.Playlist : LibraryItemType.GetParentType(type));
                    parent.Children.Add(item);
                    parent.HasChildren = true;
                }
            }
            return item;
        }
        public LibraryItem GetFixedLibraryItem(string type)
        {
            return LibraryItems.FirstOrDefault(o => o.Id == type);
        }

        /// <exception cref="Exception"><c>Exception</c>.</exception>
        public static void UpdateDatabase()
        {
            var appDataLocation = SystemHelper.GetAppDataLocation();
            AppDomain.CurrentDomain.SetData("DataDirectory", appDataLocation);
            if (!File.Exists(Path.Combine(appDataLocation, "MediaAssistant.sdf")) || !IsSupportedVersion())
            {
                ResourceHelper.CopyEmbededResource("MediaAssistant.DAL.MediaAssistant.sdf", Path.Combine(appDataLocation, "MediaAssistant.sdf"));
            }
            else
            {
                IsUpdatedVersion = IsDatabaseUpdatedVersion();
                if (IsUpdatedVersion == false)
                {
                    RunUpdateScript();
                }
            }
        }

        private static bool IsSupportedVersion()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["MediaAssistantDB"].ConnectionString;
            var connection = new SqlCeConnection(connectionString);
            try
            {
                connection.Open();
                var command = new SqlCeCommand("Select * from DBUpdate", connection);
                command.ExecuteReader();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

        private static bool IsDatabaseUpdatedVersion()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["MediaAssistantDB"].ConnectionString;
            var connection = new SqlCeConnection(connectionString);
            try
            {
                connection.Open();
                var command = new SqlCeCommand(string.Format("Select * from DBUpdate where Version='{0}'",CurrentVersion), connection);
                return command.ExecuteReader().Read();
            }
            catch
            {
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

        private static void RunUpdateScript()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["MediaAssistantDB"].ConnectionString;
            var connection = new SqlCeConnection(connectionString);
            try
            {
                connection.Open();
                var updateScript = GetUpdateScript();
                var commandScripts = updateScript.Split(";".ToCharArray()).Where(s => string.IsNullOrWhiteSpace(s) == false);
                foreach (var commandScript in commandScripts)
                {
                    if(commandScript.Length<5)
                        continue;
                    try
                    {
                        var command = new SqlCeCommand(commandScript, connection);
                        command.ExecuteNonQuery();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {

            }
            finally
            {
                connection.Close();
            }

        }

        private static string GetUpdateScript()
        {
            return ResourceHelper.ReadText("MediaAssistant.DAL.DBUpdateScript.UpdateScript.sql");
        }

        public static string GetConnectionString()
        {
            return string.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename=""{0}"";Integrated Security=True;Connect Timeout=30;User Instance=True", Path.Combine(SystemHelper.GetAppDataLocation(), "MediaAssistantDB.mdf"));
        }

        public ProfileMovie GetOrCreateProfileMovie(Movie movie, Profile selectedProfile)
        {
            if (movie == null)
            {
                return null;
            }
            var profileMovie = selectedProfile.ProfileMovies.FirstOrDefault(pm => pm.MovieId == movie.Id);
            if (profileMovie == null)
            {
                profileMovie = new ProfileMovie
                {
                    Movie = movie,
                    Profile = selectedProfile,
                };
                ProfileMovies.AddObject(profileMovie);
                SaveChanges();
            }
            return profileMovie;
        }
        public IEnumerable<Movie> GetNewMovies(Profile selectedProfile)
        {
            return (from movie in Movies.Where(m=>m.FullPath!=null).ToArray()
                    let profileMovie = movie.GetProfileMovie(selectedProfile)
                    where profileMovie == null || profileMovie.IsNewMovie()
                    select movie).ToList();
        }
        public IEnumerable<Movie> GetRecentlyPlayedMovies(Profile profile)
        {
            return (from movie in Movies.Where(m => m.FullPath != null).ToArray()
                    let profileMovie = movie.GetProfileMovie(profile)
                    where profileMovie != null && profileMovie.IsRecentlyPlayedMovie()
                    orderby profileMovie.LastPlayed descending 
                    select movie).ToList();
        }

        public IEnumerable<Movie> GetWatchedMovies(Profile selectedProfile)
        {
            var profileMovieIds = selectedProfile.ProfileMovies.Where(pm => pm.Watched).Select(pm => pm.MovieId).ToList();
            var movies = new List<Movie>();
            foreach (var movie in Movies.Where(m => profileMovieIds.Contains(m.Id)))
            {
                movie.Watched = true;
                movies.Add(movie);
            }
            return movies;
        }
        public IEnumerable<Movie> GetWishListMovie(Profile selectedProfile)
        {
            var profileMovieIds = selectedProfile.ProfileMovies.Where(pm => pm.IsInWishList.HasValue && pm.IsInWishList.Value).Select(pm => pm.MovieId).ToList();
            var movies = new List<Movie>();
            foreach (var movie in Movies.Where(m => profileMovieIds.Contains(m.Id)))
            {
                movie.IsInWishList = true;
                movies.Add(movie);
            }
            return movies;
        }
        public IEnumerable<Movie> GetStarMovies(Profile selectedProfile)
        {
            var profileMovieIds = selectedProfile.ProfileMovies.Where(pm => pm.Star > 0).Select(pm => pm.MovieId).ToList();
            var movies = new List<Movie>();
            foreach (var movie in Movies.Where(m => profileMovieIds.Contains(m.Id)))
            {
                movie.IsStarred = true;
                movies.Add(movie);
            }
            return movies;
        }

        public IEnumerable<Movie> GetFailedMovies()
        {
            return Movies.Where(m => m.Status == MovieStatus.Failed);
        }

        public IEnumerable<string> GetRecommendedMovieTitles(string movieTitle)
        {
            if (string.IsNullOrWhiteSpace(movieTitle))
                return new string[] {};
            try
            {
                var recommendedMovies = RecommendedMovies.Where(m => m.QueryTitle == movieTitle);
                if (recommendedMovies.Count() > 0)
                    return recommendedMovies.Select(m => m.RecommendedMovieTitle);

                var content = WebRequestHelper.GetContent(String.Format(URL, movieTitle.Replace(" ", "+")));
                var jsonObject = JObject.Parse(content);
                var jsonArray = jsonObject["Similar"]["Results"];
                var recommendedMovieTitles = jsonArray.Select(token => (string)token.SelectToken("Name")).Where(name => String.IsNullOrWhiteSpace(name) == false).ToList();
                if (recommendedMovieTitles.Count == 0)
                    recommendedMovieTitles.Add(RecommendationNotFound);
                foreach (var recommendedTitle in recommendedMovieTitles)
                {
                    AddRecommendation(movieTitle, recommendedTitle);
                }
                return recommendedMovieTitles;
            }
            catch (Exception)
            {
                return new string[] { };
            }
        }

        private void AddRecommendation(string movieTitle, string recommendedTitle)
        {
            var movieRecommendation = new RecommendedMovie
            {
                QueryTitle = movieTitle,
                RecommendedMovieTitle = recommendedTitle,
                Id = RecommendedMovies.GetNextId()
            };
            RecommendedMovies.AddObject(movieRecommendation);
            SaveChanges();
        }

        private const string URL = @"http://www.tastekid.com/ask/ws?q={0}&f=musica1363&k=mjc3njgwogu2&type=movie&format=JSON";
        public const string RecommendationNotFound = "Recommendation not found";
        private const string CurrentVersion = "1.4";

        public void AddMovie(Movie movie)
        {
            movie.Id = Movies.GetNextId();
            Movies.AddObject(movie);
            SaveChanges();
        }

        public bool IsIMDb250WishListExists()
        {
            return LibraryItems.Any(o=>o.Type==LibraryItemType.IMDbMustWatchList);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

H. S. Masud
Software Developer (Senior) KAZ Software Limited
Bangladesh Bangladesh
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 2 Jan 2012
Article Copyright 2012 by H. S. Masud
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid