public void Insert(Album album)
 {
     album.Id = PrepareStoredProcedure(SpInsertAlbum)
                 .WithParametersFromModel(album)
                 .Execute()
                 .AsScalar<int>();
 }
 public void DeleteOneTrackTest()
 {
     string name = MethodBase.GetCurrentMethod().Name;
     Album album = new Album(0, name, 1986);
     modelService.InsertAlbum(album);
     Track track = new Track(0, name, name, album.Id);
     modelService.InsertTrack(track);
     modelService.DeleteTrack(track);
     Assert.AreEqual(null, modelService.ReadAlbumById(album.Id));
     Assert.AreEqual(null, modelService.ReadTrackById(track.Id));
 }
 public void DeleteCollectionOfTracksIdTest()
 {
     string name = MethodBase.GetCurrentMethod().Name;
     Album album = new Album(int.MinValue, name, 1986);
     modelService.InsertAlbum(album);
     Assert.AreNotEqual(int.MinValue, album.Id);
     Track track = new Track(int.MinValue, name, name, album.Id);
     modelService.InsertTrack(track);
     Assert.AreNotEqual(int.MinValue, track.Id);
     modelService.DeleteTrack(track.Id);
     Assert.AreEqual(null, modelService.ReadAlbumById(album.Id));
     Assert.AreEqual(null, modelService.ReadTrackById(track.Id));
 }
        private List<Track> GetRandomListOfTracks(Album album, int count)
        {
            var tracks = new List<Track>();

            for (int i = 0; i < count; i++)
            {
                Track a = new Track(int.MinValue, album.Name, album.Name, album.Id);
                tracks.Add(a);
            }

            return tracks;
        }
        private List<Album> GetRandomAlbums(string name, int count)
        {
            List<Album> albums = new List<Album>();
            for (int i = 0; i < count; i++)
            {
                Album a = new Album(int.MinValue, name + ":" + i, i + 1986);
                albums.Add(a);
            }

            return albums;
        }
 public void ReadTrackByFingerprintTest()
 {
     string name = MethodBase.GetCurrentMethod().Name;
     Album album = new Album(int.MinValue, name, 1986);
     modelService.InsertAlbum(album);
     Track track = new Track(int.MinValue, name, name, album.Id, 360);
     modelService.InsertTrack(track);
     const int FakeId = int.MinValue;
     Fingerprint f = new Fingerprint(FakeId, GenericFingerprint, track.Id, int.MinValue);
     modelService.InsertFingerprint(f);
     Assert.AreNotEqual(FakeId, f.Id);
     var list = modelService.ReadTrackByFingerprint(f.Id);
     Track readT = list.FirstOrDefault(temp => temp.Id == track.Id);
     Assert.AreNotEqual(null, readT);
     Assert.AreEqual(track.Id, readT.Id);
     Assert.AreEqual(track.AlbumId, readT.AlbumId);
     Assert.AreEqual(track.Artist, readT.Artist);
     Assert.AreEqual(track.Title, readT.Title);
     Assert.AreEqual(track.TrackLengthSec, readT.TrackLengthSec);
 }
        public void InsertReadAlbumTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;
            const int FakeId = int.MinValue;
            Album album = new Album { Id = FakeId, Name = name, ReleaseYear = 1986 };
            modelService.InsertAlbum(album);
            Assert.AreNotEqual(FakeId, album.Id);
            var allAlbums = modelService.ReadAlbums(); // read all albums
            bool found = false;
            int id = 0;

            if (allAlbums.Any(a => a.Id == album.Id))
            {
                found = true;
                id = album.Id;
            }

            Assert.IsTrue(found); // check if it was inserted
            Album b = modelService.ReadAlbumById(id);
            Assert.AreEqual(id, b.Id);
            Assert.AreEqual(album.Name, b.Name);
            Assert.AreEqual(album.ReleaseYear, b.ReleaseYear);

            var albums = GetRandomAlbums(name, 10);
            modelService.InsertAlbum(albums);
            foreach (var alb in albums)
            {
                Assert.AreNotEqual(FakeId, alb.Id);
            }

            List<int> albumIds = albums.Select(a => a.Id).ToList();

            var readAlbums = modelService.ReadAlbums(); /*read all albums*/
            var readAlbumIds = readAlbums.Select(a => a.Id).ToList();
            foreach (int albumId in albumIds)
            {
                Assert.AreEqual(true, readAlbumIds.Contains(albumId));
            }
        }
        private Album GetCoresspondingAlbum(TagInfo tags)
        {
            string album = tags.Album;
            Album albumToInsert = null;
            if (string.IsNullOrEmpty(album))
            {
                albumToInsert = unknownAlbum; // The album is unknown
            }
            else
            {
                lock (lockObject)
                {
                    albumToInsert = listOfAllAlbums.FirstOrDefault(a => a.Name == album);
                    if (albumToInsert == null)
                    {
                        // No such album in the database INSERT!
                        int releaseYear = -1;
                        try
                        {
                            releaseYear = Convert.ToInt32(tags.Year.Split('-')[0].Trim());
                        }
                        catch (Exception)
                        {
                            /*swallow*/
                            Debug.WriteLine("Release Year is in a bad format. Continuw processing...");
                        }

                        albumToInsert = new Album(0, album, releaseYear);
                        try
                        {
                            modelService.InsertAlbum(albumToInsert); // Insert new ALBUM
                        }
                        catch (Exception ex)
                        {
                            if (MessageBox.Show(ex.Message + "\n Continue?", Resources.ExceptioInDal, MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
                                == DialogResult.Cancel)
                            {
                                return null;
                            }

                            albumToInsert = unknownAlbum;
                        }

                        if (albumToInsert != unknownAlbum)
                        {
                            listOfAllAlbums.Add(albumToInsert); // Modify Local Variable
                        }
                    }
                }
            }

            return albumToInsert;
        }
        public void ReadFingerprintByMultipleTrackIdTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;
            Album album = new Album(0, name, 1986);
            modelService.InsertAlbum(album);
            Track track0 = new Track(0, name, name, album.Id, 360);
            modelService.InsertTrack(track0);
            Track track1 = new Track(0, name, name, album.Id, 360);
            modelService.InsertTrack(track1);
            Track track2 = new Track(0, name, name, album.Id, 360);
            modelService.InsertTrack(track2);

            Fingerprint f0 = new Fingerprint(0, GenericFingerprint, track0.Id, 0);
            modelService.InsertFingerprint(f0);
            Fingerprint f1 = new Fingerprint(0, GenericFingerprint, track0.Id, 1);
            modelService.InsertFingerprint(f1);
            Fingerprint f2 = new Fingerprint(0, GenericFingerprint, track1.Id, 2);
            modelService.InsertFingerprint(f2);
            Fingerprint f3 = new Fingerprint(0, GenericFingerprint, track1.Id, 3);
            modelService.InsertFingerprint(f3);
            Fingerprint f4 = new Fingerprint(0, GenericFingerprint, track2.Id, 4);
            modelService.InsertFingerprint(f4);
            Fingerprint f5 = new Fingerprint(0, GenericFingerprint, track2.Id, 5);
            modelService.InsertFingerprint(f5);
            Fingerprint f6 = new Fingerprint(0, GenericFingerprint, track0.Id, 6);
            modelService.InsertFingerprint(f6);
            Fingerprint f7 = new Fingerprint(0, GenericFingerprint, track1.Id, 7);
            modelService.InsertFingerprint(f7);
            Fingerprint f8 = new Fingerprint(0, GenericFingerprint, track2.Id, 8);
            modelService.InsertFingerprint(f8);

            var dict =
                modelService.ReadFingerprintsByMultipleTrackId(new List<Track> { track0, track1, track2 }, 0);

            Assert.AreNotEqual(null, dict);
            Assert.AreEqual(3, dict.Keys.Count);
            foreach (var item in dict)
            {
                Assert.AreEqual(3, item.Value.Count);
            }

            Assert.AreEqual(true, dict.ContainsKey(track0.Id));
            Assert.AreEqual(true, dict.ContainsKey(track1.Id));
            Assert.AreEqual(true, dict.ContainsKey(track2.Id));

            foreach (var pair in dict)
            {
                Assert.AreEqual(3, pair.Value.Count);
            }
        }
 public void ReadAlbumByNameTest()
 {
     string albumName = Guid.NewGuid().ToString();
     Album album = new Album(int.MinValue, albumName);
     modelService.InsertAlbum(album);
     Assert.AreNotEqual(int.MinValue, album.Id);
     Album readAlbum = modelService.ReadAlbumByName(albumName);
     Assert.IsNotNull(readAlbum);
     Assert.AreEqual(album.Id, readAlbum.Id);
 }
        public void ReadFingerprintByIdTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;

            const int fakeId = int.MinValue;
            Album album = new Album(fakeId, name, 1986);
            modelService.InsertAlbum(album);
            Assert.AreNotEqual(fakeId, album);
            Track track = new Track(fakeId, name, name, album.Id, 360);
            modelService.InsertTrack(track);
            Assert.AreNotEqual(fakeId, track.Id);
            Fingerprint f = new Fingerprint(fakeId, GenericFingerprint, track.Id, 0);
            modelService.InsertFingerprint(f);
            Assert.AreNotEqual(fakeId, f.Id);
            Fingerprint readF = modelService.ReadFingerprintById(f.Id);
            Assert.AreEqual(f.Id, readF.Id);
            Assert.AreEqual(f.Signature.Length, readF.Signature.Length);
            for (int i = 0; i < f.Signature.Length; i++)
            {
                Assert.AreEqual(f.Signature[i], readF.Signature[i]);
            }

            Assert.AreEqual(f.TrackId, readF.TrackId);
        }
        public void InsertTrackWithBadAlbumIdForeignKeyReference()
        {
            string name = MethodBase.GetCurrentMethod().Name;

            Album a = new Album(int.MinValue, name, 1990);
            Track track = new Track(int.MinValue, name, name, a.Id);
            modelService.InsertTrack(track);
        }
        public void InsertReadTrackTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;
            Album album = new Album(int.MinValue, name, 1986);
            modelService.InsertAlbum(album);
            Assert.AreNotEqual(int.MinValue, album.Id);
            Track track = new Track(int.MinValue, name, name, album.Id, 360);
            modelService.InsertTrack(track);
            Assert.AreNotEqual(int.MinValue, track.Id);
            var listOfTracks = modelService.ReadTracks();
            bool found = false;
            foreach (Track temp in listOfTracks)
            {
                Assert.IsTrue(temp.Id > 0);
                if (temp.Id == track.Id)
                {
                    found = true;
                    break;
                }
            }
            Assert.AreEqual(true, found);
            Track t = modelService.ReadTrackById(track.Id);
            Assert.AreEqual(track.Id, t.Id);
            Assert.AreEqual(track.AlbumId, t.AlbumId);
            Assert.AreEqual(track.Artist, t.Artist);
            Assert.AreEqual(track.Title, t.Title);
            Assert.AreEqual(track.TrackLengthSec, t.TrackLengthSec);
            List<Album> listAlbums = new List<Album>();
            for (int i = 0; i < 10; i++)
            {
                Album a = new Album(int.MinValue, name + i, i + 1986);
                listAlbums.Add(a);
            }
            modelService.InsertAlbum(listAlbums);
            foreach (Album a in listAlbums)
            {
                Assert.AreNotEqual(int.MinValue, a.Id);
            }

            List<Track> listTracks = new List<Track>();
            List<int> lId = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                Track a = new Track(int.MinValue, name + i, name + i, listAlbums[i].Id);
                listTracks.Add(a);
            }

            modelService.InsertTrack(listTracks);
            foreach (Track item in listTracks)
            {
                Assert.AreNotEqual(int.MinValue, item.Id);
            }

            var readTracks = modelService.ReadTracks();
            List<int> readIds = readTracks.Select(a => a.Id).ToList();
            foreach (int i in lId)
            {
                Assert.AreEqual(true, readIds.Contains(i));
            }
        }
        public void InsertReadFingerprintTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;
            Album album = new Album(0, name, 1986);
            modelService.InsertAlbum(album);
            Track track = new Track(0, name, name, album.Id, 360);
            modelService.InsertTrack(track);
            Fingerprint f = new Fingerprint(0, GenericFingerprint, track.Id, 0);
            modelService.InsertFingerprint(f);
            var allFingerprints = modelService.ReadFingerprints();
            List<int> fingerprintIds = allFingerprints.Select(temp => temp.Id).ToList();

            Assert.AreEqual(true, fingerprintIds.Contains(f.Id));

            List<Fingerprint> addList = new List<Fingerprint>();
            for (int i = 0; i < 10; i++)
            {
                addList.Add(new Fingerprint(0, GenericFingerprint, track.Id, 0));
            }

            modelService.InsertFingerprint(addList);
            allFingerprints = modelService.ReadFingerprints();
            fingerprintIds.Clear();
            fingerprintIds.AddRange(allFingerprints.Select(temp => temp.Id));

            foreach (Fingerprint finger in addList)
            {
                Assert.AreEqual(true, fingerprintIds.Contains(finger.Id));
            }
        }
 private Album InsertRandomAlbum(string name)
 {
     Album album = new Album(int.MinValue, name, 1986);
     modelService.InsertAlbum(album);
     return album;
 }
        public void ReadFingerprintByMultipleTrackIdTest1()
        {
            string name = MethodBase.GetCurrentMethod().Name;

            Album album = new Album(0, name, 1986);
            modelService.InsertAlbum(album);
            const int NumberOfTracks = 1153;
            const int NumberOfFingerprintsPerTrack = 10;

            List<Track> listTrack = new List<Track>();
            List<Fingerprint> listOfFingerprints = new List<Fingerprint>();
            for (int i = 0; i < NumberOfTracks; i++)
            {
                Track track0 = new Track(0, name, name, album.Id, 360);
                listTrack.Add(track0);
                modelService.InsertTrack(track0);
                for (int j = 0; j < NumberOfFingerprintsPerTrack; j++)
                {
                    Fingerprint f0 = new Fingerprint(0, GenericFingerprint, track0.Id, 0);
                    listOfFingerprints.Add(f0);
                }
            }

            modelService.InsertFingerprint(listOfFingerprints);

            var dict = modelService.ReadFingerprintsByMultipleTrackId(listTrack, 0);

            Assert.AreNotEqual(null, dict);
            Assert.AreEqual(NumberOfTracks, dict.Keys.Count);
            foreach (Track track in listTrack)
            {
                Assert.AreEqual(true, dict.ContainsKey(track.Id));
                Assert.AreEqual(NumberOfFingerprintsPerTrack, dict[track.Id].Count);
            }
        }
        private void BtnStartClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(_tbRootFolder.Text) || !string.IsNullOrEmpty(_tbSingleFile.Text) && fileList == null)
            {
                fileList = new List<string>();
                if (!string.IsNullOrEmpty(_tbRootFolder.Text))
                {
                    RootFolderIsSelected(this, null);
                }

                if (!string.IsNullOrEmpty(_tbSingleFile.Text))
                {
                    TbSingleFileTextChanged(this, null);
                }
            }

            if (!fileList.Any())
            {
                MessageBox.Show(Resources.FileListEmpty, Resources.FileListEmptyCaption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            FadeAllControls(true); // Fade all controls

            int rest = fileList.Count % MaxThreadToProcessFiles;
            int filesPerThread = fileList.Count / MaxThreadToProcessFiles;

            listOfAllAlbums = modelService.ReadAlbums(); // Get all albums
            unknownAlbum = modelService.ReadUnknownAlbum(); // Read unknown albums

            switch (hashAlgorithm)
            {
                case HashAlgorithm.LSH:
                    hashTables = (int)_nudHashTables.Value; // If LSH is used # of Hash tables
                    hashKeys = (int)_nudHashKeys.Value; // If LSH is used # of keys per table
                    break;
                case HashAlgorithm.NeuralHasher:
                    if (string.IsNullOrEmpty(_tbPathToEnsemble.Text))
                    {
                        // Check if the path to ensemble is specified
                        MessageBox.Show(Resources.SpecifyPathToNetworkEnsemble, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        FadeAllControls(false);
                        return;
                    }

                    try
                    {
                        NeuralNetworkEnsemble.Load(_tbPathToEnsemble.Text); // Load the ensemble
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        FadeAllControls(false);
                        return;
                    }

                    break;
                case HashAlgorithm.None:
                    break;
            }

            BeginInvoke(new Action(() => { }), null);

            ResetControls();
            int runningThreads = MaxThreadToProcessFiles;
            for (int i = 0; i < MaxThreadToProcessFiles; i++)
            {
                // Start asynchronous operation
                int start = i * filesPerThread; // Define start and end indexes
                int end = (i == MaxThreadToProcessFiles - 1) ? i * filesPerThread + filesPerThread + rest : i * filesPerThread + filesPerThread;
                Action<int, int> action = InsertInDatabase;
                action.BeginInvoke(
                    start,
                    end,
                    result =>
                        {
                            // End Asynchronous operation
                            Action<int, int> item = (Action<int, int>)result.AsyncState;
                            item.EndInvoke(result);
                            Interlocked.Decrement(ref runningThreads);
                            if (runningThreads == 0)
                            {
                                /********* END OF INSERTION PROCESS HERE!********/

                                Invoke(
                                    new Action(
                                        () =>
                                            {
                                                _pbTotalSongs.Visible = false;
                                                FadeAllControls(false);
                                                _tbRootFolder.Text = null;
                                                _tbSingleFile.Text = null;
                                            }));
                                MessageBox.Show(Resources.InsertionEnded, Resources.End, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        },
                    action);
            }
        }
        public void ReadFingerprintByTrackIdTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;

            Album album = new Album(0, name, 1986);
            modelService.InsertAlbum(album);
            Track track = new Track(0, name, name, album.Id, 360);
            modelService.InsertTrack(track);
            Fingerprint f = new Fingerprint(0, GenericFingerprint, track.Id, 0);
            modelService.InsertFingerprint(f);

            var list = modelService.ReadFingerprintsByTrackId(track.Id, 0);
            Fingerprint readF = list.FirstOrDefault(temp => temp.Id == f.Id);
            Assert.AreNotEqual(null, readF);
            Assert.AreEqual(f.Id, readF.Id);
            Assert.AreEqual(f.Signature.Length, readF.Signature.Length);
            for (int i = 0; i < f.Signature.Length; i++)
            {
                Assert.AreEqual(f.Signature[i], readF.Signature[i]);
            }

            Assert.AreEqual(f.TrackId, readF.TrackId);
        }
 private Track InsertTrack()
 {
     Album album = new Album(0, "Track");
     modelService.InsertAlbum(album);
     Track track = new Track(0, "Random", "Random", album.Id);
     modelService.InsertTrack(track);
     return track;
 }
 public void ReadTrackByFingerprintInexistantIdTest()
 {
     string name = MethodBase.GetCurrentMethod().Name;
     Album album = new Album(int.MinValue, name, 1986);
     modelService.InsertAlbum(album);
     Track track = new Track(int.MinValue, name, name, album.Id, 360);
     modelService.InsertTrack(track);
     Assert.AreEqual(0,  modelService.ReadTrackByFingerprint(int.MinValue).Count);
 }
 public void InsertAlbum(Album album)
 {
     albumDao.Insert(album);
 }
 public void InsertFingerprintWithBadTrackIdForeignKeyreference()
 {
     string name = MethodBase.GetCurrentMethod().Name;
     Album a = new Album(0, name, 1990);
     Track track = new Track(0, name, name, a.Id);
     Fingerprint f = new Fingerprint(0, GenericFingerprint, track.Id, 0);
     modelService.InsertFingerprint(f);
 }