2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-03-07 23:37:26 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.io.Writer;
|
2010-02-01 20:16:09 +01:00
|
|
|
import java.util.Date;
|
2010-03-03 22:21:38 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2010-02-01 20:16:09 +01:00
|
|
|
import java.util.Properties;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-01-26 06:17:14 +01:00
|
|
|
import org.json.JSONException;
|
2010-03-04 20:15:46 +01:00
|
|
|
import org.json.JSONObject;
|
2010-03-07 23:37:26 +01:00
|
|
|
import org.json.JSONTokener;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-04-23 10:25:52 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2010-01-26 06:17:14 +01:00
|
|
|
|
2010-03-04 20:15:46 +01:00
|
|
|
import com.metaweb.gridworks.util.JSONUtilities;
|
|
|
|
import com.metaweb.gridworks.util.ParsingUtilities;
|
|
|
|
|
|
|
|
public class ProjectMetadata implements Jsonizable {
|
2010-03-03 22:21:38 +01:00
|
|
|
private static final int s_expressionHistoryMax = 20; // last n expressions used in this project
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-03-04 20:15:46 +01:00
|
|
|
private final Date _created;
|
|
|
|
private Date _modified;
|
2010-03-03 05:19:58 +01:00
|
|
|
private String _name;
|
|
|
|
private String _password;
|
2010-03-04 20:15:46 +01:00
|
|
|
|
2010-03-03 22:21:38 +01:00
|
|
|
private String _encoding;
|
|
|
|
private int _encodingConfidence;
|
|
|
|
private List<String> _expressions = new LinkedList<String>();
|
2010-03-03 05:19:58 +01:00
|
|
|
|
2010-04-23 10:25:52 +02:00
|
|
|
final Logger logger = LoggerFactory.getLogger("project_metadata");
|
|
|
|
|
2010-03-04 20:15:46 +01:00
|
|
|
protected ProjectMetadata(Date date) {
|
|
|
|
_created = date;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ProjectMetadata() {
|
|
|
|
_created = new Date();
|
|
|
|
_modified = _created;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
|
|
|
writer.key("name"); writer.value(_name);
|
|
|
|
writer.key("created"); writer.value(ParsingUtilities.dateToString(_created));
|
|
|
|
writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified));
|
|
|
|
|
|
|
|
if ("save".equals(options.getProperty("mode"))) {
|
|
|
|
writer.key("password"); writer.value(_password);
|
|
|
|
|
|
|
|
writer.key("encoding"); writer.value(_encoding);
|
|
|
|
writer.key("encodingConfidence"); writer.value(_encodingConfidence);
|
|
|
|
writer.key("expressions"); JSONUtilities.writeStringList(writer, _expressions);
|
|
|
|
}
|
|
|
|
writer.endObject();
|
|
|
|
}
|
|
|
|
|
2010-03-21 00:56:28 +01:00
|
|
|
public void save(File projectDir) throws Exception {
|
|
|
|
File tempFile = new File(projectDir, "metadata.temp.json");
|
2010-03-07 23:37:26 +01:00
|
|
|
try {
|
|
|
|
saveToFile(tempFile);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
2010-04-23 10:25:52 +02:00
|
|
|
logger.warn("Failed to save project metadata");
|
2010-03-07 23:37:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-21 00:56:28 +01:00
|
|
|
File file = new File(projectDir, "metadata.json");
|
|
|
|
File oldFile = new File(projectDir, "metadata.old.json");
|
2010-03-07 23:37:26 +01:00
|
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
file.renameTo(oldFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
tempFile.renameTo(file);
|
|
|
|
if (oldFile.exists()) {
|
|
|
|
oldFile.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 00:56:28 +01:00
|
|
|
protected void saveToFile(File metadataFile) throws Exception {
|
|
|
|
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
|
2010-03-07 23:37:26 +01:00
|
|
|
try {
|
|
|
|
Properties options = new Properties();
|
|
|
|
options.setProperty("mode", "save");
|
|
|
|
|
|
|
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
|
|
|
|
|
|
|
write(jsonWriter, options);
|
|
|
|
} finally {
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 00:56:28 +01:00
|
|
|
static public ProjectMetadata load(File projectDir) {
|
2010-03-07 23:37:26 +01:00
|
|
|
try {
|
2010-03-21 00:56:28 +01:00
|
|
|
return loadFromFile(new File(projectDir, "metadata.json"));
|
2010-03-07 23:37:26 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2010-03-21 00:56:28 +01:00
|
|
|
return loadFromFile(new File(projectDir, "metadata.temp.json"));
|
2010-03-07 23:37:26 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2010-03-21 00:56:28 +01:00
|
|
|
return loadFromFile(new File(projectDir, "metadata.old.json"));
|
2010-03-07 23:37:26 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-03-21 00:56:28 +01:00
|
|
|
static protected ProjectMetadata loadFromFile(File metadataFile) throws Exception {
|
|
|
|
FileReader reader = new FileReader(metadataFile);
|
2010-03-07 23:37:26 +01:00
|
|
|
try {
|
|
|
|
JSONTokener tokener = new JSONTokener(reader);
|
|
|
|
JSONObject obj = (JSONObject) tokener.nextValue();
|
|
|
|
|
|
|
|
return loadFromJSON(obj);
|
|
|
|
} finally {
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static protected ProjectMetadata loadFromJSON(JSONObject obj) {
|
|
|
|
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
|
|
|
|
|
|
|
|
pm._modified = JSONUtilities.getDate(obj, "modified", new Date());
|
|
|
|
pm._name = JSONUtilities.getString(obj, "name", "<Error recovering project name>");
|
|
|
|
pm._password = JSONUtilities.getString(obj, "password", "");
|
|
|
|
|
|
|
|
pm._encoding = JSONUtilities.getString(obj, "encoding", "");
|
|
|
|
pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0);
|
|
|
|
|
|
|
|
JSONUtilities.getStringList(obj, "expressions", pm._expressions);
|
|
|
|
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public Date getCreated() {
|
|
|
|
return _created;
|
|
|
|
}
|
2010-02-03 22:57:38 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void setName(String name) {
|
|
|
|
this._name = name;
|
|
|
|
}
|
2010-02-03 22:57:38 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public String getName() {
|
|
|
|
return _name;
|
|
|
|
}
|
2010-02-03 22:57:38 +01:00
|
|
|
|
2010-03-01 05:56:16 +01:00
|
|
|
public void setEncoding(String encoding) {
|
|
|
|
this._encoding = encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getEncoding() {
|
|
|
|
return _encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEncodingConfidence(int confidence) {
|
|
|
|
this._encodingConfidence = confidence;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEncodingConfidence(String confidence) {
|
2010-03-09 21:57:46 +01:00
|
|
|
if (confidence != null) {
|
|
|
|
this.setEncodingConfidence(Integer.parseInt(confidence));
|
|
|
|
}
|
2010-03-01 05:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getEncodingConfidence() {
|
|
|
|
return _encodingConfidence;
|
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void setPassword(String password) {
|
|
|
|
this._password = password;
|
|
|
|
}
|
2010-02-03 22:57:38 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public String getPassword() {
|
|
|
|
return _password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Date getModified() {
|
|
|
|
return _modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateModified() {
|
|
|
|
_modified = new Date();
|
|
|
|
}
|
2010-03-03 22:21:38 +01:00
|
|
|
|
|
|
|
public void addLatestExpression(String s) {
|
|
|
|
_expressions.remove(s);
|
|
|
|
_expressions.add(0, s);
|
|
|
|
while (_expressions.size() > s_expressionHistoryMax) {
|
|
|
|
_expressions.remove(_expressions.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectManager.singleton.addLatestExpression(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getExpressions() {
|
|
|
|
return _expressions;
|
|
|
|
}
|
2010-01-25 23:51:25 +01:00
|
|
|
}
|