}
}
public static void saveData() {
try {
ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:file:" + dbFileName);
Dao<Show, Integer> showDao = DaoManager.createDao(connectionSource, Show.class);
Dao<Season, Integer> seasonDao = DaoManager.createDao(connectionSource, Season.class);
Dao<Episode, Integer> episodeDao = DaoManager.createDao(connectionSource, Episode.class);
int numShows = 0;
int numSeasons = 0;
int numEpisodes = 0;
ArrayList<Show> shows = ShowLibrary.getInstance().shows;
for(Show show : shows) {
showDao.createOrUpdate(show);
numShows++;
for(Season season : show.seasons) {
seasonDao.createOrUpdate(season);
numSeasons++;
for(Episode episode : season.getEpisodes()) {
episodeDao.createOrUpdate(episode);
numEpisodes++;
}
}
}
for(Show dbShow : showDao.queryForAll()) {
Show myShow = ShowLibrary.getInstance().getShow(dbShow.getTitle());
if(myShow==null) {
for(Season season : dbShow.seasons) {
episodeDao.delete(season.getEpisodes());
}
seasonDao.delete(dbShow.seasons);
showDao.delete(dbShow);
}
}
Dao<Settings, Integer> settingsDao = DaoManager.createDao(connectionSource, Settings.class);
Dao<Feed, Integer> feedDao = DaoManager.createDao(connectionSource, Feed.class);
Dao<Search, Integer> searchDao = DaoManager.createDao(connectionSource, Search.class);
Settings settings = Settings.getInstance();
for(Feed feed : settings.getFeeds()) {
feedDao.createOrUpdate(feed);
}
for(Search search : settings.getSearches()) {
searchDao.createOrUpdate(search);
}
settingsDao.createOrUpdate(settings);
LOGGER.fine("Stored " + numShows + " shows, " + numSeasons + " seasons, and " + numEpisodes + " episodes in database.");
connectionSource.close();
} catch (SQLException e) {
LOGGER.severe("SQLException: " + e.toString());
System.exit(1);
}
}