Search in sources :

Example 21 with FieldSet

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

the class ConfiguredVocabStorage method getXPathForField.

/**
 * Returns an XPath-conformant string that specifies the full (X)path to this field.
 * May recurse to handle nested fields. Will choose primary, in lists (i.e, 1st element)
 * This should probably live in Field.java, not here (but needs to be in both Field.java
 * and Repeat.java - do I hear "Base Class"?!?!)
 *
 * @param fieldSet the containing fieldSet
 * @return NXQL conformant specifier.
 */
public static String getXPathForField(FieldSet fieldSet) {
    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] + "/";
            // Note that we do not handle paths more the 2 in length - makes no sense
            if (svcsParent.length < 2) {
                specifier += XPATH_GENERIC_FIRST_EL;
            } else if (svcsParent[1] == null) {
                // Work around ridiculous hack/nonsense in Repeat init
                specifier += fieldSet.getServicesTag();
            } else {
                specifier += svcsParent[1];
            }
            specifier += XPATH_FIRST_EL;
        }
    }
    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 {
            // First, recurse to get the fully qualified path to the parent.
            if (log.isDebugEnabled()) {
                String parentID = parentFieldSet.getID();
                log.debug("Recursing for parent: " + parentID);
            }
            specifier = getXPathForField(parentFieldSet);
            // 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 {
            // 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 22 with FieldSet

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

the class ConfiguredVocabStorage method get.

private JSONObject get(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String url, String ims_url) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
    // int status=0;
    String csid = "";
    JSONObject out = new JSONObject();
    // XXX pagination support
    String softurl = url;
    if (r.hasSoftDeleteMethod()) {
        softurl = softpath(url);
    }
    if (r.hasHierarchyUsed("screen")) {
        softurl = hierarchicalpath(softurl);
    }
    ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, softurl, null, creds, cache);
    if (doc.getStatus() == 404)
        throw new ExistException("Does not exist " + softurl);
    if (doc.getStatus() == 403) {
        // permission error - keep calm and carry on with what we can glean
        out.put("displayName", getDisplayNameKey());
        out.put("csid", csid);
        out.put("recordtype", r.getWebURL());
        return out;
    }
    if (doc.getStatus() > 299)
        throw new UnderlyingStorageException("Could not retrieve vocabulary status=" + doc.getStatus(), doc.getStatus(), softurl);
    String name = null;
    String refid = null;
    String termStatus = null;
    String parentcsid = null;
    String shortIdentifier = "";
    for (String section : r.getServicesRecordPathKeys()) {
        String path = r.getServicesRecordPath(section);
        String[] record_path = path.split(":", 2);
        String[] tag_path = record_path[1].split(",", 2);
        Document result = doc.getDocument(record_path[0].trim());
        if (result != null) {
            if ("common".equals(section)) {
                // XXX hardwired :(
                String dnXPath = getDisplayNameXPath();
                name = result.selectSingleNode(tag_path[1] + "/" + dnXPath).getText();
                if (result.selectSingleNode(tag_path[1] + "/shortIdentifier") != null) {
                    shortIdentifier = result.selectSingleNode(tag_path[1] + "/shortIdentifier").getText();
                }
                refid = result.selectSingleNode(tag_path[1] + "/refName").getText();
                // We need to replace this with the same model as for displayName
                if (result.selectSingleNode(tag_path[1] + "/termStatus") != null) {
                    termStatus = result.selectSingleNode(tag_path[1] + "/termStatus").getText();
                } else {
                    termStatus = "";
                }
                csid = result.selectSingleNode(tag_path[1] + "/csid").getText();
                parentcsid = result.selectSingleNode(tag_path[1] + "/inAuthority").getText();
                XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
            } else {
                XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
            }
        } else {
            log.warn(String.format("XML Payload for '%s' was missing part '%s'.", url, record_path[0]));
        }
    }
    // If this record has hierarchy, will pull out the relations section and map it to the hierarchy
    // fields (special case handling of XML-JSON
    handleHierarchyPayloadRetrieve(r, doc, out, csid);
    // get related sub records?
    for (FieldSet fs : r.getAllSubRecords("GET")) {
        Record sr = fs.usesRecordId();
        // sr.getID()
        if (sr.isType("authority")) {
            String getPath = url + "/" + sr.getServicesURL();
            JSONArray subout = get(storage, creds, cache, url, getPath, sr);
            if (fs instanceof Field) {
                JSONObject fielddata = subout.getJSONObject(0);
                Iterator<String> rit = fielddata.keys();
                while (rit.hasNext()) {
                    String key = rit.next();
                    out.put(key, fielddata.get(key));
                }
            } else if (fs instanceof Group) {
                if (subout.length() > 0) {
                    out.put(fs.getID(), subout.getJSONObject(0));
                }
            } else {
                out.put(fs.getID(), subout);
            }
        }
    }
    // csid = urn_processor.deconstructURN(refid,false)[4];
    out.put(getDisplayNameKey(), name);
    out.put("csid", csid);
    out.put("refid", refid);
    RefName.AuthorityItem item = RefName.AuthorityItem.parse(refid);
    out.put("namespace", item.getParentShortIdentifier());
    out.put("shortIdentifier", shortIdentifier);
    out.put("termStatus", termStatus);
    out.put("authorityid", parentcsid);
    out.put("recordtype", r.getWebURL());
    return out;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) RefName(org.collectionspace.services.common.api.RefName) JSONArray(org.json.JSONArray) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record)

Example 23 with FieldSet

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

the class ConfiguredVocabStorage method updateJSON.

public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, JSONObject jsonObject, JSONObject restrictions, Record thisr, String savePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        String csid = savePath.split("/")[3];
        Map<String, Document> body = new HashMap<String, Document>();
        for (String section : r.getServicesRecordPathKeys()) {
            String path = r.getServicesRecordPath(section);
            String[] record_path = path.split(":", 2);
            String[] tag_path = record_path[1].split(",", 2);
            Document temp = createEntry(section, tag_path[0], tag_path[1], jsonObject, null, null, thisr, false);
            if (temp != null) {
                body.put(record_path[0], temp);
            // log.info(temp.asXML());
            }
        }
        handleHierarchyPayloadSend(thisr, body, jsonObject, csid);
        ReturnedMultipartDocument out = conn.getMultipartXMLDocument(RequestMethod.PUT, savePath, body, creds, cache);
        if (out.isErrorStatus()) {
            if (out.isTransactionFailedStatus()) {
                throw new UnderlyingStorageException(VOCABULARY_UPDATE_FAILED_MESSAGE + ": " + out.TRANSACTION_FAILED_MESSAGE, out.getStatus(), savePath);
            } else {
                throw new UnderlyingStorageException(VOCABULARY_UPDATE_FAILED_MESSAGE, out.getStatus(), savePath);
            }
        }
        // subrecord update
        for (FieldSet fs : thisr.getAllSubRecords("PUT")) {
            Record sr = fs.usesRecordId();
            // get list of existing subrecords
            JSONObject existingcsid = new JSONObject();
            JSONObject updatecsid = new JSONObject();
            JSONArray createcsid = new JSONArray();
            String getPath = savePath + "/" + sr.getServicesURL();
            Integer subcount = 0;
            String firstfile = "";
            while (!getPath.equals("")) {
                JSONObject data = getListView(creds, cache, getPath, sr.getServicesListPath(), "csid", false, sr);
                String[] filepaths = (String[]) data.get("listItems");
                subcount += filepaths.length;
                if (firstfile.equals("") && subcount != 0) {
                    firstfile = filepaths[0];
                }
                for (String uri : filepaths) {
                    String path = uri;
                    if (path != null && path.startsWith("/"))
                        path = path.substring(1);
                    existingcsid.put(path, "original");
                }
                if (data.has("pagination")) {
                    Integer ps = Integer.valueOf(data.getJSONObject("pagination").getString("pageSize"));
                    Integer pn = Integer.valueOf(data.getJSONObject("pagination").getString("pageNum"));
                    Integer ti = Integer.valueOf(data.getJSONObject("pagination").getString("totalItems"));
                    if (ti > (ps * (pn + 1))) {
                        JSONObject pgRestrictions = new JSONObject();
                        pgRestrictions.put("pageSize", Integer.toString(ps));
                        pgRestrictions.put("pageNum", Integer.toString(pn + 1));
                        getPath = getRestrictedPath(getPath, pgRestrictions, sr.getServicesSearchKeyword(), "", false, "");
                    // need more values
                    } else {
                        getPath = "";
                    }
                }
            }
            // how does that compare to what we need
            if (sr.isType("authority")) {
                if (fs instanceof Field) {
                    JSONObject subdata = new JSONObject();
                    // loop thr jsonObject and find the fields I need
                    for (FieldSet subfs : sr.getAllFieldTopLevel("PUT")) {
                        String key = subfs.getID();
                        if (jsonObject.has(key)) {
                            subdata.put(key, jsonObject.get(key));
                        }
                    }
                    if (subcount == 0) {
                        // create
                        createcsid.put(subdata);
                    } else {
                        // update - there should only be one
                        String firstcsid = firstfile;
                        updatecsid.put(firstcsid, subdata);
                        existingcsid.remove(firstcsid);
                    }
                } else if (fs instanceof Group) {
                    // subrecorddata.put(value);
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONObject) {
                            if (((JSONObject) subdata).has("_subrecordcsid")) {
                                String thiscsid = ((JSONObject) subdata).getString("_subrecordcsid");
                                // update
                                if (existingcsid.has(thiscsid)) {
                                    updatecsid.put(thiscsid, (JSONObject) subdata);
                                    existingcsid.remove(thiscsid);
                                } else {
                                    // something has gone wrong... best just create it from scratch
                                    createcsid.put(subdata);
                                }
                            } else {
                                // create
                                createcsid.put(subdata);
                            }
                        }
                    }
                } else {
                    // need to find if we have csid's for each one
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONArray) {
                            JSONArray subarray = (JSONArray) subdata;
                            for (int i = 0; i < subarray.length(); i++) {
                                JSONObject subrecord = subarray.getJSONObject(i);
                                if (subrecord.has("_subrecordcsid")) {
                                    String thiscsid = subrecord.getString("_subrecordcsid");
                                    // update
                                    if (existingcsid.has(thiscsid)) {
                                        updatecsid.put(thiscsid, (JSONObject) subdata);
                                        existingcsid.remove(thiscsid);
                                    } else {
                                        // something has gone wrong... best just create it from scratch
                                        createcsid.put(subdata);
                                    }
                                } else {
                                    // create
                                    createcsid.put(subdata);
                                }
                            }
                        }
                    }
                }
                String savePathSr = savePath + "/" + sr.getServicesURL() + "/";
                // do delete JSONObject existingcsid = new JSONObject();
                Iterator<String> rit = existingcsid.keys();
                while (rit.hasNext()) {
                    String key = rit.next();
                    deleteJSON(root, creds, cache, key, savePathSr, sr);
                }
                // do update JSONObject updatecsid = new JSONObject();
                Iterator<String> keys = updatecsid.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    JSONObject value = updatecsid.getJSONObject(key);
                    String thissave = savePathSr + key;
                    updateJSON(root, creds, cache, value, new JSONObject(), sr, thissave);
                // updateJSON( root, creds, cache, key,  value, sr, savePathSr);
                }
                // do create JSONArray createcsid = new JSONArray();
                for (int i = 0; i < createcsid.length(); i++) {
                    JSONObject value = createcsid.getJSONObject(i);
                    subautocreateJSON(root, creds, cache, sr, value, savePathSr);
                }
            }
        }
    // XXX dont currently update the shortID???
    // cache.setCached(getClass(),new String[]{"shortId",vocab,filePath.split("/")[1]},shortId);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception " + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Cannot parse surrounding JSON " + e.getLocalizedMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnimplementedException("UnsupportedEncodingException" + e.getLocalizedMessage(), e);
    }
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record) JSONObject(org.json.JSONObject) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 24 with FieldSet

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

the class XmlJsonConversion method addRepeatedNodeToJson.

private static JSONArray addRepeatedNodeToJson(Element container, Repeat f, String permlevel, JSONObject tempSon) throws JSONException {
    JSONArray node = new JSONArray();
    List<FieldSet> children = getChildrenWithGroupFields(f, permlevel);
    JSONArray elementlist = extractRepeatData(container, f, permlevel);
    JSONObject siblingitem = new JSONObject();
    for (int i = 0; i < elementlist.length(); i++) {
        JSONObject element = elementlist.getJSONObject(i);
        Iterator<?> rit = element.keys();
        while (rit.hasNext()) {
            String key = (String) rit.next();
            JSONArray arrvalue = new JSONArray();
            for (FieldSet fs : children) {
                if (fs instanceof Repeat && ((Repeat) fs).hasServicesParent()) {
                    if (!((Repeat) fs).getServicesParent()[0].equals(key)) {
                        continue;
                    }
                    Object value = element.get(key);
                    arrvalue = (JSONArray) value;
                } else {
                    if (!fs.getID().equals(key)) {
                        continue;
                    }
                    Object value = element.get(key);
                    arrvalue = (JSONArray) value;
                }
                if (fs instanceof Field) {
                    for (int j = 0; j < arrvalue.length(); j++) {
                        JSONObject repeatitem = new JSONObject();
                        // XXX remove when service layer supports primary tags
                        if (f.hasPrimary() && j == 0) {
                            repeatitem.put("_primary", true);
                        }
                        Element child = (Element) arrvalue.get(j);
                        Object val = child.getText();
                        Field field = (Field) fs;
                        String id = field.getID();
                        if (f.asSibling()) {
                            addExtraToJson(siblingitem, child, field, tempSon);
                            if (field.getDataType().equals("boolean")) {
                                siblingitem.put(id, (Boolean.parseBoolean((String) val) ? true : false));
                            } else {
                                siblingitem.put(id, val);
                            }
                        } else {
                            addExtraToJson(repeatitem, child, field, tempSon);
                            if (field.getDataType().equals("boolean")) {
                                repeatitem.put(id, (Boolean.parseBoolean((String) val) ? true : false));
                            } else {
                                repeatitem.put(id, val);
                            }
                            node.put(repeatitem);
                        }
                        tempSon = addtemp(tempSon, fs.getID(), child.getText());
                    }
                } else if (fs instanceof Group) {
                    JSONObject tout = new JSONObject();
                    JSONObject tempSon2 = new JSONObject();
                    Group rp = (Group) fs;
                    addRepeatToJson(tout, container, rp, permlevel, tempSon2, "", "");
                    if (f.asSibling()) {
                        JSONArray a1 = tout.getJSONArray(rp.getID());
                        JSONObject o1 = a1.getJSONObject(0);
                        siblingitem.put(fs.getID(), o1);
                    } else {
                        JSONObject repeatitem = new JSONObject();
                        repeatitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
                        node.put(repeatitem);
                    }
                    tempSon = addtemp(tempSon, rp.getID(), tout.getJSONArray(rp.getID()));
                // log.info(f.getID()+":"+rp.getID()+":"+tempSon.toString());
                } else if (fs instanceof Repeat) {
                    JSONObject tout = new JSONObject();
                    JSONObject tempSon2 = new JSONObject();
                    Repeat rp = (Repeat) fs;
                    addRepeatToJson(tout, container, rp, permlevel, tempSon2, "", "");
                    if (f.asSibling()) {
                        siblingitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
                    } else {
                        JSONObject repeatitem = new JSONObject();
                        repeatitem.put(fs.getID(), tout.getJSONArray(rp.getID()));
                        node.put(repeatitem);
                    }
                    tempSon = addtemp(tempSon, rp.getID(), tout.getJSONArray(rp.getID()));
                // log.info(f.getID()+":"+rp.getID()+":"+tempSon.toString());
                }
            }
        }
    }
    if (f.asSibling()) {
        node.put(siblingitem);
    }
    return node;
}
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) Element(org.dom4j.Element) JSONArray(org.json.JSONArray) Repeat(org.collectionspace.chain.csp.schema.Repeat) JSONObject(org.json.JSONObject)

Example 25 with FieldSet

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

the class XmlJsonConversion method getChildrenWithGroupFields.

// merges in the pseudo sub records 'groupfields' with the normal fields unless they need to be nested
private static List<FieldSet> getChildrenWithGroupFields(Repeat parent, String operation) {
    List<FieldSet> children = new ArrayList<FieldSet>();
    if (parent.getUIType().startsWith("groupfield")) {
        String[] parts = parent.getUIType().split("/");
        Record subitems = parent.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
        for (FieldSet fd : subitems.getAllFieldTopLevel(operation)) {
            children.add(fd);
        }
    }
    for (FieldSet fs : parent.getChildren(operation)) {
        if (fs.getUIType().startsWith("groupfield")) {
            String[] parts = fs.getUIType().split("/");
            Record subitems = fs.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
            if (fs instanceof Group) {
                if (((Group) fs).getXxxServicesNoRepeat()) {
                    for (FieldSet fd : subitems.getAllFieldTopLevel(operation)) {
                        // non-nested groupfields?
                        children.add(fd);
                    }
                    // groups with xxx-services-no-repeat set to true.
                    for (FieldSet fd : ((Group) fs).getChildren(operation)) {
                        children.add(fd);
                    }
                } else {
                    // this one should be nested
                    children.add(fs);
                }
            } else {
                for (FieldSet fd : subitems.getAllFieldTopLevel(operation)) {
                    // what about nested groupfields?
                    children.add(fd);
                }
            }
        } else {
            children.add(fs);
        }
    }
    return children;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) ArrayList(java.util.ArrayList) Record(org.collectionspace.chain.csp.schema.Record)

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