Search in sources :

Example 76 with UnderlyingStorageException

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

the class XmlJsonConversion method addRepeatToXml.

private static void addRepeatToXml(Element root, Repeat repeat, JSONObject in, String section, String permlevel) throws JSONException, UnderlyingStorageException {
    if (repeat.isServicesReadOnly()) {
        // Omit fields that are read-only in the services layer.
        log.debug("Omitting services-readonly repeat: " + repeat.getID());
        return;
    }
    Element element = root;
    if (repeat.hasServicesParent()) {
        for (String path : repeat.getServicesParent()) {
            if (path != null) {
                element = element.addElement(path);
            }
        }
    } else if (!repeat.getXxxServicesNoRepeat()) {
        // Sometimes the UI is ahead of the services layer
        element = root.addElement(repeat.getServicesTag());
    }
    Object value = null;
    if (repeat.getXxxUiNoRepeat()) {
        // and sometimes the Servcies ahead of teh UI
        FieldSet[] children = repeat.getChildren(permlevel);
        if (children.length == 0)
            return;
        addFieldSetToXml(element, children[0], in, section, permlevel);
        return;
    } else {
        value = in.opt(repeat.getID());
    }
    if (value == null || ((value instanceof String) && StringUtils.isBlank((String) value)))
        return;
    if (value instanceof String) {
        // And sometimes the services ahead of the UI
        JSONArray next = new JSONArray();
        next.put(value);
        value = next;
    }
    if (!(value instanceof JSONArray))
        throw new UnderlyingStorageException("Bad JSON in repeated field: must be string or array for repeatable field" + repeat.getID());
    JSONArray array = (JSONArray) value;
    // XXX this will be changed when service layer accepts non-initial values as primary
    if (repeat.hasPrimary()) {
        Stack<Object> orderedarray = new Stack<Object>();
        for (int i = 0; i < array.length(); i++) {
            Object one_value = array.get(i);
            if (one_value instanceof JSONObject) {
                if (((JSONObject) one_value).has("_primary")) {
                    if (((JSONObject) one_value).getBoolean("_primary")) {
                        orderedarray.add(0, one_value);
                        continue;
                    }
                }
            }
            orderedarray.add(one_value);
        }
        JSONArray newarray = new JSONArray();
        int j = 0;
        for (Object obj : orderedarray) {
            newarray.put(j, obj);
            j++;
        }
        array = newarray;
    }
    Element repeatelement = element;
    for (int i = 0; i < array.length(); i++) {
        if (repeat.hasServicesParent()) {
            repeatelement = element.addElement(repeat.getServicesTag());
        }
        Object one_value = array.get(i);
        if (one_value == null || ((one_value instanceof String) && StringUtils.isBlank((String) one_value)))
            continue;
        if (one_value instanceof String) {
            // Assume it's just the first entry (useful if there's only one)
            FieldSet[] fs = repeat.getChildren(permlevel);
            if (fs.length < 1)
                continue;
            JSONObject d1 = new JSONObject();
            d1.put(fs[0].getID(), one_value);
            addFieldSetToXml(repeatelement, fs[0], d1, section, permlevel);
        } else if (one_value instanceof JSONObject) {
            List<FieldSet> children = getChildrenWithGroupFields(repeat, permlevel);
            for (FieldSet fs : children) addFieldSetToXml(repeatelement, fs, (JSONObject) one_value, section, permlevel);
        }
    }
    element = repeatelement;
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Element(org.dom4j.Element) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Stack(java.util.Stack)

Example 77 with UnderlyingStorageException

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

the class XmlJsonConversion method addGroupToXml.

// XXX could refactor this and addRepeatToXML as this is what happens in the middle of addRepeatToXML
private static void addGroupToXml(Element root, Group group, JSONObject in, String section, String permlevel) throws JSONException, UnderlyingStorageException {
    if (group.isServicesReadOnly()) {
        // Omit fields that are read-only in the services layer.
        log.debug("Omitting services-readonly group: " + group.getID());
        return;
    }
    Element element = root;
    if (group.hasServicesParent()) {
        for (String path : group.getServicesParent()) {
            if (path != null) {
                element = element.addElement(path);
            }
        }
    }
    Object value = null;
    value = in.opt(group.getID());
    if (value == null || ((value instanceof String) && StringUtils.isBlank((String) value)))
        return;
    if (value instanceof String) {
        // And sometimes the services ahead of the UI
        JSONObject next = new JSONObject();
        next.put(group.getID(), value);
        value = next;
    }
    if (!(value instanceof JSONObject))
        throw new UnderlyingStorageException("Bad JSON in repeated field: must be string or object for group field not an array - that would a repeat field");
    JSONObject object = (JSONObject) value;
    Element groupelement = element;
    groupelement = element.addElement(group.getServicesTag());
    Object one_value = object;
    if (one_value == null || ((one_value instanceof String) && StringUtils.isBlank((String) one_value))) {
    // do nothing
    } else if (one_value instanceof String) {
        // Assume it's just the first entry (useful if there's only one)
        FieldSet[] fs = group.getChildren(permlevel);
        if (fs.length < 1) {
        // do nothing
        } else {
            JSONObject d1 = new JSONObject();
            d1.put(fs[0].getID(), one_value);
            addFieldSetToXml(groupelement, fs[0], d1, section, permlevel);
        }
    } else if (one_value instanceof JSONObject) {
        List<FieldSet> children = getChildrenWithGroupFields(group, permlevel);
        for (FieldSet fs : children) addFieldSetToXml(groupelement, fs, (JSONObject) one_value, section, permlevel);
    }
    element = groupelement;
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Element(org.dom4j.Element) JSONObject(org.json.JSONObject) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

Example 78 with UnderlyingStorageException

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

the class AuthorizationStorage method refViewRetrieveJSON.

@Override
public JSONObject refViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
    try {
        JSONObject out = new JSONObject();
        // not all the records need a reference, look in cspace-config.xml for which that don't
        if (r.hasTermsUsed()) {
            String path = r.getServicesURL() + "/" + filePath + "/authorityrefs";
            ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
            if (all.getStatus() != 200)
                throw new ConnectionException("Bad request problem in AuthorizationStorage/refViewRetrieve: status not 200", all.getStatus(), path);
            Document list = all.getDocument();
            for (Object node : list.selectNodes("authority-ref-list/authority-ref-item")) {
                if (!(node instanceof Element))
                    continue;
                String key = ((Element) node).selectSingleNode("sourceField").getText();
                String uri = ((Element) node).selectSingleNode("uri").getText();
                String refname = ((Element) node).selectSingleNode("refName").getText();
                if (uri != null && uri.startsWith("/"))
                    uri = uri.substring(1);
                JSONObject data = miniForURI(storage, creds, cache, refname, uri, restrictions);
                out.put(key, data);
            }
        }
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection problem in AuthorizationStorage:" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) Element(org.dom4j.Element) JSONObject(org.json.JSONObject) 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 79 with UnderlyingStorageException

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

the class AuthorizationStorage method updateJSON.

@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Map<String, Document> parts = new HashMap<String, Document>();
        Document doc = null;
        for (String section : r.getServicesRecordPathKeys()) {
            String path = r.getServicesRecordPath(section);
            String[] record_path = path.split(":", 2);
            doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "PUT");
            parts.put(record_path[0], doc);
        }
        int status = 0;
        if (r.isMultipart()) {
            ReturnedMultipartDocument docm = conn.getMultipartXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, parts, creds, cache);
            status = docm.getStatus();
        } else {
            ReturnedDocument docm = conn.getXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, doc, creds, cache);
            status = docm.getStatus();
        }
        if (status == 404)
            throw new ExistException("Not found: " + r.getServicesURL() + "/" + filePath);
        if (status > 299 || status < 200)
            throw new UnderlyingStorageException("Bad response " + status, status, r.getServicesURL() + "/" + filePath);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception", e);
    } catch (JSONException e) {
        throw new UnimplementedException("JSONException", e);
    }
}
Also used : 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) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 80 with UnderlyingStorageException

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

the class AuthoritiesVocabulariesInitialize method createIfMissingAuthority.

public int createIfMissingAuthority(Storage storage, StringBuffer tty, Record record, Instance instance) throws ExistException, UnimplementedException, UIException, JSONException, UnderlyingStorageException {
    int result = HttpStatus.SC_OK;
    String url = record.getID() + "/" + instance.getTitleRef();
    try {
        storage.getPathsJSON(url, new JSONObject()).toString();
        if (tty != null) {
            log.debug("--- Instance " + instance.getID() + " exists.");
            tty.append("--- Instance " + instance.getID() + " exists.\n");
        }
    } catch (UnderlyingStorageException e) {
        if (e.getStatus() == HttpStatus.SC_NOT_FOUND) {
            // assume we're going to fail
            failedPosts++;
            log.info("Need to create instance " + fInstance.getID());
            if (tty != null) {
                tty.append("Need to create instance " + fInstance.getID() + '\n');
            }
            JSONObject fields = new JSONObject("{'displayName':'" + instance.getTitle() + "', 'shortIdentifier':'" + instance.getWebURL() + "'}");
            String base = record.getID();
            storage.autocreateJSON(base, fields, null);
            // We succeeded, so subtract our assumed failure
            failedPosts--;
            log.info("Instance " + instance.getID() + " created.");
            if (tty != null) {
                tty.append("Instance " + instance.getID() + " created.\n");
            }
        } else if (e.getStatus() == HttpStatus.SC_FORBIDDEN) {
            // We don't have the permissions needed to see if the instance exists.
            result = -1;
        } else {
            throw e;
        }
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) 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