Search in sources :

Example 1 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument 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 2 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument 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 3 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.

the class UserStorage method getPaths.

@Override
@SuppressWarnings("unchecked")
public String[] getPaths(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        List<String> out = new ArrayList<String>();
        Iterator rit = restrictions.keys();
        StringBuffer args = new StringBuffer();
        while (rit.hasNext()) {
            String key = (String) rit.next();
            FieldSet fs = r.getFieldTopLevel(key);
            if (!(fs instanceof Field))
                continue;
            String filter = ((Field) fs).getServicesFilterParam();
            if (filter == null)
                continue;
            args.append('&');
            args.append(filter);
            args.append('=');
            args.append(URLEncoder.encode(restrictions.getString(key), "UTF-8"));
        }
        // pagination
        String tail = args.toString();
        String path = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), tail, false, "");
        ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
        if (doc.getStatus() < 200 || doc.getStatus() > 399)
            throw new UnderlyingStorageException("Cannot retrieve account list", doc.getStatus(), path);
        Document list = doc.getDocument();
        List<Node> objects = list.selectNodes(r.getServicesListPath());
        for (Node object : objects) {
            List<Node> fields = object.selectNodes("*");
            String csid = object.selectSingleNode("csid").getText();
            for (Node field : fields) {
                if ("csid".equals(field.getName())) {
                    int idx = csid.lastIndexOf("/");
                    if (idx != -1)
                        csid = csid.substring(idx + 1);
                    out.add(csid);
                } else if ("uri".equals(field.getName())) {
                // Skip!
                } else {
                    String json_name = view_map.get(field.getName());
                    if (json_name != null) {
                        String value = field.getText();
                        // XXX hack to cope with multi values
                        if (value == null || "".equals(value)) {
                            List<Node> inners = field.selectNodes("*");
                            for (Node n : inners) {
                                value += n.getText();
                            }
                        }
                        setGleanedValue(cache, r.getServicesURL() + "/" + csid, json_name, value);
                    }
                }
            }
        }
        return out.toArray(new String[0]);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Document(org.dom4j.Document) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 4 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.

the class ConfiguredVocabStorage method getPathsJSON.

/**
 * Returns JSON containing pagenumber, pagesize, itemsinpage, totalitems and the list of items itself
 */
@Override
@SuppressWarnings("unchecked")
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        JSONObject out = new JSONObject();
        List<String> list = new ArrayList<String>();
        String url;
        if (rootPath.isEmpty()) {
            url = "/" + r.getServicesURL() + ALL_VOCAB_ITEMS;
        } else {
            String vocab = RefName.shortIdToPath(rootPath);
            url = "/" + r.getServicesURL() + "/" + vocab + ITEMS_SUFFIX;
        }
        String path = getRestrictedPath(url, restrictions, r.getServicesSearchKeyword(), "", true, getDisplayNameKey());
        boolean excludeSoftDeleted = true;
        if (restrictions.has("deleted")) {
            excludeSoftDeleted = !restrictions.getBoolean("deleted");
        }
        if (excludeSoftDeleted && r.hasSoftDeleteMethod()) {
            path = softpath(path);
        }
        ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
        Document doc = data.getDocument();
        if (doc == null) {
            throw new UnderlyingStorageException("Could not retrieve vocabulary items", data.getStatus(), path);
        }
        String[] tag_parts = r.getServicesListPath().split(",", 2);
        String listItemPath = tag_parts[1];
        String[] listItemPathElements = listItemPath.split("/");
        if (listItemPathElements.length != 2) {
            throw new RuntimeException("Illegal list item path " + listItemPath);
        }
        String listNodeName = listItemPathElements[0];
        String listItemNodeName = listItemPathElements[1];
        String listNodeChildrenSelector = "/" + listNodeName + "/*";
        JSONObject pagination = new JSONObject();
        String[] allfields = null;
        String fieldsReturnedName = r.getServicesFieldsPath();
        List<Node> nodes = doc.selectNodes(listNodeChildrenSelector);
        for (Node node : nodes) {
            if (listItemNodeName.equals(node.getName())) {
                // Risky hack - assumes displayName must be at root. Really should
                // understand that the list results are a different schema from record GET.
                String dnName = getDisplayNameKey();
                String csid = node.selectSingleNode("csid").getText();
                list.add(csid);
                String urlPlusCSID = url + "/" + csid;
                List<Node> nameNodes = node.selectNodes(dnName);
                String nameListValue = null;
                for (Node nameNode : nameNodes) {
                    String name = nameNode.getText();
                    if (nameListValue == null) {
                        nameListValue = name;
                    } else {
                        nameListValue = JSONUtils.appendWithArraySeparator(nameListValue, name);
                    }
                }
                if (nameListValue == null) {
                    throw new JSONException("No displayNames found!");
                } else {
                    String json_name = view_map.get(dnName);
                    setGleanedValue(cache, urlPlusCSID, json_name, nameListValue);
                }
                List<Node> fields = node.selectNodes("*[(name()!='" + dnName + "')]");
                for (Node field : fields) {
                    String json_name = view_map.get(field.getName());
                    if (json_name != null) {
                        String value = field.getText();
                        // XXX hack to cope with multi values
                        if (value == null || "".equals(value)) {
                            List<Node> inners = field.selectNodes("*");
                            for (Node n : inners) {
                                value += n.getText();
                            }
                        }
                        setGleanedValue(cache, urlPlusCSID, json_name, value);
                    }
                }
                if (allfields == null || allfields.length == 0) {
                    log.warn("Missing fieldsReturned value - may cause fan-out!");
                } else {
                    // Mark all the fields not yet found as gleaned -
                    for (String s : allfields) {
                        String gleaned = getGleanedValue(cache, urlPlusCSID, s);
                        if (gleaned == null) {
                            setGleanedValue(cache, urlPlusCSID, s, "");
                        }
                    }
                }
            } else if (fieldsReturnedName.equals(node.getName())) {
                String myfields = node.getText();
                allfields = myfields.split("\\|");
            } else {
                pagination.put(node.getName(), node.getText());
            }
        }
        out.put("pagination", pagination);
        out.put("listItems", list.toArray(new String[0]));
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnderlyingStorageException("UTF-8 not supported!?" + e.getLocalizedMessage());
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Error parsing JSON" + e.getLocalizedMessage());
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) JSONObject(org.json.JSONObject) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 5 with ReturnedDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument in project application by collectionspace.

the class VocabInstanceCache method buildVocabularies.

// XXX pagination? argh
@SuppressWarnings("unchecked")
private void buildVocabularies(CSPRequestCredentials creds, CSPRequestCache cache) throws ConnectionException, UnderlyingStorageException {
    ReturnedDocument data = conn.getXMLDocument(RequestMethod.GET, "/" + r.getServicesURL() + "/", null, creds, cache);
    Document doc = data.getDocument();
    if (doc == null)
        throw new UnderlyingStorageException("Could not retrieve vocabularies");
    String[] path_parts = r.getServicesInstancesPath().split(":", 2);
    String[] tag_parts = path_parts[1].split(",", 2);
    List<Node> objects = doc.getDocument().selectNodes(tag_parts[1]);
    for (Node object : objects) {
        String name = "MISSING";
        if (null != object.selectSingleNode("displayName")) {
            name = object.selectSingleNode("displayName").getText();
        }
        if (null == object.selectSingleNode("shortIdentifier")) {
            continue;
        }
        String base = object.selectSingleNode("shortIdentifier").getText();
        if (base == null)
            continue;
        if (!vocabs.containsKey(base)) {
            vocabs.put(base, name);
        }
        csids.put(base, object.selectSingleNode("csid").getText());
    }
}
Also used : Node(org.dom4j.Node) Document(org.dom4j.Document) 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)

Aggregations

ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)28 Document (org.dom4j.Document)24 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)20 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)18 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)17 Node (org.dom4j.Node)16 JSONException (org.json.JSONException)14 JSONObject (org.json.JSONObject)14 HashMap (java.util.HashMap)10 ArrayList (java.util.ArrayList)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)8 List (java.util.List)4 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)4 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)4 Field (org.collectionspace.chain.csp.schema.Field)3 JSONArray (org.json.JSONArray)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 ReturnUnknown (org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown)2