Exemplo n.º 1
0
        /// <summary>
        /// This method checks if a file is inserted in the database. The check isn't case-sensitive.
        /// </summary>
        /// <param name="path">
        /// A searchcriteria. It's the filepath of the MP3-File
        /// </param>
        /// <param name="filename">
        /// A searchcriteria. It's the filename of the file
        /// </param>
        /// <returns>
        /// True if the file is in the database, false else
        /// </returns>
        public bool ExistFileInDatabase(String path, String filename)
        {
            log.DebugFormat("ExistFileInDatabase: path={0}, filename={1}", path, filename);
            if (path == null)
                throw new ArgumentNullException("path");
            if (filename == null)
                throw new ArgumentNullException("filename");

            using (var context = new MP3ManagerContext())
            {
                var titles = context.Titles;
                var check = from e in titles
                            where e.Path.ToUpper() == @path.ToUpper()
                            && e.Filename.ToUpper() == @filename.ToUpper()
                            select e.Id;
                int count = check.Count();
                log.DebugFormat("count={0}", count);
                return count > 0;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Diese Methode löscht einen Titel aus der Datenbank.Sollte kein weiterer Titel mit dem Album verknüpft sein,
        /// so wird auch das Album gelöscht.Sollte kein weiterer Titel mit den Interpret verknüpft sein, so wird auch der Interpret gelöscht. 
        /// Sollte ein gültiger Dateiname mi dem Titel verknüpft sein, so erfolgt die Löschung erst nach der Bestätigung duch den Benutzer.
        /// </summary>
        /// <param name="title"></param>
        public void DeleteTitle(Title title)
        {
            string filename = title.Path + title.Filename;
            if (File.Exists(filename))
            {
                DialogResult result = MessageBox.Show("Es ist eine gültige Datei mit diesem Eintrag verknüpft. Sind Sie sicher, dass sie diesen Eintrag und die Datei löschen wollen?", "Info", MessageBoxButtons.YesNo);
                if (result == DialogResult.No)
                {
                    return;
                }
                //Lösche die Datei
                File.Delete(filename);
            }

            //Lösche den Eintrag in der Datenbank
            long titleId = title.Id;
            long albumId = title.AlbumId;
            long interpretId = title.InterpretId;
            using (var context = new MP3ManagerContext())
            {
                //Ist kein weitere Titel mehr zum Album zugeordnet, so lösche das Album
                int countAlbum = context.Titles.Where(e => e.AlbumId == title.AlbumId)
                                               .Where(e => e.Id != title.Id)
                                               .Count();
                if (countAlbum == 0)
                {
                    var reloadedAlbum = context.Albums
                                               .Where(e => e.Id == title.AlbumId)
                                               .FirstOrDefault();
                    if (reloadedAlbum != null)
                    {
                        context.Albums.Remove(reloadedAlbum);
                    }
                }

                //Ist kein weiterer Titel diesem Interpret zugeordnet, dann lösche den Interpreten
                int countInterpret = context.Titles.Where(e => e.InterpretId == title.InterpretId)
                                                   .Where(e => e.Id != title.Id)
                                                   .Count();
                if (countInterpret == 0)
                {
                    var reloadedInterpret = context.Interprets
                                                   .Where(e => e.Id == title.InterpretId)
                                                   .FirstOrDefault();
                    if (reloadedInterpret != null)
                    {
                        context.Interprets.Remove(reloadedInterpret);
                    }
                }

                var reloadedTitle = context.Titles
                                           .Where(e => e.Id == title.Id)
                                           .FirstOrDefault();

                if (reloadedTitle != null)
                {
                    context.Titles.Remove(reloadedTitle);
                }
                context.SaveChanges();
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Diese Methode löscht die angegebene Row aus dem Dataset und aus der Datenbanktabelle
 /// </summary>
 /// <param name="album"></param>
 private void DeleteAlbum(Album album)
 {
     using (var context = new MP3ManagerContext())
     {
         context.Albums.Remove(album);
         context.SaveChanges();
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Diese Methode löscht die Zeile des Interpreten aus dem Dataset und der Datenbank
 /// </summary>
 /// <param name="interpret">
 /// Die Zeile die gelöscht werden soll
 /// </param>
 private void DeleteInterpret(Interpret interpret)
 {
     using (var context = new MP3ManagerContext())
     {
         context.Interprets.Remove(interpret);
         context.SaveChanges();
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// This method searches for the first occurance  of an artist
        /// </summary>
        /// <param name="artist">
        /// Searchcriteria of the artistname
        /// </param>
        /// <returns>
        /// Returns the first occurance of the artist, null else
        /// </returns>
        public Interpret FindArtist(string artist)
        {
            log.DebugFormat("FindArtist:  artist={0}", artist);
            if (artist == null)
            {
                throw new ArgumentException("Artist == null");
            }

            using (var context = new MP3ManagerContext())
            {
                var interprets = context.Interprets;
                var retVal = from e in interprets
                             where e.Searchname.ToUpper().Trim() == @artist.ToUpper().Trim()
                             select e;
                if (retVal.Count() == 0)
                {
                    log.Debug("FindArtist: Interpret nicht gefunden");
                    return null;
                }
                return retVal.First();
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Liefert alle Informationen zu fehlenden 
 /// </summary>
 /// <returns></returns>
 public List<MusicBrainzInformation> GetMusicBrainzList()
 {
     using (var context = new MP3ManagerContext())
     {
         var result = context.MusicBrainzInformation
                             .Include("Album")
                             .Include("Artist")
                             .ToList();
         return result;
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Diese Methode liefert alle Titel, welche in einem Album vorhanden sind, welches noch nicht aktualisiert ist
 /// </summary>
 /// <returns></returns>
 internal List<WArtistAlbum> ReadTitlesToDownloadInformation()
 {
     using (var context = new MP3ManagerContext())
     {
         var result = context.Titles
                                         .Include("Album")
                                         .Include("Interpret")
                                         .Where(e => e.Album.InformationStatus == InformationDownloadStatus.NotStarted)
                                         .Where(e => e.Album != null && e.Album.Name != "" && e.Album.Name != WMP3FileInfo.UNKNOWN_ALBUM)
                                         .Where(e => e.Interpret != null && e.Interpret.Name != "" && e.Interpret.Name != WMP3FileInfo.UNKNOWN_INTERPRET)
                                         .OrderBy(e => e.InterpretId)
                                         .ThenBy(e => e.AlbumId)
                                         .Select(e => new WArtistAlbum()
                                         {
                                             AlbumId = e.AlbumId,
                                             AlbumName = e.Album.Name,
                                             ArtistId = e.InterpretId,
                                             ArtistName = e.Interpret.Name
                                         })
                                         .Distinct()
                                         .ToList();
         return result;
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Liefert die höchste TitleId
 /// </summary>
 /// <returns></returns>
 public int MaxTitleId()
 {
     using (var context = new MP3ManagerContext())
     {
         long maxValue = context.Titles.Max(t => t.Id);
         return maxValue <= int.MaxValue ? (int)maxValue : int.MaxValue;
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// This method searches for tablerowsof the given paameters
        /// </summary>
        /// <param name="interpret">
        /// Searchparameter for interpret
        /// </param>
        /// <param name="album">
        /// Searchparameer for album
        /// </param>
        /// <param name="title">
        /// Searchparameter for title
        /// </param>
        /// <returns></returns>
        public IEnumerable<WSearchItem> Search(string interpret, string album, string title)
        {
            if (String.IsNullOrWhiteSpace(interpret) && String.IsNullOrWhiteSpace(album) && String.IsNullOrWhiteSpace(title))
            {
                List<WSearchItem> noSearchPara = new List<WSearchItem>();
                return noSearchPara;
            }
            string interpretSearchString = CreateSearchname(interpret);
            string albumSearchString = CreateSearchname(album);
            string titleSearchString = CreateSearchname(title);

            using (var context = new MP3ManagerContext())
            {

                var result = from titleRow in context.Titles
                             join albumRow in context.Albums on titleRow.AlbumId equals albumRow.Id
                             join artistRow in context.Interprets on titleRow.InterpretId equals artistRow.Id
                             where !String.IsNullOrWhiteSpace(titleSearchString) && titleRow.Searchname.ToUpper().Trim().Contains(titleSearchString)
                                || !String.IsNullOrWhiteSpace(albumSearchString) && albumRow.Searchname.ToUpper().Trim().Contains(albumSearchString)
                                || !String.IsNullOrWhiteSpace(interpretSearchString) && artistRow.Searchname.ToUpper().Trim().Contains(interpretSearchString)
                             orderby artistRow.Name, albumRow.Name, titleRow.Name
                             select new WSearchItem { Interpret = artistRow, Album = albumRow, Title = titleRow };

                return result.AsParallel().ToList();
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// This method searches in title.name, interpret.name and album.name for the searchstring
 /// </summary>
 /// <param name="searchString">
 /// SearchCriteria
 /// </param>
 /// <returns>
 /// Matched result
 /// </returns>
 public IEnumerable<WSearchItem> Search(string searchString)
 {
     log.DebugFormat("Search in Database: searchString={0}", searchString);
     string normalizeSearchString = CreateSearchname(searchString);
     using (var context = new MP3ManagerContext())
     {
         var result = context.Titles
                             .Include("Interpret")
                             .Include("Album")
                             .Where(e => e.Searchname.ToUpper().Trim().Contains(normalizeSearchString) 
                                     || e.Album.Searchname.ToUpper().Trim().Contains(normalizeSearchString) 
                                     || e.Interpret.Searchname.ToUpper().Trim().Contains(normalizeSearchString))
                             .OrderBy(e => e.Interpret.Name)
                             .ThenBy(e => e.Album.Name)
                             .ThenBy(e => e.Name)
                             .Select(e => new WSearchItem { Interpret = e.Interpret, Album = e.Album, Title = e });
         return result.AsParallel().ToList();
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// This method inserts a new tile row
 /// </summary>
 /// <param name="mp3Info">
 /// The data to insert
 /// </param>
 /// <param name="interpretRow">
 /// The referenced interpret row
 /// </param>
 /// <param name="albumRow">
 /// The referenced album row
 /// </param>
 /// <returns>
 /// The inserted title row if the insertion was successfull, null else
 /// </returns>
 private Title InsertTitle(WMP3FileInfo mp3Info, Interpret interpretRow, Album albumRow)
 {
     if (mp3Info == null)
     {
         throw new ArgumentException("mp3Info == null");
     }
     if (interpretRow == null)
     {
         throw new ArgumentException("interpretRow == null");
     }
     if (albumRow == null)
     {
         throw new ArgumentException("albumRow == null");
     }
     if (String.IsNullOrWhiteSpace(mp3Info.Title))
     {
         throw new ArgumentException("Title null or empty");
     }
     log.DebugFormat("InsertTitle: tile={0}", mp3Info.Title);
     Title titleRow = null;
     long start = DateTime.Now.Ticks;
     using (var context = new MP3ManagerContext())
     {
         string normalizedName = CreateSearchname(mp3Info.Title);
         titleRow = context.Titles.Add(new Title()
          {
              Interpret = interpretRow,
              Album = albumRow,
              Name = mp3Info.Title,
              Length = mp3Info.Songlength,
              Bitrate = mp3Info.BitRate,
              Bytes = mp3Info.Bytes,
              Path = mp3Info.Path,
              Filename = mp3Info.Filename,
              Searchname = normalizedName,
              Track = mp3Info.Track,
              CreationDate = DateTime.Now,
              EditDate = mp3Info.EditDate,
              IsOrdered = false,
              IsCollection = false,
              DurationInSeconds = mp3Info.DurationInSeconds,
              Genre = mp3Info.Genre,
              PublicationYear = mp3Info.PublicationYear,
              SampleRate = mp3Info.SampleRate,
              Channels = mp3Info.Channels
          });
         context.SaveChanges();
         return titleRow;
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// This method inserts a new row in the album table
        /// </summary>
        /// <param name="mp3Info">
        /// The datato insert
        /// </param>
        /// <param name="interpretRow">
        /// The referenced interpretRow
        /// </param>
        /// <returns>
        /// The new albumrrow if the innsertion was successfull, null else
        /// </returns>
        private Album InsertAlbum(WMP3FileInfo mp3Info)
        {
            if (mp3Info == null)
            {
                throw new ArgumentException("mp3Info == null");
            }
            if (String.IsNullOrWhiteSpace(mp3Info.Album))
            {
                mp3Info.Album = WMP3FileInfo.UNKNOWN_ALBUM;
            }

            log.DebugFormat("InsertAlbum: albumname={0}", mp3Info.Album);
            string normalizedName = CreateSearchname(mp3Info.Album);
            var albumRow = FindAlbum(normalizedName);
            if (albumRow == null)
            {
                log.Debug("Insert new Album");
                using (var context = new MP3ManagerContext())
                {
                        albumRow = context.Albums.Add(new Album()
                        {
                            Name = mp3Info.Album, 
                            Searchname = normalizedName, 
                            InformationStatus = InformationDownloadStatus.NotStarted
                        });
                        context.SaveChanges();
                 }
            }
            return albumRow;
        }
Exemplo n.º 13
0
        /// <summary>
        /// This method inserts a new interpret-row
        /// </summary>
        /// <param name="mp3Info">
        /// The data of  the interpeet-row
        /// </param>
        /// <returns>
        /// The new Row if the insertion was successfull, null else
        /// </returns>
        private Interpret insertInterpret(WMP3FileInfo mp3Info)
        {
            if (mp3Info == null)
            {
                throw new ArgumentException("mp3Info == null");
            }
            if (String.IsNullOrWhiteSpace(mp3Info.Interpret))
            {
                mp3Info.Interpret = WMP3FileInfo.UNKNOWN_INTERPRET;
            }

            log.DebugFormat("InsertInterpret: interpret={0}", mp3Info.Interpret);
            string normalizedName = CreateSearchname(mp3Info.Interpret);
            var interpretRow = FindArtist(normalizedName);
            if (interpretRow == null)
            {
                log.Debug("Insert new Interpret");
                using (var context= new MP3ManagerContext())
                {
                    interpretRow = context.Interprets.Add(new Interpret()
                        {
                            Name=mp3Info.Interpret,
                            Searchname = normalizedName
                        });
                    context.SaveChanges();
                }
            }
            return interpretRow;
        }
Exemplo n.º 14
0
        /// <summary>
        /// This method searches for the album of  an artist
        /// </summary>
        /// <param name="albumname">
        /// Searchcriterianame of the album
        /// </param>
        /// <param name="artist">
        /// Searchcriteria Artist of the album
        /// </param>
        /// <returns>
        /// Returns the first occurance of the albu, null else
        /// </returns>
        public Album FindAlbum(string albumname)
        {

            using (var context = new MP3ManagerContext())
            {
                var albums = context.Albums;
                var retVal = from album in albums
                             where album.Searchname.ToUpper().Trim() == @albumname.ToUpper().Trim()
                             select album;
                if (retVal.Count() == 0)
                {
                    log.DebugFormat("FindAlbum: albumname={0}", albumname);
                    return null;
                }
                return retVal.First();
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Diese Methode filtert die Elemente aus, die in die Datenbank nicht vorhanden sind
        /// </summary>
        /// <param name="fileInfos">
        /// Eine Liste von Dateien, die  überprüft werden müssen
        /// </param>
        /// <returns></returns>
        public IEnumerable<WFileInfo> ExtractDatabaseFromList(IEnumerable<WFileInfo> fileInfos)
        {
            List<WFileInfo> returnValues = new List<WFileInfo>();
            if (fileInfos == null)
            {
                log.Warn("ExtractDatabaseFromList: fileInfos == null");
                return returnValues;
            }

            using (var context = new MP3ManagerContext())
            {
                var titles = context.Titles;
                var pathFilename = from title in titles
                                   select new { Filename = title.Filename, Path = title.Path };
                var pathFilenameList = pathFilename.ToList();
                foreach (var fi in fileInfos)
                {
                    log.DebugFormat("ExtractDatabaseFromList: Überprüfe Datei path={0}, filename={1}", fi.Path, fi.Filename);
                    bool doInsert = true;
                    foreach (var listElement in pathFilenameList)
                    {
                        if (listElement.Filename == fi.Filename && listElement.Path == fi.Path)
                        {
                            doInsert = false;
                            break;
                        }
                    }
                    if (doInsert)
                    {
                        log.DebugFormat("ExtractDatabaseFromList: Füge Datei in Datenbank hinzu path={0}, filename={1}", fi.Path, fi.Filename);
                        WFileInfo newInfo = new WFileInfo { Filename = fi.Filename, Path = fi.Path, Bytes = fi.Bytes, EditDate = fi.EditDate };
                        returnValues.Add(newInfo);
                    }
                }
            }
            return returnValues;
        }
Exemplo n.º 16
0
 /// <summary>
 /// Liefert den Titel mit der angegebenen Id
 /// </summary>
 /// <param name="id">
 /// Eindeutiger Titelschlüssel
 /// </param>
 /// <returns>
 /// Liefert den Titel mit der angegebenen Id
 /// </returns>
 public Title GetTitle(long id)
 {
     using (var context = new MP3ManagerContext())
     {
         var title = (from t in context.Titles
                      where t.Id == id
                      select t).FirstOrDefault();
         return title;
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// Liefert eine Liste der gespeicherten Titel
        /// </summary>
        /// <param name="reorderStatus"></param>
        /// <returns></returns>
        public IEnumerable<Title> FetchTitles(Boolean reorderStatus)
        {
            using (var context = new MP3ManagerContext())
            {

                var retValue = from title in context.Titles
                               where title.IsOrdered == reorderStatus
                               select title;
                return retValue.ToList();
            }
        }
Exemplo n.º 18
0
 /// <summary>
 /// Liefert die niedrigste TitleId
 /// </summary>
 /// <returns></returns>
 public int MinTitleId()
 {
     using (var context = new MP3ManagerContext())
     {
         long minValue = context.Titles.Min(t => t.Id);
         return minValue >= int.MinValue && minValue <= int.MaxValue ? (int)minValue : int.MinValue;
     }
 }
Exemplo n.º 19
0
 /// <summary>
 /// Diese Methode aktualisiert die übergebene Zeile in der Datenbank
 /// </summary>
 /// <param name="elementToUpdate"></param>
 public void Update(Album elementToUpdate)
 {
     if (elementToUpdate == null)
     {
         throw new ArgumentException("elementToUpdate == null");
     }
     log.DebugFormat("updateAlbum: id={0}", elementToUpdate.Id);
     using (var context = new MP3ManagerContext())
     {
         context.Albums.Attach(elementToUpdate);
         context.SaveChanges();
     }                
 }
Exemplo n.º 20
0
        /// <summary>
        /// Speichert die Entität der MusicBrainz-Informationen
        /// </summary>
        /// <param name="albumId">
        /// Der Primärschlüssel des Albums
        /// </param>
        /// <param name="entities">
        /// Die Entitäten die in die Datenbank eingetragen werden sollen.
        /// </param>
        internal void AddMusicBrainzEntities(long albumId, List<MusicBrainzInformation> entities)
        {
            using (var context = new MP3ManagerContext())
            {
                foreach (var entity in entities)
                {
                    if (!context.MusicBrainzInformation.Any(e => e.TitleMBId == entity.TitleMBId))
                    {
                        context.MusicBrainzInformation.Add(entity);
                    }
                }

                var album = context.Albums.Where(e => e.Id == albumId).SingleOrDefault();
                if (!context.Albums.Any(e => e.Id == albumId))
                {
                    string message = string.Format("Das Album mit der Id {0} konnte nicht gefunden werden.", albumId);
                    log.Error(message);
                    throw new EntityException(message);
                }

                album.InformationStatus = InformationDownloadStatus.Done;
                context.Entry(album).State = System.Data.Entity.EntityState.Modified;

                context.SaveChanges();
            }
        }
Exemplo n.º 21
0
 /// <summary>
 /// This method updates the albums which are collections of different interprets
 /// </summary>
 public void SetCollections()
 {
     using (var context = new MP3ManagerContext())
     {
         var titles = (from t1 in context.Titles
                       from t2 in context.Titles
                       where t1.AlbumId == t2.AlbumId && t1.InterpretId != t2.InterpretId && t1.IsCollection == false
                       select t1).Distinct();
         foreach (var title in titles)
         {
             if (log.IsDebugEnabled)
             {
                 log.DebugFormat("Setze isCollection = true (ID =  {0})", title.Id);
             }
             title.IsCollection = true;
             context.SaveChanges();
         }
     }
 }
Exemplo n.º 22
0
 /// <summary>
 /// Liefert eine Liste mit Titelsn zum angegebenen Album
 /// </summary>
 /// <param name="albumMBId"></param>
 /// <returns></returns>
 public List<MusicBrainzInformation> GetTitles(string albumMBId)
 {
     using (var context = new MP3ManagerContext())
     {
         var result = context.MusicBrainzInformation
                             .Where(e => e.AlbumMBId == albumMBId)
                             .ToList();
         return result;
     }
 }
Exemplo n.º 23
0
        /// <summary>
        /// This method checks, if an artist is stored in the database
        /// </summary>
        /// <param name="artist">
        /// The searchcriteria of the artist
        /// </param>
        /// <returns>
        /// True if the artist ist stored, false else
        /// </returns>
        public bool ExistArtist(string artist)
        {
            if (String.IsNullOrWhiteSpace(artist))
            {
                log.Warn("ExistArtist: artist == null or empty");
            }
            log.DebugFormat("ExistArtist:artist={0}", artist);

            using (var context = new MP3ManagerContext())
            {
                var interprets = context.Interprets;
                var check = from e in interprets
                            where e.Searchname.ToUpper().Trim() == @artist.ToUpper().Trim()
                            select e.Name;
                int count = check.Count();
                log.DebugFormat("ExistArtist: Count={0}", count);
                return check.Count() > 0;
            }
        }