Search in sources :

Example 6 with UnderlyingStorageException

use of org.collectionspace.csp.api.persistence.UnderlyingStorageException 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 7 with UnderlyingStorageException

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

the class BlobRead method get_blob.

private void get_blob(Storage s, UIRequest q, String csid, String derivative) throws UIException {
    try {
        JSONObject out = s.retrieveJSON("/blobs/" + csid + "/" + derivative, null);
        byte[] data_array = (byte[]) out.get("getByteBody");
        String contentDisp = out.has("contentdisposition") ? out.getString("contentdisposition") : null;
        q.sendUnknown(data_array, out.getString("contenttype"), contentDisp);
        int cacheMaxAgeSeconds = adminData.getUploadedMediaCacheAge();
        if (cacheMaxAgeSeconds > 0) {
            q.setCacheMaxAgeSeconds(cacheMaxAgeSeconds);
        }
    } catch (ExistException e) {
        throw new UIException("Existence exception", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } catch (UnderlyingStorageException e) {
        throw new UIException("Underlying storage problem", e);
    } catch (JSONException e) {
        throw new UIException("JSON exception", 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 8 with UnderlyingStorageException

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

the class RecordTraverser method store_get.

private void store_get(Storage storage, UIRequest request, String path) throws UIException {
    JSONObject outputJSON = new JSONObject();
    try {
        String[] bits = path.split("/");
        String token = bits[0];
        Integer indexvalue = Integer.valueOf(bits[1]);
        String key = UISession.SEARCHTRAVERSER + "" + token;
        if (request.getSession().getValue(key) instanceof JSONObject) {
            JSONObject alldata = (JSONObject) request.getSession().getValue(key);
            JSONObject pagination = new JSONObject();
            if (alldata.has("pagination")) {
                pagination = alldata.getJSONObject("pagination");
            }
            JSONArray data = alldata.getJSONArray("results");
            Integer pgSz = alldata.getInt("pageSize");
            Integer pageNm = alldata.getInt("pageNum");
            Integer total = alldata.getInt("total");
            Integer offset = pgSz * pageNm;
            Integer numInstances = alldata.getInt("numInstances");
            String base = alldata.getString("record");
            String instance = alldata.getString("instance");
            JSONObject restriction = alldata.getJSONObject("restriction");
            // only works 100% for records - auths is a problem...
            // multiplying by numInstance isn't quite enough...
            // Integer relativeindexvalue = indexvalue - offset ;
            Integer prevpageNm = 0;
            Integer prevval = indexvalue - 1;
            if (prevval >= 0 && prevval <= total) {
                JSONObject item = checkTraverserPageNum(storage, request, outputJSON, token, key, alldata, pagination, data, pgSz, pageNm, numInstances, base, instance, restriction, prevpageNm, prevval);
                outputJSON.put("previous", item);
            }
            Integer postpageNm = 0;
            Integer postval = indexvalue + 1;
            if (postval < total && postval >= 0) {
                JSONObject item = checkTraverserPageNum(storage, request, outputJSON, token, key, alldata, pagination, data, pgSz, pageNm, numInstances, base, instance, restriction, postpageNm, postval);
                outputJSON.put("next", item);
            }
            Integer currpageNm = 0;
            if (indexvalue < total && indexvalue >= 0) {
                JSONObject item = checkTraverserPageNum(storage, request, outputJSON, token, key, alldata, pagination, data, pgSz, pageNm, currpageNm, base, instance, restriction, prevpageNm, indexvalue);
                outputJSON.put("current", item);
            }
            outputJSON.put("index", indexvalue);
            outputJSON.put("token", token);
            outputJSON.put("total", total);
        } else {
            outputJSON.put("error", "Cannot find the traverser token");
        }
    } catch (JSONException e) {
        throw new UIException("Error with the traverser data", e);
    } catch (ExistException e) {
        throw new UIException("Error with the traverser data", e);
    } catch (UnimplementedException e) {
        throw new UIException("Error with the traverser data", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    }
    request.sendJSONResponse(outputJSON);
}
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 9 with UnderlyingStorageException

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

the class WebAutoComplete method doAuthorityAutocomplete.

private JSONArray doAuthorityAutocomplete(CSPRequestCache cache, Storage storage, String fieldname, String start, String vocabConstraint, String pageSize, String pageNum) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException, ConfigException {
    FieldSet fs = r.getFieldFullList(fieldname);
    JSONArray out = new JSONArray();
    Instance[] allInstances = null;
    if (fs == null || !(fs instanceof Field)) {
        if (r.hasHierarchyUsed("screen")) {
            // Configures the hierarchy section.
            Structure s = r.getStructure("screen");
            if (s.hasOption(fieldname)) {
                // This is one of the hierarchy fields
                if (vocabConstraint != null) {
                    allInstances = new Instance[1];
                    String fullname = r.getID() + "-" + vocabConstraint;
                    allInstances[0] = r.getSpec().getInstance(fullname);
                } else {
                    Option a = s.getOption(fieldname);
                    String[] data = a.getName().split(",");
                    allInstances = new Instance[data.length];
                    for (int i = 0; i < data.length; i++) {
                        allInstances[i] = (r.getSpec().getInstance(data[i]));
                    }
                }
            } else {
                fs = r.getSpec().getRecord("hierarchy").getFieldFullList(fieldname);
                if (fs instanceof Field) {
                    allInstances = ((Field) fs).getAllAutocompleteInstances();
                }
            }
        }
    } else {
        allInstances = ((Field) fs).getAllAutocompleteInstances();
    }
    if (allInstances == null) {
        // Cannot autocomplete
        return out;
    }
    // support multiassign of autocomplete instances
    for (Instance n : allInstances) {
        try {
            if (n == null) {
            // Field has no autocomplete
            } else {
                String path = n.getRecord().getID() + "/" + n.getTitleRef();
                JSONObject restriction = new JSONObject();
                if (pageSize != null) {
                    restriction.put("pageSize", pageSize);
                }
                if (pageNum != null) {
                    restriction.put("pageNum", pageNum);
                }
                FieldSet dispNameFS = n.getRecord().getDisplayNameField();
                if (dispNameFS == null) {
                    throw new ConfigException("WebAutoComplete for Instance has no displayName configured: " + n.getID());
                }
                String displayNameFieldID = dispNameFS.getID();
                // May be something other than display name
                restriction.put(displayNameFieldID, start);
                JSONObject results = storage.getPathsJSON(path, restriction);
                String[] paths = (String[]) results.get("listItems");
                for (String csid : paths) {
                    JSONObject data = storage.retrieveJSON(path + "/" + csid + "/view", new JSONObject());
                    JSONObject entry = new JSONObject();
                    // TODO - handle multiple name matches
                    String displayNameString = data.getString(displayNameFieldID);
                    JSONArray displayNames = JSONUtils.createJSONArrayFromSeparatedString(displayNameString);
                    String primaryDN = displayNames.getString(0);
                    String refid = data.getString("refid");
                    // HACK - transition period with full instead of base URN value
                    if (refid.endsWith("'" + primaryDN + "'"))
                        refid = refid.substring(0, refid.length() - (primaryDN.length() + 2));
                    entry.put("baseUrn", refid);
                    entry.put("csid", data.getString("csid"));
                    entry.put("type", n.getRecord().getWebURL());
                    entry.put("displayNames", displayNames);
                    // RefName.AuthorityItem item = RefName.AuthorityItem.parse(refid);
                    // entry.put("namespace",item.getParentShortIdentifier());
                    entry.put("namespace", data.getString("namespace"));
                    entry.put("workflow", data.getString("workflow"));
                    out.put(entry);
                }
            }
        } catch (UnderlyingStorageException x) {
            if (x.getStatus() == 403) {
            // permission error - keep calm and carry on
            } else {
                throw x;
            }
        }
    }
    // Instance n=((Field)fs).getAutocompleteInstance();
    return out;
}
Also used : Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) ConfigException(org.collectionspace.chain.csp.config.ConfigException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Option(org.collectionspace.chain.csp.schema.Option) Structure(org.collectionspace.chain.csp.schema.Structure)

Example 10 with UnderlyingStorageException

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

the class WebAutoComplete method autocomplete.

private void autocomplete(CSPRequestCache cache, Storage storage, UIRequest request) throws UIException {
    try {
        String[] path = request.getPrincipalPath();
        // Last path element is the field on which user is querying.
        String fieldName = path[path.length - 1];
        JSONArray out = new JSONArray();
        boolean hasHierarchy = r.hasHierarchyUsed("screen");
        boolean isHierarchyAutoComplete = false;
        if (hasHierarchy) {
            // Configures the hierarchy section.
            Structure s = r.getStructure("screen");
            if (s.hasOption(fieldName)) {
                // This is one of the hierarchy fields
                isHierarchyAutoComplete = true;
            }
        }
        if (r.isType("authority") || !isHierarchyAutoComplete) {
            out = doAuthorityAutocomplete(cache, storage, fieldName, request.getRequestArgument(AUTO_COMPLETE_QUERY_PARAM), request.getRequestArgument(CONSTRAIN_VOCAB_PARAM), request.getRequestArgument(PAGE_SIZE_PARAM), request.getRequestArgument(PAGE_NUM_PARAM));
        } else if (isHierarchyAutoComplete) {
            out = doRecordAutocomplete(cache, storage, fieldName, request.getRequestArgument(AUTO_COMPLETE_QUERY_PARAM), request.getRequestArgument(PAGE_SIZE_PARAM), request.getRequestArgument(PAGE_NUM_PARAM));
        } else {
            throw new ConfigException("WebAutoComplete called for record that does not support autocomplete!: " + r.getID());
        }
        request.sendJSONResponse(out);
    } catch (JSONException e) {
        throw new UIException("JSONException during autocompletion", e);
    } catch (ExistException e) {
        throw new UIException("ExistException during autocompletion", e);
    } catch (UnimplementedException e) {
        throw new UIException("UnimplementedException during autocompletion", e);
    } catch (ConfigException e) {
        throw new UIException("ConfigException during autocompletion", e);
    } catch (UnderlyingStorageException x) {
        throw new UIException("UnderlyingStorageException during autocompletion" + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ConfigException(org.collectionspace.chain.csp.config.ConfigException) Structure(org.collectionspace.chain.csp.schema.Structure) 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