/
AuditViewModel.cs
639 lines (552 loc) · 26.9 KB
/
AuditViewModel.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.Linq;
using System.Text.RegularExpressions;
using Ernest.OptimumRCMAudit.Entities;
using Ernest.OptimumRCMAudit.Main.Components;
namespace Ernest.OptimumRCMAudit.Main.Models
{
[XmlRoot("item")]
public class AuditExceptionTypeModel
{
//values must match Name col in AuditExceptionType db table
static public string InUploadButInvalid = "E4";
static public string InUploadButNotInMaster = "E1";
static public string InMasterButNotInUpload = "E2";
static public string InBothWithTemplateDifferences = "E3A";
static public string InBothWithFacilityDifferences = "E3B";
static public string InUploadButHandleManually = "E5";
}
[XmlRoot("item")]
public class AuditMasterItemModel
{
public AuditMasterItemModel()
{
ProcessedYN = false;
}
public int AuditMasterItemId { get; set; }
public string Login { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Facilities { get; set; }
public string Templates { get; set; }
public bool ProcessedYN { get; set; }
}
[XmlRoot("item")]
public class AuditExceptionModel
{
public int AuditExceptionId { get; set; }
public int AuditExceptionTypeId { get; set; }
public int DisplayOrder { get; set; }
public string AuditExceptionType { get; set; }
public string Message { get; set; }
public string AuditExceptionTypeName { get; set; }
public string UserNameFromOptimum { get; set; }
public string UserNameFromMaster { get; set; }
public string FacilitiesFromOptimum { get; set; }
public string FacilitiesFromMaster { get; set; }
public string TemplatesFromOptimum { get; set; }
public string TemplatesFromMaster { get; set; }
}
[XmlRoot("item")]
public class AuditViewModel
{
[XmlIgnore]
private IUnitOfWork _unitOfWork;
[XmlIgnore]
public IUnitOfWork TheUnitOfWork
{
get { return (_unitOfWork); }
set
{
_unitOfWork = value;
}
}
public AuditViewModel()
{
}
public AuditViewModel(string auditUserId)
{
InternalUserId = auditUserId; // _unitOfWork.AspNetUsers.GetAll().Where(u => u.UserName == controller.User.Identity.Name).FirstOrDefault().Id;
}
public AuditViewModel(IUnitOfWork unitOfWork, string auditUserId)
{
_unitOfWork = unitOfWork;
InternalUserId = auditUserId;
}
public string InternalUserId { get; set; }
public int AuditId { get; set; }
[Required(ErrorMessage = "The upload is required")]
[Display(Name = "Upload: ")]
public int ExtractId { get; set; }
public DateTime AuditStartDate { get; set; }
public DateTime? AuditEndDate { get; set; }
[Required(ErrorMessage = "The audit name is required")]
[Display(Name = "Audit Name:")]
[StringLength(200)]
public string Name { get; set; }
public string NameSave { get; set; }
#region DB Ops
/// <summary>
/// Saves existing Audit.
/// </summary>
/// <returns>Id of existing audit</returns>
public int UpdateAndSave()
{
var existingAudit = _unitOfWork.Audits.GetAll()
.Where(a => a.AuditId == this.AuditId).ToList().FirstOrDefault();
existingAudit.Name = this.Name;
_unitOfWork.Save();
return (existingAudit.AuditId);
}
/// <summary>
/// Saves new Audit.
/// </summary>
/// <returns>Id of new audit</returns>
public int AddAndSave()
{
Audit newAudit = new Audit();
newAudit.Name = this.Name;
newAudit.AuditStartDate = DateTime.Now;
newAudit.AuditEndDate = null;
newAudit.AuditUserId = InternalUserId;
newAudit.ExtractId = this.ExtractId;
_unitOfWork.Audits.Add(newAudit);
_unitOfWork.Save();
return (newAudit.AuditId);
}
/// <summary>
/// Gets an Audit.
/// </summary>
/// <param name="unitOfWork">UOW instance for db.</param>
/// <param name="auditId">The audit Id.</param>
/// <returns></returns>
static public AuditViewModel GetAudit(IUnitOfWork unitOfWork, int auditId)
{
var audit = unitOfWork.Audits.GetAll().Where(a => a.AuditId == auditId)
.Select(a => new AuditViewModel
{
AuditId = a.AuditId,
AuditStartDate = a.AuditStartDate,
AuditEndDate = a.AuditEndDate,
InternalUserId = a.AuditUserId,
Name = a.Name,
ExtractId = (int)a.ExtractId
})
.SingleOrDefault();
if (audit != null)
{
audit.TheUnitOfWork = unitOfWork;
}
return (audit);
}
/// <summary>
/// Gets all Audits.
/// </summary>
/// <param name="unitOfWork">UOW instance for db operations.</param>
/// <returns>List of Audits</returns>
static public List<AuditViewModel> GetAudits(IUnitOfWork unitOfWork)
{
var extracts = unitOfWork.Audits.GetAll()
.Select(e => new AuditViewModel
{
AuditId = e.AuditId,
Name = e.Name,
AuditStartDate = e.AuditStartDate,
AuditEndDate = e.AuditEndDate,
InternalUserId = e.AuditUserId,
ExtractId = (int)e.ExtractId
})
.ToList();
return (extracts);
}
/// <summary>
/// Gets exceptions for an Audit.
/// </summary>
/// <param name="unitOfWork">UOW instance for db operations.</param>
/// <param name="auditId">The audit Id.</param>
/// <returns>List of Audit exceptions</returns>
static public List<AuditExceptionModel> GetAuditExceptions(IUnitOfWork unitOfWork, int auditId)
{
var audits = unitOfWork.AuditExceptions.GetAll().Where(a => a.AuditId == auditId)
.Select(e => new AuditExceptionModel
{
AuditExceptionId = e.AuditExceptionId,
AuditExceptionType = e.AuditExceptionType.Description,
Message = e.Message,
AuditExceptionTypeName = e.AuditExceptionType.Name,
AuditExceptionTypeId = e.AuditExceptionTypeId,
DisplayOrder = (int)e.AuditExceptionType.DisplayOrder,
UserNameFromOptimum = e.UserNameFromOptimum,
UserNameFromMaster = e.UserNameFromMaster,
FacilitiesFromOptimum = e.FacilitiesFromOptimum,
FacilitiesFromMaster = e.FacilitiesFromMaster,
TemplatesFromOptimum = e.TemplatesFromOptimum,
TemplatesFromMaster = e.TemplatesFromMaster
})
.OrderBy(e => e.DisplayOrder)
.ToList();
return (audits);
}
/// <summary>
/// Gets master items used in Audit.
/// </summary>
/// <param name="unitOfWork">UOW instance for db operations.</param>
/// <param name="auditId">The audit Id.</param>
/// <returns></returns>
static public List<AuditMasterItemModel> GetAuditMasterItems(IUnitOfWork unitOfWork, int auditId)
{
var items = unitOfWork.AuditMasterItems.GetAll().Where(i => i.AuditId == auditId)
.Select(i => new AuditMasterItemModel
{
AuditMasterItemId = i.AuditMasterItemId,
Login = i.Login,
LastName = i.LastName,
FirstName = i.FirstName,
Facilities = i.Facilities,
Templates = i.Templates
})
.ToList();
return (items);
}
/// <summary>
/// Determines whether Audit is unique.
/// </summary>
/// <returns>Is Audit unique</returns>
public bool IsUniqueYN()
{
bool result = true;
if (AuditId == 0) //new audit
{
var auditCount = _unitOfWork.Audits.GetAll().Where(a => a.Name == this.Name).Count();
if (auditCount > 0)
{
result = false;
}
}
else if (this.Name != this.NameSave) //audit was modified
{
//does the audit name exist
var auditCount = _unitOfWork.Audits.GetAll().Where(a => a.Name == this.Name && a.AuditId != this.AuditId).Count();
if (auditCount > 0)
{
result = false;
}
}
return (result);
}
#endregion
#region Business Logic
/// <summary>
/// Determines whether item should be excluded from Audit.
/// </summary>
/// <param name="extractItem">The extract item.</param>
/// <returns>Whether item should be excluded</returns>
private bool ExcludeFromAuditYN(ExtractItemModel extractItem)
{
return (string.IsNullOrWhiteSpace(extractItem.EmployeeID) ||
string.IsNullOrWhiteSpace(extractItem.SecurityGroup) ||
string.IsNullOrWhiteSpace(extractItem.UserName) ||
extractItem.EmployeeID.Trim().ToLower() == "multiple");
// !extractItem.SecurityGroup.ToLower().Contains("template") ||
}
/// <summary>
/// Builds delimited string of Optimum templates for use in comparison.
/// </summary>
/// <param name="templates">The Optimum templates embedded in other text.</param>
/// <param name="auditName">Name of the Audit.</param>
/// <returns>Delimited string of Optimum templates for comparison</returns>
static public string CalculateOptimumTemplatesForComparison(string templates, string auditName)
{
string templatesUse = (templates ?? string.Empty).Trim().ToLower();
if (string.IsNullOrWhiteSpace(templatesUse))
{
return (string.Empty);
}
//pull out numerics as strings
string[] arrTemplates = Regex.Split(templatesUse, @"\D+").Where(i => i.Length > 0).ToArray();
//convert to actual numeric type - by this point there should be only numeric strings
List<int> arrTemplateVals = new List<int>();
bool isError = false;
foreach(string template in arrTemplates)
{
int number;
if (int.TryParse((template ?? string.Empty).Trim(), out number))
{
arrTemplateVals.Add(number);
}
else
{
isError = true;
}
}
if (isError)
{
//shouldn't happen but log it
string msg = string.Format("One or more non-numeric Optimum Template was found during audit {0}: {1}", auditName, templates);
ErrorTools.HandleError(new ApplicationException(msg), ErrorLevel.NonFatal);
}
var valsSorted = arrTemplateVals.OrderBy(o => o).ToList();
string templatesResult = Utility.IntListToDelimited(valsSorted, ",").Trim();
return (templatesResult);
}
/// <summary>
/// Builds delimited string of Optimum facilities for use in comparison.
/// </summary>
/// <param name="facilities">The Optimum facilities embedded with other text.</param>
/// <param name="auditName">Name of the Audit.</param>
/// <returns>Delimited string of facilities to use in comparison</returns>
static private string CalculateOptimumFacilitiesForComparison(string facilities, string auditName)
{
if (string.IsNullOrWhiteSpace(facilities))
{
return (string.Empty);
}
string optimumFacilitiesDelim = Properties.Settings.Default.OptimumFacilitiesDelim[1].ToString();
List<string> exceptions = new List<string>();
string facResult = Utility.StringToSortedIntString(facilities, optimumFacilitiesDelim, ref exceptions).Trim();
if (exceptions.Count > 0)
{
string msg = string.Format("One or more non-numeric Optimum Facility was found during audit {0}: {1}", auditName, facilities);
ErrorTools.HandleError(new ApplicationException(msg), ErrorLevel.NonFatal);
}
return (facResult);
}
/// <summary>
/// Builds delimited string of Master facilities for use in comparison.
/// </summary>
/// <param name="facilities">The facilities in raw format.</param>
/// <param name="auditName">Name of the audit.</param>
/// <returns>Delimited string of master facilities to use in comparison</returns>
static private string CalculateDatabaseFacilitiesForComparison(string facilities, string auditName)
{
if (string.IsNullOrWhiteSpace(facilities))
{
return (string.Empty);
}
List<string> exceptions = new List<string>();
string facResult = Utility.StringToSortedIntString(facilities, ",", ref exceptions).Trim();
if (exceptions.Count > 0)
{
string msg = string.Format("One or more non-numeric DB Facility was found during audit {0}: {1}", auditName, facilities);
ErrorTools.HandleError(new ApplicationException(msg), ErrorLevel.NonFatal);
}
return (facResult);
}
/// <summary>
/// Builds delimited string of Master templates for use in comparison.
/// </summary>
/// <param name="templates">The templates in raw format.</param>
/// <param name="auditName">Name of the audit.</param>
/// <returns>Delimited string of master templates to use in comparison</returns>
static private string CalculateDatabaseTemplatesForComparison(string templates, string auditName)
{
if (string.IsNullOrWhiteSpace(templates))
{
return (string.Empty);
}
List<string> exceptions = new List<string>();
string result = Utility.StringToSortedIntString(templates, ",", ref exceptions).Trim();
if (exceptions.Count > 0)
{
string msg = string.Format("One or more non-numeric DB Template was found during audit {0}: {1}", auditName, templates);
ErrorTools.HandleError(new ApplicationException(msg), ErrorLevel.NonFatal);
}
return (result);
}
#endregion
#region Audit
/// <summary>
/// Performs the Audit.
/// </summary>
/// <exception cref="System.ApplicationException">A problem occurred while generating database snapshot.</exception>
public void PerformAudit()
{
//when complete present...
//1-Items from upload not included in audit
//2-items in upload but not in master
//3-items in master but not in upload
//4-items in both but with differences
Dictionary<string, ExtractItemModel> extractExclude = new Dictionary<string, ExtractItemModel>();
List<ExtractItemModel> extractInclude = new List<ExtractItemModel>();
List<AuditException> auditExceptions = new List<AuditException>();
//Step 1: get extract contents, by default I'm not peristing it in the model
var extractItems = ExtractViewModel.GetExtractItems(_unitOfWork, (int)this.ExtractId);
//Step 2: determine which extract items can/should be used
foreach(ExtractItemModel extractItem in extractItems)
{
if (!ExcludeFromAuditYN(extractItem))
{
extractInclude.Add(extractItem);
}
else
{
string userNameUse = (extractItem.UserName ?? string.Empty).Trim().ToLower();
if (!extractExclude.ContainsKey(userNameUse))
{
extractExclude.Add(userNameUse, extractItem);
}
}
}
//Note: use extractInclude list as source for audit henceforth
//Step #3: generate database snapshot against which to perform the audit, and build lookup
var genResult = _unitOfWork.OptUsers.GenerateMasterUsersForAudit(this.AuditId);
if (genResult < 1)
{
throw new ApplicationException("A problem occurred while generating database snapshot.");
}
//Step #4: fetch that database snapshot
var databaseMaster = _unitOfWork.AuditMasterItems.GetAll()
.Where(i => i.AuditId == this.AuditId)
.Select(i => new AuditMasterItemModel
{
AuditMasterItemId = i.AuditMasterItemId,
FirstName = i.LastName,
LastName = i.LastName,
Login = i.Login,
Facilities = i.Facilities,
Templates = i.Templates,
ProcessedYN = false //derived col to monitor audit status
})
.ToDictionary(i => i.Login.ToLower(), i => i);
//Step #5: get some helper info to use during audit, e.g. id's for exception types
//ugly code but prevents need for lookups during the audit
int idInUploadButNotInMaster = _unitOfWork.AuditExceptionTypes.GetAll()
.Where(t => t.Name == AuditExceptionTypeModel.InUploadButNotInMaster)
.SingleOrDefault()
.AuditExceptionTypeId;
int idInMasterButNotInUpload = _unitOfWork.AuditExceptionTypes.GetAll()
.Where(t => t.Name == AuditExceptionTypeModel.InMasterButNotInUpload)
.SingleOrDefault()
.AuditExceptionTypeId;
int idInBothWithFacilityDifferences = _unitOfWork.AuditExceptionTypes.GetAll()
.Where(t => t.Name == AuditExceptionTypeModel.InBothWithFacilityDifferences)
.SingleOrDefault()
.AuditExceptionTypeId;
int idInBothWithTemplateDifferences = _unitOfWork.AuditExceptionTypes.GetAll()
.Where(t => t.Name == AuditExceptionTypeModel.InBothWithTemplateDifferences)
.SingleOrDefault()
.AuditExceptionTypeId;
int idInUploadButInvalid = _unitOfWork.AuditExceptionTypes.GetAll()
.Where(t => t.Name == AuditExceptionTypeModel.InUploadButInvalid)
.SingleOrDefault()
.AuditExceptionTypeId;
int idInUploadButHandleManually = _unitOfWork.AuditExceptionTypes.GetAll()
.Where(t => t.Name == AuditExceptionTypeModel.InUploadButHandleManually)
.SingleOrDefault()
.AuditExceptionTypeId;
//Step #6: iterate over upload/extract and perform audit
foreach (ExtractItemModel extractItem in extractInclude)
{
string optTemplates = CalculateOptimumTemplatesForComparison(extractItem.SecurityGroup, this.Name);
string optFac = CalculateOptimumFacilitiesForComparison(extractItem.EmployeeID, this.Name);
if (!databaseMaster.ContainsKey(extractItem.UserName.ToLower()))
{
AuditException ex = new AuditException();
ex.AuditId = this.AuditId;
ex.AuditExceptionTypeId = idInUploadButNotInMaster;
//ex.AuditExceptionType.Name = AuditExceptionTypeModel.InExtractButNotInMaster;
ex.Message = string.Format("User '{0}' occurs in upload but not in master", extractItem.UserName);
ex.UserNameFromOptimum = extractItem.UserName;
ex.UserNameFromMaster = string.Empty;
ex.FacilitiesFromMaster = string.Empty;
ex.FacilitiesFromOptimum = optFac;
ex.TemplatesFromOptimum = optTemplates;
ex.TemplatesFromMaster = string.Empty;
_unitOfWork.AuditExceptions.Add(ex);
}
else
{
//in both but check for facility differences
AuditMasterItemModel dbItem = databaseMaster[extractItem.UserName.ToLower()];
dbItem.ProcessedYN = true; //indicate that db entry has been processed
string dbFac = CalculateDatabaseFacilitiesForComparison(dbItem.Facilities, this.Name);
string dbTemplates = CalculateDatabaseTemplatesForComparison(dbItem.Templates, this.Name);
if (optFac != dbFac)
{
AuditException ex = new AuditException();
ex.AuditId = this.AuditId;
ex.AuditExceptionTypeId = idInBothWithFacilityDifferences;
ex.Message = string.Format("User '{0}' occurs in both but facilities differ", extractItem.UserName);
ex.UserNameFromOptimum = extractItem.UserName;
ex.UserNameFromMaster = extractItem.UserName;
ex.FacilitiesFromMaster = dbFac;
ex.FacilitiesFromOptimum = optFac;
ex.TemplatesFromOptimum = optTemplates;
ex.TemplatesFromMaster = dbTemplates;
_unitOfWork.AuditExceptions.Add(ex);
}
//in both but check for template differences
if (optTemplates != dbTemplates)
{
AuditException ex = new AuditException();
ex.AuditId = this.AuditId;
ex.AuditExceptionTypeId = idInBothWithTemplateDifferences;
ex.Message = string.Format("User '{0}' occurs in both but templates differ", extractItem.UserName);
ex.UserNameFromOptimum = extractItem.UserName;
ex.UserNameFromMaster = extractItem.UserName;
ex.FacilitiesFromMaster = dbFac;
ex.FacilitiesFromOptimum = optFac;
ex.TemplatesFromOptimum = optTemplates;
ex.TemplatesFromMaster = dbTemplates;
_unitOfWork.AuditExceptions.Add(ex);
}
}
}
//Step #7: make note of master items that were not processed
foreach(string key in databaseMaster.Keys)
{
if (!databaseMaster[key].ProcessedYN && !extractExclude.ContainsKey(key))
{
string dbFac = CalculateDatabaseFacilitiesForComparison(databaseMaster[key].Facilities, this.Name);
string dbTemplates = CalculateDatabaseTemplatesForComparison(databaseMaster[key].Templates, this.Name);
AuditException ex = new AuditException();
ex.AuditId = this.AuditId;
ex.AuditExceptionTypeId = idInMasterButNotInUpload;
ex.Message = string.Format("User '{0}' occurs in master but not in upload", key);
ex.UserNameFromMaster = key;
ex.UserNameFromOptimum = string.Empty;
ex.FacilitiesFromMaster = dbFac;
ex.FacilitiesFromOptimum = string.Empty;
ex.TemplatesFromOptimum = string.Empty;
ex.TemplatesFromMaster = dbTemplates;
_unitOfWork.AuditExceptions.Add(ex);
}
}
//Step #8: make note of extract items that were excluded - add them as exceptions
foreach (ExtractItemModel extractItem in extractExclude.Values)
{
AuditException ex = new AuditException();
string optTemplates = CalculateOptimumTemplatesForComparison(extractItem.SecurityGroup, this.Name);
ex.AuditId = this.AuditId;
ex.TemplatesFromMaster = string.Empty;
ex.TemplatesFromOptimum = optTemplates;
ex.UserNameFromMaster = string.Empty;
ex.FacilitiesFromMaster = string.Empty;
ex.UserNameFromOptimum = extractItem.UserName;
if ((extractItem.EmployeeID ?? string.Empty).Trim().ToLower() == "multiple")
{
ex.AuditExceptionTypeId = idInUploadButHandleManually;
ex.Message = string.Format("User '{0}' must be handled manually", extractItem.UserName);
ex.FacilitiesFromOptimum = extractItem.EmployeeID;
}
else
{
string optFac = CalculateOptimumFacilitiesForComparison(extractItem.EmployeeID, this.Name);
ex.AuditExceptionTypeId = idInUploadButInvalid;
ex.Message = string.Format("User '{0}' from upload was excluded from audit", extractItem.UserName);
ex.FacilitiesFromOptimum = optFac;
}
_unitOfWork.AuditExceptions.Add(ex);
}
//Step #9: set end date
Audit audit = _unitOfWork.Audits.GetAll().Where(a => a.AuditId == this.AuditId).SingleOrDefault();
audit.AuditEndDate = DateTime.Now;
//Step #9: commit everyting - may take a minute
_unitOfWork.Save();
}
#endregion
}
}