import org.collectionspace.services.common.api.Tools;
import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
import org.collectionspace.services.common.context.ServiceBindingUtils;
+import org.collectionspace.services.common.document.InvalidDocumentException;
import org.collectionspace.services.common.relation.RelationJAXBSchema;
import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.relation.RelationsDocListItem;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
+import org.nuxeo.ecm.core.api.model.PropertyException;
import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* for ACTION.GET_ALL
*/
private RelationsCommonList relationList;
-
+
@Override
public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
+
+ // Obtain document models for the subject and object of the relation.
+ DocumentModel relationDocModel = wrapDoc.getWrappedObject();
+ ServiceContext ctx = getServiceContext();
+ DocumentModel subjectDocModel = getSubjectDocModel(relationDocModel, ctx);
+ DocumentModel objectDocModel = getObjectDocModel(relationDocModel, ctx);
+
+ // Use values from the subject and object document models to populate the
+ // relevant fields of the relation's own document model.
+ if (subjectDocModel != null) {
+ relationDocModel = populateSubjectValues(relationDocModel, subjectDocModel);
+ }
+ if (objectDocModel != null) {
+ relationDocModel = populateObjectValues(relationDocModel, objectDocModel);
+ }
+
+ // FIXME: Verify the following:
+ // Do we call this method here, only after we've updated the relationDocModel?
+ // Has the wrapDoc instance itself been updated, in the process of updating the relationDocModel,
+ // or do we need to pass the updated relationDocModel back into that instance?
super.handleCreate(wrapDoc);
+
}
-
+
@Override
public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
- super.handleUpdate(wrapDoc);
+ super.handleUpdate(wrapDoc);
}
@Override
@Override
public RelationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
- RelationsCommonList relList = this.extractPagingInfo(new RelationsCommonList(), wrapDoc) ;
+ RelationsCommonList relList = this.extractPagingInfo(new RelationsCommonList(), wrapDoc);
relList.setFieldsReturned("subjectCsid|relationshipType|predicateDisplayName|objectCsid|uri|csid|subject|object");
ServiceContext ctx = getServiceContext();
String serviceContextPath = getServiceContextPath();
ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
- while(iter.hasNext()){
+ while (iter.hasNext()) {
DocumentModel docModel = iter.next();
RelationListItem relListItem = getRelationListItem(ctx, sbt, tReader, docModel, serviceContextPath);
relList.getRelationListItem().add(relListItem);
* @throws Exception the exception
*/
private RelationListItem getRelationListItem(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
- ServiceBindingType sbt,
- TenantBindingConfigReaderImpl tReader,
- DocumentModel docModel,
- String serviceContextPath) throws Exception {
+ ServiceBindingType sbt,
+ TenantBindingConfigReaderImpl tReader,
+ DocumentModel docModel,
+ String serviceContextPath) throws Exception {
RelationListItem relationListItem = new RelationListItem();
String id = getCsid(docModel);
relationListItem.setCsid(id);
relationListItem.setPredicateDisplayName((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE_DISPLAYNAME));
relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
-
+
relationListItem.setUri(serviceContextPath + id);
//Now fill in summary info for the related docs: subject and object.
return relationListItem;
}
- // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
-
- protected RelationsDocListItem createRelationsDocListItem(ServiceContext ctx,
- ServiceBindingType sbt,
- String itemCsid,
- TenantBindingConfigReaderImpl tReader,
- String documentType) throws Exception {
+ // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
+ protected RelationsDocListItem createRelationsDocListItem(ServiceContext ctx,
+ ServiceBindingType sbt,
+ String itemCsid,
+ TenantBindingConfigReaderImpl tReader,
+ String documentType) throws Exception {
RelationsDocListItem item = new RelationsDocListItem();
item.setDocumentType(documentType);//this one comes from the record, as documentType1, documentType2.
// CSPACE-4037 REMOVING: item.setService(documentType);//this one comes from the record, as documentType1, documentType2. Current app seems to use servicename for this.
item.setCsid(itemCsid);
- DocumentModel itemDocModel = NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid); //null if not found.
- if (itemDocModel!=null){
+ DocumentModel itemDocModel = NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid); //null if not found.
+ if (itemDocModel != null) {
String itemDocType = itemDocModel.getDocumentType().getName();
// CSPACE-4037 REMOVING: item.setDocumentTypeFromModel(itemDocType); //this one comes from the nuxeo documentType
//DEBUG: System.out.println("\r\n******** AuthorityItemDocumentModelHandlder documentType **************\r\n\tdocModel: "+itemDocType+"\r\n\tpayload: "+documentType);
//boolean usedDocumentTypeFromPayload = true;
/*if ( ! Tools.isBlank(documentType)){
- if (documentType.equals(itemDocType)){
- //usedDocumentTypeFromPayload = true;
- } else {
- // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
- //throw new Exception("documentType supplied was wrong. supplied: "+documentType+" required: "+itemDocType+ " itemCsid: "+itemCsid );
- }
+ if (documentType.equals(itemDocType)){
+ //usedDocumentTypeFromPayload = true;
+ } else {
+ // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
+ //throw new Exception("documentType supplied was wrong. supplied: "+documentType+" required: "+itemDocType+ " itemCsid: "+itemCsid );
+ }
} else {
- //usedDocumentTypeFromPayload = false;
- item.setDocumentType(itemDocType);
+ //usedDocumentTypeFromPayload = false;
+ item.setDocumentType(itemDocType);
} */
- if (Tools.isBlank(documentType)){
- item.setDocumentType(itemDocType);
- }
+ if (Tools.isBlank(documentType)) {
+ item.setDocumentType(itemDocType);
+ }
// TODO: clean all the output statements out of here when CSPACE-4037 is done.
//TODO: ensure that itemDocType is really the entry point, i.e. servicename==doctype
try {
propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP);
String itemDocname = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP, itemDocModel);
- if (propName==null || itemDocname==null){
+ if (propName == null || itemDocname == null) {
//System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
- } else{
+ } else {
item.setName(itemDocname);
//System.out.println("=== found prop : "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
}
- } catch (Throwable t){
- System.out.println("====Error finding objectNameProperty: "+itemDocModel+" field "+ServiceBindingUtils.OBJ_NAME_PROP+"="+propName
- +" not found in itemDocType: "+itemDocType+" inner: "+t.getMessage());
+ } catch (Throwable t) {
+ System.out.println("====Error finding objectNameProperty: " + itemDocModel + " field " + ServiceBindingUtils.OBJ_NAME_PROP + "=" + propName
+ + " not found in itemDocType: " + itemDocType + " inner: " + t.getMessage());
}
propName = "ERROR-FINDING-PROP-VALUE";
try {
propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP);
String itemDocnumber = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);
- if (propName==null || itemDocnumber==null){
- //System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
- // +" documentType: "+documentType);
- } else {
- item.setNumber(itemDocnumber);
- //System.out.println("============ found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
- // +" documentType: "+documentType);
- }
- } catch (Throwable t){
- logger.error("====Error finding objectNumberProperty: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"="+propName
- +" not found in itemDocType: "+itemDocType+" inner: "+t.getMessage());
+ if (propName == null || itemDocnumber == null) {
+ //System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
+ // +" documentType: "+documentType);
+ } else {
+ item.setNumber(itemDocnumber);
+ //System.out.println("============ found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
+ // +" documentType: "+documentType);
+ }
+ } catch (Throwable t) {
+ logger.error("====Error finding objectNumberProperty: " + ServiceBindingUtils.OBJ_NUMBER_PROP + "=" + propName
+ + " not found in itemDocType: " + itemDocType + " inner: " + t.getMessage());
}
} else {
item.setError("INVALID: related object is absent");
public String getQProperty(String prop) {
return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;
}
-}
+ /**
+ * Obtains the subject resource and uses its values to populate
+ * subject-related fields in the relation resource.
+ */
+ private DocumentModel getSubjectDocModel(DocumentModel relationDocModel, ServiceContext ctx) throws Exception {
+ // Get the document model for the subject of the relation.
+ DocumentModel subjectDocModel = null;
+ String subjectCsid = "";
+ String subjectRefName = "";
+ // FIXME: Currently assumes that the object CSID is valid if present
+ // in the incoming payload.
+ try {
+ subjectCsid = (String) relationDocModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.SUBJECT_CSID);
+ // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:
+ if (Tools.isBlank(subjectCsid)) {
+ subjectCsid = (String) relationDocModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1);
+ }
+ } catch (PropertyException pe) {
+ // Per CSPACE-4468, ignore any property exception here.
+ // (See parallel comment below in getObjectDocModel.)
+ }
+ if (Tools.notBlank(subjectCsid)) {
+ // FIXME: Call a utility routine here that uses the CSID to retrieve the subject record's docModel.
+ // The following is a placeholder:
+ subjectDocModel = getDocModelFromCsid(subjectCsid);
+ }
+ if (Tools.isBlank(subjectCsid)) {
+ try {
+ subjectRefName = (String) relationDocModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.SUBJECT_REFNAME);
+ // FIXME: Call a utility routine here - for which the method below is currently a
+ // placeholder - that uses the refName to retrieve the subject record's docModel.
+ subjectDocModel = getDocModelFromRefname(subjectRefName);
+ } catch (Exception e) {
+ throw new InvalidDocumentException(
+ "Relation record must contain a CSID or refName to identify the subject of the relation.", e);
+ }
+ }
+ return subjectDocModel;
+ }
+
+ /**
+ * Obtains the object resource and uses its values to populate
+ * object-related fields in the relation resource.
+ */
+ private DocumentModel getObjectDocModel(DocumentModel relationDocModel, ServiceContext ctx) throws Exception {
+ // Get the document model for the object of the relation.
+ String objectCsid = "";
+ String objectRefName = "";
+ DocumentModel objectDocModel = null;
+ // FIXME: Currently assumes that the object CSID is valid if present
+ // in the incoming payload.
+ try {
+ objectCsid = (String) relationDocModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.OBJECT_CSID);
+ // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:
+ if (Tools.isBlank(objectCsid)) {
+ objectCsid = (String) relationDocModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2);
+ }
+ } catch (PropertyException pe) {
+ // Per CSPACE-4468, ignore any property exception here.
+ // The objectCsid and/or subjectCsid field in a relation record
+ // can now be null (missing), because a refName value can be
+ // provided as an alternate identifier.
+ }
+ if (Tools.isBlank(objectCsid)) {
+ try {
+ objectRefName = (String) relationDocModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.OBJECT_REFNAME);
+ // FIXME: Call a utility routine here - for which the method below is currently a
+ // placeholder - that uses the refName to retrieve the object record's docModel.
+ objectDocModel = getDocModelFromRefname(objectRefName);
+ } catch (Exception e) {
+ throw new InvalidDocumentException(
+ "Relation record must have a CSID or refName to identify the object of the relation.", e);
+ }
+ }
+ return objectDocModel;
+ }
+
+ // CSPACE-4468 placeholder methods:
+
+ // FIXME: Placeholder method.
+ private DocumentModel getDocModelFromCsid(String csid) {
+ return null;
+ }
+
+ // FIXME: Placeholder method.
+ // Patrick is providing a working replacement for this method, in a framework class.
+ private DocumentModel getDocModelFromRefname(String csid) {
+ return null;
+ }
+
+ // FIXME: Placeholder method.
+ private DocumentModel populateSubjectValues(DocumentModel relationDocModel, DocumentModel objectDocModel) {
+ throw new UnsupportedOperationException("Not yet implemented");
+ }
+
+ // FIXME: Placeholder method.
+ private DocumentModel populateObjectValues(DocumentModel relationDocModel, DocumentModel objectDocModel) {
+ throw new UnsupportedOperationException("Not yet implemented");
+ }
+}
logger.trace(relationsCommon.toString());\r
}\r
\r
+ String subjectCsid = getSubjectCsid(relationsCommon);\r
+ String objectCsid = getObjectCsid(relationsCommon);\r
+\r
// If no CSID for a subject or object is included in the create payload,\r
// a refName must be provided for that subject or object as an alternate identifier.\r
- assert (hasObjectCsid(relationsCommon) || hasObjectRefname(relationsCommon));\r
- assert (hasSubjectCsid(relationsCommon) || hasSubjectRefname(relationsCommon));\r
+ assert (hasCsid(subjectCsid) || hasSubjectRefname(relationsCommon));\r
+ assert (hasCsid(objectCsid) || hasObjectRefname(relationsCommon));\r
\r
// The Subject identifier and Object ID must not be identical:\r
// that is, a resource cannot be related to itself.\r
- // FIXME: Can store values of calls above if desired to save additional checks here.\r
- if (hasObjectCsid(relationsCommon) && hasSubjectCsid(relationsCommon)) {\r
- assert (relationsCommon.getObjectCsid().trim().equalsIgnoreCase(\r
- relationsCommon.getSubjectCsid().trim()) == false) :\r
+ if (hasCsid(subjectCsid) && hasCsid(objectCsid)) {\r
+ assert (subjectCsid.trim().equalsIgnoreCase(objectCsid.trim()) == false) :\r
SUBJECT_EQUALS_OBJECT_ERROR;\r
}\r
\r
// TODO Auto-generated method stub\r
}\r
\r
- private boolean hasObjectCsid(RelationsCommon relationsCommon) {\r
- String objectCsid = relationsCommon.getObjectCsid();\r
- return hasCsid(objectCsid);\r
+ private String getSubjectCsid(RelationsCommon relationsCommon) {\r
+ String subjectCsid = relationsCommon.getSubjectCsid();\r
+ // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:\r
+ if (Tools.isBlank(subjectCsid)) {\r
+ subjectCsid = relationsCommon.getDocumentId1();\r
+ }\r
+ return subjectCsid;\r
}\r
\r
- private boolean hasSubjectCsid(RelationsCommon relationsCommon) {\r
- String subjectCsid = relationsCommon.getSubjectCsid();\r
- return hasCsid(subjectCsid);\r
+ private String getObjectCsid(RelationsCommon relationsCommon) {\r
+ String objectCsid = relationsCommon.getObjectCsid();\r
+ // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:\r
+ if (Tools.isBlank(objectCsid)) {\r
+ objectCsid = relationsCommon.getDocumentId2();\r
+ }\r
+ return objectCsid;\r
}\r
\r
private boolean hasCsid(String csid) {\r
return hasCsid;\r
}\r
\r
- private boolean hasObjectRefname(RelationsCommon relationsCommon) {\r
- String objectRefName = relationsCommon.getObjectRefName();\r
- return hasRefName(objectRefName);\r
- }\r
-\r
private boolean hasSubjectRefname(RelationsCommon relationsCommon) {\r
String subjectRefName = relationsCommon.getSubjectRefName();\r
return hasRefName(subjectRefName);\r
}\r
\r
+ private boolean hasObjectRefname(RelationsCommon relationsCommon) {\r
+ String objectRefName = relationsCommon.getObjectRefName();\r
+ return hasRefName(objectRefName);\r
+ }\r
+\r
private boolean hasRefName(String refName) {\r
boolean hasRefname = false;\r
if (Tools.isBlank(refName)) {\r