public bool Update( ClearQuestMigrationContext migrationContext, Session session, OAdEntity hostRecord, LinkChangeAction linkChangeAction) { if (null == linkChangeAction) { throw new ArgumentNullException("linkChangeAction"); } if (!linkChangeAction.Link.LinkType.ReferenceName.StartsWith(ReferenceNameQualifier)) { throw new ArgumentException("Link type mismatch."); } string childRecEntityDefName; if (!ClearQuestRecordArtifactHandler.TryExtractRecordDefName(linkChangeAction.Link.TargetArtifact, out childRecEntityDefName)) { return(false); } string childRecDispName; if (!ClearQuestRecordArtifactHandler.TryExtractRecordDispName(linkChangeAction.Link.TargetArtifact, out childRecDispName)) { return(false); } string refFieldName = linkChangeAction.Link.LinkType.ReferenceName.Substring(ReferenceNameQualifier.Length); if (string.IsNullOrEmpty(refFieldName)) { return(false); } string hostRecEntityDefName = CQWrapper.GetEntityDefName(hostRecord); if (string.IsNullOrEmpty(hostRecEntityDefName) || !CQStringComparer.EntityName.Equals(hostRecEntityDefName, this.m_hostRecordType)) { return(false); } // retrieve reference field information OAdFieldInfo refFieldInfo = CQWrapper.GetEntityFieldValue(hostRecord, refFieldName); int cqFieldType = CQWrapper.GetFieldType(refFieldInfo); if (cqFieldType != CQConstants.FIELD_REFERENCE_LIST) { // the field is not of the FIELD_REFERENCE_LIST type // [teyang] TODO conflict? return(false); } // get the current entity def OAdEntityDef hostRecordEntityDef = CQWrapper.GetEntityDef(session, CQWrapper.GetEntityDefName(hostRecord)); OAdEntityDef childRecordEntityDef = CQWrapper.GetFieldReferenceEntityDef(hostRecordEntityDef, refFieldName); string childRecordEntityDefName = CQWrapper.GetEntityDefName(childRecordEntityDef); if (!CQStringComparer.EntityName.Equals(childRecordEntityDefName, childRecEntityDefName)) { // the field is not designated to hold reference to the EntityType of the target artifact // [teyang] TODO conflict? return(false); } int valueStatus = CQWrapper.GetFieldValueStatus(refFieldInfo); if (valueStatus == (int)CQConstants.FieldStatus.HAS_VALUE) { // the field already has a list of reference values set bool targetArtifactIsSet = false; // value list expected object[] refFldValues = CQWrapper.GetFieldValueAsList(refFieldInfo); foreach (object refFldObj in refFldValues) { string refFldVal = (string)refFldObj; if (CQStringComparer.RecordName.Equals(refFldVal, childRecDispName)) { targetArtifactIsSet = true; break; } } if (targetArtifactIsSet) { // the target artifact is already referenced in the field // [teyang] TODO conflict? return(false); } } string[] modifyActionNames = CQUtilityMethods.FindAllChangeActionNamesByType( session, hostRecord, CQConstants.ACTION_MODIFY); if (modifyActionNames.Length == 0) { // [teyang] TODO conflict? return(false); } else if (modifyActionNames.Length > 1) { // [teyang] TODO conflict? return(false); } else { string modAction = modifyActionNames[0]; CQWrapper.EditEntity(session, hostRecord, modAction); string retVal = CQWrapper.AddFieldValue(hostRecord, refFieldName, childRecDispName); retVal = CQWrapper.Validate(hostRecord); if (string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict return(false); } retVal = CQWrapper.Commmit(hostRecord); if (string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict return(false); } return(true); } }