Search in sources :

Example 21 with UnderlyingStorageException

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

the class ServicesRelationStorage method getPathsJSON.

@Override
@SuppressWarnings("unchecked")
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    JSONObject out = new JSONObject();
    Boolean isHierarchical = false;
    if (isPathType(rootPath, new String[] { "main" }, 0)) {
        extractPaths(rootPath, new String[] { "main" }, 0);
    } else if (isPathType(rootPath, new String[] { "hierarchical" }, 0)) {
        extractPaths(rootPath, new String[] { "hierarchical" }, 0);
        isHierarchical = true;
    }
    try {
        JSONObject moredata = new JSONObject();
        List<String> list = new ArrayList<String>();
        ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, "/relations?" + searchPath(restrictions), null, creds, cache);
        Document doc = data.getDocument();
        if (doc == null)
            throw new UnderlyingStorageException("Could not retrieve relation, missing relations_common");
        JSONObject pagination = new JSONObject();
        String xmlroot = "relations-common-list";
        List<Node> nodes = doc.getDocument().selectNodes("/" + xmlroot + "/*");
        for (Node node : nodes) {
            if ("relation-list-item".equals(node.getName())) {
                // if(post_filter(creds,cache,restrictions,node))
                list.add(node.selectSingleNode("csid").getText());
                if (isHierarchical) {
                    JSONObject hdata = new JSONObject();
                    Node subjectNode = node.selectSingleNode("subject");
                    Node objectNode = node.selectSingleNode("object");
                    hdata.put("subjecturi", subjectNode.selectSingleNode("uri").getText());
                    hdata.put("objecturi", objectNode.selectSingleNode("uri").getText());
                    hdata.put("subjectcsid", subjectNode.selectSingleNode("csid").getText());
                    hdata.put("objectcsid", objectNode.selectSingleNode("csid").getText());
                    findNameUnderNode(hdata, "subjectname", "subjectrefname", subjectNode);
                    findNameUnderNode(hdata, "objectname", "objectrefname", objectNode);
                    hdata.put("type", node.selectSingleNode("predicate").getText());
                    hdata.put("csid", node.selectSingleNode("csid").getText());
                    moredata.put(node.selectSingleNode("csid").getText(), hdata);
                }
            } else {
                pagination.put(node.getName(), node.getText());
            }
        }
        out.put("pagination", pagination);
        out.put("listItems", list.toArray(new String[0]));
        out.put("moredata", moredata);
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl());
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Could not retrieve relation", e);
    }
}
Also used : JSONObject(org.json.JSONObject) Node(org.dom4j.Node) ArrayList(java.util.ArrayList) 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) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 22 with UnderlyingStorageException

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

the class ServicesRelationStorage method updateJSON.

@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject data, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String[] parts = extractPaths(filePath, new String[] { "main" }, 1);
        Map<String, Document> in = new HashMap<String, Document>();
        Document datapath = dataToRelation(cache, parts[0], data).toDocument();
        in.put("relations_common", datapath);
        // log.info("UPDATE"+datapath.asXML());
        // log.info("UPDATE"+"/relations/"+parts[0]);
        ReturnedMultipartDocument out = conn.getMultipartXMLDocument(RequestMethod.PUT, "/relations/" + parts[0], in, creds, cache);
        if (out.getStatus() == 404)
            throw new ExistException("Not found");
        if (out.getStatus() > 299)
            throw new UnderlyingStorageException("Could not update relation", out.getStatus(), "/relations/" + parts[0]);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Could not update relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Could not retrieve data" + e.getLocalizedMessage(), e);
    }
}
Also used : ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) HashMap(java.util.HashMap) 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) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 23 with UnderlyingStorageException

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

the class ServicesRelationStorage method splitTypeFromId.

private String[] splitTypeFromId(String path) throws UnderlyingStorageException {
    String[] out = path.split("/");
    if (out[0].equals("")) {
        path = path.substring(1);
        out = path.split("/");
    }
    if (out.length != 2)
        throw new UnderlyingStorageException("Path must be two components, not " + path);
    return out;
}
Also used : UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

Example 24 with UnderlyingStorageException

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

the class ServicesRelationStorage method getPaths.

@Override
@SuppressWarnings("unchecked")
public String[] getPaths(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    extractPaths(rootPath, new String[] { "main" }, 0);
    try {
        List<String> out = new ArrayList<String>();
        ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, "/relations/" + searchPath(restrictions), null, creds, cache);
        Document doc = data.getDocument();
        if (doc == null)
            throw new UnderlyingStorageException("Could not retrieve relation, missing relations_common");
        List<Node> objects = doc.getDocument().selectNodes("relations-common-list/relation-list-item");
        for (Node object : objects) {
            if (post_filter(creds, cache, restrictions, object))
                out.add(object.selectSingleNode("csid").getText());
        }
        return out.toArray(new String[0]);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Could not retrieve relation" + e.getLocalizedMessage());
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) 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) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 25 with UnderlyingStorageException

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

the class ServicesRelationStorage method dataToRelation.

private Relation dataToRelation(CSPRequestCache cache, String id, JSONObject data) throws JSONException, UnderlyingStorageException {
    String[] src = splitTypeFromId(data.getString("src"));
    String[] dst = splitTypeFromId(data.getString("dst"));
    if (type_to_surl.containsKey(src[0])) {
        src[0] = type_to_surl.get(src[0]);
    }
    if (type_to_surl.containsKey(dst[0])) {
        dst[0] = type_to_surl.get(dst[0]);
    }
    String type = data.getString("type");
    if (types.containsKey(type)) {
        Relationship rel = types.get(type);
        if (!rel.hasDestinationType(dst[0]) && !rel.hasDestinationType("all")) {
            throw new UnderlyingStorageException("type " + type + " is undefined for destination:" + dst[0]);
        }
        if (!rel.hasSourceType("all") && !rel.hasSourceType(src[0])) {
            throw new UnderlyingStorageException("type " + type + " is undefined for source: " + src[0]);
        }
    } else {
        throw new UnderlyingStorageException("type " + type + " is undefined");
    }
    return factory.create(id, src[0], src[1], type, dst[0], dst[1]);
}
Also used : Relationship(org.collectionspace.chain.csp.schema.Relationship) 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