-
Notifications
You must be signed in to change notification settings - Fork 3
/
WorkItem.cs
278 lines (222 loc) · 8.46 KB
/
WorkItem.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace WhiskWork.Core
{
public class WorkItemProperties : IEnumerable<KeyValuePair<string,string>>
{
private readonly NameValueCollection _properties;
public WorkItemProperties(NameValueCollection properties)
{
_properties = properties;
}
public int Count
{
get { return _properties.Count; }
}
public IEnumerable<string> AllKeys
{
get { return _properties.AllKeys; }
}
public string this[string key]
{
get
{
return _properties[key];
}
}
public IEnumerator<KeyValuePair<string,string>> GetEnumerator()
{
foreach (var key in _properties.AllKeys)
{
yield return new KeyValuePair<string, string>(key,_properties[key]);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class WorkItem
{
private readonly NameValueCollection _properties;
private readonly int? _ordinal;
private WorkItem(string id, string path, IEnumerable<string> workItemClasses, WorkItemStatus status, string parentId, int? ordinal, NameValueCollection properties)
{
Id = id;
Path = path;
Classes = workItemClasses;
Status = status;
ParentId = parentId;
_ordinal = ordinal;
_properties = properties;
}
public static WorkItem New(string id, string path)
{
return New(id, path, new NameValueCollection());
}
public static WorkItem New(string id, string path, NameValueCollection properties)
{
if (!IsValidId(id))
{
throw new ArgumentException("Id can only consist of letters, numbers and hyphen");
}
return new WorkItem(id, path, new string[0], WorkItemStatus.Normal, null,null, properties);
}
public static WorkItem NewUnchecked(string id, string path, int? ordinal, NameValueCollection properties)
{
return new WorkItem(id, path, new string[0], WorkItemStatus.Normal, null, ordinal, properties);
}
public string Id { get; private set; }
public string Path { get; private set; }
public IEnumerable<string> Classes { get; private set; }
public WorkItemStatus Status { get; private set; }
public string ParentId { get; private set; }
public int Ordinal
{
get
{
return _ordinal.HasValue ? _ordinal.Value : -1;
}
}
public bool HasOrdinal
{
get
{
return _ordinal.HasValue;
}
}
public WorkItemProperties Properties
{
get
{
return new WorkItemProperties(_properties);
}
}
public WorkItem MoveTo(WorkStep step)
{
return new WorkItem(Id,step.Path,Classes,Status,ParentId, _ordinal,_properties);
}
public WorkItem UpdateStatus(WorkItemStatus status)
{
return new WorkItem(Id, Path, Classes, status, ParentId, _ordinal, _properties);
}
public WorkItem CreateChildItem(string id)
{
return new WorkItem(id, Path, Classes, Status, Id, _ordinal, _properties);
}
public WorkItem UpdateParent(WorkItem parentItem)
{
return new WorkItem(Id, Path, Classes, Status, parentItem.Id, _ordinal, _properties);
}
public WorkItem UpdateOrdinal(int ordinal)
{
return new WorkItem(Id, Path, Classes, Status, ParentId, ordinal, _properties);
}
public WorkItem AddClass(string workItemClass)
{
var newClasses = new List<string>(Classes) { workItemClass };
return new WorkItem(Id, Path, newClasses, Status, ParentId, _ordinal, _properties);
}
public WorkItem RemoveClass(string workItemClass)
{
var newClasses = new List<string>(Classes);
newClasses.Remove(workItemClass);
return new WorkItem(Id, Path, newClasses, Status, ParentId, _ordinal, _properties);
}
public WorkItem ReplacesClasses(IEnumerable<string> newClasses)
{
return new WorkItem(Id, Path, newClasses, Status, ParentId, _ordinal, _properties);
}
public WorkItem UpdatePropertiesAndOrdinalFrom(WorkItem item)
{
var modifiedOrdinal = _ordinal;
var modifiedProperties = GetModifiedProperties(item.Properties);
if(item.HasOrdinal)
{
modifiedOrdinal = item.Ordinal;
}
return new WorkItem(Id, Path, Classes, Status, ParentId, modifiedOrdinal, modifiedProperties);
}
public WorkItem UpdateProperties(WorkItemProperties properties)
{
var modifiedProperties = GetModifiedProperties(properties);
return new WorkItem(Id, Path, Classes, Status, ParentId, _ordinal, modifiedProperties);
}
private NameValueCollection GetModifiedProperties(WorkItemProperties propertyUpdate)
{
var modifiedProperties = new NameValueCollection(_properties);
foreach (var key in propertyUpdate.AllKeys)
{
var newValue = propertyUpdate[key];
if (string.IsNullOrEmpty(newValue))
{
modifiedProperties.Remove(key);
}
else
{
modifiedProperties[key] = newValue;
}
}
return modifiedProperties;
}
public WorkItem UpdateProperties(NameValueCollection properties)
{
var modifiedProperties = new NameValueCollection(_properties);
foreach (var key in properties.AllKeys)
{
modifiedProperties[key] = properties[key];
}
return new WorkItem(Id, Path, Classes, Status, ParentId, _ordinal, modifiedProperties);
}
public override bool Equals(object obj)
{
if(!(obj is WorkItem))
{
return false;
}
var item = (WorkItem) obj;
var result = true;
result &= Id == item.Id;
result &= Path == item.Path;
result &= ParentId == item.ParentId;
result &= Status == item.Status;
result &= Ordinal == item.Ordinal;
result &= Classes.SequenceEqual(item.Classes);
result &= Properties.SequenceEqual(item.Properties);
return result;
}
public override int GetHashCode()
{
var hc = Id!=null ? Id.GetHashCode() : 1;
hc ^= Path!=null ? Path.GetHashCode() : 2;
hc ^= ParentId!=null ? ParentId.GetHashCode(): 4;
hc ^= Status.GetHashCode();
hc ^= Ordinal.GetHashCode();
hc ^= Classes.Count()>0 ? Classes.Select(s => s.GetHashCode()).Aggregate((hash, next) => hash ^ next) : 8;
hc ^= Properties.Count>0 ? Properties.Select(kv => kv.Key.GetHashCode() ^ kv.Value.GetHashCode()).Aggregate((hash, next) => hash ^ next) : 16;
return hc;
}
public override string ToString()
{
var sb = new StringBuilder();
sb.AppendFormat("Id={0},", Id);
sb.AppendFormat("Path={0},", Path);
sb.AppendFormat("ParentId={0},", ParentId);
sb.AppendFormat("Status={0},", Status);
sb.AppendFormat("Ordinal={0},", Ordinal);
sb.AppendFormat("Classes={0},", Classes.Count()>0 ? Classes.Aggregate((current, next) => current + "&" + next): string.Empty);
sb.AppendFormat("Properties={0}", Properties.Count()>0 ? Properties.Select(kv => kv.Key+":"+kv.Value).Aggregate((current, next) => current + "&" + next) : string.Empty);
return sb.ToString();
}
private static bool IsValidId(string workItemId)
{
var regex = new Regex("^[\\-,a-z,A-Z,0-9]*$");
return regex.IsMatch(workItemId);
}
}
}