private bool SetMandatoryFields(IMigrationAction action, ref OAdEntity newRecord, out List <string> processedFields) { XmlNodeList columns = action.MigrationActionDescription.SelectNodes("/WorkItemChanges/Columns/Column"); if (null == columns) { throw new MigrationException(ClearQuestResource.ClearQuest_Error_InvalidActionDescription, action.ActionId); } StringBuilder updateLog = new StringBuilder(); PrintUpdateLogHeader(action, updateLog); processedFields = new List <string>(); foreach (XmlNode columnData in columns) { string stringVal = columnData.FirstChild.InnerText; string fieldName = columnData.Attributes["ReferenceName"].Value; Debug.Assert(!string.IsNullOrEmpty(fieldName), "Field ReferenceName is absent in the Migration Description"); OAdFieldInfo aFieldInfo = CQWrapper.GetEntityFieldValue(newRecord, fieldName); int fieldRequiredness = CQWrapper.GetRequiredness(aFieldInfo); if (fieldRequiredness != CQConstants.MANDATORY) { // skipping all non-mandatory fields continue; } string originalFieldValue = CQWrapper.GetFieldValue(aFieldInfo); int attempt1Count = 0; if (!SetFieldValue(action, newRecord, fieldName, stringVal, ref attempt1Count)) { return(false); } AddFieldToUpdateLog(fieldName, stringVal, updateLog); processedFields.Add(fieldName); } AddLineToUpdateLog(updateLog); int attempt2Count = 0; if (!SetFieldValue(action, newRecord, NoteEntryFieldName, updateLog.ToString(), ref attempt2Count)) { return(false); } return(true); }
internal static object[] GetFieldValueAsList(OAdFieldInfo fldInfo) { object[] returnval = null; try { returnval = (object[])fldInfo.GetValueAsList(); } catch (COMException ex) { InteropErrorHandler.HandleCQException(ex); } return(returnval); }
internal static OAdFieldInfo GetEntityFieldValue(OAdEntity cqEntity, string fldName) { OAdFieldInfo retval = null; try { retval = (OAdFieldInfo)cqEntity.GetFieldValue(fldName); } catch (COMException ex) { InteropErrorHandler.HandleCQException(ex); } return(retval); }
internal static int GetRequiredness(OAdFieldInfo fldInfo) { int retVal = int.MinValue; try { retVal = fldInfo.GetRequiredness(); } catch (COMException ex) { InteropErrorHandler.HandleCQException(ex); } return(retVal); }
internal static int GetFieldValueStatus(OAdFieldInfo fldInfo) { int returnval = 0; try { returnval = fldInfo.GetValueStatus(); } catch (COMException ex) { InteropErrorHandler.HandleCQException(ex); } return(returnval); }
internal static string GetFieldValue(OAdFieldInfo fldInfo) { string returnval = null; try { returnval = fldInfo.GetValue(); } catch (COMException ex) { InteropErrorHandler.HandleCQException(ex); } return(returnval); }
internal static int GetFieldType(OAdFieldInfo fldInfo) { int retval = 0; try { retval = fldInfo.GetType(); } catch (COMException ex) { HandleCQException(ex); } return(retval); }
} // end of CQEntityRec CTor /// <summary> /// Populate the current record from CQ if its not already in Currituck /// and also process all its references (recursively), Links, History and Attachments /// Else just sets the currituck id for future reference /// </summary> public bool Populate() { bool partiallyMigrated = false; // first check if it exists in the memory cache CQEntity currentEntityRecords = m_cqParams.entityRecords[m_entityName]; CQEntityRec lookupEntity = currentEntityRecords.FindEntityRec(m_entityName, m_dbid); if (lookupEntity != null) { // record already populated.. Logger.Write(LogSource.CQ, TraceLevel.Verbose, "Already populated record '{0}' for Entity '{1}', DBID:{2}", lookupEntity.SourceId, lookupEntity.EntityName, lookupEntity.DBID); return(true); } m_CQEntity = CQWrapper.GetEntityByDbId(m_cqParams.cqSession, m_entityName, m_dbid); // get the source id m_sourceId = CQWrapper.GetEntityDisplayName(m_CQEntity); Logger.Write(LogSource.CQ, TraceLevel.Verbose, UtilityMethods.Format(CQResource.CQ_PROCESSING_REC, m_sourceId)); // check if it exist in currituck using static API VSTSWorkItemHelper wiHelper = (VSTSWorkItemHelper)m_MySchemaMap.vstsHelper; ArrayList checkList = new ArrayList(); checkList.Add(new WorkItemNameValueRelation(CommonConstants.VSTSSrcIdField, m_sourceId)); checkList.Add(new WorkItemNameValueRelation(CommonConstants.VSTSSrcDbField, m_cqParams.uniqueInstId)); wiHelper = (VSTSWorkItemHelper)m_MySchemaMap.vstsHelper; if (wiHelper.IsWIMigrated(checkList) == true) { // need not to load the data from CQ.. // just set the currituck id // not going to update this bug from CQ->Currituck even // if it is updated.. just get out from here as my population is done // with minimal required stuff string warningMsg = UtilityMethods.Format(CQResource.CQ_REC_MIGRATED, m_sourceId); Logger.Write(LogSource.CQ, TraceLevel.Warning, warningMsg); PostMigrationReport.WriteIssue(m_MySchemaMap.entity, m_MySchemaMap.WIT, Stats.MigrationStatus.Skipped, ReportIssueType.Warning, String.Empty, m_sourceId, IssueGroup.Wi, warningMsg); m_WITId = wiHelper.WorkItemId; //compact current object CompactMe(); return(true); } else if (wiHelper.IsCurrentWorkItemValid() == true) { // work item is already there.. partially migrated partiallyMigrated = true; } #if DEBUG CommonConstants.NoOfBugs++; #endif // create the required data structures m_imWorkItem = new InMemoryWorkItem(); string fldName; OAdEntityDef curEntityDef = CQWrapper.GetEntityDef(m_cqParams.cqSession, m_entityName); Logger.Write(LogSource.CQ, TraceLevel.Verbose, "Adding record for Entity {0}, Record {1}", m_entityName, CQWrapper.GetEntityDisplayName(m_CQEntity)); object[] fields = (object[])CQWrapper.GetEntityFieldNames(m_CQEntity); foreach (object fld in fields) { fldName = (string)fld; if (CQConstants.InternalFieldTypes.ContainsKey(fldName)) { // these are internal clearquest fields // we dont want to migrate these Logger.Write(LogSource.CQ, TraceLevel.Info, "Skipping Internal Field '{0}' while migrating data for entity {1}", fldName, m_entityName); continue; } { // process this field only if it exists in the "from" side of Field Map OAdFieldInfo fldInfo = CQWrapper.GetEntityFieldValue(m_CQEntity, fldName); int cqFieldType = CQWrapper.GetFieldType(fldInfo); switch (cqFieldType) { case CQConstants.FIELD_ID: case CQConstants.FIELD_SHORT_STRING: case CQConstants.FIELD_INT: { string fldValue = CQWrapper.GetFieldValue(fldInfo); if (fldValue != null) { m_imWorkItem.InitialView.Add(fldName, fldValue); } } break; case CQConstants.FIELD_MULTILINE_STRING: { string fldValue = CQWrapper.GetFieldValue(fldInfo); if (currentEntityRecords.Entity == null) { // build entity to get the list of allowed/suggested values currentEntityRecords.Entity = CQWrapper.BuildEntity(m_cqParams.cqSession, currentEntityRecords.EntityName); } object[] choices = (object[])CQWrapper.GetFieldChoiceList(currentEntityRecords.Entity, fldName); if (choices != null && choices.Length > 0) { // Multi Line String with List of Allowed/Suggested Values.. replace all '\n' with comma // fix for bug# 429098 if (fldValue != null) { fldValue = fldValue.Replace("\n", ","); } } /* no conversion shall be required.. bug# 20219 - shall be rendered in HTML as it is * // hack for Notes_Log & Description field.. Shall be converted to HTML (bug#429032) * if (fldName.Equals("Notes_Log", StringComparison.OrdinalIgnoreCase) || * fldName.Equals("Description", StringComparison.OrdinalIgnoreCase)) * { * fldValue = VSTSUtil.ConvertTextToHtml(fldValue); * } */ m_imWorkItem.InitialView.Add(fldName, fldValue); } break; case CQConstants.FIELD_DATE_TIME: { string fldValue = CQWrapper.GetFieldValue(fldInfo); if (fldValue != null) { // the time returned from CQ API is the local time.. DateTime fldVal = DateTime.Parse(fldValue, CultureInfo.CurrentCulture); //convert it in UTC DateTime utcTime = CQConverterUtil.ConvertLocalToUTC(fldVal); Logger.Write(LogSource.CQ, TraceLevel.Verbose, "Field [{0}], CQ Time [{1}], UTC Time [{2}]", fldName, fldVal.ToString(), utcTime.ToString()); m_imWorkItem.InitialView.Add(fldName, utcTime); } else { Logger.Write(LogSource.CQ, TraceLevel.Info, "Got null value for field {0}", fldName); } } break; case CQConstants.FIELD_REFERENCE: { // get the current entity def handle OAdEntityDef refEntityDef = CQWrapper.GetFieldReferenceEntityDef(curEntityDef, fldName); string refEntityName = CQWrapper.GetEntityDefName(refEntityDef); // special handling for users.. add the user field value also.. // we dont want to create a link in this case.. // just add the field value pair in IMWorkItem.. and // user map will be applied while saving if (TFStringComparer.WorkItemType.Equals(refEntityName, "users")) { if (CQWrapper.GetFieldValueStatus(fldInfo) == (int)CQConstants.FieldStatus.HAS_VALUE) { // single value required string refFldVal = CQWrapper.GetFieldValue(fldInfo); m_imWorkItem.InitialView.Add(fldName, refFldVal); } } else if (m_cqParams.allowedEntities.ContainsKey(refEntityName)) { int valueStatus = CQWrapper.GetFieldValueStatus(fldInfo); Logger.WriteIf((valueStatus != (int)CQConstants.FieldStatus.HAS_VALUE), LogSource.CQ, TraceLevel.Info, "No Value for Referenced field {0} in Entity {1}", refEntityName, m_entityName); if (valueStatus == (int)CQConstants.FieldStatus.HAS_VALUE) { // single value required string refFldVal = CQWrapper.GetFieldValue(fldInfo); if (String.Equals(refFldVal, SourceId, StringComparison.Ordinal)) { // reference to self.. cannot have a link on to self string warningMsg = UtilityMethods.Format(CQResource.CQ_SELF_REFERENCE, SourceId, EntityName, fldName); Logger.Write(LogSource.CQ, TraceLevel.Warning, warningMsg); PostMigrationReport.WriteIssue(m_MySchemaMap.entity, m_MySchemaMap.WIT, Stats.MigrationStatus.Warning, ReportIssueType.Warning, String.Empty, m_sourceId, IssueGroup.Wi, warningMsg ); } else { m_referencedEntities.Add(new LinkRecord(refEntityName, refFldVal)); } } } } break; case CQConstants.FIELD_REFERENCE_LIST: { // get the current entity def handle OAdEntityDef refEntityDef = CQWrapper.GetFieldReferenceEntityDef(curEntityDef, fldName); string refEntityName = CQWrapper.GetEntityDefName(refEntityDef); // special handling for user list // we dont want to create a link in this case.. // concatenate all the user names separated by comma // NO USER MAP WILL BE APPLIED WHILE SAVING (bug#400276) if (TFStringComparer.WorkItemType.Equals(refEntityName, "users")) { if (CQWrapper.GetFieldValueStatus(fldInfo) == (int)CQConstants.FieldStatus.HAS_VALUE) { object[] refFldValues = CQWrapper.GetFieldValueAsList(fldInfo); StringBuilder userList = new StringBuilder(); for (int valueIndex = 0; valueIndex < refFldValues.Length; valueIndex++) { object refFldObj = refFldValues[valueIndex]; if (valueIndex > 0) { userList.Append(","); } userList.Append((string)refFldObj); } m_imWorkItem.InitialView.Add(fldName, userList.ToString()); } } else if (m_cqParams.allowedEntities.ContainsKey(refEntityName)) { int valueStatus = CQWrapper.GetFieldValueStatus(fldInfo); Logger.WriteIf((valueStatus != (int)CQConstants.FieldStatus.HAS_VALUE), LogSource.CQ, TraceLevel.Info, "No Value for Referenced field {0} in Entity {1}", fldName, m_entityName); if (valueStatus == (int)CQConstants.FieldStatus.HAS_VALUE) { // value list expected object[] refFldValues = CQWrapper.GetFieldValueAsList(fldInfo); foreach (object refFldObj in refFldValues) { string refFldVal = (string)refFldObj; if (String.Equals(refFldVal, SourceId, StringComparison.Ordinal)) { // reference to self.. cannot have a link on to self string warningMsg = UtilityMethods.Format(CQResource.CQ_SELF_REFERENCE, SourceId, EntityName, fldName); Logger.Write(LogSource.CQ, TraceLevel.Warning, warningMsg); PostMigrationReport.WriteIssue(m_MySchemaMap.entity, m_MySchemaMap.WIT, Stats.MigrationStatus.Warning, ReportIssueType.Warning, String.Empty, m_sourceId, IssueGroup.Wi, warningMsg); } else { m_referencedEntities.Add(new LinkRecord(refEntityName, refFldVal)); } } } } } break; case CQConstants.FIELD_ATTACHMENT_LIST: case CQConstants.FIELD_STATE: case CQConstants.FIELD_JOURNAL: case CQConstants.FIELD_DBID: case CQConstants.FIELD_STATETYPE: case CQConstants.FIELD_RECORDTYPE: Logger.Write(LogSource.CQ, TraceLevel.Info, "Skipping the Field migration for Internal Field Type '{0}'", cqFieldType); // not migrating these fields as they are CQ internal fields continue; default: Logger.Write(LogSource.CQ, TraceLevel.Info, "Skipping the Field migration for Unkknown Field Type '{0}'", cqFieldType); break; } } } // end of foreachfields // add the source id and db separately m_imWorkItem.InitialView.Add(CommonConstants.VSTSSrcIdField, m_sourceId); m_imWorkItem.InitialView.Add(CommonConstants.VSTSSrcDbField, m_cqParams.uniqueInstId); // use vstshelper to migrate the data wiHelper = (VSTSWorkItemHelper)m_MySchemaMap.vstsHelper; wiHelper.IsWIMigrated(checkList); // get attachments in the imworkitem ProcessAttachments(); // history processing will use same imWorkItem for first history info // and create other history indexes int migratedHistory = 0; if (wiHelper.IsCurrentWorkItemValid()) { migratedHistory = wiHelper.GetCurrentWorkItemHistoryCount(); if (migratedHistory > 0) { // We are going for incremental migration. And as we stuff first history item of a CQBug // into InitialView itself, actual no. of migrated history is one more than the value of // the "Migration Status" field. So increment by one. ++migratedHistory; } } ArrayList historyItems = ProcessHistory(m_imWorkItem.InitialView, migratedHistory); Logger.Write(LogSource.CQ, TraceLevel.Verbose, "Dumping initial view for {0}", m_sourceId); foreach (object key in m_imWorkItem.InitialView.Keys) { Logger.Write(LogSource.CQ, TraceLevel.Verbose, "{0} - {1}", key, m_imWorkItem.InitialView[key]); } bool initialViewStatus = true; try { if (!partiallyMigrated) { // if some history items or links are left to be migrated.. leave the bug as opened.. if (historyItems.Count > 0 || m_referencedEntities.Count > 0) { Logger.Write(LogSource.CQ, TraceLevel.Verbose, "Creating initial view of {0} .. {1} Histories, {2} Links pending", SourceId, historyItems.Count, m_referencedEntities.Count); // create the record and keep it open for history editing initialViewStatus = wiHelper.CreateInitialViewOfWorkItem(m_sourceId, m_imWorkItem, false); } else { Logger.Write(LogSource.CQ, TraceLevel.Verbose, "Creating initial view of {0}", SourceId); // create all the entries in the record and set the status to done initialViewStatus = wiHelper.CreateInitialViewOfWorkItem(m_sourceId, m_imWorkItem, true); } } } catch (Exception ex) { // creation of work item failed string errMsg = UtilityMethods.Format(CQResource.CQ_WI_CREATION_FAILED, SourceId, ex.Message); CQConverter.ReportWorkItemFailure(errMsg, SourceId, m_MySchemaMap.entity, m_MySchemaMap.WIT, m_cqParams.exitOnError); if (m_cqParams.exitOnError == true) { throw new ConverterException(errMsg); } else { // continue with another work item // need to skip this work item.. m_WITId = -1; CompactMe(); return(false); } } finally { } // get back currituck id and store in this m_WITId = wiHelper.WorkItemId; // store the handle of work item to restore the state of work item helper back to // working work item which may get changed because of processing links recursively object workItem = wiHelper.GetCurrentWorkItem(); // before processing history, clean out attachments.. only if its already migrated if (wiHelper.GetCurrentWorkItemAttachmentsCount() == m_imWorkItem.Attachments.Count) { m_imWorkItem.Attachments.Clear(); } // add all the links now so that they go as part of history bool refRecordStatus = true; foreach (LinkRecord linkRec in m_referencedEntities) { if (AddReferenceRecord(linkRec) == false) { refRecordStatus = false; // once false always false } } // process duplicate records if (ProcessDuplicates(m_cqParams) == false) { refRecordStatus = false; } bool writeHistoryPassed = true; wiHelper.SetCurrentWorkItem(workItem); if (historyItems.Count > 0 || m_imWorkItem.Links.Count > 0 || m_imWorkItem.Attachments.Count > 0) { m_imWorkItem.HistoryItems = historyItems; try { writeHistoryPassed = wiHelper.WriteHistoryItems(m_sourceId, m_imWorkItem, refRecordStatus && initialViewStatus); if (!writeHistoryPassed) { // Bug#59861: In the case of the partially migrated bug, // converter says all bugs migrated successfully in // summary, but in error section it says one bug failed // due to attachment size issue. This issue has already // been written to the report. Just need to update the // statistics info. PostMigrationReport.WriteIssue(m_MySchemaMap.entity, m_MySchemaMap.WIT, Stats.MigrationStatus.Failed, ReportIssueType.Info, null, m_sourceId, IssueGroup.Wi, null); } // set the bug migration status to done only if there were no // problems with initial view and any of the references if ((!writeHistoryPassed || !refRecordStatus || !initialViewStatus) && m_cqParams.exitOnError) { // stop processing more records CompactMe(); return(false); } } catch (Exception ex) { // creation of history failed string errMsg = UtilityMethods.Format(CQResource.CQ_WI_MIG_FAILED, SourceId, ex.Message); CQConverter.ReportWorkItemFailure(errMsg, SourceId, m_MySchemaMap.entity, m_MySchemaMap.WIT, m_cqParams.exitOnError); if (m_cqParams.exitOnError == true) { throw new ConverterException(errMsg); } else { // continue with another work item.. reporting this failure CompactMe(); return(false); } } // end of catch finally { } } // end of history items processing // add to pass count ConverterMain.MigrationReport.Statistics.NumberOfItems++; // add to per work item type section if (writeHistoryPassed) { PostMigrationReport.WriteIssue(m_MySchemaMap.entity, m_MySchemaMap.WIT, Stats.MigrationStatus.Passed, ReportIssueType.Info, null, m_sourceId, IssueGroup.Wi, null); } //compact current object CompactMe(); return(true); } // end of Populate()
public bool Update( ClearQuestMigrationContext migrationContext, Session cqSession, OAdEntity hostRecord, LinkChangeAction linkChangeAction) { if (null == linkChangeAction) { throw new ArgumentNullException("linkChangeAction"); } if (!linkChangeAction.Link.LinkType.ReferenceName.Equals(REFERENCE_NAME)) { 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); } OAdEntity childEntity = CQWrapper.GetEntity(cqSession, childRecEntityDefName, childRecDispName); if (null == childEntity) { return(false); } // check if hostRecord already has a duplicate of this childRecord bool duplicateAlreadyExist = HasDuplicateRecord(hostRecord, childRecEntityDefName, childRecDispName); // find out the child entity's current state // find the current state string childEntityDefName = CQWrapper.GetEntityDefName(childEntity); OAdFieldInfo aFldInfo = CQWrapper.GetEntityFieldValue(childEntity, migrationContext.GetStateField(childEntityDefName)); string srcState = CQWrapper.GetFieldValue(aFldInfo); OAdEntityDef childEntityDef = CQWrapper.GetEntityDef(cqSession, childEntityDefName); if (linkChangeAction.ChangeActionId.Equals(WellKnownChangeActionId.Add)) { if (duplicateAlreadyExist) { // [teyang] TODO return(false); } string[] changeActionNames = CQUtilityMethods.FindAllActionNameByTypeAndSourceState( childEntityDef, srcState, CQConstants.ACTION_DUPLICATE); string changeActionName = string.Empty; if (changeActionNames.Length == 0) { // [teyang] TODO conflict } else if (changeActionNames.Length > 1) { // [teyang] TODO conflict } else { changeActionName = changeActionNames[0]; } if (!string.IsNullOrEmpty(changeActionName)) { CQWrapper.MarkEntityAsDuplicate(cqSession, childEntity, hostRecord, changeActionName); string retVal = CQWrapper.Validate(childEntity); if (string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict } retVal = CQWrapper.Commmit(childEntity); if (string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict } } } else if (linkChangeAction.ChangeActionId.Equals(WellKnownChangeActionId.Delete)) { if (!duplicateAlreadyExist) { // [teyang] TODO return(false); } string[] changeActionNames = CQUtilityMethods.FindAllActionNameByTypeAndSourceState( childEntityDef, srcState, CQConstants.ACTION_UNDUPLICATE); string changeActionName = string.Empty; if (changeActionNames.Length == 0) { // [teyang] TODO conflict } else if (changeActionNames.Length > 1) { // [teyang] TODO conflict } else { changeActionName = changeActionNames[0]; } if (!string.IsNullOrEmpty(changeActionName)) { CQWrapper.UnmarkEntityAsDuplicate(cqSession, childEntity, changeActionName); string retVal = CQWrapper.Validate(childEntity); if (string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict } retVal = CQWrapper.Commmit(childEntity); if (string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict } } } else { //throw new MigrationException(TfsWITAdapterResources.ErrorUnsupportedChangeAction); } return(true); }
public XmlDocument CreateRecordDesc( OAdEntity record, string versionStr, ClearQuestMigrationContext migrationContext, bool isLastRevOfThisSyncCycle) { string lastAuthor; DateTime lastChangeDate; FindLastRevDtls(record, out lastAuthor, out lastChangeDate); ClearQuestRecordDescription recordDesc = new ClearQuestRecordDescription(); recordDesc.CreateHeader(lastAuthor, lastChangeDate, MigrationRecordId, EntityDefName, versionStr, isLastRevOfThisSyncCycle); object[] fieldNames = (object[])CQWrapper.GetEntityFieldNames(record); foreach (object fldName in fieldNames) { string fieldName = (string)fldName; if (s_skipAlgorithm.SkipField(fieldName)) { TraceManager.TraceInformation("Skipping Field '{0}' while migrating data for Entity '{1}'", fieldName, EntityDefName); continue; } OAdFieldInfo fldInfo = CQWrapper.GetEntityFieldValue(record, fieldName); int cqFieldType = CQWrapper.GetFieldType(fldInfo); switch (cqFieldType) { case CQConstants.FIELD_INT: case CQConstants.FIELD_ID: case CQConstants.FIELD_SHORT_STRING: case CQConstants.FIELD_STATE: case CQConstants.FIELD_DBID: case CQConstants.FIELD_STATETYPE: case CQConstants.FIELD_RECORDTYPE: { string fldValue = CQWrapper.GetFieldValue(fldInfo); recordDesc.AddField(fieldName, string.Empty, fldValue ?? String.Empty); } break; case CQConstants.FIELD_MULTILINE_STRING: { string fldValue = CQWrapper.GetFieldValue(fldInfo); if (migrationContext == null || !CQStringComparer.FieldName.Equals(migrationContext.NotesLogFieldName, fieldName)) { // non-log field try { var aTestEntity = CreateTestEntity(record, migrationContext); object[] choices = (object[])CQWrapper.GetFieldChoiceList(aTestEntity, fieldName); if (choices != null && choices.Length > 0) { // Multi Line String with List of Allowed/Suggested Values.. replace all '\n' with comma // fix for bug# 429098 if (fldValue != null) { fldValue = fldValue.Replace("\n", ","); } } } catch (Exception ex) { // NOTE: // This feature of testing the multiline string choices create a dummy CQ record // The API call CreateTestEntity requires WRITE permission to CQ // If the migration account doesn't have the permission, we will simply use // the field's current value TraceManager.TraceInformation( "Skipping retrieval of Allowed Values for field '{0}' - Write permission is needed. Error: {1}", fieldName, ex.Message); } recordDesc.AddField(fieldName, string.Empty, fldValue); } else if (fldValue != null) { // log field StringBuilder sb = new StringBuilder(); List <CQNotesLog> notes = CQNotesLog.Parse(fldValue); foreach (CQNotesLog note in notes) { if (string.IsNullOrEmpty(note.Content) || note.Content.Contains(Constants.PlatformCommentSuffixMarker)) { // skip empty logs or those generated by this adapter continue; } if (note.Header.ChangeDate.CompareTo(migrationContext.CurrentHWMBaseLine) <= 0) { // skip the logs before the hwm continue; } sb.AppendFormat("{0} {1} {2}\n", CQNotesLogHeader.NotesLogHeaderIdentifier, note.HeaderString, CQNotesLogHeader.NotesLogHeaderIdentifier); sb.AppendLine(note.Content); } string extractedLog = sb.ToString(); if (!string.IsNullOrEmpty(extractedLog)) { recordDesc.AddField(fieldName, string.Empty, extractedLog); } } } break; case CQConstants.FIELD_DATE_TIME: { string fldValue = CQWrapper.GetFieldValue(fldInfo); if (fldValue != null) { // the time returned from CQ API is the local time.. DateTime fldVal = DateTime.Parse(fldValue, CultureInfo.CurrentCulture); //convert it in UTC DateTime utcTime = UtilityMethods.ConvertLocalToUTC(fldVal); TraceManager.TraceInformation("Field [{0}], CQ Time [{1}], UTC Time [{2}]", fieldName, fldVal.ToString(), utcTime.ToString()); recordDesc.AddField(fieldName, string.Empty, utcTime.ToString()); } else { recordDesc.AddField(fieldName, string.Empty, string.Empty); } } break; case CQConstants.FIELD_REFERENCE: { // get the current entity def handle OAdEntityDef curEntityDef = CQWrapper.GetEntityDef(CQSession, EntityDefName); OAdEntityDef refEntityDef = CQWrapper.GetFieldReferenceEntityDef(curEntityDef, fieldName); string refEntityName = CQWrapper.GetEntityDefName(refEntityDef); if (CQWrapper.GetFieldValueStatus(fldInfo) == (int)CQConstants.FieldStatus.HAS_VALUE) { // single value required string refFldVal = CQWrapper.GetFieldValue(fldInfo); recordDesc.AddField(fieldName, string.Empty, refFldVal ?? string.Empty); } else { recordDesc.AddField(fieldName, string.Empty, string.Empty); } } break; case CQConstants.FIELD_REFERENCE_LIST: { // get the current entity def handle OAdEntityDef curEntityDef = CQWrapper.GetEntityDef(CQSession, EntityDefName); OAdEntityDef refEntityDef = CQWrapper.GetFieldReferenceEntityDef(curEntityDef, fieldName); string refEntityName = CQWrapper.GetEntityDefName(refEntityDef); object[] refFldValues = CQWrapper.GetFieldValueAsList(fldInfo); if (refFldValues != null) { StringBuilder userList = new StringBuilder(); for (int valueIndex = 0; valueIndex < refFldValues.Length; valueIndex++) { object refFldObj = refFldValues[valueIndex]; if (valueIndex > 0) { userList.Append(","); } userList.Append((string)refFldObj); } recordDesc.AddField(fieldName, string.Empty, userList.ToString()); } else { recordDesc.AddField(fieldName, string.Empty, string.Empty); } } break; case CQConstants.FIELD_ATTACHMENT_LIST: case CQConstants.FIELD_JOURNAL: TraceManager.TraceInformation("Skipping the Field migration for Internal Field Type '{0}'", cqFieldType); // not migrating these fields as they are CQ internal fields continue; default: TraceManager.TraceInformation("Skipping the Field migration for Unkknown Field Type '{0}'", cqFieldType); break; } // end of switch cqFieldType } // end of foreach fieldNames return(recordDesc.DescriptionDocument); }
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); }
/// <summary> /// /// </summary> /// <param name="entity"></param> /// <param name="action"></param> /// <param name="convRslt"></param> /// <param name="stateTransitFieldNode"></param> /// <param name="skipFields"></param> /// <returns></returns> private bool ChangeRecordState( OAdEntity entity, IMigrationAction action, ConversionResult convRslt, XmlNode stateTransitFieldNode, out List <string> processedFields) { processedFields = new List <string>(); string destState = UtilityMethods.ExtractSingleFieldValue(stateTransitFieldNode); Debug.Assert(!string.IsNullOrEmpty(destState), "string.IsNullOrEmpty(newState)"); // get the record's display name for updating conversion history string recordDisplayName = CQWrapper.GetEntityDisplayName(entity); string entityDefName = CQWrapper.GetEntityDefName(entity); // find the current state OAdFieldInfo aFldInfo = CQWrapper.GetEntityFieldValue(entity, m_migrationContext.GetStateField(entityDefName)); string srcState = CQWrapper.GetFieldValue(aFldInfo); if (CQStringComparer.StateName.Equals(srcState, destState)) { // state does not change, skip this action return(false); } // find action def name OAdEntityDef entityDef = CQWrapper.GetEntityDef(m_userSession, entityDefName); string[] changeActionNames = CQUtilityMethods.FindAllActionNameByTypeAndStateTransition(entityDef, srcState, destState, CQConstants.ACTION_CHANGE); if (changeActionNames.Length == 0) { // [teyang] todo error handling throw new InvalidOperationException(); } string changeActionName = changeActionNames[0]; // ********* // cache the current history count for all "history fields" // i.e. pairs of HistoryFieldName, count Dictionary <string, int> recordHistoryCountCache = new Dictionary <string, int>(); BuildRecordHistoryCountCache(entity, recordHistoryCountCache); StringBuilder updateLog = new StringBuilder(); PrintUpdateLogHeader(action, updateLog); // mark entity to be editable with the desired state-change action) CQWrapper.EditEntity(m_userSession, entity, changeActionName); XmlNodeList columns = action.MigrationActionDescription.SelectNodes("/WorkItemChanges/Columns/Column"); if (null == columns) { throw new MigrationException(ClearQuestResource.ClearQuest_Error_InvalidActionDescription, action.ActionId); } foreach (XmlNode columnData in columns) { string stringVal = columnData.FirstChild.InnerText; string fieldName = columnData.Attributes["ReferenceName"].Value; Debug.Assert(!string.IsNullOrEmpty(fieldName), "Field ReferenceName is absent in the Migration Description"); OAdFieldInfo aFieldInfo = CQWrapper.GetEntityFieldValue(entity, fieldName); int fieldRequiredness = CQWrapper.GetRequiredness(aFieldInfo); if (fieldRequiredness != CQConstants.MANDATORY) { // skipping all non-mandatory fields continue; } int attempt1Count = 0; if (!SetFieldValue(action, entity, fieldName, stringVal, ref attempt1Count)) { return(false); } AddFieldToUpdateLog(fieldName, stringVal, updateLog); processedFields.Add(fieldName); } AddLineToUpdateLog(updateLog); int attempt2Count = 0; if (!SetFieldValue(action, entity, NoteEntryFieldName, updateLog.ToString(), ref attempt2Count)) { return(false); } string retVal = CQWrapper.Validate(entity); if (!string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict handling throw new InvalidOperationException(retVal); } retVal = CQWrapper.Commmit(entity); if (!string.IsNullOrEmpty(retVal)) { // [teyang] TODO conflict handling throw new InvalidOperationException(retVal); } if (action.State == ActionState.Pending) { action.State = ActionState.Complete; } // ********* // now comparing to the cache, so that we can clearly identify the item:version pairs // e.g. TargetCQRecordDisplayName : HistoryFieldName::LatestHistoryIndex Dictionary <string, int[]> updatedHistoryIndices = new Dictionary <string, int[]>(); FindUpdatedHistoryIndices(entity, recordHistoryCountCache, updatedHistoryIndices); recordHistoryCountCache.Clear(); foreach (string histFieldName in updatedHistoryIndices.Keys) { foreach (int histIndex in updatedHistoryIndices[histFieldName]) { UpdateConversionHistory(action, recordDisplayName, CQHistoryMigrationItem.CreateHistoryItemVersion(histFieldName, histIndex), convRslt); } } return(true); }
private void ModifyRecordContent( OAdEntity entity, IMigrationAction action, ConversionResult convRslt, List <string> skipFields) { XmlNodeList columns = action.MigrationActionDescription.SelectNodes("/WorkItemChanges/Columns/Column"); if (null == columns) { throw new MigrationException(ClearQuestResource.ClearQuest_Error_InvalidActionDescription, action.ActionId); } // get the record's display name for updating conversion history string recordDisplayName = CQWrapper.GetEntityDisplayName(entity); // ********* // cache the current history count for all "history fields" // i.e. pairs of HistoryFieldName, count Dictionary <string, int> recordHistoryCountCache = new Dictionary <string, int>(); BuildRecordHistoryCountCache(entity, recordHistoryCountCache); SetRecordEditable(entity); StringBuilder updateLog = new StringBuilder(); PrintUpdateLogHeader(action, updateLog); string entityDefName = CQWrapper.GetEntityDefName(entity); string stateTransitionFieldDefName = m_migrationContext.GetStateField(entityDefName); string retVal; bool recordIsUpdated = false; foreach (XmlNode columnData in columns) { string stringVal = columnData.FirstChild.InnerText; string fieldName = columnData.Attributes["ReferenceName"].Value; Debug.Assert(!string.IsNullOrEmpty(fieldName), "Field ReferenceName is absent in the Migration Description"); if (CQStringComparer.FieldName.Equals(fieldName, stateTransitionFieldDefName) || (null != skipFields && skipFields.Contains(fieldName, CQStringComparer.FieldName))) { // skip or "State" field, as it has already been submitted in a separate history/revision continue; } bool setFieldValue = false; OAdFieldInfo aFieldInfo = CQWrapper.GetEntityFieldValue(entity, fieldName); int fieldRequiredness = CQWrapper.GetRequiredness(aFieldInfo); switch (fieldRequiredness) { case CQConstants.MANDATORY: case CQConstants.OPTIONAL: setFieldValue = true; break; case CQConstants.READONLY: // [teyang] TODO conflict handling TraceManager.TraceWarning("Field {0} is READONLY", fieldName); setFieldValue = false; break; case CQConstants.USEHOOK: // [teyang] TODO conflict handling TraceManager.TraceWarning("Field {0} is USEHOOK", fieldName); setFieldValue = false; break; default: throw new InvalidOperationException(); } if (setFieldValue) { int attempt1Count = 0; if (!SetFieldValue(action, entity, fieldName, stringVal, ref attempt1Count)) { return; } AddFieldToUpdateLog(fieldName, stringVal, updateLog); recordIsUpdated = true; } } if (!recordIsUpdated) { // no update has been made to the record, mark this action to be skipped CQWrapper.Revert(entity); if (action.State == ActionState.Pending) { action.State = ActionState.Skipped; } return; } AddLineToUpdateLog(updateLog); int attempt2Count = 0; if (!SetFieldValue(action, entity, NoteEntryFieldName, updateLog.ToString(), ref attempt2Count)) { return; } retVal = CQWrapper.Validate(entity); if (!string.IsNullOrEmpty(retVal)) { IEnumerable <Microsoft.TeamFoundation.Migration.ClearQuestAdapter.CQTextParser.RecordValidationResult> validationResults; if (CQTextParser.RecordValidationTextParser.TryParse(retVal, out validationResults)) { foreach (CQTextParser.RecordValidationResult rslt in validationResults) { MigrationConflict conflict = ClearQuestInvalidFieldValueConflictType.CreateConflict(rslt, action); List <MigrationAction> actions; var resolutionRslt = m_conflictManagerService.TryResolveNewConflict(m_conflictManagerService.SourceId, conflict, out actions); if (!resolutionRslt.Resolved) { action.ChangeGroup.ContainsBackloggedAction = true; return; } } } else { throw new InvalidOperationException(retVal); } } retVal = CQWrapper.Commmit(entity); if (!string.IsNullOrEmpty(retVal)) { // [teyang] TODO: invalid update conflict handling throw new InvalidOperationException(retVal); } if (action.State == ActionState.Pending) { action.State = ActionState.Complete; } // ********* // now comparing to the cache, so that we can clearly identify the item:version pairs // e.g. TargetCQRecordDisplayName : HistoryFieldName::LatestHistoryIndex Dictionary <string, int[]> updatedHistoryIndices = new Dictionary <string, int[]>(); FindUpdatedHistoryIndices(entity, recordHistoryCountCache, updatedHistoryIndices); recordHistoryCountCache.Clear(); foreach (string histFieldName in updatedHistoryIndices.Keys) { foreach (int histIndex in updatedHistoryIndices[histFieldName]) { UpdateConversionHistory(action, recordDisplayName, CQHistoryMigrationItem.CreateHistoryItemVersion(histFieldName, histIndex), convRslt); } } }
public void ExtractLinkChangeActions( Session session, OAdEntity hostRecord, List <LinkChangeGroup> linkChangeGroups) { string hostRecDispName = CQWrapper.GetEntityDisplayName(hostRecord); string hostRecEntityDefName = CQWrapper.GetEntityDefName(hostRecord); if (string.IsNullOrEmpty(hostRecEntityDefName) || !CQStringComparer.EntityName.Equals(hostRecEntityDefName, this.m_hostRecordType)) { return; } string hostRecMigrItemId = UtilityMethods.CreateCQRecordMigrationItemId(hostRecEntityDefName, hostRecDispName); var linkChangeGroup = new LinkChangeGroup(hostRecMigrItemId, LinkChangeGroup.LinkChangeGroupStatus.Created, false); OAdFieldInfo fldInfo = CQWrapper.GetEntityFieldValue(hostRecord, m_referenceFieldName); int cqFieldType = CQWrapper.GetFieldType(fldInfo); if (cqFieldType == CQConstants.FIELD_REFERENCE) { // get the current entity def handle OAdEntityDef curEntityDef = CQWrapper.GetEntityDef(session, hostRecEntityDefName); OAdEntityDef refEntityDef = CQWrapper.GetFieldReferenceEntityDef(curEntityDef, m_referenceFieldName); string childRecEntityDefName = CQWrapper.GetEntityDefName(refEntityDef); int valueStatus = CQWrapper.GetFieldValueStatus(fldInfo); if (valueStatus == (int)CQConstants.FieldStatus.HAS_VALUE) { // single value required string refFldVal = CQWrapper.GetFieldValue(fldInfo); if (!CQStringComparer.RecordName.Equals(refFldVal, hostRecDispName)) { // NOT a reference to self.. Note TFS cannot have a reference to self OAdEntity childRecord = CQWrapper.GetEntity(session, childRecEntityDefName, refFldVal); if (null != childRecord) { string childRecDispName = CQWrapper.GetEntityDisplayName(childRecord); string childRecMigrItemId = UtilityMethods.CreateCQRecordMigrationItemId( childRecEntityDefName, childRecDispName); ILink refFieldLink = new ArtifactLink( hostRecDispName, new Artifact(hostRecMigrItemId, new ClearQuestRecordArtifactType()), new Artifact(childRecMigrItemId, new ClearQuestRecordArtifactType()), string.Empty, this); LinkChangeAction action = new LinkChangeAction( WellKnownChangeActionId.Add, refFieldLink, LinkChangeAction.LinkChangeActionStatus.Created, false); linkChangeGroup.AddChangeAction(action); } else { // [teyang] TODO replace debug assertion with a conflict? Debug.Assert(false, "null == childRecord"); } } } } linkChangeGroups.Add(linkChangeGroup); }
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); } }