use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class UserStorage method getPathsJSON.
@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> listitems = 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, "");
if (r.hasSoftDeleteMethod()) {
path = softpath(path);
}
if (r.hasHierarchyUsed("screen")) {
path = hierarchicalpath(path);
}
JSONObject data = getListView(creds, cache, path, r.getServicesListPath(), "csid", false, r);
return data;
} 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.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class ConfiguredVocabStorage method transitionWorkflowJSON.
@Override
public void transitionWorkflowJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String serviceurl, String workflowTransition) throws UnderlyingStorageException {
String vocab = RefName.shortIdToPath(filePath.split("/")[0]);
String url = null;
try {
url = generateURL(vocab, filePath.split("/")[1], "", this.r);
} catch (ExistException e) {
throw new UnderlyingStorageException("Exist exception" + e.getLocalizedMessage(), e.getStatus(), url, e);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
}
super.transitionWorkflowJSON(root, creds, cache, "", url, workflowTransition);
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException 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());
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class ConfiguredVocabStorage method miniViewAbstract.
@Override
protected JSONObject miniViewAbstract(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, JSONObject out, String servicepath, String filePath) throws UnderlyingStorageException {
try {
// actually use cache
String cachelistitem = "/" + servicepath;
if (filePath != null) {
cachelistitem = cachelistitem + "/" + filePath;
}
if (!cachelistitem.startsWith("/")) {
cachelistitem = "/" + cachelistitem;
}
String dnName = getDisplayNameKey();
String g1 = getGleanedValue(cache, cachelistitem, "refName");
String g2 = getGleanedValue(cache, cachelistitem, "shortIdentifier");
String g3 = getGleanedValue(cache, cachelistitem, dnName);
String g4 = getGleanedValue(cache, cachelistitem, "csid");
String g5 = getGleanedValue(cache, cachelistitem, "termStatus");
String g6 = getGleanedValue(cache, cachelistitem, "workflow");
if (g1 == null || g2 == null || g3 == null || g4 == null || g5 == null) {
if (log.isWarnEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("ConfiguredVocabStorage fanning out ");
if (g2 != null) {
sb.append("(shId:");
sb.append(g2);
sb.append(")");
}
if (g4 != null) {
sb.append("(csid:");
sb.append(g4);
sb.append(")");
}
sb.append(", as could not get: ");
if (g1 == null)
sb.append("refName,");
if (g2 == null)
sb.append("shortIdentifier,");
if (g3 == null)
sb.append("dnName,");
if (g4 == null)
sb.append("csid,");
if (g5 == null)
sb.append("termStatus,");
log.warn(sb.toString());
}
JSONObject cached = get(storage, creds, cache, servicepath, filePath);
g1 = cached.getString("refid");
g2 = cached.getString("shortIdentifier");
g3 = cached.getString(dnName);
g4 = cached.getString("csid");
g5 = cached.getString("termStatus");
}
out.put(dnName, g3);
out.put("refid", g1);
out.put("csid", g4);
out.put("termStatus", g5);
out.put("workflow", g6);
// out.put("authorityid", cached.get("authorityid"));
out.put("shortIdentifier", g2);
out.put("recordtype", r.getWebURL());
RefName.AuthorityItem item = RefName.AuthorityItem.parse(g1);
out.put("namespace", item.getParentShortIdentifier());
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (ExistException e) {
throw new UnderlyingStorageException("ExistException exception" + e.getLocalizedMessage(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("JSONException exception" + e.getLocalizedMessage(), e);
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class ConfiguredVocabStorage method retrieveJSON.
@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
Integer num = 0;
String[] parts = filePath.split("/");
// deal with different url structures
String vocab, csid;
if ("_direct".equals(parts[0])) {
vocab = parts[2];
csid = parts[3];
num = 4;
} else {
vocab = parts[0];
if (!VOCAB_WILDCARD.equals(vocab)) {
vocab = RefName.shortIdToPath(vocab);
}
csid = (parts.length > 1) ? parts[1] : null;
num = 2;
}
if (parts.length > num) {
String extra = "";
Integer extradata = num + 1;
if (parts.length > extradata) {
extra = parts[extradata];
}
String servicepath = generateURL(vocab, csid, "", this.r);
return viewRetrieveJSON(root, creds, cache, null, parts[num], extra, restrictions, servicepath);
} else
return simpleRetrieveJSON(root, creds, cache, vocab, csid);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException x) {
throw new UnderlyingStorageException("Error building JSON" + x.getLocalizedMessage(), x);
} catch (UnsupportedEncodingException x) {
throw new UnderlyingStorageException("Error UnsupportedEncodingException JSON", x);
}
}
Aggregations