Search in sources :

Example 16 with UnimplementedException

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

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

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

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

the class ConfiguredVocabStorage method get.

private JSONArray get(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String url, String filePath, Record thisr) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
    JSONArray itemarray = new JSONArray();
    // get list view
    JSONObject data = getListView(creds, cache, filePath, thisr.getServicesListPath(), "csid", false, thisr);
    String[] filepaths = (String[]) data.get("listItems");
    for (String uri : filepaths) {
        String path = uri;
        if (path != null && path.startsWith("/"))
            path = path.substring(1);
        String[] parts = path.split("/");
        String recordurl = parts[0];
        String mycsid = parts[parts.length - 1];
        try {
            JSONObject itemdata = simpleRetrieveJSON(creds, cache, filePath + "/" + mycsid, "", thisr);
            // add in csid so I can do update with a modicum of confidence
            itemdata.put("_subrecordcsid", mycsid);
            itemarray.put(itemdata);
        } catch (UnimplementedException e) {
            throw new UnderlyingStorageException(e.getMessage());
        }
    }
    return itemarray;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 20 with UnimplementedException

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

the class ConfiguredVocabStorage method updateJSON.

public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, JSONObject jsonObject, JSONObject restrictions, Record thisr, String savePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String csid = savePath.split("/")[3];
        Map<String, Document> body = new HashMap<String, Document>();
        for (String section : r.getServicesRecordPathKeys()) {
            String path = r.getServicesRecordPath(section);
            String[] record_path = path.split(":", 2);
            String[] tag_path = record_path[1].split(",", 2);
            Document temp = createEntry(section, tag_path[0], tag_path[1], jsonObject, null, null, thisr, false);
            if (temp != null) {
                body.put(record_path[0], temp);
            // log.info(temp.asXML());
            }
        }
        handleHierarchyPayloadSend(thisr, body, jsonObject, csid);
        ReturnedMultipartDocument out = conn.getMultipartXMLDocument(RequestMethod.PUT, savePath, body, creds, cache);
        if (out.isErrorStatus()) {
            if (out.isTransactionFailedStatus()) {
                throw new UnderlyingStorageException(VOCABULARY_UPDATE_FAILED_MESSAGE + ": " + out.TRANSACTION_FAILED_MESSAGE, out.getStatus(), savePath);
            } else {
                throw new UnderlyingStorageException(VOCABULARY_UPDATE_FAILED_MESSAGE, out.getStatus(), savePath);
            }
        }
        // subrecord update
        for (FieldSet fs : thisr.getAllSubRecords("PUT")) {
            Record sr = fs.usesRecordId();
            // get list of existing subrecords
            JSONObject existingcsid = new JSONObject();
            JSONObject updatecsid = new JSONObject();
            JSONArray createcsid = new JSONArray();
            String getPath = savePath + "/" + sr.getServicesURL();
            Integer subcount = 0;
            String firstfile = "";
            while (!getPath.equals("")) {
                JSONObject data = getListView(creds, cache, getPath, sr.getServicesListPath(), "csid", false, sr);
                String[] filepaths = (String[]) data.get("listItems");
                subcount += filepaths.length;
                if (firstfile.equals("") && subcount != 0) {
                    firstfile = filepaths[0];
                }
                for (String uri : filepaths) {
                    String path = uri;
                    if (path != null && path.startsWith("/"))
                        path = path.substring(1);
                    existingcsid.put(path, "original");
                }
                if (data.has("pagination")) {
                    Integer ps = Integer.valueOf(data.getJSONObject("pagination").getString("pageSize"));
                    Integer pn = Integer.valueOf(data.getJSONObject("pagination").getString("pageNum"));
                    Integer ti = Integer.valueOf(data.getJSONObject("pagination").getString("totalItems"));
                    if (ti > (ps * (pn + 1))) {
                        JSONObject pgRestrictions = new JSONObject();
                        pgRestrictions.put("pageSize", Integer.toString(ps));
                        pgRestrictions.put("pageNum", Integer.toString(pn + 1));
                        getPath = getRestrictedPath(getPath, pgRestrictions, sr.getServicesSearchKeyword(), "", false, "");
                    // need more values
                    } else {
                        getPath = "";
                    }
                }
            }
            // how does that compare to what we need
            if (sr.isType("authority")) {
                if (fs instanceof Field) {
                    JSONObject subdata = new JSONObject();
                    // loop thr jsonObject and find the fields I need
                    for (FieldSet subfs : sr.getAllFieldTopLevel("PUT")) {
                        String key = subfs.getID();
                        if (jsonObject.has(key)) {
                            subdata.put(key, jsonObject.get(key));
                        }
                    }
                    if (subcount == 0) {
                        // create
                        createcsid.put(subdata);
                    } else {
                        // update - there should only be one
                        String firstcsid = firstfile;
                        updatecsid.put(firstcsid, subdata);
                        existingcsid.remove(firstcsid);
                    }
                } else if (fs instanceof Group) {
                    // subrecorddata.put(value);
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONObject) {
                            if (((JSONObject) subdata).has("_subrecordcsid")) {
                                String thiscsid = ((JSONObject) subdata).getString("_subrecordcsid");
                                // update
                                if (existingcsid.has(thiscsid)) {
                                    updatecsid.put(thiscsid, (JSONObject) subdata);
                                    existingcsid.remove(thiscsid);
                                } else {
                                    // something has gone wrong... best just create it from scratch
                                    createcsid.put(subdata);
                                }
                            } else {
                                // create
                                createcsid.put(subdata);
                            }
                        }
                    }
                } else {
                    // need to find if we have csid's for each one
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONArray) {
                            JSONArray subarray = (JSONArray) subdata;
                            for (int i = 0; i < subarray.length(); i++) {
                                JSONObject subrecord = subarray.getJSONObject(i);
                                if (subrecord.has("_subrecordcsid")) {
                                    String thiscsid = subrecord.getString("_subrecordcsid");
                                    // update
                                    if (existingcsid.has(thiscsid)) {
                                        updatecsid.put(thiscsid, (JSONObject) subdata);
                                        existingcsid.remove(thiscsid);
                                    } else {
                                        // something has gone wrong... best just create it from scratch
                                        createcsid.put(subdata);
                                    }
                                } else {
                                    // create
                                    createcsid.put(subdata);
                                }
                            }
                        }
                    }
                }
                String savePathSr = savePath + "/" + sr.getServicesURL() + "/";
                // do delete JSONObject existingcsid = new JSONObject();
                Iterator<String> rit = existingcsid.keys();
                while (rit.hasNext()) {
                    String key = rit.next();
                    deleteJSON(root, creds, cache, key, savePathSr, sr);
                }
                // do update JSONObject updatecsid = new JSONObject();
                Iterator<String> keys = updatecsid.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    JSONObject value = updatecsid.getJSONObject(key);
                    String thissave = savePathSr + key;
                    updateJSON(root, creds, cache, value, new JSONObject(), sr, thissave);
                // updateJSON( root, creds, cache, key,  value, sr, savePathSr);
                }
                // do create JSONArray createcsid = new JSONArray();
                for (int i = 0; i < createcsid.length(); i++) {
                    JSONObject value = createcsid.getJSONObject(i);
                    subautocreateJSON(root, creds, cache, sr, value, savePathSr);
                }
            }
        }
    // XXX dont currently update the shortID???
    // cache.setCached(getClass(),new String[]{"shortId",vocab,filePath.split("/")[1]},shortId);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception " + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Cannot parse surrounding JSON " + e.getLocalizedMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnimplementedException("UnsupportedEncodingException" + e.getLocalizedMessage(), e);
    }
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record) JSONObject(org.json.JSONObject) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)56 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)55 ExistException (org.collectionspace.csp.api.persistence.ExistException)50 JSONException (org.json.JSONException)50 JSONObject (org.json.JSONObject)48 UIException (org.collectionspace.csp.api.ui.UIException)40 JSONArray (org.json.JSONArray)25 Record (org.collectionspace.chain.csp.schema.Record)11 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)10 IOException (java.io.IOException)7 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)6 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)6 ConfigException (org.collectionspace.chain.csp.config.ConfigException)5 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)5 Field (org.collectionspace.chain.csp.schema.Field)5 Document (org.dom4j.Document)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Group (org.collectionspace.chain.csp.schema.Group)4 Instance (org.collectionspace.chain.csp.schema.Instance)4 Storage (org.collectionspace.csp.api.persistence.Storage)4