Merge pull request #95 from ahmedshash/master
Fix for ability to import keyboard in device import #94
This commit is contained in:
@@ -50,6 +50,8 @@ namespace Disco.Models.Services.Devices.Exporting
|
||||
public bool DetailACAdapter { get; set; }
|
||||
[Display(ShortName = "Details", Name = "Battery", Description = "The Battery associated with the device")]
|
||||
public bool DetailBattery { get; set; }
|
||||
[Display(ShortName = "Details", Name = "Keyboard", Description = "The Keyboard associated with the device")]
|
||||
public bool DetailKeyboard { get; set; }
|
||||
|
||||
// Model
|
||||
[Display(ShortName = "Model", Name = "Identifier", Description = "The identifier of the device model associated with the device")]
|
||||
|
||||
@@ -30,6 +30,8 @@ namespace Disco.Models.Services.Devices.Importing
|
||||
DetailACAdapter,
|
||||
[Display(Name = "Device Battery", Description = "The Battery associated with the device")]
|
||||
DetailBattery,
|
||||
[Display(Name = "Device Keyboard", Description = "The Keyboard associated with the device")]
|
||||
DetailKeyboard,
|
||||
|
||||
[Display(Name = "Model Identifier", Description = "The identifier of the device model associated with the device")]
|
||||
ModelId,
|
||||
|
||||
@@ -143,7 +143,8 @@ namespace Disco.Services.Devices.Exporting
|
||||
DeviceDetail.HardwareKeyLanMacAddress,
|
||||
DeviceDetail.HardwareKeyWLanMacAddress,
|
||||
DeviceDetail.HardwareKeyACAdapter,
|
||||
DeviceDetail.HardwareKeyBattery
|
||||
DeviceDetail.HardwareKeyBattery,
|
||||
DeviceDetail.HardwareKeyKeyboard
|
||||
};
|
||||
|
||||
return Devices.Select(d => new DeviceExportRecord()
|
||||
@@ -225,6 +226,7 @@ namespace Disco.Services.Devices.Exporting
|
||||
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DetailWLanMacAddress", r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyWLanMacAddress).Select(dd => dd.Value).FirstOrDefault(), true);
|
||||
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DetailACAdapter", r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyACAdapter).Select(dd => dd.Value).FirstOrDefault(), true);
|
||||
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DetailBattery", r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyBattery).Select(dd => dd.Value).FirstOrDefault(), true);
|
||||
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DetailKeyboard", r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyKeyboard).Select(dd => dd.Value).FirstOrDefault(), true);
|
||||
|
||||
// Model
|
||||
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ModelId", r => r.ModelId.HasValue ? r.ModelId.Value.ToString() : null, false);
|
||||
|
||||
@@ -32,6 +32,7 @@ namespace Disco.Services.Devices.Importing
|
||||
{ DeviceImportFieldTypes.DetailWLanMacAddress, typeof(DetailWLanMacAddressImportField) },
|
||||
{ DeviceImportFieldTypes.DetailACAdapter, typeof(DetailACAdapterImportField) },
|
||||
{ DeviceImportFieldTypes.DetailBattery, typeof(DetailBatteryImportField) },
|
||||
{ DeviceImportFieldTypes.DetailKeyboard, typeof(DetailKeyboardImportField) },
|
||||
|
||||
{ DeviceImportFieldTypes.ModelId, typeof(ModelIdImportField) },
|
||||
|
||||
|
||||
@@ -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 DetailKeyboardImportField : DeviceImportFieldBase
|
||||
{
|
||||
private string parsedValue;
|
||||
private string previousValue;
|
||||
|
||||
public override DeviceImportFieldTypes FieldType { get { return DeviceImportFieldTypes.DetailKeyboard; } }
|
||||
|
||||
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.HardwareKeyKeyboard);
|
||||
|
||||
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.HardwareKeyKeyboard);
|
||||
|
||||
if (detail == null)
|
||||
{
|
||||
detail = new DeviceDetail()
|
||||
{
|
||||
Device = Device,
|
||||
DeviceSerialNumber = Device.SerialNumber,
|
||||
Scope = DeviceDetail.ScopeHardware,
|
||||
Key = DeviceDetail.HardwareKeyKeyboard
|
||||
};
|
||||
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("keyboard", System.StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
|
||||
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,6 +207,7 @@
|
||||
<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\DetailKeyboardImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DeviceAllowUnauthenticatedEnrolImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DetailBatteryImportField.cs" />
|
||||
<Compile Include="Devices\Importing\Fields\DeviceDecommissionedReasonImportField.cs" />
|
||||
|
||||
Reference in New Issue
Block a user