Search in sources :

Example 1 with Structure

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

the class DataGenerator method datagenerator.

/**
 * Guts of creating the data
 * @param storage
 * @param ui
 * @return
 * @throws UIException
 */
private JSONObject datagenerator(Storage storage, UIRequest ui) throws UIException {
    this.storage = storage;
    maxValue = new BigDecimal(dateToLong("2030-01-01"));
    minValue = new BigDecimal(dateToLong("1970-01-01"));
    log.info("initialize params");
    tty.line("initialize params");
    JSONObject out = new JSONObject();
    Structure s = record.getStructure(this.structureview);
    // how many records do we want to create
    if (ui.getRequestArgument("quantity") != null) {
        String quantity = ui.getRequestArgument("quantity");
        this.quant = Integer.parseInt(quantity);
    }
    // how many time do we want the repeatable group to repeat
    if (ui.getRequestArgument("repeats") != null) {
        String rstring = ui.getRequestArgument("repeats");
        Integer rnum = Integer.parseInt(rstring);
        this.repeatnum = rnum;
    }
    // how many records will we set relationships on
    if (ui.getRequestArgument("maxrelationships") != null) {
        String mxstring = ui.getRequestArgument("maxrelationships");
        Integer mxnum = Integer.parseInt(mxstring);
        this.maxrecords = mxnum;
    }
    // how many records will we set relationships on
    if (ui.getRequestArgument("startvalue") != null) {
        String startvalue = ui.getRequestArgument("startvalue");
        Integer stnum = Integer.parseInt(startvalue);
        this.startvalue = stnum;
    }
    // how many records will we set relationships on
    if (ui.getRequestArgument("extraprefix") != null) {
        String exstring = ui.getRequestArgument("extraprefix");
        this.extraprefix = exstring;
    }
    // setting this will speed up the initialization of this script
    if (ui.getRequestArgument("authoritylimit") != null) {
        String astring = ui.getRequestArgument("authoritylimit");
        Integer anum = Integer.parseInt(astring);
        this.authoritylimit = anum;
    }
    log.info("Creating " + quant.toString() + " records of type " + record.getWebURL());
    tty.line("Creating " + quant.toString() + " records of type " + record.getWebURL());
    tty.flush();
    for (Integer i = 0; i < quant; i++) {
        try {
            if ((i % 10) == 0) {
                log.info("So far up to number: " + i.toString());
                tty.line("So far up to number: " + i.toString());
                tty.flush();
            }
            out.put(i.toString(), makedata(i));
        } catch (JSONException e) {
            throw new UIException("Cannot generate UISpec due to JSONException", e);
        }
    }
    return out;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) Structure(org.collectionspace.chain.csp.schema.Structure) BigDecimal(java.math.BigDecimal)

Example 2 with Structure

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

the class UISpec method uispec.

/**
 * create the UISpec and return the JSONObject
 * @param storage
 * @return
 * @throws UIException
 */
protected JSONObject uispec(Storage storage) throws UIException {
    this.storage = storage;
    UISpecRunContext context = new UISpecRunContext();
    try {
        JSONObject out = new JSONObject();
        Structure s = record.getStructure(this.structureview);
        if (this.structureview.equals("search")) {
            out = generateUISpecRecordEditor(context, s.showMessageKey());
        } else {
            if (s.showListSection()) {
                /* used in termlist, reports, all */
                out.put(s.getListSectionName(), generateUISpecListSection(s, context));
            }
            if (s.showEditSection()) {
                out.put(s.getEditSectionName(), generateUISpecRecordEditor(context, s.showMessageKey()));
            }
            if (s.showHierarchySection()) {
                out.put(s.getHierarchySectionName(), generateUISpecHierarchySection(context, s.showMessageKey()));
            }
        }
        return out;
    } catch (JSONException e) {
        throw new UIException("Cannot generate UISpec due to JSONException", e);
    }
}
Also used : UISpecRunContext(org.collectionspace.chain.csp.schema.UISpecRunContext) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) Structure(org.collectionspace.chain.csp.schema.Structure)

Example 3 with Structure

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

the class WebAutoComplete method doAuthorityAutocomplete.

private JSONArray doAuthorityAutocomplete(CSPRequestCache cache, Storage storage, String fieldname, String start, String vocabConstraint, String pageSize, String pageNum) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException, ConfigException {
    FieldSet fs = r.getFieldFullList(fieldname);
    JSONArray out = new JSONArray();
    Instance[] allInstances = null;
    if (fs == null || !(fs 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 {
                fs = r.getSpec().getRecord("hierarchy").getFieldFullList(fieldname);
                if (fs instanceof Field) {
                    allInstances = ((Field) fs).getAllAutocompleteInstances();
                }
            }
        }
    } else {
        allInstances = ((Field) fs).getAllAutocompleteInstances();
    }
    if (allInstances == null) {
        // Cannot autocomplete
        return out;
    }
    // support multiassign of autocomplete instances
    for (Instance n : allInstances) {
        try {
            if (n == null) {
            // Field has no autocomplete
            } else {
                String path = n.getRecord().getID() + "/" + n.getTitleRef();
                JSONObject restriction = new JSONObject();
                if (pageSize != null) {
                    restriction.put("pageSize", pageSize);
                }
                if (pageNum != null) {
                    restriction.put("pageNum", pageNum);
                }
                FieldSet dispNameFS = n.getRecord().getDisplayNameField();
                if (dispNameFS == null) {
                    throw new ConfigException("WebAutoComplete for Instance has no displayName configured: " + n.getID());
                }
                String displayNameFieldID = dispNameFS.getID();
                // May be something other than display name
                restriction.put(displayNameFieldID, start);
                JSONObject results = storage.getPathsJSON(path, restriction);
                String[] paths = (String[]) results.get("listItems");
                for (String csid : paths) {
                    JSONObject data = storage.retrieveJSON(path + "/" + csid + "/view", new JSONObject());
                    JSONObject entry = new JSONObject();
                    // TODO - handle multiple name matches
                    String displayNameString = data.getString(displayNameFieldID);
                    JSONArray displayNames = JSONUtils.createJSONArrayFromSeparatedString(displayNameString);
                    String primaryDN = displayNames.getString(0);
                    String refid = data.getString("refid");
                    // HACK - transition period with full instead of base URN value
                    if (refid.endsWith("'" + primaryDN + "'"))
                        refid = refid.substring(0, refid.length() - (primaryDN.length() + 2));
                    entry.put("baseUrn", refid);
                    entry.put("csid", data.getString("csid"));
                    entry.put("type", n.getRecord().getWebURL());
                    entry.put("displayNames", displayNames);
                    // RefName.AuthorityItem item = RefName.AuthorityItem.parse(refid);
                    // entry.put("namespace",item.getParentShortIdentifier());
                    entry.put("namespace", data.getString("namespace"));
                    entry.put("workflow", data.getString("workflow"));
                    out.put(entry);
                }
            }
        } catch (UnderlyingStorageException x) {
            if (x.getStatus() == 403) {
            // permission error - keep calm and carry on
            } else {
                throw x;
            }
        }
    }
    // Instance n=((Field)fs).getAutocompleteInstance();
    return out;
}
Also used : Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) ConfigException(org.collectionspace.chain.csp.config.ConfigException) 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) Option(org.collectionspace.chain.csp.schema.Option) Structure(org.collectionspace.chain.csp.schema.Structure)

Example 4 with Structure

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

the class WebAutoComplete method autocomplete.

private void autocomplete(CSPRequestCache cache, Storage storage, UIRequest request) throws UIException {
    try {
        String[] path = request.getPrincipalPath();
        // Last path element is the field on which user is querying.
        String fieldName = path[path.length - 1];
        JSONArray out = new JSONArray();
        boolean hasHierarchy = r.hasHierarchyUsed("screen");
        boolean isHierarchyAutoComplete = false;
        if (hasHierarchy) {
            // Configures the hierarchy section.
            Structure s = r.getStructure("screen");
            if (s.hasOption(fieldName)) {
                // This is one of the hierarchy fields
                isHierarchyAutoComplete = true;
            }
        }
        if (r.isType("authority") || !isHierarchyAutoComplete) {
            out = doAuthorityAutocomplete(cache, storage, fieldName, request.getRequestArgument(AUTO_COMPLETE_QUERY_PARAM), request.getRequestArgument(CONSTRAIN_VOCAB_PARAM), request.getRequestArgument(PAGE_SIZE_PARAM), request.getRequestArgument(PAGE_NUM_PARAM));
        } else if (isHierarchyAutoComplete) {
            out = doRecordAutocomplete(cache, storage, fieldName, request.getRequestArgument(AUTO_COMPLETE_QUERY_PARAM), request.getRequestArgument(PAGE_SIZE_PARAM), request.getRequestArgument(PAGE_NUM_PARAM));
        } else {
            throw new ConfigException("WebAutoComplete called for record that does not support autocomplete!: " + r.getID());
        }
        request.sendJSONResponse(out);
    } catch (JSONException e) {
        throw new UIException("JSONException during autocompletion", e);
    } catch (ExistException e) {
        throw new UIException("ExistException during autocompletion", e);
    } catch (UnimplementedException e) {
        throw new UIException("UnimplementedException during autocompletion", e);
    } catch (ConfigException e) {
        throw new UIException("ConfigException during autocompletion", e);
    } catch (UnderlyingStorageException x) {
        throw new UIException("UnderlyingStorageException during autocompletion" + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ConfigException(org.collectionspace.chain.csp.config.ConfigException) Structure(org.collectionspace.chain.csp.schema.Structure) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 5 with Structure

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

the class SchemaStructure method makeASubRecord.

/**
 * Do all the real magic to make a sub record thing
 *
 * @param subr
 * @param out
 * @param repeated
 * @param context
 * @param parent
 * @throws JSONException
 */
protected void makeASubRecord(Record subr, JSONObject out, Boolean repeated, UISpecRunContext context, JSONObject parent) throws JSONException {
    for (FieldSet fs2 : subr.getAllFieldTopLevel("")) {
        if (repeated) {
            fs2.setRepeatSubRecord(true);
        }
        whatIsThisFieldSet(out, fs2, context);
        fs2.setRepeatSubRecord(false);
    }
    Structure s = subr.getStructure(this.structureview);
    if (s.showMessageKey()) {
        makeAllRecordMessageKey(context, parent, subr);
    }
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Structure(org.collectionspace.chain.csp.schema.Structure)

Aggregations

Structure (org.collectionspace.chain.csp.schema.Structure)6 JSONObject (org.json.JSONObject)4 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)3 UIException (org.collectionspace.csp.api.ui.UIException)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 ConfigException (org.collectionspace.chain.csp.config.ConfigException)2 Field (org.collectionspace.chain.csp.schema.Field)2 Instance (org.collectionspace.chain.csp.schema.Instance)2 Option (org.collectionspace.chain.csp.schema.Option)2 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)2 BigDecimal (java.math.BigDecimal)1 UISpecRunContext (org.collectionspace.chain.csp.schema.UISpecRunContext)1 ExistException (org.collectionspace.csp.api.persistence.ExistException)1 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)1