Search in sources :

Example 41 with FieldSet

use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.

the class XmlJsonConversion method FieldListFROMConfig.

private static List<String> FieldListFROMConfig(FieldSet f, String operation) {
    List<String> children = new ArrayList<String>();
    if (f.getUIType().startsWith("groupfield")) {
        String[] parts = f.getUIType().split("/");
        Record subitems = f.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
        for (FieldSet fs : subitems.getAllFieldTopLevel(operation)) {
            children.add(fs.getID());
        }
    }
    if (f instanceof Repeat) {
        for (FieldSet a : ((Repeat) f).getChildren(operation)) {
            if (a instanceof Repeat && ((Repeat) a).hasServicesParent()) {
                children.add(((Repeat) a).getServicesParent()[0]);
            } else if (a.getUIType().startsWith("groupfield")) {
                // structuredates etc
                String[] parts = a.getUIType().split("/");
                Record subitems = a.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
                if (a instanceof Group) {
                    if (((Group) a).getXxxServicesNoRepeat()) {
                        for (FieldSet fs : subitems.getAllFieldTopLevel(operation)) {
                            if (fs instanceof Repeat && ((Repeat) fs).hasServicesParent()) {
                                children.add(((Repeat) fs).getServicesParent()[0]);
                            } else {
                                children.add(fs.getID());
                            }
                        }
                    } else {
                        children.add(a.getID());
                    }
                }
            } else {
                children.add(a.getID());
            }
        }
    }
    if (f instanceof Group) {
        for (FieldSet ab : ((Group) f).getChildren(operation)) {
            children.add(ab.getID());
        }
    }
    if (f instanceof Field) {
    }
    return children;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) ArrayList(java.util.ArrayList) Record(org.collectionspace.chain.csp.schema.Record) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Example 42 with FieldSet

use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.

the class XmlJsonConversion method addRepeatToJson.

private static void addRepeatToJson(JSONObject out, Element root, Repeat f, String permlevel, JSONObject tempSon, String csid, String ims_url) throws JSONException {
    if (f.getXxxServicesNoRepeat()) {
        // not a repeat in services yet but is a repeat in UI
        FieldSet[] fields = f.getChildren(permlevel);
        if (fields.length == 0)
            return;
        JSONArray members = new JSONArray();
        JSONObject data = new JSONObject();
        addFieldSetToJson(data, root, fields[0], permlevel, tempSon, csid, ims_url);
        members.put(data);
        out.put(f.getID(), members);
        return;
    }
    String nodeName = f.getServicesTag();
    if (f.hasServicesParent()) {
        nodeName = f.getfullID();
        // XXX hack because of weird repeats in accountroles permroles etc
        if (f.getServicesParent().length == 0) {
            nodeName = f.getID();
        }
    }
    List<?> nodes = root.selectNodes(nodeName);
    if (nodes.size() == 0) {
        // add in empty primary tags and arrays etc to help UI
        if (f.asSibling()) {
            JSONObject repeated = new JSONObject();
            if (f.hasPrimary()) {
                repeated.put("_primary", true);
            }
            if (!out.has(f.getID())) {
                JSONArray temp = new JSONArray();
                out.put(f.getID(), temp);
            }
            out.getJSONArray(f.getID()).put(repeated);
        } else {
            JSONArray repeatitem = new JSONArray();
            out.put(f.getID(), repeatitem);
        }
        return;
    }
    // Only first element is important in container
    // except when we have repeating items
    int pos = 0;
    for (Object repeatcontainer : nodes) {
        pos++;
        Element container = (Element) repeatcontainer;
        if (f.asSibling()) {
            JSONArray repeatitem = addRepeatedNodeToJson(container, f, permlevel, tempSon);
            JSONArray temp = new JSONArray();
            if (!out.has(f.getID())) {
                out.put(f.getID(), temp);
            }
            for (int arraysize = 0; arraysize < repeatitem.length(); arraysize++) {
                JSONObject repeated = repeatitem.getJSONObject(arraysize);
                if (f.hasPrimary() && pos == 1) {
                    repeated.put("_primary", true);
                }
                out.getJSONArray(f.getID()).put(repeated);
            }
        } else {
            JSONArray repeatitem = addRepeatedNodeToJson(container, f, permlevel, tempSon);
            out.put(f.getID(), repeatitem);
        }
    }
}
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)

Example 43 with FieldSet

use of org.collectionspace.chain.csp.schema.FieldSet 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 44 with FieldSet

use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.

the class XmlJsonConversion method addSubRecordToXml.

private static void addSubRecordToXml(Element root, Field field, JSONObject in, String operation) throws JSONException, UnderlyingStorageException {
    String[] parts = field.getUIType().split("/");
    Record subitems = field.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
    // hard coding the section here can not be a good thing....
    if (subitems.getAllServiceFieldTopLevel(operation, "common").length > 0) {
        for (FieldSet f : subitems.getAllServiceFieldTopLevel(operation, "common")) {
            addFieldSetToXml(root, f, in, "common", operation);
        }
    // log.debug(root.asXML());
    // return doc;
    }
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Record(org.collectionspace.chain.csp.schema.Record)

Example 45 with FieldSet

use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.

the class XmlJsonConversion method addGroupToJson.

private static void addGroupToJson(JSONObject out, Element root, Group f, String operation, JSONObject tempSon, String csid, String ims_url) throws JSONException {
    String nodeName = f.getServicesTag();
    if (f.hasServicesParent()) {
        nodeName = f.getfullID();
        // XXX hack because of weird repeats in accountroles permroles etc
        if (f.getServicesParent().length == 0) {
            nodeName = f.getID();
        }
    }
    if (!f.isGrouped()) {
        Element el = root;
        if (f.getUIType().startsWith("groupfield") && f.getUIType().contains("selfrenderer")) {
            String[] parts = f.getUIType().split("/");
            Record subitems = f.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
            JSONObject temp = new JSONObject();
            for (FieldSet fs : subitems.getAllServiceFieldTopLevel(operation, "common")) {
                addFieldSetToJson(temp, el, fs, operation, tempSon, csid, ims_url);
            }
            out.put(f.getID(), temp);
            return;
        }
        if (f.getUIType().startsWith("groupfield")) {
            String[] parts = f.getUIType().split("/");
            Record subitems = f.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
            JSONObject temp = new JSONObject();
            for (FieldSet fs : subitems.getAllServiceFieldTopLevel(operation, "common")) {
                addFieldSetToJson(temp, el, fs, operation, tempSon, csid, ims_url);
            }
            out.put(f.getID(), temp);
        }
    } else {
        List<?> nodes = root.selectNodes(nodeName);
        if (nodes.size() == 0)
            return;
        // Only first element is important in group container
        for (Object repeatcontainer : nodes) {
            Element container = (Element) repeatcontainer;
            JSONArray repeatitem = addRepeatedNodeToJson(container, f, operation, tempSon);
            JSONObject repeated = repeatitem.getJSONObject(0);
            out.put(f.getID(), repeated);
        }
    }
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Element(org.dom4j.Element) JSONArray(org.json.JSONArray) Record(org.collectionspace.chain.csp.schema.Record) JSONObject(org.json.JSONObject)

Aggregations

FieldSet (org.collectionspace.chain.csp.schema.FieldSet)62 JSONObject (org.json.JSONObject)36 Record (org.collectionspace.chain.csp.schema.Record)23 Field (org.collectionspace.chain.csp.schema.Field)22 JSONArray (org.json.JSONArray)22 Element (org.dom4j.Element)19 Group (org.collectionspace.chain.csp.schema.Group)17 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)17 JSONException (org.json.JSONException)13 Repeat (org.collectionspace.chain.csp.schema.Repeat)11 Document (org.dom4j.Document)11 QName (org.dom4j.QName)11 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)10 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)9 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ArrayList (java.util.ArrayList)6 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)6 TenantSpec (org.collectionspace.chain.csp.persistence.services.TenantSpec)5 Spec (org.collectionspace.chain.csp.schema.Spec)5