use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class RelateCreateUpdate method relate.
private void relate(Storage storage, UIRequest request, String path) throws UIException {
try {
JSONObject data = request.getJSONBody();
boolean and_reverse = false;
if (!data.optBoolean("one-way"))
and_reverse = true;
if (data.has("type")) {
// Single
if (!create) {
// Update may mean tinkering with other relations because of two-way-ness
/* NOTE at the moment there's no support in the service to indicate partner, so we assume that
* an opposite relation is evidence of a two way relationship. This creates a potential bug if
* the user has set up two independent one way relationships and wants to maintain them
* independently. It's arguable that this is the behaviour they expect, arguable that it is not.
*/
// Delete the reverse record for an update
log.debug("forward is " + path);
String csid_rev = find_reverse(storage, path);
log.debug("reverse is " + csid_rev);
if (csid_rev != null)
storage.deleteJSON("/relations/main/" + csid_rev);
}
relate_one(storage, data, path, false);
if (and_reverse) {
/* If we're not one way and we're updating, create reverse (we just deleted the old one) */
relate_one(storage, data, null, true);
}
} else if (data.has("items")) {
// Multiple
JSONArray relations = data.getJSONArray("items");
for (int i = 0; i < relations.length(); i++) {
JSONObject itemdata = relations.getJSONObject(i);
if (!create)
throw new UIException("Cannot use multiple syntax for update");
if (!itemdata.optBoolean("one-way")) {
and_reverse = true;
}
relate_one(storage, relations.getJSONObject(i), path, false);
if (and_reverse)
relate_one(storage, relations.getJSONObject(i), path, true);
}
} else
throw new UIException("Bad JSON data");
request.sendJSONResponse(data);
request.setOperationPerformed(create ? Operation.CREATE : Operation.UPDATE);
if (create)
// XXX should be derivable
request.setSecondaryRedirectPath(new String[] { "relationships", path });
} catch (JSONException x) {
throw new UIException("Failed to parse json: ", x);
} catch (ExistException x) {
throw new UIException("Existence exception: ", x);
} catch (UnimplementedException x) {
throw new UIException("Unimplemented exception: ", x);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
request.sendJSONResponse(uiexception.getJSON());
}
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class RelateDelete method relate_delete.
private void relate_delete(Storage storage, UIRequest request, String path) throws UIException {
try {
String source = request.getRequestArgument(RELATION_SOURCE_PARAM);
String target = request.getRequestArgument(RELATION_TARGET_PARAM);
String type = request.getRequestArgument(RELATION_TYPE_PARAM);
String oneway = request.getRequestArgument(RELATION_ONE_WAY_PARAM);
if (oneway != null && oneway != "") {
one_way = Boolean.parseBoolean(oneway);
}
if (request.isJSON()) {
JSONObject data = request.getJSONBody();
if (data.has("source") && data.has("target") && data.has("type")) {
source = data.getJSONObject("source").getString("recordtype") + "/" + data.getJSONObject("source").getString("csid");
target = data.getJSONObject("target").getString("recordtype") + "/" + data.getJSONObject("target").getString("csid");
type = data.getString("type");
if (data.has("one-way")) {
one_way = data.getBoolean("one-way");
}
}
}
if (source != null && target != null && type != null && source != "" && target != "" && type != "") {
path = getRelationShipID(storage, source, target, type);
if (!one_way) {
// easier to find reverse if have actually sub items
String reverse = getRelationShipID(storage, target, source, type);
if (reverse != null)
storage.deleteJSON("/relations/main/" + reverse);
}
} else {
// find csids' if delete sent with just relationship csid rather than sub items
if (!one_way) {
String rev = findReverse(storage, path);
if (rev != null)
storage.deleteJSON("/relations/main/" + rev);
}
}
storage.deleteJSON("/relations/main/" + path);
request.setOperationPerformed(Operation.DELETE);
} catch (ExistException e) {
throw new UIException("Exist exception deleting ", e);
} catch (UnimplementedException e) {
throw new UIException("Unimplemented exception deleting ", e);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
request.sendJSONResponse(uiexception.getJSON());
} catch (JSONException e) {
throw new UIException("Exception building JSON ", e);
}
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class RelateRead method relate_get.
private void relate_get(Storage storage, UIRequest request, String path) throws UIException {
try {
JSONObject relation = convertPayload(storage, storage.retrieveJSON("/relations/" + searchPath + "/" + path, new JSONObject()), path);
request.sendJSONResponse(relation);
} catch (ExistException e) {
throw new UIException("JSON Not found ", e);
} catch (UnimplementedException e) {
throw new UIException("Unimplemented", e);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
request.sendJSONResponse(uiexception.getJSON());
} catch (JSONException e) {
throw new UIException("Could not build JSON ", e);
}
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class ServicesIDGenerator method retrieveJSON.
/**
* This function generates a new id for the next inputted object.
* The id in the static list on top is the primary key in the generators_id table on the service layer.
*/
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
try {
String val = conn.getTextDocument(RequestMethod.POST, "idgenerators/" + generators.get(filePath) + "/ids", null, creds, cache);
JSONObject out = new JSONObject();
out.put("next", val);
return out;
} catch (ConnectionException e) {
throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (JSONException e) {
throw new UnderlyingStorageException("JSON exception" + e.getLocalizedMessage(), e);
}
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class XmlJsonConversion method convertToXml.
//
// section should be an array
//
public static Document convertToXml(Record r, JSONObject in, String section, String operation) throws JSONException, UnderlyingStorageException {
Document doc = DocumentFactory.getInstance().createDocument();
try {
String path = r.getServicesRecordPath(section);
if (path != null) {
String[] parts = path.split(":", 2);
String[] rootel = parts[1].split(",");
Element root = doc.addElement(new QName(rootel[1], new Namespace("ns2", rootel[0])));
if (r.getAllServiceFieldTopLevel(operation, section).length > 0) {
for (FieldSet f : r.getAllServiceFieldTopLevel(operation, section)) {
addFieldSetToXml(root, f, in, section, operation);
}
return doc;
}
} else {
// Revert to DEBUG after v4.0 testing
log.warn(String.format("Record %s lacks expected section %s", r.getRecordName(), section));
}
} catch (Exception ex) {
log.debug("Error in XmlJsonConversion.convertToXml", ex);
throw ex;
}
return null;
}
Aggregations