use of com.google.refine.preference.TopList in project OpenRefine by OpenRefine.
the class GetStarredExpressionsCommand method doGet.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<String> starredExpressions = ((TopList) ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions")).getList();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("expressions");
writer.array();
for (String s : starredExpressions) {
writer.object();
writer.key("code");
writer.value(s);
writer.endObject();
}
writer.endArray();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
use of com.google.refine.preference.TopList in project OpenRefine by OpenRefine.
the class FileProjectManager method loadFromFile.
protected boolean loadFromFile(File file) {
logger.info("Loading workspace: {}", file.getAbsolutePath());
_projectsMetadata.clear();
boolean found = false;
if (file.exists() || file.canRead()) {
FileReader reader = null;
try {
reader = new FileReader(file);
JSONTokener tokener = new JSONTokener(reader);
JSONObject obj = (JSONObject) tokener.nextValue();
JSONArray a = obj.getJSONArray("projectIDs");
int count = a.length();
for (int i = 0; i < count; i++) {
long id = a.getLong(i);
File projectDir = getProjectDir(id);
ProjectMetadata metadata = ProjectMetadataUtilities.load(projectDir);
_projectsMetadata.put(id, metadata);
}
if (obj.has("preferences") && !obj.isNull("preferences")) {
_preferenceStore.load(obj.getJSONObject("preferences"));
}
if (obj.has("expressions") && !obj.isNull("expressions")) {
// backward compatibility
((TopList) _preferenceStore.get("scripting.expressions")).load(obj.getJSONArray("expressions"));
}
found = true;
} catch (JSONException e) {
logger.warn("Error reading file", e);
} catch (IOException e) {
logger.warn("Error reading file", e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
logger.warn("Exception closing file", e);
}
}
}
return found;
}
use of com.google.refine.preference.TopList in project OpenRefine by OpenRefine.
the class LogExpressionCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
try {
String expression = request.getParameter("expression");
PreferenceStore prefStore = ProjectManager.singleton.getPreferenceStore();
TopList topList = (TopList) prefStore.get("scripting.expressions");
if (topList == null) {
topList = new TopList(ProjectManager.EXPRESSION_HISTORY_MAX);
prefStore.put("scripting.expressions", topList);
}
topList.add(expression);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
response.getWriter().write("{ \"code\" : \"ok\" }");
} catch (Exception e) {
respondException(response, e);
}
}
use of com.google.refine.preference.TopList in project OpenRefine by OpenRefine.
the class ToggleStarredExpressionCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
String expression = request.getParameter("expression");
TopList starredExpressions = ((TopList) ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions"));
if (starredExpressions.getList().contains(expression)) {
starredExpressions.remove(expression);
} else {
starredExpressions.add(expression);
}
if (request.getParameter("returnList") != null) {
try {
respondJSON(response, GetStarredExpressionsCommand.getExpressionsList());
} catch (Exception e) {
respondException(response, e);
}
}
}
use of com.google.refine.preference.TopList in project OpenRefine by OpenRefine.
the class ProjectMetadata method loadFromJSON.
public static ProjectMetadata loadFromJSON(JSONObject obj) {
// TODO: Is this correct? It's using modified date for creation date
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);
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")) {
// backward compatibility
try {
((TopList) pm._preferenceStore.get("scripting.expressions")).load(obj.getJSONArray("expressions"));
} catch (JSONException e) {
// ignore
}
}
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
}
}
// Mark it as not needing writing until modified
pm.written = new Date();
return pm;
}
Aggregations