Search in sources :

Example 36 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class DataGenerator method createRecords.

/**
 * generate records of a specific type
 * @param storage
 * @param ui
 * @return
 * @throws UIException
 */
protected JSONObject createRecords(Storage storage, UIRequest ui) throws UIException {
    log.info("Making " + this.record.getID());
    tty.line("Making " + this.record.getID());
    tty.flush();
    JSONObject returnData = new JSONObject();
    JSONObject out = datagenerator(storage, ui);
    try {
        // make it a record
        JSONObject data = new JSONObject();
        Iterator rit = out.keys();
        JSONObject dataitems = new JSONObject();
        while (rit.hasNext()) {
            String key = (String) rit.next();
            data.put("fields", out.getJSONObject(key));
            String path = writer.sendJSON(storage, null, data, null);
            dataitems.put(key, path);
            // log.info(path);
            log.info("created " + this.record.getID() + " with csid of: " + path);
            tty.line("created " + this.record.getID() + " with csid of: " + path);
            tty.flush();
        }
        returnData.put(this.record.getID(), dataitems);
    } catch (JSONException x) {
        tty.line("JSONException(Failed to parse json: " + x);
        log.info("JSONException(Failed to parse json: " + x);
        throw new UIException("Failed to parse json: " + x, x);
    } catch (ExistException x) {
        log.info("ExistException(Existence exception: " + x);
        tty.line("ExistException(Existence exception: " + x);
        throw new UIException("Existence exception: " + x, x);
    } catch (UnimplementedException x) {
        tty.line("UnimplementedException(UnimplementedException: " + x);
        log.info("UnimplementedException(UnimplementedException: " + x);
        throw new UIException("Unimplemented exception: " + x, x);
    } catch (UnderlyingStorageException x) {
        tty.line("UnderlyingStorageException(UnderlyingStorageException: " + x);
        log.info("UnderlyingStorageException(UnderlyingStorageException: " + x);
        throw new UIException("Problem storing: " + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
    }
    // return something to screen
    return returnData;
}
Also used : JSONObject(org.json.JSONObject) Iterator(java.util.Iterator) 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 37 with UIException

use of org.collectionspace.csp.api.ui.UIException 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 38 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class UISchema method uirecordschema.

/**
 * Create the generic UI record schemas
 * @param storage
 * @param record
 * @return
 * @throws UIException
 */
private JSONObject uirecordschema(Storage storage, Record record) throws UIException {
    UISpecRunContext context = new UISpecRunContext();
    this.storage = storage;
    this.record = record;
    this.tenantname = this.record.getSpec().getAdminData().getTenantName();
    try {
        JSONObject out = new JSONObject();
        JSONObject details = new JSONObject();
        JSONObject properties = new JSONObject();
        if (record.hasTermsUsed()) {
            properties.put("termsUsed", actualSchemaTermsUsed(context));
        }
        if (record.hasRefObjUsed()) {
            properties.put("relations", actualSchemaRelations());
        }
        JSONObject fields = actualSchemaFields(record, context);
        JSONObject prop = fields.getJSONObject("properties");
        if (record.hasHierarchyUsed("screen")) {
            JSONObject temp = actualSchemaFields(record.getSpec().getRecord("hierarchy"), context);
            JSONObject prop2 = temp.getJSONObject("properties");
            Iterator<?> rit = prop2.keys();
            while (rit.hasNext()) {
                String key = (String) rit.next();
                Object value = prop2.get(key);
                prop.put(key, value);
            }
        }
        fields.put("properties", prop);
        properties.put("fields", fields);
        JSONObject output = new JSONObject();
        actualSchemaObject("string", null, null, null, output);
        properties.put("csid", output);
        actualSchemaObject("object", null, properties, null, details);
        out.put(record.getWebURL(), details);
        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) JSONObject(org.json.JSONObject)

Example 39 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class RecordAuthorities method store_get.

private void store_get(Storage storage, UIRequest ui, String path) throws UIException {
    try {
        JSONObject restriction = new JSONObject();
        String key = "items";
        Set<String> args = ui.getAllRequestArgument();
        for (String restrict : args) {
            if (ui.getRequestArgument(restrict) != null) {
                String value = ui.getRequestArgument(restrict);
                // }
                if (restrict.equals(WebMethod.PAGE_SIZE_PARAM) || restrict.equals(WebMethod.PAGE_NUM_PARAM) || restrict.equals("keywords")) {
                    restriction.put(restrict, value);
                } else if (restrict.equals(WebMethod.SEARCH_QUERY_PARAM)) {
                // ignore - someone was doing something odd
                } else {
                    // XXX I would so prefer not to restrict and just pass
                    // stuff up but I know it will cause issues later
                    restriction.put("queryTerm", restrict);
                    restriction.put("queryString", value);
                }
            }
        }
        // Get the data
        JSONObject outputJSON = new JSONObject();
        outputJSON.put("termsUsed", getTermsUsed(storage, base + "/" + path, restriction));
        try {
            outputJSON.put("csid", path);
        } catch (JSONException e1) {
            throw new UIException("Cannot add csid", e1);
        }
        // Write the requested JSON out
        ui.sendJSONResponse(outputJSON);
    } catch (JSONException e) {
        throw new UIException("JSONException during store_get", e);
    } catch (ExistException e) {
        throw new UIException("ExistException during store_get", e);
    } catch (UnimplementedException e) {
        throw new UIException("UnimplementedException during store_get", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        ui.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : JSONObject(org.json.JSONObject) 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 40 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class RecordCreateUpdate method store_set.

private void store_set(Storage storage, UIRequest request, String path) throws UIException {
    try {
        JSONObject restrictions = new JSONObject();
        JSONObject data = request.getJSONBody();
        if (this.base.equals("role")) {
            JSONObject fields = data.optJSONObject("fields");
            if ((fields.optString("roleName") == null || fields.optString("roleName").equals("")) && fields.optString("displayName") != null) {
                String test = fields.optString("displayName");
                test = test.toUpperCase();
                test.replaceAll("\\W", "_");
                fields.put("roleName", "xROLE_" + test);
                data.put("fields", fields);
            }
            // Note that creating a role does not impact things until we assign it
            if (!create) {
                ResponseCache.clearCache(ResponseCache.USER_PERMS_CACHE);
            }
        }
        if (this.record.getID().equals("media")) {
            JSONObject fields = data.optJSONObject("fields");
            // Handle linked media references
            if (!fields.has("blobCsid") || StringUtils.isEmpty(fields.getString("blobCsid"))) {
                // Can consider mapping srcUri to this if want to clean that up
                if (fields.has("sourceUrl")) {
                    // We have a source - see where it is from
                    String uri = fields.getString("sourceUrl");
                    if (uri.contains(BLOBS_SERVICE_URL_PATTERN)) {
                        // This is an uploaded blob, so just pull the csid and set into blobCsid
                        // Split to get CSID
                        String[] parts = uri.split(BLOBS_SERVICE_URL_PATTERN);
                        // Strip off anything trailing the CSID
                        String[] bits = parts[1].split("/");
                        fields.put("blobCsid", bits[0]);
                    } else {
                        // This must be an external Url source
                        // External Source is handled as params to the CREATE/UPDATE of the media record
                        restrictions.put(Record.BLOB_SOURCE_URL, uri);
                        // Tell the Services to delete the original after creating derivatives
                        restrictions.put(Record.BLOB_PURGE_ORIGINAL, Boolean.toString(true));
                    }
                    fields.remove("sourceUrl");
                    data.put("fields", fields);
                }
            }
        }
        if (this.record.getID().equals("output")) {
            // 
            // Invoke a report
            // 
            ReportUtils.invokeReport(this, storage, request, path);
        } else if (this.record.getID().equals("batchoutput")) {
            // do a read instead of a create as reports are special and evil
            JSONObject fields = data.optJSONObject("fields");
            JSONObject payload = new JSONObject();
            payload.put("mode", "single");
            if (fields.has("mode")) {
                payload.put("mode", fields.getString("mode"));
            }
            if (fields.has("docType")) {
                String type = spec.getRecordByWebUrl(fields.getString("docType")).getServicesTenantSg();
                payload.put("docType", type);
            }
            if (fields.has("singleCSID")) {
                payload.put("singleCSID", fields.getString("singleCSID"));
            } else if (fields.has("groupCSID")) {
                payload.put("singleCSID", fields.getString("csid"));
            }
            JSONObject out = storage.retrieveJSON(base + "/" + path, payload);
            byte[] data_array = (byte[]) out.get("getByteBody");
            String contentDisp = out.has("contentdisposition") ? out.getString("contentdisposition") : null;
            request.sendUnknown(data_array, out.getString("contenttype"), contentDisp);
            // Ensure we do not cache report output.
            request.setCacheMaxAgeSeconds(0);
            // request.sendJSONResponse(out);
            request.setOperationPerformed(create ? Operation.CREATE : Operation.UPDATE);
        } else {
            // 
            // <Please document this clause.>
            // 
            FieldSet displayNameFS = this.record.getDisplayNameField();
            String displayNameFieldName = (displayNameFS != null) ? displayNameFS.getID() : null;
            boolean remapDisplayName = false;
            String remapDisplayNameValue = null;
            boolean quickie = false;
            if (create) {
                quickie = (data.has("_view") && data.getString("_view").equals("autocomplete"));
                remapDisplayName = quickie && !"displayName".equals(displayNameFieldName);
                // Check to see if displayName field needs remapping from UI
                if (remapDisplayName) {
                    // Need to map the field for displayName, and put it into a proper structure
                    JSONObject fields = data.getJSONObject("fields");
                    remapDisplayNameValue = fields.getString("displayName");
                    if (remapDisplayNameValue != null) {
                        // This needs generalizing, in case the remapped name is nested
                        /*
							 * From vocab handling where we know where the termDisplayName is
							FieldSet parentTermGroup = (FieldSet)displayNameFS.getParent();
							JSONArray parentTermInfoArray = new JSONArray();
							JSONObject termInfo = new JSONObject();
							termInfo.put(displayNameFieldName, remapDisplayNameValue);
							parentTermInfoArray.put(termInfo);
							*/
                        fields.put(displayNameFieldName, remapDisplayNameValue);
                        fields.remove("displayName");
                    }
                }
                // REM - We needed a way to send query params, so I'm adding "restrictions" here
                path = sendJSON(storage, null, data, restrictions);
                data.put("csid", path);
                data.getJSONObject("fields").put("csid", path);
            // Is this needed???
            /*
					String refName = data.getJSONObject("fields").getString("refName");
					data.put("urn", refName);
					data.getJSONObject("fields").put("urn", refName);
					// This seems wrong - especially when we create from existing.
					if(remapDisplayName){
						JSONObject newdata = new JSONObject();
						newdata.put("urn", refName);
						newdata.put("displayName",quickieDisplayName);
						data = newdata;
					}
					 */
            } else {
                path = sendJSON(storage, path, data, restrictions);
            }
            if (path == null) {
                throw new UIException("Insufficient data for create (no fields?)");
            }
            if (this.base.equals("role")) {
                assignPermissions(storage, path, data);
            }
            if (this.base.equals("termlist")) {
                assignTerms(storage, path, data);
            }
            // We do a GET now to read back what we created.
            data = reader.getJSON(storage, path);
            if (quickie) {
                JSONObject newdata = new JSONObject();
                JSONObject fields = data.getJSONObject("fields");
                String displayName = fields.getString(remapDisplayName ? displayNameFieldName : "displayName");
                newdata.put("displayName", remapDisplayNameValue);
                String refName = fields.getString("refName");
                newdata.put("urn", refName);
                data = newdata;
            }
            request.sendJSONResponse(data);
            request.setOperationPerformed(create ? Operation.CREATE : Operation.UPDATE);
            if (create)
                request.setSecondaryRedirectPath(new String[] { url_base, path });
        }
    } catch (JSONException x) {
        throw new UIException("Failed to parse JSON: " + x, x);
    } catch (ExistException x) {
        UIException uiexception = new UIException(x.getMessage(), 0, "", x);
        request.sendJSONResponse(uiexception.getJSON());
    } catch (UnimplementedException x) {
        throw new UIException("Unimplemented exception: " + x, x);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.setStatus(x.getStatus());
        request.setFailure(true, uiexception);
        request.sendJSONResponse(uiexception.getJSON());
    } catch (Exception x) {
        throw new UIException(x);
    }
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) UIException(org.collectionspace.csp.api.ui.UIException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) 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) ConfigException(org.collectionspace.chain.csp.config.ConfigException)

Aggregations

UIException (org.collectionspace.csp.api.ui.UIException)72 JSONObject (org.json.JSONObject)51 JSONException (org.json.JSONException)50 ExistException (org.collectionspace.csp.api.persistence.ExistException)39 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)39 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)39 JSONArray (org.json.JSONArray)19 IOException (java.io.IOException)10 Record (org.collectionspace.chain.csp.schema.Record)7 Instance (org.collectionspace.chain.csp.schema.Instance)6 ConfigException (org.collectionspace.chain.csp.config.ConfigException)5 Field (org.collectionspace.chain.csp.schema.Field)4 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)4 UIRequest (org.collectionspace.csp.api.ui.UIRequest)4 MessageDigest (java.security.MessageDigest)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)3 AdminData (org.collectionspace.chain.csp.schema.AdminData)3 Structure (org.collectionspace.chain.csp.schema.Structure)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2