Search in sources :

Example 36 with Field

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

the class UISpec method actualExternalURL.

/**
 * This is a bit of JSON needed by the UI so they display fields decorated
 * with external URL-handling behavior in the UIspec
 * @param f
 * @param context
 * @return
 * @throws JSONException
 */
private JSONObject actualExternalURL(FieldSet fs, UISpecRunContext context, JSONArray decorators) throws JSONException {
    JSONObject out = new JSONObject();
    Field f = (Field) fs;
    JSONObject decorator = getDecorator("fluid", null, "cspace.externalURL", null, f.isReadOnly());
    if (!f.isRefactored()) {
        if (f.hasContainer()) {
            decorator.put("container", getSelector(f, context));
        }
    }
    if (decorators == null) {
        decorators = new JSONArray();
    }
    decorators.put(decorator);
    out.put(DECORATORS_KEY, decorators);
    out.put("value", actualFieldEntry(f, context));
    return out;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 37 with Field

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

the class UISpec method actualWorkflowState.

private JSONObject actualWorkflowState(FieldSet fs, UISpecRunContext context, JSONArray decorators) throws JSONException {
    JSONObject out = new JSONObject();
    Field f = (Field) fs;
    JSONObject decorator = getDecorator("fluid", null, "cspace.util.workflowToStyleFieldConverter", null, f.isReadOnly());
    if (!f.isRefactored()) {
        if (f.hasContainer()) {
            decorator.put("container", getSelector(f, context));
        }
    }
    if (decorators == null) {
        decorators = new JSONArray();
    }
    decorators.put(decorator);
    out.put(DECORATORS_KEY, decorators);
    out.put("value", actualFieldEntry(f, context));
    return out;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 38 with Field

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

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

the class ConfiguredVocabStorage method autocreateJSON.

@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Map<String, Document> body = new HashMap<String, Document>();
        String vocab = null;
        String pathurl = "/" + r.getServicesURL() + "/";
        if (filePath.equals("")) {
            for (String section : r.getServicesInstancesPaths()) {
                String path = r.getServicesInstancesPath(section);
                String[] record_path = path.split(":", 2);
                String[] tag_path = record_path[1].split(",", 2);
                Document temp = createEntry(section, tag_path[0], tag_path[1], jsonObject, vocab, null, r, true);
                if (temp != null) {
                    body.put(record_path[0], temp);
                // log.info(temp.asXML());
                }
            }
        } else {
            vocab = RefName.shortIdToPath(filePath);
            pathurl = "/" + r.getServicesURL() + "/" + vocab + ITEMS_SUFFIX;
            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 temp = createEntry(section, tag_path[0], tag_path[1], jsonObject, vocab, null, r, false);
                if (temp != null) {
                    body.put(record_path[0], temp);
                // log.info(temp.asXML());
                }
            }
        }
        handleHierarchyPayloadSend(r, body, jsonObject, null);
        ReturnedURL out = conn.getMultipartURL(RequestMethod.POST, pathurl, body, creds, cache);
        if (out.getStatus() > 299)
            throw new UnderlyingStorageException("Could not create vocabulary", out.getStatus(), pathurl);
        String csid = out.getURLTail();
        // create related sub records?
        for (FieldSet fs : r.getAllSubRecords("POST")) {
            Record sr = fs.usesRecordId();
            // sr.getID()
            if (sr.isType("authority")) {
                String savePath = out.getURL() + "/" + sr.getServicesURL();
                if (fs instanceof Field) {
                    // get the fields form inline XXX untested - might not work...
                    JSONObject subdata = new JSONObject();
                    // loop thr jsonObject and find the fields I need
                    for (FieldSet subfs : sr.getAllFieldTopLevel("POST")) {
                        String key = subfs.getID();
                        if (jsonObject.has(key)) {
                            subdata.put(key, jsonObject.get(key));
                        }
                    }
                    subautocreateJSON(root, creds, cache, sr, subdata, savePath);
                } else if (fs instanceof Group) {
                    // JSONObject
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONObject) {
                            JSONObject subrecord = (JSONObject) subdata;
                            subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                        } else {
                            log.warn("autocreateJSON: Contact subrecord is malformed (not a JSONObject)!");
                            if (log.isDebugEnabled()) {
                                log.debug("autocreateJSON: Contact subrecord: " + subdata.toString());
                            }
                        }
                    }
                } else {
                    // JSONArray
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONArray) {
                            JSONArray subarray = (JSONArray) subdata;
                            for (int i = 0; i < subarray.length(); i++) {
                                JSONObject subrecord = subarray.getJSONObject(i);
                                subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                            }
                        }
                    }
                }
            }
        }
        return csid;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Cannot parse surrounding JSON" + e.getLocalizedMessage(), e);
    }
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) Group(org.collectionspace.chain.csp.schema.Group) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) 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) 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) JSONObject(org.json.JSONObject) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 40 with Field

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

the class VocabRedirector method pathForAll.

/**
 * Returns information on the vocabularies (Authority namespaces) that are configured
 * for autocomplete use for the passed field, in the context record.
 * If the record is itself an Authority term editor, hierarchy fields should
 * be constrained to the vocabulary named by vocabConstraint
 * @param fieldname The name of the field for which to return the info
 * @param vocabConstraint The vocabulary when the field is hierarchic and the record is a term.
 * @return Information on the configured vocabularies
 * @throws JSONException
 */
private JSONArray pathForAll(String fieldname, String vocabConstraint) throws JSONException {
    JSONArray out = new JSONArray();
    FieldSet fd = r.getFieldFullList(fieldname);
    Instance[] allInstances = null;
    if (fd == null || !(fd instanceof Field)) {
        if (r.hasHierarchyUsed("screen")) {
            // Configures the hierarchy section.
            Structure s = r.getStructure("screen");
            if (s.hasOption(fieldname)) {
                // This is one of the hierarchy fields
                if (vocabConstraint != null) {
                    allInstances = new Instance[1];
                    String fullname = r.getID() + "-" + vocabConstraint;
                    allInstances[0] = r.getSpec().getInstance(fullname);
                } else {
                    Option a = s.getOption(fieldname);
                    String[] data = a.getName().split(",");
                    allInstances = new Instance[data.length];
                    for (int i = 0; i < data.length; i++) {
                        allInstances[i] = (r.getSpec().getInstance(data[i]));
                    }
                }
            } else {
                FieldSet fs = r.getSpec().getRecord("hierarchy").getFieldFullList(fieldname);
                if (fs instanceof Field) {
                    allInstances = ((Field) fs).getAllAutocompleteInstances();
                }
            }
        }
    } else {
        allInstances = ((Field) fd).getAllAutocompleteInstances();
    }
    for (Instance autoc : allInstances) {
        if (autoc != null) {
            JSONObject instance = new JSONObject();
            instance.put("url", "/vocabularies/" + autoc.getWebURL());
            instance.put("type", autoc.getID());
            instance.put("fullName", autoc.getTitle());
            out.put(instance);
        } else {
            log.debug(String.format("A vocab/authority instance for autocompleting the '%s' field was null or missing.", fieldname));
        }
    }
    return out;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) Option(org.collectionspace.chain.csp.schema.Option) Structure(org.collectionspace.chain.csp.schema.Structure)

Aggregations

Field (org.collectionspace.chain.csp.schema.Field)45 JSONObject (org.json.JSONObject)37 JSONArray (org.json.JSONArray)26 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)22 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)14 Group (org.collectionspace.chain.csp.schema.Group)12 Record (org.collectionspace.chain.csp.schema.Record)12 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)9 Repeat (org.collectionspace.chain.csp.schema.Repeat)9 JSONException (org.json.JSONException)9 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)7 Document (org.dom4j.Document)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)6 Element (org.dom4j.Element)6 ArrayList (java.util.ArrayList)5 Instance (org.collectionspace.chain.csp.schema.Instance)5 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)5 HashMap (java.util.HashMap)4 Option (org.collectionspace.chain.csp.schema.Option)4