Search in sources :

Example 31 with UnderlyingStorageException

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

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

the class ConfiguredVocabStorage method get.

private JSONArray get(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String url, String filePath, Record thisr) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
    JSONArray itemarray = new JSONArray();
    // get list view
    JSONObject data = getListView(creds, cache, filePath, thisr.getServicesListPath(), "csid", false, thisr);
    String[] filepaths = (String[]) data.get("listItems");
    for (String uri : filepaths) {
        String path = uri;
        if (path != null && path.startsWith("/"))
            path = path.substring(1);
        String[] parts = path.split("/");
        String recordurl = parts[0];
        String mycsid = parts[parts.length - 1];
        try {
            JSONObject itemdata = simpleRetrieveJSON(creds, cache, filePath + "/" + mycsid, "", thisr);
            // add in csid so I can do update with a modicum of confidence
            itemdata.put("_subrecordcsid", mycsid);
            itemarray.put(itemdata);
        } catch (UnimplementedException e) {
            throw new UnderlyingStorageException(e.getMessage());
        }
    }
    return itemarray;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 33 with UnderlyingStorageException

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

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

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

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