Feature #33: Enhanced Device Importing
Dynamic device importing. better input parsing and 5 additional import fields.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user