Search in sources :

Example 26 with ExistException

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

the class ConfiguredVocabStorage method getInstance.

private JSONObject getInstance(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String vocab, String ims_url) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
    String url = this.r.getServicesURL() + "/" + vocab;
    String csid = "";
    JSONObject out = new JSONObject();
    String softurl = url;
    if (r.hasSoftDeleteMethod()) {
        softurl = softpath(url);
    }
    if (r.hasHierarchyUsed("screen")) {
        softurl = hierarchicalpath(softurl);
    }
    ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, softurl, null, creds, cache);
    if (doc.getStatus() == 404) {
        throw new ExistException("Does not exist " + softurl);
    }
    if (doc.getStatus() == 403) {
        // permission error
        return out;
    }
    if (doc.getStatus() > 299) {
        throw new UnderlyingStorageException("Could not retrieve vocabulary status=" + doc.getStatus(), doc.getStatus(), softurl);
    }
    String name = null;
    String refid = null;
    String shortIdentifier = "";
    for (String section : r.getServicesRecordPathKeys()) {
        String path = r.getServicesInstancesPath(section);
        if (path == null) {
            path = r.getServicesRecordPath(section);
        }
        String[] record_path = path.split(":", 2);
        String[] tag_path = record_path[1].split(",", 2);
        Document result = doc.getDocument(record_path[0]);
        if ("common".equals(section)) {
            // XXX hardwired :(
            name = result.selectSingleNode(tag_path[1] + "/displayName").getText();
            if (result.selectSingleNode(tag_path[1] + "/shortIdentifier") != null) {
                shortIdentifier = result.selectSingleNode(tag_path[1] + "/shortIdentifier").getText();
            }
            refid = result.selectSingleNode(tag_path[1] + "/refName").getText();
            csid = result.selectSingleNode(tag_path[1] + "/csid").getText();
        } else if ("collectionspace_core".equals(section)) {
            XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
        }
    }
    out.put("displayName", name);
    out.put("csid", csid);
    out.put("refid", refid);
    out.put("shortIdentifier", shortIdentifier);
    return out;
}
Also used : ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) JSONObject(org.json.JSONObject) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)

Example 27 with ExistException

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

the class ConfiguredVocabStorage method get.

private JSONObject get(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String url, String ims_url) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
    // int status=0;
    String csid = "";
    JSONObject out = new JSONObject();
    // XXX pagination support
    String softurl = url;
    if (r.hasSoftDeleteMethod()) {
        softurl = softpath(url);
    }
    if (r.hasHierarchyUsed("screen")) {
        softurl = hierarchicalpath(softurl);
    }
    ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, softurl, null, creds, cache);
    if (doc.getStatus() == 404)
        throw new ExistException("Does not exist " + softurl);
    if (doc.getStatus() == 403) {
        // permission error - keep calm and carry on with what we can glean
        out.put("displayName", getDisplayNameKey());
        out.put("csid", csid);
        out.put("recordtype", r.getWebURL());
        return out;
    }
    if (doc.getStatus() > 299)
        throw new UnderlyingStorageException("Could not retrieve vocabulary status=" + doc.getStatus(), doc.getStatus(), softurl);
    String name = null;
    String refid = null;
    String termStatus = null;
    String parentcsid = null;
    String shortIdentifier = "";
    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 result = doc.getDocument(record_path[0].trim());
        if (result != null) {
            if ("common".equals(section)) {
                // XXX hardwired :(
                String dnXPath = getDisplayNameXPath();
                name = result.selectSingleNode(tag_path[1] + "/" + dnXPath).getText();
                if (result.selectSingleNode(tag_path[1] + "/shortIdentifier") != null) {
                    shortIdentifier = result.selectSingleNode(tag_path[1] + "/shortIdentifier").getText();
                }
                refid = result.selectSingleNode(tag_path[1] + "/refName").getText();
                // We need to replace this with the same model as for displayName
                if (result.selectSingleNode(tag_path[1] + "/termStatus") != null) {
                    termStatus = result.selectSingleNode(tag_path[1] + "/termStatus").getText();
                } else {
                    termStatus = "";
                }
                csid = result.selectSingleNode(tag_path[1] + "/csid").getText();
                parentcsid = result.selectSingleNode(tag_path[1] + "/inAuthority").getText();
                XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
            } else {
                XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
            }
        } else {
            log.warn(String.format("XML Payload for '%s' was missing part '%s'.", url, record_path[0]));
        }
    }
    // 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(r, doc, out, csid);
    // get related sub records?
    for (FieldSet fs : r.getAllSubRecords("GET")) {
        Record sr = fs.usesRecordId();
        // sr.getID()
        if (sr.isType("authority")) {
            String getPath = url + "/" + sr.getServicesURL();
            JSONArray subout = get(storage, creds, cache, url, getPath, sr);
            if (fs instanceof Field) {
                JSONObject fielddata = subout.getJSONObject(0);
                Iterator<String> rit = fielddata.keys();
                while (rit.hasNext()) {
                    String key = rit.next();
                    out.put(key, fielddata.get(key));
                }
            } else if (fs instanceof Group) {
                if (subout.length() > 0) {
                    out.put(fs.getID(), subout.getJSONObject(0));
                }
            } else {
                out.put(fs.getID(), subout);
            }
        }
    }
    // csid = urn_processor.deconstructURN(refid,false)[4];
    out.put(getDisplayNameKey(), name);
    out.put("csid", csid);
    out.put("refid", refid);
    RefName.AuthorityItem item = RefName.AuthorityItem.parse(refid);
    out.put("namespace", item.getParentShortIdentifier());
    out.put("shortIdentifier", shortIdentifier);
    out.put("termStatus", termStatus);
    out.put("authorityid", parentcsid);
    out.put("recordtype", r.getWebURL());
    return out;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) RefName(org.collectionspace.services.common.api.RefName) JSONArray(org.json.JSONArray) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) 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)

Example 28 with ExistException

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

the class ConfiguredVocabStorage method miniViewAbstract.

@Override
protected JSONObject miniViewAbstract(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, JSONObject out, String servicepath, String filePath) throws UnderlyingStorageException {
    try {
        // actually use cache
        String cachelistitem = "/" + servicepath;
        if (filePath != null) {
            cachelistitem = cachelistitem + "/" + filePath;
        }
        if (!cachelistitem.startsWith("/")) {
            cachelistitem = "/" + cachelistitem;
        }
        String dnName = getDisplayNameKey();
        String g1 = getGleanedValue(cache, cachelistitem, "refName");
        String g2 = getGleanedValue(cache, cachelistitem, "shortIdentifier");
        String g3 = getGleanedValue(cache, cachelistitem, dnName);
        String g4 = getGleanedValue(cache, cachelistitem, "csid");
        String g5 = getGleanedValue(cache, cachelistitem, "termStatus");
        String g6 = getGleanedValue(cache, cachelistitem, "workflow");
        if (g1 == null || g2 == null || g3 == null || g4 == null || g5 == null) {
            if (log.isWarnEnabled()) {
                StringBuilder sb = new StringBuilder();
                sb.append("ConfiguredVocabStorage fanning out ");
                if (g2 != null) {
                    sb.append("(shId:");
                    sb.append(g2);
                    sb.append(")");
                }
                if (g4 != null) {
                    sb.append("(csid:");
                    sb.append(g4);
                    sb.append(")");
                }
                sb.append(", as could not get: ");
                if (g1 == null)
                    sb.append("refName,");
                if (g2 == null)
                    sb.append("shortIdentifier,");
                if (g3 == null)
                    sb.append("dnName,");
                if (g4 == null)
                    sb.append("csid,");
                if (g5 == null)
                    sb.append("termStatus,");
                log.warn(sb.toString());
            }
            JSONObject cached = get(storage, creds, cache, servicepath, filePath);
            g1 = cached.getString("refid");
            g2 = cached.getString("shortIdentifier");
            g3 = cached.getString(dnName);
            g4 = cached.getString("csid");
            g5 = cached.getString("termStatus");
        }
        out.put(dnName, g3);
        out.put("refid", g1);
        out.put("csid", g4);
        out.put("termStatus", g5);
        out.put("workflow", g6);
        // out.put("authorityid", cached.get("authorityid"));
        out.put("shortIdentifier", g2);
        out.put("recordtype", r.getWebURL());
        RefName.AuthorityItem item = RefName.AuthorityItem.parse(g1);
        out.put("namespace", item.getParentShortIdentifier());
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (ExistException e) {
        throw new UnderlyingStorageException("ExistException exception" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("JSONException exception" + e.getLocalizedMessage(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) RefName(org.collectionspace.services.common.api.RefName) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 29 with ExistException

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

the class AuthorizationStorage method deleteJSON.

/**
 * Remove an object in the Service Layer.
 */
@Override
public void deleteJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        int status = 0;
        if (r.hasDeleteMethod()) {
            // get all data to post back
            // hopefully the filepath we have is the filepath to get the data from
            Document doc = null;
            try {
                doc = simpleRetrieveXML(creds, cache, filePath + "/");
                String nodepath = "/" + r.getServicesListPath() + "/*";
                List<Node> nodes = doc.selectNodes(nodepath);
                if (nodes.size() > 0) {
                    status = 201;
                    status = conn.getNone(RequestMethod.DELETE, filePath, null, creds, cache);
                // post it back to delete it
                /*
						log.info("TRYING TO DELETE ACCOUNTROLE");
						log.info(doc.asXML());
						log.info(filePath+"?_method=delete");
						ReturnedURL url = null;
						url = conn.getURL(RequestMethod.POST, filePath+"?_method=delete", doc, creds, cache);
						status = url.getStatus();
						//status=conn.getNone(RequestMethod.POST,r.getServicesURL()+"/"+filePath+"?_method=delete",doc,creds,cache);
						 	
	*/
                } else {
                    // if we didn't need to delete it because there was nothing there then everything was good
                    status = 201;
                }
            } catch (ExistException ex) {
                // ignore as nothign to delete if nothing exists
                status = 201;
            }
        } else {
            status = conn.getNone(RequestMethod.DELETE, r.getServicesURL() + "/" + filePath, null, creds, cache);
        }
        if (// XXX CSPACE-73, should be 404
        status > 299 || status < 200)
            throw new UnderlyingStorageException("Service layer exception status=" + status, status, r.getServicesURL() + "/" + filePath);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception", e);
    }
}
Also used : Node(org.dom4j.Node) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 30 with ExistException

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

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