Search in sources :

Example 1 with Relationship

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

the class ServicesRelationStorage method dataToRelation.

private Relation dataToRelation(CSPRequestCache cache, String id, JSONObject data) throws JSONException, UnderlyingStorageException {
    String[] src = splitTypeFromId(data.getString("src"));
    String[] dst = splitTypeFromId(data.getString("dst"));
    if (type_to_surl.containsKey(src[0])) {
        src[0] = type_to_surl.get(src[0]);
    }
    if (type_to_surl.containsKey(dst[0])) {
        dst[0] = type_to_surl.get(dst[0]);
    }
    String type = data.getString("type");
    if (types.containsKey(type)) {
        Relationship rel = types.get(type);
        if (!rel.hasDestinationType(dst[0]) && !rel.hasDestinationType("all")) {
            throw new UnderlyingStorageException("type " + type + " is undefined for destination:" + dst[0]);
        }
        if (!rel.hasSourceType("all") && !rel.hasSourceType(src[0])) {
            throw new UnderlyingStorageException("type " + type + " is undefined for source: " + src[0]);
        }
    } else {
        throw new UnderlyingStorageException("type " + type + " is undefined");
    }
    return factory.create(id, src[0], src[1], type, dst[0], dst[1]);
}
Also used : Relationship(org.collectionspace.chain.csp.schema.Relationship) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException)

Example 2 with Relationship

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

the class GenericStorage method handleHierarchyPayloadSend.

public void handleHierarchyPayloadSend(Record thisr, Map<String, Document> body, JSONObject jsonObject, String thiscsid) throws JSONException, ExistException, UnderlyingStorageException {
    if (thisr.hasHierarchyUsed("screen")) {
        Spec spec = this.r.getSpec();
        ArrayList<Element> alleles = new ArrayList<Element>();
        for (Relationship rel : r.getSpec().getAllRelations()) {
            Relationship newrel = rel;
            Boolean inverse = false;
            if (rel.hasInverse()) {
                newrel = thisr.getSpec().getRelation(rel.getInverse());
                inverse = true;
            }
            // does this relationship apply to this record
            if (rel.hasSourceType(r.getID())) {
                // does this record have the data in the json
                if (jsonObject.has(rel.getID())) {
                    String metaTypeField = rel.getMetaTypeField();
                    if (rel.getObject().equals("1")) {
                        if (jsonObject.has(rel.getID()) && !jsonObject.get(rel.getID()).equals("")) {
                            // Look for a metatype
                            String metaType = "";
                            if (!StringUtils.isEmpty(metaTypeField) && jsonObject.has(metaTypeField)) {
                                metaType = jsonObject.getString(metaTypeField);
                            }
                            Element bit = createRelationship(newrel, jsonObject.get(rel.getID()), thiscsid, r.getServicesURL(), metaType, inverse, spec);
                            if (bit != null) {
                                alleles.add(bit);
                            }
                        }
                    } else if (rel.getObject().equals("n")) {
                        // if()
                        JSONArray temp = jsonObject.getJSONArray(rel.getID());
                        for (int i = 0; i < temp.length(); i++) {
                            String relFieldName = rel.getChildName();
                            JSONObject relItem = temp.getJSONObject(i);
                            if (relItem.has(relFieldName) && !relItem.getString(relFieldName).equals("")) {
                                String uri = relItem.getString(relFieldName);
                                // Look for a metatype
                                String metaType = "";
                                if (!StringUtils.isEmpty(metaTypeField) && relItem.has(metaTypeField)) {
                                    metaType = relItem.getString(metaTypeField);
                                }
                                Element bit = createRelationship(newrel, uri, thiscsid, r.getServicesURL(), metaType, inverse, spec);
                                if (bit != null) {
                                    alleles.add(bit);
                                }
                            }
                        }
                    }
                }
            }
        }
        // add relationships section
        if (!alleles.isEmpty()) {
            Element[] array = alleles.toArray(new Element[0]);
            Document out = XmlJsonConversion.getXMLRelationship(array);
            body.put("relations-common-list", out);
        // log.info(out.asXML());
        } else {
            Document out = XmlJsonConversion.getXMLRelationship(null);
            body.put("relations-common-list", out);
        // log.info(out.asXML());
        }
    // probably should put empty array in if no data
    }
}
Also used : JSONObject(org.json.JSONObject) Element(org.dom4j.Element) Relationship(org.collectionspace.chain.csp.schema.Relationship) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Spec(org.collectionspace.chain.csp.schema.Spec) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)

Example 3 with Relationship

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

the class RecordRead method getHierarchy.

private JSONObject getHierarchy(Storage storage, JSONObject fields) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException {
    for (Relationship r : record.getSpec().getAllRelations()) {
        if (r.showSiblings()) {
            // JSONObject temp = new JSONObject();
            // temp.put("_primary", true);
            JSONArray children = new JSONArray();
            // children.put(temp);
            fields.put(r.getSiblingParent(), children);
            if (fields.has(r.getID())) {
                // String broadterm = fields.getString(r.getID());
                String child = r.getSiblingChild();
                if (fields.has(child)) {
                    String broader = fields.getString(child);
                    JSONObject restrict = new JSONObject();
                    restrict.put("dst", broader);
                    restrict.put("type", "hasBroader");
                    JSONObject reldata = storage.getPathsJSON("relations/hierarchical", restrict);
                    fields.remove(child);
                    for (int i = 0; i < reldata.getJSONObject("moredata").length(); i++) {
                        String[] reld = (String[]) reldata.get("listItems");
                        String hcsid = reld[i];
                        JSONObject mored = reldata.getJSONObject("moredata").getJSONObject(hcsid);
                        // it's name is
                        JSONObject siblings = new JSONObject();
                        if (!fields.getString("csid").equals(mored.getString("subjectcsid"))) {
                            siblings.put(child, mored.getString("subjectrefname"));
                            children.put(siblings);
                        }
                    }
                }
                fields.put(r.getSiblingParent(), children);
            }
        }
        // add empty array if necessary
        if (!fields.has(r.getID()) && r.mustExistInSpec()) {
            if (r.getObject().equals("n")) {
                JSONObject temp = new JSONObject();
                temp.put("_primary", true);
                JSONArray at = new JSONArray();
                at.put(temp);
                fields.put(r.getID(), at);
            } else {
                fields.put(r.getID(), "");
            }
        }
    }
    return fields;
}
Also used : JSONObject(org.json.JSONObject) Relationship(org.collectionspace.chain.csp.schema.Relationship) JSONArray(org.json.JSONArray)

Example 4 with Relationship

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

the class VocabulariesRead method getHierarchy.

private JSONObject getHierarchy(Storage storage, JSONObject fields) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException {
    for (Relationship r : n.getRecord().getSpec().getAllRelations()) {
        if (r.showSiblings()) {
            // JSONObject temp = new JSONObject();
            // temp.put("_primary", true);
            JSONArray children = new JSONArray();
            // children.put(temp);
            fields.put(r.getSiblingParent(), children);
            if (fields.has(r.getID())) {
                // String broadterm = fields.getString(r.getID());
                String child = r.getSiblingChild();
                if (fields.has(child)) {
                    String broader = fields.getString(child);
                    JSONObject restrict = new JSONObject();
                    restrict.put("dst", broader);
                    restrict.put("type", "hasBroader");
                    JSONObject reldata = storage.getPathsJSON("relations/hierarchical", restrict);
                    fields.remove(child);
                    for (int i = 0; i < reldata.getJSONObject("moredata").length(); i++) {
                        String[] reld = (String[]) reldata.get("listItems");
                        String hcsid = reld[i];
                        JSONObject mored = reldata.getJSONObject("moredata").getJSONObject(hcsid);
                        // it's name is
                        JSONObject siblings = new JSONObject();
                        if (!fields.getString("csid").equals(mored.getString("subjectcsid"))) {
                            siblings.put(child, mored.getString("subjectrefname"));
                            children.put(siblings);
                        }
                    }
                }
                fields.put(r.getSiblingParent(), children);
            }
        }
        // add empty array if necessary
        if (!fields.has(r.getID()) && r.mustExistInSpec()) {
            if (r.getObject().equals("n")) {
                JSONObject temp = new JSONObject();
                temp.put("_primary", true);
                JSONArray at = new JSONArray();
                at.put(temp);
                fields.put(r.getID(), at);
            } else {
                fields.put(r.getID(), "");
            }
        }
    }
    if (!fields.has("relatedTerms")) {
        JSONObject temp = new JSONObject();
        temp.put("_primary", true);
        JSONArray at = new JSONArray();
        at.put(temp);
        fields.put("relatedTerms", at);
    }
    return fields;
}
Also used : JSONObject(org.json.JSONObject) Relationship(org.collectionspace.chain.csp.schema.Relationship) JSONArray(org.json.JSONArray)

Example 5 with Relationship

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

the class GenericStorage method handleHierarchyPayloadRetrieve.

public void handleHierarchyPayloadRetrieve(Record r, ReturnedMultipartDocument doc, JSONObject out, String thiscsid) throws JSONException {
    if (r.hasHierarchyUsed("screen")) {
        // lets do relationship stuff...
        Document list = doc.getDocument("relations-common-list");
        if (list == null)
            return;
        List<Node> nodes = list.selectNodes("/relations-common-list/*");
        for (Node node : nodes) {
            if ("relation-list-item".equals(node.getName())) {
                // String test = node.asXML();
                String relationshipType = node.selectSingleNode("relationshipType").getText();
                // Be forgiving while waiting for services to complete code.
                Node relationshipMetaTypeNode = node.selectSingleNode("relationshipMetaType");
                String relationshipMetaType = (relationshipMetaTypeNode != null) ? relationshipMetaTypeNode.getText() : "";
                // String subjCSID = node.selectSingleNode("subjectCsid").getText();
                // String objCSID = node.selectSingleNode("objectCsid").getText();
                String subjURI = "";
                String subjCSID = "";
                String subjDocType = "";
                if (node.selectSingleNode("subject/uri") != null) {
                    subjCSID = node.selectSingleNode("subject/csid").getText();
                    subjDocType = node.selectSingleNode("subject/documentType").getText();
                    subjURI = node.selectSingleNode("subject/refName").getText();
                }
                String objRefName = "";
                String objDocType = "";
                String objCSID = "";
                if (node.selectSingleNode("object/uri") != null) {
                    objCSID = node.selectSingleNode("object/csid").getText();
                    objDocType = node.selectSingleNode("object/documentType").getText();
                    objRefName = node.selectSingleNode("object/refName").getText();
                }
                String relateduri = objRefName;
                String relatedcsid = objCSID;
                String relatedser = objDocType;
                if (r.getSpec().hasRelationshipByPredicate(relationshipType)) {
                    Relationship rel = r.getSpec().getRelationshipByPredicate(relationshipType);
                    Relationship newrel = rel;
                    // is this subject or object
                    if (thiscsid.equals(objCSID)) {
                        // should we invert
                        if (r.getSpec().hasRelationshipInverse(rel.getID())) {
                            newrel = r.getSpec().getInverseRelationship(rel.getID());
                            relateduri = subjURI;
                            relatedcsid = subjCSID;
                            relatedser = subjDocType;
                        }
                    }
                    String metaTypeField = newrel.getMetaTypeField();
                    if (newrel.getObject().equals("n")) {
                        // array
                        JSONObject subdata = new JSONObject();
                        subdata.put(newrel.getChildName(), relateduri);
                        if (!StringUtils.isEmpty(metaTypeField)) {
                            subdata.put(metaTypeField, relationshipMetaType);
                        }
                        if (out.has(newrel.getID())) {
                            out.getJSONArray(newrel.getID()).put(subdata);
                        } else {
                            JSONArray relList = new JSONArray();
                            relList.put(subdata);
                            out.put(newrel.getID(), relList);
                        }
                    } else {
                        // string
                        out.put(newrel.getID(), relateduri);
                        if (!StringUtils.isEmpty(metaTypeField)) {
                            out.put(metaTypeField, relationshipMetaType);
                        }
                        if (newrel.showSiblings()) {
                            out.put(newrel.getSiblingChild(), relatedser + "/" + relatedcsid);
                        // out.put(newrel.getSiblingChild(), relateduri);
                        }
                    }
                }
            }
        }
    }
}
Also used : JSONObject(org.json.JSONObject) Node(org.dom4j.Node) Relationship(org.collectionspace.chain.csp.schema.Relationship) JSONArray(org.json.JSONArray) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)

Aggregations

Relationship (org.collectionspace.chain.csp.schema.Relationship)5 JSONArray (org.json.JSONArray)4 JSONObject (org.json.JSONObject)4 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)2 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)2 Document (org.dom4j.Document)2 ArrayList (java.util.ArrayList)1 Spec (org.collectionspace.chain.csp.schema.Spec)1 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)1 Element (org.dom4j.Element)1 Node (org.dom4j.Node)1