Search in sources :

Example 1 with PreferenceStore

use of com.google.refine.preference.PreferenceStore in project OpenRefine by OpenRefine.

the class GetPreferenceCommand method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Project project = request.getParameter("project") != null ? getProject(request) : null;
    PreferenceStore ps = project != null ? project.getMetadata().getPreferenceStore() : ProjectManager.singleton.getPreferenceStore();
    String prefName = request.getParameter("name");
    Object pref = ps.get(prefName);
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.object();
        writer.key("value");
        if (pref == null || pref instanceof String || pref instanceof Number || pref instanceof Boolean) {
            writer.value(pref);
        } else if (pref instanceof TopList) {
            TopList tl = (TopList) pref;
            tl.write(writer, new Properties());
        } else {
            writer.value(pref.toString());
        }
        writer.endObject();
    } catch (JSONException e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) TopList(com.google.refine.preference.TopList) Project(com.google.refine.model.Project) JSONException(org.json.JSONException) Properties(java.util.Properties) PreferenceStore(com.google.refine.preference.PreferenceStore)

Example 2 with PreferenceStore

use of com.google.refine.preference.PreferenceStore in project OpenRefine by OpenRefine.

the class LoadLanguageCommand method doPost.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String modname = request.getParameter("module");
    if (modname == null) {
        modname = "core";
    }
    String[] langs = request.getParameterValues("lang");
    if (langs == null || "".equals(langs[0])) {
        PreferenceStore ps = ProjectManager.singleton.getPreferenceStore();
        if (ps != null) {
            langs = new String[] { (String) ps.get("userLang") };
        }
    }
    // TODO: Switch this to just use English as the default language so we
    // so we don't have to maintain a separate redundant file.
    langs = Arrays.copyOf(langs, langs.length + 1);
    langs[langs.length - 1] = "default";
    JSONObject json = null;
    boolean loaded = false;
    for (String lang : langs) {
        json = loadLanguage(this.servlet, modname, lang);
        if (json != null) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            try {
                json.write(response.getWriter());
            } catch (JSONException e) {
                logger.error("Error writing language labels to response stream");
            }
            response.getWriter().flush();
            response.getWriter().close();
            loaded = true;
            break;
        }
    }
    if (!loaded) {
        logger.error("Failed to load any language files");
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) PreferenceStore(com.google.refine.preference.PreferenceStore)

Example 3 with PreferenceStore

use of com.google.refine.preference.PreferenceStore in project OpenRefine by OpenRefine.

the class GetAllPreferencesCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Project project = request.getParameter("project") != null ? getProject(request) : null;
    PreferenceStore ps = project != null ? project.getMetadata().getPreferenceStore() : ProjectManager.singleton.getPreferenceStore();
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.object();
        for (String key : ps.getKeys()) {
            Object pref = ps.get(key);
            if (pref == null || pref instanceof String || pref instanceof Number || pref instanceof Boolean) {
                writer.key(key);
                writer.value(pref);
            }
        }
        writer.endObject();
    } catch (JSONException e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) Project(com.google.refine.model.Project) JSONException(org.json.JSONException) PreferenceStore(com.google.refine.preference.PreferenceStore)

Example 4 with PreferenceStore

use of com.google.refine.preference.PreferenceStore in project OpenRefine by OpenRefine.

the class SetPreferenceCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Project project = request.getParameter("project") != null ? getProject(request) : null;
    PreferenceStore ps = project != null ? project.getMetadata().getPreferenceStore() : ProjectManager.singleton.getPreferenceStore();
    String prefName = request.getParameter("name");
    String valueString = request.getParameter("value");
    try {
        Object o = valueString == null ? null : new JSONTokener(valueString).nextValue();
        ps.put(prefName, PreferenceStore.loadObject(o));
        respond(response, "{ \"code\" : \"ok\" }");
    } catch (JSONException e) {
        respondException(response, e);
    }
}
Also used : JSONTokener(org.json.JSONTokener) Project(com.google.refine.model.Project) JSONException(org.json.JSONException) PreferenceStore(com.google.refine.preference.PreferenceStore)

Aggregations

PreferenceStore (com.google.refine.preference.PreferenceStore)4 JSONException (org.json.JSONException)4 Project (com.google.refine.model.Project)3 JSONWriter (org.json.JSONWriter)2 TopList (com.google.refine.preference.TopList)1 Properties (java.util.Properties)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1