Search in sources :

Example 11 with Instance

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

the class RecordTraverser method subTraverser.

private JSONObject subTraverser(Storage storage, UIRequest request, String key, Integer numInstances, String base, String instance, JSONObject restriction) throws JSONException, UIException, ExistException, UnimplementedException, UnderlyingStorageException {
    JSONObject results = new JSONObject();
    Record myr = this.spec.getRecord(base);
    if (myr.isType("record") || myr.isType("searchall")) {
        this.searcher = new RecordSearchList(myr, RecordSearchList.MODE_SEARCH);
        // Need to set up maps for recordtype.
        this.searcher.configure(this.spec);
        results = this.searcher.getJSON(storage, restriction, key, base);
    } else if (myr.isType("authority")) {
        if (myr.hasInstance(instance)) {
            Instance myn = myr.getInstance(instance);
            this.avsearcher = new AuthoritiesVocabulariesSearchList(myn, true);
        } else {
            this.avsearcher = new AuthoritiesVocabulariesSearchList(myr, true);
        }
        if (this.avsearcher != null) {
            results = this.avsearcher.getJSON(storage, restriction, key);
        }
    }
    // cache for record traverser
    if (results.has("pagination") && results.getJSONObject("pagination").has("separatelists")) {
        GenericSearch.createTraverser(request, base, instance, results, restriction, key, numInstances);
    }
    return results;
}
Also used : RecordSearchList(org.collectionspace.chain.csp.webui.record.RecordSearchList) JSONObject(org.json.JSONObject) Instance(org.collectionspace.chain.csp.schema.Instance) Record(org.collectionspace.chain.csp.schema.Record) AuthoritiesVocabulariesSearchList(org.collectionspace.chain.csp.webui.authorities.AuthoritiesVocabulariesSearchList)

Example 12 with Instance

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

the class AuthoritiesVocabulariesInitialize method initializeVocab.

private void initializeVocab(Storage storage, UIRequest request, String path) throws UIException {
    try {
        if (fInstance == null) {
            // For now simply loop through all the instances one after the other.
            for (Instance instance : r.getAllInstances()) {
                log.info(instance.getID());
                // does instance exist?
                if (createIfMissingAuthority(storage, null, this.r, instance) == -1) {
                    log.warn(String.format("The currently authenticated user does not have sufficient permission to determine if the '%s' authority/term-list is properly initialized.", instance.getID()));
                }
                resetvocabdata(storage, request, instance);
            }
        } else {
            log.info(fInstance.getID());
            resetvocabdata(storage, request, this.fInstance);
        }
    } catch (JSONException e) {
        throw new UIException("Cannot generate JSON", e);
    } catch (ExistException e) {
        throw new UIException("Exist exception", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented exception", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : Instance(org.collectionspace.chain.csp.schema.Instance) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 13 with Instance

use of org.collectionspace.chain.csp.schema.Instance 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)

Example 14 with Instance

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

the class WebLoginStatus method getPermissions.

private JSONObject getPermissions(Storage storage) throws JSONException, UIException, ExistException, UnimplementedException, UnderlyingStorageException {
    final String WORKFLOW_DELETE_RESOURCE_TAIL = WORKFLOW_SUB_RESOURCE + "delete";
    final String WORKFLOW_LOCK_RESOURCE_TAIL = WORKFLOW_SUB_RESOURCE + "lock";
    JSONObject data = new JSONObject();
    JSONObject perms = new JSONObject();
    // spec.getRecordByWebUrl("userperm").getID();
    String permbase = "accountperms";
    String base = spec.getRecordByWebUrl("userperm").getID();
    JSONObject activePermissions = storage.retrieveJSON(base + "/0/", new JSONObject());
    // UI doesn't know what it wants to do about pagination etc
    if (activePermissions.has("account")) {
        JSONObject account = activePermissions.getJSONObject("account");
        String csid = account.getString("accountId");
        String userId = account.getString("userId");
        String tenantId = account.getString("tenantId");
        String screenName = userId;
        if (account.has("screenName")) {
            screenName = account.getString("screenName");
        }
        data.put("csid", csid);
        data.put("screenName", screenName);
        data.put("userId", userId);
        data.put("tenant", tenantId);
    }
    if (activePermissions.has("permission")) {
        JSONArray active = activePermissions.getJSONArray("permission");
        for (int j = 0; j < active.length(); j++) {
            JSONObject perm = active.getJSONObject(j);
            if (perm.has("resourceName") && perm.has("actionGroup")) {
                JSONArray newPermissionsList = null;
                String resourceName = perm.getString("resourceName");
                String resourceNameUI = null;
                // Check and filter the workflow resources
                if (resourceName.endsWith(WORKFLOW_DELETE_RESOURCE_TAIL)) {
                    // Only consider if we can write the workflow transition
                    if (Generic.PermissionIncludesWritable(perm.getString("actionGroup"))) {
                        if (resourceName.startsWith("/")) {
                            resourceName = resourceName.substring(1);
                        }
                        // Get the base resource that the workflow is related to
                        String baseResource = resourceName.substring(0, resourceName.length() - WORKFLOW_DELETE_RESOURCE_TAIL.length());
                        resourceNameUI = Generic.ResourceNameUI(spec, baseResource);
                        // Workflow delete means we have CRUDL permissions
                        if (!perms.has(resourceNameUI)) {
                            newPermissionsList = Generic.PermissionLevelArray("CRUDL");
                        } else {
                            JSONArray prevPermsList = (JSONArray) perms.get(resourceNameUI);
                            newPermissionsList = Generic.PermissionLevelArrayEnsure(prevPermsList, Generic.DELETE_PERMISSION);
                        }
                    }
                } else if (resourceName.endsWith(WORKFLOW_LOCK_RESOURCE_TAIL)) {
                    // TODO Should ignore this if record not configred to support locking.
                    if (Generic.PermissionIncludesWritable(perm.getString("actionGroup"))) {
                        if (resourceName.startsWith("/")) {
                            resourceName = resourceName.substring(1);
                        }
                        // Get the base resource that the workflow is related to
                        String baseResource = resourceName.substring(0, resourceName.length() - WORKFLOW_LOCK_RESOURCE_TAIL.length());
                        resourceNameUI = Generic.ResourceNameUI(spec, baseResource);
                        // Workflow lock means we have CRUDL permissions too
                        if (!perms.has(resourceNameUI)) {
                            newPermissionsList = Generic.PermissionLevelArray("CRUDLK");
                        } else {
                            JSONArray prevPermsList = (JSONArray) perms.get(resourceNameUI);
                            newPermissionsList = Generic.PermissionLevelArrayEnsure(prevPermsList, Generic.LOCK_PERMISSION);
                        }
                    }
                } else if (resourceName.endsWith(WORKFLOW_SIMPLE_SUB_RESOURCE)) {
                    log.info("WebLoginStatus.getPerms: Ignoring resource for simple workflow subresource");
                } else if (resourceName.contains(WORKFLOW_SUB_RESOURCE)) {
                    log.debug("WebLoginStatus.getPerms: Ignoring resource for unrecognized workflow transition: " + resourceName);
                } else {
                    // TODO - If the associated record is configured with soft-Delete, should we
                    // filter 'D' from the actionGroup here?
                    resourceNameUI = Generic.ResourceNameUI(spec, resourceName);
                    // Converts the "actionGroup" from the Services into a list of permission verbs
                    newPermissionsList = Generic.PermissionLevelArray(perm.getString("actionGroup"));
                    if (perms.has(resourceNameUI) == true) {
                        // Merge the existing permissions list with another.
                        JSONArray prevPermsList = (JSONArray) perms.get(resourceNameUI);
                        newPermissionsList = Generic.PermissionLevelArrayEnsure(prevPermsList, newPermissionsList);
                    }
                }
                if (newPermissionsList != null) {
                    perms.put(resourceNameUI, newPermissionsList);
                }
            }
        }
        // to match that of the parent authority.
        for (Record r : spec.getAllRecords()) {
            if (r.isType("authority")) {
                for (Instance ins : r.getAllInstances()) {
                    JSONArray permsdata = new JSONArray();
                    if (r.getWebURL().equals("vocab")) {
                        if (perms.has("vocabularyitems")) {
                            permsdata = perms.getJSONArray("vocabularyitems");
                        }
                    } else {
                        if (perms.has(r.getWebURL())) {
                            permsdata = perms.getJSONArray(r.getWebURL());
                        }
                    }
                    perms.put(ins.getID(), permsdata);
                }
            }
        }
    } else {
        // no permissions = no roles for this tenant
        return new JSONObject();
    }
    for (Record r : spec.getAllRecords()) {
        if (!perms.has(r.getWebURL())) {
            perms.put(r.getWebURL(), new JSONArray());
        }
    }
    // make termlist permissions match vocabularyItems permissions
    // perms.put("termlist", perms.get("vocab"));
    data.put("permissions", perms);
    return data;
}
Also used : JSONObject(org.json.JSONObject) Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) Record(org.collectionspace.chain.csp.schema.Record)

Example 15 with Instance

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

the class WebTermList method termlist.

private void termlist(CSPRequestCache cache, Storage storage, UIRequest request, String path) throws UIException {
    try {
        // {tenant}/{tenantname}/{recordType}/termList/{termListType}
        // needs to be {tenant}/{tenantname}/{recordType}/termList/{fieldname}
        // as blanks etc are on a field basis not a vocab basis
        String[] bits = path.split("/");
        Record vb = this.spec.getRecord("vocab");
        Field f = (Field) r.getFieldTopLevel(bits[0]);
        if (f == null) {
            f = (Field) r.getFieldFullList(bits[0]);
        }
        // If the field isn't in this record, look for it in subrecords (e.g. contacts).
        if (f == null) {
            FieldSet[] subRecordFields = r.getAllSubRecords("GET");
            for (int i = 0; i < subRecordFields.length; i++) {
                FieldSet subRecordField = subRecordFields[i];
                Group group = (Group) subRecordField;
                if (group.usesRecord()) {
                    Record subRecord = group.usesRecordId();
                    f = (Field) subRecord.getFieldTopLevel(bits[0]);
                    if (f == null) {
                        f = (Field) subRecord.getFieldFullList(bits[0]);
                    }
                    if (f != null) {
                        break;
                    }
                }
            }
        }
        JSONArray result = new JSONArray();
        if (f.getAllAutocompleteInstances() != null && f.getAllAutocompleteInstances()[0] != null) {
            for (Instance ins : f.getAllAutocompleteInstances()) {
                JSONArray getallnames = ctl.get(storage, ins.getTitleRef(), vb);
                for (int i = 0; i < getallnames.length(); i++) {
                    result.put(getallnames.get(i));
                }
            }
        } else {
            String msg = String.format("Dynamic term list(s) (aka, \"autocomplete\" list) for the field '%s' of record '%s' does not exist.  Check that it is defined in the Application layer configuration.", f.toString(), r.whoamI);
            log.error(msg);
        }
        JSONObject out = generateENUMField(storage, f, result, false);
        request.sendJSONResponse(out);
        int cacheMaxAgeSeconds = spec.getAdminData().getTermListCacheAge();
        if (cacheMaxAgeSeconds > 0) {
            request.setCacheMaxAgeSeconds(cacheMaxAgeSeconds);
        }
    } catch (JSONException e) {
        throw new UIException("JSONException during autocompletion", e);
    }
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) UIException(org.collectionspace.csp.api.ui.UIException) Record(org.collectionspace.chain.csp.schema.Record)

Aggregations

Instance (org.collectionspace.chain.csp.schema.Instance)16 JSONObject (org.json.JSONObject)12 Record (org.collectionspace.chain.csp.schema.Record)11 JSONArray (org.json.JSONArray)7 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)6 UIException (org.collectionspace.csp.api.ui.UIException)6 JSONException (org.json.JSONException)6 Field (org.collectionspace.chain.csp.schema.Field)5 ConfigException (org.collectionspace.chain.csp.config.ConfigException)3 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)3 Option (org.collectionspace.chain.csp.schema.Option)3 ExistException (org.collectionspace.csp.api.persistence.ExistException)3 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)2 Structure (org.collectionspace.chain.csp.schema.Structure)2 AuthoritiesVocabulariesInitialize (org.collectionspace.chain.csp.webui.authorities.AuthoritiesVocabulariesInitialize)2 AuthoritiesVocabulariesSearchList (org.collectionspace.chain.csp.webui.authorities.AuthoritiesVocabulariesSearchList)2 RecordSearchList (org.collectionspace.chain.csp.webui.record.RecordSearchList)2