diff --git a/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy b/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy index 1bc0065..aeaeb3e 100644 --- a/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy +++ b/cli/src/main/groovy/com/jdbernard/wdiwtlt/cli/CommandLineInterface.groovy @@ -2,7 +2,7 @@ package com.jdbernard.wdiwtlt.cli import com.jdbernard.wdiwtlt.ConfigWrapper import com.jdbernard.wdiwtlt.MediaLibrary -import com.jdbernard.wdiwtlt.db.ORM +import com.jdbernard.wdiwtlt.db.DbApi import com.jdbernard.wdiwtlt.db.models.* import com.jdbernard.io.NonBlockingInputStreamReader import com.jdbernard.util.AnsiEscapeCodeSequence as ANSI @@ -183,10 +183,10 @@ Configuration: if (!hcfg) exitErr("Cannot load database configuation.") // Create our database instance - def orm + def dbapi try { def hds = new HikariDataSource(hcfg) - orm = new ORM(hds) } + dbapi = new DbApi(hds) } catch (Exception e) { exitErr("Could not establish a connection to the database:\n\t" + e.localizedMessage) } @@ -202,7 +202,7 @@ Configuration: // Create our CLI object try { - def cliInst = new CommandLineInterface(new MediaLibrary(orm, libRoot), + def cliInst = new CommandLineInterface(new MediaLibrary(dbapi, libRoot), vlcj, System.in, System.out, givenCfg) cliInst.repl() } catch (Exception e) { e.printStackTrace(); exitErr(e.localizedMessage) } diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy index bfd5f6d..828664f 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy @@ -1,6 +1,6 @@ package com.jdbernard.wdiwtlt -import com.jdbernard.wdiwtlt.db.ORM +import com.jdbernard.wdiwtlt.db.DbApi import com.jdbernard.wdiwtlt.db.models.* import java.sql.Timestamp @@ -22,32 +22,32 @@ public class MediaLibrary { private static UPPERCASE_PATTERN = Pattern.compile(/(.)(\p{javaUpperCase})/) - @Delegate ORM orm + @Delegate DbApi dbapi private File libraryRoot public long autoDeletePeriod = 1000 * 60 * 60 * 24 * 7 // one week - public MediaLibrary(ORM orm, File rootDir) { + public MediaLibrary(DbApi dbapi, File rootDir) { logger.debug("Creating a MediaLibrary rooted at: {}", rootDir.canonicalPath) - this.orm = orm + this.dbapi = dbapi this.libraryRoot = rootDir } public void clean() { - orm.removeEmptyAlbums() - orm.removeEmptyArtists() - orm.removeEmptyPlaylists() + dbapi.removeEmptyAlbums() + dbapi.removeEmptyArtists() + dbapi.removeEmptyPlaylists() Timestamp staleTS = new Timestamp(new Date().time - autoDeletePeriod) - orm.withTransaction { - def stalePlaylists = orm.getPlaylistsWhere( + dbapi.withTransaction { + def stalePlaylists = dbapi.getPlaylistsWhere( userCreated: false, lastUsedBefore: staleTS) - stalePlaylists.each { orm.delete(it) } } - orm.withTransaction { - def staleBookmarks = orm.getBookmarksWhere( + stalePlaylists.each { dbapi.delete(it) } } + dbapi.withTransaction { + def staleBookmarks = dbapi.getBookmarksWhere( userCreated: false, lastUsedBefore: staleTS) - staleBookmarks.each { orm.delete(it) } } } + staleBookmarks.each { dbapi.delete(it) } } } public def rescanLibrary() { def results = [ total: 0, ignored: 0, new: 0] @@ -70,7 +70,7 @@ public class MediaLibrary { return null } def relPath = getRelativePath(libraryRoot, f) - MediaFile mf = orm.getMediaFileByFilePath(relPath) + MediaFile mf = dbapi.getMediaFileByFilePath(relPath) if (mf) { logger.info( @@ -113,8 +113,8 @@ public class MediaLibrary { // Hash the file mf.fileHash = f.withInputStream { DigestUtils.md5Hex(it) } - orm.withTransaction { - orm.create(mf) + dbapi.withTransaction { + dbapi.create(mf) associateWithArtistsAndAlbums(mf, artistNames, albumNames, safeToInteger(fileTag?.getFirst(YEAR))) } @@ -124,38 +124,38 @@ public class MediaLibrary { public def getByIdOrName(Class modelClass, String input) { def match if (safeToInteger(input)) { - match = orm.getById(modelClass, safeToInteger(input)) + match = dbapi.getById(modelClass, safeToInteger(input)) if (match) match = [match] } else { - match = orm.getByName(modelClass, input) - if (!match) match = orm.getLike(modelClass, ["name"], [input]) } + match = dbapi.getByName(modelClass, input) + if (!match) match = dbapi.getLike(modelClass, ["name"], [input]) } return match } public List getArtistsByName(String name) { - return [orm.getArtistByName(name)] ?: orm.getArtistsLikeName(name) } + return [dbapi.getArtistByName(name)] ?: dbapi.getArtistsLikeName(name) } public List getAlbumsByName(String name) { - return orm.getAlbumsWhere(name: name) ?: orm.getAlbumsLikeName(name) } + return dbapi.getAlbumsWhere(name: name) ?: dbapi.getAlbumsLikeName(name) } public List collectMediaFiles(List models) { if (!models) return [] return models.collectMany { m -> if (m.class == MediaFile) return [m] - return orm.getMediaFilesWhere((idKeyFor(m.class)): m.id) }.findAll() } + return dbapi.getMediaFilesWhere((idKeyFor(m.class)): m.id) }.findAll() } public List splitArtist(Artist toSplit, Pattern splitPattern) { return splitArtist(toSplit, pattern.split(toSplit.name)) } public List splitArtist(Artist toSplit, List newNames) { - def albums = orm.getAlbumsWhere(artistId: toSplit.id) - def mediaFiles = orm.getMediaFilesByArtistId(toSplit.id) + def albums = dbapi.getAlbumsWhere(artistId: toSplit.id) + def mediaFiles = dbapi.getMediaFilesByArtistId(toSplit.id) toSplit.name = newNames[0] List newArtists = newNames[1..-1].collect { new Artist(it) } newArtists.each { newArtist -> - albums.each { orm.associate(newArtist, it) } - mediaFiles.each { orm.associate(newArtist, it) } } + albums.each { dbapi.associate(newArtist, it) } + mediaFiles.each { dbapi.associate(newArtist, it) } } return [toSplit] + newArtists } @@ -164,12 +164,12 @@ public class MediaLibrary { // Find or create artists. def artists = artistNames.collect { artistName -> - Artist artist = orm.getArtistByName(artistName) - if (!artist) artist = orm.create(new Artist(name: artistName)) + Artist artist = dbapi.getArtistByName(artistName) + if (!artist) artist = dbapi.create(new Artist(name: artistName)) return artist } // Associate file with artists. - artists.each { orm.associate(it, mf) } + artists.each { dbapi.associate(it, mf) } // Find or create albums def albums = albumNames.collect { albumName -> @@ -182,42 +182,42 @@ public class MediaLibrary { // associated with one of the artists for this piece. album = artists.inject(null, { foundAlbum, artist -> if (foundAlbum) return foundAlbum - def cur = orm.getAlbumsWhere(name: albumName, + def cur = dbapi.getAlbumsWhere(name: albumName, year: albumYear, artistId: artist.id) return cur ? cur[0] : null }) // If we don't have it with one of the artists, see if we have // one that matches the name and year. if (!album) { - def cur = orm.getAlbumsWhere( + def cur = dbapi.getAlbumsWhere( name: albumName, year: albumYear) album = cur ? cur[0] : null } } else { album = artists.inject(null, { foundAlbum, artist -> if (foundAlbum) return foundAlbum - def cur = orm.getAlbumsWhere( + def cur = dbapi.getAlbumsWhere( name: albumName, artistId: artist.id) return cur ? cur[0] : null }) if (!album) { - def cur = orm.getAlbumsWhere(name: albumName) + def cur = dbapi.getAlbumsWhere(name: albumName) album = cur ? cur[0] : null } } // We still can't find the album at all. We'll need to create it if (!album) - album = orm.create(new Album(name: albumName, year: albumYear)) + album = dbapi.create(new Album(name: albumName, year: albumYear)) return album } // Associate file with albums - albums.each { orm.associate(it, mf) } + albums.each { dbapi.associate(it, mf) } // Make sure we have association between all of the artists and albums. artists.each { artist -> - def albumsForArtist = orm.getAlbumsWhere(artistId: artist.id) + def albumsForArtist = dbapi.getAlbumsWhere(artistId: artist.id) def albumsMissing = albums - albumsForArtist - albumsMissing.each { album -> orm.associate(artist, album) } } } + albumsMissing.each { album -> dbapi.associate(artist, album) } } } /** #### `getRelativePath` * Given a parent path and a child path, assuming the child path is diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/DbApi.groovy similarity index 99% rename from core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy rename to core/src/main/groovy/com/jdbernard/wdiwtlt/db/DbApi.groovy index a952131..6b8dd8f 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/DbApi.groovy @@ -19,15 +19,15 @@ import org.slf4j.LoggerFactory import com.jdbernard.wdiwtlt.db.models.* -public class ORM { +public class DbApi { private DataSource dataSource private Sql sql private static UPPERCASE_PATTERN = Pattern.compile(/(.)(\p{javaUpperCase})/) - private static Logger logger = LoggerFactory.getLogger(ORM) + private static Logger logger = LoggerFactory.getLogger(DbApi) - public ORM(DataSource dataSource) { + public DbApi(DataSource dataSource) { this.dataSource = dataSource this.sql = new Sql(dataSource) }