Search in sources :

Example 36 with FieldSet

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

the class UISpec method actualRepeatNonSiblingEntry.

/**
 * Overwrite with the output you need for the thing you are doing.
 * @param out
 * @param r
 * @param context
 * @param preProtoTree
 * @throws JSONException
 */
@Override
protected void actualRepeatNonSiblingEntry(JSONObject out, Repeat r, UISpecRunContext context, JSONObject preProtoTree) throws JSONException {
    JSONArray decorators = new JSONArray();
    JSONObject content = new JSONObject();
    JSONObject options = new JSONObject();
    JSONObject expander = new JSONObject();
    expander.put("type", "fluid.noexpand");
    expander.put("tree", preProtoTree);
    JSONObject repeatTree = new JSONObject();
    repeatTree.put("expander", expander);
    if (r.getParent() instanceof Record) {
        options.put("elPath", displayAsveryplainWithoutEnclosure(r, context));
    } else {
        options.put("elPath", r.getID());
        options.put("root", "{row}");
    }
    options.put("repeatTree", repeatTree);
    // is this a uispec for search - if so no primary tags wanted
    if (r.getSearchType().startsWith("repeator") && this.spectype.equals("search")) {
        options.put("hidePrimary", true);
    }
    JSONObject decorator = getDecorator("fluid", null, "cspace.makeRepeatable", options, r.isReadOnly());
    decorators.put(decorator);
    content.put(DECORATORS_KEY, decorators);
    String selector = getSelector(r, context);
    // CSPACE-2619 scalar repeatables are different from group repeats
    if (r.getChildren("").length == 1) {
        FieldSet child = getFirstChild(r);
        if (child instanceof Field || child instanceof Group) {
            selector = getSelector(child, context);
        }
    }
    if (r.isExpander()) {
        JSONArray expanders = out.getJSONArray("expander");
        makeExpander(out, expanders);
    } else {
        out.put(selector, content);
    }
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) Group(org.collectionspace.chain.csp.schema.Group) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Record(org.collectionspace.chain.csp.schema.Record)

Example 37 with FieldSet

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

the class UISpec method actualStructuredDate.

/**
 * Overwrite with output you need for this thing you are doing
 * @param fs
 * @param out
 * @param sub
 * @param subexpander
 * @param subitems
 * @param options
 * @throws JSONException
 */
@Override
protected void actualStructuredDate(FieldSet fs, JSONObject out, UISpecRunContext sub, JSONObject subexpander, Record subitems, JSONObject options) throws JSONException {
    out.put("value", displayAsveryplain("fields." + fs.getPrimaryKey()));
    Boolean truerepeat = false;
    FieldParent fsp = fs.getParent();
    if (fsp instanceof Repeat && !(fsp instanceof Group)) {
        // remove bogus repeats used in search
        Repeat rp = (Repeat) fsp;
        if (isATrueRepeat(rp)) {
            String prefix = "";
            if (fs instanceof Group && !((Group) fs).getXxxServicesNoRepeat()) {
                // XXX refacetor with some idea of UIContext.
                // add a prefix for nested non repeatables
                prefix = fs.getID() + ".";
            }
            truerepeat = true;
            for (FieldSet fs2 : subitems.getAllFieldTopLevel("")) {
                subexpander.put(getSelector(fs2, sub), prefix + fs2.getID());
            }
            if (fs instanceof Group && !((Group) fs).getXxxServicesNoRepeat()) {
                options.put("elPath", prefix + fs.getPrimaryKey());
                options.put("root", "{row}");
                out.put("value", displayAsveryplain("{row}." + prefix + fs.getPrimaryKey()));
            } else if (fs instanceof Repeat) {
                options.put("elPath", fs.getPrimaryKey());
                options.put("root", "{row}");
                out.put("value", displayAsveryplain("{row}." + fs.getPrimaryKey()));
            }
        }
    }
    if (!truerepeat) {
        options.put("elPath", "fields." + fs.getPrimaryKey());
        for (FieldSet fs2 : subitems.getAllFieldTopLevel("")) {
            if (!fs2.getSearchType().equals("false") || !this.spectype.equals("search")) {
                // only hide if this is a search uispec - need to extend to all uispec stuff
                subexpander.put(getSelector(fs2, sub), displayAsveryplainWithoutEnclosure(fs2, sub));
            }
        }
    }
    options.put("elPaths", subexpander);
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) FieldParent(org.collectionspace.chain.csp.schema.FieldParent) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Example 38 with FieldSet

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

Example 39 with FieldSet

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

the class GenericSearch method getSearchSpecifierForField.

/**
 * Returns an NXQL-conformant string that specifies the full (X)path to this field.
 * May recurse to handle nested fields.
 * This should probably live in Field.java, not here.
 *
 * @param fieldname the name of the field
 * @param fieldSet the containing fieldSet
 * @return NXQL conformant specifier.
 */
public static String getSearchSpecifierForField(FieldSet fieldSet, Boolean isOrderNotSearch) {
    // String specifier = fieldname;	// default is just the simple field name
    String specifier = fieldSet.getServicesTag();
    // leaf, and the first part is held in the "services parent"
    if (fieldSet.hasServicesParent()) {
        // Prepend the services parent field, and make the child a wildcard
        String[] svcsParent = fieldSet.getServicesParent();
        if (svcsParent[0] != null && !svcsParent[0].isEmpty()) {
            specifier = svcsParent[0] + (isOrderNotSearch ? "/0" : "/*");
        }
    }
    FieldParent parent = fieldSet.getParent();
    // Assume we are recursing until we see otherwise
    boolean isRootLevelField = false;
    if (parent instanceof Record) {
        // A simple reference to base field.
        isRootLevelField = true;
        log.debug("Specifier for root-level field: " + specifier + " is: " + specifier);
    } else {
        FieldSet parentFieldSet = (FieldSet) parent;
        // "repeator" marks things for some expansion - not handled here (?)
        if (parentFieldSet.getSearchType().equals("repeator")) {
            isRootLevelField = true;
        } else {
            // Otherwise, we're dealing with some amount of nesting.
            // First, recurse to get the fully qualified path to the parent.
            String parentID = parentFieldSet.getID();
            log.debug("Recursing for parent: " + parentID);
            specifier = getSearchSpecifierForField(parentFieldSet, isOrderNotSearch);
            // Is parent a scalar list or a complex list?
            Repeat rp = (Repeat) parentFieldSet;
            FieldSet[] children = rp.getChildren("");
            int size = children.length;
            // or a complex schema from which only 1 field is used, will break this.
            if (size > 1) {
                // The parent is a complex schema, not just a scalar repeat
                // Append the field name to build an XPath-like specifier.
                specifier += "/" + fieldSet.getServicesTag();
            } else if (isOrderNotSearch) {
                specifier += "/0";
            } else {
            // Leave specifier as is. We just search on the parent name,
            // as the backend is smart about scalar lists.
            }
        }
        log.debug("Specifier for non-leaf field: " + fieldSet.getServicesTag() + " is: " + specifier);
    }
    if (isRootLevelField) {
    // TODO - map leaf names like "titleGroupList/titleGroup" to "titleGroupList/*"
    }
    return specifier;
}
Also used : FieldParent(org.collectionspace.chain.csp.schema.FieldParent) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Record(org.collectionspace.chain.csp.schema.Record) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Example 40 with FieldSet

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

the class XmlJsonConversion method convertToXml.

// 
// section should be an array
// 
public static Document convertToXml(Record r, JSONObject in, String section, String operation) throws JSONException, UnderlyingStorageException {
    Document doc = DocumentFactory.getInstance().createDocument();
    try {
        String path = r.getServicesRecordPath(section);
        if (path != null) {
            String[] parts = path.split(":", 2);
            String[] rootel = parts[1].split(",");
            Element root = doc.addElement(new QName(rootel[1], new Namespace("ns2", rootel[0])));
            if (r.getAllServiceFieldTopLevel(operation, section).length > 0) {
                for (FieldSet f : r.getAllServiceFieldTopLevel(operation, section)) {
                    addFieldSetToXml(root, f, in, section, operation);
                }
                return doc;
            }
        } else {
            // Revert to DEBUG after v4.0 testing
            log.warn(String.format("Record %s lacks expected section %s", r.getRecordName(), section));
        }
    } catch (Exception ex) {
        log.debug("Error in XmlJsonConversion.convertToXml", ex);
        throw ex;
    }
    return null;
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) QName(org.dom4j.QName) Element(org.dom4j.Element) Document(org.dom4j.Document) Namespace(org.dom4j.Namespace) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

Aggregations

FieldSet (org.collectionspace.chain.csp.schema.FieldSet)62 JSONObject (org.json.JSONObject)36 Record (org.collectionspace.chain.csp.schema.Record)23 Field (org.collectionspace.chain.csp.schema.Field)22 JSONArray (org.json.JSONArray)22 Element (org.dom4j.Element)19 Group (org.collectionspace.chain.csp.schema.Group)17 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)17 JSONException (org.json.JSONException)13 Repeat (org.collectionspace.chain.csp.schema.Repeat)11 Document (org.dom4j.Document)11 QName (org.dom4j.QName)11 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)10 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)9 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ArrayList (java.util.ArrayList)6 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)6 TenantSpec (org.collectionspace.chain.csp.persistence.services.TenantSpec)5 Spec (org.collectionspace.chain.csp.schema.Spec)5