use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument 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.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class ServicesRelationStorage method post_filter.
// Needed because of CSPACE-1080
// XXX is this still needed?CSPACE-1080 has been resolved...
private boolean post_filter(CSPRequestCredentials creds, CSPRequestCache cache, JSONObject restrictions, Node candidate) throws ExistException, UnderlyingStorageException, ConnectionException, JSONException {
if (restrictions == null)
return true;
// Subject
String src_csid = candidate.selectSingleNode("subjectCsid").getText();
String rest_src = restrictions.optString("src");
if (rest_src != null && !"".equals(rest_src)) {
String[] data = rest_src.split("/");
if (data[0].equals("")) {
rest_src = rest_src.substring(1);
data = rest_src.split("/");
}
if (!src_csid.equals(rest_src.split("/")[1]))
return false;
}
String dst_csid = candidate.selectSingleNode("objectCsid").getText();
String rest_dst = restrictions.optString("dst");
if (rest_dst != null && !"".equals(rest_dst)) {
String[] data2 = rest_dst.split("/");
if (data2[0].equals("")) {
rest_dst = rest_dst.substring(1);
data2 = rest_dst.split("/");
}
if (!dst_csid.equals(rest_dst.split("/")[1]))
return false;
}
// Retrieve the relation (CSPACE-1081)
ReturnedMultipartDocument rel = conn.getMultipartXMLDocument(RequestMethod.GET, candidate.selectSingleNode("uri").getText(), null, creds, cache);
if (rel.getStatus() == 404)
throw new ExistException("Not found");
Document rel_doc = rel.getDocument("relations_common");
if (rel_doc == null)
throw new UnderlyingStorageException("Could not retrieve relation, missing relations_common");
String type = rel_doc.selectSingleNode("relations_common/relationshipType").getText();
if (restrictions.has("type") && !type.equals(restrictions.optString("type")))
return false;
return true;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class ConfiguredVocabStorage method getInstance.
private JSONObject getInstance(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String vocab, String ims_url) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
String url = this.r.getServicesURL() + "/" + vocab;
String csid = "";
JSONObject out = new JSONObject();
String softurl = url;
if (r.hasSoftDeleteMethod()) {
softurl = softpath(url);
}
if (r.hasHierarchyUsed("screen")) {
softurl = hierarchicalpath(softurl);
}
ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, softurl, null, creds, cache);
if (doc.getStatus() == 404) {
throw new ExistException("Does not exist " + softurl);
}
if (doc.getStatus() == 403) {
// permission error
return out;
}
if (doc.getStatus() > 299) {
throw new UnderlyingStorageException("Could not retrieve vocabulary status=" + doc.getStatus(), doc.getStatus(), softurl);
}
String name = null;
String refid = null;
String shortIdentifier = "";
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesInstancesPath(section);
if (path == null) {
path = r.getServicesRecordPath(section);
}
String[] record_path = path.split(":", 2);
String[] tag_path = record_path[1].split(",", 2);
Document result = doc.getDocument(record_path[0]);
if ("common".equals(section)) {
// XXX hardwired :(
name = result.selectSingleNode(tag_path[1] + "/displayName").getText();
if (result.selectSingleNode(tag_path[1] + "/shortIdentifier") != null) {
shortIdentifier = result.selectSingleNode(tag_path[1] + "/shortIdentifier").getText();
}
refid = result.selectSingleNode(tag_path[1] + "/refName").getText();
csid = result.selectSingleNode(tag_path[1] + "/csid").getText();
} else if ("collectionspace_core".equals(section)) {
XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
}
}
out.put("displayName", name);
out.put("csid", csid);
out.put("refid", refid);
out.put("shortIdentifier", shortIdentifier);
return out;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class ConfiguredVocabStorage method get.
private JSONObject get(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String url, String ims_url) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
// int status=0;
String csid = "";
JSONObject out = new JSONObject();
// XXX pagination support
String softurl = url;
if (r.hasSoftDeleteMethod()) {
softurl = softpath(url);
}
if (r.hasHierarchyUsed("screen")) {
softurl = hierarchicalpath(softurl);
}
ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, softurl, null, creds, cache);
if (doc.getStatus() == 404)
throw new ExistException("Does not exist " + softurl);
if (doc.getStatus() == 403) {
// permission error - keep calm and carry on with what we can glean
out.put("displayName", getDisplayNameKey());
out.put("csid", csid);
out.put("recordtype", r.getWebURL());
return out;
}
if (doc.getStatus() > 299)
throw new UnderlyingStorageException("Could not retrieve vocabulary status=" + doc.getStatus(), doc.getStatus(), softurl);
String name = null;
String refid = null;
String termStatus = null;
String parentcsid = null;
String shortIdentifier = "";
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
String[] tag_path = record_path[1].split(",", 2);
Document result = doc.getDocument(record_path[0].trim());
if (result != null) {
if ("common".equals(section)) {
// XXX hardwired :(
String dnXPath = getDisplayNameXPath();
name = result.selectSingleNode(tag_path[1] + "/" + dnXPath).getText();
if (result.selectSingleNode(tag_path[1] + "/shortIdentifier") != null) {
shortIdentifier = result.selectSingleNode(tag_path[1] + "/shortIdentifier").getText();
}
refid = result.selectSingleNode(tag_path[1] + "/refName").getText();
// We need to replace this with the same model as for displayName
if (result.selectSingleNode(tag_path[1] + "/termStatus") != null) {
termStatus = result.selectSingleNode(tag_path[1] + "/termStatus").getText();
} else {
termStatus = "";
}
csid = result.selectSingleNode(tag_path[1] + "/csid").getText();
parentcsid = result.selectSingleNode(tag_path[1] + "/inAuthority").getText();
XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
} else {
XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
}
} else {
log.warn(String.format("XML Payload for '%s' was missing part '%s'.", url, record_path[0]));
}
}
// 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(r, doc, out, csid);
// get related sub records?
for (FieldSet fs : r.getAllSubRecords("GET")) {
Record sr = fs.usesRecordId();
// sr.getID()
if (sr.isType("authority")) {
String getPath = url + "/" + sr.getServicesURL();
JSONArray subout = get(storage, creds, cache, url, getPath, sr);
if (fs instanceof Field) {
JSONObject fielddata = subout.getJSONObject(0);
Iterator<String> rit = fielddata.keys();
while (rit.hasNext()) {
String key = rit.next();
out.put(key, fielddata.get(key));
}
} else if (fs instanceof Group) {
if (subout.length() > 0) {
out.put(fs.getID(), subout.getJSONObject(0));
}
} else {
out.put(fs.getID(), subout);
}
}
}
// csid = urn_processor.deconstructURN(refid,false)[4];
out.put(getDisplayNameKey(), name);
out.put("csid", csid);
out.put("refid", refid);
RefName.AuthorityItem item = RefName.AuthorityItem.parse(refid);
out.put("namespace", item.getParentShortIdentifier());
out.put("shortIdentifier", shortIdentifier);
out.put("termStatus", termStatus);
out.put("authorityid", parentcsid);
out.put("recordtype", r.getWebURL());
return out;
}
use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.
the class ConfiguredVocabStorage method updateJSON.
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, JSONObject jsonObject, JSONObject restrictions, Record thisr, String savePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
String csid = savePath.split("/")[3];
Map<String, Document> body = new HashMap<String, Document>();
for (String section : r.getServicesRecordPathKeys()) {
String path = r.getServicesRecordPath(section);
String[] record_path = path.split(":", 2);
String[] tag_path = record_path[1].split(",", 2);
Document temp = createEntry(section, tag_path[0], tag_path[1], jsonObject, null, null, thisr, false);
if (temp != null) {
body.put(record_path[0], temp);
// log.info(temp.asXML());
}
}
handleHierarchyPayloadSend(thisr, body, jsonObject, csid);
ReturnedMultipartDocument out = conn.getMultipartXMLDocument(RequestMethod.PUT, savePath, body, creds, cache);
if (out.isErrorStatus()) {
if (out.isTransactionFailedStatus()) {
throw new UnderlyingStorageException(VOCABULARY_UPDATE_FAILED_MESSAGE + ": " + out.TRANSACTION_FAILED_MESSAGE, out.getStatus(), savePath);
} else {
throw new UnderlyingStorageException(VOCABULARY_UPDATE_FAILED_MESSAGE, out.getStatus(), savePath);
}
}
// subrecord update
for (FieldSet fs : thisr.getAllSubRecords("PUT")) {
Record sr = fs.usesRecordId();
// get list of existing subrecords
JSONObject existingcsid = new JSONObject();
JSONObject updatecsid = new JSONObject();
JSONArray createcsid = new JSONArray();
String getPath = savePath + "/" + sr.getServicesURL();
Integer subcount = 0;
String firstfile = "";
while (!getPath.equals("")) {
JSONObject data = getListView(creds, cache, getPath, sr.getServicesListPath(), "csid", false, sr);
String[] filepaths = (String[]) data.get("listItems");
subcount += filepaths.length;
if (firstfile.equals("") && subcount != 0) {
firstfile = filepaths[0];
}
for (String uri : filepaths) {
String path = uri;
if (path != null && path.startsWith("/"))
path = path.substring(1);
existingcsid.put(path, "original");
}
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 pgRestrictions = new JSONObject();
pgRestrictions.put("pageSize", Integer.toString(ps));
pgRestrictions.put("pageNum", Integer.toString(pn + 1));
getPath = getRestrictedPath(getPath, pgRestrictions, sr.getServicesSearchKeyword(), "", false, "");
// need more values
} else {
getPath = "";
}
}
}
// how does that compare to what we need
if (sr.isType("authority")) {
if (fs instanceof Field) {
JSONObject subdata = new JSONObject();
// loop thr jsonObject and find the fields I need
for (FieldSet subfs : sr.getAllFieldTopLevel("PUT")) {
String key = subfs.getID();
if (jsonObject.has(key)) {
subdata.put(key, jsonObject.get(key));
}
}
if (subcount == 0) {
// create
createcsid.put(subdata);
} else {
// update - there should only be one
String firstcsid = firstfile;
updatecsid.put(firstcsid, subdata);
existingcsid.remove(firstcsid);
}
} else if (fs instanceof Group) {
// subrecorddata.put(value);
if (jsonObject.has(fs.getID())) {
Object subdata = jsonObject.get(fs.getID());
if (subdata instanceof JSONObject) {
if (((JSONObject) subdata).has("_subrecordcsid")) {
String thiscsid = ((JSONObject) subdata).getString("_subrecordcsid");
// update
if (existingcsid.has(thiscsid)) {
updatecsid.put(thiscsid, (JSONObject) subdata);
existingcsid.remove(thiscsid);
} else {
// something has gone wrong... best just create it from scratch
createcsid.put(subdata);
}
} else {
// create
createcsid.put(subdata);
}
}
}
} else {
// need to find if we have csid's for each one
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);
if (subrecord.has("_subrecordcsid")) {
String thiscsid = subrecord.getString("_subrecordcsid");
// update
if (existingcsid.has(thiscsid)) {
updatecsid.put(thiscsid, (JSONObject) subdata);
existingcsid.remove(thiscsid);
} else {
// something has gone wrong... best just create it from scratch
createcsid.put(subdata);
}
} else {
// create
createcsid.put(subdata);
}
}
}
}
}
String savePathSr = savePath + "/" + sr.getServicesURL() + "/";
// do delete JSONObject existingcsid = new JSONObject();
Iterator<String> rit = existingcsid.keys();
while (rit.hasNext()) {
String key = rit.next();
deleteJSON(root, creds, cache, key, savePathSr, sr);
}
// do update JSONObject updatecsid = new JSONObject();
Iterator<String> keys = updatecsid.keys();
while (keys.hasNext()) {
String key = keys.next();
JSONObject value = updatecsid.getJSONObject(key);
String thissave = savePathSr + key;
updateJSON(root, creds, cache, value, new JSONObject(), sr, thissave);
// updateJSON( root, creds, cache, key, value, sr, savePathSr);
}
// do create JSONArray createcsid = new JSONArray();
for (int i = 0; i < createcsid.length(); i++) {
JSONObject value = createcsid.getJSONObject(i);
subautocreateJSON(root, creds, cache, sr, value, savePathSr);
}
}
}
// XXX dont currently update the shortID???
// cache.setCached(getClass(),new String[]{"shortId",vocab,filePath.split("/")[1]},shortId);
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Connection exception " + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("Cannot parse surrounding JSON " + e.getLocalizedMessage(), e);
} catch (UnsupportedEncodingException e) {
throw new UnimplementedException("UnsupportedEncodingException" + e.getLocalizedMessage(), e);
}
}
Aggregations