5ce9e51ae7
Microsoft Excel files can be used to import/export devices. Several import bugs were also fixed in the process.
71 lines
2.6 KiB
C#
71 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, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
|
{
|
|
var value = DataReader.GetString(ColumnIndex);
|
|
|
|
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 (FieldAction == EntityState.Added ||
|
|
FieldAction == EntityState.Modified)
|
|
{
|
|
Device.Location = parsedValue;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
|
|
{
|
|
// column name
|
|
var possibleColumns = Context.Columns
|
|
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
|
|
h.Name.IndexOf("location", StringComparison.OrdinalIgnoreCase) >= 0);
|
|
|
|
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
|
|
}
|
|
}
|
|
}
|