diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy index 78606f4..e3091ad 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy @@ -33,9 +33,7 @@ public class MediaLibrary { orm.removeEmptyPlaylists() } - public void rescanLibrary() { - libraryRoot.eachFileRecurse { addFile(it) } - } + public void rescanLibrary() { libraryRoot.eachFileRecurse { addFile(it) } } public MediaFile addFile(File f) { if (!f.exists() || !f.isFile()) { @@ -91,12 +89,12 @@ public class MediaLibrary { } } - public def getByIdOrName(String input, Class modelClass) { + public def getByIdOrName(Class modelClass, String input) { def match - if (safeToInteger(input)) match = [orm.getById(safeToInteger(input), modelClass)] + if (safeToInteger(input)) match = [orm.getById(modelClass, safeToInteger(input))] else { - match = orm.getByName(input, modelClass) - if (!match) match = orm.getLike(["name"], [input], modelClass) } + match = orm.getByName(modelClass, input) + if (!match) match = orm.getLike(modelClass, ["name"], [input]) } return match } public List getArtistsByName(String name) { diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy index 445aad0..c699db3 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy @@ -37,19 +37,24 @@ public class ORM { def query = "SELECT * FROM " + pluralize(nameFromModel(modelClass.simpleName)) logger.debug("Selecting models.\n\tSQL: {}", query) - return sql.rows(query).collect { recordToModel(it, modelClass) } } + return sql.rows(query).collect { recordToModel(modelClass, it) } } - public def getById(int id, Class modelClass) { + public def getById(Class modelClass, int id) { + def model = modelClass.instantiate() + model.id = id + return refresh(model) } + + public def refresh(def model) { def query = new StringBuilder() .append("SELECT * FROM ") - .append(pluralize(nameFromModel(modelClass.simpleName))) + .append(pluralize(nameFromModel(model.class.simpleName))) .append(" WHERE id = ?") .toString() - logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, id) - return recordToModel(sql.firstRow(query, [id]), modelClass) } + logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, model.id) + return updateModel(sql.firstRow(query, [id]), model) } - public def getByName(String name, Class modelClass) { + public def getByName(Class modelClass, String name) { def query = new StringBuilder() .append("SELECT * FROM ") .append(pluralize(nameFromModel(modelClass.simpleName))) @@ -59,8 +64,8 @@ public class ORM { logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, name) return sql.rows(query, [name]).collect { recordToModel(it, modelClass) } } - public def getBy(List columns, List values, - Class modelClass) { + public def getBy(Class modelClass, List columns, + List values) { def query = new StringBuilder() .append("SELECT * FROM ") .append(pluralize(nameFromModel(modelClass.simpleName))) @@ -72,8 +77,8 @@ public class ORM { return sql.rows(query, values) .collect { recordToModel(it, modelClass) } } - public def getLike(List columns, List values, - Class modelClass) { + public def getLike(Class modelClass, List columns, + List values) { values = values.collect { "%$it%".toString() } String query = new StringBuilder() .append("SELECT * FROM ") @@ -174,10 +179,10 @@ public class ORM { return associate(m1.class, m2.class, m1.id, m2.id) } /// ### Album-specific methods - public Album getAlbumById(int id) { return getById(id, Album) } + public Album getAlbumById(int id) { return getById(Album, id) } public List getAlbumByName(String name) { - return getByName(name, Album) } + return getByName(Album, name) } public Album getAlbumByNameAndArtistId(String name, int artistId) { def query = """\ @@ -191,7 +196,7 @@ public class ORM { logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", query, params) def albums = sql.rows(query, params) - .collect { recordToModel(it, Album) } + .collect { recordToModel(Album, it) } return albums ? albums[0] : null } public List getAlbumsByArtistId(int artistId) { @@ -204,20 +209,20 @@ public class ORM { logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", query, artistId) return sql.rows(query, [artistId]) - .collect { recordToModel(it, Album) } } + .collect { recordToModel(Album, it) } } public List getAlbums() { return getAll(Album) } public List getAlbumsLikeName(String name) { - return getLike(["name"], [name], Album) } + return getLike(Album, ["name"], [name]) } public List removeEmptyAlbums() { throw new UnsupportedOperationException("Not yet implemented."); } /// ### Artist-specific methods - public Artist getArtistById(int id) { return getById(id, Artist) } + public Artist getArtistById(int id) { return getById(Artist, id) } public List getArtistByName(String name) { - return getByName(name, Artist) } + return getByName(Artist, name) } public List getArtists() { return getAll(Artist) } public List getArtistsByAlbumId(int albumId) { @@ -229,10 +234,10 @@ public class ORM { aa.album_id = ?""" logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}", query, artistId) return sql.rows(query, [albumId]) - .collect { recordToModel(it, Artist) } } + .collect { recordToModel(Artist, it) } } public List getArtistsLikeName(String name) { - return getLike(["name"], [name], Artist) } + return getLike(Artist, ["name"], [name]) } public List removeEmptyArtists() { throw new UnsupportedOperationException("Not yet implemented."); @@ -242,29 +247,29 @@ public class ORM { return associate(Artist, Album, artistId, albumId) } /// ### Bookmark-specific methods - public Bookmark getBookmarkById(int id) { return getById(id, Bookmark) } + public Bookmark getBookmarkById(int id) { return getById(Bookmark, id) } public List getBookmarkByName(String name) { - return getByName(name, Bookmark) } + return getByName(Bookmark, name) } public List getBookmarks() { return getAll(Bookmark) } /// ### Image-specific methods - public Image getImageById(int id) { return getById(id, Image) } + public Image getImageById(int id) { return getById(Image, id) } /// ### MediaFile-specific methods - public MediaFile getMediaFileById(int id) { return getById(id, MediaFile) } + public MediaFile getMediaFileById(int id) { return getById(MediaFile, id) } public List getMediaFileByName(String name) { - return getByName(name, MediaFile) } + return getByName(MediaFile, name) } public List getMediaFiles() { return getAll(MediaFile) } public List getMediaFilesLikeName(String name) { - return getLike(["name"], [name], MediaFile) } + return getLike(MediaFile, ["name"], [name]) } public MediaFile getMediaFileByFilePath(String filePath) { - def files = getBy(["file_path"], [filePath], MediaFile) + def files = getBy(MediaFile, ["file_path"], [filePath]) return files ? files[0] : null } public def associateMediaFileWithAlbum(int mediaFileId, int albumId) { @@ -289,20 +294,27 @@ public class ORM { return mf } /// ### Playlist-specific methods - public Playlist getPlaylistById(int id) { return getById(id, Playlist) } + public Playlist getPlaylistById(int id) { return getById(Playlist, id) } public List getPlaylistByName(String name) { - return getByName(name, Playlist) } + return getByName(Playlist, name) } public List getPlaylists() { return getAll(Playlist) } + public Playlist addToPlaylist(Playlist p, MediaFile mf) { + return withTransaction { + associate(p, mf) + p.mediaFileCount++ + update(p) + return refresh(p) } } + public List removeEmptyPlaylists() { throw new UnsupportedOperationException("Not yet implemented."); } /// ### Tag-specific methods - public Tag getTagById(int id) { return getById(id, Tag) } - public Tag getTagByName(String name) { return getByName(name, Tag)[0] } + public Tag getTagById(int id) { return getById(Tag, id) } + public Tag getTagByName(String name) { return getByName(Tag, name)[0] } /// ### Utility functions public void withTransaction(Closure c) { @@ -325,10 +337,10 @@ public class ORM { field.set(model, record[nameFromModel(field.name)]) } return model } - static def recordToModel(def record, Class clazz) { + static def recordToModel(Class modelClass, def record) { if (record == null) return null - def model = clazz.newInstance() + def model = modelClass.newInstance() getInstanceFields(model.class).each { field -> field.set(model, record[nameFromModel(field.name)]) } @@ -345,6 +357,6 @@ public class ORM { return record } - static def getInstanceFields(Class clazz) { - return clazz.fields.findAll { !Modifier.isStatic(it.modifiers) } } + static def getInstanceFields(Class modelClass) { + return modelClass.fields.findAll { !Modifier.isStatic(it.modifiers) } } }