use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class GenericStorage method simpleRetrieveJSON.
/**
* return data just as the service layer gives it to the App layer
* no extra columns required
* @param creds
* @param cache
* @param filePath
* @param servicesurl
* @param thisr
* @return
* @throws ExistException
* @throws UnimplementedException
* @throws UnderlyingStorageException
*/
public JSONObject simpleRetrieveJSON(CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String servicesurl, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException {
String csid = "";
if (filePath == null) {
filePath = "";
}
String[] path_parts = filePath.split("/");
if (path_parts.length > 1)
csid = path_parts[1];
else
csid = filePath;
JSONObject out = new JSONObject();
try {
String softpath = filePath;
if (thisr.hasSoftDeleteMethod()) {
softpath = softpath(filePath);
}
if (thisr.hasHierarchyUsed("screen")) {
softpath = hierarchicalpath(softpath);
}
if (thisr.isMultipart()) {
ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
for (String section : thisr.getServicesRecordPathKeys()) {
String path = thisr.getServicesRecordPath(section);
String[] parts = path.split(":", 2);
if (doc.getDocument(parts[0]) != null) {
convertToJson(out, doc.getDocument(parts[0]), thisr, "GET", section, csid);
}
}
// If this record has hierarchy, will pull out the relations section and map it to the hierarchy
// fields (special case handling of XML-JSON
handleHierarchyPayloadRetrieve(thisr, doc, out, csid);
} else {
ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
convertToJson(out, doc.getDocument(), thisr, "GET", "common", csid);
}
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Service layer exception", e);
}
/*
* Get data for any sub records that are part of this record e.g. contact in person, blob in media
*/
try {
for (FieldSet fs : thisr.getAllSubRecords("GET")) {
Boolean validator = true;
Record sr = fs.usesRecordId();
if (fs.usesRecordValidator() != null) {
validator = false;
if (out.has(fs.usesRecordValidator())) {
String test = out.getString(fs.usesRecordValidator());
if (test != null && !test.equals("")) {
validator = true;
}
}
}
if (validator) {
String getPath = servicesurl + filePath + "/" + sr.getServicesURL();
if (null != fs.getServicesUrl()) {
getPath = fs.getServicesUrl();
}
if (fs.getWithCSID() != null) {
getPath = getPath + "/" + out.getString(fs.getWithCSID());
}
// need to get update and delete working? tho not going to be used for media as handling blob seperately
if (fs instanceof Group) {
JSONObject outer = simpleRetrieveJSON(creds, cache, getPath, "", sr);
JSONArray group = new JSONArray();
group.put(outer);
out.put(fs.getID(), group);
}
if (fs instanceof Repeat) {
// NEED TO GET A LIST OF ALL THE THINGS
JSONArray repeat = new JSONArray();
String path = getPath;
while (!path.equals("")) {
JSONObject data = getListView(creds, cache, path, sr.getServicesListPath(), "csid", false, r);
if (data.has("listItems")) {
String[] results = (String[]) data.get("listItems");
for (String result : results) {
JSONObject rout = simpleRetrieveJSON(creds, cache, getPath + "/" + result, "", sr);
// add in csid so I can do update with a modicum of confidence
rout.put("_subrecordcsid", result);
repeat.put(rout);
}
}
if (data.has("pagination")) {
Integer ps = Integer.valueOf(data.getJSONObject("pagination").getString("pageSize"));
Integer pn = Integer.valueOf(data.getJSONObject("pagination").getString("pageNum"));
Integer ti = Integer.valueOf(data.getJSONObject("pagination").getString("totalItems"));
if (ti > (ps * (pn + 1))) {
JSONObject restrictions = new JSONObject();
restrictions.put("pageSize", Integer.toString(ps));
restrictions.put("pageNum", Integer.toString(pn + 1));
path = getRestrictedPath(getPath, restrictions, sr.getServicesSearchKeyword(), "", false, "");
// need more values
} else {
path = "";
}
}
}
// group.put(outer);
out.put(fs.getID(), repeat);
}
}
}
} catch (Exception e) {
// ignore exceptions for sub records at the moment - make it more intelligent later
// throw new UnderlyingStorageException("Service layer exception",e);
}
return out;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class GenericStorage method autocreateJSON.
/**
* Convert the JSON from the UI Layer into XML for the Service layer while using the XML structure from cspace-config.xml
* Send the XML through to the Service Layer to store it in the database
* The Service Layer returns a url to the object we just stored.
* @param {ContextualisedStorage} root
* @param {CSPRequestCredentials} creds
* @param {CSPRequestCache} cache
* @param {String} filePath part of the path to the Service URL (containing the type of object)
* @param {JSONObject} jsonObject The JSON string coming in from the UI Layer, containing the object to be stored
* @return {String} csid The id of the object in the database
*/
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
ReturnedURL url = null;
Document doc = null;
// used by userroles and permroles as they have complex urls
if (r.hasPrimaryField()) {
for (String section : r.getServicesRecordPathKeys()) {
doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "POST");
String path = r.getServicesURL();
path = path.replace("*", getSubCsid(jsonObject, r.getPrimaryField()));
String restrictedPath = getRestrictedPath(path, restrictions, null);
deleteJSON(root, creds, cache, restrictedPath);
url = conn.getURL(RequestMethod.POST, restrictedPath, doc, creds, cache);
}
} else {
String restrictedPath = getRestrictedPath(r.getServicesURL(), restrictions, null);
// REM - We need a way to send query params (restrictions) on POST and UPDATE
url = autoCreateSub(creds, cache, jsonObject, doc, restrictedPath, r);
}
// I am developing this.. it might not work...
for (FieldSet fs : r.getAllSubRecords("POST")) {
Record sr = fs.usesRecordId();
// sr.getID()
if (sr.isType("authority")) {
// need to use code from configuredVocabStorage
} else {
String savePath = url.getURL() + "/" + sr.getServicesURL();
if (fs instanceof Field) {
// get the fields form inline XXX untested - might not work...
JSONObject subdata = new JSONObject();
// loop thr jsonObject and find the fields I need
for (FieldSet subfs : sr.getAllFieldTopLevel("POST")) {
String key = subfs.getID();
if (jsonObject.has(key)) {
subdata.put(key, jsonObject.get(key));
}
}
subautocreateJSON(root, creds, cache, sr, subdata, savePath);
} else if (fs instanceof Group) {
// JSONObject
if (jsonObject.has(fs.getID())) {
Object subdata = jsonObject.get(fs.getID());
if (subdata instanceof JSONObject) {
JSONObject subrecord = (JSONObject) subdata;
subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
}
}
} else {
// JSONArray
if (jsonObject.has(fs.getID())) {
Object subdata = jsonObject.get(fs.getID());
if (subdata instanceof JSONArray) {
JSONArray subarray = (JSONArray) subdata;
for (int i = 0; i < subarray.length(); i++) {
JSONObject subrecord = subarray.getJSONObject(i);
subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
}
}
}
}
}
}
return url.getURLTail();
} catch (ConnectionException e) {
String msg = e.getMessage();
if (e.getStatus() == 403) {
// permissions error
msg += " permissions error";
}
throw new UnderlyingStorageException(msg, e.getStatus(), e.getUrl(), e);
} catch (UnderlyingStorageException e) {
// REM - CSPACE-5632: Need to catch and rethrow this exception type to prevent throwing an "UnimplementedException" exception below.
throw e;
} catch (Exception e) {
throw new UnimplementedException("JSONException", e);
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class GenericStorage method getPathsJSON.
/**
* Gets a list of csids of a certain type of record together with the pagination info
*/
@Override
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
FieldSet dispNameFS = r.getDisplayNameField();
String displayNameFieldID;
boolean supportsTermCompletion;
if (dispNameFS != null) {
displayNameFieldID = dispNameFS.getID();
supportsTermCompletion = true;
} else {
displayNameFieldID = "";
supportsTermCompletion = false;
}
String path = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), "", supportsTermCompletion, displayNameFieldID);
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("Service layer exception", e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Service layer exception", e);
}
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class GenericStorage method getHardListView.
/**
* return list view of items
* TODO make getHardListView and getRepeatableHardListView to share more code as they aren't different enough to warrant the level of code repeat
* @param creds
* @param cache
* @param path
* @param nodeName
* @param matchlistitem
* @param csidfield
* @param fullcsid
* @return
* @throws ConnectionException
* @throws JSONException
*/
protected JSONObject getHardListView(CSPRequestCredentials creds, CSPRequestCache cache, String path, String listItemPath, String csidfield, Boolean fullcsid) throws ConnectionException, JSONException {
String[] listItemPathElements = listItemPath.split("/");
if (listItemPathElements.length != 2) {
throw new IllegalArgumentException("Illegal list item path " + listItemPath);
}
String listNodeName = listItemPathElements[0];
String listItemNodeName = listItemPathElements[1];
String listNodeChildrenSelector = "/" + listNodeName + "/*";
JSONObject out = new JSONObject();
JSONObject pagination = new JSONObject();
Document list = null;
List<String> listitems = new ArrayList<String>();
ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
if (all.getStatus() != 200) {
// throw new StatusException(all.getStatus(),path,"Bad request during identifier cache map update: status not 200");
throw new ConnectionException("Bad request during identifier cache map update: status not 200 is " + Integer.toString(all.getStatus()), all.getStatus(), path);
}
list = all.getDocument();
List<Node> nodes = list.selectNodes(listNodeChildrenSelector);
if (listNodeName.equals("roles_list") || listNodeName.equals("permissions_list")) {
// XXX CSPACE-1887 workaround
for (Node node : nodes) {
if (listItemNodeName.equals(node.getName())) {
String csid = node.valueOf("@csid");
listitems.add(csid);
} else {
pagination.put(node.getName(), node.getText());
}
}
} else {
String[] allfields = null;
String fieldsReturnedName = r.getServicesFieldsPath();
for (Node node : nodes) {
if (listItemNodeName.equals(node.getName())) {
List<Node> fields = node.selectNodes("*");
String csid = "";
String extraFieldValue = "";
String urlPlusCSID = null;
if (node.selectSingleNode(csidfield) != null) {
csid = node.selectSingleNode(csidfield).getText();
urlPlusCSID = r.getServicesURL() + "/" + csid;
setGleanedValue(cache, urlPlusCSID, "csid", csid);
}
for (Node field : fields) {
if (csidfield.equals(field.getName())) {
if (!fullcsid) {
int idx = csid.lastIndexOf("/");
if (idx != -1) {
csid = csid.substring(idx + 1);
urlPlusCSID = r.getServicesURL() + "/" + csid;
}
}
listitems.add(csid);
} 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, urlPlusCSID, json_name, value);
}
}
}
if (allfields == null || allfields.length == 0) {
if (log.isWarnEnabled()) {
log.warn("getHardListView(): Missing fieldsReturned value - may cause fan-out!\nRecord:" + r.getID() + " request to: " + path);
}
} 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", listitems.toArray(new String[0]));
return out;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ConnectionException in project application by collectionspace.
the class GenericStorage method refObjViewRetrieveJSON.
/**
* get data needed for list of objects related to a termUsed
* @param storage
* @param creds
* @param cache
* @param path
* @return
* @throws ExistException
* @throws UnderlyingStorageException
* @throws JSONException
* @throws UnimplementedException
*/
public JSONObject refObjViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String path, JSONObject restrictions, Record vr) throws ExistException, UnderlyingStorageException, JSONException, UnimplementedException {
JSONObject out = new JSONObject();
try {
// map of servicenames of fields to descriptors
Map<String, String> refObj_view_good = new HashMap<String, String>();
// map of csid to service name of field
Map<String, String> refObj_view_map = new HashMap<String, String>();
if (vr.hasRefObjUsed()) {
path = getRestrictedPath(path, restrictions, "kw", "", false, "");
// XXX need a way to append the data needed from the field,
// which we don't know until after we have got the information...
refObj_view_map.put("docType", "docType");
refObj_view_map.put("docId", "docId");
refObj_view_map.put("docName", "docName");
refObj_view_map.put("docNumber", "docNumber");
refObj_view_map.put("sourceField", "sourceField");
refObj_view_map.put("uri", "uri");
refObj_view_map.put("refName", "refName");
refObj_view_good.put("terms_docType", "docType");
refObj_view_good.put("terms_docId", "docId");
refObj_view_good.put("terms_docName", "docName");
refObj_view_good.put("terms_docNumber", "docNumber");
refObj_view_good.put("terms_sourceField", "sourceField");
refObj_view_good.put("terms_refName", "refName");
// XXX this might be the wrong record to pass to checkf or hard/soft delet listing
JSONObject data = getRepeatableListView(storage, creds, cache, path, "authority-ref-doc-list/authority-ref-doc-item", "uri", true, vr, refObj_view_map);
JSONArray recs = data.getJSONArray("listItems");
if (data.has("pagination")) {
out.put("pagination", data.getJSONObject("pagination"));
}
JSONArray items = new JSONArray();
// String[] filepaths = (String[]) data.get("listItems");
for (int i = 0; i < recs.length(); ++i) {
String uri = recs.getJSONObject(i).getString("csid");
// recs.getJSONObject(i).getString("csid");
String filePath = uri;
if (filePath != null && filePath.startsWith("/"))
filePath = filePath.substring(1);
String[] parts = filePath.split("/");
String recordurl = parts[0];
Record thisr = vr.getSpec().getRecordByServicesUrl(recordurl);
// Set up the glean maps required for this record. We need to reset these each time
// through the loop, because every record could be a different type.
Map<String, String> thisr_view_good = new HashMap<String, String>(refObj_view_good);
Map<String, String> thisr_view_map = new HashMap<String, String>(refObj_view_map);
Set<String> thisr_xxx_view_deurn = new HashSet<String>();
Set<String> thisr_view_search_optional = new HashSet<String>();
Map<String, List<String>> thisr_view_merge = new HashMap<String, List<String>>();
Map<String, List<String>> thisr_view_useCsid = new HashMap<String, List<String>>();
initializeGlean(thisr, thisr_view_good, thisr_view_map, thisr_xxx_view_deurn, thisr_view_search_optional, thisr_view_merge, thisr_view_useCsid);
String csid = parts[parts.length - 1];
JSONObject dataitem = miniViewRetrieveJSON(cache, creds, csid, "terms", uri, thisr, thisr_view_good, thisr_xxx_view_deurn, thisr_view_search_optional, thisr_view_merge, thisr_view_useCsid);
dataitem.getJSONObject("summarylist").put("uri", filePath);
String key = recs.getJSONObject(i).getString("sourceField");
dataitem.getJSONObject("summarylist").put("sourceField", key);
String fieldName = "unknown";
String fieldSelector = "unknown";
if (key.contains(":")) {
fieldName = key.split(":")[1];
// FIXME: We might remove the following if CSPACE-2909's fix makes this moot - ADR 2012-07-19
while (thisr.getFieldFullList(fieldName) instanceof Repeat || thisr.getFieldFullList(fieldName) instanceof Group) {
fieldName = ((Repeat) thisr.getFieldFullList(fieldName)).getChildren("GET")[0].getID();
}
Field fieldinstance = (Field) thisr.getFieldFullList(fieldName);
fieldSelector = fieldinstance.getSelector();
}
dataitem.put("csid", csid);
dataitem.put("sourceFieldselector", fieldSelector);
dataitem.put("sourceFieldName", fieldName);
dataitem.put("sourceFieldType", dataitem.getJSONObject("summarylist").getString("docType"));
dataitem.put("sourceFieldType", dataitem.getJSONObject("summarylist").getString("docType"));
// items.put(csid+":"+key,dataitem);
items.put(dataitem);
}
out.put("items", items);
}
return out;
} catch (ConnectionException e) {
log.error("failed to retrieve refObjs for " + path);
JSONObject dataitem = new JSONObject();
dataitem.put("csid", "");
dataitem.put("sourceFieldselector", "Functionality Failed");
dataitem.put("sourceFieldName", "Functionality Failed");
dataitem.put("sourceFieldType", "Functionality Failed");
dataitem.put("message", e.getMessage());
out.put("Functionality Failed", dataitem);
// return out;
throw new UnderlyingStorageException("Connection problem" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (UnsupportedEncodingException uae) {
log.error("failed to retrieve refObjs for " + path);
JSONObject dataitem = new JSONObject();
dataitem.put("message", uae.getMessage());
out.put("Functionality Failed", dataitem);
throw new UnderlyingStorageException("Problem building query" + uae.getLocalizedMessage(), uae);
}
}
Aggregations