Search in sources :

Example 16 with Repeat

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

the class GenericStorage method refViewRetrieveJSON.

/**
 * get data needed for terms Used block of a record
 * @param creds
 * @param cache
 * @param path
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 * @throws JSONException
 * @throws UnsupportedEncodingException
 */
public JSONObject refViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String path, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException, UnsupportedEncodingException {
    try {
        JSONObject out = new JSONObject();
        JSONObject pagination = new JSONObject();
        JSONObject listitems = new JSONObject();
        // not all the records need a reference, look in cspace-config.xml for which that don't
        if (r.hasTermsUsed()) {
            path = getRestrictedPath(path, restrictions, "kw", "", false, "");
            if (r.hasSoftDeleteMethod()) {
                // XXX completely not the right thinsg to do but a good enough hack
                path = softpath(path);
            }
            if (r.hasHierarchyUsed("screen")) {
                path = hierarchicalpath(path);
            }
            ReturnedDocument all = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
            if (all.getStatus() != 200)
                throw new ConnectionException("Bad request during identifier cache map update: status not 200", all.getStatus(), path);
            Document list = all.getDocument();
            // assumes consistency in service layer payloads - possible could configure this rather than hard code?
            List<Node> nodes = list.selectNodes("authority-ref-list/*");
            for (Node node : nodes) {
                if (node.getName().equals("authority-ref-item")) {
                    if (!(node instanceof Element))
                        continue;
                    if (((Element) node).hasContent()) {
                        String key = ((Element) node).selectSingleNode("sourceField").getText();
                        String refname = ((Element) node).selectSingleNode("refName").getText();
                        String itemDisplayName = ((Element) node).selectSingleNode("itemDisplayName").getText();
                        String uri = "";
                        if (null != ((Element) node).selectSingleNode("uri")) {
                            // seems to be missing sometimes
                            uri = ((Element) node).selectSingleNode("uri").getText();
                        }
                        String fieldName = key;
                        if (key.split(":").length > 1) {
                            fieldName = key.split(":")[1];
                        }
                        Field fieldinstance = null;
                        if (r.getFieldFullList(fieldName) instanceof Repeat) {
                            Repeat rp = (Repeat) r.getFieldFullList(fieldName);
                            for (FieldSet a : rp.getChildren("GET")) {
                                if (a instanceof Field && a.hasAutocompleteInstance()) {
                                    fieldinstance = (Field) a;
                                }
                            }
                        } else {
                            fieldinstance = (Field) r.getFieldFullList(fieldName);
                        }
                        if (fieldinstance != null) {
                            JSONObject data = new JSONObject();
                            data.put("sourceField", key);
                            data.put("itemDisplayName", itemDisplayName);
                            data.put("refname", refname);
                            data.put("uri", uri);
                            // JSONObject data=miniForURI(storage,creds,cache,refname,null,restrictions);
                            /*
								if(!data.has("refid")){//incase of permissions errors try our best
									data.put("refid",refname);
									if(data.has("displayName")){
										String itemDisplayName=((Element)node).selectSingleNode("itemDisplayName").getText();
										String temp = data.getString("displayName");
										data.remove("displayName");
										data.put(temp, itemDisplayName);
									}
								}
								*/
                            data.put("sourceFieldselector", fieldinstance.getSelector());
                            data.put("sourceFieldName", fieldName);
                            data.put("sourceFieldType", r.getID());
                            if (listitems.has(key)) {
                                JSONArray temp = listitems.getJSONArray(key).put(data);
                                listitems.put(key, temp);
                            } else {
                                JSONArray temp = new JSONArray();
                                temp.put(data);
                                listitems.put(key, temp);
                            }
                        }
                    }
                } else {
                    pagination.put(node.getName(), node.getText());
                }
            }
        }
        out.put("pagination", pagination);
        out.put("listItems", listitems);
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection problem" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    }
}
Also used : Node(org.dom4j.Node) Element(org.dom4j.Element) JSONArray(org.json.JSONArray) Repeat(org.collectionspace.chain.csp.schema.Repeat) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) 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) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 17 with Repeat

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

the class ServiceBindingsGeneration method createAuthRefOrTermRef.

/*
	 * If we have authRef's or termRef's, then we create an entry in the bindings xml and return 'true'; otherwise, we return 'false'
	 */
private boolean createAuthRefOrTermRef(Element auth, Namespace types, Record r, String section, FieldSet in) {
    boolean result = false;
    // for debugging - remove after
    String fieldName = in.getID();
    // for debugging - remove after
    String sec = in.getSection();
    // Ignore passed-in section, in order to create authRefs and termRefs for every section
    if (isAuthOrTermRef(in) && in.getSection().equals(section)) {
        // Let the caller know we created a referenced term
        result = true;
        Boolean isEnum = false;
        if (in instanceof Field) {
            isEnum = (((Field) in).getUIType()).equals("enum");
        }
        Element tele = auth.addElement(new QName("item", types));
        Element tele2 = tele.addElement(new QName("key", types));
        Element tele3 = tele.addElement(new QName("value", types));
        String refType = AUTH_REF;
        if (isEnum == true) {
            refType = TERM_REF;
        }
        tele2.addText(refType);
        String name = "";
        FieldSet fs = (FieldSet) in;
        while (fs.getParent().isTrueRepeatField() == true) {
            String xpathStructuredPart = fs.getServicesTag();
            if (fs.getParent() instanceof Repeat) {
                xpathStructuredPart = getXpathStructuredPart((Repeat) fs.getParent());
            }
            fs = (FieldSet) fs.getParent();
            // Repeatable scalars are separated by the '|' character; otherwise, we use the '/' for xpath expressions
            String separator = "|";
            if (xpathStructuredPart.endsWith("*")) {
                separator = "/";
            }
            name = xpathStructuredPart + separator + name;
        }
        name += in.getServicesTag();
        tele3.addText(name);
    }
    return result;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) QName(org.dom4j.QName) Element(org.dom4j.Element) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Example 18 with Repeat

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

the class ServiceBindingsGeneration method isScalarRepeat.

/*
	 * Repeatable scalars (multivalued scalars)...
	 */
private boolean isScalarRepeat(FieldSet fieldSet) {
    boolean result = false;
    FieldParent parent = fieldSet.getParent();
    if (parent instanceof Repeat) {
        Repeat repeat = (Repeat) parent;
        if (repeat.isTrueRepeatField() == true) {
            String[] parts = repeat.getfullID().split("/");
            if (parts.length == 1) {
                result = true;
            }
        }
    }
    return result;
}
Also used : FieldParent(org.collectionspace.chain.csp.schema.FieldParent) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Example 19 with Repeat

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

the class ServiceBindingsGeneration method getFullyQualifiedFieldPath.

/*
	 * Returns the fully qualified path of a field name
	 */
private String getFullyQualifiedFieldPath(FieldSet in) {
    String tail = null;
    // Start by assuming it's just a plain scalar field, so we would just return the name
    String name = in.getServicesTag();
    // 
    // Debug info
    // 
    String[] fullName = in.getIDPath();
    boolean isPartOfGroup = in.getParent() instanceof Group;
    boolean isGroup = in instanceof Group;
    boolean isGroupField = in.isAGroupField();
    // 
    if (in.getParent().isTrueRepeatField() == true) {
        name = "";
        FieldSet fst = in;
        while (fst.getParent().isTrueRepeatField() == true) {
            // Fields that are part of a repeatable structured group have a form like this 'titleGroupList/[0]/title'
            tail = "/";
            if (isScalarRepeat(fst) == true) {
                // Scalar-repeats (aka, multi-valued fields) look like 'responsibleDepartments/[0]' and *not* 'responsibleDepartments/[0]/responsibleDepartment'
                tail = "";
            }
            fst = (FieldSet) fst.getParent();
            if (fst instanceof Repeat) {
                Repeat rt = (Repeat) fst;
                if (rt.hasServicesParent()) {
                    name += rt.getServicesParent()[0];
                } else {
                    name += rt.getServicesTag();
                }
            } else {
                // REM - This 'else' clause is not necessary and should be removed.
                Group gp = (Group) fst;
                if (gp.hasServicesParent()) {
                    name += gp.getServicesParent()[0];
                } else {
                    name += gp.getServicesTag();
                }
            }
            name += GROUP_LIST_SUFFIX + tail;
        }
        if (name.contains(GROUP_LIST_SUFFIX) && name.endsWith("/")) {
            name += in.getServicesTag();
        }
    }
    return name;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Aggregations

Repeat (org.collectionspace.chain.csp.schema.Repeat)19 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)11 Field (org.collectionspace.chain.csp.schema.Field)9 Group (org.collectionspace.chain.csp.schema.Group)9 Record (org.collectionspace.chain.csp.schema.Record)7 JSONObject (org.json.JSONObject)7 FieldParent (org.collectionspace.chain.csp.schema.FieldParent)6 JSONArray (org.json.JSONArray)6 Element (org.dom4j.Element)5 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)3 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)3 QName (org.dom4j.QName)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)2 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)2 UISpecRunContext (org.collectionspace.chain.csp.schema.UISpecRunContext)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1