Feature #33: Enhanced Device Importing
Dynamic device importing. better input parsing and 5 additional import fields.
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using Disco.Services.Devices.Importing.Fields;
|
||||
using Disco.Services.Tasks;
|
||||
using LumenWorks.Framework.IO.Csv;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
public static class DeviceImport
|
||||
{
|
||||
|
||||
internal static Lazy<Dictionary<DeviceImportFieldTypes, Type>> FieldHandlers = new Lazy<Dictionary<DeviceImportFieldTypes, Type>>(() =>
|
||||
{
|
||||
return new Dictionary<DeviceImportFieldTypes, Type>()
|
||||
{
|
||||
{ DeviceImportFieldTypes.DeviceSerialNumber, typeof(DeviceSerialNumberImportField) },
|
||||
{ DeviceImportFieldTypes.DeviceAssetNumber, typeof(DeviceAssetNumberImportField) },
|
||||
{ DeviceImportFieldTypes.DeviceLocation, typeof(DeviceLocationImportField) },
|
||||
{ DeviceImportFieldTypes.DeviceDecommissionedDate, typeof(DeviceDecommissionedDateImportField) },
|
||||
{ DeviceImportFieldTypes.DeviceDecommissionedReason, typeof(DeviceDecommissionedReasonImportField) },
|
||||
|
||||
{ DeviceImportFieldTypes.DetailLanMacAddress, typeof(DetailLanMacAddressImportField) },
|
||||
{ DeviceImportFieldTypes.DetailWLanMacAddress, typeof(DetailWLanMacAddressImportField) },
|
||||
{ DeviceImportFieldTypes.DetailACAdapter, typeof(DetailACAdapterImportField) },
|
||||
|
||||
{ DeviceImportFieldTypes.ModelId, typeof(ModelIdImportField) },
|
||||
|
||||
{ DeviceImportFieldTypes.BatchId, typeof(BatchIdImportField) },
|
||||
|
||||
{ DeviceImportFieldTypes.ProfileId, typeof(ProfileIdImportField) },
|
||||
|
||||
{ DeviceImportFieldTypes.AssignedUserId, typeof(AssignedUserIdImportField) }
|
||||
};
|
||||
});
|
||||
|
||||
public static DeviceImportContext BeginImport(DiscoDataContext Database, string Filename, bool HasHeader, Stream FileContent)
|
||||
{
|
||||
if (FileContent == null)
|
||||
throw new ArgumentNullException("FileContent");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Filename))
|
||||
Filename = "<None Specified>";
|
||||
|
||||
DeviceImportContext context;
|
||||
List<Tuple<string, DeviceImportFieldTypes>> header;
|
||||
List<string[]> rawData;
|
||||
|
||||
using (TextReader csvTextReader = new StreamReader(FileContent))
|
||||
{
|
||||
using (CsvReader csvReader = new CsvReader(csvTextReader, HasHeader))
|
||||
{
|
||||
csvReader.DefaultParseErrorAction = ParseErrorAction.ThrowException;
|
||||
csvReader.MissingFieldAction = MissingFieldAction.ReplaceByNull;
|
||||
|
||||
rawData = csvReader.ToList();
|
||||
header = csvReader.GetFieldHeaders().Select(h => Tuple.Create(h, DeviceImportFieldTypes.IgnoreColumn)).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
context = new DeviceImportContext(Filename, header, rawData);
|
||||
|
||||
context.GuessHeaderTypes(Database);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private static void GuessHeaderTypes(this DeviceImportContext Context, DiscoDataContext Database)
|
||||
{
|
||||
FieldHandlers.Value.ToList().ForEach(h =>
|
||||
{
|
||||
var instance = (DeviceImportFieldBase)Activator.CreateInstance(h.Value);
|
||||
var column = instance.GuessHeader(Database, Context);
|
||||
if (column.HasValue)
|
||||
Context.Header[column.Value] = Tuple.Create(Context.Header[column.Value].Item1, instance.FieldType);
|
||||
});
|
||||
}
|
||||
|
||||
public static void UpdateHeaderTypes(this DeviceImportContext Context, List<DeviceImportFieldTypes> HeaderTypes)
|
||||
{
|
||||
if (HeaderTypes == null)
|
||||
throw new ArgumentNullException("HeaderTypes");
|
||||
|
||||
if (HeaderTypes.Count != Context.Header.Count)
|
||||
throw new ArgumentException("The number of Header Types supplied does not match the number of Headers", "HeaderTypes");
|
||||
|
||||
if (!HeaderTypes.Any(h => h == DeviceImportFieldTypes.DeviceSerialNumber))
|
||||
throw new ArgumentException("At least one column must be the Device Serial Number", "HeaderTypes");
|
||||
|
||||
if (HeaderTypes.Where(h => h != DeviceImportFieldTypes.IgnoreColumn).GroupBy(h => h, (k, i) => Tuple.Create(k, i.Count())).Any(g => g.Item2 > 1))
|
||||
throw new ArgumentException("Column types can only be specified once for each type", "HeaderTypes");
|
||||
|
||||
Context.Header = Context.Header.Zip(HeaderTypes, (h, ht) => Tuple.Create(h.Item1, ht)).ToList();
|
||||
}
|
||||
|
||||
public static void ParseRecords(this DeviceImportContext Context, DiscoDataContext Database, IScheduledTaskBasicStatus Status)
|
||||
{
|
||||
if (Context.Header == null)
|
||||
throw new InvalidOperationException("The Import Context has not been initialized");
|
||||
|
||||
if (Context.Header.Count == 0)
|
||||
throw new InvalidOperationException("No Headers were found");
|
||||
|
||||
if (!Context.Header.Any(h => h.Item2 == DeviceImportFieldTypes.DeviceSerialNumber))
|
||||
throw new ArgumentException("At least one column must be the Device Serial Number", "Header");
|
||||
|
||||
if (Context.RawData == null || Context.RawData.Count == 0)
|
||||
throw new ArgumentException("No data was found in the import file", "RawData");
|
||||
|
||||
IDeviceImportCache cache;
|
||||
if (Context.RawData.Count > 20)
|
||||
cache = new DeviceImportInMemoryCache(Database);
|
||||
else
|
||||
cache = new DeviceImportDatabaseCache(Database);
|
||||
|
||||
Context.HeaderDeviceSerialNumberIndex = Context.Header.IndexOf(Context.Header.First(h => h.Item2 == DeviceImportFieldTypes.DeviceSerialNumber));
|
||||
Context.ParsedHeaders = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h.Item1, h.Item2, i))
|
||||
.Where(h => h.Item2 != DeviceImportFieldTypes.IgnoreColumn)
|
||||
.Select(h => new Tuple<string, DeviceImportFieldTypes, Func<string[], string>, Type>(h.Item1, h.Item2, (f) => f[h.Item3], DeviceImport.FieldHandlers.Value[h.Item2]))
|
||||
.ToList();
|
||||
|
||||
DateTime nextProgress = DateTime.Now;
|
||||
Status.UpdateStatus(0, "Parsing Import Records", "Starting...");
|
||||
|
||||
Context.Records = Context.RawData.Select((d, recordIndex) =>
|
||||
{
|
||||
string deviceSerialNumber = Fields.DeviceSerialNumberImportField.ParseRawDeviceSerialNumber(d[Context.HeaderDeviceSerialNumberIndex]);
|
||||
|
||||
if (nextProgress <= DateTime.Now)
|
||||
{
|
||||
Status.UpdateStatus(((double)recordIndex / Context.RawData.Count) * 100, string.Format("Parsing: {0}", deviceSerialNumber));
|
||||
nextProgress = DateTime.Now.AddSeconds(.5);
|
||||
}
|
||||
|
||||
Device existingDevice = null;
|
||||
if (Fields.DeviceSerialNumberImportField.IsDeviceSerialNumberValid(deviceSerialNumber))
|
||||
existingDevice = cache.Devices.FirstOrDefault(device => device.SerialNumber == deviceSerialNumber);
|
||||
|
||||
var values = Context.ParsedHeaders
|
||||
.ToDictionary(k => k.Item2, k => k.Item3(d));
|
||||
|
||||
var fields = Context.ParsedHeaders.Select(h =>
|
||||
{
|
||||
var f = (DeviceImportFieldBase)Activator.CreateInstance(h.Item4);
|
||||
f.Parse(Database, cache, Context, recordIndex, deviceSerialNumber, existingDevice, values, h.Item3(d));
|
||||
return f;
|
||||
}).ToList();
|
||||
|
||||
EntityState recordAction;
|
||||
if (fields.Any(f => !f.FieldAction.HasValue))
|
||||
recordAction = EntityState.Detached;
|
||||
else if (existingDevice == null)
|
||||
recordAction = EntityState.Added;
|
||||
else if (fields.Any(f => f.FieldAction == EntityState.Modified))
|
||||
recordAction = EntityState.Modified;
|
||||
else
|
||||
recordAction = EntityState.Unchanged;
|
||||
|
||||
return new DeviceImportRecord(deviceSerialNumber, fields, recordAction);
|
||||
}).Cast<IDeviceImportRecord>().ToList();
|
||||
}
|
||||
|
||||
public static int ApplyRecords(this DeviceImportContext Context, DiscoDataContext Database, IScheduledTaskBasicStatus Status)
|
||||
{
|
||||
if (Context.Records == null)
|
||||
throw new InvalidOperationException("Import Records have not been parsed");
|
||||
|
||||
if (Context.Records.Count == 0)
|
||||
throw new InvalidOperationException("There are no records to import");
|
||||
|
||||
DateTime nextProgress = DateTime.Now;
|
||||
Status.UpdateStatus(0, "Applying Import Records to Database", "Starting...");
|
||||
|
||||
int affectedRecords = 0;
|
||||
|
||||
foreach (var record in Context.Records.Cast<DeviceImportRecord>().Select((r, i) => Tuple.Create(r, i)))
|
||||
{
|
||||
if (nextProgress <= DateTime.Now)
|
||||
{
|
||||
Status.UpdateStatus(((double)record.Item2 / Context.Records.Count) * 100, string.Format("Applying: {0}", record.Item1.DeviceSerialNumber));
|
||||
nextProgress = DateTime.Now.AddSeconds(.5);
|
||||
}
|
||||
|
||||
if (record.Item1.Apply(Database))
|
||||
affectedRecords++;
|
||||
}
|
||||
|
||||
return affectedRecords;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Services.Tasks;
|
||||
using Quartz;
|
||||
using System;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
public class DeviceImportApplyTask : ScheduledTask
|
||||
{
|
||||
private const string JobDataMapContext = "Context";
|
||||
|
||||
public override string TaskName { get { return "Import Devices - Applying Changes"; } }
|
||||
public override bool SingleInstanceTask { get { return false; } }
|
||||
public override bool CancelInitiallySupported { get { return false; } }
|
||||
|
||||
public static ScheduledTaskStatus ScheduleNow(DeviceImportContext Context)
|
||||
{
|
||||
if (Context == null)
|
||||
throw new ArgumentNullException("Context");
|
||||
|
||||
// Build Data Map
|
||||
var task = new DeviceImportApplyTask();
|
||||
JobDataMap taskData = new JobDataMap() { { JobDataMapContext, Context } };
|
||||
|
||||
// Schedule Task
|
||||
return task.ScheduleTask(taskData);
|
||||
}
|
||||
|
||||
protected override void ExecuteTask()
|
||||
{
|
||||
var context = (DeviceImportContext)this.ExecutionContext.JobDetail.JobDataMap[JobDataMapContext];
|
||||
|
||||
using (DiscoDataContext Database = new DiscoDataContext())
|
||||
{
|
||||
context.AffectedRecords = context.ApplyRecords(Database, this.Status);
|
||||
}
|
||||
|
||||
Status.SetFinishedMessage(string.Format("Successfully imported/updated {0} device{1}", context.AffectedRecords, context.AffectedRecords == 1 ? null : "s"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using Disco.Services.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
public class DeviceImportContext : IDeviceImportContext
|
||||
{
|
||||
public string SessionId { get; private set; }
|
||||
public string Filename { get; private set; }
|
||||
|
||||
public List<Tuple<string, DeviceImportFieldTypes>> Header { get; internal set; }
|
||||
public List<Tuple<string, DeviceImportFieldTypes, Func<string[], string>, Type>> ParsedHeaders { get; internal set; }
|
||||
internal int HeaderDeviceSerialNumberIndex { get; set; }
|
||||
|
||||
public List<string[]> RawData { get; private set; }
|
||||
|
||||
public List<IDeviceImportRecord> Records { get; internal set; }
|
||||
public int AffectedRecords { get; internal set; }
|
||||
|
||||
internal DeviceImportContext(string Filename, List<Tuple<string, DeviceImportFieldTypes>> Header, List<string[]> RawData)
|
||||
{
|
||||
this.SessionId = Guid.NewGuid().ToString("D");
|
||||
|
||||
this.Filename = Filename;
|
||||
this.Header = Header;
|
||||
this.RawData = RawData;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
internal class DeviceImportDatabaseCache : IDeviceImportCache
|
||||
{
|
||||
private DiscoDataContext Database;
|
||||
|
||||
public DeviceImportDatabaseCache(DiscoDataContext Database)
|
||||
{
|
||||
this.Database = Database;
|
||||
}
|
||||
|
||||
public Device FindDevice(string DeviceSerialNumber)
|
||||
{
|
||||
return Database.Devices.FirstOrDefault(d => d.SerialNumber == DeviceSerialNumber);
|
||||
}
|
||||
|
||||
public IEnumerable<Device> Devices
|
||||
{
|
||||
get { return Database.Devices; }
|
||||
}
|
||||
|
||||
public IEnumerable<DeviceModel> DeviceModels
|
||||
{
|
||||
get { return Database.DeviceModels; }
|
||||
}
|
||||
|
||||
public IEnumerable<DeviceProfile> DeviceProfiles
|
||||
{
|
||||
get { return Database.DeviceProfiles; }
|
||||
}
|
||||
|
||||
public IEnumerable<DeviceBatch> DeviceBatches
|
||||
{
|
||||
get { return Database.DeviceBatches; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
internal abstract class DeviceImportFieldBase : IDeviceImportField
|
||||
{
|
||||
public abstract DeviceImportFieldTypes FieldType { get; }
|
||||
|
||||
public EntityState? FieldAction { get; protected set; }
|
||||
|
||||
public string ErrorMessage { get; protected set; }
|
||||
|
||||
public abstract object RawParsedValue { get; }
|
||||
public abstract string FriendlyValue { get; }
|
||||
public abstract string FriendlyPreviousValue { get; }
|
||||
|
||||
public abstract bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value);
|
||||
public abstract bool Apply(DiscoDataContext Database, Device Device);
|
||||
|
||||
public abstract int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context);
|
||||
|
||||
#region Helpers
|
||||
protected bool Error(string Message)
|
||||
{
|
||||
this.ErrorMessage = Message;
|
||||
this.FieldAction = null;
|
||||
return false;
|
||||
}
|
||||
protected bool Success(EntityState Action)
|
||||
{
|
||||
this.FieldAction = Action;
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
internal class DeviceImportInMemoryCache : IDeviceImportCache
|
||||
{
|
||||
private DiscoDataContext Database;
|
||||
|
||||
private Lazy<IEnumerable<Device>> devices;
|
||||
private Lazy<IEnumerable<DeviceModel>> deviceModels;
|
||||
private Lazy<IEnumerable<DeviceProfile>> deviceProfiles;
|
||||
private Lazy<IEnumerable<DeviceBatch>> deviceBatches;
|
||||
|
||||
public DeviceImportInMemoryCache(DiscoDataContext Database)
|
||||
{
|
||||
this.Database = Database;
|
||||
|
||||
this.devices = new Lazy<IEnumerable<Device>>(() => Database.Devices.Include("DeviceDetails").ToList());
|
||||
this.deviceModels = new Lazy<IEnumerable<DeviceModel>>(() => Database.DeviceModels.ToList());
|
||||
this.deviceProfiles = new Lazy<IEnumerable<DeviceProfile>>(() => Database.DeviceProfiles.ToList());
|
||||
this.deviceBatches = new Lazy<IEnumerable<DeviceBatch>>(() => Database.DeviceBatches.ToList());
|
||||
}
|
||||
|
||||
public Device FindDevice(string DeviceSerialNumber)
|
||||
{
|
||||
return devices.Value.FirstOrDefault(d => d.SerialNumber.Equals(DeviceSerialNumber, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
public IEnumerable<Device> Devices
|
||||
{
|
||||
get { return devices.Value; }
|
||||
}
|
||||
|
||||
public IEnumerable<DeviceModel> DeviceModels
|
||||
{
|
||||
get { return deviceModels.Value; }
|
||||
}
|
||||
|
||||
public IEnumerable<DeviceProfile> DeviceProfiles
|
||||
{
|
||||
get { return deviceProfiles.Value; }
|
||||
}
|
||||
|
||||
public IEnumerable<DeviceBatch> DeviceBatches
|
||||
{
|
||||
get { return deviceBatches.Value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Services.Tasks;
|
||||
using Quartz;
|
||||
using System;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
public class DeviceImportParseTask : ScheduledTask
|
||||
{
|
||||
private const string JobDataMapContext = "Context";
|
||||
|
||||
public override string TaskName { get { return "Import Devices - Parsing Records"; } }
|
||||
public override bool SingleInstanceTask { get { return false; } }
|
||||
public override bool CancelInitiallySupported { get { return false; } }
|
||||
|
||||
public static ScheduledTaskStatus ScheduleNow(DeviceImportContext Context)
|
||||
{
|
||||
if (Context == null)
|
||||
throw new ArgumentNullException("Context");
|
||||
|
||||
// Build Data Map
|
||||
var task = new DeviceImportParseTask();
|
||||
JobDataMap taskData = new JobDataMap() { { JobDataMapContext, Context } };
|
||||
|
||||
// Schedule Task
|
||||
return task.ScheduleTask(taskData);
|
||||
}
|
||||
|
||||
protected override void ExecuteTask()
|
||||
{
|
||||
var context = (DeviceImportContext)this.ExecutionContext.JobDetail.JobDataMap[JobDataMapContext];
|
||||
|
||||
using (DiscoDataContext Database = new DiscoDataContext())
|
||||
{
|
||||
context.ParseRecords(Database, this.Status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
internal class DeviceImportRecord : IDeviceImportRecord
|
||||
{
|
||||
public string DeviceSerialNumber { get; private set; }
|
||||
|
||||
public IEnumerable<IDeviceImportField> Fields { get; private set; }
|
||||
|
||||
public EntityState RecordAction { get; private set; }
|
||||
|
||||
public bool HasError
|
||||
{
|
||||
get { return Fields.Any(f => !f.FieldAction.HasValue); }
|
||||
}
|
||||
|
||||
internal DeviceImportRecord(string DeviceSerialNumber, IEnumerable<IDeviceImportField> Fields, EntityState RecordAction)
|
||||
{
|
||||
this.DeviceSerialNumber = DeviceSerialNumber;
|
||||
this.Fields = Fields;
|
||||
this.RecordAction = RecordAction;
|
||||
}
|
||||
|
||||
public bool Apply(DiscoDataContext Database)
|
||||
{
|
||||
if (RecordAction == EntityState.Detached || !HasError)
|
||||
{
|
||||
Device device;
|
||||
|
||||
if (RecordAction == EntityState.Unchanged)
|
||||
{
|
||||
// Unchanged - No Action Required
|
||||
return false;
|
||||
}
|
||||
else if (RecordAction == EntityState.Modified)
|
||||
{
|
||||
device = Database.Devices.Find(this.DeviceSerialNumber);
|
||||
}
|
||||
else if (RecordAction == EntityState.Added)
|
||||
{
|
||||
// Create Device
|
||||
device = new Device()
|
||||
{
|
||||
SerialNumber = DeviceSerialNumber.ToUpper(),
|
||||
CreatedDate = DateTime.Now,
|
||||
AllowUnauthenticatedEnrol = true,
|
||||
DeviceProfileId = Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId,
|
||||
DeviceModelId = 1 // Default 'Unknown Device Model'
|
||||
};
|
||||
Database.Devices.Add(device);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Invalid State
|
||||
return false;
|
||||
}
|
||||
|
||||
bool changesMade = false;
|
||||
|
||||
foreach (var field in Fields.Cast<DeviceImportFieldBase>())
|
||||
{
|
||||
changesMade = field.Apply(Database, device) || changesMade;
|
||||
}
|
||||
|
||||
// Commit Changes
|
||||
if (changesMade)
|
||||
Database.SaveChanges();
|
||||
|
||||
return changesMade;
|
||||
}
|
||||
|
||||
// Record has Errors
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using Disco.Services.Users;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class AssignedUserIdImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string parsedValue;
|
||||
private string friendlyValue;
|
||||
private string friendlyPreviousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.AssignedUserId; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return friendlyValue; } }
|
||||
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
friendlyValue = Value;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
{
|
||||
friendlyValue = null;
|
||||
parsedValue = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
parsedValue = Value.Trim();
|
||||
|
||||
if (!parsedValue.Contains('\\'))
|
||||
parsedValue = string.Format(@"{0}\{1}", Interop.ActiveDirectory.ActiveDirectory.Context.PrimaryDomain.NetBiosName, parsedValue);
|
||||
|
||||
friendlyValue = parsedValue;
|
||||
|
||||
if (parsedValue.Length > 50)
|
||||
return Error("Cannot be more than 50 characters");
|
||||
}
|
||||
|
||||
if (parsedValue != null)
|
||||
{
|
||||
// Check User Exists
|
||||
|
||||
// Try Database
|
||||
User user = Database.Users.FirstOrDefault(u => u.UserId == parsedValue);
|
||||
try
|
||||
{
|
||||
// Try Updating from AD
|
||||
user = UserService.GetUser(parsedValue, Database);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (user == null)
|
||||
return Error(ex.Message);
|
||||
}
|
||||
parsedValue = user.UserId;
|
||||
friendlyValue = string.Format("{0} [{1}]", user.DisplayName, user.UserId);
|
||||
|
||||
// Check Decommissioned
|
||||
bool? importDecommissioning = null;
|
||||
if (Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedDate) || Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedReason))
|
||||
importDecommissioning = Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedDate) && !string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedDate]) ||
|
||||
Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedReason) && !string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedReason]);
|
||||
|
||||
if (importDecommissioning.HasValue && importDecommissioning.Value)
|
||||
return Error("Cannot assign a user to a device being decommissioned");
|
||||
|
||||
if (ExistingDevice != null && ExistingDevice.DecommissionedDate.HasValue && !importDecommissioning.HasValue)
|
||||
{
|
||||
return Error("Cannot assign a user to a decommissioned device");
|
||||
}
|
||||
}
|
||||
|
||||
if (ExistingDevice == null && parsedValue != null)
|
||||
{
|
||||
return Success(EntityState.Added);
|
||||
}
|
||||
else if (ExistingDevice != null && ExistingDevice.AssignedUserId != parsedValue)
|
||||
{
|
||||
if (ExistingDevice.AssignedUserId != null)
|
||||
friendlyPreviousValue = string.Format("{0} [{1}]", ExistingDevice.AssignedUser.DisplayName, ExistingDevice.AssignedUser.UserId);
|
||||
else
|
||||
friendlyPreviousValue = null;
|
||||
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
// Remove Current Assignments
|
||||
var currentAssignments = Device.DeviceUserAssignments.Where(dua => !dua.UnassignedDate.HasValue);
|
||||
foreach (var currentAssignment in currentAssignments)
|
||||
{
|
||||
currentAssignment.UnassignedDate = DateTime.Now;
|
||||
}
|
||||
|
||||
// Add Assignment
|
||||
if (parsedValue != null)
|
||||
{
|
||||
var assignment = new DeviceUserAssignment()
|
||||
{
|
||||
Device = Device,
|
||||
DeviceSerialNumber = Device.SerialNumber,
|
||||
AssignedUserId = parsedValue,
|
||||
AssignedDate = DateTime.Now
|
||||
};
|
||||
Database.DeviceUserAssignments.Add(assignment);
|
||||
}
|
||||
Device.AssignedUserId = parsedValue;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn &&
|
||||
h.Item1.Item1.IndexOf("user id", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("userid", System.StringComparison.OrdinalIgnoreCase) >= 0
|
||||
);
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class BatchIdImportField : DeviceImportFieldBase
|
||||
{
|
||||
private int? parsedValue;
|
||||
private string friendlyValue;
|
||||
private string friendlyPreviousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.BatchId; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return friendlyValue; } }
|
||||
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
friendlyValue = Value;
|
||||
|
||||
// Validate
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
this.parsedValue = null; // Default = null
|
||||
else
|
||||
{
|
||||
int valueInt;
|
||||
if (int.TryParse(Value, out valueInt))
|
||||
this.parsedValue = valueInt;
|
||||
else
|
||||
return Error("The Batch Identifier must be a number");
|
||||
}
|
||||
|
||||
if (this.parsedValue.HasValue)
|
||||
{
|
||||
var b = Cache.DeviceBatches.FirstOrDefault(db => db.Id == parsedValue);
|
||||
if (b == null)
|
||||
return Error(string.Format("The identifier ({0}) does not match any Device Batch", Value));
|
||||
friendlyValue = string.Format("{0} [{1}]", b.Name, b.Id);
|
||||
}
|
||||
else
|
||||
friendlyValue = null;
|
||||
|
||||
if (ExistingDevice == null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null && ExistingDevice.DeviceBatchId != parsedValue)
|
||||
{
|
||||
DeviceBatch previousBatch = null;
|
||||
if (ExistingDevice.DeviceBatchId.HasValue)
|
||||
previousBatch = Cache.DeviceBatches.FirstOrDefault(db => db.Id == ExistingDevice.DeviceBatchId.Value);
|
||||
|
||||
if (previousBatch != null)
|
||||
friendlyPreviousValue = string.Format("{0} [{1}]", previousBatch.Name, previousBatch.Id);
|
||||
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
Device.DeviceBatchId = this.parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && h.Item1.Item1.IndexOf("batch", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
// All Integers Numbers
|
||||
possibleColumns = possibleColumns.Where(h =>
|
||||
{
|
||||
int lastValue;
|
||||
return Context.RawData.Select(v => v[h.Item2]).Take(100).Where(v => !string.IsNullOrWhiteSpace(v)).All(v => int.TryParse(v, out lastValue));
|
||||
}).ToList();
|
||||
|
||||
// Multiple Columns, tighten column definition
|
||||
if (possibleColumns.Count() > 1)
|
||||
{
|
||||
possibleColumns = possibleColumns
|
||||
.Where(h =>
|
||||
h.Item1.Item1.IndexOf("batchid", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("batch id", StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
}
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DetailACAdapterImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string parsedValue;
|
||||
private string previousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DetailACAdapter; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
parsedValue = null;
|
||||
else
|
||||
{
|
||||
parsedValue = Value.Trim();
|
||||
}
|
||||
|
||||
if (ExistingDevice == null && parsedValue != null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null)
|
||||
{
|
||||
var detail = ExistingDevice.DeviceDetails.FirstOrDefault(dd => dd.Scope == DeviceDetail.ScopeHardware && dd.Key == DeviceDetail.HardwareKeyACAdapter);
|
||||
|
||||
if (detail == null && parsedValue == null)
|
||||
return Success(EntityState.Unchanged);
|
||||
else if (detail == null && parsedValue != null)
|
||||
{
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else if (detail.Value != parsedValue)
|
||||
{
|
||||
previousValue = detail.Value;
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
|
||||
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
|
||||
dd.DeviceSerialNumber == Device.SerialNumber &&
|
||||
dd.Scope == DeviceDetail.ScopeHardware &&
|
||||
dd.Key == DeviceDetail.HardwareKeyACAdapter);
|
||||
|
||||
if (detail == null)
|
||||
{
|
||||
detail = new DeviceDetail()
|
||||
{
|
||||
Device = Device,
|
||||
DeviceSerialNumber = Device.SerialNumber,
|
||||
Scope = DeviceDetail.ScopeHardware,
|
||||
Key = DeviceDetail.HardwareKeyACAdapter
|
||||
};
|
||||
Database.DeviceDetails.Add(detail);
|
||||
}
|
||||
|
||||
detail.Value = parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn &&
|
||||
(h.Item1.Item1.IndexOf("ac adapter", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("acadapter", System.StringComparison.OrdinalIgnoreCase) >= 0));
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DetailLanMacAddressImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string parsedValue;
|
||||
private string previousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DetailLanMacAddress; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
parsedValue = null;
|
||||
else
|
||||
{
|
||||
parsedValue = Value.Trim();
|
||||
}
|
||||
|
||||
if (ExistingDevice == null && parsedValue != null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null)
|
||||
{
|
||||
var detail = ExistingDevice.DeviceDetails.FirstOrDefault(dd => dd.Scope == DeviceDetail.ScopeHardware && dd.Key == DeviceDetail.HardwareKeyLanMacAddress);
|
||||
|
||||
if (detail == null && parsedValue == null)
|
||||
return Success(EntityState.Unchanged);
|
||||
else if (detail == null && parsedValue != null)
|
||||
{
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else if (detail.Value != parsedValue)
|
||||
{
|
||||
previousValue = detail.Value;
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
|
||||
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
|
||||
dd.DeviceSerialNumber == Device.SerialNumber &&
|
||||
dd.Scope == DeviceDetail.ScopeHardware &&
|
||||
dd.Key == DeviceDetail.HardwareKeyLanMacAddress);
|
||||
|
||||
if (detail == null)
|
||||
{
|
||||
detail = new DeviceDetail()
|
||||
{
|
||||
Device = Device,
|
||||
DeviceSerialNumber = Device.SerialNumber,
|
||||
Scope = DeviceDetail.ScopeHardware,
|
||||
Key = DeviceDetail.HardwareKeyLanMacAddress
|
||||
};
|
||||
Database.DeviceDetails.Add(detail);
|
||||
}
|
||||
|
||||
detail.Value = parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && (
|
||||
h.Item1.Item1.IndexOf("wlan", System.StringComparison.OrdinalIgnoreCase) < 0 &&
|
||||
h.Item1.Item1.IndexOf("wireless", System.StringComparison.OrdinalIgnoreCase) < 0 && (
|
||||
h.Item1.Item1.IndexOf("lan address", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("lan mac", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("lan mac address", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("lanaddress", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("lanmac", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("lanmacaddress", System.StringComparison.OrdinalIgnoreCase) >= 0
|
||||
)));
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DetailWLanMacAddressImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string parsedValue;
|
||||
private string previousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DetailWLanMacAddress; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
parsedValue = null;
|
||||
else
|
||||
{
|
||||
parsedValue = Value.Trim();
|
||||
}
|
||||
|
||||
if (ExistingDevice == null && parsedValue != null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null)
|
||||
{
|
||||
var detail = ExistingDevice.DeviceDetails.FirstOrDefault(dd => dd.Scope == DeviceDetail.ScopeHardware && dd.Key == DeviceDetail.HardwareKeyWLanMacAddress);
|
||||
|
||||
if (detail == null && parsedValue == null)
|
||||
return Success(EntityState.Unchanged);
|
||||
else if (detail == null && parsedValue != null)
|
||||
{
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else if (detail.Value != parsedValue)
|
||||
{
|
||||
previousValue = detail.Value;
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
|
||||
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
|
||||
dd.DeviceSerialNumber == Device.SerialNumber &&
|
||||
dd.Scope == DeviceDetail.ScopeHardware &&
|
||||
dd.Key == DeviceDetail.HardwareKeyWLanMacAddress);
|
||||
|
||||
if (detail == null)
|
||||
{
|
||||
detail = new DeviceDetail()
|
||||
{
|
||||
Device = Device,
|
||||
DeviceSerialNumber = Device.SerialNumber,
|
||||
Scope = DeviceDetail.ScopeHardware,
|
||||
Key = DeviceDetail.HardwareKeyWLanMacAddress
|
||||
};
|
||||
Database.DeviceDetails.Add(detail);
|
||||
}
|
||||
|
||||
detail.Value = parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && (
|
||||
h.Item1.Item1.IndexOf("wireless lan mac address", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wireless lan address", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wireless mac address", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wireless mac", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wlan address", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wlan mac", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wlan mac address", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wlanaddress", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wlanmac", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("wlanmacaddress", System.StringComparison.OrdinalIgnoreCase) >= 0
|
||||
));
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DeviceAssetNumberImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string parsedValue;
|
||||
private string previousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DeviceAssetNumber; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
parsedValue = null;
|
||||
else
|
||||
{
|
||||
parsedValue = Value.Trim();
|
||||
if (parsedValue.Length > 40)
|
||||
return Error("Cannot be more than 40 characters");
|
||||
}
|
||||
|
||||
if (ExistingDevice == null && parsedValue != null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null && ExistingDevice.AssetNumber != parsedValue)
|
||||
{
|
||||
previousValue = ExistingDevice.AssetNumber;
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
Device.AssetNumber = this.parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// 'asset' in column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && h.Item1.Item1.IndexOf("asset", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DeviceDecommissionedDateImportField : DeviceImportFieldBase
|
||||
{
|
||||
private const string DateFormat = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
private string rawValue;
|
||||
private DateTime? parsedValue;
|
||||
private DateTime? previousValue;
|
||||
private bool setReason { get; set; }
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DeviceDecommissionedDate; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue.HasValue ? parsedValue.Value.ToString(DateFormat) : rawValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue.HasValue ? previousValue.Value.ToString(DateFormat) : null; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
{
|
||||
rawValue = null;
|
||||
parsedValue = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
DateTime valueDateTime;
|
||||
if (!DateTime.TryParse(Value.Trim(), CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.AssumeLocal, out valueDateTime))
|
||||
{
|
||||
rawValue = Value.Trim();
|
||||
return Error(string.Format("Cannot parse the value as a Date/Time using {0} culture (system default).", CultureInfo.CurrentCulture.Name));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Accuracy to the second (remove any milliseconds)
|
||||
parsedValue = new DateTime((valueDateTime.Ticks / 10000000L) * 10000000L);
|
||||
}
|
||||
}
|
||||
|
||||
string errorMessage;
|
||||
if (parsedValue.HasValue && !CanDecommissionDevice(ExistingDevice, Values, out errorMessage))
|
||||
return Error(errorMessage);
|
||||
|
||||
setReason = !Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedReason) ||
|
||||
(parsedValue.HasValue && string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedReason]));
|
||||
|
||||
if (ExistingDevice != null && ExistingDevice.DecommissionedDate != parsedValue)
|
||||
{
|
||||
previousValue = ExistingDevice.DecommissionedDate;
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
// Decommission or Recommission Device
|
||||
Device.DecommissionedDate = this.parsedValue;
|
||||
|
||||
if (setReason)
|
||||
Device.DecommissionReason = this.parsedValue.HasValue ? (DecommissionReasons?)DecommissionReasons.EndOfLife : null;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn &&
|
||||
h.Item1.Item1.IndexOf("decommission date", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("decommissiondate", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("decommissioned date", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("decommissioneddate", System.StringComparison.OrdinalIgnoreCase) >= 0
|
||||
);
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static bool CanDecommissionDevice(Device Device, Dictionary<DeviceImportFieldTypes, string> Values, out string ErrorMessage)
|
||||
{
|
||||
if (Device == null)
|
||||
{
|
||||
ErrorMessage = "Cannot decommission new devices";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check device is assigned (or being removed in this import)
|
||||
|
||||
if ((!Values.ContainsKey(DeviceImportFieldTypes.AssignedUserId) && Device.AssignedUserId != null) ||
|
||||
(Values.ContainsKey(DeviceImportFieldTypes.AssignedUserId) && !string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.AssignedUserId])))
|
||||
{
|
||||
if (Device.AssignedUserId != null)
|
||||
ErrorMessage = string.Format("The device is assigned to a user ({0} [{1}]) and cannot be decommissioned", Device.AssignedUser.DisplayName, Device.AssignedUser.UserId);
|
||||
else
|
||||
ErrorMessage = string.Format("The device is being assigned to a user ({0}) and cannot be decommissioned", Values[DeviceImportFieldTypes.AssignedUserId]);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check device doesn't have any open jobs
|
||||
var openJobCount = Device.Jobs.Count(j => !j.ClosedDate.HasValue);
|
||||
if (openJobCount > 0)
|
||||
{
|
||||
ErrorMessage = string.Format("The device is associated with {0} open job{1} and cannot be decommissioned", openJobCount, openJobCount == 1 ? null : "s");
|
||||
return false;
|
||||
}
|
||||
|
||||
ErrorMessage = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DeviceDecommissionedReasonImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string rawValue;
|
||||
private DecommissionReasons? parsedValue;
|
||||
private DecommissionReasons? previousValue;
|
||||
private bool setDate { get; set; }
|
||||
private static Lazy<Dictionary<string, DecommissionReasons>> decommissionReasonsMap = new Lazy<Dictionary<string, DecommissionReasons>>(() => BuildDecommissionReasonsMap());
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DeviceDecommissionedReason; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue.HasValue ? parsedValue.Value.ToString() : rawValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue.HasValue ? previousValue.Value.ToString() : null; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
{
|
||||
rawValue = null;
|
||||
parsedValue = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
DecommissionReasons valueReason;
|
||||
if (!decommissionReasonsMap.Value.TryGetValue(Value.Trim(), out valueReason))
|
||||
{
|
||||
rawValue = Value.Trim();
|
||||
return Error("Cannot parse the value as a Decommission Reason");
|
||||
}
|
||||
else
|
||||
{
|
||||
parsedValue = valueReason;
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedValue.HasValue && !Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedDate))
|
||||
{
|
||||
string errorMessage;
|
||||
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(ExistingDevice, Values, out errorMessage))
|
||||
return Error(errorMessage);
|
||||
|
||||
setDate = true;
|
||||
}
|
||||
else if (parsedValue.HasValue && string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedDate]))
|
||||
{
|
||||
setDate = true;
|
||||
}
|
||||
|
||||
if (ExistingDevice != null && ExistingDevice.DecommissionReason != parsedValue)
|
||||
{
|
||||
previousValue = ExistingDevice.DecommissionReason;
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
// Decommission or Recommission Device
|
||||
Device.DecommissionReason = this.parsedValue;
|
||||
|
||||
if (setDate)
|
||||
Device.DecommissionedDate = this.parsedValue.HasValue ? (DateTime?)DateTime.Now : null;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn &&
|
||||
h.Item1.Item1.IndexOf("decommission reason", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("decommissionreason", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("decommissioned reason", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("decommissionedreason", System.StringComparison.OrdinalIgnoreCase) >= 0
|
||||
);
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static Dictionary<string, DecommissionReasons> BuildDecommissionReasonsMap()
|
||||
{
|
||||
return Enum.GetValues(typeof(DecommissionReasons)).Cast<DecommissionReasons>().SelectMany(r =>
|
||||
{
|
||||
var rCamelName = r.ToString();
|
||||
var rName = string.Join(string.Empty, rCamelName.SelectMany(c => char.IsUpper(c) ? new char[] { ' ', c } : new char[] { c })).Trim();
|
||||
if (rCamelName == rName)
|
||||
return new Tuple<DecommissionReasons, string>[] { Tuple.Create(r, rCamelName) };
|
||||
else
|
||||
return new Tuple<DecommissionReasons, string>[] { Tuple.Create(r, rCamelName), Tuple.Create(r, rName) };
|
||||
}).ToDictionary(k => k.Item2, v => v.Item1, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DeviceLocationImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string parsedValue;
|
||||
private string previousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DeviceLocation; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
parsedValue = null;
|
||||
else
|
||||
{
|
||||
parsedValue = Value.Trim();
|
||||
if (parsedValue.Length > 250)
|
||||
return Error("Cannot be more than 250 characters");
|
||||
}
|
||||
|
||||
if (ExistingDevice == null && parsedValue != null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null && ExistingDevice.Location != parsedValue)
|
||||
{
|
||||
previousValue = ExistingDevice.Location;
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
Device.Location = this.parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && h.Item1.Item1.IndexOf("location", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class DeviceSerialNumberImportField : DeviceImportFieldBase
|
||||
{
|
||||
private const int maxLength = 60;
|
||||
private string parsedValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DeviceSerialNumber; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return parsedValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
// Validate
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
return Error("The Device Serial Number is required");
|
||||
else
|
||||
{
|
||||
parsedValue = Value.Trim();
|
||||
if (parsedValue.Length > maxLength)
|
||||
return Error(string.Format("Cannot be more than {0} characters", maxLength));
|
||||
}
|
||||
|
||||
// Duplicate
|
||||
var duplicate = Context.RawData
|
||||
.Take(RecordIndex)
|
||||
.Select((r, i) => Tuple.Create(i, ParseRawDeviceSerialNumber(r[Context.HeaderDeviceSerialNumberIndex])))
|
||||
.Where(r => IsDeviceSerialNumberValid(r.Item2))
|
||||
.FirstOrDefault(r => r.Item2.Equals(parsedValue, StringComparison.OrdinalIgnoreCase));
|
||||
if (duplicate != null)
|
||||
return Error(string.Format("This Device Serial Number was already present on Row {0}", duplicate.Item1 + 1));
|
||||
|
||||
// No action required
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
// Do nothing for Device Serial Number
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// 'serial' in column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && h.Item1.Item1.IndexOf("serial", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
// All Values
|
||||
possibleColumns = possibleColumns.Where(h =>
|
||||
{
|
||||
return Context.RawData.Select(v => v[h.Item2]).All(v => !string.IsNullOrWhiteSpace(v));
|
||||
}).ToList();
|
||||
|
||||
if (possibleColumns.Count() > 1)
|
||||
{
|
||||
// Hunt Database
|
||||
var possibleColumnIndex = possibleColumns.Where(h =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var top50SerialNumbers = Context.RawData.Select(v => v[h.Item2]).Take(50).ToArray();
|
||||
return Database.Devices.Count(d => top50SerialNumbers.Contains(d.SerialNumber)) > 0;
|
||||
}
|
||||
catch (Exception) { return false; }
|
||||
}).Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
|
||||
if (possibleColumnIndex.HasValue)
|
||||
return possibleColumnIndex;
|
||||
}
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static string ParseRawDeviceSerialNumber(string DeviceSerialNumber)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(DeviceSerialNumber))
|
||||
return null;
|
||||
else
|
||||
return DeviceSerialNumber.Trim();
|
||||
}
|
||||
public static bool IsDeviceSerialNumberValid(string DeviceSerialNumber)
|
||||
{
|
||||
return DeviceSerialNumber != null && DeviceSerialNumber.Length <= maxLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class ModelIdImportField : DeviceImportFieldBase
|
||||
{
|
||||
private int parsedValue;
|
||||
private string friendlyValue;
|
||||
private string friendlyPreviousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.ModelId; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return friendlyValue; } }
|
||||
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
friendlyValue = Value;
|
||||
|
||||
// Validate
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
this.parsedValue = 1; // Default Model
|
||||
else
|
||||
if (!int.TryParse(Value, out parsedValue))
|
||||
return Error("The Model Identifier must be a number");
|
||||
|
||||
var m = Cache.DeviceModels.FirstOrDefault(dm => dm.Id == parsedValue);
|
||||
|
||||
if (m == null)
|
||||
return Error(string.Format("The identifier ({0}) does not match any Device Model", Value));
|
||||
|
||||
friendlyValue = string.Format("{0} [{1}]", m.Description, m.Id);
|
||||
|
||||
if (ExistingDevice == null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null && ExistingDevice.DeviceModelId != parsedValue)
|
||||
{
|
||||
friendlyPreviousValue = null;
|
||||
if (ExistingDevice.DeviceModelId.HasValue)
|
||||
{
|
||||
var previousModel = Cache.DeviceModels.FirstOrDefault(dm => dm.Id == ExistingDevice.DeviceModelId.Value);
|
||||
friendlyPreviousValue = string.Format("{0} [{1}]", previousModel.Description, previousModel.Id);
|
||||
}
|
||||
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
Device.DeviceModelId = this.parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// 'model' in column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && h.Item1.Item1.IndexOf("model", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
// All Integers Numbers
|
||||
possibleColumns = possibleColumns.Where(h =>
|
||||
{
|
||||
int lastValue;
|
||||
return Context.RawData.Select(v => v[h.Item2]).Take(100).Where(v => !string.IsNullOrWhiteSpace(v)).All(v => int.TryParse(v, out lastValue));
|
||||
}).ToList();
|
||||
|
||||
// Multiple Columns, tighten column definition
|
||||
if (possibleColumns.Count() > 1)
|
||||
{
|
||||
possibleColumns = possibleColumns
|
||||
.Where(h =>
|
||||
h.Item1.Item1.IndexOf("modelid", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("model id", StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
}
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Devices.Importing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace Disco.Services.Devices.Importing.Fields
|
||||
{
|
||||
internal class ProfileIdImportField : DeviceImportFieldBase
|
||||
{
|
||||
private int parsedValue;
|
||||
private string friendlyValue;
|
||||
private string friendlyPreviousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.ProfileId; } }
|
||||
|
||||
public override object RawParsedValue { get { return parsedValue; } }
|
||||
public override string FriendlyValue { get { return friendlyValue; } }
|
||||
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, DeviceImportContext Context, int RecordIndex, string DeviceSerialNumber, Device ExistingDevice, Dictionary<DeviceImportFieldTypes, string> Values, string Value)
|
||||
{
|
||||
friendlyValue = Value;
|
||||
|
||||
// Validate
|
||||
if (string.IsNullOrWhiteSpace(Value))
|
||||
this.parsedValue = Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId;
|
||||
else
|
||||
if (!int.TryParse(Value, out parsedValue))
|
||||
return Error("The Profile Identifier must be a number");
|
||||
|
||||
var p = Cache.DeviceProfiles.FirstOrDefault(dp => dp.Id == parsedValue);
|
||||
|
||||
if (p == null)
|
||||
return Error(string.Format("The identifier ({0}) does not match any Device Profile", Value));
|
||||
|
||||
friendlyValue = string.Format("{0} [{1}]", p.Description, p.Id);
|
||||
|
||||
if (ExistingDevice == null)
|
||||
return Success(EntityState.Added);
|
||||
else if (ExistingDevice != null && ExistingDevice.DeviceProfileId != parsedValue)
|
||||
{
|
||||
var previousProfile = Cache.DeviceProfiles.FirstOrDefault(dp => dp.Id == ExistingDevice.DeviceProfileId);
|
||||
friendlyPreviousValue = string.Format("{0} [{1}]", previousProfile.Description, previousProfile.Id);
|
||||
|
||||
return Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
return Success(EntityState.Unchanged);
|
||||
}
|
||||
|
||||
public override bool Apply(DiscoDataContext Database, Device Device)
|
||||
{
|
||||
if (this.FieldAction == EntityState.Added ||
|
||||
this.FieldAction == EntityState.Modified)
|
||||
{
|
||||
Device.DeviceProfileId = this.parsedValue;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
|
||||
{
|
||||
// column name
|
||||
var possibleColumns = Context.Header
|
||||
.Select((h, i) => Tuple.Create(h, i))
|
||||
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn && h.Item1.Item1.IndexOf("profile", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
// All Integers Numbers
|
||||
possibleColumns = possibleColumns.Where(h =>
|
||||
{
|
||||
int lastValue;
|
||||
return Context.RawData.Select(v => v[h.Item2]).Take(100).Where(v => !string.IsNullOrWhiteSpace(v)).All(v => int.TryParse(v, out lastValue));
|
||||
}).ToList();
|
||||
|
||||
// Multiple Columns, tighten column definition
|
||||
if (possibleColumns.Count() > 1)
|
||||
{
|
||||
possibleColumns = possibleColumns
|
||||
.Where(h =>
|
||||
h.Item1.Item1.IndexOf("profileid", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
h.Item1.Item1.IndexOf("profile id", StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
}
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Disco.Models.Repository;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Disco.Services.Devices.Importing
|
||||
{
|
||||
internal interface IDeviceImportCache
|
||||
{
|
||||
Device FindDevice(string DeviceSerialNumber);
|
||||
|
||||
IEnumerable<Device> Devices { get; }
|
||||
IEnumerable<DeviceModel> DeviceModels { get; }
|
||||
IEnumerable<DeviceProfile> DeviceProfiles { get; }
|
||||
IEnumerable<DeviceBatch> DeviceBatches { get; }
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,9 @@
|
||||
<Reference Include="EntityFramework">
|
||||
<HintPath>..\packages\EntityFramework.5.0.0\lib\net45\EntityFramework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="LumenWorks.Framework.IO">
|
||||
<HintPath>..\packages\LumenWorks.Framework.IO.3.8.0\lib\net20\LumenWorks.Framework.IO.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.AspNet.SignalR.Core, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Microsoft.AspNet.SignalR.Core.1.1.2\lib\net40\Microsoft.AspNet.SignalR.Core.dll</HintPath>
|
||||
@@ -175,6 +178,27 @@
|
||||
<Compile Include="Devices\Exporting\DeviceExport.cs" />
|
||||
<Compile Include="Devices\Exporting\DeviceExportTask.cs" />
|
||||
<Compile Include="Devices\Exporting\DeviceExportTaskContext.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImport.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImportApplyTask.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImportContext.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImportDatabaseCache.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImportFieldBase.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImportInMemoryCache.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImportParseTask.cs" />
|
||||
<Compile Include="Devices\Importing\DeviceImportRecord.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\BatchIdImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\AssignedUserIdImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DeviceDecommissionedReasonImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DeviceDecommissionedDateImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\ProfileIdImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DetailACAdapterImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DetailWLanMacAddressImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DetailLanMacAddressImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DeviceLocationImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DeviceAssetNumberImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DeviceSerialNumberImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\ModelIdImportField.cs" />
|
||||
<Compile Include="Devices\Importing\IDeviceImportCache.cs" />
|
||||
<Compile Include="Extensions\DateTimeExtensions.cs" />
|
||||
<Compile Include="Extensions\StringExtensions.cs" />
|
||||
<Compile Include="Interop\ActiveDirectory\ActiveDirectory.cs" />
|
||||
@@ -298,9 +322,7 @@
|
||||
<ItemGroup>
|
||||
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Devices\Importing\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
|
||||
<package id="LumenWorks.Framework.IO" version="3.8.0" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.SignalR.Core" version="1.1.2" targetFramework="net45" />
|
||||
|
||||
Reference in New Issue
Block a user