use of org.dom4j.Node in project application by collectionspace.
the class ServicesRelationStorage method getPathsJSON.
@Override
@SuppressWarnings("unchecked")
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
JSONObject out = new JSONObject();
Boolean isHierarchical = false;
if (isPathType(rootPath, new String[] { "main" }, 0)) {
extractPaths(rootPath, new String[] { "main" }, 0);
} else if (isPathType(rootPath, new String[] { "hierarchical" }, 0)) {
extractPaths(rootPath, new String[] { "hierarchical" }, 0);
isHierarchical = true;
}
try {
JSONObject moredata = new JSONObject();
List<String> list = new ArrayList<String>();
ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, "/relations?" + searchPath(restrictions), null, creds, cache);
Document doc = data.getDocument();
if (doc == null)
throw new UnderlyingStorageException("Could not retrieve relation, missing relations_common");
JSONObject pagination = new JSONObject();
String xmlroot = "relations-common-list";
List<Node> nodes = doc.getDocument().selectNodes("/" + xmlroot + "/*");
for (Node node : nodes) {
if ("relation-list-item".equals(node.getName())) {
// if(post_filter(creds,cache,restrictions,node))
list.add(node.selectSingleNode("csid").getText());
if (isHierarchical) {
JSONObject hdata = new JSONObject();
Node subjectNode = node.selectSingleNode("subject");
Node objectNode = node.selectSingleNode("object");
hdata.put("subjecturi", subjectNode.selectSingleNode("uri").getText());
hdata.put("objecturi", objectNode.selectSingleNode("uri").getText());
hdata.put("subjectcsid", subjectNode.selectSingleNode("csid").getText());
hdata.put("objectcsid", objectNode.selectSingleNode("csid").getText());
findNameUnderNode(hdata, "subjectname", "subjectrefname", subjectNode);
findNameUnderNode(hdata, "objectname", "objectrefname", objectNode);
hdata.put("type", node.selectSingleNode("predicate").getText());
hdata.put("csid", node.selectSingleNode("csid").getText());
moredata.put(node.selectSingleNode("csid").getText(), hdata);
}
} else {
pagination.put(node.getName(), node.getText());
}
}
out.put("pagination", pagination);
out.put("listItems", list.toArray(new String[0]));
out.put("moredata", moredata);
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl());
} catch (JSONException e) {
throw new UnderlyingStorageException("Could not retrieve relation", e);
}
}
use of org.dom4j.Node in project application by collectionspace.
the class ServicesRelationStorage method getPaths.
@Override
@SuppressWarnings("unchecked")
public String[] getPaths(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
extractPaths(rootPath, new String[] { "main" }, 0);
try {
List<String> out = new ArrayList<String>();
ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, "/relations/" + searchPath(restrictions), null, creds, cache);
Document doc = data.getDocument();
if (doc == null)
throw new UnderlyingStorageException("Could not retrieve relation, missing relations_common");
List<Node> objects = doc.getDocument().selectNodes("relations-common-list/relation-list-item");
for (Node object : objects) {
if (post_filter(creds, cache, restrictions, object))
out.add(object.selectSingleNode("csid").getText());
}
return out.toArray(new String[0]);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage());
}
}
use of org.dom4j.Node in project application by collectionspace.
the class ServicesRelationStorage method findNameUnderNode.
private void findNameUnderNode(JSONObject out, String nameKey, String refNameKey, Node itemNode) throws JSONException {
// Look for something to put into the subjectname. Start with refName,
// then name, then number
Node itemRefName = itemNode.selectSingleNode("refName");
String nameValue = null;
if (itemRefName != null) {
String refNameValue = itemRefName.getText();
out.put(refNameKey, refNameValue);
RefName.AuthorityItem item = RefName.AuthorityItem.parse(refNameValue);
if (item != null) {
nameValue = item.displayName;
} else {
RefName.Authority authority = RefName.Authority.parse(refNameValue);
if (authority != null) {
nameValue = authority.displayName;
}
}
}
// If no displayName from refName, then try name element
if (nameValue == null) {
Node itemNameNode = itemNode.selectSingleNode("name");
if (itemNameNode != null) {
nameValue = itemNameNode.getText();
}
}
// Still nothing? try number element
if (nameValue == null) {
Node itemNumberNode = itemNode.selectSingleNode("number");
if (itemNumberNode != null) {
nameValue = itemNumberNode.getText();
}
}
if (nameValue == null) {
nameValue = "MISSING DATA";
}
out.put(nameKey, nameValue);
}
use of org.dom4j.Node in project application by collectionspace.
the class UserStorage method getPaths.
@Override
@SuppressWarnings("unchecked")
public String[] getPaths(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
List<String> out = new ArrayList<String>();
Iterator rit = restrictions.keys();
StringBuffer args = new StringBuffer();
while (rit.hasNext()) {
String key = (String) rit.next();
FieldSet fs = r.getFieldTopLevel(key);
if (!(fs instanceof Field))
continue;
String filter = ((Field) fs).getServicesFilterParam();
if (filter == null)
continue;
args.append('&');
args.append(filter);
args.append('=');
args.append(URLEncoder.encode(restrictions.getString(key), "UTF-8"));
}
// pagination
String tail = args.toString();
String path = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), tail, false, "");
ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
if (doc.getStatus() < 200 || doc.getStatus() > 399)
throw new UnderlyingStorageException("Cannot retrieve account list", doc.getStatus(), path);
Document list = doc.getDocument();
List<Node> objects = list.selectNodes(r.getServicesListPath());
for (Node object : objects) {
List<Node> fields = object.selectNodes("*");
String csid = object.selectSingleNode("csid").getText();
for (Node field : fields) {
if ("csid".equals(field.getName())) {
int idx = csid.lastIndexOf("/");
if (idx != -1)
csid = csid.substring(idx + 1);
out.add(csid);
} else if ("uri".equals(field.getName())) {
// Skip!
} else {
String json_name = view_map.get(field.getName());
if (json_name != null) {
String value = field.getText();
// XXX hack to cope with multi values
if (value == null || "".equals(value)) {
List<Node> inners = field.selectNodes("*");
for (Node n : inners) {
value += n.getText();
}
}
setGleanedValue(cache, r.getServicesURL() + "/" + csid, json_name, value);
}
}
}
}
return out.toArray(new String[0]);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (UnsupportedEncodingException e) {
throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
}
}
use of org.dom4j.Node in project application by collectionspace.
the class ConfiguredVocabStorage method getPathsJSON.
/**
* Returns JSON containing pagenumber, pagesize, itemsinpage, totalitems and the list of items itself
*/
@Override
@SuppressWarnings("unchecked")
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
JSONObject out = new JSONObject();
List<String> list = new ArrayList<String>();
String url;
if (rootPath.isEmpty()) {
url = "/" + r.getServicesURL() + ALL_VOCAB_ITEMS;
} else {
String vocab = RefName.shortIdToPath(rootPath);
url = "/" + r.getServicesURL() + "/" + vocab + ITEMS_SUFFIX;
}
String path = getRestrictedPath(url, restrictions, r.getServicesSearchKeyword(), "", true, getDisplayNameKey());
boolean excludeSoftDeleted = true;
if (restrictions.has("deleted")) {
excludeSoftDeleted = !restrictions.getBoolean("deleted");
}
if (excludeSoftDeleted && r.hasSoftDeleteMethod()) {
path = softpath(path);
}
ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
Document doc = data.getDocument();
if (doc == null) {
throw new UnderlyingStorageException("Could not retrieve vocabulary items", data.getStatus(), path);
}
String[] tag_parts = r.getServicesListPath().split(",", 2);
String listItemPath = tag_parts[1];
String[] listItemPathElements = listItemPath.split("/");
if (listItemPathElements.length != 2) {
throw new RuntimeException("Illegal list item path " + listItemPath);
}
String listNodeName = listItemPathElements[0];
String listItemNodeName = listItemPathElements[1];
String listNodeChildrenSelector = "/" + listNodeName + "/*";
JSONObject pagination = new JSONObject();
String[] allfields = null;
String fieldsReturnedName = r.getServicesFieldsPath();
List<Node> nodes = doc.selectNodes(listNodeChildrenSelector);
for (Node node : nodes) {
if (listItemNodeName.equals(node.getName())) {
// Risky hack - assumes displayName must be at root. Really should
// understand that the list results are a different schema from record GET.
String dnName = getDisplayNameKey();
String csid = node.selectSingleNode("csid").getText();
list.add(csid);
String urlPlusCSID = url + "/" + csid;
List<Node> nameNodes = node.selectNodes(dnName);
String nameListValue = null;
for (Node nameNode : nameNodes) {
String name = nameNode.getText();
if (nameListValue == null) {
nameListValue = name;
} else {
nameListValue = JSONUtils.appendWithArraySeparator(nameListValue, name);
}
}
if (nameListValue == null) {
throw new JSONException("No displayNames found!");
} else {
String json_name = view_map.get(dnName);
setGleanedValue(cache, urlPlusCSID, json_name, nameListValue);
}
List<Node> fields = node.selectNodes("*[(name()!='" + dnName + "')]");
for (Node field : fields) {
String json_name = view_map.get(field.getName());
if (json_name != null) {
String value = field.getText();
// XXX hack to cope with multi values
if (value == null || "".equals(value)) {
List<Node> inners = field.selectNodes("*");
for (Node n : inners) {
value += n.getText();
}
}
setGleanedValue(cache, urlPlusCSID, json_name, value);
}
}
if (allfields == null || allfields.length == 0) {
log.warn("Missing fieldsReturned value - may cause fan-out!");
} else {
// Mark all the fields not yet found as gleaned -
for (String s : allfields) {
String gleaned = getGleanedValue(cache, urlPlusCSID, s);
if (gleaned == null) {
setGleanedValue(cache, urlPlusCSID, s, "");
}
}
}
} else if (fieldsReturnedName.equals(node.getName())) {
String myfields = node.getText();
allfields = myfields.split("\\|");
} else {
pagination.put(node.getName(), node.getText());
}
}
out.put("pagination", pagination);
out.put("listItems", list.toArray(new String[0]));
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (UnsupportedEncodingException e) {
throw new UnderlyingStorageException("UTF-8 not supported!?" + e.getLocalizedMessage());
} catch (JSONException e) {
throw new UnderlyingStorageException("Error parsing JSON" + e.getLocalizedMessage());
}
}
Aggregations