2010-06-16 00:11:35 +02:00
|
|
|
package com.metaweb.gridworks.io;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
import java.io.Writer;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONWriter;
|
|
|
|
|
|
|
|
import com.metaweb.gridworks.ProjectManager;
|
|
|
|
import com.metaweb.gridworks.history.History;
|
|
|
|
import com.metaweb.gridworks.history.HistoryEntry;
|
2010-06-16 14:35:37 +02:00
|
|
|
import com.metaweb.gridworks.history.HistoryEntryManager;
|
2010-06-16 00:11:35 +02:00
|
|
|
import com.metaweb.gridworks.util.Pool;
|
|
|
|
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
public class FileHistoryEntry implements HistoryEntryManager{
|
2010-06-16 00:11:35 +02:00
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
public void delete(HistoryEntry historyEntry) {
|
|
|
|
File file = getChangeFile(historyEntry);
|
2010-06-16 00:11:35 +02:00
|
|
|
if (file.exists()) {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
public void save(HistoryEntry historyEntry, Writer writer, Properties options) {
|
2010-06-16 00:11:35 +02:00
|
|
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
|
|
|
try {
|
2010-06-16 14:35:37 +02:00
|
|
|
historyEntry.write(jsonWriter, options);
|
2010-06-16 00:11:35 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
public void loadChange(HistoryEntry historyEntry) {
|
|
|
|
File changeFile = getChangeFile(historyEntry);
|
2010-06-16 00:11:35 +02:00
|
|
|
|
|
|
|
try {
|
2010-06-16 14:35:37 +02:00
|
|
|
loadChange(historyEntry, changeFile);
|
2010-06-16 00:11:35 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException("Failed to load change file " + changeFile.getAbsolutePath(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
protected void loadChange(HistoryEntry historyEntry, File file) throws Exception {
|
2010-06-16 00:11:35 +02:00
|
|
|
ZipFile zipFile = new ZipFile(file);
|
|
|
|
try {
|
|
|
|
Pool pool = new Pool();
|
|
|
|
ZipEntry poolEntry = zipFile.getEntry("pool.txt");
|
|
|
|
if (poolEntry != null) {
|
|
|
|
pool.load(new InputStreamReader(
|
|
|
|
zipFile.getInputStream(poolEntry)));
|
|
|
|
} // else, it's a legacy project file
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
historyEntry.setChange(History.readOneChange(
|
|
|
|
zipFile.getInputStream(zipFile.getEntry("change.txt")), pool));
|
2010-06-16 00:11:35 +02:00
|
|
|
} finally {
|
|
|
|
zipFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
public void saveChange(HistoryEntry historyEntry) throws Exception {
|
|
|
|
File changeFile = getChangeFile(historyEntry);
|
2010-06-16 00:11:35 +02:00
|
|
|
if (!(changeFile.exists())) {
|
2010-06-16 14:35:37 +02:00
|
|
|
saveChange(historyEntry, changeFile);
|
2010-06-16 00:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
protected void saveChange(HistoryEntry historyEntry, File file) throws Exception {
|
2010-06-16 00:11:35 +02:00
|
|
|
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
|
|
|
|
try {
|
|
|
|
Pool pool = new Pool();
|
|
|
|
|
|
|
|
out.putNextEntry(new ZipEntry("change.txt"));
|
|
|
|
try {
|
2010-06-16 14:35:37 +02:00
|
|
|
History.writeOneChange(out, historyEntry.getChange(), pool);
|
2010-06-16 00:11:35 +02:00
|
|
|
} finally {
|
|
|
|
out.closeEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
out.putNextEntry(new ZipEntry("pool.txt"));
|
|
|
|
try {
|
|
|
|
pool.save(out);
|
|
|
|
} finally {
|
|
|
|
out.closeEntry();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
protected File getChangeFile(HistoryEntry historyEntry) {
|
|
|
|
return new File(getHistoryDir(historyEntry), historyEntry.id + ".change.zip");
|
2010-06-16 00:11:35 +02:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:35:37 +02:00
|
|
|
protected File getHistoryDir(HistoryEntry historyEntry) {
|
|
|
|
File dir = new File(((FileProjectManager)ProjectManager.singleton)
|
|
|
|
.getProjectDir(historyEntry.projectID),
|
|
|
|
"history");
|
2010-06-16 00:11:35 +02:00
|
|
|
dir.mkdirs();
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
}
|