Search in sources :

Example 1 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class RecordDelete method store_delete.

private void store_delete(Storage storage, UIRequest request, String path) throws UIException {
    try {
        if (base.equals("role")) {
            // business logic. Only delete role if no active users exists who have this role set
            // CSPACE-3283
            // Note that given this, we need not clear the userperms cache when deleting a role.
            String url = base + "/" + path + "/" + "accountroles/";
            JSONObject accounts = storage.retrieveJSON(url, new JSONObject());
            if (accounts.has("account") && accounts.getJSONArray("account").length() > 0) {
                if (accounts.getJSONArray("account").getJSONObject(0).length() > 0) {
                    // refuse to delete as has roles attached
                    UIException uiexception = new UIException("This Role has Accounts associated with it");
                    request.sendJSONResponse(uiexception.getJSON());
                    return;
                }
            }
        }
        storage.deleteJSON(base + "/" + path);
    } catch (ExistException e) {
        throw new UIException("JSON Not found " + e, 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("JSONException ", 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)

Example 2 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class RecordRead method buildRelationsSection.

// Builds an object, that has a map of record type to Array mappings.
private JSONObject buildRelationsSection(Storage storage, String csid) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException, UIException {
    JSONObject recordtypes = new JSONObject();
    JSONObject restrictions = new JSONObject();
    JSONObject out = new JSONObject();
    JSONArray paginations = new JSONArray();
    restrictions.put("src", base + "/" + csid);
    // loop over all procedure/recordtypes
    for (Record thisr : spec.getAllRecords()) {
        if ((thisr.isType("procedure") && !thisr.isType("vocabulary")) || "collection-object".equals(thisr.getID()))
            try {
                this.relatedObjSearcher = new RecordSearchList(thisr, RecordSearchList.MODE_SEARCH_RELATED);
                this.relatedObjSearcher.configure(spec);
                JSONObject temp = this.relatedObjSearcher.getResults(null, storage, restrictions, "results", csid);
                JSONArray results = temp.getJSONArray("results");
                if (results.length() > 0) {
                    out.put(thisr.getWebURL(), results);
                }
            } catch (Exception e) {
                log.warn("Unable to to retrieve results for " + thisr.getSearchURL(), e);
            }
    }
    return out;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Record(org.collectionspace.chain.csp.schema.Record) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException)

Example 3 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class RecordRead method getJSON.

/* Wrapper exists to decomplexify exceptions: also used inCreateUpdate, hence not private */
public JSONObject getJSON(Storage storage, String csid) throws UIException {
    JSONObject out = new JSONObject();
    JSONObject restrictions = new JSONObject();
    try {
        if (record_type || authorization_type) {
            JSONObject fields = storage.retrieveJSON(base + "/" + csid, restrictions);
            // XXX remove this, subject to UI team approval?
            fields.put("csid", csid);
            out.put("csid", csid);
            if (base.equals("role")) {
                if (authorization_type) {
                    JSONObject permissions = storage.retrieveJSON(base + "/" + csid + "/" + "permroles/", restrictions);
                    JSONArray allperms = getPermissions(storage, permissions);
                    fields.put("permissions", allperms);
                    String url = base + "/" + csid + "/" + "accountroles/";
                    JSONObject accounts = storage.retrieveJSON(url, restrictions);
                    JSONArray usedby = new JSONArray();
                    if (accounts.has("account")) {
                        usedby = accounts.getJSONArray("account");
                    }
                    fields.put("usedBy", usedby);
                }
            } else if (base.equals("termlist")) {
                String shortname = fields.getString("shortIdentifier");
                JSONArray allUsed = getUsedBy(shortname);
                fields.put("usedBys", allUsed);
            } else {
                fields = getHierarchy(storage, fields);
                // JSONObject relations=createRelations(storage,csid);
                if (!showbasicinfoonly) {
                    JSONObject tusd = this.termsused.getTermsUsed(storage, base + "/" + csid, new JSONObject());
                    out.put("termsUsed", tusd.getJSONArray("results"));
                    JSONObject relations = buildRelationsSection(storage, csid);
                    out.put("relations", relations);
                }
            }
            out.put("fields", fields);
        } else {
            out = storage.retrieveJSON(base + "/" + csid, restrictions);
        }
    } catch (ExistException e) {
        UIException uiexception = new UIException(e.getMessage(), e);
        return uiexception.getJSON();
    } catch (UnimplementedException e) {
        UIException uiexception = new UIException(e.getMessage(), e);
        return uiexception.getJSON();
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        return uiexception.getJSON();
    } catch (JSONException e) {
        throw new UIException("Could not create JSON", e);
    }
    if (out == null) {
        UIException uiexception = new UIException("No JSON Found");
        return uiexception.getJSON();
    }
    return out;
}
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 4 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class RecordRelated method getRelation.

private JSONObject getRelation(Storage storage, JSONObject myres, JSONObject recordtypes, JSONArray paginated) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
    JSONObject data = storage.getPathsJSON("relations/main", myres);
    String[] relations = (String[]) data.get("listItems");
    JSONObject pagination = data.getJSONObject("pagination");
    // get a list of what is paginated
    JSONObject typelist = new JSONObject();
    for (String r : relations) {
        try {
            JSONObject relateitem = generateRelationEntry(storage, r);
            String type = relateitem.getString("recordtype");
            typelist.put(type, "1");
            if (!recordtypes.has(type)) {
                recordtypes.put(type, new JSONArray());
            }
            recordtypes.getJSONArray(type).put(relateitem);
        } catch (Exception e) {
        // Never mind.
        // Probably should do something with the errors... could be a permissions issue
        }
    }
    pagination.put("recordtypes", typelist.names());
    if (!pagination.getString("totalItems").equals("0")) {
        paginated.put(pagination);
    }
    return recordtypes;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException)

Example 5 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.

the class RecordSearchList method advancedSearch.

private void advancedSearch(Storage storage, UIRequest ui, String path, JSONObject params) throws UIException {
    try {
        JSONObject results = new JSONObject();
        JSONObject restrictedkey = GenericSearch.setRestricted(ui, null, null, null, true, this.r);
        JSONObject restriction = restrictedkey.getJSONObject("restriction");
        String key = restrictedkey.getString("key");
        GenericSearch.buildQuery(this.r, params, restriction);
        key = "results";
        results = getJSON(storage, restriction, key, base);
        // 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 advancedSearch " + e.getMessage(), 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)

Aggregations

UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)108 JSONObject (org.json.JSONObject)75 JSONException (org.json.JSONException)73 ExistException (org.collectionspace.csp.api.persistence.ExistException)57 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)55 UIException (org.collectionspace.csp.api.ui.UIException)40 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)39 JSONArray (org.json.JSONArray)34 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)30 Document (org.dom4j.Document)29 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)23 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)16 Field (org.collectionspace.chain.csp.schema.Field)14 Record (org.collectionspace.chain.csp.schema.Record)14 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 Node (org.dom4j.Node)10 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)9 IOException (java.io.IOException)7