Search in sources :

Example 1 with TopList

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);
    }
}
Also used : TopList(com.google.refine.preference.TopList) JSONWriter(org.json.JSONWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with TopList

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;
}
Also used : JSONTokener(org.json.JSONTokener) TopList(com.google.refine.preference.TopList) JSONObject(org.json.JSONObject) ProjectMetadata(com.google.refine.ProjectMetadata) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 3 with TopList

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);
    }
}
Also used : TopList(com.google.refine.preference.TopList) PreferenceStore(com.google.refine.preference.PreferenceStore) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 4 with TopList

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);
        }
    }
}
Also used : TopList(com.google.refine.preference.TopList) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 5 with TopList

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;
}
Also used : TopList(com.google.refine.preference.TopList) Serializable(java.io.Serializable) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Date(java.util.Date)

Aggregations

TopList (com.google.refine.preference.TopList)9 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)4 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 ProjectMetadata (com.google.refine.ProjectMetadata)1 PreferenceStore (com.google.refine.preference.PreferenceStore)1 File (java.io.File)1 FileReader (java.io.FileReader)1 Serializable (java.io.Serializable)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 JSONArray (org.json.JSONArray)1 JSONTokener (org.json.JSONTokener)1 JSONWriter (org.json.JSONWriter)1 Test (org.testng.annotations.Test)1