|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.TeamFoundation.WorkItemTracking.Client; |
| 7 | +using System.Diagnostics; |
| 8 | +using Microsoft.ApplicationInsights; |
| 9 | +using VSTS.DataBulkEditor.Engine.Configuration.FieldMap; |
| 10 | + |
| 11 | +namespace VSTS.DataBulkEditor.Engine.ComponentContext |
| 12 | +{ |
| 13 | + public class MultiValueConditionalMap : FieldMapBase |
| 14 | + { |
| 15 | + private MultiValueConditionalMapConfig config; |
| 16 | + |
| 17 | + public MultiValueConditionalMap(MultiValueConditionalMapConfig config) |
| 18 | + { |
| 19 | + this.config = config; |
| 20 | + } |
| 21 | + |
| 22 | + internal override void InternalExecute(WorkItem source, WorkItem target) |
| 23 | + { |
| 24 | + if (fieldsExist(config.sourceFieldsAndValues, source) && fieldsExist(config.targetFieldsAndValues, target)) |
| 25 | + { |
| 26 | + if (fieldsValueMatch(config.sourceFieldsAndValues, source)) |
| 27 | + { |
| 28 | + fieldsUpdate(config.targetFieldsAndValues, target); |
| 29 | + } |
| 30 | + Trace.WriteLine(string.Format(" [UPDATE] field mapped {0}:{1} to {2}:{3}", source.Id, config.sourceFieldsAndValues.Keys.ToString(), target.Id, config.targetFieldsAndValues.Keys.ToString())); |
| 31 | + } else |
| 32 | + { |
| 33 | + Trace.WriteLine(string.Format(" [SKIPPED] Not all source and target fields exist", source.Id, config.sourceFieldsAndValues.Keys.ToString(), target.Id, config.targetFieldsAndValues.Keys.ToString())); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private void fieldsUpdate(Dictionary<string, string> fieldAndValues, WorkItem workitem) |
| 38 | + { |
| 39 | + foreach (string field in fieldAndValues.Keys) |
| 40 | + { |
| 41 | + workitem.Fields[field].Value = fieldAndValues[field]; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private bool fieldsValueMatch(Dictionary<string, string> fieldAndValues, WorkItem workitem) |
| 46 | + { |
| 47 | + bool matches = true; |
| 48 | + foreach (string field in fieldAndValues.Keys) |
| 49 | + { |
| 50 | + if((string)workitem.Fields[field].Value != fieldAndValues[field]) |
| 51 | + { |
| 52 | + matches = false; |
| 53 | + } |
| 54 | + } |
| 55 | + return matches; |
| 56 | + } |
| 57 | + |
| 58 | + private bool fieldsExist(Dictionary<string, string> fieldsAndValues, WorkItem workitem) |
| 59 | + { |
| 60 | + bool exists = true; |
| 61 | + foreach (string field in fieldsAndValues.Keys) |
| 62 | + { |
| 63 | + if (!workitem.Fields.Contains(field)) |
| 64 | + { |
| 65 | + exists = false; |
| 66 | + } |
| 67 | + } |
| 68 | + return exists; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + } |
| 73 | +} |
0 commit comments