/// <summary> /// Check the field name in already used field names accross different WIT's /// and make sure there are no field name and type collisions. /// </summary> /// <param name="witName">WIT Name</param> /// <param name="witdFldDef">Field Definition handle</param> /// <param name="usedFiledNameTypes">List of already used field names/types across WIT's</param> public static void CheckFieldTypeCollision(string witName, ref Common.FieldDefinition witFldDef, Dictionary <string, Common.FieldDefinition> usedFiledNameDefs) { if (usedFiledNameDefs.ContainsKey(witFldDef.name)) { // Field name is already used across WIT's, we should repeat it with exact casing. witFldDef.name = usedFiledNameDefs[witFldDef.name].name; string witFieldType = witFldDef.type.ToString(); string usedType = usedFiledNameDefs[witFldDef.name].type.ToString(); if (!TFStringComparer.FieldType.Equals(witFieldType, usedType)) { // The type is different from what already used in some WIT. // Qualify field name by prefixing WIT name. witFldDef.name = witName + "_" + witFldDef.name; CheckFieldTypeCollision(witName, ref witFldDef, usedFiledNameDefs); } } }
/// <summary> /// Check the field name in Store.FieldCollection and come out with a unique field name /// and the corresponding unique refname /// </summary> /// <param name="conn">VSTS connection handle</param> /// <param name="witdFldDef">Field Definition handle</param> /// <param name="usedFieldNames">list of already used field names</param> public static void ValidateFieldNames(VSTSConnection conn, ref Common.FieldDefinition witdFldDef, Hashtable usedFieldNames, Hashtable usedFieldRefNames) { Debug.Assert(conn != null); Debug.Assert(witdFldDef != null); WorkItemStore store = conn.store; FieldDefinitionCollection fldDefCollection = store.FieldDefinitions; // if this field is already used in current schema, append numbers to it monotonically increasing if (usedFieldNames[witdFldDef.name] != null) { int i = 1; while (usedFieldNames[witdFldDef.name + i.ToString(CultureInfo.InvariantCulture)] != null) { i++; } // got a field which is not there in existing list witdFldDef.name = witdFldDef.name + i.ToString(CultureInfo.InvariantCulture); } if (fldDefCollection.Contains(witdFldDef.name)) { FieldDefinition currituckFldDef = fldDefCollection[witdFldDef.name]; string witdtype = witdFldDef.type.ToString(); string vstsFldType = currituckFldDef.FieldType.ToString(); if (String.Equals(witdtype, vstsFldType, StringComparison.OrdinalIgnoreCase)) { // reuse the same refname from the existing field in store witdFldDef.refname = currituckFldDef.ReferenceName; } else { // generate new field based on using field name and type.. witdFldDef.name = witdFldDef.name + " " + witdtype; // recheck if this new field exists in the currituck ValidateFieldNames(conn, ref witdFldDef, usedFieldNames, usedFieldRefNames); } } else { // this field does not exist in curritcuk.. // check if this is one of the core internal field not supposed to be used by the customer /* * fix for bug#11206 * there is additional constraint that some of the core fields names are internal * and cannot be used by the user... for temporary solution listing out all those * core fields.. shall go out once there is some better solution from currituck */ if (!VSTSConstants.TfsInternalFields.ContainsKey(witdFldDef.name)) { // can use this safely with our own type and refname witdFldDef.refname = ReferenceNamePrefix + witdFldDef.name; // remove unwanted characters from refname // if the refname is taken from Currituck, there would not be any unwanted characters to be replaced witdFldDef.refname = FieldRefNameRegEx.Replace(witdFldDef.refname, "_"); // see if this refname is already used with some other field in tfs // or in the current schema, append numbers to it monotonically increasing if (fldDefCollection.Contains(witdFldDef.refname) || usedFieldRefNames.ContainsKey(witdFldDef.refname)) { string newFldRefName = String.Empty; // generate a unique ref name int suffix = 1; do { newFldRefName = String.Concat(witdFldDef.refname, suffix++); }while (fldDefCollection.Contains(newFldRefName) || usedFieldRefNames.ContainsKey(newFldRefName)); witdFldDef.refname = newFldRefName; } } else { /* * If the field name happen to be one of the intenal core field name, treat as * that is a existing field with different type and generate new field name */ // generate new field based using field name and type.. witdFldDef.name = witdFldDef.name + " " + witdFldDef.type.ToString(); // recheck if this new field exists in the currituck ValidateFieldNames(conn, ref witdFldDef, usedFieldNames, usedFieldRefNames); } } }