POOLab18-SlopifyV2/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala

166 lines
5.9 KiB
Scala

package ch.hevs.isc.slopify_v2
import java.io._
import scala.collection.immutable.HashMap
import scala.io.{BufferedSource, Source}
object DataBaseHelper {
def create(directory:String) : DataBase = {
val db = new DataBase()
for (a <- new File(directory).listFiles() if a.isDirectory) {
val artistName = a.getName
//println(s"found new artist : $artistName")
val artist: Artist = new Artist(artistName)
for (b <- a.listFiles() if b.isDirectory ) {
val albumName = b.getName
//println(s"found new album $albumName for artist : $artistName")
val album: Album = new Album(albumName)
for (c <- b.listFiles() if c.isFile if c.getName.toLowerCase().endsWith(".mp3") ) {
val fileName = c.getName.substring(0, c.getName.length - 4)
val format1 = """(\d*) (.*)""".r
val format2 = """(\d*)-(\d*) (.*)""".r
val format3 = """(\d*)-(.*)""".r
var songName: String = ""
var songNumber: Int = 0
fileName match {
case format1(nr, name) => {
//println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '${a.getName}'")
songName = name
songNumber = Integer.parseInt(nr)
}
case format2(cd, nr, name) => {
//println(s"found song nr #$nr on cd#$cd name:'$name' in album '$albumName' for artist : '${a.getName}'")
songName = name
songNumber = Integer.parseInt(nr)
}
case format3(nr, name) => {
//println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '$artistName'")
songName = name
songNumber = Integer.parseInt(nr)
}
}
val song: Song = new Song(songName, songNumber)
album.addSong(song)
}
artist.addAlbum(album)
}
db.addArtist(artist)
}
return db
}
def save(fileName: String, db: DataBase) : Unit = {
val fos: FileOutputStream = new FileOutputStream(fileName)
val oos: ObjectOutputStream = new ObjectOutputStream(fos)
oos.writeObject(db)
oos.close()
}
def load(fileName:String) : DataBase = {
val fis: FileInputStream = new FileInputStream(fileName)
val ois: ObjectInputStream = new ObjectInputStream(fis)
val db: DataBase = ois.readObject().asInstanceOf[DataBase]
ois.close()
return db
}
def exportHTML(directory: String, dataBase: DataBase): Unit = {
var artists: Array[Artist] = dataBase.getArtists().sortBy(_.name)
var artistRows: String = ""
for (artist: Artist <- artists) {
val artistFileName: String = formatName(artist.name)
val artistUrl: String = s"./$artistFileName.html"
artistRows += s"<tr><td><a href='$artistUrl'>${artist.name}</a></td></tr>\n"
val albums: Array[Album] = artist.getAlbums().sortBy(_.name)
var albumRows: String = ""
for (album: Album <- albums) {
val albumFileName: String = artistFileName + "_" + formatName(album.name)
val albumUrl: String = s"./$albumFileName.html"
albumRows += s"<tr><td><a href='$albumUrl'>${album.name}</a></td></tr>\n"
val songs: Array[Song] = album.getSongs().sortBy(_.number)
var songRows: String = ""
for (song: Song <- songs) {
val songFileName: String = albumFileName + "_" + formatName(song.number + "-" + song.title)
val songUrl: String = s"./$songFileName.html"
songRows += s"<tr><td>${song.number}</td><td><a href='$songUrl'>${song.title}</a></td></tr>\n"
formatTemplate("res/template/song.html", HashMap(
"artist.name" -> artist.name,
"artist.page" -> artistUrl,
"album.name" -> album.name,
"album.page" -> albumUrl,
"song.title" -> song.title,
"song.number" -> song.number.toString
), directory + "/" + songFileName + ".html")
}
formatTemplate("res/template/album.html", HashMap(
"songs" -> songRows,
"artist.name" -> artist.name,
"artist.page" -> artistUrl,
"album.name" -> album.name
), directory + "/" + albumFileName + ".html")
}
formatTemplate("res/template/artist.html", HashMap(
"albums" -> albumRows,
"artist.name" -> artist.name
), directory + "/" + artistFileName + ".html")
}
formatTemplate("res/template/index.html", HashMap(
"artists" -> artistRows
), directory + "/" + "index.html")
copyFile("res/template/base.js", directory + "/base.js")
copyFile("res/template/base.css", directory + "/base.css")
copyFile("res/template/index.css", directory + "/index.css")
copyFile("res/template/artist.css", directory + "/artist.css")
copyFile("res/template/album.css", directory + "/album.css")
copyFile("res/template/song.css", directory + "/song.css")
}
def copyFile(from: String, to: String): Unit = {
val fis: FileInputStream = new FileInputStream(from)
val fos: FileOutputStream = new FileOutputStream(to)
fos.write(fis.readAllBytes())
fis.close()
fos.close()
}
def formatName(name: String): String = {
var formatted: String = name.toLowerCase
formatted = formatted.replace(" ", "_")
formatted = formatted.replace("'", "-")
formatted = formatted.replaceAll("[^a-zA-Z_0-9\\-().]", "")
return formatted
}
def formatTemplate(templateName: String, variables: Map[String, String], outPath: String): Unit = {
val source: BufferedSource = Source.fromFile(templateName)
var template: String = source.getLines().mkString("\n")
source.close()
for ((key: String, value: String) <- variables) {
template = template.replace(s"{{$key}}", value)
}
val fos: FileOutputStream = new FileOutputStream(outPath)
fos.write(template.getBytes)
fos.close()
}
def main(args: Array[String]): Unit = {
val db: DataBase = create("res/songs")
exportHTML("/tmp/html", db)
}
}