use of org.collectionspace.chain.csp.schema.Field 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.schema.Field 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);
}
}
use of org.collectionspace.chain.csp.schema.Field in project application by collectionspace.
the class ServiceBindingsGeneration method createInitFieldEntry.
/*
* Attempt to create an entry in the service bindings for this field to be indexed. Returns true if created and false if not.
*/
private boolean createInitFieldEntry(Element paramsElement, Namespace namespace, Record record, FieldSet f, Boolean isAuthority) {
boolean result = false;
if (f instanceof Field) {
Field fd = (Field) f;
String fieldName = fd.getServicesTag().toLowerCase();
if (fd.shouldIndex() == true) {
Element fieldElement = paramsElement.addElement(new QName("field", namespace));
Element tableElement = fieldElement.addElement(new QName("table", namespace));
// getServiceTableName(fd, record, isAuthority);
String tableName = fd.getServiceTableName(isAuthority);
tableElement.addText(tableName);
Element columnElement = fieldElement.addElement(new QName("col", namespace));
columnElement.addText(fieldName);
result = true;
}
} else {
log.trace(String.format("Config Generation: '%s' - Ignoring FieldSet instance '%s:%s'", getConfigFile().getName(), Arrays.toString(f.getIDPath()), f.getID()));
}
return result;
}
use of org.collectionspace.chain.csp.schema.Field in project application by collectionspace.
the class XmlJsonConversion method addRepeatedNodeToJson.
private static JSONArray addRepeatedNodeToJson(Element container, Repeat f, String permlevel, JSONObject tempSon) throws JSONException {
JSONArray node = new JSONArray();
List<FieldSet> children = getChildrenWithGroupFields(f, permlevel);
JSONArray elementlist = extractRepeatData(container, f, permlevel);
JSONObject siblingitem = new JSONObject();
for (int i = 0; i < elementlist.length(); i++) {
JSONObject element = elementlist.getJSONObject(i);
Iterator<?> rit = element.keys();
while (rit.hasNext()) {
String key = (String) rit.next();
JSONArray arrvalue = new JSONArray();
for (FieldSet fs : children) {
if (fs instanceof Repeat && ((Repeat) fs).hasServicesParent()) {
if (!((Repeat) fs).getServicesParent()[0].equals(key)) {
continue;
}
Object value = element.get(key);
arrvalue = (JSONArray) value;
} else {
if (!fs.getID().equals(key)) {
continue;
}
Object value = element.get(key);
arrvalue = (JSONArray) value;
}
if (fs instanceof Field) {
for (int j = 0; j < arrvalue.length(); j++) {
JSONObject repeatitem = new JSONObject();
// XXX remove when service layer supports primary tags
if (f.hasPrimary() && j == 0) {
repeatitem.put("_primary", true);
}
Element child = (Element) arrvalue.get(j);
Object val = child.getText();
Field field = (Field) fs;
String id = field.getID();
if (f.asSibling()) {
addExtraToJson(siblingitem, child, field, tempSon);
if (field.getDataType().equals("boolean")) {
siblingitem.put(id, (Boolean.parseBoolean((String) val) ? true : false));
} else {
siblingitem.put(id, val);
}
} else {
addExtraToJson(repeatitem, child, field, tempSon);
if (field.getDataType().equals("boolean")) {
repeatitem.put(id, (Boolean.parseBoolean((String) val) ? true : false));
} else {
repeatitem.put(id, val);
}
node.put(repeatitem);
}
tempSon = addtemp(tempSon, fs.getID(), child.getText());
}
} else if (fs instanceof Group) {
JSONObject tout = new JSONObject();
JSONObject tempSon2 = new JSONObject();
Group rp = (Group) fs;
addRepeatToJson(tout, container, rp, permlevel, tempSon2, "", "");
if (f.asSibling()) {
JSONArray a1 = tout.getJSONArray(rp.getID());
JSONObject o1 = a1.getJSONObject(0);
siblingitem.put(fs.getID(), o1);
} else {
JSONObject repeatitem = new JSONObject();
repeatitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
node.put(repeatitem);
}
tempSon = addtemp(tempSon, rp.getID(), tout.getJSONArray(rp.getID()));
// log.info(f.getID()+":"+rp.getID()+":"+tempSon.toString());
} else if (fs instanceof Repeat) {
JSONObject tout = new JSONObject();
JSONObject tempSon2 = new JSONObject();
Repeat rp = (Repeat) fs;
addRepeatToJson(tout, container, rp, permlevel, tempSon2, "", "");
if (f.asSibling()) {
siblingitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
} else {
JSONObject repeatitem = new JSONObject();
repeatitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
node.put(repeatitem);
}
tempSon = addtemp(tempSon, rp.getID(), tout.getJSONArray(rp.getID()));
// log.info(f.getID()+":"+rp.getID()+":"+tempSon.toString());
}
}
}
}
if (f.asSibling()) {
node.put(siblingitem);
}
return node;
}
use of org.collectionspace.chain.csp.schema.Field 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);
}
}
Aggregations