Search in sources :

Example 56 with UnderlyingStorageException

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

the class BlobStorage method autocreateJSON.

@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    ReturnedURL url = null;
    try {
        byte[] bitten = (byte[]) jsonObject.get("getbyteBody");
        String uploadname = jsonObject.getString("fileName");
        String type = jsonObject.getString("contentType");
        String path = r.getServicesURL();
        url = conn.getStringURL(RequestMethod.POST, path, bitten, uploadname, type, creds, cache);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException(e.getMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnimplementedException("JSONException", e);
    }
    return conn.getBase() + url.getURL();
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 57 with UnderlyingStorageException

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

the class GenericStorage method subautocreateJSON.

/**
 * needs some tests.. just copied from ConfiguredVocabStorage
 * @param root
 * @param creds
 * @param cache
 * @param myr
 * @param jsonObject
 * @param savePrefix
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 */
public String subautocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, Record myr, JSONObject jsonObject, String savePrefix) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        ReturnedURL url = null;
        Document doc = null;
        // XXX I would hope this might be removed if userroles etc ever get improved to be more like the rest
        if (myr.hasPrimaryField()) {
            for (String section : myr.getServicesRecordPathKeys()) {
                doc = XmlJsonConversion.convertToXml(myr, jsonObject, section, "POST");
                String path = myr.getServicesURL();
                path = path.replace("*", getSubCsid(jsonObject, myr.getPrimaryField()));
                deleteJSON(root, creds, cache, path);
                url = conn.getURL(RequestMethod.POST, path, doc, creds, cache);
            }
        } else {
            url = autoCreateSub(creds, cache, jsonObject, doc, savePrefix, myr);
        }
        // create related sub records?
        for (FieldSet allfs : myr.getAllSubRecords("POST")) {
            Record sr = allfs.usesRecordId();
            if (sr.isType("authority")) {
            } else {
                String savePath = url.getURL() + "/" + sr.getServicesURL();
                if (jsonObject.has(sr.getID())) {
                    Object subdata = jsonObject.get(sr.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);
                        }
                    } else if (subdata instanceof JSONObject) {
                        JSONObject subrecord = (JSONObject) subdata;
                        subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                    }
                }
            }
        }
        return url.getURLTail();
    } 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) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Record(org.collectionspace.chain.csp.schema.Record) JSONObject(org.json.JSONObject) 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) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 58 with UnderlyingStorageException

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

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

Example 60 with UnderlyingStorageException

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

the class VocabInstanceCache method getVocabularyId.

String getVocabularyId(CSPRequestCredentials creds, CSPRequestCache cache, String id) throws ConnectionException, UnderlyingStorageException, ExistException {
    // must allow for the dynamic creation of instances when the system is working
    vocabs = new ConcurrentHashMap<String, String>();
    for (Instance n : r.getAllInstances()) {
        vocabs.put(n.getTitleRef(), n.getTitle());
    }
    if (csids.containsKey(id))
        return csids.get(id);
    synchronized (getClass()) {
        buildVocabularies(creds, cache);
        if (csids.containsKey(id))
            return csids.get(id);
        createVocabulary(creds, cache, id);
        if (csids.containsKey(id))
            return csids.get(id);
        throw new UnderlyingStorageException("Bad vocabulary " + id);
    }
}
Also used : Instance(org.collectionspace.chain.csp.schema.Instance) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

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