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()
}
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<Artist> getArtistsByName(String name) {

View File

@ -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<String> columns, List<Object> values,
Class modelClass) {
public def getBy(Class modelClass, List<String> columns,
List<Object> 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<String> columns, List<Object> values,
Class modelClass) {
public def getLike(Class modelClass, List<String> columns,
List<Object> 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<Album> 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<Album> 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<Album> getAlbums() { return getAll(Album) }
public List<Album> getAlbumsLikeName(String name) {
return getLike(["name"], [name], Album) }
return getLike(Album, ["name"], [name]) }
public List<Album> 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<Artist> getArtistByName(String name) {
return getByName(name, Artist) }
return getByName(Artist, name) }
public List<Artist> getArtists() { return getAll(Artist) }
public List<Artist> 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<Artist> getArtistsLikeName(String name) {
return getLike(["name"], [name], Artist) }
return getLike(Artist, ["name"], [name]) }
public List<Artist> 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<Bookmark> getBookmarkByName(String name) {
return getByName(name, Bookmark) }
return getByName(Bookmark, name) }
public List<Bookmark> 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<MediaFile> getMediaFileByName(String name) {
return getByName(name, MediaFile) }
return getByName(MediaFile, name) }
public List<MediaFile> getMediaFiles() { return getAll(MediaFile) }
public List<MediaFile> 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<Playlist> getPlaylistByName(String name) {
return getByName(name, Playlist) }
return getByName(Playlist, name) }
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() {
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) } }
}