Search in sources :

Example 21 with UnimplementedException

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

the class GenericStorage method simpleRetrieveJSON.

/**
 * return data just as the service layer gives it to the App layer
 * no extra columns required
 * @param creds
 * @param cache
 * @param filePath
 * @param servicesurl
 * @param thisr
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 */
public JSONObject simpleRetrieveJSON(CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String servicesurl, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException {
    String csid = "";
    if (filePath == null) {
        filePath = "";
    }
    String[] path_parts = filePath.split("/");
    if (path_parts.length > 1)
        csid = path_parts[1];
    else
        csid = filePath;
    JSONObject out = new JSONObject();
    try {
        String softpath = filePath;
        if (thisr.hasSoftDeleteMethod()) {
            softpath = softpath(filePath);
        }
        if (thisr.hasHierarchyUsed("screen")) {
            softpath = hierarchicalpath(softpath);
        }
        if (thisr.isMultipart()) {
            ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
                throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
            for (String section : thisr.getServicesRecordPathKeys()) {
                String path = thisr.getServicesRecordPath(section);
                String[] parts = path.split(":", 2);
                if (doc.getDocument(parts[0]) != null) {
                    convertToJson(out, doc.getDocument(parts[0]), thisr, "GET", section, csid);
                }
            }
            // If this record has hierarchy, will pull out the relations section and map it to the hierarchy
            // fields (special case handling of XML-JSON
            handleHierarchyPayloadRetrieve(thisr, doc, out, csid);
        } else {
            ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
                throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
            convertToJson(out, doc.getDocument(), thisr, "GET", "common", csid);
        }
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Service layer exception", e);
    }
    /*
		 * Get data for any sub records that are part of this record e.g. contact in person, blob in media
		 */
    try {
        for (FieldSet fs : thisr.getAllSubRecords("GET")) {
            Boolean validator = true;
            Record sr = fs.usesRecordId();
            if (fs.usesRecordValidator() != null) {
                validator = false;
                if (out.has(fs.usesRecordValidator())) {
                    String test = out.getString(fs.usesRecordValidator());
                    if (test != null && !test.equals("")) {
                        validator = true;
                    }
                }
            }
            if (validator) {
                String getPath = servicesurl + filePath + "/" + sr.getServicesURL();
                if (null != fs.getServicesUrl()) {
                    getPath = fs.getServicesUrl();
                }
                if (fs.getWithCSID() != null) {
                    getPath = getPath + "/" + out.getString(fs.getWithCSID());
                }
                // need to get update and delete working? tho not going to be used for media as handling blob seperately
                if (fs instanceof Group) {
                    JSONObject outer = simpleRetrieveJSON(creds, cache, getPath, "", sr);
                    JSONArray group = new JSONArray();
                    group.put(outer);
                    out.put(fs.getID(), group);
                }
                if (fs instanceof Repeat) {
                    // NEED TO GET A LIST OF ALL THE THINGS
                    JSONArray repeat = new JSONArray();
                    String path = getPath;
                    while (!path.equals("")) {
                        JSONObject data = getListView(creds, cache, path, sr.getServicesListPath(), "csid", false, r);
                        if (data.has("listItems")) {
                            String[] results = (String[]) data.get("listItems");
                            for (String result : results) {
                                JSONObject rout = simpleRetrieveJSON(creds, cache, getPath + "/" + result, "", sr);
                                // add in csid so I can do update with a modicum of confidence
                                rout.put("_subrecordcsid", result);
                                repeat.put(rout);
                            }
                        }
                        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 restrictions = new JSONObject();
                                restrictions.put("pageSize", Integer.toString(ps));
                                restrictions.put("pageNum", Integer.toString(pn + 1));
                                path = getRestrictedPath(getPath, restrictions, sr.getServicesSearchKeyword(), "", false, "");
                            // need more values
                            } else {
                                path = "";
                            }
                        }
                    }
                    // group.put(outer);
                    out.put(fs.getID(), repeat);
                }
            }
        }
    } catch (Exception e) {
    // ignore exceptions for sub records at the moment - make it more intelligent later
    // throw new UnderlyingStorageException("Service layer exception",e);
    }
    return out;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Repeat(org.collectionspace.chain.csp.schema.Repeat) 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) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 22 with UnimplementedException

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

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

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

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

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