e9042f7666
Dynamic device importing. better input parsing and 5 additional import fields.
69 lines
2.6 KiB
C#
69 lines
2.6 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|