Refactor workspace commands for Jackson serialization
This commit is contained in:
parent
553a73ec40
commit
e99a491338
@ -46,28 +46,39 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONWriter;
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRawValue;
|
||||||
|
|
||||||
|
import com.google.refine.Jsonizable;
|
||||||
import com.google.refine.ProjectManager;
|
import com.google.refine.ProjectManager;
|
||||||
import com.google.refine.commands.Command;
|
import com.google.refine.commands.Command;
|
||||||
import com.google.refine.model.metadata.ProjectMetadata;
|
import com.google.refine.model.metadata.ProjectMetadata;
|
||||||
|
|
||||||
public class GetAllProjectMetadataCommand extends Command {
|
public class GetAllProjectMetadataCommand extends Command {
|
||||||
@Override
|
public static class AllProjectMetadata implements Jsonizable {
|
||||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
@JsonProperty("projects")
|
||||||
throws ServletException, IOException {
|
protected Map<Long, ProjectMetadata> projects;
|
||||||
|
@JsonProperty("customMetadataColumns")
|
||||||
|
@JsonInclude(Include.NON_NULL)
|
||||||
|
@JsonRawValue
|
||||||
|
protected String customMetadataColumns;
|
||||||
|
|
||||||
try {
|
protected AllProjectMetadata(Map<Long, ProjectMetadata> map, String json) {
|
||||||
response.setCharacterEncoding("UTF-8");
|
projects = map;
|
||||||
response.setHeader("Content-Type", "application/json");
|
customMetadataColumns = json;
|
||||||
|
}
|
||||||
JSONWriter writer = new JSONWriter(response.getWriter());
|
|
||||||
Properties options = new Properties();
|
@Override
|
||||||
|
public void write(JSONWriter writer, Properties options)
|
||||||
writer.object();
|
throws JSONException {
|
||||||
|
|
||||||
|
writer.object();
|
||||||
|
|
||||||
writer.key("projects");
|
writer.key("projects");
|
||||||
writer.object();
|
writer.object();
|
||||||
Map<Long, ProjectMetadata> m = ProjectManager.singleton.getAllProjectMetadata();
|
for (Entry<Long,ProjectMetadata> e : projects.entrySet()) {
|
||||||
for (Entry<Long,ProjectMetadata> e : m.entrySet()) {
|
|
||||||
ProjectMetadata pm = e.getValue();
|
ProjectMetadata pm = e.getValue();
|
||||||
if (pm != null) {
|
if (pm != null) {
|
||||||
writer.key(e.getKey().toString());
|
writer.key(e.getKey().toString());
|
||||||
@ -75,15 +86,22 @@ public class GetAllProjectMetadataCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
|
|
||||||
String userMeta = (String)ProjectManager.singleton.getPreferenceStore().get("userMetadata");
|
|
||||||
if (userMeta != null) {
|
|
||||||
writer.key("customMetadataColumns");
|
|
||||||
JSONArray customMetadataColumns = new JSONArray(userMeta);
|
|
||||||
writer.value(customMetadataColumns);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (customMetadataColumns != null) {
|
||||||
|
writer.key("customMetadataColumns");
|
||||||
|
writer.value(new JSONArray(customMetadataColumns));
|
||||||
|
}
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
String userMeta = (String)ProjectManager.singleton.getPreferenceStore().get("userMetadata");
|
||||||
|
respondJSON(response, new AllProjectMetadata(ProjectManager.singleton.getAllProjectMetadata(), userMeta));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
respondException(response, e);
|
respondException(response, e);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,11 @@
|
|||||||
package com.google.refine.commands.workspace;
|
package com.google.refine.commands.workspace;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -35,33 +39,48 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONWriter;
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import com.google.refine.Jsonizable;
|
||||||
import com.google.refine.ProjectManager;
|
import com.google.refine.ProjectManager;
|
||||||
import com.google.refine.commands.Command;
|
import com.google.refine.commands.Command;
|
||||||
|
|
||||||
public class GetAllProjectTagsCommand extends Command {
|
public class GetAllProjectTagsCommand extends Command {
|
||||||
|
|
||||||
|
public static class AllProjectsTags implements Jsonizable {
|
||||||
|
|
||||||
|
@JsonProperty("tags")
|
||||||
|
protected Set<String> tags;
|
||||||
|
|
||||||
|
protected AllProjectsTags(Set<String> tags) {
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(JSONWriter writer, Properties options)
|
||||||
|
throws JSONException {
|
||||||
|
writer.object();
|
||||||
|
writer.key("tags");
|
||||||
|
writer.array();
|
||||||
|
if (tags != null) {
|
||||||
|
for (String tag : tags) {
|
||||||
|
writer.value(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.endArray();
|
||||||
|
writer.endObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response.setCharacterEncoding("UTF-8");
|
Map<String, Integer> tagMap = ProjectManager.singleton.getAllProjectTags();
|
||||||
response.setHeader("Content-Type", "application/json");
|
Set<String> tags = tagMap == null ? Collections.emptySet() : tagMap.keySet();
|
||||||
|
respondJSON(response, new AllProjectsTags(tags));
|
||||||
JSONWriter writer = new JSONWriter(response.getWriter());
|
|
||||||
// Properties options = new Properties();
|
|
||||||
|
|
||||||
writer.object();
|
|
||||||
writer.key("tags");
|
|
||||||
writer.array();
|
|
||||||
Map<String, Integer> tags = ProjectManager.singleton.getAllProjectTags();
|
|
||||||
if (tags != null) {
|
|
||||||
for (Map.Entry<String, Integer> entry : tags.entrySet()) {
|
|
||||||
writer.value(entry.getKey());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writer.endArray();
|
|
||||||
writer.endObject();
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
respondException(response, e);
|
respondException(response, e);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user