Search in sources :

Example 16 with ExistException

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

the class UserDetailsRead method getJSON.

/* Wrapper exists to decomplexify exceptions */
private JSONObject getJSON(Storage storage, String csid) throws UIException {
    JSONObject out = new JSONObject();
    try {
        if (record_type) {
            JSONObject fields = storage.retrieveJSON(base + "/" + csid, new JSONObject());
            // XXX remove this, subject to UI team approval?
            fields.put("csid", csid);
            JSONObject roles = storage.retrieveJSON(base + "/" + csid + "/" + "userrole", new JSONObject());
            JSONArray allroles = Generic.getRoles(storage, roles);
            fields.put("role", allroles);
            out.put("fields", fields);
            out.put("isError", false);
            JSONObject messages = new JSONObject();
            messages.put("message", "");
            messages.put("severity", "info");
            JSONArray arr = new JSONArray();
            arr.put(messages);
            out.put("messages", arr);
            out.put("relations", new JSONArray());
        } else {
            out = storage.retrieveJSON(base + "/" + csid, new JSONObject());
        }
    } catch (ExistException e) {
        throw new UIException("JSON Not found ", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } 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, e);
    }
    if (out == null) {
        throw new UIException("No JSON Found");
    }
    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 17 with ExistException

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

the class UserDetailsReset method getcsID.

private JSONObject getcsID(Storage storage, String emailparam) throws UIException {
    JSONObject restriction = new JSONObject();
    JSONObject failedJSON = new JSONObject();
    try {
        failedJSON.put("isError", true);
        if (emailparam != null && emailparam != "") {
            restriction.put("email", emailparam);
            restriction.put("pageSize", "40");
            int resultsize = 1;
            int pagenum = 0;
            String checkpagination = "";
            while (resultsize > 0) {
                restriction.put("pageNum", pagenum);
                /* XXX need to force it to only do an exact match */
                JSONObject data = storage.getPathsJSON(base, restriction);
                String[] paths = (String[]) data.get("listItems");
                pagenum++;
                if (paths.length == 0 || checkpagination.equals(paths[0])) {
                    resultsize = 0;
                // testing whether we have actually returned the same page or the next page - all csid returned should be unique
                } else {
                    checkpagination = paths[0];
                    /* make sure it is an exact match */
                    for (int i = 0; i < paths.length; i++) {
                        // GET full details
                        JSONObject fields = storage.retrieveJSON(base + "/" + paths[i], new JSONObject());
                        String emailtest = fields.getString("email");
                        if (emailtest.equals(emailparam)) {
                            JSONObject outputJSON = new JSONObject();
                            outputJSON.put("fields", fields);
                            outputJSON.put("isError", false);
                            outputJSON.put("csid", paths[i]);
                            return outputJSON;
                        }
                    }
                }
            }
            failedJSON.put("message", "Could not find a user with email " + emailparam);
        } else {
            failedJSON.put("message", "No email specified ");
        }
        return failedJSON;
    } catch (JSONException e) {
        throw new UIException("JSONException during search on email address", e);
    } catch (ExistException e) {
        throw new UIException("ExistException during search on email address", e);
    } catch (UnimplementedException e) {
        throw new UIException("UnimplementedException during search on email address", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        return 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 18 with ExistException

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

the class UserRolesRead method getJSON.

/* Wrapper exists to decomplexify exceptions */
private JSONObject getJSON(Storage storage, String csid) throws UIException {
    JSONObject out = new JSONObject();
    try {
        if (!record_type) {
            JSONObject fields = storage.retrieveJSON("base+/" + csid, new JSONObject());
            // XXX remove this, subject to UI team approval?
            fields.put("csid", csid);
            out.put("fields", fields);
            out.put("isError", false);
            JSONObject messages = new JSONObject();
            messages.put("message", "");
            messages.put("severity", "info");
            JSONArray arr = new JSONArray();
            arr.put(messages);
            out.put("messages", arr);
            out.put("relations", new JSONArray());
        } else {
            out = storage.retrieveJSON(this.sub_base + "/" + csid, new JSONObject());
        }
    } catch (ExistException e) {
        throw new UIException("ExistException " + e, e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } 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, e);
    }
    if (out == null) {
        throw new UIException("No JSON Found");
    }
    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 19 with ExistException

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

the class StubJSONStore method fileFromPath.

/**
 * Generate a file from a path.
 *
 * @param path the path
 * @return the file
 * @throws UnderlyingStorageException
 * @throws ExistException
 */
private File fileFromPath(String path) throws UnderlyingStorageException, ExistException {
    if (path.startsWith("/"))
        path = path.substring(1);
    String[] parts = path.split("/");
    if (parts.length == 1)
        throw new ExistException("Cannot store in root directory");
    List<String> fullpath = new ArrayList<String>();
    for (int i = 0; i < parts.length; i++) {
        if (// Repeated slash, ignore
        "".equals(parts[i]))
            continue;
        fullpath.add(parts[i]);
    }
    String file = fullpath.remove(fullpath.size() - 1);
    File root = new File(store_root);
    for (String dir : fullpath) {
        dir = dir.replaceAll("[^A-Za-z0-9_,.-]", "");
        root = new File(root, dir);
        if (!root.exists()) {
            root.mkdir();
        }
    }
    file = file.replaceAll("[^A-Za-z0-9_,.-]", "");
    return new File(root, file + ".json");
}
Also used : ArrayList(java.util.ArrayList) ExistException(org.collectionspace.csp.api.persistence.ExistException) File(java.io.File)

Example 20 with ExistException

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

the class StubJSONStore method retrieveJSON.

/* (non-Javadoc)
	 * @see org.collectionspace.JSONStore#retrieveJson(java.lang.String)
	 */
public JSONObject retrieveJSON(String filePath, JSONObject restrictions) throws ExistException, UnderlyingStorageException, UnimplementedException {
    // XXX hack: support views properly
    String XXXCHOP = "/view";
    String XXXCHOP2 = "/refs";
    if (filePath.endsWith(XXXCHOP))
        filePath = filePath.substring(0, filePath.length() - XXXCHOP.length());
    if (filePath.endsWith(XXXCHOP2))
        return new JSONObject();
    if (idRequest(filePath))
        return id.retrieveJSON(filePath, restrictions);
    File jsonFile = fileFromPath(filePath);
    if (!jsonFile.exists()) {
        throw new ExistException("No such file: " + filePath);
    }
    try {
        FileReader r = new FileReader(jsonFile);
        String data = IOUtils.toString(r);
        r.close();
        JSONObject jsonObject = new JSONObject(data);
        return jsonObject;
    } catch (IOException ioe) {
        // XXX
        return new JSONObject();
    } catch (JSONException je) {
        // XXX
        return new JSONObject();
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) FileReader(java.io.FileReader) IOException(java.io.IOException) ExistException(org.collectionspace.csp.api.persistence.ExistException) File(java.io.File)

Aggregations

ExistException (org.collectionspace.csp.api.persistence.ExistException)64 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)57 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)50 JSONObject (org.json.JSONObject)50 JSONException (org.json.JSONException)49 UIException (org.collectionspace.csp.api.ui.UIException)40 JSONArray (org.json.JSONArray)23 IOException (java.io.IOException)10 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)10 Record (org.collectionspace.chain.csp.schema.Record)9 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)8 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)8 Document (org.dom4j.Document)7 ConfigException (org.collectionspace.chain.csp.config.ConfigException)5 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)5 File (java.io.File)4 Field (org.collectionspace.chain.csp.schema.Field)4 Instance (org.collectionspace.chain.csp.schema.Instance)4 Storage (org.collectionspace.csp.api.persistence.Storage)4 RefName (org.collectionspace.services.common.api.RefName)4