예제 #1
0
        private void SetFieldValue(OAdEntity entity, string fieldName, string fieldValue)
        {
            if (string.IsNullOrEmpty(fieldValue))
            {
                return;
            }

            string retVal = CQWrapper.SetFieldValue(entity, fieldName, fieldValue);

            Trace.WriteIf(!string.IsNullOrEmpty(retVal), "retVal = " + retVal);
            Assert.IsTrue(string.IsNullOrEmpty(retVal),
                          string.Format("SetFiledValue returned non-empty result : {0}, {1}", fieldName, fieldValue));
        }
        private bool SetFieldValue(
            IMigrationAction action,
            OAdEntity record,
            string fieldName,
            string stringVal,
            ref int numOfAttempts)
        {
            numOfAttempts++;

            OAdFieldInfo aFieldInfo         = CQWrapper.GetEntityFieldValue(record, fieldName);
            string       originalFieldValue = CQWrapper.GetFieldValue(aFieldInfo);

            // doing the real job: setting field value with CQ OM
            string cqRetVal = CQWrapper.SetFieldValue(record, fieldName, stringVal);

            // error handling
            if (!string.IsNullOrEmpty(cqRetVal))
            {
                MigrationConflict conflict = ClearQuestSetFieldValueConflictType.CreateConflict(
                    UtilityMethods.ExtractSourceWorkItemId(action),
                    UtilityMethods.ExtractSourceWorkItemRevision(action),
                    fieldName, stringVal, cqRetVal);
                List <MigrationAction> migrationActions;
                var resolutionResult = m_conflictManagerService.TryResolveNewConflict(
                    m_conflictManagerService.SourceId,
                    conflict,
                    out migrationActions);

                if (!resolutionResult.Resolved)
                {
                    // cannot resolve the conflict, move on to next MigrationAction
                    return(false);
                }
                else if (numOfAttempts <= 3)
                {
                    // not reached maximum set value attempts yet
                    if (resolutionResult.ResolutionType == ConflictResolutionType.UpdatedConflictedChangeAction)
                    {
                        XmlNode column = UtilityMethods.ExtractSingleFieldNodeFromMigrationDescription(
                            action.MigrationActionDescription, fieldName);

                        if (null == column)
                        {
                            // the field has been dropped during conflict resolution
                            // restore the "original" value
                            return(SetFieldValue(action, record, fieldName, originalFieldValue, ref numOfAttempts));
                        }
                        else
                        {
                            string newFieldValue = UtilityMethods.ExtractSingleFieldValue(column);
                            return(SetFieldValue(action, record, fieldName, stringVal, ref numOfAttempts));
                        }
                    }
                }
                else
                {
                    // reached max set value attempts WITH unresolved conflict
                    return(false);
                }
            }

            return(true);
        }
        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 hostRecEntityDefName = CQWrapper.GetEntityDefName(hostRecord);

            if (string.IsNullOrEmpty(hostRecEntityDefName) ||
                !CQStringComparer.EntityName.Equals(hostRecEntityDefName, this.m_hostRecordType))
            {
                return(false);
            }

            string refFieldName = linkChangeAction.Link.LinkType.ReferenceName.Substring(ReferenceNameQualifier.Length);

            if (string.IsNullOrEmpty(refFieldName))
            {
                return(false);
            }

            // retrieve reference field information
            OAdFieldInfo refFieldInfo = CQWrapper.GetEntityFieldValue(hostRecord, refFieldName);
            int          cqFieldType  = CQWrapper.GetFieldType(refFieldInfo);

            if (cqFieldType != CQConstants.FIELD_REFERENCE)
            {
                // the field is not of the reference 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 reference value set

                // single value required
                string refFldVal = CQWrapper.GetFieldValue(refFieldInfo);
                if (CQStringComparer.RecordName.Equals(refFldVal, childRecDispName))
                {
                    // the target artifact is already referenced in the field

                    // [teyang] TODO conflict?
                    return(false);
                }
                else
                {
                    // field currently holds a reference to another entity

                    // [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.SetFieldValue(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);
            }
        }