use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class ReportUtils method extractParamsFromJSON.
private static void extractParamsFromJSON(UIRequest request, JSONObject payloadOut) throws Exception {
JSONObject payloadIn = null;
try {
// Look for a JSON payload from the request
payloadIn = request.getJSONBody();
} catch (UIException e) {
log.trace(e.getMessage());
}
if (payloadIn != null) {
// incoming query params
JSONObject fields = payloadIn.optJSONObject("fields");
// default value set to "single"
setPayloadField("mode", payloadOut, fields, payloadIn, "single");
setPayloadField("docType", payloadOut, fields, payloadIn);
//
// If mode is 'single' then look for a 'singleCSID' param, otherwise if mode is 'group' look for a 'groupCSID'.
// If <mode>CSID param is missing then try to use 'csid' from the query params (from the 'fields' var).
//
String exceptionMsg = null;
if (payloadOut.getString("mode").equals("single")) {
if (setPayloadField("singleCSID", payloadOut, fields, payloadIn) == false) {
if (fields != null && fields.getString("csid").trim().isEmpty() == false) {
payloadOut.put("singleCSID", fields.getString("csid"));
} else {
exceptionMsg = String.format("Report invocation context specified '%s' mode but did not provide a '%s' param.", "single", "singleCSID");
}
}
} else if (payloadOut.getString("mode").equals("group")) {
if (setPayloadField("groupCSID", payloadOut, fields, payloadIn) == false) {
if (fields != null && fields.getString("csid").trim().isEmpty() == false) {
payloadOut.put("groupCSID", fields.getString("csid"));
} else {
exceptionMsg = String.format("Report invocation context specified '%s' mode but did not provide a '%s' param.", "group", "groupCSID");
}
}
} else {
exceptionMsg = String.format("The Report invocation mode '%s' is unknown.", payloadOut.getString("mode"));
}
if (exceptionMsg != null) {
throw new UIException(exceptionMsg);
}
}
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class RelateSearchList method search_or_list.
private void search_or_list(Storage storage, UIRequest request, String source, String target, String type) throws UIException {
try {
JSONObject restrictions = new JSONObject();
addRestriction(restrictions, "src", source, true);
addRestriction(restrictions, "dst", target, true);
addRestriction(restrictions, "type", type, false);
// XXX CSPACE-1834 need to support pagination
JSONObject results = storage.getPathsJSON("relations/" + searchPath, restrictions);
String[] relations = (String[]) results.get("listItems");
JSONObject out = new JSONObject();
JSONArray data = new JSONArray();
if (searchPath.equals("main")) {
for (String r : relations) data.put(r);
out.put("items", data);
} else {
if (results.has("listItems")) {
if (results.getJSONObject("moredata").length() > 0) {
// there is a relationship
String[] reld = (String[]) results.get("listItems");
String hcsid = reld[0];
JSONObject mored = results.getJSONObject("moredata").getJSONObject(hcsid);
// it's name is
JSONObject broaderthan = new JSONObject();
broaderthan.put("label", mored.getString("objectname"));
out.put("broader", broaderthan);
}
}
}
request.sendJSONResponse(out);
} 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.ui.UIException in project application by collectionspace.
the class DataGenerator method datagenerator.
/**
* Guts of creating the data
* @param storage
* @param ui
* @return
* @throws UIException
*/
private JSONObject datagenerator(Storage storage, UIRequest ui) throws UIException {
this.storage = storage;
maxValue = new BigDecimal(dateToLong("2030-01-01"));
minValue = new BigDecimal(dateToLong("1970-01-01"));
log.info("initialize params");
tty.line("initialize params");
JSONObject out = new JSONObject();
Structure s = record.getStructure(this.structureview);
// how many records do we want to create
if (ui.getRequestArgument("quantity") != null) {
String quantity = ui.getRequestArgument("quantity");
this.quant = Integer.parseInt(quantity);
}
// how many time do we want the repeatable group to repeat
if (ui.getRequestArgument("repeats") != null) {
String rstring = ui.getRequestArgument("repeats");
Integer rnum = Integer.parseInt(rstring);
this.repeatnum = rnum;
}
// how many records will we set relationships on
if (ui.getRequestArgument("maxrelationships") != null) {
String mxstring = ui.getRequestArgument("maxrelationships");
Integer mxnum = Integer.parseInt(mxstring);
this.maxrecords = mxnum;
}
// how many records will we set relationships on
if (ui.getRequestArgument("startvalue") != null) {
String startvalue = ui.getRequestArgument("startvalue");
Integer stnum = Integer.parseInt(startvalue);
this.startvalue = stnum;
}
// how many records will we set relationships on
if (ui.getRequestArgument("extraprefix") != null) {
String exstring = ui.getRequestArgument("extraprefix");
this.extraprefix = exstring;
}
// setting this will speed up the initialization of this script
if (ui.getRequestArgument("authoritylimit") != null) {
String astring = ui.getRequestArgument("authoritylimit");
Integer anum = Integer.parseInt(astring);
this.authoritylimit = anum;
}
log.info("Creating " + quant.toString() + " records of type " + record.getWebURL());
tty.line("Creating " + quant.toString() + " records of type " + record.getWebURL());
tty.flush();
for (Integer i = 0; i < quant; i++) {
try {
if ((i % 10) == 0) {
log.info("So far up to number: " + i.toString());
tty.line("So far up to number: " + i.toString());
tty.flush();
}
out.put(i.toString(), makedata(i));
} catch (JSONException e) {
throw new UIException("Cannot generate UISpec due to JSONException", e);
}
}
return out;
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class UISchema method uisearchschema.
/**
* Create the search uischemas
* @param storage
* @param record
* @return
* @throws UIException
*/
private JSONObject uisearchschema(Storage storage, Record record) throws UIException {
UISpecRunContext context = new UISpecRunContext();
this.storage = storage;
this.record = record;
this.tenantname = this.record.getSpec().getAdminData().getTenantName();
try {
JSONObject out = new JSONObject();
JSONObject fields = actualSchemaFields(record, context);
JSONObject prop = fields.getJSONObject("properties");
fields.put("properties", prop);
out.put(record.getWebURL(), fields);
return out;
} catch (JSONException e) {
throw new UIException("Cannot generate UISpec due to JSONException", e);
}
}
use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.
the class UISchema method uiotherschema.
/**
* Create all the other weird schemas that the UI wants
* @param storage
* @param params
* @return
* @throws UIException
* @throws UnauthorizedException
*/
private JSONObject uiotherschema(UISession session, Storage storage, String params) throws UIException, UnauthorizedException {
JSONObject out = new JSONObject();
String sectionname = "";
String sectionid = params.toLowerCase();
if (schema != null) {
sectionid = schema.getID();
sectionname = schema.getWebURL();
}
try {
if (sectionid.toLowerCase().equals("recordlist")) {
JSONObject schema = new JSONObject();
JSONArray recrds = new JSONArray();
for (Record rc : this.spec.getAllRecords()) {
if (rc.isShowType("authority")) {
if (rc.isInRecordList()) {
for (Instance ins : rc.getAllInstances()) {
recrds.put(ins.getWebURL());
}
}
} else if (rc.isShowType("record") || rc.isShowType("procedure")) {
if (rc.isInRecordList()) {
recrds.put(rc.getWebURL());
}
}
}
schema.put("type", "array");
schema.put("default", recrds);
out.put(sectionname, schema);
} else if (sectionid.toLowerCase().equals("namespaces")) {
assertLoginStatus(session);
JSONObject namespaces = new JSONObject();
JSONObject namespacesProps = new JSONObject();
for (Record rc : this.spec.getAllRecords()) {
if (rc.isInRecordList()) {
if (rc.isShowType("authority")) {
JSONObject authInfoProps = new JSONObject();
int cardinal = 0;
for (Instance ins : rc.getAllInstances()) {
JSONObject instanceInfo = new JSONObject();
JSONObject instanceProps = new JSONObject();
JSONObject nptAllowed = new JSONObject();
nptAllowed.put("type", "boolean");
nptAllowed.put("default", ins.getNPTAllowed());
instanceProps.put("nptAllowed", nptAllowed);
// Preserve the order of the namespaces
JSONObject orderProp = new JSONObject();
orderProp.put("type", "integer");
orderProp.put("default", cardinal);
instanceProps.put("order", orderProp);
JSONObject workflowState = new JSONObject();
workflowState.put("type", "string");
workflowState.put("default", getWorkflowState(storage, ins));
instanceProps.put("workflowState", workflowState);
instanceInfo.put("type", "object");
instanceInfo.put("properties", instanceProps);
authInfoProps.put(ins.getWebURL(), instanceInfo);
cardinal++;
}
JSONObject authorityInfo = new JSONObject();
authorityInfo.put("type", "object");
authorityInfo.put("properties", authInfoProps);
namespacesProps.put(rc.getWebURL(), authorityInfo);
}
}
}
namespaces.put("type", "object");
namespaces.put("properties", namespacesProps);
out.put("namespaces", namespaces);
/**
*{
* "namespaces": {
* "type": "object",
* "properties": {
* "person": {
* "type": "object"
* "properties": {
* "person" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* },
* "persontest" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* }
* }
* },
* "organization": {
* "type": "object"
* "properties": {
* "organization" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* },
* "organizationtest" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* }
* }
* },
* ...
* }
* }
*}
*/
} else if (sectionid.toLowerCase().equals("recordtypes")) {
JSONObject procedures_schema = new JSONObject();
JSONObject authorities_schema = new JSONObject();
JSONObject admin_schema = new JSONObject();
JSONObject cataloging_schema = new JSONObject();
JSONObject searchAll_schema = new JSONObject();
JSONArray procedures_records = new JSONArray();
JSONArray authorities_records = new JSONArray();
JSONArray admin_records = new JSONArray();
JSONArray cataloging_records = new JSONArray();
JSONArray searchAll_records = new JSONArray();
/**
* { "procedures": { "type": "array", "default": ["loanout",
* "movement", ...] }, "vocabularies": { "type": "array",
* "default": ["person", "organization", ...] }, "cataloging": {
* "type": "array", "default": ["cataloging"] } }
*/
for (Record rc : this.spec.getAllRecords()) {
if (rc.isInRecordList()) {
if (rc.isShowType("procedure")) {
procedures_records.put(rc.getWebURL());
} else if (rc.isShowType("authority")) {
for (Instance ins : rc.getAllInstances()) {
authorities_records.put(ins.getWebURL());
}
} else if (rc.isShowType("record")) {
// FIXME Assumes that "records" are either
// procedures, authorities, or cataloging.
// Should instead have a type "cataloging"
cataloging_records.put(rc.getWebURL());
} else if (rc.isShowType("searchall")) {
searchAll_records.put(rc.getWebURL());
} else if (rc.isShowType("authorizationdata") || rc.isShowType("userdata")) {
admin_records.put(rc.getWebURL());
}
}
}
procedures_schema.put("type", "array");
procedures_schema.put("default", procedures_records);
authorities_schema.put("type", "array");
authorities_schema.put("default", authorities_records);
admin_schema.put("type", "array");
admin_schema.put("default", admin_records);
cataloging_schema.put("type", "array");
cataloging_schema.put("default", cataloging_records);
searchAll_schema.put("type", "array");
searchAll_schema.put("default", searchAll_records);
JSONObject record_types = new JSONObject();
JSONObject types_list = new JSONObject();
types_list.put("procedures", procedures_schema);
types_list.put("vocabularies", authorities_schema);
types_list.put("cataloging", cataloging_schema);
types_list.put("all", searchAll_schema);
types_list.put("administration", admin_schema);
record_types.put("type", "object");
record_types.put("properties", types_list);
out.put("recordtypes", record_types);
}
} catch (JSONException e) {
throw new UIException("Cannot generate UISpec due to JSONException", e);
}
return out;
}
Aggregations