Search in sources :

Example 6 with ConnectionException

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

the class UserStorage method getPathsJSON.

@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> listitems = 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, "");
        if (r.hasSoftDeleteMethod()) {
            path = softpath(path);
        }
        if (r.hasHierarchyUsed("screen")) {
            path = hierarchicalpath(path);
        }
        JSONObject data = getListView(creds, cache, path, r.getServicesListPath(), "csid", false, r);
        return data;
    } 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 : Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 7 with ConnectionException

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

the class ConfiguredVocabStorage method transitionWorkflowJSON.

@Override
public void transitionWorkflowJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String serviceurl, String workflowTransition) throws UnderlyingStorageException {
    String vocab = RefName.shortIdToPath(filePath.split("/")[0]);
    String url = null;
    try {
        url = generateURL(vocab, filePath.split("/")[1], "", this.r);
    } catch (ExistException e) {
        throw new UnderlyingStorageException("Exist exception" + e.getLocalizedMessage(), e.getStatus(), url, e);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
    super.transitionWorkflowJSON(root, creds, cache, "", url, workflowTransition);
}
Also used : ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 8 with ConnectionException

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

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

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

the class ConfiguredVocabStorage method retrieveJSON.

@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Integer num = 0;
        String[] parts = filePath.split("/");
        // deal with different url structures
        String vocab, csid;
        if ("_direct".equals(parts[0])) {
            vocab = parts[2];
            csid = parts[3];
            num = 4;
        } else {
            vocab = parts[0];
            if (!VOCAB_WILDCARD.equals(vocab)) {
                vocab = RefName.shortIdToPath(vocab);
            }
            csid = (parts.length > 1) ? parts[1] : null;
            num = 2;
        }
        if (parts.length > num) {
            String extra = "";
            Integer extradata = num + 1;
            if (parts.length > extradata) {
                extra = parts[extradata];
            }
            String servicepath = generateURL(vocab, csid, "", this.r);
            return viewRetrieveJSON(root, creds, cache, null, parts[num], extra, restrictions, servicepath);
        } else
            return simpleRetrieveJSON(root, creds, cache, vocab, csid);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException x) {
        throw new UnderlyingStorageException("Error building JSON" + x.getLocalizedMessage(), x);
    } catch (UnsupportedEncodingException x) {
        throw new UnderlyingStorageException("Error UnsupportedEncodingException JSON", x);
    }
}
Also used : JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)39 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)37 JSONException (org.json.JSONException)28 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)26 Document (org.dom4j.Document)24 JSONObject (org.json.JSONObject)23 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)21 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 ArrayList (java.util.ArrayList)11 Node (org.dom4j.Node)11 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)10 HashMap (java.util.HashMap)8 Field (org.collectionspace.chain.csp.schema.Field)8 Record (org.collectionspace.chain.csp.schema.Record)8 ExistException (org.collectionspace.csp.api.persistence.ExistException)8 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)8 JSONArray (org.json.JSONArray)8 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)6 Group (org.collectionspace.chain.csp.schema.Group)6 IOException (java.io.IOException)3