Example #1
0
 internal static void DBSave <T>(this List <T> entities) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         entities.ForEach(e => client.Collection.Save <T>(e));
     }
 }
        public void UpdateAddLibraryLocation(IDBClient db, VideoLibraryLocation location)
        {
            var db4oClient = db as Db4oClient;

            // add the location to the database
            if (db4oClient != null) db4oClient.Client.Store(location);
        }
        private void UpdateCoverArt()
        {
            _coverUpdateTimer.Stop();
            try
            {
                // get a hook to the DB
                IDBClient db = Database.RetrieveClient();

                //// get the audio library locations
                //IList<Album> albums = _theModule._theDatabase.RetrieveAlbumsWithoutCoverArt(db);

                // get the current timestamp, we'll use this in case anything gets modified while this is happening
                DateTime beforeUpdate = DateTime.Now;

                //_log.Info("Started cover art update at: " + DateTime.Now.ToLocalTime());

                //// recurse through each of the library locations
                //foreach (Album album in albums)
                //{
                //    if (!_theModule._theCore._keepRunning)
                //    {
                //        return;
                //    }

                //    bool artistPresent = (null != album._artist.Name);
                //    bool titlePresent = (null != album._title);
                //    // if this has the imported cover art
                //    if (artistPresent && titlePresent && album._coverArt == AudioModuleConstants.IMPORTED_COVERART)
                //    {
                //        // try to retrieve the cover art from Amazon
                //        var amazon = new AmazonRestService();
                //        string coverArt = amazon.RetrieveImage(album, AudioModuleConstants.IMAGE_FOLDER);

                //        // if successful, store it on the album
                //        if (null != coverArt)
                //        {
                //            // add the cover art with the required prefix
                //            string coverArtPath = AudioModuleConstants.IMAGES_PREFIX + "/" + coverArt;
                //            album._coverArt = coverArtPath;
                //            _theModule._theDatabase.UpdateAddAlbum(db, album);
                //            db.Commit();
                //        }
                //    }
                //}

                TimeSpan elapsedTime = DateTime.Now - beforeUpdate;
                _log.Info("Finished cover art update at: " + DateTime.Now.ToLocalTime() + ".  Took: " +
                          elapsedTime.TotalSeconds + " seconds");

                db.Close();
            }
            catch (Db4oException)
            {
                // TODO
            }
            finally
            {
                _coverUpdateTimer.Start();
            }
        }
Example #4
0
 internal static bool DBExists <T>(Expression <Func <T, bool> > where) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.Count(Query <T> .Where(where)) > 0);
     }
 }
Example #5
0
 internal static void DBDownloadGridFS(Type type, string id, Stream stream)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         client.GridFS.Download(stream, Query.EQ("_id", id));
     }
 }
        public void AddAlbum(IDBClient db, Album toAdd)
        {
            var db4oClient = db as Db4oClient;

            // add the Album to the database
            if (db4oClient != null) db4oClient.Client.Store(toAdd);
        }
Example #7
0
 internal static long DBRemoveAll <T>() where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.RemoveAll().DocumentsAffected);
     }
 }
Example #8
0
 internal static long DBRemoveAll <T>(Expression <Func <T, bool> > where) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.Remove(Query <T> .Where(where)).DocumentsAffected);
     }
 }
Example #9
0
 internal static bool DBRemove <T>(string id) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.Remove(Query <T> .Where(e => e.Id == id)).Ok);
     }
 }
Example #10
0
 internal static IQueryable <T> DBSelect <T>(Expression <Func <T, bool> > where) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.FindAs <T>(Query <T> .Where(where)).AsQueryable());
     }
 }
Example #11
0
 internal static bool DBExists <T>(string id) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.Count(Query <T> .Where(t => t.Id == id)) > 0);
     }
 }
Example #12
0
 internal static long DBCount <T>() where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.Count());
     }
 }
Example #13
0
 internal static void DBSave(Type type, object entity)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         client.Collection.Save(type, entity);
     }
 }
Example #14
0
 internal static void DBRemoveGridFS(Type type, string remoteFileName)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         client.GridFS.Delete(remoteFileName);
     }
 }
Example #15
0
 internal static void DBSave <T>(this T entity) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(entity.GetType()))
     {
         client.Collection.Save <T>(entity);
     }
 }
Example #16
0
 internal static T DBFind <T>(Type type, string id) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         return(client.Collection.FindOneByIdAs <T>(id));
     }
 }
Example #17
0
 internal static List <T> DBFindAll <T>() where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.FindAllAs <T>().ToList());
     }
 }
Example #18
0
 internal static bool DBRemove <T>(this T entity) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(entity.GetType()))
     {
         return(client.Collection.Remove(Query <T> .EQ(e => e.Id, entity.Id)).Ok);
     }
 }
Example #19
0
 internal static void DBRemoveGridFS(Type type, BsonValue id)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         client.GridFS.DeleteById(id);
     }
 }
Example #20
0
 internal static MongoGridFSFileInfo DBLoadGridFS(Type type, BsonValue id)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         return(client.GridFS.FindOneById(id));
     }
 }
Example #21
0
 internal static List <MongoGridFSFileInfo> DBLoadGridFS(Type type, string remoteFileName)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         return(client.GridFS.Find(remoteFileName).ToList <MongoGridFSFileInfo>());
     }
 }
Example #22
0
 internal static T DBFind <T>(Expression <Func <T, bool> > where) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         return(client.Collection.FindOneAs <T>(Query <T> .Where(where)));
     }
 }
Example #23
0
 internal static void DBRemoveGridFS(Type type, string[] ids)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         client.GridFS.Delete(Query <IMongoFile> .Where(f => ids.Contains(f.Id)));
     }
 }
        public void RemoveAlbum(IDBClient db, Album toRemove)
        {
            var db4oClient = db as Db4oClient;

            // remove the Album from the database
            if (db4oClient != null) db4oClient.Client.Delete(toRemove);
        }
Example #25
0
 internal static void DBRemoveAllGridFS(Type type)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         client.GridFS.Files.RemoveAll();
         client.GridFS.Chunks.RemoveAll();
     }
 }
        public void RemoveLibraryLocation(IDBClient db, AudioLibraryLocation toDelete)
        {
            var db4oClient = db as Db4oClient;

            if (db4oClient != null)
            {
                db4oClient.Client.Delete(toDelete);
            }
        }
        public void RemoveAlbum(IDBClient db, Album toRemove)
        {
            var db4oClient = db as Db4oClient;

            // remove the Album from the database
            if (db4oClient != null)
            {
                db4oClient.Client.Delete(toRemove);
            }
        }
        public void UpdateAddTrack(IDBClient db, Track toAdd)
        {
            var db4oClient = db as Db4oClient;

            // add the track to the database
            if (db4oClient != null)
            {
                db4oClient.Client.Store(toAdd);
            }
        }
Example #29
0
 internal static MongoGridFSFileInfo DBSaveGridFS(Type type, IMongoFile file)
 {
     using (IDBClient client = DBFactory.GetClient(type))
     {
         using (Stream stream = File.OpenRead(file.LocalFileName))
         {
             return(client.GridFS.Upload(stream, file.RemoteFileName, BuildMongoGridFSCreateOptions(file)));//.Upload(file.FileName);
         }
     }
 }
        public void UpdateAddArtist(IDBClient db, Artist toAdd)
        {
            var db4oClient = db as Db4oClient;

            // add the artist to the database
            if (db4oClient != null)
            {
                db4oClient.Client.Store(toAdd);
            }
        }
        public void UpdateAddLibraryLocation(IDBClient db, AudioLibraryLocation location)
        {
            var db4oClient = db as Db4oClient;

            // add the location to the database
            if (db4oClient != null)
            {
                db4oClient.Client.Store(location);
            }
        }
        public void AddAlbum(IDBClient db, Album toAdd)
        {
            var db4oClient = db as Db4oClient;

            // add the Album to the database
            if (db4oClient != null)
            {
                db4oClient.Client.Store(toAdd);
            }
        }
Example #33
0
 internal static IQueryable <T> DBSelect <T>(Expression <Func <T, bool> > where, Expression <Func <T, object> > orderby, int pageIndex, int pageSize, out int pageCount, out int allCount) where T : IEntity
 {
     using (IDBClient client = DBFactory.GetClient(typeof(T)))
     {
         var queryable = client.Collection.FindAs <T>(Query <T> .Where(where)).AsQueryable();
         allCount  = queryable.Count();
         pageCount = (allCount / pageSize) + (allCount % pageSize > 0 ? 1 : 0);
         return(queryable.OrderByDescending(orderby).Skip(pageIndex * pageSize).Take(pageSize));
     }
 }
        public IList<VideoLibraryLocation> RetrieveLibraryLocations(IDBClient db)
        {
            var db4oClient = db as Db4oClient;

            if (db4oClient != null)
            {
                IEnumerable<VideoLibraryLocation> result = from VideoLibraryLocation a in db4oClient.Client
                                                           select a;

                return result.ToList();
            }
            return null;
        }
        public IList<Album> RetrieveAlbumsWithoutCoverArt(IDBClient db)
        {
            var db4oClient = db as Db4oClient;

            // search for the albums
            if (db4oClient != null)
            {
                IEnumerable<Album> result = from Album a in db4oClient.Client
                                            where a.CoverArt == Album.IMPORTED_COVERART
                                            select a;

                return result.ToList();
            }

            return null;
        }
        public IList<Album> RetrieveAllAlbums(IDBClient db)
        {
            var db4oClient = db as Db4oClient;

            // query to select all albums
            if (db4oClient != null)
            {
                IEnumerable<Album> result = from Album a in db4oClient.Client
                                            select a;

                IList<Album> resultList = result.ToList();

                return resultList;
            }
            return null;
        }
        public VideoLibraryLocation RetrieveLibraryLocation(IDBClient db, string name, string path)
        {
            var db4oClient = db as Db4oClient;

            if (db4oClient != null)
            {
                IEnumerable<VideoLibraryLocation> result = from VideoLibraryLocation a in db4oClient.Client
                                                           where a.Name == name && a.Path == path
                                                           select a;

                // return the first one if anything returned
                if (result.Count() > 0)
                {
                    return result.ToArray()[0];
                }
            }
            return null;
        }
        public Album RetrieveAlbumByName(IDBClient db, string albumName, Compression compression)
        {
            var db4oClient = db as Db4oClient;

            // search for the album
            if (db4oClient != null)
            {
                IEnumerable<Album> result = from Album a in db4oClient.Client
                                            where a.Title == albumName &&
                                                  a.Compression.Equals(compression)
                                            select a;

                // return the first one if anything returned
                if (result.Count() > 0)
                {
                    return result.ToArray()[0];
                }
            }
            return null;
        }
        public int RetrieveNumberOfAlbums(IDBClient db)
        {
            var db4oClient = db as Db4oClient;

            // query to select all albums
            if (db4oClient != null)
            {
                IEnumerable<Album> result = from Album a in db4oClient.Client
                                            select a;

                return result.Count();
            }

            return 0;
        }
        public void RemoveLibraryLocation(IDBClient db, VideoLibraryLocation toDelete)
        {
            var db4oClient = db as Db4oClient;

            if (db4oClient != null) db4oClient.Client.Delete(toDelete);
        }
        public Track RetrieveTrackByNameAndAlbumName(IDBClient db, string trackName, string albumName,
                                                     Compression compression)
        {
            var db4oClient = db as Db4oClient;

            // search for the track
            if (db4oClient != null)
            {
                IEnumerable<Track> result = from Track t in db4oClient.Client
                                            where t.Title == trackName &&
                                                  t.Album.Title == albumName &&
                                                  t.Album.Compression.Equals(compression)
                                            select t;

                // return the first one if anything returned
                if (result.Count() > 0)
                {
                    return result.ToArray()[0];
                }
            }
            return null;
        }
        public IList<Track> RetrieveTracksByArtist(IDBClient db, Artist theArtist, Compression compression)
        {
            var db4oClient = db as Db4oClient;

            // search for the track
            if (db4oClient != null)
            {
                IEnumerable<Track> result = from Track t in db4oClient.Client
                                            where t.Album.Artist == theArtist &&
                                                  t.Album.Compression.Equals(compression)
                                            select t;

                // return the first one if anything returned
                return result.ToList();
            }

            return null;
        }
        public void UpdateAddArtist(IDBClient db, Artist toAdd)
        {
            var db4oClient = db as Db4oClient;

            // add the artist to the database
            if (db4oClient != null) db4oClient.Client.Store(toAdd);
        }
        public void UpdateAddTrack(IDBClient db, Track toAdd)
        {
            var db4oClient = db as Db4oClient;

            // add the track to the database
            if (db4oClient != null) db4oClient.Client.Store(toAdd);
        }
        public IList<Album> RetrieveAllAlbumsByArtist(IDBClient db, Compression compression)
        {
            var db4oClient = db as Db4oClient;

            // query to select all albums
            if (db4oClient != null)
            {
                IEnumerable<Album> result = from Album a in db4oClient.Client
                                            where a.Compression.Equals(compression)
                                            orderby a.Artist.Name
                                            select a;

                IList<Album> resultList = result.ToList();

                return resultList;
            }
            return null;
        }
        public int RetrieveNumberOfTracks(IDBClient db)
        {
            var db4oClient = db as Db4oClient;

            // query to select all tracks
            if (db4oClient != null)
            {
                IEnumerable<Track> result = from Track t in db4oClient.Client
                                            select t;

                return result.Count();
            }
            return 0;
        }
        private void ProcessTrack(IDBClient db, string file)
        {
            // open the file using taglib
            try
            {
                // work out the compression type from the extension
                var compression = Compression.Lossy;
                if (AudioConstants.SUPPORTED_LOSSLESS.Contains(Path.GetExtension(file)))
                    compression = Compression.Lossless;

                File musicFile = File.Create(file);

                // check whether or not the tag information is valid
                if (null == musicFile.Tag.Album || null == musicFile.Tag.Title)
                {
                    _log.Error("Invalid tag information for: " + file);
                }
                else
                {
                    // retrieve the album artist first
                    if (null == _albumArtist)
                    {
                        // find the artist that should be for this album
                        _albumArtist = Database.RetrieveArtistByName(db, musicFile.Tag.AlbumArtists[0]);

                        // check if we have an existing album artist
                        if (null == _albumArtist)
                        {
                            // if not, create one
                            _albumArtist = new Artist(musicFile.Tag.AlbumArtists[0]);
                        }
                    }

                    // have an album to work with
                    if (null == _theAlbum)
                    {
                        // we'll attempt to find an album to add it to
                        _theAlbum = Database.RetrieveAlbumByName(db, musicFile.Tag.Album, compression);

                        // if there isn't an existing album
                        if (null == _theAlbum)
                        {
                            // create a new album
                            _theAlbum = new Album(musicFile.Tag.Album, _albumArtist, musicFile.Tag.FirstGenre,
                                                  (int) musicFile.Tag.Year, compression);
                        }
                    }
                    else
                    {
                        // make sure we have the right album
                        if (_theAlbum.Title != musicFile.Tag.Album)
                        {
                            // we'll attempt to find an album to add it to or create a new one
                            _theAlbum = Database.RetrieveAlbumByName(db, musicFile.Tag.Album, compression) ??
                                        new Album(musicFile.Tag.Album, _albumArtist, musicFile.Tag.FirstGenre,
                                            (int) musicFile.Tag.Year, compression);

                            // if there isn't an existing album
                        }
                    }

                    // initialise the output track
                    Track theTrack;
                    var trackArtists = new List<Artist>();

                    // special case for tracks that have more than one artist
                    if (musicFile.Tag.Performers.Count() > 1)
                    {
                        foreach (var artist in musicFile.Tag.Performers)
                        {
                            // we'll try with the album artist first
                            var performer = _albumArtist;
                            if (artist != _albumArtist.Name)
                                performer = Database.RetrieveArtistByName(db, artist) ?? new Artist(artist);

                            trackArtists.Add(performer);
                        }
                    }
                    else
                    {
                        // we'll try with the album artist first
                        if (musicFile.Tag.FirstPerformer == _albumArtist.Name)
                        {
                            trackArtists.Add(_albumArtist);
                        }
                        else
                        {
                            var performer = Database.RetrieveArtistByName(db, musicFile.Tag.FirstPerformer) ??
                                            new Artist(musicFile.Tag.FirstPerformer);
                            trackArtists.Add(performer);
                        }

                        // check for a track in the local object instead of hitting the DB
                        try
                        {
                            // TODO not sure if this will work with the multiple artists now
                            _theAlbum.Tracks.First(
                                x => (x.TrackNumber == (int) musicFile.Tag.Track && x.Title == musicFile.Tag.Title));
                        }
                        catch (InvalidOperationException) {}
                    }

                    // update the running tally
                    foreach (var artist in trackArtists)
                    {
                        int result = 0;
                        if (_tally.ContainsKey(artist))
                            result = _tally[artist];

                        if (0 == result)
                            _tally.Add(artist, 1);
                        else
                            _tally[artist] = ++result;

                    }

                    // create a new track
                    theTrack = new Track((int) musicFile.Tag.Track, trackArtists, _theAlbum,
                                         musicFile.Properties.Duration, musicFile.Tag.Title, file);

                    // add the new track to the album
                    _theAlbum.AddTrack(theTrack);

                    // update the reference in the DB
                    Database.UpdateAddTrack(db, theTrack);
                }
            }
            catch (ArgumentOutOfRangeException e)
            {
                _log.Error("Taglib had problem reading: " + file, e);
                return;
            }
            catch (Db4oException e)
            {
                _log.Error("DB problem when processing track: " + file, e);
            }
            catch (IOException e)
            {
                _log.Error("File IO problem when processing track: " + file, e);
            }
        }
        public Artist RetrieveArtistByName(IDBClient db, string artistName)
        {
            var db4oClient = db as Db4oClient;

            // search for the track
            if (db4oClient != null)
            {
                IEnumerable<Artist> result = from Artist a in db4oClient.Client
                                             where a.Name == artistName
                                             select a;

                // return the first one if anything returned
                if (result.Count() > 0)
                {
                    return result.ToArray()[0];
                }
            }
            return null;
        }