VIVO-1319 Remove dependency on sourceforge.net JSON parser. (#60)

* VIVO-1246 improve the ConfigurationBeanLoader:

Add cardinality parameters minOccurs and maxOccurs
Create README.md document in the edu.cornell.mannlib.vitro.webapp.utils.configuration package
Split large class of unit tests into separate classes by functionality

* VIVO-1247, remove duplicate code used with ConfigurationBeanLoader.

Now that the @Property annotation includes cardinality parameters, we can remove a lot of duplicate code.

* VIVO-1246 Move unit tests to the new location.

* VIVO-1246 The documentation was in the wrong place.

* Ignore all /bin/ directories under eclipse

* First set of unit tests, along with enhancements to the stubs.

* Clean up the unit tests with more expressive power.

* VIVO-1319 Full tests on the controller output formats.

* Create a Jackson-based quote(), to replace the net.sf.json-based quote()

* Eliminate the use of net.sf.json.util.JSONUtils

* Use Jackson JSON library instead of net.sf.json

JSONNode, JSONArray, JSONObject become JsonNode, ArrayNode, and ObjectNode.
No direct replacement for HSONSerializer, so create JacksonUtils

Some of the message signatures are different, so adjust accordingly.
This commit is contained in:
Jim Blake 2017-05-22 14:42:10 -04:00 committed by grahamtriggs
parent a8bd8829db
commit 3829f48b0b
42 changed files with 3013 additions and 318 deletions

View file

@ -7,8 +7,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -21,6 +19,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupDao;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
@ -61,9 +60,9 @@ public class ListClassGroupsController extends FreemarkerHttpServlet {
publicName = "(unnamed group)";
}
try {
json += "{ \"name\": " + JSONUtils.quote("<a href='./editForm?uri="+URLEncoder.encode(vcg.getURI())+"&amp;controller=Classgroup'>"+publicName+"</a>") + ", ";
json += "{ \"name\": " + JacksonUtils.quote("<a href='./editForm?uri="+URLEncoder.encode(vcg.getURI())+"&amp;controller=Classgroup'>"+publicName+"</a>") + ", ";
} catch (Exception e) {
json += "{ \"name\": " + JSONUtils.quote(publicName) + ", ";
json += "{ \"name\": " + JacksonUtils.quote(publicName) + ", ";
}
Integer t;
@ -77,16 +76,16 @@ public class ListClassGroupsController extends FreemarkerHttpServlet {
VClass vcw = classIt.next();
if (vcw.getName() != null && vcw.getURI() != null) {
try {
json += "{ \"name\": " + JSONUtils.quote("<a href='vclassEdit?uri="+URLEncoder.encode(vcw.getURI())+"'>"+vcw.getName()+"</a>") + ", ";
json += "{ \"name\": " + JacksonUtils.quote("<a href='vclassEdit?uri="+URLEncoder.encode(vcw.getURI())+"'>"+vcw.getName()+"</a>") + ", ";
} catch (Exception e) {
json += "" + JSONUtils.quote(vcw.getName()) + ", ";
json += "" + JacksonUtils.quote(vcw.getName()) + ", ";
}
} else {
json += "\"\", ";
}
String shortDefStr = (vcw.getShortDef() == null) ? "" : vcw.getShortDef();
json += "\"data\": { \"shortDef\": " + JSONUtils.quote(shortDefStr) + "}, \"children\": [] ";
json += "\"data\": { \"shortDef\": " + JacksonUtils.quote(shortDefStr) + "}, \"children\": [] ";
if (classIt.hasNext())
json += "} , ";
else

View file

@ -10,8 +10,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -28,6 +26,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.DatatypeDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
public class ListDatatypePropertiesController extends FreemarkerHttpServlet {
@ -113,12 +112,12 @@ public class ListDatatypePropertiesController extends FreemarkerHttpServlet {
String nameStr = prop.getPickListName()==null ? prop.getName()==null ? prop.getURI()==null ? "(no name)" : prop.getURI() : prop.getName() : prop.getPickListName();
try {
json += "{ \"name\": " + JSONUtils.quote("<a href='datapropEdit?uri="+ URLEncoder.encode(prop.getURI())+"'>" + nameStr + "</a>") + ", ";
json += "{ \"name\": " + JacksonUtils.quote("<a href='datapropEdit?uri="+ URLEncoder.encode(prop.getURI())+"'>" + nameStr + "</a>") + ", ";
} catch (Exception e) {
json += "{ \"name\": " + JSONUtils.quote(nameStr) + ", ";
json += "{ \"name\": " + JacksonUtils.quote(nameStr) + ", ";
}
json += "\"data\": { \"internalName\": " + JSONUtils.quote(prop.getPickListName()) + ", ";
json += "\"data\": { \"internalName\": " + JacksonUtils.quote(prop.getPickListName()) + ", ";
/* VClass vc = null;
String domainStr="";
@ -138,15 +137,15 @@ public class ListDatatypePropertiesController extends FreemarkerHttpServlet {
dpLangNeut = prop;
}
String domainStr = getVClassNameFromURI(dpLangNeut.getDomainVClassURI(), vcDao, vcDaoLangNeut);
json += "\"domainVClass\": " + JSONUtils.quote(domainStr) + ", " ;
json += "\"domainVClass\": " + JacksonUtils.quote(domainStr) + ", " ;
Datatype rangeDatatype = dDao.getDatatypeByURI(prop.getRangeDatatypeURI());
String rangeDatatypeStr = (rangeDatatype==null)?prop.getRangeDatatypeURI():rangeDatatype.getName();
json += "\"rangeVClass\": " + JSONUtils.quote(rangeDatatypeStr) + ", " ;
json += "\"rangeVClass\": " + JacksonUtils.quote(rangeDatatypeStr) + ", " ;
if (prop.getGroupURI() != null) {
PropertyGroup pGroup = pgDao.getGroupByURI(prop.getGroupURI());
json += "\"group\": " + JSONUtils.quote((pGroup == null) ? "unknown group" : pGroup.getName()) + " } } " ;
json += "\"group\": " + JacksonUtils.quote((pGroup == null) ? "unknown group" : pGroup.getName()) + " } } " ;
} else {
json += "\"group\": \"unspecified\" } }" ;
}

View file

@ -2,19 +2,12 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -23,15 +16,12 @@ import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.AuthorizationReques
import edu.cornell.mannlib.vitro.webapp.beans.FauxProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.PropertyGroup;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.FauxPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
public class ListFauxPropertiesController extends FreemarkerHttpServlet {

View file

@ -8,8 +8,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -24,6 +22,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyGroupDao;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
public class ListPropertyGroupsController extends FreemarkerHttpServlet {
@ -62,9 +61,9 @@ public class ListPropertyGroupsController extends FreemarkerHttpServlet {
publicName = "(unnamed group)";
}
try {
json += "{ \"name\": " + JSONUtils.quote("<a href='./editForm?uri="+URLEncoder.encode(pg.getURI(),"UTF-8")+"&amp;controller=PropertyGroup'>" + publicName + "</a>") + ", ";
json += "{ \"name\": " + JacksonUtils.quote("<a href='./editForm?uri="+URLEncoder.encode(pg.getURI(),"UTF-8")+"&amp;controller=PropertyGroup'>" + publicName + "</a>") + ", ";
} catch (Exception e) {
json += "{ \"name\": " + JSONUtils.quote(publicName) + ", ";
json += "{ \"name\": " + JacksonUtils.quote(publicName) + ", ";
}
Integer t;
@ -89,10 +88,10 @@ public class ListPropertyGroupsController extends FreemarkerHttpServlet {
}
if (prop.getURI() != null) {
try {
json += "{ \"name\": " + JSONUtils.quote("<a href='" + controllerStr
json += "{ \"name\": " + JacksonUtils.quote("<a href='" + controllerStr
+ "?uri="+URLEncoder.encode(prop.getURI(),"UTF-8")+"'>"+ nameStr +"</a>") + ", ";
} catch (Exception e) {
json += JSONUtils.quote(nameStr) + ", ";
json += JacksonUtils.quote(nameStr) + ", ";
}
} else {
json += "\"\", ";

View file

@ -11,8 +11,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -32,6 +30,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.PropertyGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyInstanceDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
public class ListPropertyWebappsController extends FreemarkerHttpServlet {
@ -154,27 +153,27 @@ public class ListPropertyWebappsController extends FreemarkerHttpServlet {
String propNameStr = ShowObjectPropertyHierarchyController.getDisplayLabel(prop);
try {
json += "{ \"name\": " + JSONUtils.quote("<a href='./propertyEdit?uri="+URLEncoder.encode(prop.getURI())+"'>"
json += "{ \"name\": " + JacksonUtils.quote("<a href='./propertyEdit?uri="+URLEncoder.encode(prop.getURI())+"'>"
+ propNameStr + "</a>") + ", ";
} catch (Exception e) {
json += "{ \"name\": \"" + propNameStr + "\", ";
}
json += "\"data\": { \"internalName\": " + JSONUtils.quote(prop.getLocalNameWithPrefix()) + ", ";
json += "\"data\": { \"internalName\": " + JacksonUtils.quote(prop.getLocalNameWithPrefix()) + ", ";
ObjectProperty opLangNeut = opDaoLangNeut.getObjectPropertyByURI(prop.getURI());
if(opLangNeut == null) {
opLangNeut = prop;
}
String domainStr = getVClassNameFromURI(opLangNeut.getDomainVClassURI(), vcDao, vcDaoLangNeut);
json += "\"domainVClass\": " + JSONUtils.quote(domainStr) + ", " ;
json += "\"domainVClass\": " + JacksonUtils.quote(domainStr) + ", " ;
String rangeStr = getVClassNameFromURI(opLangNeut.getRangeVClassURI(), vcDao, vcDaoLangNeut);
json += "\"rangeVClass\": " + JSONUtils.quote(rangeStr) + ", " ;
json += "\"rangeVClass\": " + JacksonUtils.quote(rangeStr) + ", " ;
if (prop.getGroupURI() != null) {
PropertyGroup pGroup = pgDao.getGroupByURI(prop.getGroupURI());
json += "\"group\": " + JSONUtils.quote((pGroup == null) ? "unknown group" : pGroup.getName()) + " } } " ;
json += "\"group\": " + JacksonUtils.quote((pGroup == null) ? "unknown group" : pGroup.getName()) + " } } " ;
} else {
json += "\"group\": \"unspecified\" } }" ;
}

View file

@ -8,8 +8,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -26,6 +24,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.PropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
public class ListVClassWebappsController extends FreemarkerHttpServlet {
@ -90,15 +89,15 @@ public class ListVClassWebappsController extends FreemarkerHttpServlet {
if ( (ontologyURI==null) || ( (ontologyURI != null) && (cls.getNamespace()!=null) && (ontologyURI.equals(cls.getNamespace())) ) ) {
if (cls.getName() != null)
try {
json += "{ \"name\": " + JSONUtils.quote("<a href='./vclassEdit?uri="+URLEncoder.encode(cls.getURI(),"UTF-8")+"'>"+cls.getPickListName()+"</a>") + ", ";
json += "{ \"name\": " + JacksonUtils.quote("<a href='./vclassEdit?uri="+URLEncoder.encode(cls.getURI(),"UTF-8")+"'>"+cls.getPickListName()+"</a>") + ", ";
} catch (Exception e) {
json += "{ \"name\": " + JSONUtils.quote(cls.getPickListName()) + ", ";
json += "{ \"name\": " + JacksonUtils.quote(cls.getPickListName()) + ", ";
}
else
json += "{ \"name\": \"\"";
String shortDef = (cls.getShortDef() == null) ? "" : cls.getShortDef();
json += "\"data\": { \"shortDef\": " + JSONUtils.quote(shortDef) + ", ";
json += "\"data\": { \"shortDef\": " + JacksonUtils.quote(shortDef) + ", ";
// get group name
WebappDaoFactory wadf = vreq.getUnfilteredWebappDaoFactory();
@ -113,7 +112,7 @@ public class ListVClassWebappsController extends FreemarkerHttpServlet {
}
}
json += "\"classGroup\": " + JSONUtils.quote(groupName) + ", ";
json += "\"classGroup\": " + JacksonUtils.quote(groupName) + ", ";
// get ontology name
OntologyDao ontDao = wadf.getOntologyDao();
@ -122,7 +121,7 @@ public class ListVClassWebappsController extends FreemarkerHttpServlet {
if (ont != null && ont.getName() != null) {
ontName = ont.getName();
}
json += "\"ontology\": " + JSONUtils.quote(ontName) + "} }";
json += "\"ontology\": " + JacksonUtils.quote(ontName) + "} }";
counter++;

View file

@ -10,11 +10,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.vocabulary.OWL;
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
@ -29,6 +26,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
public class ShowClassHierarchyController extends FreemarkerHttpServlet {
@ -196,16 +194,16 @@ public class ShowClassHierarchyController extends FreemarkerHttpServlet {
tempString += "}, { \"name\": ";
}
try {
tempString += JSONUtils.quote("<a href='vclassEdit?uri=" +
tempString += JacksonUtils.quote("<a href='vclassEdit?uri=" +
URLEncoder.encode(vcw.getURI(),"UTF-8") + "'>" +
vcw.getPickListName() + "</a>") +", ";
} catch (Exception e) {
tempString += JSONUtils.quote(((vcw.getPickListName() == null)
tempString += JacksonUtils.quote(((vcw.getPickListName() == null)
? "" : vcw.getPickListName())) + ", ";
}
String shortDef = ((vcw.getShortDef() == null) ? "" : vcw.getShortDef()) ;
tempString += "\"data\": { \"shortDef\": " + JSONUtils.quote(shortDef) + ", ";
tempString += "\"data\": { \"shortDef\": " + JacksonUtils.quote(shortDef) + ", ";
// Get group name if it exists
VClassGroupDao groupDao= wadf.getVClassGroupDao();
@ -218,7 +216,7 @@ public class ShowClassHierarchyController extends FreemarkerHttpServlet {
groupName = classGroup.getPublicName();
}
}
tempString += "\"classGroup\": " + JSONUtils.quote(
tempString += "\"classGroup\": " + JacksonUtils.quote(
(groupName == null) ? "" : groupName) + ", ";
// Get ontology name
OntologyDao ontDao = wadf.getOntologyDao();
@ -227,7 +225,7 @@ public class ShowClassHierarchyController extends FreemarkerHttpServlet {
if (ont != null && ont.getName() != null) {
ontName = ont.getName();
}
tempString += "\"ontology\": " + JSONUtils.quote(
tempString += "\"ontology\": " + JacksonUtils.quote(
(ontName == null) ? "" : ontName) + "}, \"children\": [";
previous_posn = position;

View file

@ -9,8 +9,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -27,6 +25,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.DatatypeDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
public class ShowDataPropertyHierarchyController extends FreemarkerHttpServlet {
@ -211,11 +210,11 @@ public class ShowDataPropertyHierarchyController extends FreemarkerHttpServlet {
? dp.getURI() == null
? "(no name)" : dp.getURI() : dp.getName() : dp.getPickListName();
tempString += JSONUtils.quote(
tempString += JacksonUtils.quote(
"<a href='datapropEdit?uri=" + URLEncoder.encode(
dp.getURI()) + "'>" + nameStr + "</a>") + ", ";
tempString += "\"data\": { \"internalName\": " + JSONUtils.quote(
tempString += "\"data\": { \"internalName\": " + JacksonUtils.quote(
dp.getPickListName()) + ", ";
DataProperty dpLangNeut = dpDaoLangNeut.getDataPropertyByURI(dp.getURI());
@ -225,20 +224,20 @@ public class ShowDataPropertyHierarchyController extends FreemarkerHttpServlet {
String domainStr = getVClassNameFromURI(dpLangNeut.getDomainVClassURI(), vcDao, vcDaoLangNeut);
try {
tempString += "\"domainVClass\": " + JSONUtils.quote(domainStr) + ", " ;
tempString += "\"domainVClass\": " + JacksonUtils.quote(domainStr) + ", " ;
} catch (NullPointerException e) {
tempString += "\"domainVClass\": \"\",";
}
try {
Datatype rangeDatatype = dDao.getDatatypeByURI(dp.getRangeDatatypeURI());
String rangeDatatypeStr = (rangeDatatype==null)?dp.getRangeDatatypeURI():rangeDatatype.getName();
tempString += "\"rangeVClass\": " + JSONUtils.quote((rangeDatatypeStr != null) ? rangeDatatypeStr : "") + ", " ;
tempString += "\"rangeVClass\": " + JacksonUtils.quote((rangeDatatypeStr != null) ? rangeDatatypeStr : "") + ", " ;
} catch (NullPointerException e) {
tempString += "\"rangeVClass\": \"\",";
}
if (dp.getGroupURI() != null) {
PropertyGroup pGroup = pgDao.getGroupByURI(dp.getGroupURI());
tempString += "\"group\": " + JSONUtils.quote((pGroup == null) ? "unknown group" : pGroup.getName());
tempString += "\"group\": " + JacksonUtils.quote((pGroup == null) ? "unknown group" : pGroup.getName());
} else {
tempString += "\"group\": \"unspecified\"";
}

View file

@ -12,8 +12,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.sf.json.util.JSONUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -28,6 +26,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.Tem
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
public class ShowObjectPropertyHierarchyController extends FreemarkerHttpServlet {
@ -210,11 +209,11 @@ public class ShowObjectPropertyHierarchyController extends FreemarkerHttpServlet
String nameStr = getDisplayLabel(op) == null ? "(no name)" : getDisplayLabel(op);
tempString += JSONUtils.quote(
tempString += JacksonUtils.quote(
"<a href='propertyEdit?uri=" + URLEncoder.encode(
op.getURI()) + "'>" + nameStr + "</a>") + ", ";
tempString += "\"data\": { \"internalName\": " + JSONUtils.quote(
tempString += "\"data\": { \"internalName\": " + JacksonUtils.quote(
op.getLocalNameWithPrefix()) + ", ";
ObjectProperty opLangNeut = opDaoLangNeut.getObjectPropertyByURI(op.getURI());
@ -225,18 +224,18 @@ public class ShowObjectPropertyHierarchyController extends FreemarkerHttpServlet
String rangeStr = getVClassNameFromURI(opLangNeut.getRangeVClassURI(), vcDao, vcDaoLangNeut);
try {
tempString += "\"domainVClass\": " + JSONUtils.quote(domainStr) + ", " ;
tempString += "\"domainVClass\": " + JacksonUtils.quote(domainStr) + ", " ;
} catch (NullPointerException e) {
tempString += "\"domainVClass\": \"\",";
}
try {
tempString += "\"rangeVClass\": " + JSONUtils.quote(rangeStr) + ", " ;
tempString += "\"rangeVClass\": " + JacksonUtils.quote(rangeStr) + ", " ;
} catch (NullPointerException e) {
tempString += "\"rangeVClass\": \"\",";
}
if (op.getGroupURI() != null) {
PropertyGroup pGroup = pgDao.getGroupByURI(op.getGroupURI());
tempString += "\"group\": " + JSONUtils.quote(
tempString += "\"group\": " + JacksonUtils.quote(
(pGroup == null) ? "unknown group" : pGroup.getName());
} else {
tempString += "\"group\": \"unspecified\"";

View file

@ -12,12 +12,8 @@ import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
@ -31,6 +27,10 @@ import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.vocabulary.XSD;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -258,7 +258,7 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
String pageUri,
OntModel queryModel) {
//Create json array to be set within form specific data
JSONArray jsonArray = new JSONArray();
ArrayNode jsonArray = new ObjectMapper().createArrayNode();
String querystr = getExistingDataGettersQuery();
//Bind pageUri to query
QuerySolutionMap initialBindings = new QuerySolutionMap();
@ -291,7 +291,7 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
}
}
private void addJSONArrayToFormSpecificData(JSONArray jsonArray, EditConfigurationVTwo editConfig) {
private void addJSONArrayToFormSpecificData(ArrayNode jsonArray, EditConfigurationVTwo editConfig) {
HashMap<String, Object> data = editConfig.getFormSpecificData();
data.put("existingPageContentUnits", jsonArray.toString());
//Experimenting with putting actual array in
@ -300,7 +300,7 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
}
private void processExistingDataGetter(int counter, String dataGetterURI, String dgClassName,
EditConfigurationVTwo editConfig, OntModel queryModel, JSONArray jsonArray, ServletContext context) {
EditConfigurationVTwo editConfig, OntModel queryModel, ArrayNode jsonArray, ServletContext context) {
ProcessDataGetterN3 pn = ProcessDataGetterN3Utils.getDataGetterProcessorN3(dgClassName, null);
//Add N3 Optional as well
@ -323,10 +323,10 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
//including: (i) The JSON object representing the existing information to be returned to template
//Takes data getter information, packs within JSON object to send back to the form
private void addDataGetterSpecificFormData(String dataGetterURI, ProcessDataGetterN3 pn, OntModel queryModel, JSONArray jsonArray, ServletContext context) {
JSONObject jo = pn.getExistingValuesJSON(dataGetterURI, queryModel, context);
private void addDataGetterSpecificFormData(String dataGetterURI, ProcessDataGetterN3 pn, OntModel queryModel, ArrayNode jsonArray, ServletContext context) {
ObjectNode jo = pn.getExistingValuesJSON(dataGetterURI, queryModel, context);
//Add dataGetterURI to jsonObject
jo.element("URI", dataGetterURI);
jo.put("URI", dataGetterURI);
jsonArray.add(jo);
}

View file

@ -8,15 +8,14 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.rdf.model.Literal;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.BaseEditSubmissionPreprocessorVTwo;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils;
@ -27,6 +26,7 @@ import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.EditConfigu
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.ManagePageGenerator;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessDataGetterN3;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessDataGetterN3Utils;
import edu.cornell.mannlib.vitro.webapp.utils.json.JacksonUtils;
public class ManagePagePreprocessor extends
BaseEditSubmissionPreprocessorVTwo {
@ -39,7 +39,7 @@ public class ManagePagePreprocessor extends
private static Map<String, List<String>> transformedLiteralsFromForm = null;
private static Map<String, List<String>> urisFromForm = null;
private static List<String> pageContentUnits = null;//String submission from form
private static List<JSONObject> pageContentUnitsJSON = null;//converted to JSON objects that can be read
private static List<ObjectNode> pageContentUnitsJSON = null;//converted to JSON objects that can be read
// String datatype
// Will be editing the edit configuration as well as edit submission here
@ -131,7 +131,7 @@ public class ManagePagePreprocessor extends
private void processDataGetters() {
convertToJson();
int counter = 0;
for(JSONObject jsonObject:pageContentUnitsJSON) {
for(ObjectNode jsonObject:pageContentUnitsJSON) {
String dataGetterClass = getDataGetterClass(jsonObject);
ProcessDataGetterN3 pn = ProcessDataGetterN3Utils.getDataGetterProcessorN3(dataGetterClass, jsonObject);
//UPDATE: using class type to indicate class type/ could also get it from
@ -175,12 +175,12 @@ public class ManagePagePreprocessor extends
private void convertToJson() {
//Iterate through list of inputs
pageContentUnitsJSON = new ArrayList<JSONObject>();
pageContentUnitsJSON = new ArrayList<ObjectNode>();
//page content units might return null in case self-contained template is selected
//otherwise there should be page content units returned from the form
if(pageContentUnits != null) {
for(String pageContentUnit: pageContentUnits) {
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( pageContentUnit );
ObjectNode jsonObject = (ObjectNode) JacksonUtils.parseJson(pageContentUnit);
pageContentUnitsJSON.add(jsonObject);
}
}
@ -190,29 +190,29 @@ public class ManagePagePreprocessor extends
//Each field name will correspond to the names of the fileds/uris on form/literals on form
//generated here
private void addInputsToSubmission(ProcessDataGetterN3 pn, int counter, JSONObject jsonObject) {
private void addInputsToSubmission(ProcessDataGetterN3 pn, int counter, ObjectNode jsonObject) {
List<String> literalLabels = pn.getLiteralVarNamesBase();
List<String> uriLabels = pn.getUriVarNamesBase();
for(String literalLabel:literalLabels) {
List<String> literalValues = new ArrayList<String>();
Object jsonValue = jsonObject.get(literalLabel);
JsonNode jsonValue = jsonObject.get(literalLabel);
//Var names will depend on which data getter object this is on the page, so depends on counter
String submissionLiteralName = pn.getVarName(literalLabel, counter);
//Single value
if(jsonValue instanceof String) {
if(jsonValue.isTextual()) {
//TODO: Deal with multiple submission values
//This retrieves the value for this particular json object
String jsonString = jsonObject.getString(literalLabel);
String jsonString = jsonObject.get(literalLabel).asText();
jsonString = pn.replaceEncodedQuotesWithEscapedQuotes(jsonString);
literalValues.add(jsonString);
} else if(jsonValue instanceof JSONArray) {
JSONArray values = jsonObject.getJSONArray(literalLabel);
literalValues = (List<String>) JSONSerializer.toJava(values);
} else if(jsonValue.isArray()) {
ArrayNode values = (ArrayNode) jsonObject.get(literalLabel);
literalValues = JacksonUtils.jsonArrayToStrings(values);
//Replacing encoded quotes here as well
this.replaceEncodedQuotesInList(pn, literalValues);
} else if(jsonValue instanceof Boolean) {
Boolean booleanValue = jsonObject.getBoolean(literalLabel);
} else if(jsonValue.isBoolean()) {
Boolean booleanValue = jsonObject.get(literalLabel).asBoolean();
//Adds string version
literalValues.add(booleanValue.toString());
}
@ -227,19 +227,19 @@ public class ManagePagePreprocessor extends
for(String uriLabel:uriLabels) {
List<String> uriValues = new ArrayList<String>();
Object jsonValue = jsonObject.get(uriLabel);
JsonNode jsonValue = jsonObject.get(uriLabel);
//Var names will depend on which data getter object this is on the page, so depends on counter
String submissionUriName = pn.getVarName(uriLabel, counter);
//if single value, then, add to values
if(jsonValue instanceof String) {
if(jsonValue.isTextual()) {
//Var names will depend on which data getter object this is on the page, so depends on counter
//This retrieves the value for this particular json object and adds to list
uriValues.add(jsonObject.getString(uriLabel));
uriValues.add(jsonObject.get(uriLabel).asText());
} else if(jsonValue instanceof JSONArray) {
} else if(jsonValue.isArray()) {
//multiple values
JSONArray values = jsonObject.getJSONArray(uriLabel);
uriValues = (List<String>) JSONSerializer.toJava(values);
ArrayNode values = (ArrayNode) jsonObject.get(uriLabel);
uriValues = JacksonUtils.jsonArrayToStrings(values);
} else {
//This may include JSON Objects but no way to deal with these right now
@ -257,8 +257,8 @@ public class ManagePagePreprocessor extends
//Although this is editing an existing page, new content might have been added which would not include
//existing data getter URIs, so important to check whether the key exists within the json object in the first place
String dataGetterURISubmissionName = pn.getDataGetterVarName(counter);
if(jsonObject.containsKey("URI")) {
String URIValue = jsonObject.getString("URI");
if(jsonObject.has("URI")) {
String URIValue = jsonObject.get("URI").asText();
if(URIValue != null) {
log.debug("Existing URI for data getter found: " + URIValue);
submission.addUriToForm(editConfiguration, dataGetterURISubmissionName, new String[]{URIValue});
@ -366,8 +366,8 @@ public class ManagePagePreprocessor extends
//Each JSON Object will indicate the type of the data getter within it
private String getDataGetterClass(JSONObject jsonObject) {
String javaURI = jsonObject.getString("dataGetterClass");
private String getDataGetterClass(ObjectNode jsonObject) {
String javaURI = jsonObject.get("dataGetterClass").asText();
return getQualifiedDataGetterName(javaURI);

View file

@ -8,12 +8,8 @@ import java.util.List;
import javax.servlet.ServletContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
@ -23,6 +19,10 @@ import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Resource;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
@ -153,10 +153,10 @@ public class ProcessClassGroupDataGetterN3 extends ProcessDataGetterAbstract {
return query;
}
public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
JSONObject jObject = new JSONObject();
jObject.element("dataGetterClass", classType);
jObject.element(classTypeVarBase, classType);
public ObjectNode getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
ObjectNode jObject = new ObjectMapper().createObjectNode();
jObject.put("dataGetterClass", classType);
jObject.put(classTypeVarBase, classType);
//Get class group
getExistingClassGroup(dataGetterURI, jObject, queryModel);
//Get classes within class group
@ -164,7 +164,7 @@ public class ProcessClassGroupDataGetterN3 extends ProcessDataGetterAbstract {
return jObject;
}
private void getExistingClassGroup(String dataGetterURI, JSONObject jObject, OntModel queryModel) {
private void getExistingClassGroup(String dataGetterURI, ObjectNode jObject, OntModel queryModel) {
String querystr = getExistingValuesClassGroup(dataGetterURI);
QueryExecution qe = null;
try{
@ -175,7 +175,7 @@ public class ProcessClassGroupDataGetterN3 extends ProcessDataGetterAbstract {
QuerySolution qs = results.nextSolution();
Resource classGroupResource = qs.getResource("classGroup");
//Put both literals in existing literals
jObject.element(classGroupVarBase, classGroupResource.getURI());
jObject.put(classGroupVarBase, classGroupResource.getURI());
}
} catch(Exception ex) {
log.error("Exception occurred in retrieving existing values with query " + querystr, ex);
@ -186,10 +186,10 @@ public class ProcessClassGroupDataGetterN3 extends ProcessDataGetterAbstract {
//Assumes JSON Object received will have the class group resource URI within it
//TODO: Refactor to take advantage of existing code that uses OTHER JSON library
protected void getExistingClassesInClassGroup(ServletContext context, String dataGetterURI, JSONObject jObject) {
protected void getExistingClassesInClassGroup(ServletContext context, String dataGetterURI, ObjectNode jObject) {
//Check for class group resource within json object
if(jObject.containsKey(classGroupVarBase)) {
String classGroupURI = jObject.getString(classGroupVarBase);
if(jObject.has(classGroupVarBase)) {
String classGroupURI = jObject.get(classGroupVarBase).asText();
//Get classes for classGroupURI and include in
VClassGroupCache vcgc = VClassGroupCache.getVClassGroupCache(context);
VClassGroup group = vcgc.getGroup(classGroupURI);
@ -201,20 +201,21 @@ public class ProcessClassGroupDataGetterN3 extends ProcessDataGetterAbstract {
//JSONObject will include results JSON object that will include classes JSON Arrya as well as
//class group information
protected void populateClassesInClassGroupJSON(JSONObject jObject, VClassGroup group) {
JSONArray classes = new JSONArray();
protected void populateClassesInClassGroupJSON(ObjectNode jObject, VClassGroup group) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode classes = mapper.createArrayNode();
for( VClass vc : group){
JSONObject vcObj = new JSONObject();
vcObj.element("name", vc.getName());
vcObj.element("URI", vc.getURI());
ObjectNode vcObj = mapper.createObjectNode();
vcObj.put("name", vc.getName());
vcObj.put("URI", vc.getURI());
classes.add(vcObj);
}
JSONObject results = new JSONObject();
ObjectNode results = mapper.createObjectNode();
results.element("classes", classes);
results.element("classGroupName", group.getPublicName());
results.element("classGroupUri", group.getURI());
jObject.element("results", results);
results.set("classes", classes);
results.put("classGroupName", group.getPublicName());
results.put("classGroupUri", group.getURI());
jObject.set("results", results);
}
}

View file

@ -2,16 +2,16 @@
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import javax.servlet.ServletContext;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.rdf.model.Literal;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
//Returns the appropriate n3 based on data getter
@ -36,7 +36,7 @@ public interface ProcessDataGetterN3 {
public Map<String, List<Literal>> retrieveExistingLiteralValues();
public Map<String, List<String>> retrieveExistingUriValues();
public void populateExistingValues(String dataGetterURI, int counter, OntModel queryModel);
public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context);
public ObjectNode getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context);
public String replaceEncodedQuotesWithEscapedQuotes(String inputStr);
}

View file

@ -5,18 +5,18 @@ package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocess
import java.lang.reflect.Constructor;
import java.util.HashMap;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
/*
* This class determines what n3 should be returned for a particular data getter and can be overwritten or extended in VIVO.
*/
public class ProcessDataGetterN3Utils {
private static final Log log = LogFactory.getLog(ProcessDataGetterN3Utils.class);
public static ProcessDataGetterN3 getDataGetterProcessorN3(String dataGetterClass, JSONObject jsonObject) {
public static ProcessDataGetterN3 getDataGetterProcessorN3(String dataGetterClass, ObjectNode jsonObject) {
HashMap<String, String> map = ProcessDataGetterN3Map.getDataGetterTypeToProcessorMap();
//
if(map.containsKey(dataGetterClass)) {
@ -32,7 +32,7 @@ public class ProcessDataGetterN3Utils {
return null;
}
private static ProcessDataGetterN3 instantiateClass(String processorClass, JSONObject jsonObject) {
private static ProcessDataGetterN3 instantiateClass(String processorClass, ObjectNode jsonObject) {
ProcessDataGetterN3 pn = null;
try {
Class<?> clz = Class.forName(processorClass);

View file

@ -5,13 +5,11 @@ package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocess
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletContext;
import net.sf.json.JSONObject;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
@ -21,6 +19,9 @@ import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Literal;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
//Returns the appropriate n3 based on data getter
public class ProcessFixedHTMLN3 extends ProcessDataGetterAbstract {
@ -136,10 +137,10 @@ public class ProcessFixedHTMLN3 extends ProcessDataGetterAbstract {
//Method to create a JSON object with existing values to return to form
//There may be a better way to do this without having to run the query twice
//TODO: Refactor code if required
public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
JSONObject jObject = new JSONObject();
jObject.element("dataGetterClass", classType);
jObject.element(classTypeVarBase, classType);
public ObjectNode getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
ObjectNode jObject = new ObjectMapper().createObjectNode();
jObject.put("dataGetterClass", classType);
jObject.put(classTypeVarBase, classType);
String querystr = getExistingValuesSparqlQuery(dataGetterURI);
QueryExecution qe = null;
try{
@ -152,9 +153,9 @@ public class ProcessFixedHTMLN3 extends ProcessDataGetterAbstract {
Literal htmlValueLiteral = qs.getLiteral("htmlValue");
String htmlValueString = htmlValueLiteral.getString();
htmlValueString = this.replaceQuotes(htmlValueString);
jObject.element("saveToVar", saveToVarLiteral.getString());
jObject.put("saveToVar", saveToVarLiteral.getString());
//TODO: Handle single and double quotes within string and escape properlyu
jObject.element("htmlValue", htmlValueString);
jObject.put("htmlValue", htmlValueString);
}
} catch(Exception ex) {
log.error("Exception occurred in retrieving existing values with query " + querystr, ex);

View file

@ -2,16 +2,14 @@
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
@ -19,16 +17,15 @@ import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Resource;
import javax.servlet.ServletContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
//Returns the appropriate n3 for selection of classes from within class group
public class ProcessIndividualsForClassesDataGetterN3 extends ProcessClassGroupDataGetterN3 {
private static String classType = "java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.IndividualsForClassesDataGetter";
@ -174,11 +171,11 @@ public class ProcessIndividualsForClassesDataGetterN3 extends ProcessClassGroup
}
public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
JSONObject jObject = new JSONObject();
jObject.element("dataGetterClass", classType);
public ObjectNode getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
ObjectNode jObject = new ObjectMapper().createObjectNode();
jObject.put("dataGetterClass", classType);
//Update to include class type as variable
jObject.element(classTypeVarBase, classType);
jObject.put(classTypeVarBase, classType);
//Get selected class group and which classes were selected
getExistingClassGroupAndIndividuals(dataGetterURI, jObject, queryModel);
//Get all classes within the class group
@ -186,14 +183,14 @@ public class ProcessIndividualsForClassesDataGetterN3 extends ProcessClassGroup
return jObject;
}
private void getExistingClassGroupAndIndividuals(String dataGetterURI, JSONObject jObject, OntModel queryModel) {
private void getExistingClassGroupAndIndividuals(String dataGetterURI, ObjectNode jObject, OntModel queryModel) {
String querystr = getExistingValuesIndividualsForClasses(dataGetterURI);
QueryExecution qe = null;
try{
Query query = QueryFactory.create(querystr);
qe = QueryExecutionFactory.create(query, queryModel);
ResultSet results = qe.execSelect();
JSONArray individualsForClasses = new JSONArray();
ArrayNode individualsForClasses = new ObjectMapper().createArrayNode();
String classGroupURI = null;
while( results.hasNext()){
QuerySolution qs = results.nextSolution();
@ -207,9 +204,9 @@ public class ProcessIndividualsForClassesDataGetterN3 extends ProcessClassGroup
}
jObject.element("classGroup", classGroupURI);
jObject.put("classGroup", classGroupURI);
//this is a json array
jObject.element(individualClassVarNameBase, individualsForClasses);
jObject.set(individualClassVarNameBase, individualsForClasses);
} catch(Exception ex) {
log.error("Exception occurred in retrieving existing values with query " + querystr, ex);
}

View file

@ -5,13 +5,11 @@ package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocess
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletContext;
import net.sf.json.JSONObject;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
@ -22,6 +20,9 @@ import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Resource;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
//Returns the appropriate n3 based on data getter
public class ProcessSearchIndividualsDataGetterN3 extends ProcessDataGetterAbstract {
@ -138,10 +139,10 @@ public class ProcessSearchIndividualsDataGetterN3 extends ProcessDataGetterAbst
//Method to create a JSON object with existing values to return to form
//There may be a better way to do this without having to run the query twice
//TODO: Refactor code if required
public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
JSONObject jObject = new JSONObject();
jObject.element("dataGetterClass", classType);
jObject.element(classTypeVarBase, classType);
public ObjectNode getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
ObjectNode jObject = new ObjectMapper().createObjectNode();
jObject.put("dataGetterClass", classType);
jObject.put(classTypeVarBase, classType);
String querystr = getExistingValuesSparqlQuery(dataGetterURI);
QueryExecution qe = null;
try{
@ -153,9 +154,9 @@ public class ProcessSearchIndividualsDataGetterN3 extends ProcessDataGetterAbst
Literal saveToVarLiteral = qs.getLiteral("saveToVar");
Resource vclassUriResource = qs.getResource("vclassUri");
String vclassUriString = vclassUriResource.getURI();
jObject.element("saveToVar", saveToVarLiteral.getString());
jObject.put("saveToVar", saveToVarLiteral.getString());
//TODO: Handle single and double quotes within string and escape properlyu
jObject.element("vclassUri", vclassUriString);
jObject.put("vclassUri", vclassUriString);
}
} catch(Exception ex) {
log.error("Exception occurred in retrieving existing values with query " + querystr, ex);

View file

@ -2,15 +2,14 @@
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
@ -20,12 +19,12 @@ import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Resource;
import javax.servlet.ServletContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
//Returns the appropriate n3 based on data getter
public class ProcessSparqlDataGetterN3 extends ProcessDataGetterAbstract {
private static String classType = "java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter";
@ -157,10 +156,10 @@ public class ProcessSparqlDataGetterN3 extends ProcessDataGetterAbstract {
public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
JSONObject jObject = new JSONObject();
jObject.element("dataGetterClass", classType);
jObject.element(classTypeVarBase, classType);
public ObjectNode getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
ObjectNode jObject = new ObjectMapper().createObjectNode();
jObject.put("dataGetterClass", classType);
jObject.put(classTypeVarBase, classType);
String querystr = getExistingValuesSparqlQuery(dataGetterURI);
QueryExecution qe = null;
try{
@ -179,12 +178,12 @@ public class ProcessSparqlDataGetterN3 extends ProcessDataGetterAbstract {
//or incorrect html
queryString = replaceQuotes(queryString);
Resource queryModelResource = qs.getResource("queryModel");
jObject.element("saveToVar", saveToVarLiteral.getString());
jObject.element("query",queryString);
jObject.put("saveToVar", saveToVarLiteral.getString());
jObject.put("query", queryString);
if(queryModelResource != null) {
jObject.element("queryModel", queryModelResource.getURI());
jObject.put("queryModel", queryModelResource.getURI());
} else {
jObject.element("queryModel", "");
jObject.put("queryModel", "");
}
}

View file

@ -0,0 +1,81 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils.json;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
/**
* Some utility methods to ease the transition from net.sf.json to Jackson.
*/
public class JacksonUtils {
private static final String QUOTE = "\"";
/**
* A "clean room" replacement for net.sf.json.util.JSONUtils.quote().
*/
public static String quote(String raw) {
if (raw == null) {
// Null string is treated like an empty string.
return QUOTE + QUOTE;
} else {
return new StringBuilder(QUOTE)
.append(JsonStringEncoder.getInstance().quoteAsString(raw))
.append(QUOTE).toString();
}
}
/**
* net.sf.json could parse a JSON string without throwing a checked
* exception. Make it so we can do the same with Jackson.
*/
public static JsonNode parseJson(String json) {
try {
return new ObjectMapper().readTree(json);
} catch (IOException e) {
throw new JacksonUtilsException(e);
}
}
/**
* net.sf.json provided this method (assuming that 'values' is an array of
* JSON objects that contain Strings.
*
* literalValues = (List<String>) JSONSerializer.toJava(values);
*
* So here is a replacement for that.
*/
public static List<String> jsonArrayToStrings(ArrayNode values) {
List<String> strings = new ArrayList<>();
for (JsonNode node : values) {
strings.add(node.asText());
}
return strings;
}
public static class JacksonUtilsException extends RuntimeException {
public JacksonUtilsException() {
super();
}
public JacksonUtilsException(String message, Throwable cause) {
super(message, cause);
}
public JacksonUtilsException(String message) {
super(message);
}
public JacksonUtilsException(Throwable cause) {
super(cause);
}
}
}

View file

@ -11,7 +11,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.RDFNode;