/
XpathBuilder.cs
382 lines (320 loc) · 14.1 KB
/
XpathBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Text;
using System.Windows.Forms;
// TFS dependencies
namespace EventSubscriptionTool
{
public partial class XPathBuilderForm : Form
{
public string QueryString { get; set; }
public XPathBuilderForm()
{
QueryString = "";
this.Font = SystemInformation.MenuFont;
InitializeComponent();
//Shared.PopulateServer(serverListComboBox);
//eventComboBox.SelectedIndex = 13;
// Clear the grid view
fieldValueDataGridView.Rows.Clear();
//Object[] projectRow = { PortfolioProject, NewValue, "=", projectsComboBox.Text };
//fieldValueDataGridView.Rows.Add(projectRow);
}
#region "Generate Expression"
enum Val
{
New = 0,
Old = 1,
NewOrOld = 2
}
private StringBuilder xPathExpression;
private const string StringFieldPath = "\"CoreFields/StringFields/Field[ReferenceName='";
private const string IntegerFieldPath = "\"CoreFields/IntegerFields/Field[ReferenceName='";
private const string NewValuePath = "']/NewValue\"";
private const string OldValuePath = "']/OldValue\"";
private const string OperatorAnd = " AND ";
private const string OperatorOr = " OR ";
private const string NewValue = "New Value";
private const string OldValue = "Old Value";
private const string NewOrOldValue = "New or Old Value";
private const char ValueSeparator = ',';
// Special fields
private const string SystemIdField = "System.Id";
private const string PortfolioProject = "PortfolioProject";
// Special operators
//private const string OperatorContains = "contains";
// Special functions
//private const string FunctionContains = "contains";
private void GenerateExpressionButton_Click(object sender, EventArgs e)
{
Generate();
}
private void Generate()
{
xPathExpression = new StringBuilder();
int rowCount = fieldValueDataGridView.RowCount;
// Check if the last row is empty if it is don't use it
if (fieldValueDataGridView.Rows[rowCount - 1].Cells[0].Value == null ||
fieldValueDataGridView.Rows[rowCount - 1].Cells[1].Value == null ||
fieldValueDataGridView.Rows[rowCount - 1].Cells[2].Value == null ||
fieldValueDataGridView.Rows[rowCount - 1].Cells[3].Value == null)
{
rowCount--;
}
for (int i = 0; i < rowCount; i++)
{
// Make sure data was entered into each column for this row
if (fieldValueDataGridView.Rows[i].Cells[0].Value == null ||
fieldValueDataGridView.Rows[i].Cells[1].Value == null ||
fieldValueDataGridView.Rows[i].Cells[2].Value == null ||
fieldValueDataGridView.Rows[i].Cells[3].Value == null)
{
//MessageBox.Show("Some columns are empty, please fix and try again.");
return;
}
if (String.IsNullOrEmpty(fieldValueDataGridView.Rows[i].Cells[0].Value.ToString().Trim()) ||
String.IsNullOrEmpty(fieldValueDataGridView.Rows[i].Cells[1].Value.ToString().Trim()) ||
String.IsNullOrEmpty(fieldValueDataGridView.Rows[i].Cells[2].Value.ToString().Trim()))
{
// MessageBox.Show("Some columns are empty, please fix and try again.");
return;
}
// Determine if this field is a string field or integer field
// Currently the only system field includes is System.Id that is an integer
bool stringField = true;
if (string.Compare(fieldValueDataGridView.Rows[i].Cells[0].Value.ToString(), SystemIdField) == 0)
{
stringField = false;
}
// Determine if we are testing for "NewValue", "OldValue" or both
// Default to new value
Val newOldvalue = Val.New;
// If it's a old value use "OldValue"
if (string.Compare(fieldValueDataGridView.Rows[i].Cells[1].Value.ToString(), OldValue) == 0)
{
newOldvalue = Val.Old;
}
// If it's a new or old value then construct an "OR" clause
else if (string.Compare(fieldValueDataGridView.Rows[i].Cells[1].Value.ToString(), NewOrOldValue) == 0)
{
newOldvalue = Val.NewOrOld;
}
// Build the expression
string field = fieldValueDataGridView.Rows[i].Cells[0].Value.ToString();
string expressionOperator = fieldValueDataGridView.Rows[i].Cells[2].Value.ToString();
string value = fieldValueDataGridView.Rows[i].Cells[3].Value.ToString();
// Look for the separator in the value, this is how we will do an "OR" on the field
string[] values = value.Split(ValueSeparator);
for (int j = 0; j < values.Length; j++)
{
// If it's the first value and there are greater than 1 values then add an opening bracket
if (j == 0 && values.Length > 1)
{
xPathExpression.Append("(");
}
/*
// If it's a special string expression generate it
// If this is a special string operator
if (expressionOperator.CompareTo(OperatorContains) == 0)
{
if (!stringField)
{
MessageBox.Show("String functions can only be used on string fields.");
return;
}
xPathExpression.Append(GenerateSpecialStringExpression(field, values[j], FunctionContains, newOldvalue));
}*/
xPathExpression.Append(GenerateExpression(field, values[j], expressionOperator, newOldvalue, stringField));
// If there are more than one value add an or between clauses except the last one
if (values.Length > 1)
{
// If it's the last value add a closing bracket
if (j == values.Length - 1)
{
xPathExpression.Append(")");
}
else
{
// Otherwise add an "OR" to separate the expressions
xPathExpression.Append(OperatorOr);
}
}
}
// If it's not the last expression item then add an "AND"
if (i < rowCount - 1)
{
xPathExpression.Append(OperatorAnd);
}
}
textBoxExpression.Text = xPathExpression.ToString();
}
string GenerateExpression(
string field, // Field reference name
string value, // Value of field
string expressionOperator, // Operator to apply to value
Val newOldvalue, // Are we looking for new/old value or both
bool stringField) // String field or Integer field
{
StringBuilder expression = new StringBuilder();
// If this field is the portfolio project we ignore the new/old value and just compose the
// expression "PortfolioProject" = 'project'
if (field.CompareTo(PortfolioProject) == 0)
{
return "\"" + PortfolioProject + "\" " + expressionOperator + " '" + value + "'";
}
// If we are testing for both new and old value need to combine in brackets
if (newOldvalue == Val.NewOrOld)
{
expression.Append("(");
}
// If it's a string field use the string field path otherwise integer field path
if (stringField)
{
expression.Append(StringFieldPath);
}
else
{
expression.Append(IntegerFieldPath);
}
expression.Append(field);
// If we are testing for both new and old value or New Value the first clause will be New value
if (newOldvalue == Val.NewOrOld || newOldvalue == Val.New)
{
expression.Append(NewValuePath);
}
else
{
expression.Append(OldValuePath);
}
expression.Append(" ");
expression.Append(expressionOperator);
expression.Append(" ");
// If it's string field put single quotes around value
if (stringField)
{
expression.Append("'");
}
expression.Append(value);
// If it's string field put single quotes around value
if (stringField)
{
expression.Append("'");
}
// If we are testing for both new and old then add the "OR" section
// If we are testing for both new and old value need to combine in brackets
if (newOldvalue == Val.NewOrOld)
{
expression.Append(OperatorOr);
// If it's a string field use the string field path otherwise integer field path
if (stringField)
{
expression.Append(StringFieldPath);
}
else
{
expression.Append(IntegerFieldPath);
}
expression.Append(field);
expression.Append(OldValuePath);
expression.Append(" ");
expression.Append(expressionOperator);
expression.Append(" ");
// If it's string field put single quotes around value
if (stringField)
{
expression.Append("'");
}
expression.Append(value);
if (stringField)
{
expression.Append("'");
}
expression.Append(")");
}
return expression.ToString();
}
/*
string GenerateSpecialStringExpression(
string field, // Field reference name
string value, // Value of field
string function, // Operator to apply to value
NewOldValue newOldvalue) // Are we looking for new/old value or both
{
StringBuilder expression = new StringBuilder();
// If this field is the portfolio project we ignore the new/old value and just compose the
// expression "PortfolioProject" = 'project'
if (field.CompareTo(PortfolioProject) == 0)
{
expression.Append(function);
expression.Append("(\"");
expression.Append(PortfolioProject);
expression.Append("\", '");
expression.Append(value);
expression.Append("')");
return expression.ToString();
}
// If we are testing for both new and old value need to combine in brackets
if (newOldvalue == NewOldValue.NewOrOldValue)
{
expression.Append("(");
}
// If this is a special string operator
expression.Append(function);
expression.Append("(");
expression.Append(StringFieldPath);
expression.Append(field);
// If we are testing for both new and old value or New Value the first clause will be New value
if (newOldvalue == NewOldValue.NewOrOldValue || newOldvalue == NewOldValue.NewValue)
{
expression.Append(NewValuePath);
}
else
{
expression.Append(OldValuePath);
}
expression.Append(", '");
expression.Append(value);
expression.Append("')");
// If we are testing for both new and old then add the "OR" section
// If we are testing for both new and old value need to combine in brackets
if (newOldvalue == NewOldValue.NewOrOldValue)
{
expression.Append(OperatorOr);
expression.Append(function);
expression.Append("(");
expression.Append(StringFieldPath);
expression.Append(field);
expression.Append(OldValuePath);
expression.Append(", '");
expression.Append(value);
expression.Append("'))");
}
return expression.ToString();
}*/
#endregion
private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void fieldValueDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
Generate();
}
private void fieldValueDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
Generate();
}
private void fieldValueDataGridView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
Generate();
}
private void fieldValueDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
Generate();
}
private void UseExpressionButton_Click(object sender, EventArgs e)
{
QueryString = textBoxExpression.Text;
this.Close();
}
}
}