public static List <PlaylistSong> CombinationsPlaylist(List <Song> inputList, Playlist playlist, int durationSeconds)
        {
            //Set up the environment for the recursive combinatorial algorithm to work.
            Random      rand       = new Random();
            List <Song> resultList = new List <Song>();
            //Shuffling the list of Songs each time means every Playlist will be different, even if they've
            //got the samew target lengths.
            List <Song>         workingList      = Shuffle(inputList);
            List <PlaylistSong> playlistSongList = new List <PlaylistSong>();
            PlaylistSong        newPLS           = null;
            int songOrder = 1;

            resultList = Combinations(workingList, durationSeconds);

            //A PlaylistSong is not the same as a Song; a PlaylistSong is just a pair of foreign keys (SongID, PlaylistID)
            //plus an ordinal. In order to complete the Playlist, the PlaylistSongs need to be created from the ListSong<>.
            foreach (Song s in resultList)
            {
                newPLS            = new PlaylistSong();
                newPLS.PlaylistID = playlist.PlaylistID;
                newPLS.SongID     = s.SongID;
                newPLS.SongOrder  = songOrder;
                CDCatalogManager.AddPlaylistSong(newPLS);
                playlistSongList.Add(newPLS);
                songOrder++;
            }
            return(playlistSongList);
        }
        public static void FilterArtistsByAlbum(Album album)
        {
            //This method is used by the AddSong subwindow to assist the user in selecting Albums and Artists.
            int artistID = album.ArtistID;

            subWindowArtistList.Clear();
            subWindowArtistList = CDCatalogManager.GetArtists().Where(art => art.ArtistID.Equals(artistID)).ToList();
            subWindowAlbumList.Clear();
            subWindowAlbumList = CDCatalogManager.GetAlbums().Where(alb => alb.ArtistID.Equals(artistID)).ToList();
        }
        public static List <PlaylistSong> GeneratePlayList(string playListName, int minutes)
        {
            List <PlaylistSong> playlistSongList = new List <PlaylistSong>();
            List <Song>         workingList      = CDCatalogManager.GetSongs(); //TODO: Filter songs based on Genre and/or Rating.
            int seconds = (minutes * 60);                                       //Song lengths are stored in seconds, but the user selects a playlist duration in minutes.

            Playlist playlist = new Playlist();

            playlist.PlaylistName = playListName; //TODO: Consider checking for and preventing duplicate names.
            CDCatalogManager.AddPlaylist(playlist);

            playlistSongList = CombinationsPlaylist(workingList, playlist, seconds); //Works speedily now.
            //playlistSongList = RandomPlayList(workingList, seconds, playlist);  //My prior algorithm, which did not guarantee a return.

            return(playlistSongList);
        }
        public static void AddArtistGo(string artistName)
        {
            //Add a new Artist to the model.
            int doesArtistExist = CDCatalogManager.GetArtists().Where(a => a.ArtistName.Equals(artistName)).Count();

            if (doesArtistExist == 0)
            {
                Artist artist = new Artist();
                artist.ArtistName = artistName;
                CDCatalogManager.AddArtist(artist);
            }
            else
            {
                //TODO: Message to user? Or not, because the artist they tried to add is in the database,
                //which is what they wanted.
            }
        }
        public static bool addAlbumGo(string albumTitle, Artist artist, int albumRating, string year, List <TrackInfo> tracks, out string message)
        {
            //Verify Album input fields, then add the Album and all associated tracks to the model.
            bool isValid = true;

            message = "";
            int yr             = 0;
            int doesAlbumExist = CDCatalogManager.GetAlbums().Where(a => a.AlbumTitle.Equals(albumTitle)).Where(a => a.ArtistID.Equals(artist.ArtistID)).Count();

            if (!(doesAlbumExist == 0))
            {
                isValid  = false;
                message += "An album by this artist with this title already exists in the database.\n";
            }
            if (!int.TryParse(year, out yr))
            {
                isValid  = false;
                message += "Year must be an integer.\n";
            }

            if (isValid)
            {
                Song  song  = new Song();
                Album album = new Album();
                album.AlbumTitle = albumTitle;
                album.ArtistID   = artist.ArtistID;
                album.Rating     = albumRating;
                album.Year       = yr;
                CDCatalogManager.AddAlbum(album);

                foreach (TrackInfo t in tracks)
                {
                    song             = new Song();
                    song.AlbumID     = album.AlbumID;
                    song.ArtistID    = t.artist.ArtistID;
                    song.GenreID     = t.genreID;
                    song.Rating      = t.rating;
                    song.SongTitle   = t.title;
                    song.TrackLength = t.tracklength;
                    song.TrackNumber = t.tracknum;
                    CDCatalogManager.AddSong(song);
                }
            }

            return(isValid);
        }
        public static void AddGenreGo(string genreName)
        {
            //Add a new Genre to the model.
            int doesGenreExist = CDCatalogManager.GetGenres().Where(g => g.GenreName.Equals(genreName)).Count();

            if (doesGenreExist == 0)
            {
                Genre genre = new Genre();
                genre.GenreName = genreName;
                CDCatalogManager.AddGenre(genre);
            }
            else
            {
                //TODO: Message to user? Or not, because the genre they tried to add is in the database,
                //which is what they wanted.
            }
        }
        public static int CreatePlaylistGo(string playListName, int minutes)
        {
            //Generate a random Playlist of the length specified.
            subWindowPlaylistSongs.Clear();
            Song songObject    = new Song();
            int  totalDuration = 0;

            if (minutes > 0)
            {
                List <PlaylistSong> playlistSongList = GeneratePlayList(playListName, minutes).OrderBy(pls => pls.SongOrder).ToList();
                for (int i = 0; i < playlistSongList.Count; i++)
                {
                    PlaylistSong pls = playlistSongList[i];
                    songObject = CDCatalogManager.GetSongs().Where(s => s.SongID.Equals(pls.SongID)).First();
                    subWindowPlaylistSongs.Add(songObject);
                    totalDuration += songObject.TrackLength;
                }
            }
            return(totalDuration);
        }
 public static void RateAlbumGo(Album album, int rating)
 {
     //Change an Album's Rating. Validation isn't necessary because the ComboBox controls what value the user can submit.
     album.Rating = rating;
     CDCatalogManager.UpdateAlbum(album);
 }
 public static void RateSongGo(Song song, int rating)
 {
     //Change a Song's Rating. Validation isn't necessary because the ComboBox controls what value the user can submit.
     song.Rating = rating;
     CDCatalogManager.UpdateSong(song);
 }
예제 #10
0
 public static void GetAllArtists()
 {
     filterArtistList = CDCatalogManager.GetArtists();
 }
예제 #11
0
 public static void GetAllSongs()
 {
     displaySongList = CDCatalogManager.GetSongs();
 }
예제 #12
0
 public static void AddSongsFillGenres()
 {
     //Populate the Genre list for subwindows.
     subWindowGenreList.Clear();
     subWindowGenreList = CDCatalogManager.GetGenres();
 }
예제 #13
0
 public static void AddSongsFillAlbums()
 {
     //Populate the Song list for subwindows.
     subWindowAlbumList.Clear();
     subWindowAlbumList = CDCatalogManager.GetAlbums();
 }
예제 #14
0
 public static void AddSongsFillArtists()
 {
     //Populate the Artist list for subwindows.
     subWindowArtistList.Clear();
     subWindowArtistList = CDCatalogManager.GetArtists();
 }
예제 #15
0
 public static void GetAllGenres()
 {
     filterGenreList = CDCatalogManager.GetGenres();
 }
예제 #16
0
        public static bool AddSongGo(string title, Artist artist, Album album, string trackIn, Genre genre,
                                     string minutes, string seconds, int rating, out string message)
        {
            //Validate the input fields from the AddSong window, then add the Song to the model.
            bool isValid = true;

            message = "";
            int tracknum      = 0;
            int trackminutes  = 0;
            int trackseconds  = 0;
            int doesSongExist = CDCatalogManager.GetSongs().Where(s => s.SongTitle.Equals(title)).Where(
                s => s.AlbumID.Equals(album.AlbumID)).Where(
                s => s.ArtistID.Equals(artist.ArtistID)).Count();

            if (doesSongExist != 0)
            {
                isValid  = false;
                message += "A Song by this Artist on this Album already exists in the database.";
            }
            if (title == "")
            {
                isValid  = false;
                message += "Song Title cannot be blank.\n";
            }
            if (null == artist)
            {
                isValid  = false;
                message += "Please select an Artist.\n";
            }
            if (null == album)
            {
                isValid  = false;
                message += "Please select an Album.\n";
            }
            if (!int.TryParse(trackIn, out tracknum) || tracknum < 0)
            {
                isValid  = false;
                message += "Track # must be a nonnegative integer.\n";
            }
            if (null == genre)
            {
                isValid  = false;
                message += "Please select a Genre.\n";
            }
            if (minutes != "")
            {
                if (!int.TryParse(minutes, out trackminutes) || trackminutes < 0)
                {
                    isValid  = false;
                    message += "Track minutes must be a nonnegative integer or blank.\n";
                }
            }
            if (!int.TryParse(seconds, out trackseconds) || trackseconds < 1)
            {
                isValid  = false;
                message += "Track seconds must be a positive integer.\n";
            }

            if (isValid)
            {
                Song song = new Song();
                song.SongTitle   = title;
                song.ArtistID    = artist.ArtistID;
                song.AlbumID     = album.AlbumID;
                song.TrackNumber = tracknum;
                song.GenreID     = genre.GenreID;
                song.TrackLength = ((trackminutes * 60) + trackseconds);
                song.Rating      = rating;
                CDCatalogManager.AddSong(song);
            }


            return(isValid);
        }
예제 #17
0
 public static void GetAllPlaylists()
 {
     filterPlaylistList = CDCatalogManager.GetPlaylists();
 }
예제 #18
0
 //The GetAll* methods are just local wrappers for the DB layer, to save the developer
 //from having to type "CDCatalogManager." over and over again.
 public static void GetAllAlbums()
 {
     filterAlbumList = CDCatalogManager.GetAlbums();
 }
예제 #19
0
        //No longer using this code.
        public static List <PlaylistSong> RandomPlayList(List <Song> sourceList, int targetSeconds, Playlist playList)
        {
            ///Algorithm needs to:
            ///1) Check playlist length against target--while totalLength less than or equal to targetSeconds + 60?
            ///
            ///2) Reduce available songs to those that will fit in the time.
            ///3) Add a song, add song length to totalLength
            ///
            Random rand = new Random();
            List <PlaylistSong> playlistSongList = new List <PlaylistSong>();
            PlaylistSong        newPLS           = new PlaylistSong();

            bool keepGoing      = true;
            bool isFull         = false;
            bool notEnoughSongs = false;
            int  iterator       = 0;
            int  index          = 0;
            int  songOrder      = 1;
            int  totalLength    = 0;

            List <Song> songList     = sourceList.OrderBy(s => s.SongID).ToList();
            List <Song> filteredList = songList.Where(s => s.TrackLength <= ((targetSeconds + 60) - totalLength)).ToList();
            List <Song> workingList  = new List <Song>();

            //checking the iterator keeps this from becoming an infinite loop,
            //but all testing, even with fairly small datasets, has so far found
            //a solution before the iterator iterates out.
            while (iterator < 1000000000 && keepGoing == true)
            {
                while (totalLength <= (targetSeconds + 60) && isFull == false && notEnoughSongs == false)
                {
                    //reduce the available songs to only those which will not exceed the specified playlist length plus one minute.
                    filteredList = songList.Where(s => s.TrackLength <= ((targetSeconds + 60) - totalLength)).ToList();

                    if (filteredList.Count > 0) //If we're out of songs, we can't add any more to the playlist
                    {
                        index = rand.Next(0, filteredList.Count);
                        workingList.Add(songList[index]);
                        totalLength += songList[index].TrackLength;
                        songList.RemoveAt(index);
                    }
                    else
                    {
                        notEnoughSongs = true;
                        keepGoing      = false;
                    }

                    if (totalLength >= (targetSeconds - 60))
                    {
                        isFull = true;
                    }
                }

                if (totalLength >= (targetSeconds - 60) && totalLength <= (targetSeconds + 60))
                {
                    songOrder = 1;
                    Song s = new Song();

                    while (workingList.Count > 0)
                    {
                        newPLS            = new PlaylistSong();
                        s                 = workingList[rand.Next(0, workingList.Count)];
                        newPLS.PlaylistID = playList.PlaylistID;
                        newPLS.SongID     = s.SongID;
                        newPLS.SongOrder  = songOrder;
                        CDCatalogManager.AddPlaylistSong(newPLS);
                        playlistSongList.Add(newPLS);
                        workingList.Remove(s);
                        songOrder++;
                    }

                    keepGoing = false;
                }
                else
                {
                    workingList.Clear();
                    totalLength  = 0;
                    isFull       = false;
                    songList     = sourceList.OrderBy(s => s.SongID).ToList();
                    filteredList = songList.Where(s => s.TrackLength <= ((targetSeconds + 60) - totalLength)).ToList();
                    iterator++;
                }
            }

            return(playlistSongList);
        }
예제 #20
0
 public static void FilterSongsByPlaylist(Playlist playlist)
 {
     //Find all the Songs in addAlbumAddTrack particular Playlist.
     filteredSongList.Clear();
     filteredSongList = CDCatalogManager.GetSongsFromPlaylist(playlist);
 }