package net.kirke.mp3dj;
import java.io.File;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.kirke.mp3dj.resources.Msgs;
import net.kirke.mp3dj.resources.Props;
/**
* Refresh daemon that keeps database reflecting tree of mp3 files.
*
* HSQLDB - Lightweight 100% Java SQL Database Engine
* http://hsqldb.org/
*
* JDBC (java.sql) docs:
* http://java.sun.com/javase/6/docs/api/
*
* @author Kirk Erickson (latest modification by $Author: kirke $)
* @version $Revision: 1.3 $ $Date: 2008-06-21 04:46:35 $
*/
public class Refresh implements Runnable, Serializable {
private Msgs msgs;
private RefreshFiles files;
private static String mp3Dir = Props.getProperty("mp3.dir");
/**
* Refresh (recreate) database by reading and parsing id3 tag
* information from mp3 files subordinate to 'mp3Dir'.
*/
protected Refresh() {
files = null;
msgs = new Msgs();
}
/**
* RefreshHolder is loaded on the first execution of Refresh.getInstance()
* or the first access to RefreshHolder.instance , not before.
*/
private static class RefreshHolder {
private final static Refresh INSTANCE = new Refresh();
}
public static Refresh getInstance() {
return RefreshHolder.INSTANCE;
}
/**
* Process files under dir (recursively)
*/
private void visitAllFiles(File dir, RefreshFiles files) {
if (dir.isDirectory()) {
if ((dir.getName()).endsWith("[Incoming]")) {
return;
}
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllFiles(new File(dir, children[i]), files);
}
} else if (!dir.exists()) {
Object params1[] = { dir.getAbsolutePath() };
String msg = msgs.getMessage("SKIPPED_DOES_NOT_EXIST", params1);
System.out.println("visitAllFiles: " + msg);
} else {
files.insert(dir);
}
}
public void run() {
File dir = new File(mp3Dir);
String msg = null;
long startTime = System.currentTimeMillis();
if (!dir.exists()) {
Object params1[] = { mp3Dir };
msg = msgs.getMessage("DOES_NOT_EXIST", params1);
System.out.println("run: " + msg);
return;
}
Object params2[] = { ConnectionHelper.getUrl(), mp3Dir };
msg = msgs.getMessage("REFRESHING", params2);
System.out.println("run: " + msg);
Connection connection = null;
try {
connection = ConnectionHelper.getConnection();
if (connection == null) {
msg = msgs.getMessage("REFRESH_FAILED", params2);
System.out.println("run: " + msg);
return;
}
RefreshFolders folders = new RefreshFolders(connection);
RefreshGenres genres = new RefreshGenres(connection);
files = new RefreshFiles(connection, mp3Dir, folders, genres);
// Delete items from files table which no longer exist.
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM files");
while (rs.next()) {
String path = mp3Dir + File.separator
+ rs.getString(2) + File.separator + rs.getString(3);
File f = new File(path);
if (!f.exists()) {
folders.delete(rs.getString(2)); // folder
genres.delete(rs.getString(16)); // genre
System.out.println("delete: " + path);
files.delete(rs.getInt(1)); // id
}
}
// Insert new items into files table or update changed files.
visitAllFiles(dir, files);
} catch (SQLException x) {
x.printStackTrace();
throw new DAOException(x);
} finally {
ConnectionHelper.close(connection);
}
long milliseconds = System.currentTimeMillis() - startTime;
int mins = (int) Math.floor(milliseconds/1000 / 60);
int secs = (int) Math.floor(milliseconds/1000) % 60;
Object params3[] = { ConnectionHelper.getUrl(), mins, secs };
if (mins == 0) {
msg = msgs.getMessage("REFRESHED_SUCCESSFULLY_NOMINS", params3);
} else {
msg = msgs.getMessage("REFRESHED_SUCCESSFULLY", params3);
}
System.out.println("run: " + msg);
}
}