[VIVO-1320] Convert some org.json usages to Jackson
This commit is contained in:
parent
06a9360b43
commit
58cba471a8
16 changed files with 146 additions and 176 deletions
|
@ -12,9 +12,10 @@ import javax.servlet.annotation.WebServlet;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.apache.jena.ontology.OntModel;
|
||||
import org.apache.jena.rdf.model.Model;
|
||||
|
@ -139,7 +140,7 @@ public class MenuManagementEdit extends VitroHttpServlet {
|
|||
|
||||
private void sendReorderResponse(String errorMessage, HttpServletResponse resp) {
|
||||
try{
|
||||
JSONObject rObj = new JSONObject();
|
||||
ObjectNode rObj = JsonNodeFactory.instance.objectNode();
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ import org.apache.jena.rdf.model.RDFNode;
|
|||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
|
||||
import org.apache.jena.query.QuerySolution;
|
||||
import org.apache.jena.query.ResultSet;
|
||||
|
@ -133,8 +132,6 @@ class IndividualResponseBuilder {
|
|||
if (openSocialManager.isVisible()) {
|
||||
body.put("bodyOnload", "my.init();");
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
log.error("JSONException in doTemplate()", e);
|
||||
} catch (IOException e) {
|
||||
log.error("IOException in doTemplate()", e);
|
||||
} catch (SQLException e) {
|
||||
|
|
|
@ -5,9 +5,10 @@ package edu.cornell.mannlib.vitro.webapp.controller.individuallist;
|
|||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
|
||||
|
@ -26,19 +27,22 @@ public class IndividualJsonWrapper {
|
|||
addJSONFields = add;
|
||||
}
|
||||
|
||||
static JSONObject packageIndividualAsJson(VitroRequest vreq, Individual ind)
|
||||
throws JSONException {
|
||||
static ObjectNode packageIndividualAsJson(VitroRequest vreq, Individual ind) {
|
||||
// need an unfiltered dao to get firstnames and lastnames
|
||||
WebappDaoFactory fullWdf = vreq.getUnfilteredWebappDaoFactory();
|
||||
|
||||
JSONObject jo = new JSONObject();
|
||||
ObjectNode jo = JsonNodeFactory.instance.objectNode();
|
||||
jo.put("URI", ind.getURI());
|
||||
jo.put("label", ind.getRdfsLabel());
|
||||
jo.put("name", ind.getName());
|
||||
jo.put("thumbUrl", ind.getThumbUrl());
|
||||
jo.put("imageUrl", ind.getImageUrl());
|
||||
jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq));
|
||||
jo.put("mostSpecificTypes", getMostSpecificTypes(ind, fullWdf));
|
||||
ArrayNode ao = JsonNodeFactory.instance.arrayNode();
|
||||
for (String type : getMostSpecificTypes(ind, fullWdf)) {
|
||||
ao.add(type);
|
||||
}
|
||||
jo.put("mostSpecificTypes", ao);
|
||||
if (addJSONFields != null) {
|
||||
addJSONFields.add(jo, vreq, ind);
|
||||
}
|
||||
|
@ -55,6 +59,6 @@ public class IndividualJsonWrapper {
|
|||
}
|
||||
|
||||
public interface AddJSONFields {
|
||||
public void add(JSONObject jo, VitroRequest vreq, Individual ind) throws JSONException;
|
||||
public void add(ObjectNode jo, VitroRequest vreq, Individual ind);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,12 +8,13 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
|
@ -34,9 +35,10 @@ public class IndividualListResultsUtils {
|
|||
* Process results related to VClass or vclasses. Handles both single and
|
||||
* multiple vclasses being sent.
|
||||
*/
|
||||
public static JSONObject wrapIndividualListResultsInJson(IndividualListResults results, VitroRequest vreq,
|
||||
boolean multipleVclasses) {
|
||||
JSONObject rObj = new JSONObject();
|
||||
public static ObjectNode wrapIndividualListResultsInJson(IndividualListResults results, VitroRequest vreq,
|
||||
boolean multipleVclasses) {
|
||||
|
||||
ObjectNode rObj = JsonNodeFactory.instance.objectNode();
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
dumpParametersFromRequest(vreq);
|
||||
|
@ -91,47 +93,44 @@ public class IndividualListResultsUtils {
|
|||
return list;
|
||||
}
|
||||
|
||||
private static JSONObject packageVClassAsJson(VClass vclass)
|
||||
throws JSONException {
|
||||
JSONObject jvclass = new JSONObject();
|
||||
private static ObjectNode packageVClassAsJson(VClass vclass) {
|
||||
ObjectNode jvclass = JsonNodeFactory.instance.objectNode();
|
||||
jvclass.put("URI", vclass.getURI());
|
||||
jvclass.put("name", vclass.getName());
|
||||
return jvclass;
|
||||
}
|
||||
|
||||
private static JSONArray packageLettersAsJson() throws JSONException,
|
||||
UnsupportedEncodingException {
|
||||
private static ArrayNode packageLettersAsJson() throws UnsupportedEncodingException {
|
||||
List<String> letters = Controllers.getLetters();
|
||||
JSONArray jletters = new JSONArray();
|
||||
ArrayNode jletters = JsonNodeFactory.instance.arrayNode();
|
||||
for (String s : letters) {
|
||||
JSONObject jo = new JSONObject();
|
||||
ObjectNode jo = JsonNodeFactory.instance.objectNode();
|
||||
jo.put("text", s);
|
||||
jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8"));
|
||||
jletters.put(jo);
|
||||
jletters.add(jo);
|
||||
}
|
||||
return jletters;
|
||||
}
|
||||
|
||||
private static JSONArray packagePageRecordsAsJson(List<PageRecord> pages)
|
||||
throws JSONException {
|
||||
JSONArray wpages = new JSONArray();
|
||||
private static ArrayNode packagePageRecordsAsJson(List<PageRecord> pages) {
|
||||
ArrayNode wpages = JsonNodeFactory.instance.arrayNode();
|
||||
for (PageRecord pr : pages) {
|
||||
JSONObject p = new JSONObject();
|
||||
ObjectNode p = JsonNodeFactory.instance.objectNode();
|
||||
p.put("text", pr.text);
|
||||
p.put("param", pr.param);
|
||||
p.put("index", pr.index);
|
||||
wpages.put(p);
|
||||
wpages.add(p);
|
||||
}
|
||||
return wpages;
|
||||
}
|
||||
|
||||
private static JSONArray packageIndividualsAsJson(VitroRequest vreq,
|
||||
List<Individual> inds) throws JSONException {
|
||||
private static ArrayNode packageIndividualsAsJson(VitroRequest vreq,
|
||||
List<Individual> inds) {
|
||||
log.debug("Number of individuals returned from request: " + inds.size());
|
||||
|
||||
JSONArray jInds = new JSONArray();
|
||||
ArrayNode jInds = JsonNodeFactory.instance.arrayNode();
|
||||
for (Individual ind : inds) {
|
||||
jInds.put(IndividualJsonWrapper.packageIndividualAsJson(vreq, ind));
|
||||
jInds.add(IndividualJsonWrapper.packageIndividualAsJson(vreq, ind));
|
||||
}
|
||||
return jInds;
|
||||
}
|
||||
|
|
|
@ -6,9 +6,12 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
|
||||
|
@ -28,8 +31,8 @@ public class GetAllVClasses extends JsonObjectProducer {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected JSONObject process() throws Exception {
|
||||
JSONObject map = new JSONObject();
|
||||
protected ObjectNode process() throws Exception {
|
||||
ObjectNode map = JsonNodeFactory.instance.objectNode();
|
||||
//Get all VClassGroups
|
||||
List<VClass> vclasses = new ArrayList<VClass>();
|
||||
VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq);
|
||||
|
@ -43,10 +46,10 @@ public class GetAllVClasses extends JsonObjectProducer {
|
|||
|
||||
//Sort vclass by name
|
||||
Collections.sort(vclasses);
|
||||
ArrayList<JSONObject> classes = new ArrayList<JSONObject>(vclasses.size());
|
||||
ArrayNode classes = JsonNodeFactory.instance.arrayNode();
|
||||
|
||||
for(VClass vc: vclasses) {
|
||||
JSONObject vcObj = new JSONObject();
|
||||
ObjectNode vcObj = JsonNodeFactory.instance.objectNode();
|
||||
vcObj.put("name", vc.getName());
|
||||
vcObj.put("URI", vc.getURI());
|
||||
classes.add(vcObj);
|
||||
|
|
|
@ -6,12 +6,11 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
|
@ -37,8 +36,8 @@ public class GetRandomSearchIndividualsByVClass extends GetSearchIndividualsByVC
|
|||
* Search for individuals by VClass.
|
||||
*/
|
||||
@Override
|
||||
protected JSONObject process() throws Exception {
|
||||
JSONObject rObj = null;
|
||||
protected ObjectNode process() throws Exception {
|
||||
ObjectNode rObj = null;
|
||||
|
||||
//This gets the first vclass value and sets that as display type.
|
||||
List<String> vclassIds = super.getVclassIds(vreq);
|
||||
|
@ -57,13 +56,13 @@ public class GetRandomSearchIndividualsByVClass extends GetSearchIndividualsByVC
|
|||
* Look through the return object. For each individual, render the short
|
||||
* view and insert the resulting HTML into the object.
|
||||
*/
|
||||
private void addShortViewRenderings(JSONObject rObj) throws JSONException {
|
||||
JSONArray individuals = rObj.getJSONArray("individuals");
|
||||
String vclassName = rObj.getJSONObject("vclass").getString("name");
|
||||
for (int i = 0; i < individuals.length(); i++) {
|
||||
JSONObject individual = individuals.getJSONObject(i);
|
||||
private void addShortViewRenderings(ObjectNode rObj) {
|
||||
ArrayNode individuals = (ArrayNode) rObj.get("individuals");
|
||||
String vclassName = rObj.get("vclass").get("name").asText();
|
||||
for (int i = 0; i < individuals.size(); i++) {
|
||||
ObjectNode individual = (ObjectNode) individuals.get(i);
|
||||
individual.put("shortViewHtml",
|
||||
renderShortView(individual.getString("URI"), vclassName));
|
||||
renderShortView(individual.get("URI").asText(), vclassName));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,12 +6,11 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
|
@ -38,8 +37,8 @@ public class GetRenderedSearchIndividualsByVClass extends GetSearchIndividualsBy
|
|||
* information are in the request parameters.
|
||||
*/
|
||||
@Override
|
||||
protected JSONObject process() throws Exception {
|
||||
JSONObject rObj = null;
|
||||
protected ObjectNode process() throws Exception {
|
||||
ObjectNode rObj = null;
|
||||
|
||||
//This gets the first vclass value and sets that as display type
|
||||
List<String> vclassIds = super.getVclassIds(vreq);
|
||||
|
@ -65,13 +64,13 @@ public class GetRenderedSearchIndividualsByVClass extends GetSearchIndividualsBy
|
|||
* Look through the return object. For each individual, render the short
|
||||
* view and insert the resulting HTML into the object.
|
||||
*/
|
||||
private void addShortViewRenderings(JSONObject rObj) throws JSONException {
|
||||
JSONArray individuals = rObj.getJSONArray("individuals");
|
||||
String vclassName = rObj.getJSONObject("vclass").getString("name");
|
||||
for (int i = 0; i < individuals.length(); i++) {
|
||||
JSONObject individual = individuals.getJSONObject(i);
|
||||
private void addShortViewRenderings(ObjectNode rObj) {
|
||||
ArrayNode individuals = (ArrayNode) rObj.get("individuals");
|
||||
String vclassName = rObj.get("vclass").get("name").asText();
|
||||
for (int i = 0; i < individuals.size(); i++) {
|
||||
ObjectNode individual = (ObjectNode) individuals.get(i);
|
||||
individual.put("shortViewHtml",
|
||||
renderShortView(individual.getString("URI"), vclassName));
|
||||
renderShortView(individual.get("URI").asText(), vclassName));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.controller.json;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
|
@ -21,8 +22,8 @@ public class GetSearchIndividualsByVClass extends JsonObjectProducer {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected JSONObject process() throws Exception {
|
||||
VClass vclass=null;
|
||||
protected ObjectNode process() throws Exception {
|
||||
VClass vclass = null;
|
||||
|
||||
String queryType = (String) vreq.getAttribute("queryType");
|
||||
String vitroClassIdStr = vreq.getParameter("vclassId");
|
||||
|
|
|
@ -5,9 +5,9 @@ package edu.cornell.mannlib.vitro.webapp.controller.json;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
|
@ -25,7 +25,7 @@ public class GetSearchIndividualsByVClasses extends JsonObjectProducer {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected JSONObject process() throws Exception {
|
||||
protected ObjectNode process() throws Exception {
|
||||
log.debug("Executing retrieval of individuals by vclasses");
|
||||
VClass vclass=null;
|
||||
log.debug("Retrieving search individuals by vclasses");
|
||||
|
|
|
@ -4,9 +4,11 @@ package edu.cornell.mannlib.vitro.webapp.controller.json;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
|
||||
|
@ -26,8 +28,8 @@ public class GetVClassesForVClassGroup extends JsonObjectProducer {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected JSONObject process() throws Exception {
|
||||
JSONObject map = new JSONObject();
|
||||
protected ObjectNode process() throws Exception {
|
||||
ObjectNode map = JsonNodeFactory.instance.objectNode();
|
||||
String vcgUri = vreq.getParameter("classgroupUri");
|
||||
if( vcgUri == null ){
|
||||
throw new Exception("no URI passed for classgroupUri");
|
||||
|
@ -38,10 +40,10 @@ public class GetVClassesForVClassGroup extends JsonObjectProducer {
|
|||
if( vcg == null ){
|
||||
throw new Exception("Could not find vclassgroup: " + vcgUri);
|
||||
}
|
||||
|
||||
ArrayList<JSONObject> classes = new ArrayList<JSONObject>(vcg.size());
|
||||
|
||||
ArrayNode classes = JsonNodeFactory.instance.arrayNode();
|
||||
for( VClass vc : vcg){
|
||||
JSONObject vcObj = new JSONObject();
|
||||
ObjectNode vcObj = JsonNodeFactory.instance.objectNode();
|
||||
vcObj.put("name", vc.getName());
|
||||
vcObj.put("URI", vc.getURI());
|
||||
vcObj.put("entityCount", vc.getEntityCount());
|
||||
|
|
|
@ -8,10 +8,12 @@ import java.io.Writer;
|
|||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
|
||||
|
@ -42,10 +44,10 @@ public abstract class JsonObjectProducer extends JsonProducer {
|
|||
* Sub-classes implement this method. Given the request, produce a JSON
|
||||
* object as the result.
|
||||
*/
|
||||
protected abstract JSONObject process() throws Exception;
|
||||
protected abstract ObjectNode process() throws Exception;
|
||||
|
||||
public final void process(HttpServletResponse resp) throws IOException {
|
||||
JSONObject jsonObject = null;
|
||||
ObjectNode jsonObject = null;
|
||||
String errorMessage = "";
|
||||
|
||||
try {
|
||||
|
@ -57,20 +59,16 @@ public abstract class JsonObjectProducer extends JsonProducer {
|
|||
}
|
||||
|
||||
if (jsonObject == null) {
|
||||
jsonObject = new JSONObject();
|
||||
jsonObject = JsonNodeFactory.instance.objectNode();
|
||||
}
|
||||
|
||||
log.debug("Response to JSON request: " + jsonObject.toString());
|
||||
|
||||
try {
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
Writer writer = resp.getWriter();
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
Writer writer = resp.getWriter();
|
||||
|
||||
jsonObject.put("errorMessage", errorMessage);
|
||||
writer.write(jsonObject.toString());
|
||||
} catch (JSONException e) {
|
||||
log.error(e, e);
|
||||
}
|
||||
jsonObject.put("errorMessage", errorMessage);
|
||||
writer.write(jsonObject.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ import javax.servlet.annotation.WebServlet;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
|
@ -82,7 +82,7 @@ public class JsonServlet extends VitroHttpServlet {
|
|||
}
|
||||
|
||||
|
||||
public static JSONObject getSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception {
|
||||
public static ObjectNode getSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception {
|
||||
List<String> vclassURIs = Collections.singletonList(vclassURI);
|
||||
VitroRequest vreq = new VitroRequest(req);
|
||||
|
||||
|
@ -91,7 +91,7 @@ public class JsonServlet extends VitroHttpServlet {
|
|||
return IndividualListResultsUtils.wrapIndividualListResultsInJson(vcResults, vreq, false);
|
||||
}
|
||||
|
||||
public static JSONObject getSearchIndividualsByVClasses(List<String> vclassURIs, HttpServletRequest req) throws Exception {
|
||||
public static ObjectNode getSearchIndividualsByVClasses(List<String> vclassURIs, HttpServletRequest req) throws Exception {
|
||||
VitroRequest vreq = new VitroRequest(req);
|
||||
log.debug("Retrieve search results for vclasses" + vclassURIs.toString());
|
||||
IndividualListResults vcResults = getSearchVClassIntersectionResults(vclassURIs, vreq);
|
||||
|
@ -126,7 +126,7 @@ public class JsonServlet extends VitroHttpServlet {
|
|||
return value;
|
||||
}
|
||||
|
||||
public static JSONObject getRandomSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception {
|
||||
public static ObjectNode getRandomSearchIndividualsByVClass(String vclassURI, HttpServletRequest req) throws Exception {
|
||||
VitroRequest vreq = new VitroRequest(req);
|
||||
|
||||
IndividualListResults vcResults = getRandomSearchVClassResults(vclassURI, vreq);
|
||||
|
|
|
@ -28,10 +28,9 @@ import java.util.SortedMap;
|
|||
import java.util.TreeMap;
|
||||
//import java.util.regex.Pattern;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
//import org.springframework.extensions.surf.util.ISO8601DateFormat;
|
||||
|
||||
/**
|
||||
|
@ -49,44 +48,27 @@ public final class JsonToFmModel
|
|||
//
|
||||
// public static boolean autoConvertISO8601 = true;
|
||||
|
||||
/**
|
||||
* Convert JSON Object string to Freemarker-compatible data model
|
||||
*
|
||||
* @param jsonString JSON string
|
||||
* @return model
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static Map<String, Object> convertJSONObjectToMap(String jsonString) throws JSONException
|
||||
{
|
||||
JSONObject jo = new JSONObject(new JSONTokener(jsonString));
|
||||
return convertJSONObjectToMap(jo);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONObject is an unordered collection of name/value pairs -> convert to Map (equivalent to Freemarker "hash")
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> convertJSONObjectToMap(JSONObject jo) throws JSONException
|
||||
public static Map<String, Object> convertJSONObjectToMap(ObjectNode jo)
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
|
||||
Iterator<String> itr = (Iterator<String>)jo.keys();
|
||||
|
||||
Iterator<String> itr = (Iterator<String>)jo.fieldNames();
|
||||
while (itr.hasNext())
|
||||
{
|
||||
String key = (String)itr.next();
|
||||
|
||||
Object o = jo.get(key);
|
||||
if (o instanceof JSONObject)
|
||||
if (o instanceof ObjectNode)
|
||||
{
|
||||
model.put(key, convertJSONObjectToMap((JSONObject)o));
|
||||
model.put(key, convertJSONObjectToMap((ObjectNode)o));
|
||||
}
|
||||
else if (o instanceof JSONArray)
|
||||
else if (o instanceof ArrayNode)
|
||||
{
|
||||
model.put(key, convertJSONArrayToList((JSONArray)o));
|
||||
}
|
||||
else if (o == JSONObject.NULL)
|
||||
{
|
||||
model.put(key, null); // note: http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing
|
||||
model.put(key, convertJSONArrayToList((ArrayNode)o));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -102,43 +84,24 @@ public final class JsonToFmModel
|
|||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert JSON Array string to Freemarker-compatible data model
|
||||
*
|
||||
* @param jsonString JSON String
|
||||
* @return model
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static Map<String, Object> convertJSONArrayToMap(String jsonString) throws JSONException
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
JSONArray ja = new JSONArray(new JSONTokener(jsonString));
|
||||
model.put(ROOT_ARRAY, convertJSONArrayToList(ja));
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSONArray is an ordered sequence of values -> convert to List (equivalent to Freemarker "sequence")
|
||||
*/
|
||||
public static List<Object> convertJSONArrayToList(JSONArray ja) throws JSONException
|
||||
public static List<Object> convertJSONArrayToList(ArrayNode ja)
|
||||
{
|
||||
List<Object> model = new ArrayList<Object>();
|
||||
|
||||
for (int i = 0; i < ja.length(); i++)
|
||||
|
||||
for (int i = 0; i < ja.size(); i++)
|
||||
{
|
||||
Object o = ja.get(i);
|
||||
|
||||
if (o instanceof JSONArray)
|
||||
JsonNode o = ja.get(i);
|
||||
|
||||
if (o instanceof ArrayNode)
|
||||
{
|
||||
model.add(convertJSONArrayToList((JSONArray)o));
|
||||
model.add(convertJSONArrayToList((ArrayNode) o));
|
||||
}
|
||||
else if (o instanceof JSONObject)
|
||||
else if (o instanceof ObjectNode)
|
||||
{
|
||||
model.add(convertJSONObjectToMap((JSONObject)o));
|
||||
}
|
||||
else if (o == JSONObject.NULL)
|
||||
{
|
||||
model.add(null);
|
||||
model.add(convertJSONObjectToMap((ObjectNode) o));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -11,11 +11,11 @@ import java.util.Map;
|
|||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.apache.jena.rdf.model.Model;
|
||||
|
||||
|
@ -109,12 +109,12 @@ public class BrowseDataGetter extends DataGetterBase implements DataGetter {
|
|||
VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri);
|
||||
map.put("class", new VClassTemplateModel(vclass));
|
||||
|
||||
JSONObject vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request);
|
||||
map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("totalCount") ));
|
||||
map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("alpha") ));
|
||||
map.put("individuals", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("individuals") ));
|
||||
map.put("pages", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("pages") ));
|
||||
map.put("letters", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("letters") ));
|
||||
ObjectNode vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request);
|
||||
map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("totalCount") ));
|
||||
map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("alpha") ));
|
||||
map.put("individuals", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("individuals") ));
|
||||
map.put("pages", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("pages") ));
|
||||
map.put("letters", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("letters") ));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
@ -253,10 +253,7 @@ public class BrowseDataGetter extends DataGetterBase implements DataGetter {
|
|||
/**
|
||||
* For processig of JSONObject
|
||||
*/
|
||||
public JSONObject convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) {
|
||||
JSONObject rObj = null;
|
||||
return rObj;
|
||||
public ObjectNode convertToJSON(Map<String, Object> dataMap, VitroRequest vreq) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -11,11 +11,11 @@ import java.util.Map;
|
|||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividualBuilder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
|
@ -84,12 +84,12 @@ public class BrowseWidget extends Widget {
|
|||
VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri);
|
||||
map.put("class", new VClassTemplateModel(vclass));
|
||||
|
||||
JSONObject vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request);
|
||||
map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("totalCount") ));
|
||||
map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (String) vclassRes.get("alpha") ));
|
||||
map.put("individuals", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("individuals") ));
|
||||
map.put("pages", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("pages") ));
|
||||
map.put("letters", JsonToFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("letters") ));
|
||||
ObjectNode vclassRes = JsonServlet.getSearchIndividualsByVClass(vclass.getURI(), request);
|
||||
map.put("totalCount", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("totalCount") ));
|
||||
map.put("alpha", JsonToFmModel.convertJSONObjectToMap( (ObjectNode) vclassRes.get("alpha") ));
|
||||
map.put("individuals", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("individuals") ));
|
||||
map.put("pages", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("pages") ));
|
||||
map.put("letters", JsonToFmModel.convertJSONArrayToList( (ArrayNode) vclassRes.get("letters") ));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
|
|
@ -15,11 +15,12 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.identifier.RequestIdentifiers;
|
||||
|
@ -189,20 +190,26 @@ public class OpenSocialManager {
|
|||
|
||||
// JSON Helper Functions
|
||||
public static String buildJSONPersonIds(List<String> personIds,
|
||||
String message) throws JSONException {
|
||||
JSONObject json = new JSONObject();
|
||||
String message) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode json = mapper.createObjectNode();
|
||||
json.put("message", message);
|
||||
json.put("personIds", personIds);
|
||||
|
||||
ArrayNode persons = mapper.createArrayNode();
|
||||
for (String personId : personIds) {
|
||||
persons.add(personId);
|
||||
}
|
||||
json.put("personIds", persons);
|
||||
return json.toString();
|
||||
}
|
||||
|
||||
public static String buildJSONPersonIds(String personId, String message) throws JSONException {
|
||||
public static String buildJSONPersonIds(String personId, String message) {
|
||||
List<String> personIds = new ArrayList<String>();
|
||||
personIds.add(personId);
|
||||
return buildJSONPersonIds(personIds, message);
|
||||
}
|
||||
|
||||
public static String buildJSONPersonIds(Individual ind, String message) throws JSONException {
|
||||
public static String buildJSONPersonIds(Individual ind, String message) {
|
||||
List<String> personIds = new ArrayList<String>();
|
||||
personIds.add(ind.getURI());
|
||||
return buildJSONPersonIds(personIds, message);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue