Search in sources :

Example 16 with UIException

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

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

the class BlobCreateUpdate method store_set.

private void store_set(Storage storage, UIRequest request, String path) throws UIException {
    // REM - Documentation here would be nice
    try {
        JSONObject data = new JSONObject();
        JSONObject data2 = new JSONObject();
        JSONObject data3 = new JSONObject();
        data2.put("fileName", request.getFileName());
        data2.put("getbyteBody", request.getbyteBody());
        data2.put("contentType", "multipart/form-data");
        data.put("fields", data2);
        if (create) {
            path = sendJSON(storage, null, data, null);
            data3.put("file", path);
            String[] parts = path.split("/");
            String csid = parts[parts.length - 1];
            data3.put("csid", csid);
        }
        // readblob metadata
        JSONObject data7 = reader.getJSON(storage, data3.getString("csid"));
        data7.put("file", path);
        data7.put("csid", data3.get("csid"));
        request.sendJSONResponse(data7);
        request.setOperationPerformed(Operation.OK);
    } catch (JSONException x) {
        throw new UIException("Failed to parse json: " + x, x);
    } catch (Exception x) {
        throw new UIException(x);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConfigException(org.collectionspace.chain.csp.config.ConfigException)

Example 18 with UIException

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

the class BlobRead method get_blob.

private void get_blob(Storage s, UIRequest q, String csid, String derivative) throws UIException {
    try {
        JSONObject out = s.retrieveJSON("/blobs/" + csid + "/" + derivative, null);
        byte[] data_array = (byte[]) out.get("getByteBody");
        String contentDisp = out.has("contentdisposition") ? out.getString("contentdisposition") : null;
        q.sendUnknown(data_array, out.getString("contenttype"), contentDisp);
        int cacheMaxAgeSeconds = adminData.getUploadedMediaCacheAge();
        if (cacheMaxAgeSeconds > 0) {
            q.setCacheMaxAgeSeconds(cacheMaxAgeSeconds);
        }
    } catch (ExistException e) {
        throw new UIException("Existence exception", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } catch (UnderlyingStorageException e) {
        throw new UIException("Underlying storage problem", e);
    } catch (JSONException e) {
        throw new UIException("JSON exception", e);
    }
}
Also used : 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)

Example 19 with UIException

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

the class Generic method createHash.

/**
 * Function to create a hash for the record traverser functionality
 * @param csid
 * @return
 * @throws UIException
 */
public static String createHash(String csid) throws UIException {
    try {
        byte[] buffer = csid.getBytes();
        byte[] result = null;
        StringBuffer buf = null;
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        result = new byte[md5.getDigestLength()];
        md5.reset();
        md5.update(buffer);
        result = md5.digest(tokensalt.getBytes());
        // create hex string from the 16-byte hash
        buf = new StringBuffer(result.length * 2);
        for (int i = 0; i < result.length; i++) {
            int intVal = result[i] & 0xff;
            if (intVal < 0x10) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(intVal).toUpperCase());
        }
        return buf.toString().substring(0, 32);
    } catch (NoSuchAlgorithmException e) {
        throw new UIException("There were problems with the algorithum");
    }
}
Also used : UIException(org.collectionspace.csp.api.ui.UIException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 20 with UIException

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

the class GenericSearch method setRestricted.

/**
 * Pivots from the UI restriction concept to what the services needs. Initialises valriables if needed
 *
 * @param ui
 * @param param
 * @param pageNum
 * @param pageSize
 * @param search
 * @param r
 * @return
 * @throws UIException
 * @throws JSONException
 */
public static JSONObject setRestricted(UIRequest ui, String param, String pageNum, String pageSize, Boolean search, Record r) throws UIException, JSONException {
    JSONObject returndata = new JSONObject();
    JSONObject restriction = new JSONObject();
    String key = "results";
    if (param == null) {
        key = "items";
    }
    // restriction.put("pageNum","0"); //initialise
    Set<String> args = ui.getAllRequestArgument();
    for (String restrict : args) {
        if (!restrict.equals("_")) {
            if (ui.getRequestArgument(restrict) != null) {
                String value = ui.getRequestArgument(restrict);
                if (restrict.equals(WebMethod.SEARCH_QUERY_PARAM) && search) {
                    restrict = "keywords";
                    key = "results";
                }
                if (restrict.equals(WebMethod.PAGE_SIZE_PARAM) || restrict.equals(WebMethod.PAGE_NUM_PARAM) || restrict.equals(WebMethod.MARK_RELATED_QUERY_PARAM) || restrict.equals("keywords") || restrict.equals("sortDir")) {
                    restriction.put(restrict, value);
                } else if (restrict.equals("sortKey")) {
                    // //"summarylist.updatedAt"//movements_common:locationDate
                    if (r.isType("searchall")) {
                        log.debug("Ignoring sortKey (nor supported on searchall record):" + value);
                    } else {
                        String[] bits = value.split("\\.");
                        String fieldname = value;
                        if (bits.length > 1) {
                            fieldname = bits[1];
                        }
                        FieldSet fs = null;
                        if (fieldname.equals("number")) {
                            fs = r.getMiniNumber();
                        } else if (fieldname.equals("summary")) {
                            fs = r.getMiniSummary();
                        } else {
                            // convert sortKey
                            // CSPACE-4909: Getting null with fieldname = "movements_common:locationDate"
                            fs = r.getFieldFullList(fieldname);
                        }
                        if (fs == null) {
                            String msg = String.format("Undefined field name '%s' specified in query for '%s' records.", fieldname, r.whoamI);
                            UIException e = new UIException(msg);
                            log.error(msg, e);
                            throw e;
                        }
                        if (fs.hasMergeData()) {
                            // if this field is made up of multi merged fields in the UI then just pick the first field to sort on as services doesn't search on merged fields.
                            Field f = (Field) fs;
                            for (String fm : f.getAllMerge()) {
                                if (fm != null) {
                                    fs = r.getFieldFullList(fm);
                                    break;
                                }
                            }
                        }
                        fieldname = fs.getID();
                        String sortFieldname = r.getSortKey(fieldname);
                        if (StringUtils.isNotEmpty(sortFieldname)) {
                            log.debug("Found sort key " + sortFieldname + " for " + fieldname);
                            fs = r.getFieldFullList(sortFieldname);
                        }
                        // for debugging only
                        FieldSet tmp = fs;
                        fieldname = getSearchSpecifierForField(fs, true);
                        String tablebase = r.getServicesRecordPath(fs.getSection()).split(":", 2)[0];
                        String newvalue = tablebase + ":" + fieldname;
                        restriction.put(restrict, newvalue);
                    }
                } else if (restrict.equals("query")) {
                // 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);
                }
            }
        }
    }
    if (param != null && !param.equals("")) {
        restriction.put("queryTerm", "kw");
        restriction.put("queryString", param);
    // restriction.put(r.getDisplayNameField().getID(),param);
    }
    if (pageNum != null) {
        restriction.put("pageNum", pageNum);
    }
    if (pageSize != null) {
        restriction.put("pageSize", pageSize);
    }
    returndata.put("key", key);
    returndata.put("restriction", restriction);
    return returndata;
}
Also used : 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)

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