Search in sources :

Example 11 with UIException

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);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) UIException(org.collectionspace.csp.api.ui.UIException)

Example 12 with UIException

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());
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 13 with UIException

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;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) Structure(org.collectionspace.chain.csp.schema.Structure) BigDecimal(java.math.BigDecimal)

Example 14 with UIException

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);
    }
}
Also used : UISpecRunContext(org.collectionspace.chain.csp.schema.UISpecRunContext) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException)

Example 15 with UIException

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;
}
Also used : JSONObject(org.json.JSONObject) Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) Record(org.collectionspace.chain.csp.schema.Record)

Aggregations

UIException (org.collectionspace.csp.api.ui.UIException)72 JSONObject (org.json.JSONObject)51 JSONException (org.json.JSONException)50 ExistException (org.collectionspace.csp.api.persistence.ExistException)39 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)39 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)39 JSONArray (org.json.JSONArray)19 IOException (java.io.IOException)10 Record (org.collectionspace.chain.csp.schema.Record)7 Instance (org.collectionspace.chain.csp.schema.Instance)6 ConfigException (org.collectionspace.chain.csp.config.ConfigException)5 Field (org.collectionspace.chain.csp.schema.Field)4 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)4 UIRequest (org.collectionspace.csp.api.ui.UIRequest)4 MessageDigest (java.security.MessageDigest)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)3 AdminData (org.collectionspace.chain.csp.schema.AdminData)3 Structure (org.collectionspace.chain.csp.schema.Structure)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2