/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package org.jampa.gui.runnables;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.jampa.controllers.Controller;
import org.jampa.gui.translations.Messages;
import org.jampa.logging.Log;
import org.jampa.model.disk.DirectoryItem;
import org.jampa.model.disk.FileItem;
public class LibraryUpdater implements IRunnableWithProgress {
private DirectoryItem _rootItem;
public LibraryUpdater(DirectoryItem rootItem) {
_rootItem = rootItem;
}
private void internalCheckDirectories(IProgressMonitor monitor, DirectoryItem directoryItem) {
// First add the files.
FileItem item;
Iterator<FileItem> iter = directoryItem.getFileList().iterator();
while (iter.hasNext()) {
if (monitor.isCanceled())
return;
item = iter.next();
monitor.subTask(Messages.getString("Controller.UpdateLibraryCheckDirectories") + item.getLabel()); //$NON-NLS-1$
if (!Controller.getInstance().getHSQLController().doesLibraryItemExists(item.getFileName())) {
Log.getInstance(Controller.class).info("Adding file to library: " + item.getFileName()); //$NON-NLS-1$
Controller.getInstance().getHSQLController().insertFileToLibrary(item.getFileName());
}
monitor.worked(1);
}
// then the directories.
Iterator<DirectoryItem> dirIter = directoryItem.getDirectoryList().iterator();
while (dirIter.hasNext()) {
internalCheckDirectories(monitor, dirIter.next());
}
}
private void internalCheckLibrary(IProgressMonitor monitor) {
ResultSet rs = Controller.getInstance().getHSQLController().getLibraryData();
try {
String path;
while (rs.next()) {
if (monitor.isCanceled())
return;
path = rs.getString("PATH"); //$NON-NLS-1$
monitor.subTask(Messages.getString("Controller.UpdateLibraryCheckDatabase") + path); //$NON-NLS-1$
if (!_rootItem.doesFileExists(path)) {
Log.getInstance(Controller.class).info("Removing file from library: " + path); //$NON-NLS-1$
Controller.getInstance().getHSQLController().deleteLibraryItem(path);
}
monitor.worked(1);
}
} catch (SQLException e) {
Log.getInstance(Controller.class).error(e.getMessage());
}
}
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
int nbFiles = _rootItem.countAllFileItems();
int nbRecords = Controller.getInstance().getHSQLController().getLibraryRecordCount();
Log.getInstance(Controller.class).info("Files number: " + nbFiles); //$NON-NLS-1$
Log.getInstance(Controller.class).info("Records number: " + nbRecords); //$NON-NLS-1$
monitor.beginTask(Messages.getString("Controller.UpdateLibrary"), nbFiles + nbRecords); //$NON-NLS-1$
internalCheckDirectories(monitor, _rootItem);
internalCheckLibrary(monitor);
monitor.done();
}
}