/
ImportJobBase.cs
271 lines (231 loc) · 9.37 KB
/
ImportJobBase.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using SC = Sitecore;
using SCData = Sitecore.Data;
using SCItems = Sitecore.Data.Items;
using SCFields = Sitecore.Data.Fields;
using Specialized.Content.Import.Interfaces;
using Specialized.Content.Import.SupportClasses;
using Specialized.Content.Import.Mapping.Xls;
using Specialized.Content.Import.Mapping;
namespace Specialized.Content.Import
{
public class ImportJobBase : IHasImportStatus<StatusMessage>
{
#region Public properties
/// <summary>
/// Returns the name of the CurrentMapping being imported during a job
/// </summary>
public string CurrentMappingName { get; protected set; }
/// <summary>
/// Returns the number of the current item within the CurrentMapping being imported during a job
/// </summary>
public int CurrentItemIndex { get { return this._currentItemIndex; } }
public bool IsAsync { get; set; } = false;
/// <summary>
/// Report whether job is currently running
/// </summary>
public bool IsJobRunning { get { return this.ImportStatus == Enumerations.JobStatus.Running; } }
/// <summary>
/// Return whether the last running job was canceled
/// </summary>
public bool IsJobCanceled { get { return this.ImportStatus == Enumerations.JobStatus.Canceled; } }
/// <summary>
/// Returns current status of job
/// </summary>
public Enumerations.JobStatus ImportStatus { get; set; } = Enumerations.JobStatus.Idle;
public List<StatusMessage> ImportMessages { get; set; } = new List<StatusMessage>();
public Enumerations.ImportJobType ImportType { get; set; }
public BackgroundWorker BgWorker { get { return this._bgWorker; } }
/// <summary>
/// Occurs when progress is updated when job runs asynchronously
/// </summary>
public event ProgressChangedHandler OnProgressChange;
/// <summary>
/// Occurs when job ends when run asynchronously
/// </summary>
public event ProgressEndedHandler OnProgressEnd;
public event DoWorkEventHandler DoWorkEvent;
/// <summary>
/// Returns the percent of rows completed in the CurrentMapping during a job
/// </summary>
public int CurrentPercent
{
get { return this._currentPercent; }
protected set { this._currentPercent = value; }
}
// event handlers for background worker/async operation
public delegate void ProgressChangedHandler(object sender, ImportProgressChangedEventArgs e);
public delegate void ProgressEndedHandler(object sender, ImportProgressEndedEventArgs e);
#endregion
#region Private properties
private readonly BackgroundWorker _bgWorker;
private int _currentPercent = 0;
#endregion
#region Protected properties
protected int _currentItemIndex = -1;
protected SCData.Database _database = null;
protected SCItems.Item _parent = null;
#endregion
#region Public methods
public ImportJobBase()
{
this._bgWorker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
this._bgWorker.ProgressChanged += bgWorkerProgressChanged;
this._bgWorker.RunWorkerCompleted += bgWorkerProgressEnded;
}
/// <summary>
/// Start importing data from ImportMappings using BackgroundWorker
/// </summary>
public void ImportDataAsync()
{
this._bgWorker.DoWork += this.DoWorkEvent;
this._bgWorker.RunWorkerAsync();
}
/// <summary>
/// Start importing data from ImportMappings
/// </summary>
public void ImportData()
{
this.DoWorkEvent(null, null);
}
/// <summary>
/// Allows cancellation of import job
/// </summary>
public void CancelImport()
{
if (this.IsAsync)
{
this._bgWorker.CancelAsync();
}
else
{
this.ImportStatus = Enumerations.JobStatus.Canceled;
}
}
/// <summary>
/// Static constructor to load assembly
/// </summary>
static ImportJobBase()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
String dllName = new AssemblyName(args.Name).Name + ".dll";
var execAss = Assembly.GetExecutingAssembly();
string resName = execAss.GetManifestResourceNames().FirstOrDefault(mrn => mrn.EndsWith(dllName));
if (resName == null)
{
return null;
}
using (var stream = execAss.GetManifestResourceStream(resName))
{
byte[] assData = new byte[stream.Length];
stream.Read(assData, 0, assData.Length);
return Assembly.Load(assData);
}
};
}
#endregion
#region Protected methods
/// <summary>
/// Get Sitecore Item using sitecore DataBase for current instance
/// </summary>
/// <param name="itemID">Item ID as Guide</param>
/// <returns>Sitecore Item</returns>
protected SCItems.Item GetItem(Guid itemID)
{
return this.GetItem(new SCData.ID(itemID));
}
protected SCItems.Item GetItem(SCData.ID itemId)
{
return this._database.GetItem(itemId);
}
protected void SetParent(MappingBase currentMapping)
{
if (currentMapping.ParentIDSource == Enumerations.ParentSource.Global)
{
this._parent = this.GetItem(currentMapping.GlobalParentID);
if (this._parent == null)
{
LogStatus("A valid Item ID must be specified for GlobalParentID when ParentSource is Global.");
return;
}
}
}
protected void InitializeImportBase()
{
this.ImportStatus = Enumerations.JobStatus.Running;
this._database = SC.Configuration.Factory.GetDatabase("master");
this.ImportMessages = new List<StatusMessage>();
if (this._database == null)
{
throw new NullReferenceException("Master DB cannot be accessed. Please make sure the import is running with access to the Master DB.");
}
}
/// <summary>
/// Log an error message to job error list. CurrentItem will be -1 if not due to row or file specific error.
/// </summary>
/// <param name="statusMessage">Status message to log</param>
protected void LogStatus(string statusMessage)
{
LogStatus(statusMessage, this._currentItemIndex.ToString());
}
/// <summary>
/// Log an error message allowing an override of the currentItem to specify a file name when appropriate
/// </summary>
/// <param name="statusMessage">Status message to log</param>
/// <param name="currentItem">The item string to log the message related to</param>
protected void LogStatus(string statusMessage, string currentItem)
{
this.ImportMessages.Add(new StatusMessage()
{
JobType = this.ImportType,
MappingName = this.CurrentMappingName,
CurrentItem = currentItem,
Message = statusMessage
});
}
#endregion
#region Background process for Async operation
/// <summary>
/// Report progress changes to event delegate supplied by OnProgressChange
/// </summary>
/// <param name="sender">Object sender</param>
/// <param name="e">ProgressChangedEventArgs</param>
private void bgWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
this._currentPercent = e.ProgressPercentage;
ImportProgressChangedEventArgs args = new ImportProgressChangedEventArgs(e.ProgressPercentage, (string)e.UserState, this.IsJobCanceled);
this.OnProgressChange(this, args);
}
/// <summary>
/// Report when background job ends to delegate supplied by OnProfressEnd
/// </summary>
/// <param name="sender">Object sender</param>
/// <param name="e">RunWorkerCompletedEventArgs</param>
private void bgWorkerProgressEnded(object sender, RunWorkerCompletedEventArgs e)
{
ImportProgressEndedEventArgs args = (ImportProgressEndedEventArgs)e;
args.PercentCompleted = this._currentPercent;
this.OnProgressEnd(this, args);
}
/// <summary>
/// Determine whether operation should continue for async & non-canceled, or non-async operations
/// </summary>
/// <returns></returns>
protected bool checkContinue()
{
return ((this.IsAsync && !this._bgWorker.CancellationPending) || (!this.IsAsync && !this.IsJobCanceled));
}
#endregion
}
}