MediaLibrary: Moved some helpers for working with model classes, names, and files into here.

This commit is contained in:
Jonathan Bernard 2016-03-14 03:50:51 -05:00
parent e148acd897
commit 9205732b0e

View File

@ -20,6 +20,8 @@ public class MediaLibrary {
private static Logger logger = LoggerFactory.getLogger(MediaLibrary) private static Logger logger = LoggerFactory.getLogger(MediaLibrary)
private static UPPERCASE_PATTERN = Pattern.compile(/(.)(\p{javaUpperCase})/)
@Delegate ORM orm @Delegate ORM orm
private File libraryRoot private File libraryRoot
@ -135,6 +137,13 @@ public class MediaLibrary {
public List<Album> getAlbumsByName(String name) { public List<Album> getAlbumsByName(String name) {
return orm.getAlbumsWhere(name: name) ?: orm.getAlbumsLikeName(name) } return orm.getAlbumsWhere(name: name) ?: orm.getAlbumsLikeName(name) }
public List<MediaFile> collectMediaFiles(List<Model> models) {
if (!models) return []
return models.collectMany { m ->
if (m.class == MediaFile) return [m]
return orm.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)) }
@ -233,9 +242,21 @@ public class MediaLibrary {
if (b != parentPath.length) return "" if (b != parentPath.length) return ""
return (['.'] + childPath[b..<childPath.length]).join('/') } return (['.'] + childPath[b..<childPath.length]).join('/') }
public static Integer safeToInteger(def val) { public static Integer safeToInteger(def val) {
if (val == null) return null if (val == null) return null
try { return val.trim() as Integer } try { return val.trim() as Integer }
catch (NumberFormatException nfe) { return null } } catch (NumberFormatException nfe) { return null } }
public static String uncapitalize(String s) {
if (s == null) return null;
if (s.length() < 2) return s.toLowerCase();
return s[0].toLowerCase() + s[1..-1] }
public static String toEnglish(Class c) {
return UPPERCASE_PATTERN.matcher(c.simpleName).
replaceAll(/$1 $2/).toLowerCase() }
public static String idKeyFor(Class c) {
return uncapitalize(c.simpleName) + 'Id'; }
} }