-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomEventWithUserResources.cs
291 lines (279 loc) · 10.5 KB
/
CustomEventWithUserResources.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
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Filtering;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.SystemModule.Notifications;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Base.General;
using DevExpress.Persistent.Base.General.Compatibility;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.Validation;
using DevExpress.XtraScheduler.Xml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
namespace CustomEventsAndResources.Module.BusinessObjects;
[DefaultClassOptions]
public class CustomEventWithUserResources : BaseObject, IEvent, IXafEntityObject, IRecurrentEvent, IReminderEvent
{
#region Base Properties
[Browsable(false)]
public Object AppointmentId => ID;
[Browsable(false)]
public virtual Int32 Type { get; set; } // event type - Normal, Recurrence Pattern or Recurrence Exception
public virtual String Subject { get; set; }
public virtual String Description { get; set; }
[ImmediatePostData] // needed for immediate update of StartOn/EndOn properties
public virtual Boolean AllDay { get; set; }
public virtual String Location { get; set; }
public virtual Int32 Label { get; set; }
public virtual Int32 Status { get; set; }
public virtual Nullable<DateTime> StartOn { get; set; }
DateTime IEvent.StartOn
{
get { return StartOn.HasValue ? StartOn.Value : DateTime.MinValue; }
set { StartOn = value; }
}
public virtual Nullable<DateTime> EndOn { get; set; }
DateTime IEvent.EndOn
{
get { return EndOn.HasValue ? EndOn.Value : DateTime.MinValue; }
set { EndOn = value; }
}
#endregion
#region Resources
private Boolean isUpdateResourcesDelayed;
private String resourceId;
public override void OnSaving() {
if ((ObjectSpace != null) && isUpdateResourcesDelayed) {
isUpdateResourcesDelayed = false;
UpdateResources();
}
}
private void UpdateResources() {
while (Resources.Count > 0) {
Resources.RemoveAt(Resources.Count - 1);
}
if (!String.IsNullOrEmpty(resourceId)) {
XmlDocument xmlDocument = DevExpress.Utils.SafeXml.CreateDocument(resourceId);
foreach (XmlNode xmlNode in xmlDocument.DocumentElement.ChildNodes) {
AppointmentResourceIdXmlLoader loader = new AppointmentResourceIdXmlLoader(xmlNode);
Object keyMemberValue = loader.ObjectFromXml();
Object obj = ObjectSpace.GetObjectByKey(typeof(ApplicationUser), keyMemberValue);
if (obj != null) {
Resources.Add((ApplicationUser)obj);
}
}
}
}
public void UpdateResourceIds() {
resourceId = "<ResourceIds>\r\n";
foreach (ApplicationUser resource in Resources) {
resourceId += String.Format("<ResourceId Type=\"{0}\" Value=\"{1}\" />\r\n", resource.ID.GetType().FullName, resource.ID);
}
resourceId += "</ResourceIds>";
}
public virtual IList<ApplicationUser> Resources { get; set; } = new ObservableCollection<ApplicationUser>();
[NotMapped, Browsable(false)]
public virtual String ResourceId {
get {
if (resourceId == null) {
UpdateResourceIds();
}
return resourceId;
}
set {
if (resourceId != value) {
resourceId = value;
if (ObjectSpace != null) {
UpdateResources();
} else {
isUpdateResourcesDelayed = true;
}
}
}
}
#endregion
#region IRecurrentEvent
[Browsable(false)]
public virtual CustomEventWithUserResources RecurrencePattern { get; set; }
[Browsable(false)]
public virtual IList<CustomEventWithUserResources> RecurrenceEvents { get; set; } = new ObservableCollection<CustomEventWithUserResources>();
[StringLength(300)]
[NonCloneable, DisplayName("Recurrence"), FieldSize(FieldSizeAttribute.Unlimited)]
public virtual String RecurrenceInfoXml { get; set; }
IRecurrentEvent IRecurrentEvent.RecurrencePattern {
get { return RecurrencePattern; }
set { RecurrencePattern = (CustomEventWithUserResources)value; }
}
#endregion
#region IReminderEvent
private const int NoneReminder = -1;
private IList<PostponeTime> postponeTimes;
private string reminderInfoXml;
private int remindInSeconds = NoneReminder;
private DateTime? alarmTime;
private void UpdateRemindersInfoXml(bool UpdateAlarmTime) {
if (RemindIn.HasValue && AlarmTime.HasValue) {
AppointmentReminderInfo apptReminder = new AppointmentReminderInfo();
ReminderInfo reminderInfo = new ReminderInfo();
reminderInfo.TimeBeforeStart = RemindIn.Value;
DateTime notNullableStartOn = StartOn.HasValue ? StartOn.Value : DateTime.MinValue;
if (UpdateAlarmTime) {
reminderInfo.AlertTime = AlarmTime.Value;
} else {
reminderInfo.AlertTime = notNullableStartOn - RemindIn.Value;
}
apptReminder.ReminderInfos.Add(reminderInfo);
reminderInfoXml = apptReminder.ToXml();
} else {
reminderInfoXml = null;
}
}
private void UpdateAlarmTime() {
if (!string.IsNullOrEmpty(reminderInfoXml)) {
AppointmentReminderInfo appointmentReminderInfo = new AppointmentReminderInfo();
try {
appointmentReminderInfo.FromXml(reminderInfoXml);
alarmTime = appointmentReminderInfo.ReminderInfos[0].AlertTime;
} catch (XmlException e) {
Tracing.Tracer.LogError(e);
}
} else {
alarmTime = null;
remindInSeconds = NoneReminder;
IsPostponed = false;
}
}
private IList<PostponeTime> CreatePostponeTimes() {
IList<PostponeTime> result = PostponeTime.CreateDefaultPostponeTimesList();
result.Add(new PostponeTime("None", null, "None"));
result.Add(new PostponeTime("AtStartTime", TimeSpan.Zero, "0 minutes"));
CustomizeNotificationsPostponeTimeListEventArgs args = new CustomizeNotificationsPostponeTimeListEventArgs(result);
if (CustomizeReminderTimeLookup != null) {
CustomizeReminderTimeLookup(this, args);
}
PostponeTime.SortPostponeTimesList(args.PostponeTimesList);
return args.PostponeTimesList;
}
[ImmediatePostData]
[NotMapped]
[ModelDefault("AllowClear", "False")]
[DataSourceProperty(nameof(PostponeTimeList))]
[SearchMemberOptions(SearchMemberMode.Exclude)]
[VisibleInDetailView(false), VisibleInListView(false)]
public virtual PostponeTime ReminderTime {
get {
if (RemindIn.HasValue) {
return PostponeTimeList.Where(x => (x.RemindIn != null && x.RemindIn.Value == RemindIn.Value)).FirstOrDefault();
} else {
return PostponeTimeList.Where(x => x.RemindIn == null).FirstOrDefault();
}
}
set {
if (value != null) {
if (value.RemindIn.HasValue) {
RemindIn = value.RemindIn.Value;
} else {
RemindIn = null;
}
}
}
}
[Browsable(false), NotMapped]
public IEnumerable<PostponeTime> PostponeTimeList {
get {
if (postponeTimes == null) {
postponeTimes = CreatePostponeTimes();
}
return postponeTimes;
}
}
[NonCloneable]
[Browsable(false)]
[StringLength(200)]
public virtual string ReminderInfoXml {
get { return reminderInfoXml; }
set {
if (reminderInfoXml != value) {
reminderInfoXml = value;
if (ObjectSpace != null) {
UpdateAlarmTime();
}
}
}
}
[Browsable(false)]
[NotMapped]
public TimeSpan? RemindIn {
get {
if (remindInSeconds < 0) {
return null;
}
return TimeSpan.FromSeconds(remindInSeconds);
}
set {
remindInSeconds = value.HasValue ? (int)value.Value.TotalSeconds : NoneReminder;
}
}
[Browsable(false)]
public virtual int RemindInSeconds { get; set; }
[VisibleInDetailView(false), VisibleInListView(false), VisibleInLookupListView(false)]
public string NotificationMessage {
get { return Subject; }
}
[Browsable(false)]
public object UniqueId {
get { return ID; }
}
[VisibleInDetailView(false), VisibleInListView(false), VisibleInLookupListView(false)]
public virtual DateTime? AlarmTime {
get { return alarmTime; }
set {
if (alarmTime != value) {
alarmTime = value;
if (ObjectSpace != null) {
if (value == null) {
remindInSeconds = NoneReminder;
IsPostponed = false;
}
UpdateRemindersInfoXml(true);
}
}
}
}
[VisibleInDetailView(false), VisibleInListView(false), VisibleInLookupListView(false)]
public virtual bool IsPostponed { get; set; }
public event EventHandler<CustomizeNotificationsPostponeTimeListEventArgs> CustomizeReminderTimeLookup;
#endregion
#region Blazor compatibility
[NotMapped, Browsable(false)]
public string RecurrenceInfoXmlBlazor {
get { return RecurrenceInfoXml?.ToNewRecurrenceInfoXml(); }
set { RecurrenceInfoXml = value?.ToOldRecurrenceInfoXml(); }
}
#endregion
#region Construction
public override void OnCreated() {
base.OnCreated();
var now = DateTime.Now;
StartOn = now;
EndOn = now.AddHours(1);
}
#endregion
#region Validation
[Browsable(false)]
[RuleFromBoolProperty("CustomEventWithUserResourcesIntervalValid", DefaultContexts.Save, "The start date must be less than the end date", SkipNullOrEmptyValues = false, UsedProperties = "StartOn, EndOn")]
public Boolean IsIntervalValid {
get { return StartOn <= EndOn; }
}
#endregion
}