2010-09-22 19:04:10 +02:00
|
|
|
package com.google.refine;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
import java.io.Serializable;
|
2010-05-05 01:24:48 +02:00
|
|
|
import java.util.Date;
|
2010-08-06 07:04:25 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
2010-05-05 01:24:48 +02:00
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONWriter;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.preference.PreferenceStore;
|
|
|
|
import com.google.refine.preference.TopList;
|
|
|
|
import com.google.refine.util.JSONUtilities;
|
|
|
|
import com.google.refine.util.ParsingUtilities;
|
2010-05-05 01:24:48 +02:00
|
|
|
|
|
|
|
public class ProjectMetadata implements Jsonizable {
|
|
|
|
private final Date _created;
|
|
|
|
private Date _modified;
|
|
|
|
private String _name;
|
|
|
|
private String _password;
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
private String _encoding;
|
|
|
|
private int _encodingConfidence;
|
2010-06-17 04:15:26 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
private Map<String, Serializable> _customMetadata = new HashMap<String, Serializable>();
|
|
|
|
private PreferenceStore _preferenceStore = new PreferenceStore();
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
final Logger logger = LoggerFactory.getLogger("project_metadata");
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
protected ProjectMetadata(Date date) {
|
|
|
|
_created = date;
|
2010-06-17 04:15:26 +02:00
|
|
|
preparePreferenceStore(_preferenceStore);
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ProjectMetadata() {
|
|
|
|
_created = new Date();
|
|
|
|
_modified = _created;
|
2010-06-17 04:15:26 +02:00
|
|
|
preparePreferenceStore(_preferenceStore);
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
writer.object();
|
|
|
|
writer.key("name"); writer.value(_name);
|
|
|
|
writer.key("created"); writer.value(ParsingUtilities.dateToString(_created));
|
|
|
|
writer.key("modified"); writer.value(ParsingUtilities.dateToString(_modified));
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
if ("save".equals(options.getProperty("mode"))) {
|
|
|
|
writer.key("password"); writer.value(_password);
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
writer.key("encoding"); writer.value(_encoding);
|
|
|
|
writer.key("encodingConfidence"); writer.value(_encodingConfidence);
|
2010-08-06 07:04:25 +02:00
|
|
|
|
|
|
|
writer.key("customMetadata"); writer.object();
|
|
|
|
for (String key : _customMetadata.keySet()) {
|
|
|
|
Serializable value = _customMetadata.get(key);
|
|
|
|
writer.key(key);
|
|
|
|
writer.value(value);
|
|
|
|
}
|
|
|
|
writer.endObject();
|
|
|
|
|
2010-06-17 04:15:26 +02:00
|
|
|
writer.key("preferences"); _preferenceStore.write(writer, options);
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-08-06 07:04:25 +02:00
|
|
|
|
2010-06-15 22:55:38 +02:00
|
|
|
public void write(JSONWriter jsonWriter) throws Exception {
|
|
|
|
Properties options = new Properties();
|
|
|
|
options.setProperty("mode", "save");
|
|
|
|
|
|
|
|
write(jsonWriter, options);
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2010-06-17 04:15:26 +02:00
|
|
|
|
2010-06-15 22:55:38 +02:00
|
|
|
static public ProjectMetadata loadFromJSON(JSONObject obj) {
|
2010-05-05 01:24:48 +02:00
|
|
|
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
pm._modified = JSONUtilities.getDate(obj, "modified", new Date());
|
|
|
|
pm._name = JSONUtilities.getString(obj, "name", "<Error recovering project name>");
|
|
|
|
pm._password = JSONUtilities.getString(obj, "password", "");
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
pm._encoding = JSONUtilities.getString(obj, "encoding", "");
|
|
|
|
pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0);
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-06-17 04:15:26 +02:00
|
|
|
if (obj.has("preferences") && !obj.isNull("preferences")) {
|
|
|
|
try {
|
|
|
|
pm._preferenceStore.load(obj.getJSONObject("preferences"));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-07 22:25:31 +02:00
|
|
|
if (obj.has("expressions") && !obj.isNull("expressions")) { // backward compatibility
|
2010-06-17 04:15:26 +02:00
|
|
|
try {
|
2010-08-07 22:25:31 +02:00
|
|
|
((TopList) pm._preferenceStore.get("scripting.expressions"))
|
2010-06-17 04:15:26 +02:00
|
|
|
.load(obj.getJSONArray("expressions"));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
if (obj.has("customMetadata") && !obj.isNull("customMetadata")) {
|
|
|
|
try {
|
|
|
|
JSONObject obj2 = obj.getJSONObject("customMetadata");
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Iterator<String> keys = obj2.keys();
|
|
|
|
while (keys.hasNext()) {
|
|
|
|
String key = keys.next();
|
|
|
|
Object value = obj2.get(key);
|
|
|
|
if (value != null && value instanceof Serializable) {
|
|
|
|
pm._customMetadata.put(key, (Serializable) value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
return pm;
|
|
|
|
}
|
2010-06-17 04:15:26 +02:00
|
|
|
|
|
|
|
static protected void preparePreferenceStore(PreferenceStore ps) {
|
|
|
|
ProjectManager.preparePreferenceStore(ps);
|
|
|
|
// Any project specific preferences?
|
|
|
|
}
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
public Date getCreated() {
|
|
|
|
return _created;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
this._name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEncoding(String encoding) {
|
|
|
|
this._encoding = encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getEncoding() {
|
|
|
|
return _encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEncodingConfidence(int confidence) {
|
|
|
|
this._encodingConfidence = confidence;
|
|
|
|
}
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
public void setEncodingConfidence(String confidence) {
|
|
|
|
if (confidence != null) {
|
|
|
|
this.setEncodingConfidence(Integer.parseInt(confidence));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getEncodingConfidence() {
|
|
|
|
return _encodingConfidence;
|
|
|
|
}
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
public void setPassword(String password) {
|
|
|
|
this._password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
|
|
|
return _password;
|
|
|
|
}
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
public Date getModified() {
|
|
|
|
return _modified;
|
|
|
|
}
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-05-05 01:24:48 +02:00
|
|
|
public void updateModified() {
|
|
|
|
_modified = new Date();
|
|
|
|
}
|
2010-06-15 22:55:38 +02:00
|
|
|
|
2010-06-17 04:15:26 +02:00
|
|
|
public PreferenceStore getPreferenceStore() {
|
|
|
|
return _preferenceStore;
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|
2010-08-06 07:04:25 +02:00
|
|
|
|
|
|
|
public Serializable getCustomMetadata(String key) {
|
|
|
|
return _customMetadata.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCustomMetadata(String key, Serializable value) {
|
|
|
|
if (value == null) {
|
|
|
|
_customMetadata.remove(key);
|
|
|
|
} else {
|
|
|
|
_customMetadata.put(key, value);
|
|
|
|
}
|
|
|
|
}
|
2010-05-05 01:24:48 +02:00
|
|
|
}
|