Implemented a generic preference store for both the whole workspace and each project.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@988 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
0fedad047b
commit
e7d0fc5ed6
@ -12,6 +12,8 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.metaweb.gridworks.history.HistoryEntryManager;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.preference.PreferenceStore;
|
||||
import com.metaweb.gridworks.preference.TopList;
|
||||
|
||||
|
||||
public abstract class ProjectManager {
|
||||
@ -19,7 +21,7 @@ public abstract class ProjectManager {
|
||||
static protected final int s_expressionHistoryMax = 100;
|
||||
|
||||
protected Map<Long, ProjectMetadata> _projectsMetadata;
|
||||
protected List<String> _expressions;
|
||||
protected PreferenceStore _preferenceStore;
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger("project_manager");
|
||||
|
||||
@ -100,19 +102,19 @@ public abstract class ProjectManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PreferenceStore getPreferenceStore() {
|
||||
return _preferenceStore;
|
||||
}
|
||||
|
||||
public void addLatestExpression(String s) {
|
||||
synchronized (this) {
|
||||
_expressions.remove(s);
|
||||
_expressions.add(0, s);
|
||||
while (_expressions.size() > s_expressionHistoryMax) {
|
||||
_expressions.remove(_expressions.size() - 1);
|
||||
}
|
||||
((TopList) _preferenceStore.get("expressions")).add(s);
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getExpressions() {
|
||||
return _expressions;
|
||||
return ((TopList) _preferenceStore.get("expressions")).getList();
|
||||
}
|
||||
|
||||
public abstract void save(boolean b);
|
||||
@ -124,4 +126,8 @@ public abstract class ProjectManager {
|
||||
public abstract void deleteProject(long projectID) ;
|
||||
|
||||
public abstract HistoryEntryManager getHistoryEntryManager();
|
||||
|
||||
static protected void preparePreferenceStore(PreferenceStore ps) {
|
||||
ps.put("expressions", new TopList(s_expressionHistoryMax));
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.metaweb.gridworks;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -11,12 +9,12 @@ import org.json.JSONWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.metaweb.gridworks.preference.PreferenceStore;
|
||||
import com.metaweb.gridworks.preference.TopList;
|
||||
import com.metaweb.gridworks.util.JSONUtilities;
|
||||
import com.metaweb.gridworks.util.ParsingUtilities;
|
||||
|
||||
public class ProjectMetadata implements Jsonizable {
|
||||
private static final int s_expressionHistoryMax = 20; // last n expressions used in this project
|
||||
|
||||
private final Date _created;
|
||||
private Date _modified;
|
||||
private String _name;
|
||||
@ -24,17 +22,20 @@ public class ProjectMetadata implements Jsonizable {
|
||||
|
||||
private String _encoding;
|
||||
private int _encodingConfidence;
|
||||
private List<String> _expressions = new LinkedList<String>();
|
||||
|
||||
private PreferenceStore _preferenceStore = new PreferenceStore();
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger("project_metadata");
|
||||
|
||||
protected ProjectMetadata(Date date) {
|
||||
_created = date;
|
||||
preparePreferenceStore(_preferenceStore);
|
||||
}
|
||||
|
||||
public ProjectMetadata() {
|
||||
_created = new Date();
|
||||
_modified = _created;
|
||||
preparePreferenceStore(_preferenceStore);
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
@ -50,7 +51,7 @@ public class ProjectMetadata implements Jsonizable {
|
||||
|
||||
writer.key("encoding"); writer.value(_encoding);
|
||||
writer.key("encodingConfidence"); writer.value(_encodingConfidence);
|
||||
writer.key("expressions"); JSONUtilities.writeStringList(writer, _expressions);
|
||||
writer.key("preferences"); _preferenceStore.write(writer, options);
|
||||
}
|
||||
writer.endObject();
|
||||
}
|
||||
@ -63,9 +64,7 @@ public class ProjectMetadata implements Jsonizable {
|
||||
|
||||
write(jsonWriter, options);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static public ProjectMetadata loadFromJSON(JSONObject obj) {
|
||||
ProjectMetadata pm = new ProjectMetadata(JSONUtilities.getDate(obj, "modified", new Date()));
|
||||
|
||||
@ -76,10 +75,30 @@ public class ProjectMetadata implements Jsonizable {
|
||||
pm._encoding = JSONUtilities.getString(obj, "encoding", "");
|
||||
pm._encodingConfidence = JSONUtilities.getInt(obj, "encodingConfidence", 0);
|
||||
|
||||
JSONUtilities.getStringList(obj, "expressions", pm._expressions);
|
||||
|
||||
if (obj.has("preferences") && !obj.isNull("preferences")) {
|
||||
try {
|
||||
pm._preferenceStore.load(obj.getJSONObject("preferences"));
|
||||
} catch (JSONException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (obj.has("expressions") && !obj.isNull("expressions")) {
|
||||
try {
|
||||
((TopList) pm._preferenceStore.get("expressions"))
|
||||
.load(obj.getJSONArray("expressions"));
|
||||
} catch (JSONException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
return pm;
|
||||
}
|
||||
|
||||
static protected void preparePreferenceStore(PreferenceStore ps) {
|
||||
ProjectManager.preparePreferenceStore(ps);
|
||||
// Any project specific preferences?
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return _created;
|
||||
@ -131,17 +150,7 @@ public class ProjectMetadata implements Jsonizable {
|
||||
_modified = new Date();
|
||||
}
|
||||
|
||||
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;
|
||||
public PreferenceStore getPreferenceStore() {
|
||||
return _preferenceStore;
|
||||
}
|
||||
}
|
||||
|
@ -12,9 +12,9 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.ProjectManager;
|
||||
import com.metaweb.gridworks.ProjectMetadata;
|
||||
import com.metaweb.gridworks.commands.Command;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.preference.TopList;
|
||||
|
||||
public class GetExpressionHistoryCommand extends Command {
|
||||
|
||||
@ -24,10 +24,17 @@ public class GetExpressionHistoryCommand extends Command {
|
||||
|
||||
try {
|
||||
Project project = getProject(request);
|
||||
ProjectMetadata pm = ProjectManager.singleton.getProjectMetadata(project.id);
|
||||
|
||||
List<String> localExpressions = pm.getExpressions();
|
||||
List<String> globalExpressions = ProjectManager.singleton.getExpressions();
|
||||
List<String> localExpressions =
|
||||
((TopList) project.getMetadata().getPreferenceStore().get("expressions"))
|
||||
.getList();
|
||||
|
||||
localExpressions = localExpressions.size() > 20 ? localExpressions.subList(0, 20) : localExpressions;
|
||||
|
||||
List<String> globalExpressions =
|
||||
((TopList) ProjectManager.singleton.getPreferenceStore().get("expressions"))
|
||||
.getList();
|
||||
|
||||
Set<String> done = new HashSet<String>();
|
||||
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
|
@ -6,8 +6,10 @@ import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.metaweb.gridworks.ProjectManager;
|
||||
import com.metaweb.gridworks.commands.Command;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.preference.TopList;
|
||||
|
||||
public class LogExpressionCommand extends Command {
|
||||
|
||||
@ -19,7 +21,11 @@ public class LogExpressionCommand extends Command {
|
||||
Project project = getProject(request);
|
||||
String expression = request.getParameter("expression");
|
||||
|
||||
project.getMetadata().addLatestExpression(expression);
|
||||
((TopList) project.getMetadata().getPreferenceStore().get("expressions"))
|
||||
.add(expression);
|
||||
|
||||
((TopList) ProjectManager.singleton.getPreferenceStore().get("expressions"))
|
||||
.add(expression);
|
||||
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
|
@ -13,8 +13,8 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import org.apache.tools.tar.TarEntry;
|
||||
@ -32,9 +32,10 @@ import com.metaweb.gridworks.ProjectManager;
|
||||
import com.metaweb.gridworks.ProjectMetadata;
|
||||
import com.metaweb.gridworks.history.HistoryEntryManager;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.util.JSONUtilities;
|
||||
import com.metaweb.gridworks.preference.PreferenceStore;
|
||||
import com.metaweb.gridworks.preference.TopList;
|
||||
|
||||
public class FileProjectManager extends ProjectManager{
|
||||
public class FileProjectManager extends ProjectManager {
|
||||
|
||||
protected File _workspaceDir;
|
||||
|
||||
@ -53,12 +54,14 @@ public class FileProjectManager extends ProjectManager{
|
||||
_workspaceDir.mkdirs();
|
||||
|
||||
_projectsMetadata = new HashMap<Long, ProjectMetadata>();
|
||||
_expressions = new LinkedList<String>();
|
||||
_preferenceStore = new PreferenceStore();
|
||||
_projects = new HashMap<Long, Project>();
|
||||
|
||||
|
||||
preparePreferenceStore(_preferenceStore);
|
||||
|
||||
load();
|
||||
}
|
||||
|
||||
|
||||
public File getWorkspaceDir() {
|
||||
return _workspaceDir;
|
||||
}
|
||||
@ -281,7 +284,9 @@ public class FileProjectManager extends ProjectManager{
|
||||
jsonWriter.endArray();
|
||||
writer.write('\n');
|
||||
|
||||
jsonWriter.key("expressions"); JSONUtilities.writeStringList(jsonWriter, _expressions);
|
||||
jsonWriter.key("preferences");
|
||||
_preferenceStore.write(jsonWriter, new Properties());
|
||||
|
||||
jsonWriter.endObject();
|
||||
} finally {
|
||||
writer.close();
|
||||
@ -405,7 +410,6 @@ public class FileProjectManager extends ProjectManager{
|
||||
logger.info("Loading workspace: {}", file.getAbsolutePath());
|
||||
|
||||
_projectsMetadata.clear();
|
||||
_expressions.clear();
|
||||
|
||||
boolean found = false;
|
||||
|
||||
@ -426,8 +430,16 @@ public class FileProjectManager extends ProjectManager{
|
||||
|
||||
_projectsMetadata.put(id, metadata);
|
||||
}
|
||||
|
||||
JSONUtilities.getStringList(obj, "expressions", _expressions);
|
||||
|
||||
if (obj.has("preferences") && !obj.isNull("preferences")) {
|
||||
_preferenceStore.load(obj.getJSONObject("preferences"));
|
||||
}
|
||||
|
||||
if (obj.has("expressions") && !obj.isNull("expressions")) {
|
||||
((TopList) _preferenceStore.get("expressions"))
|
||||
.load(obj.getJSONArray("expressions"));
|
||||
}
|
||||
|
||||
found = true;
|
||||
} catch (JSONException e) {
|
||||
logger.warn("Error reading file", e);
|
||||
|
@ -0,0 +1,74 @@
|
||||
package com.metaweb.gridworks.preference;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.Jsonizable;
|
||||
|
||||
public class PreferenceStore implements Jsonizable {
|
||||
protected Map<String, Object> _prefs = new HashMap<String, Object>();
|
||||
|
||||
public void put(String key, Object value) {
|
||||
_prefs.put(key, value);
|
||||
}
|
||||
|
||||
public Object get(String key) {
|
||||
return _prefs.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JSONWriter writer, Properties options) throws JSONException {
|
||||
writer.object();
|
||||
|
||||
writer.key("entries");
|
||||
writer.object();
|
||||
for (String k : _prefs.keySet()) {
|
||||
writer.key(k);
|
||||
|
||||
Object o = _prefs.get(k);
|
||||
if (o instanceof Jsonizable) {
|
||||
((Jsonizable) o).write(writer, options);
|
||||
} else {
|
||||
writer.value(o);
|
||||
}
|
||||
}
|
||||
writer.endObject();
|
||||
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void load(JSONObject obj) throws JSONException {
|
||||
if (obj.has("entries") && !obj.isNull("entries")) {
|
||||
JSONObject entries = obj.getJSONObject("entries");
|
||||
|
||||
Iterator<String> i = entries.keys();
|
||||
while (i.hasNext()) {
|
||||
String key = i.next();
|
||||
Object o = entries.get(key);
|
||||
if (o instanceof JSONObject) {
|
||||
try {
|
||||
JSONObject obj2 = (JSONObject) o;
|
||||
String className = obj2.getString("class");
|
||||
Class klass = Class.forName(className);
|
||||
Method method = klass.getMethod("load", JSONObject.class);
|
||||
|
||||
_prefs.put(key, method.invoke(null, obj2));
|
||||
} catch (Exception e) {
|
||||
//
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
_prefs.put(key, o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
72
main/src/com/metaweb/gridworks/preference/TopList.java
Normal file
72
main/src/com/metaweb/gridworks/preference/TopList.java
Normal file
@ -0,0 +1,72 @@
|
||||
package com.metaweb.gridworks.preference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.collections.list.UnmodifiableList;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.Jsonizable;
|
||||
|
||||
|
||||
public class TopList implements Jsonizable {
|
||||
private static final long serialVersionUID = 2666669643063493350L;
|
||||
|
||||
final protected int _top;
|
||||
final protected List<String> _list = new ArrayList<String>();
|
||||
|
||||
public TopList(int top) {
|
||||
_top = top;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getList() {
|
||||
return (List<String>) UnmodifiableList.decorate(_list);
|
||||
}
|
||||
|
||||
public void add(String element) {
|
||||
_list.remove(element);
|
||||
_list.add(0, element);
|
||||
while (_list.size() > _top) {
|
||||
_list.remove(_list.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JSONWriter writer, Properties options) throws JSONException {
|
||||
writer.object();
|
||||
writer.key("class"); writer.value(this.getClass().getName());
|
||||
|
||||
writer.key("top"); writer.value(_top);
|
||||
writer.key("list");
|
||||
writer.array();
|
||||
for (String element : _list) {
|
||||
writer.value(element);
|
||||
}
|
||||
writer.endArray();
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
static public TopList load(JSONObject obj) throws JSONException {
|
||||
int top = obj.has("top") && !obj.isNull("top") ? obj.getInt("top") : 10;
|
||||
TopList tl = new TopList(top);
|
||||
|
||||
if (obj.has("list") && !obj.isNull("list")) {
|
||||
JSONArray a = obj.getJSONArray("list");
|
||||
|
||||
tl.load(a);
|
||||
}
|
||||
return tl;
|
||||
}
|
||||
|
||||
public void load(JSONArray a) throws JSONException {
|
||||
int length = a.length();
|
||||
for (int i = 0; i < length && _list.size() < _top; i++) {
|
||||
_list.add(a.getString(i));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user