Search in sources :

Example 26 with Record

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

the class DirectRedirector method retrieveJSON.

@Override
public JSONObject retrieveJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String path, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    /* Find relevant controller, and call */
    String[] url = path.split("/");
    RefName.AuthorityItem itemParsed = RefName.AuthorityItem.parse(url[2]);
    String thisShortid = itemParsed.getShortIdentifier();
    String thisparent = itemParsed.getParentShortIdentifier();
    String displayName = itemParsed.displayName;
    String test = itemParsed.inAuthority.resource;
    String vocab = RefName.shortIdToPath(thisparent);
    String csid = RefName.shortIdToPath(thisShortid);
    Record r = spec.getRecordByServicesUrl(itemParsed.inAuthority.resource);
    String storageID = r.getID();
    if (r.isType("vocabulary")) {
        url[0] = r.getServicesURL();
        storageID = "vocab";
    }
    if (!r.isType("authority") && !r.isType("vocabulary"))
        throw new UnimplementedException("Only authorities and vocabularies supported at direct at the moment");
    return root.retrieveJSON(root, creds, cache, storageID + "/_direct/" + url[0] + "/" + vocab + "/" + csid, restrictions);
}
Also used : RefName(org.collectionspace.services.common.api.RefName) Record(org.collectionspace.chain.csp.schema.Record) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 27 with Record

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

the class GenericStorage method subautocreateJSON.

/**
 * needs some tests.. just copied from ConfiguredVocabStorage
 * @param root
 * @param creds
 * @param cache
 * @param myr
 * @param jsonObject
 * @param savePrefix
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 */
public String subautocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, Record myr, JSONObject jsonObject, String savePrefix) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        ReturnedURL url = null;
        Document doc = null;
        // XXX I would hope this might be removed if userroles etc ever get improved to be more like the rest
        if (myr.hasPrimaryField()) {
            for (String section : myr.getServicesRecordPathKeys()) {
                doc = XmlJsonConversion.convertToXml(myr, jsonObject, section, "POST");
                String path = myr.getServicesURL();
                path = path.replace("*", getSubCsid(jsonObject, myr.getPrimaryField()));
                deleteJSON(root, creds, cache, path);
                url = conn.getURL(RequestMethod.POST, path, doc, creds, cache);
            }
        } else {
            url = autoCreateSub(creds, cache, jsonObject, doc, savePrefix, myr);
        }
        // create related sub records?
        for (FieldSet allfs : myr.getAllSubRecords("POST")) {
            Record sr = allfs.usesRecordId();
            if (sr.isType("authority")) {
            } else {
                String savePath = url.getURL() + "/" + sr.getServicesURL();
                if (jsonObject.has(sr.getID())) {
                    Object subdata = jsonObject.get(sr.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);
                        }
                    } else if (subdata instanceof JSONObject) {
                        JSONObject subrecord = (JSONObject) subdata;
                        subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                    }
                }
            }
        }
        return url.getURLTail();
    } 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) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Record(org.collectionspace.chain.csp.schema.Record) 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) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 28 with Record

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

the class DataGenerator method createAllRecords.

/**
 * Create all record types (that make sense)
 * @param storage
 * @param ui
 * @return
 * @throws UIException
 */
protected JSONObject createAllRecords(Storage storage, UIRequest ui) throws UIException {
    log.info("Lets make some records");
    tty.line("Lets make some records");
    tty.flush();
    JSONObject returnData = new JSONObject();
    try {
        for (Record r : spec.getAllRecords()) {
            if (r.isType("authority") || r.isType("authorizationdata") || r.isType("id") || r.isType("userdata")) {
            // don't do these yet (if ever)
            } else if (r.getID().equals("structureddate") || r.getID().equals("media") || r.getID().equals("hierarchy") || r.getID().equals("blobs") || r.getID().equals("dimension") || r.getID().equals("contacts") || r.isType("searchall")) {
            // and ignore these
            } else if (r.getID().equals("termlist") || r.getID().equals("termlistitem")) {
            // and ignore these
            } else {
                this.record = r;
                this.structureview = "screen";
                this.writer = new RecordCreateUpdate(r, true);
                JSONObject items = createRecords(storage, ui);
                returnData.put(r.getID(), items.getJSONObject(r.getID()));
            }
        }
        // lets create some relationships
        log.info("Initializing relationships");
        tty.line("Initializing relationships");
        tty.flush();
        createDataSetRelationships(returnData);
    } catch (JSONException x) {
        throw new UIException("Failed to parse json: ", x);
    } catch (ExistException x) {
        throw new UIException("Existence exception: ", x);
    } catch (UnimplementedException x) {
        throw new UIException("Unimplemented exception: ", x);
    } catch (UnderlyingStorageException x) {
        throw new UIException("Problem storing: " + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
    }
    return returnData;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) Record(org.collectionspace.chain.csp.schema.Record) RecordCreateUpdate(org.collectionspace.chain.csp.webui.record.RecordCreateUpdate) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 29 with Record

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

the class DataGenerator method getAuthdata.

/**
 * Return appropriate authority for the field in question
 * @param f
 * @param context
 * @return
 * @throws JSONException
 */
private JSONObject getAuthdata(FieldSet fs, UISpecRunContext context) throws JSONException {
    JSONObject dataitem = new JSONObject();
    JSONObject allnames = new JSONObject();
    Integer i = 0;
    if (fs instanceof Field) {
        for (Instance type : ((Field) fs).getAllAutocompleteInstances()) {
            String iid = type.getTitleRef();
            Record ir = type.getRecord();
            JSONArray thesenames = ctl.get(this.storage, iid, ir, authoritylimit);
            log.info("getting authority: " + type.getID() + ":" + type.getRecord());
            try {
                tty.line("getting authority: " + type.getID() + ":" + type.getRecord());
            } catch (UIException e) {
            }
            allnames.put(i.toString(), thesenames);
            i++;
        }
    }
    Random objrandom = new Random();
    Integer pickobj = objrandom.nextInt(allnames.length());
    JSONArray getallnames = allnames.getJSONArray(pickobj.toString());
    // use random number to choose which item to use
    Random arrayrandom = new Random();
    if (getallnames.length() > 0) {
        int pick = arrayrandom.nextInt(getallnames.length());
        dataitem = getallnames.getJSONObject(pick);
    }
    return dataitem;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) JSONObject(org.json.JSONObject) Random(java.util.Random) Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) UIException(org.collectionspace.csp.api.ui.UIException) Record(org.collectionspace.chain.csp.schema.Record)

Example 30 with Record

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

the class SchemaStructure method makeAGroupField.

/**
 * Do all the magic to make a GroupField
 *
 * @param fs
 * @param context
 * @throws JSONException
 */
protected JSONObject makeAGroupField(FieldSet fs, UISpecRunContext context) throws JSONException {
    JSONObject out = new JSONObject();
    if (fs instanceof Field || fs instanceof Repeat) {
        JSONObject subexpander = new JSONObject();
        JSONObject options = new JSONObject();
        String[] parts = fs.getUIType().split("/");
        Record subitems = fs.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
        UISpecRunContext sub = context.createChild();
        sub.setUIPrefix(fs.getID());
        sub.setPad(false);
        if (isAStructureDate(fs)) {
            makeAStructureDate(fs, out, subexpander, options, subitems, sub, context);
        } else if (fs.isASelfRenderer()) {
            makeASelfRenderer(fs, context, out, subexpander, options, subitems, sub);
        } else {
            makeAOtherGroup(fs, out, subexpander, options, subitems, sub);
        }
    }
    return out;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) UISpecRunContext(org.collectionspace.chain.csp.schema.UISpecRunContext) JSONObject(org.json.JSONObject) Repeat(org.collectionspace.chain.csp.schema.Repeat) Record(org.collectionspace.chain.csp.schema.Record)

Aggregations

Record (org.collectionspace.chain.csp.schema.Record)59 JSONObject (org.json.JSONObject)33 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)23 JSONArray (org.json.JSONArray)19 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)14 JSONException (org.json.JSONException)14 Field (org.collectionspace.chain.csp.schema.Field)12 Group (org.collectionspace.chain.csp.schema.Group)12 Instance (org.collectionspace.chain.csp.schema.Instance)11 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)11 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)10 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)10 Document (org.dom4j.Document)10 HashMap (java.util.HashMap)9 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)9 ExistException (org.collectionspace.csp.api.persistence.ExistException)9 Element (org.dom4j.Element)9 UIException (org.collectionspace.csp.api.ui.UIException)8 IOException (java.io.IOException)7 Repeat (org.collectionspace.chain.csp.schema.Repeat)7