From e7d0fc5ed6770f68829b185a1a0309a32e2b33c0 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Thu, 17 Jun 2010 02:15:26 +0000 Subject: [PATCH] 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 --- .../com/metaweb/gridworks/ProjectManager.java | 20 +++-- .../metaweb/gridworks/ProjectMetadata.java | 55 ++++++++------ .../expr/GetExpressionHistoryCommand.java | 15 +++- .../commands/expr/LogExpressionCommand.java | 8 +- .../gridworks/io/FileProjectManager.java | 32 +++++--- .../gridworks/preference/PreferenceStore.java | 74 +++++++++++++++++++ .../metaweb/gridworks/preference/TopList.java | 72 ++++++++++++++++++ 7 files changed, 231 insertions(+), 45 deletions(-) create mode 100644 main/src/com/metaweb/gridworks/preference/PreferenceStore.java create mode 100644 main/src/com/metaweb/gridworks/preference/TopList.java diff --git a/main/src/com/metaweb/gridworks/ProjectManager.java b/main/src/com/metaweb/gridworks/ProjectManager.java index 224239308..cb7130ed8 100644 --- a/main/src/com/metaweb/gridworks/ProjectManager.java +++ b/main/src/com/metaweb/gridworks/ProjectManager.java @@ -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 _projectsMetadata; - protected List _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 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)); + } } diff --git a/main/src/com/metaweb/gridworks/ProjectMetadata.java b/main/src/com/metaweb/gridworks/ProjectMetadata.java index acaf97ddf..6716c176e 100644 --- a/main/src/com/metaweb/gridworks/ProjectMetadata.java +++ b/main/src/com/metaweb/gridworks/ProjectMetadata.java @@ -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 _expressions = new LinkedList(); + + 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 getExpressions() { - return _expressions; + public PreferenceStore getPreferenceStore() { + return _preferenceStore; } } diff --git a/main/src/com/metaweb/gridworks/commands/expr/GetExpressionHistoryCommand.java b/main/src/com/metaweb/gridworks/commands/expr/GetExpressionHistoryCommand.java index 8f7e38343..974585977 100644 --- a/main/src/com/metaweb/gridworks/commands/expr/GetExpressionHistoryCommand.java +++ b/main/src/com/metaweb/gridworks/commands/expr/GetExpressionHistoryCommand.java @@ -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 localExpressions = pm.getExpressions(); - List globalExpressions = ProjectManager.singleton.getExpressions(); + List localExpressions = + ((TopList) project.getMetadata().getPreferenceStore().get("expressions")) + .getList(); + + localExpressions = localExpressions.size() > 20 ? localExpressions.subList(0, 20) : localExpressions; + + List globalExpressions = + ((TopList) ProjectManager.singleton.getPreferenceStore().get("expressions")) + .getList(); + Set done = new HashSet(); response.setCharacterEncoding("UTF-8"); diff --git a/main/src/com/metaweb/gridworks/commands/expr/LogExpressionCommand.java b/main/src/com/metaweb/gridworks/commands/expr/LogExpressionCommand.java index 8307e01e1..924cfb7dd 100644 --- a/main/src/com/metaweb/gridworks/commands/expr/LogExpressionCommand.java +++ b/main/src/com/metaweb/gridworks/commands/expr/LogExpressionCommand.java @@ -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"); diff --git a/main/src/com/metaweb/gridworks/io/FileProjectManager.java b/main/src/com/metaweb/gridworks/io/FileProjectManager.java index 75e6f4d01..9c1913406 100644 --- a/main/src/com/metaweb/gridworks/io/FileProjectManager.java +++ b/main/src/com/metaweb/gridworks/io/FileProjectManager.java @@ -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(); - _expressions = new LinkedList(); + _preferenceStore = new PreferenceStore(); _projects = new HashMap(); - + + 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); diff --git a/main/src/com/metaweb/gridworks/preference/PreferenceStore.java b/main/src/com/metaweb/gridworks/preference/PreferenceStore.java new file mode 100644 index 000000000..c5a82ce64 --- /dev/null +++ b/main/src/com/metaweb/gridworks/preference/PreferenceStore.java @@ -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 _prefs = new HashMap(); + + 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 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); + } + } + } + } +} diff --git a/main/src/com/metaweb/gridworks/preference/TopList.java b/main/src/com/metaweb/gridworks/preference/TopList.java new file mode 100644 index 000000000..fa02d8e54 --- /dev/null +++ b/main/src/com/metaweb/gridworks/preference/TopList.java @@ -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 _list = new ArrayList(); + + public TopList(int top) { + _top = top; + } + + @SuppressWarnings("unchecked") + public List getList() { + return (List) 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)); + } + } +}