use of org.collectionspace.csp.api.persistence.UnderlyingStorageException 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.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class ServicesRelationStorage method updateJSON.
@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject data, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
String[] parts = extractPaths(filePath, new String[] { "main" }, 1);
Map<String, Document> in = new HashMap<String, Document>();
Document datapath = dataToRelation(cache, parts[0], data).toDocument();
in.put("relations_common", datapath);
// log.info("UPDATE"+datapath.asXML());
// log.info("UPDATE"+"/relations/"+parts[0]);
ReturnedMultipartDocument out = conn.getMultipartXMLDocument(RequestMethod.PUT, "/relations/" + parts[0], in, creds, cache);
if (out.getStatus() == 404)
throw new ExistException("Not found");
if (out.getStatus() > 299)
throw new UnderlyingStorageException("Could not update relation", out.getStatus(), "/relations/" + parts[0]);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Could not update relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Could not retrieve data" + e.getLocalizedMessage(), e);
}
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class ServicesRelationStorage method splitTypeFromId.
private String[] splitTypeFromId(String path) throws UnderlyingStorageException {
String[] out = path.split("/");
if (out[0].equals("")) {
path = path.substring(1);
out = path.split("/");
}
if (out.length != 2)
throw new UnderlyingStorageException("Path must be two components, not " + path);
return out;
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException 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.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class ServicesRelationStorage method dataToRelation.
private Relation dataToRelation(CSPRequestCache cache, String id, JSONObject data) throws JSONException, UnderlyingStorageException {
String[] src = splitTypeFromId(data.getString("src"));
String[] dst = splitTypeFromId(data.getString("dst"));
if (type_to_surl.containsKey(src[0])) {
src[0] = type_to_surl.get(src[0]);
}
if (type_to_surl.containsKey(dst[0])) {
dst[0] = type_to_surl.get(dst[0]);
}
String type = data.getString("type");
if (types.containsKey(type)) {
Relationship rel = types.get(type);
if (!rel.hasDestinationType(dst[0]) && !rel.hasDestinationType("all")) {
throw new UnderlyingStorageException("type " + type + " is undefined for destination:" + dst[0]);
}
if (!rel.hasSourceType("all") && !rel.hasSourceType(src[0])) {
throw new UnderlyingStorageException("type " + type + " is undefined for source: " + src[0]);
}
} else {
throw new UnderlyingStorageException("type " + type + " is undefined");
}
return factory.create(id, src[0], src[1], type, dst[0], dst[1]);
}
Aggregations