Re-ordered argument lists for functions taking a model class.

This commit is contained in:
Jonathan Bernard 2016-02-12 14:18:43 -06:00
parent 069972bfd5
commit a795397b3b
2 changed files with 51 additions and 41 deletions

View File

@ -33,9 +33,7 @@ public class MediaLibrary {
orm.removeEmptyPlaylists() orm.removeEmptyPlaylists()
} }
public void rescanLibrary() { public void rescanLibrary() { libraryRoot.eachFileRecurse { addFile(it) } }
libraryRoot.eachFileRecurse { addFile(it) }
}
public MediaFile addFile(File f) { public MediaFile addFile(File f) {
if (!f.exists() || !f.isFile()) { 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 def match
if (safeToInteger(input)) match = [orm.getById(safeToInteger(input), modelClass)] if (safeToInteger(input)) match = [orm.getById(modelClass, safeToInteger(input))]
else { else {
match = orm.getByName(input, modelClass) match = orm.getByName(modelClass, input)
if (!match) match = orm.getLike(["name"], [input], modelClass) } if (!match) match = orm.getLike(modelClass, ["name"], [input]) }
return match } return match }
public List<Artist> getArtistsByName(String name) { public List<Artist> getArtistsByName(String name) {

View File

@ -37,19 +37,24 @@ public class ORM {
def query = "SELECT * FROM " + def query = "SELECT * FROM " +
pluralize(nameFromModel(modelClass.simpleName)) pluralize(nameFromModel(modelClass.simpleName))
logger.debug("Selecting models.\n\tSQL: {}", query) 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() def query = new StringBuilder()
.append("SELECT * FROM ") .append("SELECT * FROM ")
.append(pluralize(nameFromModel(modelClass.simpleName))) .append(pluralize(nameFromModel(model.class.simpleName)))
.append(" WHERE id = ?") .append(" WHERE id = ?")
.toString() .toString()
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, id) logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, model.id)
return recordToModel(sql.firstRow(query, [id]), modelClass) } 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() def query = new StringBuilder()
.append("SELECT * FROM ") .append("SELECT * FROM ")
.append(pluralize(nameFromModel(modelClass.simpleName))) .append(pluralize(nameFromModel(modelClass.simpleName)))
@ -59,8 +64,8 @@ public class ORM {
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, name) logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, name)
return sql.rows(query, [name]).collect { recordToModel(it, modelClass) } } return sql.rows(query, [name]).collect { recordToModel(it, modelClass) } }
public def getBy(List<String> columns, List<Object> values, public def getBy(Class modelClass, List<String> columns,
Class modelClass) { List<Object> values) {
def query = new StringBuilder() def query = new StringBuilder()
.append("SELECT * FROM ") .append("SELECT * FROM ")
.append(pluralize(nameFromModel(modelClass.simpleName))) .append(pluralize(nameFromModel(modelClass.simpleName)))
@ -72,8 +77,8 @@ public class ORM {
return sql.rows(query, values) return sql.rows(query, values)
.collect { recordToModel(it, modelClass) } } .collect { recordToModel(it, modelClass) } }
public def getLike(List<String> columns, List<Object> values, public def getLike(Class modelClass, List<String> columns,
Class modelClass) { List<Object> values) {
values = values.collect { "%$it%".toString() } values = values.collect { "%$it%".toString() }
String query = new StringBuilder() String query = new StringBuilder()
.append("SELECT * FROM ") .append("SELECT * FROM ")
@ -174,10 +179,10 @@ public class ORM {
return associate(m1.class, m2.class, m1.id, m2.id) } return associate(m1.class, m2.class, m1.id, m2.id) }
/// ### Album-specific methods /// ### Album-specific methods
public Album getAlbumById(int id) { return getById(id, Album) } public Album getAlbumById(int id) { return getById(Album, id) }
public List<Album> getAlbumByName(String name) { public List<Album> getAlbumByName(String name) {
return getByName(name, Album) } return getByName(Album, name) }
public Album getAlbumByNameAndArtistId(String name, int artistId) { public Album getAlbumByNameAndArtistId(String name, int artistId) {
def query = """\ def query = """\
@ -191,7 +196,7 @@ public class ORM {
logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}",
query, params) query, params)
def albums = sql.rows(query, params) def albums = sql.rows(query, params)
.collect { recordToModel(it, Album) } .collect { recordToModel(Album, it) }
return albums ? albums[0] : null } return albums ? albums[0] : null }
public List<Album> getAlbumsByArtistId(int artistId) { public List<Album> getAlbumsByArtistId(int artistId) {
@ -204,20 +209,20 @@ public class ORM {
logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", query, artistId) logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", query, artistId)
return sql.rows(query, [artistId]) return sql.rows(query, [artistId])
.collect { recordToModel(it, Album) } } .collect { recordToModel(Album, it) } }
public List<Album> getAlbums() { return getAll(Album) } public List<Album> getAlbums() { return getAll(Album) }
public List<Album> getAlbumsLikeName(String name) { public List<Album> getAlbumsLikeName(String name) {
return getLike(["name"], [name], Album) } return getLike(Album, ["name"], [name]) }
public List<Album> removeEmptyAlbums() { public List<Album> removeEmptyAlbums() {
throw new UnsupportedOperationException("Not yet implemented."); throw new UnsupportedOperationException("Not yet implemented.");
} }
/// ### Artist-specific methods /// ### Artist-specific methods
public Artist getArtistById(int id) { return getById(id, Artist) } public Artist getArtistById(int id) { return getById(Artist, id) }
public List<Artist> getArtistByName(String name) { public List<Artist> getArtistByName(String name) {
return getByName(name, Artist) } return getByName(Artist, name) }
public List<Artist> getArtists() { return getAll(Artist) } public List<Artist> getArtists() { return getAll(Artist) }
public List<Artist> getArtistsByAlbumId(int albumId) { public List<Artist> getArtistsByAlbumId(int albumId) {
@ -229,10 +234,10 @@ public class ORM {
aa.album_id = ?""" aa.album_id = ?"""
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}", query, artistId) logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}", query, artistId)
return sql.rows(query, [albumId]) return sql.rows(query, [albumId])
.collect { recordToModel(it, Artist) } } .collect { recordToModel(Artist, it) } }
public List<Artist> getArtistsLikeName(String name) { public List<Artist> getArtistsLikeName(String name) {
return getLike(["name"], [name], Artist) } return getLike(Artist, ["name"], [name]) }
public List<Artist> removeEmptyArtists() { public List<Artist> removeEmptyArtists() {
throw new UnsupportedOperationException("Not yet implemented."); throw new UnsupportedOperationException("Not yet implemented.");
@ -242,29 +247,29 @@ public class ORM {
return associate(Artist, Album, artistId, albumId) } return associate(Artist, Album, artistId, albumId) }
/// ### Bookmark-specific methods /// ### Bookmark-specific methods
public Bookmark getBookmarkById(int id) { return getById(id, Bookmark) } public Bookmark getBookmarkById(int id) { return getById(Bookmark, id) }
public List<Bookmark> getBookmarkByName(String name) { public List<Bookmark> getBookmarkByName(String name) {
return getByName(name, Bookmark) } return getByName(Bookmark, name) }
public List<Bookmark> getBookmarks() { return getAll(Bookmark) } public List<Bookmark> getBookmarks() { return getAll(Bookmark) }
/// ### Image-specific methods /// ### Image-specific methods
public Image getImageById(int id) { return getById(id, Image) } public Image getImageById(int id) { return getById(Image, id) }
/// ### MediaFile-specific methods /// ### MediaFile-specific methods
public MediaFile getMediaFileById(int id) { return getById(id, MediaFile) } public MediaFile getMediaFileById(int id) { return getById(MediaFile, id) }
public List<MediaFile> getMediaFileByName(String name) { public List<MediaFile> getMediaFileByName(String name) {
return getByName(name, MediaFile) } return getByName(MediaFile, name) }
public List<MediaFile> getMediaFiles() { return getAll(MediaFile) } public List<MediaFile> getMediaFiles() { return getAll(MediaFile) }
public List<MediaFile> getMediaFilesLikeName(String name) { public List<MediaFile> getMediaFilesLikeName(String name) {
return getLike(["name"], [name], MediaFile) } return getLike(MediaFile, ["name"], [name]) }
public MediaFile getMediaFileByFilePath(String filePath) { public MediaFile getMediaFileByFilePath(String filePath) {
def files = getBy(["file_path"], [filePath], MediaFile) def files = getBy(MediaFile, ["file_path"], [filePath])
return files ? files[0] : null } return files ? files[0] : null }
public def associateMediaFileWithAlbum(int mediaFileId, int albumId) { public def associateMediaFileWithAlbum(int mediaFileId, int albumId) {
@ -289,20 +294,27 @@ public class ORM {
return mf } return mf }
/// ### Playlist-specific methods /// ### Playlist-specific methods
public Playlist getPlaylistById(int id) { return getById(id, Playlist) } public Playlist getPlaylistById(int id) { return getById(Playlist, id) }
public List<Playlist> getPlaylistByName(String name) { public List<Playlist> getPlaylistByName(String name) {
return getByName(name, Playlist) } return getByName(Playlist, name) }
public List<Playlist> getPlaylists() { return getAll(Playlist) } public List<Playlist> 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<Playlist> removeEmptyPlaylists() { public List<Playlist> removeEmptyPlaylists() {
throw new UnsupportedOperationException("Not yet implemented."); throw new UnsupportedOperationException("Not yet implemented.");
} }
/// ### Tag-specific methods /// ### Tag-specific methods
public Tag getTagById(int id) { return getById(id, Tag) } public Tag getTagById(int id) { return getById(Tag, id) }
public Tag getTagByName(String name) { return getByName(name, Tag)[0] } public Tag getTagByName(String name) { return getByName(Tag, name)[0] }
/// ### Utility functions /// ### Utility functions
public void withTransaction(Closure c) { public void withTransaction(Closure c) {
@ -325,10 +337,10 @@ public class ORM {
field.set(model, record[nameFromModel(field.name)]) } field.set(model, record[nameFromModel(field.name)]) }
return model } return model }
static def recordToModel(def record, Class clazz) { static def recordToModel(Class modelClass, def record) {
if (record == null) return null if (record == null) return null
def model = clazz.newInstance() def model = modelClass.newInstance()
getInstanceFields(model.class).each { field -> getInstanceFields(model.class).each { field ->
field.set(model, record[nameFromModel(field.name)]) } field.set(model, record[nameFromModel(field.name)]) }
@ -345,6 +357,6 @@ public class ORM {
return record } return record }
static def getInstanceFields(Class clazz) { static def getInstanceFields(Class modelClass) {
return clazz.fields.findAll { !Modifier.isStatic(it.modifiers) } } return modelClass.fields.findAll { !Modifier.isStatic(it.modifiers) } }
} }