Search in sources :

Example 6 with RefName

use of org.collectionspace.services.common.api.RefName in project application by collectionspace.

the class GenericStorage method createRelationship.

// NOTE that this is building XML Doc, and not JSON as is typical!
protected static Element createRelationship(Relationship rel, Object data, String csid, String subjtype, String metaType, Boolean reverseIt, Spec spec) throws ExistException, UnderlyingStorageException, JSONException {
    Document doc = DocumentFactory.getInstance().createDocument();
    Element subroot = doc.addElement("relation-list-item");
    Element predicate = subroot.addElement("predicate");
    predicate.addText(rel.getPredicate());
    Element relMetaType = subroot.addElement("relationshipMetaType");
    relMetaType.addText(metaType);
    String subjectName = "subject";
    String objectName = "object";
    if (reverseIt) {
        subjectName = "object";
        objectName = "subject";
    }
    Element subject = subroot.addElement(subjectName);
    Element subjcsid = subject.addElement("csid");
    if (csid != null) {
        subjcsid.addText(csid);
    } else {
        subjcsid.addText("${itemCSID}");
    }
    // find out record type from urn
    String refName = (String) data;
    Element object = subroot.addElement(objectName);
    // We may or may not be dealing with a sub-resource like AuthItems.
    // TODO - this may all be unneccessary, as the services should fill in
    // doc types, etc. now automatically.
    // OTOH, not a bad idea to validate the refName...
    RefName.AuthorityItem itemParsed = RefName.AuthorityItem.parse(refName);
    if (itemParsed != null) {
        String serviceurl = itemParsed.inAuthority.resource;
        Record myr = spec.getRecordByServicesUrl(serviceurl);
        if (myr.isType("authority")) {
            Element objRefName = object.addElement("refName");
            objRefName.addText(refName);
        } else {
            throw new JSONException("Relation object refName is for sub-resources other than authority item - NYI!");
        }
    } else {
        RefName.Authority resourceParsed = RefName.Authority.parse(refName);
        if (resourceParsed != null) {
            Element objRefName = object.addElement("refName");
            objRefName.addText(refName);
        } else {
            throw new JSONException("Relation object refName does not appear to be valid!");
        }
    }
    // log.info(subroot.asXML());
    return subroot;
}
Also used : RefName(org.collectionspace.services.common.api.RefName) Element(org.dom4j.Element) JSONException(org.json.JSONException) Record(org.collectionspace.chain.csp.schema.Record) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)

Aggregations

RefName (org.collectionspace.services.common.api.RefName)6 Record (org.collectionspace.chain.csp.schema.Record)4 ExistException (org.collectionspace.csp.api.persistence.ExistException)4 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)4 JSONObject (org.json.JSONObject)4 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)2 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)2 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)2 Document (org.dom4j.Document)2 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)1 Field (org.collectionspace.chain.csp.schema.Field)1 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)1 Group (org.collectionspace.chain.csp.schema.Group)1 Element (org.dom4j.Element)1 Node (org.dom4j.Node)1