From: Aron Roberts Date: Tue, 11 Oct 2011 03:33:50 +0000 (+0000) Subject: CSPACE-4468: Placeholder code en route to making refNames first class identifiers... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=c1187a35dbb2306ad14feeabda6bf210ec3f48e2;p=tmp%2Fjakarta-migration.git CSPACE-4468: Placeholder code en route to making refNames first class identifiers in Relation records. --- diff --git a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index 59cf1e37d..a20f7de0e 100644 --- a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -659,24 +659,29 @@ public abstract class AuthorityItemDocumentModelHandler RelationsDocListItem itemObject = item.getObject(); String subjectCsid = itemSubject.getCsid(); - rc.setDocumentId1(subjectCsid); rc.setSubjectCsid(subjectCsid); + rc.setDocumentId1(subjectCsid); // populate legacy field for backward compatibility - String objCsid = item.getObject().getCsid(); - rc.setDocumentId2(objCsid); + String objCsid = itemObject.getCsid(); rc.setObjectCsid(objCsid); + rc.setDocumentId2(objCsid); // populate legacy field for backward compatibility + + rc.setSubjectRefName(itemSubject.getRefName()); + rc.setObjectRefName(itemObject.getRefName()); rc.setRelationshipType(item.getPredicate()); //RelationshipType foo = (RelationshipType.valueOf(item.getPredicate())) ; //rc.setPredicate(foo); //this must be one of the type found in the enum in services/jaxb/src/main/resources/relations_common.xsd + rc.setSubjectDocumentType(itemSubject.getDocumentType()); + rc.setObjectDocumentType(itemObject.getDocumentType()); + // populate legacy fields for backward compatibility rc.setDocumentType1(itemSubject.getDocumentType()); rc.setDocumentType2(itemObject.getDocumentType()); rc.setSubjectUri(itemSubject.getUri()); rc.setObjectUri(itemObject.getUri()); - PoxPayloadOut payloadOut = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME); PayloadOutputPart outputPart = new PayloadOutputPart(RelationClient.SERVICE_COMMONPART_NAME, rc); payloadOut.addPart(outputPart); diff --git a/services/jaxb/src/main/resources/relations_common.xsd b/services/jaxb/src/main/resources/relations_common.xsd index ce9d46207..bccb241b7 100644 --- a/services/jaxb/src/main/resources/relations_common.xsd +++ b/services/jaxb/src/main/resources/relations_common.xsd @@ -77,6 +77,7 @@ + diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java index fcf9b7958..0e988e172 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java @@ -31,6 +31,7 @@ import org.collectionspace.services.common.ServiceMain; 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; @@ -48,6 +49,7 @@ import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandler 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; @@ -72,15 +74,36 @@ public class RelationDocumentModelHandler * for ACTION.GET_ALL */ private RelationsCommonList relationList; - + @Override public void handleCreate(DocumentWrapper 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 wrapDoc) throws Exception { - super.handleUpdate(wrapDoc); + super.handleUpdate(wrapDoc); } @Override @@ -118,7 +141,7 @@ public class RelationDocumentModelHandler @Override public RelationsCommonList extractCommonPartList(DocumentWrapper 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(); @@ -128,7 +151,7 @@ public class RelationDocumentModelHandler ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName); Iterator 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); @@ -147,10 +170,10 @@ public class RelationDocumentModelHandler * @throws Exception the exception */ private RelationListItem getRelationListItem(ServiceContext 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); @@ -163,7 +186,7 @@ public class RelationDocumentModelHandler 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. @@ -189,39 +212,38 @@ public class RelationDocumentModelHandler 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 @@ -231,32 +253,32 @@ public class RelationDocumentModelHandler 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"); @@ -270,5 +292,104 @@ public class RelationDocumentModelHandler 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"); + } +} diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java index e89f04ddc..af72ae538 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java @@ -41,17 +41,18 @@ public class RelationValidatorHandler extends ValidatorHandlerImpl