Beispiel #1
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();
            }
        }
Beispiel #2
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();
            }
        }
Beispiel #3
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();
     }
 }
Beispiel #4
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();
     }
 }
Beispiel #5
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();
         }
     }
 }
Beispiel #6
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();
     }                
 }
Beispiel #7
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;
     }
 }
Beispiel #8
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;
        }
Beispiel #9
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;
        }