Search in sources :

Example 31 with ExistException

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

the class GenericStorage method autocreateJSON.

/**
 * Convert the JSON from the UI Layer into XML for the Service layer while using the XML structure from cspace-config.xml
 * Send the XML through to the Service Layer to store it in the database
 * The Service Layer returns a url to the object we just stored.
 * @param {ContextualisedStorage} root
 * @param {CSPRequestCredentials} creds
 * @param {CSPRequestCache} cache
 * @param {String} filePath part of the path to the Service URL (containing the type of object)
 * @param {JSONObject} jsonObject The JSON string coming in from the UI Layer, containing the object to be stored
 * @return {String} csid The id of the object in the database
 */
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        ReturnedURL url = null;
        Document doc = null;
        // used by userroles and permroles as they have complex urls
        if (r.hasPrimaryField()) {
            for (String section : r.getServicesRecordPathKeys()) {
                doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "POST");
                String path = r.getServicesURL();
                path = path.replace("*", getSubCsid(jsonObject, r.getPrimaryField()));
                String restrictedPath = getRestrictedPath(path, restrictions, null);
                deleteJSON(root, creds, cache, restrictedPath);
                url = conn.getURL(RequestMethod.POST, restrictedPath, doc, creds, cache);
            }
        } else {
            String restrictedPath = getRestrictedPath(r.getServicesURL(), restrictions, null);
            // REM - We need a way to send query params (restrictions) on POST and UPDATE
            url = autoCreateSub(creds, cache, jsonObject, doc, restrictedPath, r);
        }
        // I am developing this.. it might not work...
        for (FieldSet fs : r.getAllSubRecords("POST")) {
            Record sr = fs.usesRecordId();
            // sr.getID()
            if (sr.isType("authority")) {
            // need to use code from configuredVocabStorage
            } else {
                String savePath = url.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 {
                    // 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 url.getURLTail();
    } catch (ConnectionException e) {
        String msg = e.getMessage();
        if (e.getStatus() == 403) {
            // permissions error
            msg += " permissions error";
        }
        throw new UnderlyingStorageException(msg, e.getStatus(), e.getUrl(), e);
    } catch (UnderlyingStorageException e) {
        // REM - CSPACE-5632: Need to catch and rethrow this exception type to prevent throwing an "UnimplementedException" exception below.
        throw e;
    } catch (Exception e) {
        throw new UnimplementedException("JSONException", e);
    }
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) Group(org.collectionspace.chain.csp.schema.Group) JSONArray(org.json.JSONArray) 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) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) DocumentException(org.dom4j.DocumentException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException) IOException(java.io.IOException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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)

Example 32 with ExistException

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

the class AuthoritiesVocabulariesSearchList method generateMiniRecord.

private JSONObject generateMiniRecord(Storage storage, String auth_type, String inst_type, String csid) throws JSONException {
    JSONObject out = new JSONObject();
    try {
        StringBuilder sb = new StringBuilder();
        sb.append(auth_type);
        sb.append("/");
        sb.append(inst_type);
        sb.append("/");
        sb.append(csid);
        sb.append((this.search) ? "/view/search" : "/view/list");
        String path = sb.toString();
        out = storage.retrieveJSON(path, new JSONObject());
        out.put("csid", csid);
    // Record type should be set properly from list results.
    // out.put("recordtype",inst_type);
    } catch (ExistException e) {
        out.put("csid", csid);
        out.put("isError", true);
        JSONObject msg = new JSONObject();
        msg.put("severity", "error");
        msg.put("message", "Exist Exception:" + e.getMessage());
        JSONArray msgs = new JSONArray();
        msgs.put(msg);
        out.put("messages", msgs);
    } catch (UnimplementedException e) {
        out.put("csid", csid);
        out.put("isError", true);
        JSONObject msg = new JSONObject();
        msg.put("severity", "error");
        msg.put("message", "Unimplemented  Exception:" + e.getMessage());
        JSONArray msgs = new JSONArray();
        msgs.put(msg);
        out.put("messages", msgs);
    } catch (UnderlyingStorageException e) {
        out.put("csid", csid);
        out.put("isError", true);
        JSONObject msg = new JSONObject();
        msg.put("severity", "error");
        msg.put("message", "UnderlyingStorage Exception:" + e.getMessage());
        JSONArray msgs = new JSONArray();
        msgs.put(msg);
        out.put("messages", msgs);
    }
    return out;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 33 with ExistException

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

the class VocabulariesRead method createRelations.

private JSONObject createRelations(Storage storage, String csid) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
    JSONObject recordtypes = new JSONObject();
    JSONObject restrictions = new JSONObject();
    restrictions.put("src", base + "/" + csid);
    // XXX needs pagination support CSPACE-1819
    JSONObject data = storage.getPathsJSON("relations/main", restrictions);
    String[] relations = (String[]) data.get("listItems");
    for (String r : relations) {
        try {
            JSONObject relateitem = generateRelationEntry(storage, r);
            String type = relateitem.getString("recordtype");
            if (!recordtypes.has(type)) {
                recordtypes.put(type, new JSONArray());
            }
            recordtypes.getJSONArray(type).put(relateitem);
        } catch (Exception e) {
        // Never mind.
        }
    }
    return recordtypes;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) 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)

Example 34 with ExistException

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

the class DataGenerator method createAllRecords.

/**
 * Create all record types (that make sense)
 * @param storage
 * @param ui
 * @return
 * @throws UIException
 */
protected JSONObject createAllRecords(Storage storage, UIRequest ui) throws UIException {
    log.info("Lets make some records");
    tty.line("Lets make some records");
    tty.flush();
    JSONObject returnData = new JSONObject();
    try {
        for (Record r : spec.getAllRecords()) {
            if (r.isType("authority") || r.isType("authorizationdata") || r.isType("id") || r.isType("userdata")) {
            // don't do these yet (if ever)
            } else if (r.getID().equals("structureddate") || r.getID().equals("media") || r.getID().equals("hierarchy") || r.getID().equals("blobs") || r.getID().equals("dimension") || r.getID().equals("contacts") || r.isType("searchall")) {
            // and ignore these
            } else if (r.getID().equals("termlist") || r.getID().equals("termlistitem")) {
            // and ignore these
            } else {
                this.record = r;
                this.structureview = "screen";
                this.writer = new RecordCreateUpdate(r, true);
                JSONObject items = createRecords(storage, ui);
                returnData.put(r.getID(), items.getJSONObject(r.getID()));
            }
        }
        // lets create some relationships
        log.info("Initializing relationships");
        tty.line("Initializing relationships");
        tty.flush();
        createDataSetRelationships(returnData);
    } 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) {
        throw new UIException("Problem storing: " + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
    }
    return returnData;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) Record(org.collectionspace.chain.csp.schema.Record) RecordCreateUpdate(org.collectionspace.chain.csp.webui.record.RecordCreateUpdate) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 35 with ExistException

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

the class DataGenerator method createRecords.

/**
 * generate records of a specific type
 * @param storage
 * @param ui
 * @return
 * @throws UIException
 */
protected JSONObject createRecords(Storage storage, UIRequest ui) throws UIException {
    log.info("Making " + this.record.getID());
    tty.line("Making " + this.record.getID());
    tty.flush();
    JSONObject returnData = new JSONObject();
    JSONObject out = datagenerator(storage, ui);
    try {
        // make it a record
        JSONObject data = new JSONObject();
        Iterator rit = out.keys();
        JSONObject dataitems = new JSONObject();
        while (rit.hasNext()) {
            String key = (String) rit.next();
            data.put("fields", out.getJSONObject(key));
            String path = writer.sendJSON(storage, null, data, null);
            dataitems.put(key, path);
            // log.info(path);
            log.info("created " + this.record.getID() + " with csid of: " + path);
            tty.line("created " + this.record.getID() + " with csid of: " + path);
            tty.flush();
        }
        returnData.put(this.record.getID(), dataitems);
    } catch (JSONException x) {
        tty.line("JSONException(Failed to parse json: " + x);
        log.info("JSONException(Failed to parse json: " + x);
        throw new UIException("Failed to parse json: " + x, x);
    } catch (ExistException x) {
        log.info("ExistException(Existence exception: " + x);
        tty.line("ExistException(Existence exception: " + x);
        throw new UIException("Existence exception: " + x, x);
    } catch (UnimplementedException x) {
        tty.line("UnimplementedException(UnimplementedException: " + x);
        log.info("UnimplementedException(UnimplementedException: " + x);
        throw new UIException("Unimplemented exception: " + x, x);
    } catch (UnderlyingStorageException x) {
        tty.line("UnderlyingStorageException(UnderlyingStorageException: " + x);
        log.info("UnderlyingStorageException(UnderlyingStorageException: " + x);
        throw new UIException("Problem storing: " + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
    }
    // return something to screen
    return returnData;
}
Also used : JSONObject(org.json.JSONObject) Iterator(java.util.Iterator) 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

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