Some cleanup.

This commit is contained in:
runeliza 2011-07-13 14:40:14 +00:00
parent e5028dc668
commit 59b2d9431e

View file

@ -77,47 +77,10 @@ public class GrefineMqlreadServlet extends VitroHttpServlet {
try {
// parse query
ArrayList<String> subjectUriList = new ArrayList<String>();
Map<String, JSONArray> propertyUriMap = new HashMap<String, JSONArray>();
String query = vreq.getParameter("query");
System.out.println("query: " + query);
JSONObject rawJson = new JSONObject(query);
JSONArray qJsonArr = rawJson.getJSONArray("query");
ArrayList<String> subjectUriList = new ArrayList();
Map<String, JSONArray> propertyUriMap = new HashMap();
for (int i=0; i<qJsonArr.length(); i++) {
Object obj = qJsonArr.get(i);
if (obj instanceof JSONObject) {
JSONObject jsonObj = (JSONObject)obj;
JSONArray jsonObjNames = jsonObj.names();
for (int j=0; j<jsonObjNames.length(); j++) {
String objName = (String)jsonObjNames.get(j);
if (objName.contains("http://")) { // most likely this is a propertyUri
// e.g. http://weill.cornell.edu/vivo/ontology/wcmc#cwid
Object propertyUriObj = jsonObj.get(objName);
if (propertyUriObj instanceof JSONArray) {
propertyUriMap.put(objName, (JSONArray)propertyUriObj);
}
} else if ("id".equals(objName)) { // id
Object idObj = jsonObj.get(objName); // TODO: This is a String object but not sure what it is for
} else if ("id|=".equals(objName)) { // list of subject uri
Object subjectUriObj = jsonObj.get(objName);
if (subjectUriObj instanceof JSONArray) {
JSONArray subjectUriUriArr = (JSONArray)subjectUriObj;
for (int k=0; k<subjectUriUriArr.length(); k++) {
// e.g. http://vivo.med.cornell.edu/individual/cwid-jsd2002
Object subjectUriUriObj = subjectUriUriArr.get(k);
if (subjectUriUriObj instanceof String) {
subjectUriList.add((String)subjectUriUriObj);
}
}
}
}
}
}
}
parseQuery(query, subjectUriList, propertyUriMap);
// run SPARQL query to get the results
JSONArray resultAllJsonArr = new JSONArray();
@ -153,11 +116,58 @@ public class GrefineMqlreadServlet extends VitroHttpServlet {
resultAllJson.put("result", resultAllJsonArr);
} catch (JSONException e) {
log.error("JSONException: " + e);
throw new ServletException("GrefineMqlreadServlet JSONException: " + e);
log.error("GrefineMqlreadServlet getResult JSONException: " + e);
}
// System.out.println(resultAllJson);
return resultAllJson;
}
/**
* Construct json from query String
* @param query
* @param subjectUriList
* @param propertyUriMap
*/
private void parseQuery(String query, ArrayList<String> subjectUriList, Map<String, JSONArray> propertyUriMap) {
try {
JSONObject rawJson = new JSONObject(query);
JSONArray qJsonArr = rawJson.getJSONArray("query");
for (int i=0; i<qJsonArr.length(); i++) {
Object obj = qJsonArr.get(i);
if (obj instanceof JSONObject) {
JSONObject jsonObj = (JSONObject)obj;
JSONArray jsonObjNames = jsonObj.names();
for (int j=0; j<jsonObjNames.length(); j++) {
String objName = (String)jsonObjNames.get(j);
if (objName.contains("http://")) { // most likely this is a propertyUri
// e.g. http://weill.cornell.edu/vivo/ontology/wcmc#cwid
Object propertyUriObj = jsonObj.get(objName);
if (propertyUriObj instanceof JSONArray) {
propertyUriMap.put(objName, (JSONArray)propertyUriObj);
}
} else if ("id".equals(objName)) { // id
Object idObj = jsonObj.get(objName); // TODO: This is a String object but not sure what it is for
} else if ("id|=".equals(objName)) { // list of subject uri
Object subjectUriObj = jsonObj.get(objName);
if (subjectUriObj instanceof JSONArray) {
JSONArray subjectUriUriArr = (JSONArray)subjectUriObj;
for (int k=0; k<subjectUriUriArr.length(); k++) {
// e.g. http://vivo.med.cornell.edu/individual/cwid-jsd2002
Object subjectUriUriObj = subjectUriUriArr.get(k);
if (subjectUriUriObj instanceof String) {
subjectUriList.add((String)subjectUriUriObj);
}
}
}
}
}
}
}
} catch (JSONException e) {
log.error("GrefineMqlreadServlet parseQuery JSONException: " + e);
}
}
}