Search in sources :

Example 41 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class RecordRead method store_get.

private void store_get(Storage storage, UIRequest request, String path) throws Exception {
    // Get the data
    try {
        if (record.isType("blob")) {
            JSONObject outputJSON = getJSON(storage, path);
            String content = outputJSON.getString("contenttype");
            String contentDisp = outputJSON.has("contentdisposition") ? outputJSON.getString("contentdisposition") : null;
            byte[] bob = (byte[]) outputJSON.get("getByteBody");
            String getByteBody = bob.toString();
            request.sendUnknown(getByteBody, content, contentDisp);
        } else if (record.getID().equals("output")) {
            // 
            // Invoke a report
            // 
            ReportUtils.invokeReport(this, storage, request, path);
        } else if (record.getID().equals("batchoutput")) {
            String[] bits = path.split("/");
            JSONObject payload = new JSONObject();
            if (bits.length > 1 && !bits[1].equals("output")) {
                payload.put("mode", "single");
                String type = spec.getRecordByWebUrl(bits[1]).getServicesTenantDoctype(false);
                payload.put("docType", type);
                payload.put("singleCSID", bits[2]);
                path = bits[0];
            }
            JSONObject out = storage.retrieveJSON(base + "/" + path, payload);
            request.sendJSONResponse(out);
        } else {
            JSONObject outputJSON = getJSON(storage, path);
            outputJSON.put("csid", path);
            // Write the requested JSON out
            request.sendJSONResponse(outputJSON);
        }
    } catch (JSONException e1) {
        throw new UIException("Cannot add csid", e1);
    } catch (ExistException e) {
        throw new UIException("Cannot add csid", e);
    } catch (UnimplementedException e) {
        throw new UIException("Cannot add csid", e);
    } catch (UnderlyingStorageException e) {
        throw new UIException("Cannot add csid", e);
    }
}
Also used : JSONObject(org.json.JSONObject) 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 42 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class RecordRelated method store_get.

private void store_get(Storage storage, UIRequest ui, String path) throws UIException {
    try {
        JSONObject restriction = new JSONObject();
        restriction.put("src", base + "/" + path);
        String key = "items";
        Set<String> args = ui.getAllRequestArgument();
        for (String restrict : args) {
            if (ui.getRequestArgument(restrict) != null) {
                String value = ui.getRequestArgument(restrict);
                // }
                if (restrict.equals(WebMethod.PAGE_SIZE_PARAM) || restrict.equals(WebMethod.PAGE_NUM_PARAM) || restrict.equals("keywords")) {
                    restriction.put(restrict, value);
                } else if (restrict.equals(WebMethod.SEARCH_QUERY_PARAM)) {
                // ignore - someone was doing something odd
                } else {
                    // XXX I would so prefer not to restrict and just pass
                    // stuff up but I know it will cause issues later
                    restriction.put("queryTerm", restrict);
                    restriction.put("queryString", value);
                }
            }
        }
        // Get the data
        JSONObject outputJSON = new JSONObject();
        JSONObject recordtypes = getRelations(storage, restriction, new JSONObject(), new JSONArray());
        outputJSON.put("relations", recordtypes);
        try {
            outputJSON.put("csid", path);
        } catch (JSONException e1) {
            throw new UIException("Cannot add csid", e1);
        }
        // Write the requested JSON out
        ui.sendJSONResponse(outputJSON);
    } catch (JSONException e) {
        throw new UIException("JSONException during store_get", e);
    } catch (ExistException e) {
        throw new UIException("ExistException during store_get", e);
    } catch (UnimplementedException e) {
        throw new UIException("UnimplementedException during store_get", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        ui.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 43 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class RecordSearchList method search_or_list.

/**
 * This function is the general function that calls the correct funtions to
 * get all the data that the UI requested and get it in the correct format
 * for the UI.
 *
 * @param {Storage} storage The type of storage requested (e.g.
 * RecordStorage, AuthorizationStorage,...)
 * @param {UIRequest} ui The request from the ui to which we send a
 * response.
 * @param {String} param If a querystring has been added to the
 * URL(e.g.'?query='), it will be in this param
 * @param {String} pageSize The amount of results per page requested.
 * @param {String} pageNum The amount of pages requested.
 * @throws UIException
 */
private void search_or_list(Storage storage, UIRequest ui, String path) throws UIException {
    try {
        JSONObject restrictedkey = GenericSearch.setRestricted(ui, null, null, null, (mode == MODE_SEARCH), this.r);
        JSONObject restriction = restrictedkey.getJSONObject("restriction");
        String key = restrictedkey.getString("key");
        JSONObject results = getResults(ui, storage, restriction, key, path);
        // cache for record traverser
        if (results.has("pagination") && results.getJSONObject("pagination").has("separatelists")) {
            GenericSearch.createTraverser(ui, this.r.getID(), "", results, restriction, key, 1);
        }
        ui.sendJSONResponse(results);
    } catch (JSONException e) {
        throw new UIException("JSONException during search_or_list", e);
    } catch (ExistException e) {
        throw new UIException("ExistException during search_or_list", e);
    } catch (UnimplementedException e) {
        throw new UIException("UnimplementedException during search_or_list", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        ui.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : JSONObject(org.json.JSONObject) 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 44 with UIException

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

Example 45 with UIException

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

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