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