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;
}
Aggregations