Search in sources :

Example 96 with UnderlyingStorageException

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

the class ConfiguredVocabStorage method deleteJSON.

@Override
public void deleteJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String vocab = RefName.shortIdToPath(filePath.split("/")[0]);
        String url = generateURL(vocab, filePath.split("/")[1], "", this.r);
        if (r.hasSoftDeleteMethod()) {
            // The url we compute already has the filepath built in, so just pass an empty
            // filepath, and it will work out.
            String emptyFilepath = "";
            super.transitionWorkflowJSON(root, creds, cache, emptyFilepath, url, WORKFLOW_TRANSITION_DELETE);
        } else {
            int status = conn.getNone(RequestMethod.DELETE, url, null, creds, cache);
            if (status > 299)
                throw new UnderlyingStorageException("Could not retrieve vocabulary", status, url);
        }
    // cache.removeCached(getClass(),new String[]{"namefor",vocab,filePath.split("/")[1]});
    // cache.removeCached(getClass(),new String[]{"reffor",vocab,filePath.split("/")[1]});
    // cache.removeCached(getClass(),new String[]{"shortId",vocab,filePath.split("/")[1]});
    // cache.removeCached(getClass(),new String[]{"csidfor",vocab,filePath.split("/")[1]});
    // delete name and id versions from teh cache?
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
}
Also used : UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 97 with UnderlyingStorageException

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

the class ConfiguredVocabStorage method autocreateJSON.

@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Map<String, Document> body = new HashMap<String, Document>();
        String vocab = null;
        String pathurl = "/" + r.getServicesURL() + "/";
        if (filePath.equals("")) {
            for (String section : r.getServicesInstancesPaths()) {
                String path = r.getServicesInstancesPath(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, vocab, null, r, true);
                if (temp != null) {
                    body.put(record_path[0], temp);
                // log.info(temp.asXML());
                }
            }
        } else {
            vocab = RefName.shortIdToPath(filePath);
            pathurl = "/" + r.getServicesURL() + "/" + vocab + ITEMS_SUFFIX;
            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, vocab, null, r, false);
                if (temp != null) {
                    body.put(record_path[0], temp);
                // log.info(temp.asXML());
                }
            }
        }
        handleHierarchyPayloadSend(r, body, jsonObject, null);
        ReturnedURL out = conn.getMultipartURL(RequestMethod.POST, pathurl, body, creds, cache);
        if (out.getStatus() > 299)
            throw new UnderlyingStorageException("Could not create vocabulary", out.getStatus(), pathurl);
        String csid = out.getURLTail();
        // create related sub records?
        for (FieldSet fs : r.getAllSubRecords("POST")) {
            Record sr = fs.usesRecordId();
            // sr.getID()
            if (sr.isType("authority")) {
                String savePath = out.getURL() + "/" + sr.getServicesURL();
                if (fs instanceof Field) {
                    // get the fields form inline XXX untested - might not work...
                    JSONObject subdata = new JSONObject();
                    // loop thr jsonObject and find the fields I need
                    for (FieldSet subfs : sr.getAllFieldTopLevel("POST")) {
                        String key = subfs.getID();
                        if (jsonObject.has(key)) {
                            subdata.put(key, jsonObject.get(key));
                        }
                    }
                    subautocreateJSON(root, creds, cache, sr, subdata, savePath);
                } else if (fs instanceof Group) {
                    // JSONObject
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONObject) {
                            JSONObject subrecord = (JSONObject) subdata;
                            subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                        } else {
                            log.warn("autocreateJSON: Contact subrecord is malformed (not a JSONObject)!");
                            if (log.isDebugEnabled()) {
                                log.debug("autocreateJSON: Contact subrecord: " + subdata.toString());
                            }
                        }
                    }
                } else {
                    // JSONArray
                    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);
                                subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                            }
                        }
                    }
                }
            }
        }
        return csid;
    } 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);
    }
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) Group(org.collectionspace.chain.csp.schema.Group) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) 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) 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) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 98 with UnderlyingStorageException

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

the class UserDetailsDelete method store_delete.

private void store_delete(Storage storage, UIRequest request, String path) throws UIException {
    try {
        // Deleting a user needs to clear the userperms cache for safety.
        ResponseCache.clearCache(ResponseCache.USER_PERMS_CACHE);
        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());
    }
}
Also used : 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 99 with UnderlyingStorageException

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

the class UserDetailsReset method reset_password.

/* check token and if matches csid then reset password 
	 * */
private void reset_password(Storage storage, UIRequest request, Request in) throws UIException {
    // mock login else service layer gets upset
    // XXX ARGH
    AdminData ad = spec.getAdminData();
    request.getSession().setValue(UISession.USERID, ad.getAuthUser());
    request.getSession().setValue(UISession.PASSWORD, ad.getAuthPass());
    in.reset();
    JSONObject outputJSON = new JSONObject();
    if (testSuccess(in.getStorage())) {
        JSONObject data = null;
        data = request.getJSONBody();
        String token;
        try {
            token = data.getString("token");
            String password = data.getString("password");
            String email = data.getString("email");
            JSONObject userdetails = getcsID(storage, email);
            if (!userdetails.getBoolean("isError")) {
                String csid = userdetails.getString("csid");
                if (testToken(csid, token)) {
                    /* update userdetails */
                    String path = csid;
                    JSONObject fields = userdetails.getJSONObject("fields");
                    try {
                        JSONObject changedata = new JSONObject();
                        JSONObject updatefields = fields;
                        updatefields.put("password", password);
                        changedata.put("fields", updatefields);
                        changedata.put("csid", csid);
                        sendJSON(storage, path, changedata);
                        outputJSON.put("isError", false);
                        JSONObject messages = new JSONObject();
                        messages.put("message", "Your Password has been succesfully changed, Please login");
                        messages.put("severity", "info");
                        JSONArray arr = new JSONArray();
                        arr.put(messages);
                        outputJSON.put("messages", arr);
                    } 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);
                        outputJSON = uiexception.getJSON();
                    }
                } else {
                    outputJSON.put("isError", false);
                    JSONObject messages = new JSONObject();
                    messages.put("message", "Token was not valid");
                    messages.put("severity", "error");
                    JSONArray arr = new JSONArray();
                    arr.put(messages);
                    outputJSON.put("messages", arr);
                }
            } else {
                outputJSON = userdetails;
            }
            request.getSession().setValue(UISession.USERID, "");
            request.getSession().setValue(UISession.PASSWORD, "");
            in.reset();
        } catch (JSONException x) {
            throw new UIException("Failed to parse json: ", x);
        }
    } else {
        try {
            outputJSON.put("isError", false);
            JSONObject messages = new JSONObject();
            messages.put("message", "The admin details in cspace-config.xml failed");
            messages.put("severity", "error");
            JSONArray arr = new JSONArray();
            arr.put(messages);
            outputJSON.put("messages", arr);
        } catch (JSONException x) {
            throw new UIException("Failed to parse json: ", x);
        }
    }
    /* should we automagically log them in or let them do that?, 
		 * I think we should let them login, it has the advantage 
		 * that they find out straight away if they can't remember the new password  */
    request.sendJSONResponse(outputJSON);
    request.setOperationPerformed(Operation.CREATE);
}
Also used : AdminData(org.collectionspace.chain.csp.schema.AdminData) 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 100 with UnderlyingStorageException

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

the class UserDetailsSearchList method search_or_list.

private void search_or_list(Storage storage, UIRequest ui, String param, String pageSize, String pageNum) throws UIException {
    try {
        JSONObject restriction = new JSONObject();
        String key = "items";
        if (param != null) {
            restriction.put("screenName", param);
            key = "results";
        }
        if (pageSize != null) {
            restriction.put("pageSize", pageSize);
        }
        if (pageNum != null) {
            restriction.put("pageNum", pageNum);
        }
        JSONObject data = storage.getPathsJSON(base, restriction);
        String[] paths = (String[]) data.get("listItems");
        JSONObject pagination = new JSONObject();
        if (data.has("pagination")) {
            pagination = data.getJSONObject("pagination");
        }
        JSONObject resultsObject = new JSONObject();
        resultsObject = pathsToJSON(storage, base, paths, key, pagination);
        ui.sendJSONResponse(resultsObject);
    } 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 (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