2009-05-11 20:17:53 -04:00
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
package net.sourceforge.filebot;
|
2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
import static net.sourceforge.filebot.History.*;
|
2009-05-17 13:14:03 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
import java.io.File;
|
2009-05-17 13:14:03 -04:00
|
|
|
import java.io.IOException;
|
2009-07-20 07:03:24 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2009-05-11 20:17:53 -04:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
import net.sourceforge.filebot.History.Element;
|
2009-07-20 07:03:24 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
|
2011-09-09 10:50:01 -04:00
|
|
|
public final class HistorySpooler {
|
2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
private static final HistorySpooler instance = new HistorySpooler();
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
public static HistorySpooler getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2013-02-25 12:27:34 -05:00
|
|
|
private HistoryStorage persistentHistory = null;
|
|
|
|
private History sessionHistory = new History();
|
2009-05-11 20:17:53 -04:00
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
public synchronized History getCompleteHistory() {
|
2009-05-11 20:17:53 -04:00
|
|
|
History history = new History();
|
|
|
|
|
|
|
|
// add persistent history
|
2013-02-25 12:27:34 -05:00
|
|
|
try {
|
|
|
|
if (getPersistentHistory() != null) {
|
|
|
|
history.addAll(getPersistentHistory().read().sequences());
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
2013-02-25 12:27:34 -05:00
|
|
|
} catch (IOException e) {
|
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load history", e);
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add session history
|
2009-05-17 13:14:03 -04:00
|
|
|
history.addAll(sessionHistory.sequences());
|
2009-05-11 20:17:53 -04:00
|
|
|
|
|
|
|
return history;
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
|
|
|
public History getSessionHistory() {
|
|
|
|
return sessionHistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-10 12:21:54 -04:00
|
|
|
public synchronized void append(Iterable<Entry<File, File>> elements) {
|
2009-07-20 07:03:24 -04:00
|
|
|
List<Element> sequence = new ArrayList<Element>();
|
|
|
|
|
2011-10-10 12:21:54 -04:00
|
|
|
for (Entry<File, File> element : elements) {
|
|
|
|
sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile()));
|
2009-07-20 07:03:24 -04:00
|
|
|
}
|
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
// append to session history
|
2011-09-09 10:50:01 -04:00
|
|
|
if (sequence.size() > 0) {
|
|
|
|
sessionHistory.add(sequence);
|
|
|
|
}
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-17 13:14:03 -04:00
|
|
|
public synchronized void commit(History history) {
|
|
|
|
try {
|
2013-02-25 12:27:34 -05:00
|
|
|
if (getPersistentHistory() != null) {
|
|
|
|
getPersistentHistory().write(history);
|
|
|
|
}
|
2009-05-17 13:14:03 -04:00
|
|
|
|
|
|
|
// clear session history
|
|
|
|
sessionHistory.clear();
|
|
|
|
} catch (IOException e) {
|
2009-07-18 18:06:32 -04:00
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to store history", e);
|
2009-05-17 13:14:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
public synchronized void commit() {
|
2009-05-17 13:14:03 -04:00
|
|
|
// check if session history is not empty
|
|
|
|
if (sessionHistory.sequences().size() > 0) {
|
|
|
|
commit(getCompleteHistory());
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:50:18 -04:00
|
|
|
|
2013-02-25 12:27:34 -05:00
|
|
|
public synchronized void setPersistentHistory(HistoryStorage persistentHistory) {
|
|
|
|
this.persistentHistory = persistentHistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public synchronized HistoryStorage getPersistentHistory() {
|
|
|
|
return persistentHistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static interface HistoryStorage {
|
|
|
|
|
|
|
|
History read() throws IOException;
|
|
|
|
|
|
|
|
|
|
|
|
void write(History history) throws IOException;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class HistoryFileStorage implements HistoryStorage {
|
|
|
|
|
|
|
|
private final File file;
|
|
|
|
|
|
|
|
|
|
|
|
public HistoryFileStorage(File file) {
|
|
|
|
this.file = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public History read() throws IOException {
|
|
|
|
if (file.exists()) {
|
|
|
|
return importHistory(file);
|
|
|
|
} else {
|
|
|
|
return new History();
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
2013-02-25 12:27:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(History history) throws IOException {
|
|
|
|
exportHistory(history, file);
|
|
|
|
}
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|
2013-02-25 12:27:34 -05:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
}
|