-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathContentScheduleCollection.cs
301 lines (254 loc) · 10.6 KB
/
ContentScheduleCollection.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
using System.Collections.Specialized;
namespace Umbraco.Cms.Core.Models;
public class ContentScheduleCollection : INotifyCollectionChanged, IDeepCloneable, IEquatable<ContentScheduleCollection>
{
// underlying storage for the collection backed by a sorted list so that the schedule is always in order of date and that duplicate dates per culture are not allowed
private readonly Dictionary<string, SortedList<DateTime, ContentSchedule>> _schedule
= new(StringComparer.InvariantCultureIgnoreCase);
public event NotifyCollectionChangedEventHandler? CollectionChanged;
/// <summary>
/// Returns all schedules registered
/// </summary>
/// <returns></returns>
public IReadOnlyList<ContentSchedule> FullSchedule => _schedule.SelectMany(x => x.Value.Values).ToList();
public object DeepClone()
{
var clone = new ContentScheduleCollection();
foreach (KeyValuePair<string, SortedList<DateTime, ContentSchedule>> cultureSched in _schedule)
{
var list = new SortedList<DateTime, ContentSchedule>();
foreach (KeyValuePair<DateTime, ContentSchedule> schedEntry in cultureSched.Value)
{
list.Add(schedEntry.Key, (ContentSchedule)schedEntry.Value.DeepClone());
}
clone._schedule[cultureSched.Key] = list;
}
return clone;
}
public bool Equals(ContentScheduleCollection? other)
{
if (other == null)
{
return false;
}
Dictionary<string, SortedList<DateTime, ContentSchedule>> thisSched = _schedule;
Dictionary<string, SortedList<DateTime, ContentSchedule>> thatSched = other._schedule;
if (thisSched.Count != thatSched.Count)
{
return false;
}
foreach ((var culture, SortedList<DateTime, ContentSchedule> thisList) in thisSched)
{
// if culture is missing, or actions differ, false
if (!thatSched.TryGetValue(culture, out SortedList<DateTime, ContentSchedule>? thatList) ||
!thatList.SequenceEqual(thisList))
{
return false;
}
}
return true;
}
public static ContentScheduleCollection CreateWithEntry(DateTime? release, DateTime? expire)
{
var schedule = new ContentScheduleCollection();
schedule.Add(string.Empty, release, expire);
return schedule;
}
/// <summary>
/// Clears all <see cref="CollectionChanged" /> event handlers
/// </summary>
public void ClearCollectionChangedEvents() => CollectionChanged = null;
/// <summary>
/// Add an existing schedule
/// </summary>
/// <param name="schedule"></param>
public void Add(ContentSchedule schedule)
{
if (!_schedule.TryGetValue(schedule.Culture, out SortedList<DateTime, ContentSchedule>? changes))
{
changes = new SortedList<DateTime, ContentSchedule>();
_schedule[schedule.Culture] = changes;
}
// TODO: Below will throw if there are duplicate dates added, validate/return bool?
changes.Add(schedule.Date, schedule);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, schedule));
}
private void OnCollectionChanged(NotifyCollectionChangedEventArgs args) => CollectionChanged?.Invoke(this, args);
/// <summary>
/// Adds a new schedule for invariant content
/// </summary>
/// <param name="releaseDate"></param>
/// <param name="expireDate"></param>
public bool Add(DateTime? releaseDate, DateTime? expireDate) => Add(string.Empty, releaseDate, expireDate);
/// <summary>
/// Adds a new schedule for a culture
/// </summary>
/// <param name="culture"></param>
/// <param name="releaseDate"></param>
/// <param name="expireDate"></param>
/// <returns>true if successfully added, false if validation fails</returns>
public bool Add(string? culture, DateTime? releaseDate, DateTime? expireDate)
{
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
if (releaseDate.HasValue && expireDate.HasValue && releaseDate >= expireDate)
{
return false;
}
if (!releaseDate.HasValue && !expireDate.HasValue)
{
return false;
}
// TODO: Do we allow passing in a release or expiry date that is before now?
if (!_schedule.TryGetValue(culture, out SortedList<DateTime, ContentSchedule>? changes))
{
changes = new SortedList<DateTime, ContentSchedule>();
_schedule[culture] = changes;
}
// TODO: Below will throw if there are duplicate dates added, should validate/return bool?
// but the bool won't indicate which date was in error, maybe have 2 diff methods to schedule start/end?
if (releaseDate.HasValue)
{
var entry = new ContentSchedule(culture, releaseDate.Value, ContentScheduleAction.Release);
changes.Add(releaseDate.Value, entry);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, entry));
}
if (expireDate.HasValue)
{
var entry = new ContentSchedule(culture, expireDate.Value, ContentScheduleAction.Expire);
changes.Add(expireDate.Value, entry);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, entry));
}
return true;
}
/// <summary>
/// Remove a scheduled change
/// </summary>
/// <param name="change"></param>
public void Remove(ContentSchedule change)
{
if (_schedule.TryGetValue(change.Culture, out SortedList<DateTime, ContentSchedule>? s))
{
var removed = s.Remove(change.Date);
if (removed)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, change));
if (s.Count == 0)
{
_schedule.Remove(change.Culture);
}
}
}
}
internal void RemoveIfExists(string culture, ContentScheduleAction action)
{
ContentSchedule? changeToRemove = FullSchedule.FirstOrDefault(schedule =>
MatchingScheduleTakingIntoAccountDifferentInvariantNotations(schedule, culture, action));
if (changeToRemove is not null)
{
Remove(changeToRemove);
}
}
public void AddOrUpdate(string culture, DateTime dateTime, ContentScheduleAction action)
{
// we need to remove the old one as ContentSchedule.Date is immutable
ContentSchedule? changeToRemove = FullSchedule.FirstOrDefault(schedule =>
MatchingScheduleTakingIntoAccountDifferentInvariantNotations(schedule, culture, action));
if (changeToRemove is not null)
{
Remove(changeToRemove);
}
Add(new ContentSchedule(culture, dateTime, action));
}
private bool MatchingScheduleTakingIntoAccountDifferentInvariantNotations(ContentSchedule change, string culture,
ContentScheduleAction action)
{
if (change.Action != action)
{
return false;
}
if (culture is "" or "*")
{
return change.Culture is "" or "*";
}
return change.Culture == culture;
}
/// <summary>
/// Clear all of the scheduled change type for invariant content
/// </summary>
/// <param name="action"></param>
/// <param name="changeDate">If specified, will clear all entries with dates less than or equal to the value</param>
public void Clear(ContentScheduleAction action, DateTime? changeDate = null) =>
Clear(string.Empty, action, changeDate);
/// <summary>
/// Clear all of the scheduled change type for the culture
/// </summary>
/// <param name="culture"></param>
/// <param name="action"></param>
/// <param name="date">If specified, will clear all entries with dates less than or equal to the value</param>
public void Clear(string? culture, ContentScheduleAction action, DateTime? date = null)
{
if (culture is null || !_schedule.TryGetValue(culture, out SortedList<DateTime, ContentSchedule>? schedules))
{
return;
}
var removes = schedules.Where(x => x.Value.Action == action && (!date.HasValue || x.Value.Date <= date.Value))
.ToList();
foreach (KeyValuePair<DateTime, ContentSchedule> remove in removes)
{
var removed = schedules.Remove(remove.Value.Date);
if (!removed)
{
continue;
}
OnCollectionChanged(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, remove.Value));
}
if (schedules.Count == 0)
{
_schedule.Remove(culture);
}
}
/// <summary>
/// Returns all pending schedules based on the date and type provided
/// </summary>
/// <param name="action"></param>
/// <param name="date"></param>
/// <returns></returns>
public IReadOnlyList<ContentSchedule> GetPending(ContentScheduleAction action, DateTime date) =>
_schedule.Values.SelectMany(x => x.Values).Where(x => x.Date <= date).ToList();
/// <summary>
/// Gets the schedule for invariant content
/// </summary>
/// <returns></returns>
public IEnumerable<ContentSchedule> GetSchedule(ContentScheduleAction? action = null) =>
GetSchedule(string.Empty, action);
/// <summary>
/// Gets the schedule for a culture
/// </summary>
/// <param name="culture"></param>
/// <param name="action"></param>
/// <returns></returns>
public IEnumerable<ContentSchedule> GetSchedule(string? culture, ContentScheduleAction? action = null)
{
if (culture is not null && _schedule.TryGetValue(culture, out SortedList<DateTime, ContentSchedule>? changes))
{
return action == null ? changes.Values : changes.Values.Where(x => x.Action == action.Value);
}
return Enumerable.Empty<ContentSchedule>();
}
public override bool Equals(object? obj)
=> obj is ContentScheduleCollection other && Equals(other);
public static ContentScheduleCollection CreateWithEntry(string culture, DateTime? release, DateTime? expire)
{
var schedule = new ContentScheduleCollection();
schedule.Add(culture, release, expire);
return schedule;
}
public override int GetHashCode()
{
throw new NotImplementedException();
}
}