Feature: MS Excel (xlsx) Import/Export

Microsoft Excel files can be used to import/export devices. Several
import bugs were also fixed in the process.
This commit is contained in:
Gary Sharp
2017-03-25 15:37:28 +11:00
parent ed66f4f285
commit 5ce9e51ae7
51 changed files with 1959 additions and 1083 deletions
+3
View File
@@ -60,6 +60,9 @@
<Compile Include="ClientServices\EnrolmentInformation\WirelessProfile.cs" />
<Compile Include="ClientServices\EnrolmentInformation\WirelessProfileStore.cs" />
<Compile Include="ClientServices\EnrolmentInformation\WirelessProfileTransformation.cs" />
<Compile Include="Services\Devices\Exporting\DeviceExportFieldMetadata.cs" />
<Compile Include="Services\Devices\Importing\IDeviceImportColumn.cs" />
<Compile Include="Services\Devices\Importing\IDeviceImportDataReader.cs" />
<Compile Include="Services\Documents\DocumentTemplatePackage.cs" />
<Compile Include="Services\Jobs\LocationModes.cs" />
<Compile Include="ClientServices\EnrolmentInformation\Certificate.cs" />
@@ -0,0 +1,21 @@
using System;
namespace Disco.Models.Services.Devices.Exporting
{
public class DeviceExportFieldMetadata
{
public string Name { get; set; }
public string ColumnName { get; set; }
public Type ValueType { get; set; }
public Func<DeviceExportRecord, object> Accessor { get; set; }
public Func<object, string> CsvEncoder { get; set; }
public DeviceExportFieldMetadata(string Name, Type ValueType, Func<DeviceExportRecord, object> Accessor, Func<object, string> CsvEncoder)
{
this.Name = Name;
this.ValueType = ValueType;
this.Accessor = Accessor;
this.CsvEncoder = CsvEncoder;
}
}
}
@@ -12,10 +12,7 @@ namespace Disco.Models.Services.Devices.Exporting
public DeviceExportTypes ExportType { get; set; }
public int? ExportTypeTargetId { get; set; }
/// <summary>
/// Adds '=' to the beginning of the string to stop Excel removing the leading zeros
/// </summary>
public bool ExcelCsvFormat { get; set; }
public bool ExcelFormat { get; set; }
// Device
[Display(ShortName = "Device", Name = "Serial Number", Description = "The device serial number")]
@@ -135,7 +132,7 @@ namespace Disco.Models.Services.Devices.Exporting
return new DeviceExportOptions()
{
ExportType = DeviceExportTypes.All,
ExcelCsvFormat = true,
ExcelFormat = true,
DeviceSerialNumber = true,
ModelId = true,
ProfileId = true,
@@ -4,7 +4,7 @@ namespace Disco.Models.Services.Devices.Exporting
{
public class DeviceExportResult
{
public MemoryStream CsvResult { get; set; }
public MemoryStream Result { get; set; }
public int RecordCount { get; set; }
}
}
@@ -0,0 +1,14 @@
using System;
namespace Disco.Models.Services.Devices.Importing
{
public interface IDeviceImportColumn
{
int Index { get; }
string Name { get; }
DeviceImportFieldTypes Type { get; }
Type Handler { get; }
IDeviceImportField GetHandlerInstance();
}
}
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Models.Services.Devices.Importing
{
@@ -10,11 +7,19 @@ namespace Disco.Models.Services.Devices.Importing
{
string SessionId { get; }
string Filename { get; }
List<Tuple<string, DeviceImportFieldTypes>> Header { get; }
List<Tuple<string, DeviceImportFieldTypes, Func<string[], string>, Type>> ParsedHeaders { get; }
List<string[]> RawData { get; }
string DatasetName { get; }
int ColumnCount { get; }
IEnumerable<IDeviceImportColumn> Columns { get; }
IDeviceImportColumn GetColumn(int Index);
void SetColumnType(int Index, DeviceImportFieldTypes Type);
int? GetColumnByType(DeviceImportFieldTypes FieldType);
List<IDeviceImportRecord> Records { get; }
int AffectedRecords { get; }
int RecordCount { get; }
IDeviceImportDataReader GetDataReader();
IEnumerable<KeyValuePair<DeviceImportFieldTypes, Type>> GetFieldHandlers();
List<IDeviceImportRecord> Records { get; set; }
int AffectedRecords { get; set; }
}
}
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Models.Services.Devices.Importing
{
public interface IDeviceImportDataReader : IDisposable
{
void Reset();
bool Read();
int Index { get; }
int GetRowNumber(int Index);
string GetString(int ColumnIndex);
IEnumerable<string> GetStrings(int ColumnIndex);
bool TryGetNullableInt(int ColumnIndex, out int? value);
bool TryGetNullableBool(int ColumnIndex, out bool? value);
bool TryGetNullableDateTime(int ColumnIndex, out DateTime? value);
bool TestAllNotEmpty(int ColumnIndex);
bool TestAllNullableInt(int ColumnIndex);
bool TestAllInt(int ColumnIndex);
bool TestAllNullableBool(int ColumnIndex);
bool TestAllNullableDateTime(int ColumnIndex);
}
}
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace Disco.Models.Services.Devices.Importing
{
@@ -9,6 +9,7 @@ namespace Disco.Models.Services.Devices.Importing
{
public interface IDeviceImportRecord
{
int Index { get; }
string DeviceSerialNumber { get; }
IEnumerable<IDeviceImportField> Fields { get; }
+130 -86
View File
@@ -1,4 +1,5 @@
using Disco.Data.Repository;
using ClosedXML.Excel;
using Disco.Data.Repository;
using Disco.Models.Repository;
using Disco.Models.Services.Devices.Exporting;
using Disco.Services.Tasks;
@@ -6,10 +7,10 @@ using Disco.Services.Users;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Services.Devices.Exporting
{
@@ -52,7 +53,7 @@ namespace Disco.Services.Devices.Exporting
try
{
TaskStatus.IgnoreCurrentProcessChanges = true;
TaskStatus.ProgressMultiplier = 20 / 100;
TaskStatus.ProgressMultiplier = (double)20 / 100;
TaskStatus.ProgressOffset = 40;
Interop.ActiveDirectory.ADNetworkLogonDatesUpdateTask.UpdateLastNetworkLogonDates(Database, TaskStatus);
@@ -73,41 +74,23 @@ namespace Disco.Services.Devices.Exporting
TaskStatus.UpdateStatus(80, string.Format("Formatting {0} records for export", records.Count));
using (StreamWriter writer = new StreamWriter(stream, Encoding.Default, 0x400, true))
if (Options.ExcelFormat)
{
// Header
writer.Write('"');
writer.Write(string.Join("\",\"", metadata.Select(m => m.Item2)));
writer.Write('"');
// Records
foreach (var record in records)
{
writer.WriteLine();
writer.Write(string.Join(",", metadata.Select(m =>
{
var value = m.Item3(record);
var isString = m.Item4;
if (value == null)
return null;
else if (!isString)
return value;
else if (Options.ExcelCsvFormat)
return string.Concat("=\"", value, "\"");
else
return string.Concat("\"", value, "\"");
})));
}
WriteXlsx(stream, metadata, records);
}
else
{
WriteCSV(stream, metadata, records);
}
stream.Position = 0;
return new DeviceExportResult()
{
CsvResult = stream,
Result = stream,
RecordCount = records.Count
};
}
public static DeviceExportResult GenerateExport(DiscoDataContext Database, IQueryable<Device> Devices, DeviceExportOptions Options)
{
return GenerateExport(Database, Devices, Options, ScheduledTaskMockStatus.Create("Device Export"));
@@ -137,6 +120,57 @@ namespace Disco.Services.Devices.Exporting
return GenerateExport(Database, Options, ScheduledTaskMockStatus.Create("Device Export"));
}
private static void WriteCSV(Stream OutputStream, List<DeviceExportFieldMetadata> Metadata, List<DeviceExportRecord> Records)
{
using (StreamWriter writer = new StreamWriter(OutputStream, Encoding.Default, 0x400, true))
{
// Header
writer.Write('"');
writer.Write(string.Join("\",\"", Metadata.Select(m => m.ColumnName)));
writer.Write('"');
// Records
foreach (var record in Records)
{
writer.WriteLine();
for (int i = 0; i < Metadata.Count; i++)
{
if (i != 0)
{
writer.Write(',');
}
var value = Metadata[i].Accessor(record);
writer.Write(Metadata[i].CsvEncoder(value));
}
}
}
}
private static void WriteXlsx(Stream OutputStream, List<DeviceExportFieldMetadata> Metadata, List<DeviceExportRecord> Records)
{
// Create DataTable
var dataTable = new DataTable();
foreach (var field in Metadata)
{
dataTable.Columns.Add(field.ColumnName, field.ValueType);
}
foreach (var record in Records)
{
dataTable.Rows.Add(Metadata.Select(m => m.Accessor(record)).ToArray());
}
using (var xlWorkbook = new XLWorkbook())
{
var sheet = xlWorkbook.AddWorksheet("DeviceExport");
var table = sheet.Cell(1, 1).InsertTable(dataTable, "Devices");
table.Theme = XLTableTheme.TableStyleMedium2;
table.Columns().ForEach(c => c.WorksheetColumn().AdjustToContents(2, 15, 30));
xlWorkbook.SaveAs(OutputStream);
}
}
private static IEnumerable<DeviceExportRecord> BuildRecords(IQueryable<Device> Devices)
{
var deviceDetailHardwareKeys = new List<string> {
@@ -185,93 +219,103 @@ namespace Disco.Services.Devices.Exporting
});
}
/// <returns>Tuple Format: Property Name, Column Name, Property Access, Escape CSV Value?</returns>
private static List<Tuple<string, string, Func<DeviceExportRecord, string>, bool>> BuildMetadata(this DeviceExportOptions Options)
private static List<DeviceExportFieldMetadata> BuildMetadata(this DeviceExportOptions Options)
{
var allAssessors = BuildRecordAssessors().ToList();
var allAssessors = BuildRecordAccessors().ToList();
return typeof(DeviceExportOptions).GetProperties()
.Where(p => p.PropertyType == typeof(bool))
.Select(p => Tuple.Create(p, (DisplayAttribute)p.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault()))
.Where(p => p.Item2 != null && (bool)p.Item1.GetValue(Options))
.Select(p => new
{
property = p,
details = (DisplayAttribute)p.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault()
})
.Where(p => p.details != null && (bool)p.property.GetValue(Options))
.Select(p =>
{
var accessor = allAssessors.First(i => i.Item1 == p.Item1.Name);
var columnName = (p.Item2.ShortName == "Device" || p.Item2.ShortName == "Details") ? p.Item2.Name : string.Format("{0} {1}", p.Item2.ShortName, p.Item2.Name);
return Tuple.Create(p.Item1.Name, columnName, accessor.Item2, accessor.Item3);
var fieldMetadata = allAssessors.First(i => i.Name == p.property.Name);
fieldMetadata.ColumnName = (p.details.ShortName == "Device" || p.details.ShortName == "Details") ? p.details.Name : $"{p.details.ShortName} {p.details.Name}";
return fieldMetadata;
}).ToList();
}
/// <returns>Tuple Format: Property Name, Property Access, Escape CSV Value?</returns>
private static IEnumerable<Tuple<string, Func<DeviceExportRecord, string>, bool>> BuildRecordAssessors()
private static IEnumerable<DeviceExportFieldMetadata> BuildRecordAccessors()
{
const string DateFormat = "yyyy-MM-dd";
const string DateTimeFormat = DateFormat + " HH:mm:ss";
Func<object, string> csvStringEncoded = (o) => o == null ? null : $"\"{((string)o).Replace("\"", "\"\"")}\"";
Func<object, string> csvToStringEncoded = (o) => o == null ? null : o.ToString();
Func<object, string> csvCurrencyEncoded = (o) => ((decimal?)o).HasValue ? ((decimal?)o).Value.ToString("C") : null;
Func<object, string> csvDateEncoded = (o) => ((DateTime)o).ToString(DateFormat);
Func<object, string> csvDateTimeEncoded = (o) => ((DateTime)o).ToString(DateTimeFormat);
Func<object, string> csvNullableDateEncoded = (o) => ((DateTime?)o).HasValue ? csvDateEncoded(o) : null;
Func<object, string> csvNullableDateTimeEncoded = (o) => ((DateTime?)o).HasValue ? csvDateTimeEncoded(o) : null;
// Device
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceSerialNumber", r => r.Device.SerialNumber, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceAssetNumber", r => r.Device.AssetNumber, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceLocation", r => r.Device.Location, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceComputerName", r => r.Device.DeviceDomainId, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceLastNetworkLogon", r => r.Device.LastNetworkLogonDate.HasValue ? r.Device.LastNetworkLogonDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceCreatedDate", r => r.Device.CreatedDate.ToString(DateTimeFormat), false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceFirstEnrolledDate", r => r.Device.EnrolledDate.HasValue ? r.Device.EnrolledDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceLastEnrolledDate", r => r.Device.LastEnrolDate.HasValue ? r.Device.LastEnrolDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceAllowUnauthenticatedEnrol", r => r.Device.AllowUnauthenticatedEnrol.ToString(), false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceDecommissionedDate", r => r.Device.DecommissionedDate.HasValue ? r.Device.DecommissionedDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DeviceDecommissionedReason", r => r.Device.DecommissionReason.HasValue ? r.Device.DecommissionReason.Value.ToString() : null, true);
yield return new DeviceExportFieldMetadata("DeviceSerialNumber", typeof(string), r => r.Device.SerialNumber, csvStringEncoded);
yield return new DeviceExportFieldMetadata("DeviceAssetNumber", typeof(string), r => r.Device.AssetNumber, csvStringEncoded);
yield return new DeviceExportFieldMetadata("DeviceLocation", typeof(string), r => r.Device.Location, csvStringEncoded);
yield return new DeviceExportFieldMetadata("DeviceComputerName", typeof(string), r => r.Device.DeviceDomainId, csvStringEncoded);
yield return new DeviceExportFieldMetadata("DeviceLastNetworkLogon", typeof(DateTime), r => r.Device.LastNetworkLogonDate, csvNullableDateTimeEncoded);
yield return new DeviceExportFieldMetadata("DeviceCreatedDate", typeof(DateTime), r => r.Device.CreatedDate, csvDateTimeEncoded);
yield return new DeviceExportFieldMetadata("DeviceFirstEnrolledDate", typeof(DateTime), r => r.Device.EnrolledDate, csvNullableDateTimeEncoded);
yield return new DeviceExportFieldMetadata("DeviceLastEnrolledDate", typeof(DateTime), r => r.Device.LastEnrolDate, csvNullableDateTimeEncoded);
yield return new DeviceExportFieldMetadata("DeviceAllowUnauthenticatedEnrol", typeof(bool), r => r.Device.AllowUnauthenticatedEnrol, csvToStringEncoded);
yield return new DeviceExportFieldMetadata("DeviceDecommissionedDate", typeof(DateTime), r => r.Device.DecommissionedDate, csvNullableDateTimeEncoded);
yield return new DeviceExportFieldMetadata("DeviceDecommissionedReason", typeof(string), r => r.Device.DecommissionReason, csvToStringEncoded);
// Details
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("DetailLanMacAddress", r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyLanMacAddress).Select(dd => dd.Value).FirstOrDefault(), true);
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);
yield return new DeviceExportFieldMetadata("DetailLanMacAddress", typeof(string), r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyLanMacAddress).Select(dd => dd.Value).FirstOrDefault(), csvStringEncoded);
yield return new DeviceExportFieldMetadata("DetailWLanMacAddress", typeof(string), r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyWLanMacAddress).Select(dd => dd.Value).FirstOrDefault(), csvStringEncoded);
yield return new DeviceExportFieldMetadata("DetailACAdapter", typeof(string), r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyACAdapter).Select(dd => dd.Value).FirstOrDefault(), csvStringEncoded);
yield return new DeviceExportFieldMetadata("DetailBattery", typeof(string), r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyBattery).Select(dd => dd.Value).FirstOrDefault(), csvStringEncoded);
yield return new DeviceExportFieldMetadata("DetailKeyboard", typeof(string), r => r.DeviceDetails.Where(dd => dd.Key == DeviceDetail.HardwareKeyKeyboard).Select(dd => dd.Value).FirstOrDefault(), csvStringEncoded);
// Model
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ModelId", r => r.ModelId.HasValue ? r.ModelId.Value.ToString() : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ModelDescription", r => r.ModelDescription, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ModelManufacturer", r => r.ModelManufacturer, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ModelModel", r => r.ModelModel, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ModelType", r => r.ModelType, true);
yield return new DeviceExportFieldMetadata("ModelId", typeof(int), r => r.ModelId, csvToStringEncoded);
yield return new DeviceExportFieldMetadata("ModelDescription", typeof(string), r => r.ModelDescription, csvStringEncoded);
yield return new DeviceExportFieldMetadata("ModelManufacturer", typeof(string), r => r.ModelManufacturer, csvStringEncoded);
yield return new DeviceExportFieldMetadata("ModelModel", typeof(string), r => r.ModelModel, csvStringEncoded);
yield return new DeviceExportFieldMetadata("ModelType", typeof(string), r => r.ModelType, csvStringEncoded);
// Batch
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchId", r => r.BatchId.HasValue ? r.BatchId.Value.ToString() : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchName", r => r.BatchName, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchPurchaseDate", r => r.BatchPurchaseDate.HasValue ? r.BatchPurchaseDate.Value.ToString(DateFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchSupplier", r => r.BatchSupplier, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchUnitCost", r => r.BatchUnitCost.HasValue ? r.BatchUnitCost.ToString() : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchWarrantyValidUntilDate", r => r.BatchWarrantyValidUntilDate.HasValue ? r.BatchWarrantyValidUntilDate.Value.ToString(DateFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchInsuredDate", r => r.BatchInsuredDate.HasValue ? r.BatchInsuredDate.Value.ToString(DateFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchInsuranceSupplier", r => r.BatchInsuranceSupplier, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("BatchInsuredUntilDate", r => r.BatchInsuredUntilDate.HasValue ? r.BatchInsuredUntilDate.Value.ToString(DateFormat) : null, false);
yield return new DeviceExportFieldMetadata("BatchId", typeof(int), r => r.BatchId, csvToStringEncoded);
yield return new DeviceExportFieldMetadata("BatchName", typeof(string), r => r.BatchName, csvStringEncoded);
yield return new DeviceExportFieldMetadata("BatchPurchaseDate", typeof(DateTime), r => r.BatchPurchaseDate, csvNullableDateEncoded);
yield return new DeviceExportFieldMetadata("BatchSupplier", typeof(string), r => r.BatchSupplier, csvStringEncoded);
yield return new DeviceExportFieldMetadata("BatchUnitCost", typeof(decimal), r => r.BatchUnitCost, csvCurrencyEncoded);
yield return new DeviceExportFieldMetadata("BatchWarrantyValidUntilDate", typeof(DateTime), r => r.BatchWarrantyValidUntilDate, csvNullableDateEncoded);
yield return new DeviceExportFieldMetadata("BatchInsuredDate", typeof(DateTime), r => r.BatchInsuredDate, csvNullableDateEncoded);
yield return new DeviceExportFieldMetadata("BatchInsuranceSupplier", typeof(string), r => r.BatchInsuranceSupplier, csvStringEncoded);
yield return new DeviceExportFieldMetadata("BatchInsuredUntilDate", typeof(DateTime), r => r.BatchInsuredUntilDate, csvNullableDateEncoded);
// Profile
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ProfileId", r => r.ProfileId.ToString(), false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ProfileName", r => r.ProfileName, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("ProfileShortName", r => r.ProfileShortName, true);
yield return new DeviceExportFieldMetadata("ProfileId", typeof(int), r => r.ProfileId, csvToStringEncoded);
yield return new DeviceExportFieldMetadata("ProfileName", typeof(string), r => r.ProfileName, csvStringEncoded);
yield return new DeviceExportFieldMetadata("ProfileShortName", typeof(string), r => r.ProfileShortName, csvStringEncoded);
// User
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AssignedUserId", r => r.AssignedUser != null ? r.AssignedUser.UserId : null, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AssignedUserDate", r => r.DeviceUserAssignment != null ? r.DeviceUserAssignment.AssignedDate.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AssignedUserDisplayName", r => r.AssignedUser != null ? r.AssignedUser.DisplayName : null, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AssignedUserSurname", r => r.AssignedUser != null ? r.AssignedUser.Surname : null, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AssignedUserGivenName", r => r.AssignedUser != null ? r.AssignedUser.GivenName : null, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AssignedUserPhoneNumber", r => r.AssignedUser != null ? r.AssignedUser.PhoneNumber : null, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AssignedUserEmailAddress", r => r.AssignedUser != null ? r.AssignedUser.EmailAddress : null, true);
yield return new DeviceExportFieldMetadata("AssignedUserId", typeof(string), r => r.AssignedUser?.UserId, csvStringEncoded);
yield return new DeviceExportFieldMetadata("AssignedUserDate", typeof(DateTime), r => r.DeviceUserAssignment?.AssignedDate, csvNullableDateTimeEncoded);
yield return new DeviceExportFieldMetadata("AssignedUserDisplayName", typeof(string), r => r.AssignedUser?.DisplayName, csvStringEncoded);
yield return new DeviceExportFieldMetadata("AssignedUserSurname", typeof(string), r => r.AssignedUser?.Surname, csvStringEncoded);
yield return new DeviceExportFieldMetadata("AssignedUserGivenName", typeof(string), r => r.AssignedUser?.GivenName, csvStringEncoded);
yield return new DeviceExportFieldMetadata("AssignedUserPhoneNumber", typeof(string), r => r.AssignedUser?.PhoneNumber, csvStringEncoded);
yield return new DeviceExportFieldMetadata("AssignedUserEmailAddress", typeof(string), r => r.AssignedUser?.EmailAddress, csvStringEncoded);
// Jobs
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("JobsTotalCount", r => r.JobsTotalCount.ToString(), false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("JobsOpenCount", r => r.JobsOpenCount.ToString(), false);
yield return new DeviceExportFieldMetadata("JobsTotalCount", typeof(int), r => r.JobsTotalCount, csvToStringEncoded);
yield return new DeviceExportFieldMetadata("JobsOpenCount", typeof(int), r => r.JobsOpenCount, csvToStringEncoded);
// Attachments
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("AttachmentsCount", r => r.AttachmentsCount.ToString(), false);
yield return new DeviceExportFieldMetadata("AttachmentsCount", typeof(int), r => r.AttachmentsCount, csvToStringEncoded);
// Certificates
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("CertificateName", r => r.DeviceCertificate != null ? r.DeviceCertificate.Name : null, true);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("CertificateAllocatedDate", r => r.DeviceCertificate != null && r.DeviceCertificate.AllocatedDate.HasValue ? r.DeviceCertificate.AllocatedDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("CertificateExpirationDate", r => r.DeviceCertificate != null && r.DeviceCertificate.ExpirationDate.HasValue ? r.DeviceCertificate.ExpirationDate.Value.ToString(DateTimeFormat) : null, false);
yield return new Tuple<string, Func<DeviceExportRecord, string>, bool>("CertificateProviderId", r => r.DeviceCertificate != null ? r.DeviceCertificate.ProviderId : null, true);
yield return new DeviceExportFieldMetadata("CertificateName", typeof(string), r => r.DeviceCertificate?.Name, csvStringEncoded);
yield return new DeviceExportFieldMetadata("CertificateAllocatedDate", typeof(DateTime), r => r.DeviceCertificate?.AllocatedDate, csvNullableDateTimeEncoded);
yield return new DeviceExportFieldMetadata("CertificateExpirationDate", typeof(DateTime), r => r.DeviceCertificate?.ExpirationDate, csvNullableDateTimeEncoded);
yield return new DeviceExportFieldMetadata("CertificateProviderId", typeof(string), r => r.DeviceCertificate?.ProviderId, csvStringEncoded);
}
}
@@ -0,0 +1,142 @@
using Disco.Models.Services.Devices.Importing;
using Disco.Services.Devices.Importing.Fields;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Disco.Services.Devices.Importing
{
public abstract class BaseDeviceImportContext : IDeviceImportContext
{
private 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.DeviceAllowUnauthenticatedEnrol, typeof(DeviceAllowUnauthenticatedEnrolImportField) },
{ DeviceImportFieldTypes.DeviceDecommissionedDate, typeof(DeviceDecommissionedDateImportField) },
{ DeviceImportFieldTypes.DeviceDecommissionedReason, typeof(DeviceDecommissionedReasonImportField) },
{ DeviceImportFieldTypes.DetailLanMacAddress, typeof(DetailLanMacAddressImportField) },
{ DeviceImportFieldTypes.DetailWLanMacAddress, typeof(DetailWLanMacAddressImportField) },
{ DeviceImportFieldTypes.DetailACAdapter, typeof(DetailACAdapterImportField) },
{ DeviceImportFieldTypes.DetailBattery, typeof(DetailBatteryImportField) },
{ DeviceImportFieldTypes.DetailKeyboard, typeof(DetailKeyboardImportField) },
{ DeviceImportFieldTypes.ModelId, typeof(ModelIdImportField) },
{ DeviceImportFieldTypes.BatchId, typeof(BatchIdImportField) },
{ DeviceImportFieldTypes.ProfileId, typeof(ProfileIdImportField) },
{ DeviceImportFieldTypes.AssignedUserId, typeof(AssignedUserIdImportField) }
};
});
private List<DeviceImportColumn> columns;
private Dictionary<DeviceImportFieldTypes, DeviceImportColumn> columnsByType;
public string SessionId { get; }
public string Filename { get; }
public string DatasetName { get; private set; }
public abstract int RecordCount { get; }
public List<IDeviceImportRecord> Records { get; set; }
public int AffectedRecords { get; set; }
public int ColumnCount { get { return columns.Count; } }
public IEnumerable<IDeviceImportColumn> Columns
{
get
{
if (columns == null)
throw new ArgumentNullException(nameof(columns));
return columns;
}
}
public BaseDeviceImportContext(string Filename)
{
SessionId = Guid.NewGuid().ToString("D");
this.Filename = DatasetName = string.IsNullOrWhiteSpace(Filename) ? "<File Name Not Specified>" : Filename;
}
public abstract IDeviceImportDataReader GetDataReader();
public IDeviceImportColumn GetColumn(int Index)
{
if (columns == null)
throw new ArgumentNullException(nameof(columns));
return columns[Index];
}
public void SetColumnType(int Index, DeviceImportFieldTypes Type)
{
if (columns == null)
throw new ArgumentNullException(nameof(columns));
var column = columns[Index];
if (column.Type == Type)
{
return; // No change
}
if (column.Type != DeviceImportFieldTypes.IgnoreColumn)
{
columnsByType.Remove(column.Type);
}
column.Type = Type;
if (Type == DeviceImportFieldTypes.IgnoreColumn)
{
column.Handler = null;
}
else
{
columnsByType[Type] = column;
column.Handler = FieldHandlers.Value[Type];
}
}
public int? GetColumnByType(DeviceImportFieldTypes FieldType)
{
if (columnsByType == null)
throw new ArgumentNullException(nameof(columnsByType));
DeviceImportColumn column;
if (columnsByType.TryGetValue(FieldType, out column))
{
return column.Index;
}
else
{
return null;
}
}
protected void SetDatasetName(string DatasetName)
{
this.DatasetName = DatasetName;
}
protected void SetColumns(IEnumerable<DeviceImportColumn> Columns)
{
columns = Columns.ToList();
columnsByType = columns
.Where(c => c.Type != DeviceImportFieldTypes.IgnoreColumn)
.ToDictionary(c => c.Type);
}
public IEnumerable<KeyValuePair<DeviceImportFieldTypes, Type>> GetFieldHandlers()
{
return FieldHandlers.Value;
}
}
}
@@ -0,0 +1,56 @@
using Disco.Models.Services.Devices.Importing;
using LumenWorks.Framework.IO.Csv;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace Disco.Services.Devices.Importing
{
public class CsvDeviceImportContext : BaseDeviceImportContext
{
private List<string[]> rawData;
private bool hasHeaderRow;
public CsvDeviceImportContext(string Filename, bool HasHeader, Stream CsvStream)
: base(Filename)
{
hasHeaderRow = HasHeader;
ParseCsv(CsvStream);
}
public override int RecordCount
{
get
{
return rawData.Count;
}
}
public override IDeviceImportDataReader GetDataReader()
{
return new CsvDeviceImportDataReader(this, rawData, hasHeaderRow);
}
private void ParseCsv(Stream CsvStream)
{
using (TextReader csvTextReader = new StreamReader(CsvStream))
{
using (CsvReader csvReader = new CsvReader(csvTextReader, hasHeaderRow))
{
csvReader.DefaultParseErrorAction = ParseErrorAction.ThrowException;
csvReader.MissingFieldAction = MissingFieldAction.ReplaceByNull;
rawData = csvReader.ToList();
SetColumns(csvReader.GetFieldHeaders().Select((h, i) => new DeviceImportColumn()
{
Index = i,
Name = h,
Type = DeviceImportFieldTypes.IgnoreColumn
}));
}
}
}
}
}
@@ -0,0 +1,222 @@
using Disco.Models.Services.Devices.Importing;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Disco.Services.Devices.Importing
{
public class CsvDeviceImportDataReader : IDeviceImportDataReader
{
private static string[] TrueValues = { "true", "1", "yes", "-1", "on" };
private static string[] FalseValues = { "false", "0", "no", "off" };
private CsvDeviceImportContext context;
private List<string[]> rawData;
private int currentRowIndex;
private int rowOffset;
private string[] currentRow;
public int Index { get { return currentRowIndex; } }
public CsvDeviceImportDataReader(CsvDeviceImportContext Context, List<string[]> RawData, bool HasHeaderRow)
{
context = Context;
rawData = RawData;
currentRowIndex = 0;
rowOffset = HasHeaderRow ? 1 : 0;
}
public void Reset()
{
currentRowIndex = 0;
currentRow = null;
}
public bool Read()
{
if (++currentRowIndex >= rawData.Count)
{
currentRowIndex--;
return false;
}
currentRow = rawData[currentRowIndex];
return true;
}
public int GetRowNumber(int Index)
{
return Index + rowOffset;
}
public string GetString(int ColumnIndex)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(CsvDeviceImportDataReader.Read)} must be called before retrieving values");
var value = currentRow[ColumnIndex];
if (value.Length == 0)
return null;
return value;
}
public IEnumerable<string> GetStrings(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex]);
}
public bool TryGetNullableInt(int ColumnIndex, out int? value)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(CsvDeviceImportDataReader.Read)} must be called before retrieving values");
return TryGetNullableInt(currentRow[ColumnIndex], out value);
}
public bool TryGetNullableBool(int ColumnIndex, out bool? value)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(CsvDeviceImportDataReader.Read)} must be called before retrieving values");
return TryGetNullableBool(currentRow[ColumnIndex], out value);
}
public bool TryGetNullableDateTime(int ColumnIndex, out DateTime? value)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(CsvDeviceImportDataReader.Read)} must be called before retrieving values");
return TryGetNullableDateTime(currentRow[ColumnIndex], out value);
}
public bool TestAllNotEmpty(int ColumnIndex)
{
return GetStrings(ColumnIndex).All(s => !string.IsNullOrWhiteSpace(s));
}
public bool TestAllNullableInt(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
int? value;
return TryGetNullableInt(c, out value);
});
}
public bool TestAllInt(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
int? value;
return TryGetNullableInt(c, out value) && value.HasValue;
});
}
public bool TestAllNullableBool(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
bool? value;
return TryGetNullableBool(c, out value);
});
}
public bool TestAllNullableDateTime(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
DateTime? value;
return TryGetNullableDateTime(c, out value);
});
}
public void Dispose()
{
// Nothing to dispose
}
private bool TryGetNullableDateTime(string content, out DateTime? value)
{
if (string.IsNullOrWhiteSpace(content))
{
value = null;
return true;
}
else
{
content = content.Trim();
DateTime valueDateTime;
if (DateTime.TryParse(content, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out valueDateTime))
{
value = valueDateTime;
return true;
}
else
{
value = null;
return false;
}
}
}
private bool TryGetNullableBool(string content, out bool? value)
{
if (string.IsNullOrWhiteSpace(content))
{
value = null;
return true;
}
else
{
content = content.Trim();
if (TrueValues.Contains(content, StringComparer.OrdinalIgnoreCase))
{
value = true;
return true;
}
else if (FalseValues.Contains(content, StringComparer.OrdinalIgnoreCase))
{
value = false;
return true;
}
else
{
value = null;
return false;
}
}
}
private bool TryGetNullableInt(string content, out int? value)
{
if (string.IsNullOrWhiteSpace(content))
{
value = null;
return true;
}
else
{
int intValue;
if (int.TryParse(content, out intValue))
{
value = intValue;
return true;
}
else
{
value = null;
return false;
}
}
}
}
}
+83 -110
View File
@@ -3,170 +3,143 @@ 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.DeviceAllowUnauthenticatedEnrol, typeof(DeviceAllowUnauthenticatedEnrolImportField) },
{ DeviceImportFieldTypes.DeviceDecommissionedDate, typeof(DeviceDecommissionedDateImportField) },
{ DeviceImportFieldTypes.DeviceDecommissionedReason, typeof(DeviceDecommissionedReasonImportField) },
{ DeviceImportFieldTypes.DetailLanMacAddress, typeof(DetailLanMacAddressImportField) },
{ DeviceImportFieldTypes.DetailWLanMacAddress, typeof(DetailWLanMacAddressImportField) },
{ DeviceImportFieldTypes.DetailACAdapter, typeof(DetailACAdapterImportField) },
{ DeviceImportFieldTypes.DetailBattery, typeof(DetailBatteryImportField) },
{ DeviceImportFieldTypes.DetailKeyboard, typeof(DetailKeyboardImportField) },
{ 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)
public static IDeviceImportContext BeginImport(DiscoDataContext Database, string Filename, bool HasHeader, Stream FileContent)
{
if (FileContent == null)
throw new ArgumentNullException("FileContent");
throw new ArgumentNullException(nameof(FileContent));
if (string.IsNullOrWhiteSpace(Filename))
Filename = "<None Specified>";
if (FileContent.Length == 0)
throw new ArgumentNullException(nameof(FileContent));
DeviceImportContext context;
List<Tuple<string, DeviceImportFieldTypes>> header;
List<string[]> rawData;
IDeviceImportContext context;
using (TextReader csvTextReader = new StreamReader(FileContent))
if (Filename?.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ?? false)
{
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();
}
// Use Xlsx Context
context = new XlsxDeviceImportContext(Filename, HasHeader, FileContent);
}
else
{
// Use/Default Csv Context
context = new CsvDeviceImportContext(Filename, HasHeader, FileContent);
}
context = new DeviceImportContext(Filename, header, rawData);
context.GuessHeaderTypes(Database);
return context;
}
private static void GuessHeaderTypes(this DeviceImportContext Context, DiscoDataContext Database)
private static void GuessHeaderTypes(this IDeviceImportContext Context, DiscoDataContext Database)
{
FieldHandlers.Value.ToList().ForEach(h =>
using (var dataReader = Context.GetDataReader())
{
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);
});
foreach (var fieldHandler in Context.GetFieldHandlers())
{
dataReader.Reset();
var instance = (DeviceImportFieldBase)Activator.CreateInstance(fieldHandler.Value);
var column = instance.GuessColumn(Database, Context, dataReader);
if (column.HasValue)
Context.SetColumnType(column.Value, instance.FieldType);
}
}
}
public static void UpdateHeaderTypes(this DeviceImportContext Context, List<DeviceImportFieldTypes> HeaderTypes)
public static void UpdateColumnTypes(this IDeviceImportContext Context, List<DeviceImportFieldTypes> ColumnTypes)
{
if (HeaderTypes == null)
throw new ArgumentNullException("HeaderTypes");
if (ColumnTypes == null)
throw new ArgumentNullException(nameof(ColumnTypes));
if (HeaderTypes.Count != Context.Header.Count)
throw new ArgumentException("The number of Header Types supplied does not match the number of Headers", "HeaderTypes");
if (ColumnTypes.Count != Context.ColumnCount)
throw new ArgumentException("The number of Column Types supplied does not match the number of Headers", nameof(ColumnTypes));
if (!HeaderTypes.Any(h => h == DeviceImportFieldTypes.DeviceSerialNumber))
throw new ArgumentException("At least one column must be the Device Serial Number", "HeaderTypes");
if (!ColumnTypes.Any(h => h == DeviceImportFieldTypes.DeviceSerialNumber))
throw new ArgumentException("At least one column must be the Device Serial Number", nameof(ColumnTypes));
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");
if (ColumnTypes.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", nameof(ColumnTypes));
Context.Header = Context.Header.Zip(HeaderTypes, (h, ht) => Tuple.Create(h.Item1, ht)).ToList();
for (int columnIndex = 0; columnIndex < Context.ColumnCount; columnIndex++)
{
Context.SetColumnType(columnIndex, ColumnTypes[columnIndex]);
}
}
public static void ParseRecords(this DeviceImportContext Context, DiscoDataContext Database, IScheduledTaskStatus Status)
public static void ParseRecords(this IDeviceImportContext Context, DiscoDataContext Database, IScheduledTaskStatus Status)
{
if (Context.Header == null)
throw new InvalidOperationException("The Import Context has not been initialized");
if (Context.ColumnCount == 0)
throw new InvalidOperationException("No columns were found");
if (Context.Header.Count == 0)
throw new InvalidOperationException("No Headers were found");
if (!Context.GetColumnByType(DeviceImportFieldTypes.DeviceSerialNumber).HasValue)
throw new ArgumentException("At least one column must be the Device Serial Number", nameof(Context.Columns));
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");
if (Context.RecordCount == 0)
throw new ArgumentException("No data was found in the import file", nameof(Context.RecordCount));
IDeviceImportCache cache;
if (Context.RawData.Count > 20)
if (Context.RecordCount > 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]))
var deviceSerialNumberIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceSerialNumber).Value;
var columns = Context.Columns
.Where(h => h.Type != DeviceImportFieldTypes.IgnoreColumn)
.ToList();
Status.UpdateStatus(0, "Parsing Import Records", "Starting...");
Context.Records = Context.RawData.Select((d, recordIndex) =>
var records = new List<IDeviceImportRecord>();
using (var dataReader = Context.GetDataReader())
{
string deviceSerialNumber = Fields.DeviceSerialNumberImportField.ParseRawDeviceSerialNumber(d[Context.HeaderDeviceSerialNumberIndex]);
Status.UpdateStatus(((double)recordIndex / Context.RawData.Count) * 100, string.Format("Parsing: {0}", deviceSerialNumber));
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 =>
while (dataReader.Read())
{
var f = (DeviceImportFieldBase)Activator.CreateInstance(h.Item4);
f.Parse(Database, cache, Context, recordIndex, deviceSerialNumber, existingDevice, values, h.Item3(d));
return f;
}).ToList();
string deviceSerialNumber = DeviceSerialNumberImportField.ParseRawDeviceSerialNumber(dataReader.GetString(deviceSerialNumberIndex));
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;
Status.UpdateStatus(((double)dataReader.Index / Context.RecordCount) * 100, string.Format("Parsing: {0}", deviceSerialNumber));
return new DeviceImportRecord(deviceSerialNumber, fields, recordAction);
}).Cast<IDeviceImportRecord>().ToList();
Device existingDevice = null;
if (DeviceSerialNumberImportField.IsDeviceSerialNumberValid(deviceSerialNumber))
existingDevice = cache.Devices.FirstOrDefault(device => device.SerialNumber == deviceSerialNumber);
var fields = columns.Select(h =>
{
var f = (DeviceImportFieldBase)h.GetHandlerInstance();
f.Parse(Database, cache, Context, deviceSerialNumber, existingDevice, records, dataReader, h.Index);
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;
records.Add(new DeviceImportRecord(dataReader.Index, deviceSerialNumber, fields, recordAction));
}
}
Context.Records = records;
}
public static int ApplyRecords(this DeviceImportContext Context, DiscoDataContext Database, IScheduledTaskStatus Status)
public static int ApplyRecords(this IDeviceImportContext Context, DiscoDataContext Database, IScheduledTaskStatus Status)
{
if (Context.Records == null)
throw new InvalidOperationException("Import Records have not been parsed");
@@ -1,35 +0,0 @@
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;
}
}
}
@@ -1,12 +1,8 @@
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
{
@@ -22,21 +18,22 @@ namespace Disco.Services.Devices.Importing
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 Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex);
public abstract bool Apply(DiscoDataContext Database, Device Device);
public virtual void Applied(DiscoDataContext Database, Device Device, ref bool DeviceADDescriptionSet) { return; }
public abstract int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context);
public abstract int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader);
#region Helpers
protected bool Error(string Message)
{
this.ErrorMessage = Message;
this.FieldAction = null;
ErrorMessage = Message;
FieldAction = null;
return false;
}
protected bool Success(EntityState Action)
{
this.FieldAction = Action;
FieldAction = Action;
return true;
}
#endregion
@@ -1,4 +1,5 @@
using Disco.Data.Repository;
using Disco.Models.Services.Devices.Importing;
using Disco.Services.Tasks;
using Quartz;
using System;
@@ -13,7 +14,7 @@ namespace Disco.Services.Devices.Importing
public override bool SingleInstanceTask { get { return false; } }
public override bool CancelInitiallySupported { get { return false; } }
public static ScheduledTaskStatus ScheduleNow(DeviceImportContext Context)
public static ScheduledTaskStatus ScheduleNow(IDeviceImportContext Context)
{
if (Context == null)
throw new ArgumentNullException("Context");
@@ -28,11 +29,11 @@ namespace Disco.Services.Devices.Importing
protected override void ExecuteTask()
{
var context = (DeviceImportContext)this.ExecutionContext.JobDetail.JobDataMap[JobDataMapContext];
var context = (IDeviceImportContext)ExecutionContext.JobDetail.JobDataMap[JobDataMapContext];
using (DiscoDataContext Database = new DiscoDataContext())
{
context.ParseRecords(Database, this.Status);
context.ParseRecords(Database, Status);
}
}
}
@@ -5,13 +5,12 @@ 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 int Index { get; private set; }
public string DeviceSerialNumber { get; private set; }
public IEnumerable<IDeviceImportField> Fields { get; private set; }
@@ -23,8 +22,9 @@ namespace Disco.Services.Devices.Importing
get { return Fields.Any(f => !f.FieldAction.HasValue); }
}
internal DeviceImportRecord(string DeviceSerialNumber, IEnumerable<IDeviceImportField> Fields, EntityState RecordAction)
internal DeviceImportRecord(int Index, string DeviceSerialNumber, IEnumerable<IDeviceImportField> Fields, EntityState RecordAction)
{
this.Index = Index;
this.DeviceSerialNumber = DeviceSerialNumber;
this.Fields = Fields;
this.RecordAction = RecordAction;
@@ -43,7 +43,7 @@ namespace Disco.Services.Devices.Importing
}
else if (RecordAction == EntityState.Modified)
{
device = Database.Devices.Find(this.DeviceSerialNumber);
device = Database.Devices.Find(DeviceSerialNumber);
}
else if (RecordAction == EntityState.Added)
{
@@ -82,6 +82,13 @@ namespace Disco.Services.Devices.Importing
if (changesMade)
Database.SaveChanges();
bool adDescriptionSet = false;
foreach (var field in Fields.Cast<DeviceImportFieldBase>())
{
field.Applied(Database, device, ref adDescriptionSet);
}
return changesMade;
}
@@ -22,18 +22,18 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
friendlyValue = Value;
var value = friendlyValue = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(Value))
if (string.IsNullOrWhiteSpace(value))
{
friendlyValue = null;
parsedValue = null;
}
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
parsedValue = ActiveDirectory.ParseDomainAccountId(parsedValue);
@@ -48,25 +48,32 @@ namespace Disco.Services.Devices.Importing.Fields
// Check User Exists
// Try Database
User user = Database.Users.FirstOrDefault(u => u.UserId == parsedValue);
try
using (var database = new DiscoDataContext())
{
// Try Updating from AD
user = UserService.GetUser(parsedValue, 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 = $"{user.DisplayName} [{user.UserId}]";
}
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]);
int? decommissionedDateIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedDate);
int? decommissionedReasonIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedReason);
if (decommissionedDateIndex.HasValue || decommissionedReasonIndex.HasValue)
{
importDecommissioning = (decommissionedDateIndex.HasValue && !string.IsNullOrWhiteSpace(DataReader.GetString(decommissionedDateIndex.Value))) ||
(decommissionedReasonIndex.HasValue && !string.IsNullOrWhiteSpace(DataReader.GetString(decommissionedReasonIndex.Value)));
}
if (importDecommissioning.HasValue && importDecommissioning.Value)
return Error("Cannot assign a user to a device being decommissioned");
@@ -84,7 +91,7 @@ namespace Disco.Services.Devices.Importing.Fields
else if (ExistingDevice != null && ExistingDevice.AssignedUserId != parsedValue)
{
if (ExistingDevice.AssignedUserId != null)
friendlyPreviousValue = string.Format("{0} [{1}]", ExistingDevice.AssignedUser.DisplayName, ExistingDevice.AssignedUser.UserId);
friendlyPreviousValue = $"{ExistingDevice.AssignedUser.DisplayName} [{ExistingDevice.AssignedUser.UserId}]";
else
friendlyPreviousValue = null;
@@ -96,7 +103,7 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Modified)
{
// Remove Current Assignments
var currentAssignments = Device.DeviceUserAssignments.Where(dua => !dua.UnassignedDate.HasValue);
@@ -130,16 +137,31 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override void Applied(DiscoDataContext Database, Device Device, ref bool DeviceADDescriptionSet)
{
if (!DeviceADDescriptionSet)
{
if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId))
{
var adAccount = Device.ActiveDirectoryAccount();
if (adAccount != null && !adAccount.IsCriticalSystemObject)
{
adAccount.SetDescription(Device);
DeviceADDescriptionSet = true;
}
}
}
}
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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", System.StringComparison.OrdinalIgnoreCase) >= 0
);
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("user", StringComparison.OrdinalIgnoreCase) >= 0);
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -20,28 +20,23 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
friendlyValue = Value;
// Validate
if (string.IsNullOrWhiteSpace(Value))
this.parsedValue = null; // Default = null
if (DataReader.TryGetNullableInt(ColumnIndex, out parsedValue))
{
friendlyValue = parsedValue.ToString();
}
else
{
int valueInt;
if (int.TryParse(Value, out valueInt))
this.parsedValue = valueInt;
else
return Error("The Batch Identifier must be a number");
return Error("The Batch Identifier must be a number");
}
if (this.parsedValue.HasValue)
if (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);
return Error($"The identifier ({friendlyValue}) does not match any Device Batch");
friendlyValue = $"{b.Name} [{b.Id}]";
}
else
friendlyValue = null;
@@ -55,7 +50,7 @@ namespace Disco.Services.Devices.Importing.Fields
previousBatch = Cache.DeviceBatches.FirstOrDefault(db => db.Id == ExistingDevice.DeviceBatchId.Value);
if (previousBatch != null)
friendlyPreviousValue = string.Format("{0} [{1}]", previousBatch.Name, previousBatch.Id);
friendlyPreviousValue = $"{previousBatch.Name} [{previousBatch.Id}]";
return Success(EntityState.Modified);
}
@@ -65,10 +60,10 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
Device.DeviceBatchId = this.parsedValue;
Device.DeviceBatchId = parsedValue;
return true;
}
else
@@ -77,30 +72,27 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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);
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("batch", 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();
// All Nullable<int> Values
possibleColumns = possibleColumns
.Where(h => DataReader.TestAllNullableInt(h.Index)).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);
h.Name.IndexOf("batchid", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("batch id", StringComparison.OrdinalIgnoreCase) >= 0);
}
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -19,13 +19,15 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
parsedValue = null;
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
}
if (ExistingDevice == null && parsedValue != null)
@@ -54,8 +56,8 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
@@ -84,16 +86,15 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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));
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
(h.Name.IndexOf("ac adapter", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("acadapter", StringComparison.OrdinalIgnoreCase) >= 0));
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -19,13 +19,15 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
parsedValue = null;
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
}
if (ExistingDevice == null && parsedValue != null)
@@ -54,8 +56,8 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
@@ -84,15 +86,14 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// column name
var possibleColumns = Context.Header
.Select((h, i) => Tuple.Create(h, i))
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn &&
h.Item1.Item1.IndexOf("battery", System.StringComparison.OrdinalIgnoreCase) >= 0);
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("battery", StringComparison.OrdinalIgnoreCase) >= 0);
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -19,13 +19,15 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
parsedValue = null;
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
}
if (ExistingDevice == null && parsedValue != null)
@@ -54,8 +56,8 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
@@ -84,15 +86,14 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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);
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("keyboard", StringComparison.OrdinalIgnoreCase) >= 0);
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -19,13 +19,15 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
parsedValue = null;
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
}
if (ExistingDevice == null && parsedValue != null)
@@ -54,8 +56,8 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
@@ -84,23 +86,22 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn && (
h.Name.IndexOf("wlan", StringComparison.OrdinalIgnoreCase) < 0 &&
h.Name.IndexOf("wireless", StringComparison.OrdinalIgnoreCase) < 0 && (
h.Name.IndexOf("lan address", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("lan mac", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("lan mac address", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("lanaddress", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("lanmac", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("lanmacaddress", StringComparison.OrdinalIgnoreCase) >= 0
)));
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -19,13 +19,15 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
parsedValue = null;
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
}
if (ExistingDevice == null && parsedValue != null)
@@ -54,8 +56,8 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
DeviceDetail detail = Database.DeviceDetails.FirstOrDefault(dd =>
@@ -84,25 +86,24 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn && (
h.Name.IndexOf("wireless lan mac address", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wireless lan address", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wireless mac address", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wireless mac", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wlan address", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wlan mac", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wlan mac address", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wlanaddress", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wlanmac", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("wlanmacaddress", StringComparison.OrdinalIgnoreCase) >= 0
));
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -1,7 +1,6 @@
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;
@@ -11,9 +10,7 @@ namespace Disco.Services.Devices.Importing.Fields
{
internal class DeviceAllowUnauthenticatedEnrolImportField : DeviceImportFieldBase
{
private static string[] TrueValues = { "true", "1", "yes", "-1", "on" };
private static string[] FalseValues = { "false", "0", "no", "off" };
private bool parsedValue;
private bool? parsedValue;
private string friendlyValue;
private string friendlyPreviousValue;
@@ -23,22 +20,30 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
friendlyValue = Value;
if (!ParseBoolean(Value, out parsedValue))
if (DataReader.TryGetNullableBool(ColumnIndex, out parsedValue))
{
friendlyValue = parsedValue.ToString();
}
else
{
return Error("Expected a Boolean expression (True, 1, Yes, On, False, 0, No, Off)");
}
friendlyValue = parsedValue.ToString();
friendlyValue = parsedValue?.ToString() ?? "Not Set";
if (parsedValue == true)
if (parsedValue.HasValue && parsedValue.Value == true)
{
// 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]);
int? decommissionedDateIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedDate);
int? decommissionedReasonIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedReason);
if (decommissionedDateIndex.HasValue || decommissionedReasonIndex.HasValue)
{
importDecommissioning = (decommissionedDateIndex.HasValue && !string.IsNullOrWhiteSpace(DataReader.GetString(decommissionedDateIndex.Value))) ||
(decommissionedReasonIndex.HasValue && !string.IsNullOrWhiteSpace(DataReader.GetString(decommissionedReasonIndex.Value)));
}
if (importDecommissioning.HasValue && importDecommissioning.Value)
return Error("Cannot enrol a device being decommissioned");
@@ -49,7 +54,11 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
if (ExistingDevice == null && parsedValue != false) // Default: True
if (!parsedValue.HasValue)
{
return Success(EntityState.Unchanged);
}
else if (ExistingDevice == null && parsedValue.Value != false) // Default: True
{
return Success(EntityState.Added);
}
@@ -65,10 +74,11 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Modified ||
this.FieldAction == EntityState.Added)
if (parsedValue.HasValue &&
(FieldAction == EntityState.Modified ||
FieldAction == EntityState.Added))
{
Device.AllowUnauthenticatedEnrol = parsedValue;
Device.AllowUnauthenticatedEnrol = parsedValue.Value;
return true;
}
@@ -78,52 +88,20 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// column name
var possibleColumns = Context.Header
.Select((h, i) => Tuple.Create(h, i))
.Where(h => h.Item1.Item2 == DeviceImportFieldTypes.IgnoreColumn &&
h.Item1.Item1.IndexOf("trust enrol", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Item1.Item1.IndexOf("enrolment trusted", System.StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Item1.Item1.IndexOf("trust", System.StringComparison.OrdinalIgnoreCase) >= 0
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("trust enrol", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("enrolment trusted", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("trust", StringComparison.OrdinalIgnoreCase) >= 0
);
// All Boolean
possibleColumns = possibleColumns.Where(h =>
{
bool lastValue;
return Context.RawData.Select(v => v[h.Item2]).Take(100).Where(v => !string.IsNullOrWhiteSpace(v)).All(v => ParseBoolean(v, out lastValue));
}).ToList();
possibleColumns = possibleColumns.Where(h => DataReader.TestAllNullableBool(h.Index)).ToList();
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
}
private static bool ParseBoolean(string value, out bool result)
{
if (string.IsNullOrWhiteSpace(value))
{
result = false;
return false;
}
value = value.Trim();
if (TrueValues.Contains(value, StringComparer.OrdinalIgnoreCase))
{
result = true;
return true;
}
else if (FalseValues.Contains(value, StringComparer.OrdinalIgnoreCase))
{
result = false;
return true;
}
else
{
result = false;
return false;
}
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -19,13 +19,15 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
parsedValue = null;
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
if (parsedValue.Length > 40)
return Error("Cannot be more than 40 characters");
}
@@ -43,10 +45,10 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
Device.AssetNumber = this.parsedValue;
Device.AssetNumber = parsedValue;
return true;
}
else
@@ -55,14 +57,14 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// '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);
// column name
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("asset", StringComparison.OrdinalIgnoreCase) >= 0);
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -1,6 +1,7 @@
using Disco.Data.Repository;
using Disco.Models.Repository;
using Disco.Models.Services.Devices.Importing;
using Disco.Services.Interop.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Data;
@@ -24,53 +25,67 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
if (!DataReader.TryGetNullableDateTime(ColumnIndex, out parsedValue))
{
rawValue = null;
parsedValue = null;
rawValue = DataReader.GetString(ColumnIndex);
return Error($"Cannot parse the value as a Date/Time using {CultureInfo.CurrentCulture.Name} culture (system default).");
}
else
if (parsedValue.HasValue)
{
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);
}
// Accuracy to the second (remove any milliseconds)
parsedValue = new DateTime((parsedValue.Value.Ticks / 10000000L) * 10000000L);
}
string errorMessage;
if (parsedValue.HasValue && !CanDecommissionDevice(ExistingDevice, Values, out errorMessage))
if (parsedValue.HasValue && !CanDecommissionDevice(ExistingDevice, Context, DataReader, out errorMessage))
return Error(errorMessage);
setReason = !Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedReason) ||
(parsedValue.HasValue && string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedReason]));
var decommissionReasonIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedReason);
setReason = !decommissionReasonIndex.HasValue ||
(parsedValue.HasValue && string.IsNullOrWhiteSpace(DataReader.GetString(decommissionReasonIndex.Value)));
if (ExistingDevice != null && ExistingDevice.DecommissionedDate != parsedValue)
if (ExistingDevice != null && ExistingDevice.DecommissionedDate.HasValue)
{
// Accuracy to the second (remove any milliseconds)
previousValue = new DateTime((ExistingDevice.DecommissionedDate.Value.Ticks / 10000000L) * 10000000L);
}
else
{
previousValue = null;
}
if (previousValue != parsedValue)
{
previousValue = ExistingDevice.DecommissionedDate;
return Success(EntityState.Modified);
}
else
{
previousValue = null;
return Success(EntityState.Unchanged);
}
}
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Modified)
{
// Decommission or Recommission Device
Device.DecommissionedDate = this.parsedValue;
Device.DecommissionedDate = parsedValue;
if (setReason)
Device.DecommissionReason = this.parsedValue.HasValue ? (DecommissionReasons?)DecommissionReasons.EndOfLife : null;
{
if (parsedValue.HasValue && !Device.DecommissionReason.HasValue)
{
Device.DecommissionReason = DecommissionReasons.EndOfLife;
}
else if (!parsedValue.HasValue && Device.DecommissionReason.HasValue)
{
Device.DecommissionReason = null;
}
}
return true;
}
@@ -80,22 +95,52 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override void Applied(DiscoDataContext Database, Device Device, ref bool DeviceADDescriptionSet)
{
// 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
);
if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId))
{
var adAccount = Device.ActiveDirectoryAccount();
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
if (adAccount != null && !adAccount.IsCriticalSystemObject)
{
if (Device.DecommissionedDate.HasValue)
{
// Disable AD Account
adAccount.DisableAccount();
}
else
{
// Enable AD Account
adAccount.EnableAccount();
}
if (!DeviceADDescriptionSet)
{
adAccount.SetDescription(Device);
DeviceADDescriptionSet = true;
}
}
}
}
public static bool CanDecommissionDevice(Device Device, Dictionary<DeviceImportFieldTypes, string> Values, out string ErrorMessage)
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("decommission date", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("decommissiondate", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("decommissioned date", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("decommissioneddate", StringComparison.OrdinalIgnoreCase) >= 0
);
possibleColumns = possibleColumns
.Where(h => DataReader.TestAllNullableDateTime(h.Index)).ToList();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
public static bool CanDecommissionDevice(Device Device, IDeviceImportContext Context, IDeviceImportDataReader DataReader, out string ErrorMessage)
{
if (Device == null)
{
@@ -105,13 +150,14 @@ namespace Disco.Services.Devices.Importing.Fields
// 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])))
var assignedUserIndex = Context.GetColumnByType(DeviceImportFieldTypes.AssignedUserId);
if ((!assignedUserIndex.HasValue && Device.AssignedUserId != null) ||
(assignedUserIndex.HasValue && !string.IsNullOrWhiteSpace(DataReader.GetString(assignedUserIndex.Value))))
{
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);
ErrorMessage = $"The device is assigned to a user ({Device.AssignedUser.DisplayName} [{Device.AssignedUser.UserId}]) and cannot be decommissioned";
else
ErrorMessage = string.Format("The device is being assigned to a user ({0}) and cannot be decommissioned", Values[DeviceImportFieldTypes.AssignedUserId]);
ErrorMessage = $"The device is being assigned to a user ({DataReader.GetString(assignedUserIndex.Value)}) and cannot be decommissioned";
return false;
}
@@ -119,7 +165,7 @@ namespace Disco.Services.Devices.Importing.Fields
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");
ErrorMessage = $"The device is associated with {openJobCount} open job{(openJobCount == 1 ? null : "s")} and cannot be decommissioned";
return false;
}
@@ -1,6 +1,7 @@
using Disco.Data.Repository;
using Disco.Models.Repository;
using Disco.Models.Services.Devices.Importing;
using Disco.Services.Interop.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Data;
@@ -22,9 +23,11 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
{
rawValue = null;
parsedValue = null;
@@ -32,9 +35,9 @@ namespace Disco.Services.Devices.Importing.Fields
else
{
DecommissionReasons valueReason;
if (!decommissionReasonsMap.Value.TryGetValue(Value.Trim(), out valueReason))
if (!decommissionReasonsMap.Value.TryGetValue(value.Trim(), out valueReason))
{
rawValue = Value.Trim();
rawValue = value.Trim();
return Error("Cannot parse the value as a Decommission Reason");
}
else
@@ -43,15 +46,16 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
if (parsedValue.HasValue && !Values.ContainsKey(DeviceImportFieldTypes.DeviceDecommissionedDate))
var decommissionedDateIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedDate);
if (parsedValue.HasValue && !decommissionedDateIndex.HasValue)
{
string errorMessage;
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(ExistingDevice, Values, out errorMessage))
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(ExistingDevice, Context, DataReader, out errorMessage))
return Error(errorMessage);
setDate = true;
}
else if (parsedValue.HasValue && string.IsNullOrWhiteSpace(Values[DeviceImportFieldTypes.DeviceDecommissionedDate]))
else if (parsedValue.HasValue && string.IsNullOrWhiteSpace(DataReader.GetString(decommissionedDateIndex.Value)))
{
setDate = true;
}
@@ -67,13 +71,22 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Modified)
{
// Decommission or Recommission Device
Device.DecommissionReason = this.parsedValue;
Device.DecommissionReason = parsedValue;
if (setDate)
Device.DecommissionedDate = this.parsedValue.HasValue ? (DateTime?)DateTime.Now : null;
{
if (parsedValue.HasValue && !Device.DecommissionedDate.HasValue)
{
Device.DecommissionedDate = DateTime.Now;
}
else if (!parsedValue.HasValue && Device.DecommissionedDate.HasValue)
{
Device.DecommissionedDate = null;
}
}
return true;
}
@@ -83,19 +96,50 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override void Applied(DiscoDataContext Database, Device Device, ref bool DeviceADDescriptionSet)
{
// Only Enable/Disable if DeviceDecommissionedDate field is not present
if (setDate)
{
if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId))
{
var adAccount = Device.ActiveDirectoryAccount();
if (adAccount != null && !adAccount.IsCriticalSystemObject)
{
if (Device.DecommissionedDate.HasValue)
{
// Disable AD Account
adAccount.DisableAccount();
}
else
{
// Enable AD Account
adAccount.EnableAccount();
}
if (!DeviceADDescriptionSet)
{
adAccount.SetDescription(Device);
DeviceADDescriptionSet = true;
}
}
}
}
}
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("decommission reason", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("decommissionreason", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("decommissioned reason", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("decommissionedreason", StringComparison.OrdinalIgnoreCase) >= 0
);
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
public static Dictionary<string, DecommissionReasons> BuildDecommissionReasonsMap()
@@ -19,13 +19,15 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
if (string.IsNullOrWhiteSpace(Value))
var value = DataReader.GetString(ColumnIndex);
if (string.IsNullOrWhiteSpace(value))
parsedValue = null;
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
if (parsedValue.Length > 250)
return Error("Cannot be more than 250 characters");
}
@@ -43,10 +45,10 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
Device.Location = this.parsedValue;
Device.Location = parsedValue;
return true;
}
else
@@ -55,14 +57,14 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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);
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("location", StringComparison.OrdinalIgnoreCase) >= 0);
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -19,14 +19,16 @@ namespace Disco.Services.Devices.Importing.Fields
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)
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);
// Validate
if (string.IsNullOrWhiteSpace(Value))
if (string.IsNullOrWhiteSpace(value))
return Error("The Device Serial Number is required");
else
{
parsedValue = Value.Trim();
parsedValue = value.Trim();
if (parsedValue.Length > maxLength)
return Error($"Cannot be more than {maxLength} characters");
if (parsedValue.Contains(@"/"))
@@ -36,13 +38,11 @@ namespace Disco.Services.Devices.Importing.Fields
}
// 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));
var duplicate = PreviousRecords
.Where(r => IsDeviceSerialNumberValid(r.DeviceSerialNumber))
.FirstOrDefault(r => r.DeviceSerialNumber.Equals(parsedValue, StringComparison.OrdinalIgnoreCase));
if (duplicate != null)
return Error($"This Device Serial Number was already present on Row {duplicate.Item1 + 1}");
return Error($"This Device Serial Number was already present on Row {DataReader.GetRowNumber(duplicate.Index)}");
// No action required
return Success(EntityState.Unchanged);
@@ -54,18 +54,14 @@ namespace Disco.Services.Devices.Importing.Fields
return false;
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// '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);
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn && h.Name.IndexOf("serial", StringComparison.OrdinalIgnoreCase) >= 0);
// All Values
possibleColumns = possibleColumns.Where(h =>
{
return Context.RawData.Select(v => v[h.Item2]).All(v => !string.IsNullOrWhiteSpace(v));
}).ToList();
possibleColumns = possibleColumns.Where(h => DataReader.TestAllNotEmpty(h.Index)).ToList();
if (possibleColumns.Count() > 1)
{
@@ -74,17 +70,17 @@ namespace Disco.Services.Devices.Importing.Fields
{
try
{
var top50SerialNumbers = Context.RawData.Select(v => v[h.Item2]).Take(50).ToArray();
var top50SerialNumbers = DataReader.GetStrings(h.Index).Take(50).ToList();
return Database.Devices.Count(d => top50SerialNumbers.Contains(d.SerialNumber)) > 0;
}
catch (Exception) { return false; }
}).Select(h => (int?)h.Item2).FirstOrDefault();
}).Select(h => (int?)h.Index).FirstOrDefault();
if (possibleColumnIndex.HasValue)
return possibleColumnIndex;
}
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
public static string ParseRawDeviceSerialNumber(string DeviceSerialNumber)
@@ -98,5 +94,6 @@ namespace Disco.Services.Devices.Importing.Fields
{
return DeviceSerialNumber != null && DeviceSerialNumber.Length <= maxLength;
}
}
}
@@ -1,6 +1,7 @@
using Disco.Data.Repository;
using Disco.Models.Repository;
using Disco.Models.Services.Devices.Importing;
using Disco.Services.Interop.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Data;
@@ -20,23 +21,37 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
friendlyValue = Value;
int? intValue;
if (DataReader.TryGetNullableInt(ColumnIndex, out intValue))
{
if (!intValue.HasValue)
{
if (ExistingDevice == null)
{
intValue = 1; // Default Model for new devices
}
else
{
return Error("The Model Identifier cannot be blank");
}
}
// Validate
if (string.IsNullOrWhiteSpace(Value))
this.parsedValue = 1; // Default Model
parsedValue = intValue.Value;
friendlyValue = parsedValue.ToString();
}
else
if (!int.TryParse(Value, out parsedValue))
return Error("The Model Identifier must be a number");
{
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));
return Error($"The identifier ({parsedValue}) does not match any Device Model");
friendlyValue = string.Format("{0} [{1}]", m.Description, m.Id);
friendlyValue = $"{m.Description} [{m.Id}]";
if (ExistingDevice == null)
return Success(EntityState.Added);
@@ -46,7 +61,7 @@ namespace Disco.Services.Devices.Importing.Fields
if (ExistingDevice.DeviceModelId.HasValue)
{
var previousModel = Cache.DeviceModels.FirstOrDefault(dm => dm.Id == ExistingDevice.DeviceModelId.Value);
friendlyPreviousValue = string.Format("{0} [{1}]", previousModel.Description, previousModel.Id);
friendlyPreviousValue = $"{previousModel.Description} [{previousModel.Id}]";
}
return Success(EntityState.Modified);
@@ -57,10 +72,10 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
Device.DeviceModelId = this.parsedValue;
Device.DeviceModelId = parsedValue;
return true;
}
else
@@ -69,30 +84,43 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override void Applied(DiscoDataContext Database, Device Device, ref bool DeviceADDescriptionSet)
{
// '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);
if (!DeviceADDescriptionSet)
{
if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId))
{
var adAccount = Device.ActiveDirectoryAccount();
if (adAccount != null && !adAccount.IsCriticalSystemObject)
{
adAccount.SetDescription(Device);
DeviceADDescriptionSet = true;
}
}
}
}
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("model", 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();
possibleColumns = possibleColumns.Where(h => DataReader.TestAllInt(h.Index)).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);
h.Name.IndexOf("modelid", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("model id", StringComparison.OrdinalIgnoreCase) >= 0);
}
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -1,6 +1,7 @@
using Disco.Data.Repository;
using Disco.Models.Repository;
using Disco.Models.Services.Devices.Importing;
using Disco.Services.Interop.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Data;
@@ -20,30 +21,44 @@ namespace Disco.Services.Devices.Importing.Fields
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)
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
{
friendlyValue = Value;
int? intValue;
if (DataReader.TryGetNullableInt(ColumnIndex, out intValue))
{
if (!intValue.HasValue)
{
if (ExistingDevice == null)
{
intValue = Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId; // Default Model for new devices
}
else
{
return Error("The Profile Identifier cannot be blank");
}
}
// Validate
if (string.IsNullOrWhiteSpace(Value))
this.parsedValue = Database.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId;
parsedValue = intValue.Value;
friendlyValue = parsedValue.ToString();
}
else
if (!int.TryParse(Value, out parsedValue))
return Error("The Profile Identifier must be a number");
{
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));
return Error($"The identifier ({parsedValue}) does not match any Device Profile");
friendlyValue = string.Format("{0} [{1}]", p.Description, p.Id);
friendlyValue = $"{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);
friendlyPreviousValue = $"{previousProfile.Description} [{previousProfile.Id}]";
return Success(EntityState.Modified);
}
@@ -53,10 +68,10 @@ namespace Disco.Services.Devices.Importing.Fields
public override bool Apply(DiscoDataContext Database, Device Device)
{
if (this.FieldAction == EntityState.Added ||
this.FieldAction == EntityState.Modified)
if (FieldAction == EntityState.Added ||
FieldAction == EntityState.Modified)
{
Device.DeviceProfileId = this.parsedValue;
Device.DeviceProfileId = parsedValue;
return true;
}
else
@@ -65,30 +80,43 @@ namespace Disco.Services.Devices.Importing.Fields
}
}
public override int? GuessHeader(DiscoDataContext Database, DeviceImportContext Context)
public override void Applied(DiscoDataContext Database, Device Device, ref bool DeviceADDescriptionSet)
{
if (!DeviceADDescriptionSet)
{
if (ActiveDirectory.IsValidDomainAccountId(Device.DeviceDomainId))
{
var adAccount = Device.ActiveDirectoryAccount();
if (adAccount != null && !adAccount.IsCriticalSystemObject)
{
adAccount.SetDescription(Device);
DeviceADDescriptionSet = true;
}
}
}
}
public override int? GuessColumn(DiscoDataContext Database, IDeviceImportContext Context, IDeviceImportDataReader DataReader)
{
// 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);
var possibleColumns = Context.Columns
.Where(h => h.Type == DeviceImportFieldTypes.IgnoreColumn &&
h.Name.IndexOf("profile", 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();
possibleColumns = possibleColumns.Where(h => DataReader.TestAllInt(h.Index)).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);
h.Name.IndexOf("profileid", StringComparison.OrdinalIgnoreCase) >= 0 ||
h.Name.IndexOf("profile id", StringComparison.OrdinalIgnoreCase) >= 0);
}
return possibleColumns.Select(h => (int?)h.Item2).FirstOrDefault();
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
}
}
}
@@ -0,0 +1,133 @@
using ClosedXML.Excel;
using Disco.Models.Services.Devices.Importing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Disco.Services.Devices.Importing
{
public class XlsxDeviceImportContext : BaseDeviceImportContext
{
private bool hasHeaderRow;
private List<object[]> rawData;
public XlsxDeviceImportContext(string Filename, bool HasHeaderRow, Stream XlsxStream)
: base(Filename)
{
hasHeaderRow = HasHeaderRow;
using (var stream = new MemoryStream((int)XlsxStream.Length))
{
XlsxStream.CopyTo(stream);
ParseXlsx(stream);
}
}
public override int RecordCount
{
get
{
return rawData.Count;
}
}
public override IDeviceImportDataReader GetDataReader()
{
return new XlsxDeviceImportDataReader(this, rawData, hasHeaderRow);
}
private void ParseXlsx(Stream XlsxStream)
{
using (var xlWorkbook = new XLWorkbook(XlsxStream, XLEventTracking.Disabled))
{
if (xlWorkbook.Worksheets.Count == 0)
throw new IndexOutOfRangeException("Workbook contains no worksheets");
// Use first worksheet
var worksheet = xlWorkbook.Worksheets.Worksheet(1);
SetDatasetName($"{Filename} [Sheet: {worksheet.Name}]");
var columnCount = worksheet.LastColumnUsed().ColumnNumber();
if (hasHeaderRow)
{
var headerRow = worksheet.FirstRow();
SetColumns(Enumerable.Range(1, columnCount)
.Select(i =>
{
var cell = headerRow.Cell(i);
var headerName = cell.GetString();
if (string.IsNullOrWhiteSpace(headerName))
{
headerName = $"Column {cell.WorksheetColumn().ColumnLetter()}";
}
return new DeviceImportColumn()
{
Index = i - 1,
Name = headerName,
Type = DeviceImportFieldTypes.IgnoreColumn
};
}));
}
else
{
SetColumns(Enumerable.Range(1, columnCount)
.Select(i => new DeviceImportColumn()
{
Index = i - 1,
Name = $"Column {worksheet.Column(i).ColumnLetter()}",
Type = DeviceImportFieldTypes.IgnoreColumn
}));
}
// Import Data
var rawData = new List<object[]>();
foreach (var row in worksheet.RowsUsed().Skip(hasHeaderRow ? 1 : 0))
{
var record = new object[columnCount];
rawData.Add(record);
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
{
var cell = row.Cell(columnIndex + 1);
var cellValue = cell.Value;
switch (cell.DataType)
{
case XLCellValues.Number:
if (cellValue is double)
{
record[columnIndex] = (double)cellValue;
}
continue;
case XLCellValues.Boolean:
if (cellValue is bool)
{
record[columnIndex] = (bool)cellValue;
}
continue;
case XLCellValues.DateTime:
if (cellValue is DateTime)
{
record[columnIndex] = (DateTime)cellValue;
}
continue;
}
var stringValue = cellValue == null ? null : cellValue.ToString();
if (stringValue != null && stringValue.Length == 0)
{
stringValue = null;
}
record[columnIndex] = stringValue;
}
}
this.rawData = rawData;
}
}
}
}
@@ -0,0 +1,264 @@
using Disco.Models.Services.Devices.Importing;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Disco.Services.Devices.Importing
{
public class XlsxDeviceImportDataReader : IDeviceImportDataReader
{
private static string[] TrueValues = { "true", "1", "yes", "-1", "on" };
private static string[] FalseValues = { "false", "0", "no", "off" };
private XlsxDeviceImportContext context;
private List<object[]> rawData;
private int currentRowIndex;
private int rowOffset;
private object[] currentRow;
public int Index { get { return currentRowIndex; } }
public XlsxDeviceImportDataReader(XlsxDeviceImportContext Context, List<object[]> RawData, bool HasHeaderRow)
{
context = Context;
rawData = RawData;
currentRowIndex = 0;
rowOffset = currentRowIndex = HasHeaderRow ? 2 : 1;
}
public void Reset()
{
currentRowIndex = 0;
currentRow = null;
}
public bool Read()
{
if (++currentRowIndex >= rawData.Count)
{
currentRowIndex--;
return false;
}
currentRow = rawData[currentRowIndex];
return true;
}
public int GetRowNumber(int Index)
{
return Index + rowOffset;
}
public string GetString(int ColumnIndex)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(XlsxDeviceImportDataReader.Read)} must be called before retrieving values");
var cell = currentRow[ColumnIndex];
if (cell == null)
return null;
else
return cell.ToString();
}
public IEnumerable<string> GetStrings(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex]?.ToString());
}
public bool TryGetNullableInt(int ColumnIndex, out int? value)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(XlsxDeviceImportDataReader.Read)} must be called before retrieving values");
return TryGetNullableInt(currentRow[ColumnIndex], out value);
}
public bool TryGetNullableBool(int ColumnIndex, out bool? value)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(XlsxDeviceImportDataReader.Read)} must be called before retrieving values");
return TryGetNullableBool(currentRow[ColumnIndex], out value);
}
public bool TryGetNullableDateTime(int ColumnIndex, out DateTime? value)
{
if (currentRow == null)
throw new InvalidOperationException($"{nameof(XlsxDeviceImportDataReader.Read)} must be called before retrieving values");
return TryGetNullableDateTime(currentRow[ColumnIndex], out value);
}
public bool TestAllNotEmpty(int ColumnIndex)
{
return GetStrings(ColumnIndex).All(s => !string.IsNullOrWhiteSpace(s));
}
public bool TestAllNullableInt(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
int? value;
return TryGetNullableInt(c, out value);
});
}
public bool TestAllInt(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
int? value;
return TryGetNullableInt(c, out value) && value.HasValue;
});
}
public bool TestAllNullableBool(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
bool? value;
return TryGetNullableBool(c, out value);
});
}
public bool TestAllNullableDateTime(int ColumnIndex)
{
return rawData.Select(r => r[ColumnIndex])
.All(c =>
{
DateTime? value;
return TryGetNullableDateTime(c, out value);
});
}
public void Dispose()
{
// Nothing to dispose
}
private bool TryGetNullableDateTime(object Content, out DateTime? value)
{
if (Content == null)
{
value = null;
return true;
}
if (Content is DateTime)
{
value = (DateTime)Content;
return true;
}
var stringValue = Content.ToString();
if (string.IsNullOrWhiteSpace(stringValue))
{
value = null;
return true;
}
else
{
stringValue = stringValue.Trim();
DateTime valueDateTime;
if (DateTime.TryParse(stringValue, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out valueDateTime))
{
value = valueDateTime;
return true;
}
else
{
value = null;
return false;
}
}
}
private bool TryGetNullableBool(object Content, out bool? value)
{
if (Content == null)
{
value = null;
return true;
}
if (Content is bool)
{
value = (bool)Content;
return true;
}
var stringValue = Content.ToString();
if (string.IsNullOrWhiteSpace(stringValue))
{
value = null;
return true;
}
else
{
stringValue = stringValue.Trim();
if (TrueValues.Contains(stringValue, StringComparer.OrdinalIgnoreCase))
{
value = true;
return true;
}
else if (FalseValues.Contains(stringValue, StringComparer.OrdinalIgnoreCase))
{
value = false;
return true;
}
else
{
value = null;
return false;
}
}
}
private bool TryGetNullableInt(object Content, out int? value)
{
if (Content == null)
{
value = null;
return true;
}
if (Content is double)
{
value = (int)((double)Content);
return true;
}
var stringValue = Content.ToString();
if (string.IsNullOrWhiteSpace(stringValue))
{
value = null;
return true;
}
else
{
int intValue;
if (int.TryParse(stringValue, out intValue))
{
value = intValue;
return true;
}
else
{
value = null;
return false;
}
}
}
}
}
+14 -1
View File
@@ -36,6 +36,14 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="ClosedXML, Version=0.85.0.0, Culture=neutral, PublicKeyToken=fd1eb21b62ae805b, processorArchitecture=MSIL">
<HintPath>..\packages\ClosedXML.0.85.0\lib\net40\ClosedXML.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\DocumentFormat.OpenXml.2.5\lib\DocumentFormat.OpenXml.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework">
<HintPath>..\packages\EntityFramework.5.0.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
@@ -244,11 +252,14 @@
<Compile Include="Devices\Exporting\DeviceExport.cs" />
<Compile Include="Devices\Exporting\DeviceExportTask.cs" />
<Compile Include="Devices\Exporting\DeviceExportTaskContext.cs" />
<Compile Include="Devices\Importing\BaseDeviceImportContext.cs" />
<Compile Include="Devices\Importing\CsvDeviceImportDataReader.cs" />
<Compile Include="Devices\Importing\DeviceImport.cs" />
<Compile Include="Devices\Importing\DeviceImportApplyTask.cs" />
<Compile Include="Devices\Importing\DeviceImportContext.cs" />
<Compile Include="Devices\Importing\CsvDeviceImportContext.cs" />
<Compile Include="Devices\Importing\DeviceImportDatabaseCache.cs" />
<Compile Include="Devices\Importing\DeviceImportFieldBase.cs" />
<Compile Include="Devices\Importing\DeviceImportColumn.cs" />
<Compile Include="Devices\Importing\DeviceImportInMemoryCache.cs" />
<Compile Include="Devices\Importing\DeviceImportParseTask.cs" />
<Compile Include="Devices\Importing\DeviceImportRecord.cs" />
@@ -269,6 +280,8 @@
<Compile Include="Devices\Importing\Fields\ModelIdImportField.cs" />
<Compile Include="Devices\Importing\IDeviceImportCache.cs" />
<Compile Include="Devices\DeviceUpdatesHub.cs" />
<Compile Include="Devices\Importing\XlsxDeviceImportContext.cs" />
<Compile Include="Devices\Importing\XlsxDeviceImportDataReader.cs" />
<Compile Include="Devices\ManagedGroups\DeviceBatchAssignedUsersManagedGroup.cs" />
<Compile Include="Devices\ManagedGroups\DeviceBatchDevicesManagedGroup.cs" />
<Compile Include="Devices\ManagedGroups\DeviceManagedGroups.cs" />
+48 -48
View File
@@ -4,8 +4,6 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Disco.Data.Repository;
@@ -81,11 +79,11 @@ namespace Disco.Services.Plugins
public List<PluginFeatureManifest> GetFeatures(Type FeatureCategoryType)
{
return this.Features.Where(fm => fm.CategoryType.IsAssignableFrom(FeatureCategoryType)).ToList();
return Features.Where(fm => fm.CategoryType.IsAssignableFrom(FeatureCategoryType)).ToList();
}
public PluginFeatureManifest GetFeature(string PluginFeatureId)
{
return this.Features.Where(fm => fm.Id == PluginFeatureId).FirstOrDefault();
return Features.Where(fm => fm.Id == PluginFeatureId).FirstOrDefault();
}
public Plugin CreateInstance()
@@ -135,6 +133,7 @@ namespace Disco.Services.Plugins
return new List<string>()
{
"C5",
"ClosedXML",
"Common.Logging",
"Disco.BI",
"Disco.Data",
@@ -142,6 +141,7 @@ namespace Disco.Services.Plugins
"Disco.Services",
"Disco.Web",
"Disco.Web.Extensions",
"DocumentFormat.OpenXml",
"EntityFramework",
"Exceptionless",
"Exceptionless.Models",
@@ -159,7 +159,7 @@ namespace Disco.Services.Plugins
"Owin",
"PdfiumViewer",
"PdfSharp",
"PList",
"PListNet",
"Quartz",
"RazorGenerator.Mvc",
"Renci.SshNet",
@@ -183,7 +183,7 @@ namespace Disco.Services.Plugins
"System.Web.WebPages.Razor",
"T4MVCExtensions",
"WebActivatorEx",
"zxing"
"ZXingNet"
};
});
public static IReadOnlyCollection<string> PluginExcludedAssemblies
@@ -297,34 +297,34 @@ namespace Disco.Services.Plugins
if (!environmentInitalized)
{
var assemblyFullPath = Path.Combine(this.PluginLocation, this.AssemblyPath);
var assemblyFullPath = Path.Combine(PluginLocation, AssemblyPath);
if (!File.Exists(assemblyFullPath))
throw new FileNotFoundException(string.Format("Plugin Assembly [{0}] not found at: {1}", this.Id, assemblyFullPath), assemblyFullPath);
throw new FileNotFoundException(string.Format("Plugin Assembly [{0}] not found at: {1}", Id, assemblyFullPath), assemblyFullPath);
if (this.PluginAssembly == null)
this.PluginAssembly = Assembly.LoadFile(assemblyFullPath);
if (PluginAssembly == null)
PluginAssembly = Assembly.LoadFile(assemblyFullPath);
if (this.PluginAssembly == null)
throw new InvalidOperationException(string.Format("Unable to load Plugin Assembly [{0}] at: {1}", this.Id, assemblyFullPath));
if (PluginAssembly == null)
throw new InvalidOperationException(string.Format("Unable to load Plugin Assembly [{0}] at: {1}", Id, assemblyFullPath));
PluginsLog.LogInitializingPluginAssembly(this.PluginAssembly);
PluginsLog.LogInitializingPluginAssembly(PluginAssembly);
// Check Manifest/Assembly Versions Match
if (this.Version != this.PluginAssembly.GetName().Version)
throw new InvalidOperationException(string.Format("The plugin [{0}] manifest version [{1}] doesn't match the plugin assembly [{2} : {3}]", this.Id, this.Version, assemblyFullPath, this.PluginAssembly.GetName().Version));
if (Version != PluginAssembly.GetName().Version)
throw new InvalidOperationException(string.Format("The plugin [{0}] manifest version [{1}] doesn't match the plugin assembly [{2} : {3}]", Id, Version, assemblyFullPath, PluginAssembly.GetName().Version));
if (this.Type == null)
this.Type = this.PluginAssembly.GetType(this.TypeName, true, true);
if (Type == null)
Type = PluginAssembly.GetType(TypeName, true, true);
if (this.ConfigurationHandlerType == null)
this.ConfigurationHandlerType = this.PluginAssembly.GetType(this.ConfigurationHandlerTypeName, true, true);
if (ConfigurationHandlerType == null)
ConfigurationHandlerType = PluginAssembly.GetType(ConfigurationHandlerTypeName, true, true);
if (!string.IsNullOrEmpty(this.WebHandlerTypeName) && this.WebHandlerType == null)
this.WebHandlerType = this.PluginAssembly.GetType(this.WebHandlerTypeName, true, true);
if (!string.IsNullOrEmpty(WebHandlerTypeName) && WebHandlerType == null)
WebHandlerType = PluginAssembly.GetType(WebHandlerTypeName, true, true);
// Update non-static values
this.StorageLocation = Path.Combine(Database.DiscoConfiguration.PluginStorageLocation, this.Id);
StorageLocation = Path.Combine(Database.DiscoConfiguration.PluginStorageLocation, Id);
environmentInitalized = true;
}
@@ -336,7 +336,7 @@ namespace Disco.Services.Plugins
// Initialize Plugin
InitializePluginEnvironment(Database);
using (var pluginInstance = this.CreateInstance())
using (var pluginInstance = CreateInstance())
{
pluginInstance.AfterUpdate(Database, PreviousManifest);
}
@@ -348,7 +348,7 @@ namespace Disco.Services.Plugins
// Initialize Plugin
InitializePluginEnvironment(Database);
using (var pluginInstance = this.CreateInstance())
using (var pluginInstance = CreateInstance())
{
pluginInstance.Uninstall(Database, UninstallData, Status);
}
@@ -360,7 +360,7 @@ namespace Disco.Services.Plugins
// Initialize Plugin
InitializePluginEnvironment(Database);
using (var pluginInstance = this.CreateInstance())
using (var pluginInstance = CreateInstance())
{
pluginInstance.Install(Database, Status);
}
@@ -373,7 +373,7 @@ namespace Disco.Services.Plugins
InitializePluginEnvironment(Database);
// Initialize Plugin
using (var pluginInstance = this.CreateInstance())
using (var pluginInstance = CreateInstance())
{
pluginInstance.Initialize(Database);
}
@@ -398,12 +398,12 @@ namespace Disco.Services.Plugins
public PluginConfigurationHandler CreateConfigurationHandler()
{
// Configuration Handler is Required
if (this.ConfigurationHandlerType == null)
if (ConfigurationHandlerType == null)
throw new ArgumentNullException("ConfigurationType");
if (!typeof(PluginConfigurationHandler).IsAssignableFrom(this.ConfigurationHandlerType))
if (!typeof(PluginConfigurationHandler).IsAssignableFrom(ConfigurationHandlerType))
throw new ArgumentException("The Plugin ConfigurationHandlerType must inherit Disco.Services.Plugins.PluginConfigurationHandler", "ConfigurationHandlerType");
var handler = (PluginConfigurationHandler)Activator.CreateInstance(this.ConfigurationHandlerType);
var handler = (PluginConfigurationHandler)Activator.CreateInstance(ConfigurationHandlerType);
handler.Manifest = this;
@@ -414,7 +414,7 @@ namespace Disco.Services.Plugins
{
get
{
return string.Format("/Config/Plugins/{0}", HttpUtility.UrlEncode(this.Id));
return string.Format("/Config/Plugins/{0}", HttpUtility.UrlEncode(Id));
}
}
[JsonIgnore]
@@ -422,31 +422,31 @@ namespace Disco.Services.Plugins
{
get
{
return this.WebHandlerType != null;
return WebHandlerType != null;
}
}
public PluginWebHandler CreateWebHandler(Controller HostController)
{
// Web Handler is Not Required
if (this.WebHandlerType == null)
if (WebHandlerType == null)
return null;
if (!typeof(PluginWebHandler).IsAssignableFrom(this.WebHandlerType))
if (!typeof(PluginWebHandler).IsAssignableFrom(WebHandlerType))
throw new ArgumentException("The Plugin WebHandlerType must inherit Disco.Services.Plugins.PluginWebHandler", "WebHandlerType");
// Determine WebHandler Authorize Attributes
if (this.WebHandlerAuthorizers == null)
if (WebHandlerAuthorizers == null)
{
this.WebHandlerAuthorizers = this.WebHandlerType.GetCustomAttributes<DiscoAuthorizeBaseAttribute>(true).ToArray();
WebHandlerAuthorizers = WebHandlerType.GetCustomAttributes<DiscoAuthorizeBaseAttribute>(true).ToArray();
}
if (this.WebHandlerAuthorizers.Length > 0)
if (WebHandlerAuthorizers.Length > 0)
{
var attributeDenied = this.WebHandlerAuthorizers.FirstOrDefault(a => !a.IsAuthorized(HostController.HttpContext));
var attributeDenied = WebHandlerAuthorizers.FirstOrDefault(a => !a.IsAuthorized(HostController.HttpContext));
if (attributeDenied != null)
throw new AccessDeniedException(attributeDenied.HandleUnauthorizedMessage(), string.Format("[Plugin]::{0}::[Handler]", this.Id));
throw new AccessDeniedException(attributeDenied.HandleUnauthorizedMessage(), string.Format("[Plugin]::{0}::[Handler]", Id));
}
var handler = (PluginWebHandler)Activator.CreateInstance(this.WebHandlerType);
var handler = (PluginWebHandler)Activator.CreateInstance(WebHandlerType);
handler.Manifest = this;
handler.HostController = HostController;
@@ -458,7 +458,7 @@ namespace Disco.Services.Plugins
{
get
{
return string.Format("/Plugin/{0}", HttpUtility.UrlEncode(this.Id));
return string.Format("/Plugin/{0}", HttpUtility.UrlEncode(Id));
}
}
public string WebActionUrl(string Action)
@@ -467,7 +467,7 @@ namespace Disco.Services.Plugins
throw new NotSupportedException("This plugin doesn't have a web handler");
var url = UrlHelper.GenerateUrl("Plugin", null, null,
new RouteValueDictionary(new Dictionary<string, object>() { { "PluginId", this.Id }, { "PluginAction", Action } }),
new RouteValueDictionary(new Dictionary<string, object>() { { "PluginId", Id }, { "PluginAction", Action } }),
RouteTable.Routes, HttpContext.Current.Request.RequestContext, false);
return url;
@@ -481,17 +481,17 @@ namespace Disco.Services.Plugins
if (Resource.Contains(".."))
throw new ArgumentException("Resource Paths cannot navigate to the parent", "Resource");
var resourcePath = Path.Combine(this.PluginLocation, "WebResources", Resource.Replace(@"/", @"\"));
var resourcePath = Path.Combine(PluginLocation, "WebResources", Resource.Replace(@"/", @"\"));
Tuple<string, DateTime> resourceHash;
string resourceKey = string.Format("{0}://{1}", this.Name, Resource);
string resourceKey = string.Format("{0}://{1}", Name, Resource);
if (WebResourceHashes.TryGetValue(resourceKey, out resourceHash))
{
#if DEBUG
var fileDateCheck = System.IO.File.GetLastWriteTime(resourcePath);
if (fileDateCheck == resourceHash.Item2)
#endif
return new Tuple<string, string>(resourcePath, resourceHash.Item1);
return new Tuple<string, string>(resourcePath, resourceHash.Item1);
}
if (!File.Exists(resourcePath))
@@ -513,10 +513,10 @@ namespace Disco.Services.Plugins
}
public string WebResourceUrl(string Resource)
{
var resourcePath = this.WebResourcePath(Resource);
var resourcePath = WebResourcePath(Resource);
var url = UrlHelper.GenerateUrl("Plugin_Resources", null, null,
new RouteValueDictionary(new Dictionary<string, object>() { { "PluginId", this.Id }, { "res", Resource } }),
new RouteValueDictionary(new Dictionary<string, object>() { { "PluginId", Id }, { "res", Resource } }),
RouteTable.Routes, HttpContext.Current.Request.RequestContext, false);
url += string.Format("?v={0}", resourcePath.Item2);
@@ -526,7 +526,7 @@ namespace Disco.Services.Plugins
public void LogException(Exception PluginException)
{
PluginsLog.LogPluginException(this.ToString(), PluginException);
PluginsLog.LogPluginException(ToString(), PluginException);
}
public void LogWarning(string Message)
{
@@ -547,7 +547,7 @@ namespace Disco.Services.Plugins
public override string ToString()
{
return string.Format("{0} [{1} v{2}]", this.Name, this.Id, this.VersionFormatted);
return string.Format("{0} [{1} v{2}]", Name, Id, VersionFormatted);
}
}
}
+2
View File
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ClosedXML" version="0.85.0" targetFramework="net45" />
<package id="DocumentFormat.OpenXml" version="2.5" targetFramework="net45" />
<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
<package id="Exceptionless" version="1.5.2092" targetFramework="net45" />
<package id="LumenWorks.Framework.IO" version="3.8.0" targetFramework="net45" />
@@ -103,6 +103,8 @@
<Compile Include="DataModelExtension\DocumentTemplateExtensions.cs" />
<Compile Include="DataModelExtension\JobSubTypeExtensions.cs" />
<Compile Include="DataModelExtension\JobTypeExtensions.cs" />
<Compile Include="MvcExtensions\File\FileContentSpanResult.cs" />
<Compile Include="MvcExtensions\File\FileExtensions.cs" />
<Compile Include="MvcExtensions\JsonNet\JsonDotNetValueProviderFactory.cs" />
<Compile Include="MvcExtensions\JsonNet\JsonNetResult.cs" />
<Compile Include="MvcExtensions\PartialCompiled\PartialCompiledHtmlExtensions.cs" />
@@ -0,0 +1,34 @@
using System;
using System.Web;
using System.Web.Mvc;
namespace Disco.Web.Extensions
{
public class FileContentSpanResult : FileResult
{
public byte[] FileBuffer { get; private set; }
public int Start { get; private set; }
public int Length { get; private set; }
public FileContentSpanResult(byte[] fileBuffer, int start, int length, string contentType) : base(contentType)
{
if (fileBuffer == null)
throw new ArgumentNullException(nameof(fileBuffer));
if (start < 0 || start >= fileBuffer.Length)
throw new ArgumentOutOfRangeException(nameof(start));
if (start + length > fileBuffer.Length)
throw new ArgumentOutOfRangeException(nameof(length));
FileBuffer = fileBuffer;
Start = start;
Length = length;
}
protected override void WriteFile(HttpResponseBase response)
{
response.OutputStream.Write(this.FileBuffer, Start, Length);
}
}
}
@@ -0,0 +1,17 @@
using System.Web.Mvc;
namespace Disco.Web.Extensions
{
public static class FileExtensions
{
public static FileContentSpanResult File(this IController controller, byte[] fileBuffer, int start, int length, string contentType, string fileDownloadName)
{
return new FileContentSpanResult(fileBuffer, start, length, contentType)
{
FileDownloadName = fileDownloadName
};
}
}
}
@@ -11,6 +11,7 @@ using Disco.Services.Interop;
using Disco.Services.Interop.ActiveDirectory;
using Disco.Services.Users;
using Disco.Services.Web;
using Disco.Web.Extensions;
using Disco.Web.Models.Device;
using System;
using System.Collections.Generic;
@@ -611,15 +612,15 @@ namespace Disco.Web.Areas.API.Controllers
#region Importing
internal const string ImportSessionCacheKey = "DeviceImportContext_{0}";
internal static void Import_StoreContext(DeviceImportContext Context)
internal static void Import_StoreContext(IDeviceImportContext Context)
{
string key = string.Format(ImportSessionCacheKey, Context.SessionId);
HttpRuntime.Cache.Insert(key, Context, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
}
internal static DeviceImportContext Import_RetrieveContext(string SessionId, bool Remove = false)
internal static IDeviceImportContext Import_RetrieveContext(string SessionId, bool Remove = false)
{
string key = string.Format(ImportSessionCacheKey, SessionId);
DeviceImportContext context = HttpRuntime.Cache.Get(key) as DeviceImportContext;
IDeviceImportContext context = HttpRuntime.Cache.Get(key) as IDeviceImportContext;
if (Remove && context != null)
HttpRuntime.Cache.Remove(key);
@@ -654,7 +655,7 @@ namespace Disco.Web.Areas.API.Controllers
if (context == null)
throw new ArgumentException("The Import Session Id is invalid or the session timed out (60 minutes), try importing again", "Id");
context.UpdateHeaderTypes(Headers);
context.UpdateColumnTypes(Headers);
var status = DeviceImportParseTask.ScheduleNow(context);
@@ -729,12 +730,25 @@ namespace Disco.Web.Areas.API.Controllers
if (context == null)
throw new ArgumentException("The Id specified is invalid, or the export data expired (60 minutes)", "Id");
if (context.Result == null || context.Result.CsvResult == null)
if (context.Result == null || context.Result.Result == null)
throw new ArgumentException("The export session is still running, or failed to complete successfully", "Id");
var filename = string.Format("DiscoDeviceExport-{0:yyyyMMdd-HHmmss}.csv", context.TaskStatus.StartedTimestamp.Value);
string filename;
string mimeType;
if (context.Options.ExcelFormat)
{
filename = $"DiscoDeviceExport-{context.TaskStatus.StartedTimestamp.Value:yyyyMMdd-HHmmss}.xlsx";
mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
else
{
filename = $"DiscoDeviceExport-{context.TaskStatus.StartedTimestamp.Value:yyyyMMdd-HHmmss}.csv";
mimeType = "text/csv";
}
return File(context.Result.CsvResult.ToArray(), "text/csv", filename);
var fileStream = context.Result.Result;
return this.File(fileStream.GetBuffer(), 0, (int)fileStream.Length, mimeType, filename);
}
#endregion
+1 -1
View File
@@ -11,7 +11,7 @@ namespace Disco.Web.Models.Device
{
public class ImportModel : DeviceImportModel
{
[Required, Display(Name = "CSV Import File")]
[Required, Display(Name = "Import File")]
public HttpPostedFileBase ImportFile { get; set; }
[Required, Display(Name = "Has Header")]
+1 -1
View File
@@ -35,7 +35,7 @@
<tr>
<th>&nbsp;</th>
<td>
@Html.CheckBoxFor(m => m.Options.ExcelCsvFormat) <label for="Options_ExcelCsvFormat">Microsoft Excel CSV Format</label>
@Html.CheckBoxFor(m => m.Options.ExcelFormat) <label for="Options_ExcelFormat">Microsoft Excel Format</label>
</td>
</tr>
</table>
+38 -39
View File
@@ -2,7 +2,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34014
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@@ -27,7 +27,6 @@ namespace Disco.Web.Views.Device
using System.Web.UI;
using System.Web.WebPages;
using Disco;
using Disco.BI.Extensions;
using Disco.Models.Repository;
#line 2 "..\..\Views\Device\Export.cshtml"
@@ -173,17 +172,17 @@ WriteLiteral(" ");
#line 38 "..\..\Views\Device\Export.cshtml"
Write(Html.CheckBoxFor(m => m.Options.ExcelCsvFormat));
Write(Html.CheckBoxFor(m => m.Options.ExcelFormat));
#line default
#line hidden
WriteLiteral(" <label");
WriteLiteral(" for=\"Options_ExcelCsvFormat\"");
WriteLiteral(" for=\"Options_ExcelFormat\"");
WriteLiteral(">Microsoft Excel CSV Format</label>\r\n </td>\r\n <" +
"/tr>\r\n </table>\r\n </div>\r\n");
WriteLiteral(">Microsoft Excel Format</label>\r\n </td>\r\n </tr>" +
"\r\n </table>\r\n </div>\r\n");
WriteLiteral(" <div");
@@ -311,40 +310,40 @@ WriteLiteral(">\r\n");
#line hidden
WriteLiteral(" <li");
WriteAttribute("title", Tuple.Create(" title=\"", 3833), Tuple.Create("\"", 3864)
WriteAttribute("title", Tuple.Create(" title=\"", 3823), Tuple.Create("\"", 3854)
#line 66 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 3841), Tuple.Create<System.Object, System.Int32>(optionItem.Description
, Tuple.Create(Tuple.Create("", 3831), Tuple.Create<System.Object, System.Int32>(optionItem.Description
#line default
#line hidden
, 3841), false)
, 3831), false)
);
WriteLiteral(">\r\n <input");
WriteLiteral(" type=\"checkbox\"");
WriteAttribute("id", Tuple.Create(" id=\"", 3946), Tuple.Create("\"", 3983)
, Tuple.Create(Tuple.Create("", 3951), Tuple.Create("Options_", 3951), true)
WriteAttribute("id", Tuple.Create(" id=\"", 3936), Tuple.Create("\"", 3973)
, Tuple.Create(Tuple.Create("", 3941), Tuple.Create("Options_", 3941), true)
#line 67 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 3959), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
, Tuple.Create(Tuple.Create("", 3949), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
#line default
#line hidden
, 3959), false)
, 3949), false)
);
WriteAttribute("name", Tuple.Create(" name=\"", 3984), Tuple.Create("\"", 4023)
, Tuple.Create(Tuple.Create("", 3991), Tuple.Create("Options.", 3991), true)
WriteAttribute("name", Tuple.Create(" name=\"", 3974), Tuple.Create("\"", 4013)
, Tuple.Create(Tuple.Create("", 3981), Tuple.Create("Options.", 3981), true)
#line 67 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 3999), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
, Tuple.Create(Tuple.Create("", 3989), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
#line default
#line hidden
, 3999), false)
, 3989), false)
);
WriteLiteral(" value=\"true\"");
@@ -360,15 +359,15 @@ WriteLiteral(" ");
#line hidden
WriteLiteral("/><label");
WriteAttribute("for", Tuple.Create(" for=\"", 4093), Tuple.Create("\"", 4131)
, Tuple.Create(Tuple.Create("", 4099), Tuple.Create("Options_", 4099), true)
WriteAttribute("for", Tuple.Create(" for=\"", 4083), Tuple.Create("\"", 4121)
, Tuple.Create(Tuple.Create("", 4089), Tuple.Create("Options_", 4089), true)
#line 67 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 4107), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
, Tuple.Create(Tuple.Create("", 4097), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
#line default
#line hidden
, 4107), false)
, 4097), false)
);
WriteLiteral(">");
@@ -416,40 +415,40 @@ WriteLiteral(">\r\n");
#line hidden
WriteLiteral(" <li");
WriteAttribute("title", Tuple.Create(" title=\"", 4666), Tuple.Create("\"", 4697)
WriteAttribute("title", Tuple.Create(" title=\"", 4656), Tuple.Create("\"", 4687)
#line 75 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 4674), Tuple.Create<System.Object, System.Int32>(optionItem.Description
, Tuple.Create(Tuple.Create("", 4664), Tuple.Create<System.Object, System.Int32>(optionItem.Description
#line default
#line hidden
, 4674), false)
, 4664), false)
);
WriteLiteral(">\r\n <input");
WriteLiteral(" type=\"checkbox\"");
WriteAttribute("id", Tuple.Create(" id=\"", 4779), Tuple.Create("\"", 4816)
, Tuple.Create(Tuple.Create("", 4784), Tuple.Create("Options_", 4784), true)
WriteAttribute("id", Tuple.Create(" id=\"", 4769), Tuple.Create("\"", 4806)
, Tuple.Create(Tuple.Create("", 4774), Tuple.Create("Options_", 4774), true)
#line 76 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 4792), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
, Tuple.Create(Tuple.Create("", 4782), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
#line default
#line hidden
, 4792), false)
, 4782), false)
);
WriteAttribute("name", Tuple.Create(" name=\"", 4817), Tuple.Create("\"", 4856)
, Tuple.Create(Tuple.Create("", 4824), Tuple.Create("Options.", 4824), true)
WriteAttribute("name", Tuple.Create(" name=\"", 4807), Tuple.Create("\"", 4846)
, Tuple.Create(Tuple.Create("", 4814), Tuple.Create("Options.", 4814), true)
#line 76 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 4832), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
, Tuple.Create(Tuple.Create("", 4822), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
#line default
#line hidden
, 4832), false)
, 4822), false)
);
WriteLiteral(" value=\"true\"");
@@ -465,15 +464,15 @@ WriteLiteral(" ");
#line hidden
WriteLiteral("/><label");
WriteAttribute("for", Tuple.Create(" for=\"", 4926), Tuple.Create("\"", 4964)
, Tuple.Create(Tuple.Create("", 4932), Tuple.Create("Options_", 4932), true)
WriteAttribute("for", Tuple.Create(" for=\"", 4916), Tuple.Create("\"", 4954)
, Tuple.Create(Tuple.Create("", 4922), Tuple.Create("Options_", 4922), true)
#line 76 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 4940), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
, Tuple.Create(Tuple.Create("", 4930), Tuple.Create<System.Object, System.Int32>(optionItem.PropertyName
#line default
#line hidden
, 4940), false)
, 4930), false)
);
WriteLiteral(">");
@@ -604,14 +603,14 @@ WriteLiteral(" record");
#line hidden
WriteLiteral(" were successfully exported.</h4>\r\n <a");
WriteAttribute("href", Tuple.Create(" href=\"", 9307), Tuple.Create("\"", 9379)
WriteAttribute("href", Tuple.Create(" href=\"", 9297), Tuple.Create("\"", 9369)
#line 177 "..\..\Views\Device\Export.cshtml"
, Tuple.Create(Tuple.Create("", 9314), Tuple.Create<System.Object, System.Int32>(Url.Action(MVC.API.Device.ExportRetrieve(Model.ExportSessionId))
, Tuple.Create(Tuple.Create("", 9304), Tuple.Create<System.Object, System.Int32>(Url.Action(MVC.API.Device.ExportRetrieve(Model.ExportSessionId))
#line default
#line hidden
, 9314), false)
, 9304), false)
);
WriteLiteral(" class=\"button\"");
+8 -2
View File
@@ -55,10 +55,16 @@
</script>
}
<div id="Devices_Import_Documentation">
<h3>CSV Import Specification</h3>
<h3>XLSX/CSV Import Specification</h3>
<h4>Format</h4>
<ul>
<li>The import file must be in <strong>comma-separated values format</strong> (<a href="http://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">CSV Reference</a>).</li>
<li>
The import file must be in either:
<ul>
<li><strong>CSV (comma-separated values) format</strong> (<a href="http://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">CSV Reference</a>), or</li>
<li><strong>XLSX (Microsoft Excel) format</strong></li>
</ul>
</li>
<li>Be conscious of editors removing leading zeros from serial numbers (ie: Microsoft Excel).</li>
</ul>
<h4>Fields</h4>
+53 -44
View File
@@ -2,7 +2,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34014
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@@ -27,7 +27,6 @@ namespace Disco.Web.Views.Device
using System.Web.UI;
using System.Web.WebPages;
using Disco;
using Disco.BI.Extensions;
using Disco.Models.Repository;
#line 2 "..\..\Views\Device\Import.cshtml"
@@ -211,17 +210,27 @@ WriteLiteral(" <div");
WriteLiteral(" id=\"Devices_Import_Documentation\"");
WriteLiteral(">\r\n <h3>CSV Import Specification</h3>\r\n <h4>Format</h4>\r\n <u" +
"l>\r\n <li>The import file must be in <strong>comma-separated values fo" +
"rmat</strong> (<a");
WriteLiteral(@">
<h3>XLSX/CSV Import Specification</h3>
<h4>Format</h4>
<ul>
<li>
The import file must be in either:
<ul>
<li><strong>CSV (comma-separated values) format</strong> (<a");
WriteLiteral(" href=\"http://en.wikipedia.org/wiki/Comma-separated_values\"");
WriteLiteral(" target=\"_blank\"");
WriteLiteral(">CSV Reference</a>).</li>\r\n <li>Be conscious of editors removing leadi" +
"ng zeros from serial numbers (ie: Microsoft Excel).</li>\r\n </ul>\r\n " +
" <h4>Fields</h4>\r\n <div");
WriteLiteral(@">CSV Reference</a>), or</li>
<li><strong>XLSX (Microsoft Excel) format</strong></li>
</ul>
</li>
<li>Be conscious of editors removing leading zeros from serial numbers (ie: Microsoft Excel).</li>
</ul>
<h4>Fields</h4>
<div");
WriteLiteral(" class=\"smallMessage\"");
@@ -239,13 +248,13 @@ WriteLiteral(">Field Name</th>\r\n <th>Description</th>\r\n
"\r\n </thead>\r\n <tbody>\r\n");
#line 74 "..\..\Views\Device\Import.cshtml"
#line 80 "..\..\Views\Device\Import.cshtml"
#line default
#line hidden
#line 74 "..\..\Views\Device\Import.cshtml"
#line 80 "..\..\Views\Device\Import.cshtml"
foreach (var field in Model.HeaderTypes)
{
@@ -255,7 +264,7 @@ WriteLiteral(">Field Name</th>\r\n <th>Description</th>\r\n
WriteLiteral(" <tr>\r\n <th>");
#line 77 "..\..\Views\Device\Import.cshtml"
#line 83 "..\..\Views\Device\Import.cshtml"
Write(field.Item2);
@@ -266,7 +275,7 @@ WriteLiteral("</th>\r\n <td>\r\n");
WriteLiteral(" ");
#line 79 "..\..\Views\Device\Import.cshtml"
#line 85 "..\..\Views\Device\Import.cshtml"
Write(field.Item3);
@@ -275,13 +284,13 @@ WriteLiteral(" ");
WriteLiteral("\r\n");
#line 80 "..\..\Views\Device\Import.cshtml"
#line 86 "..\..\Views\Device\Import.cshtml"
#line default
#line hidden
#line 80 "..\..\Views\Device\Import.cshtml"
#line 86 "..\..\Views\Device\Import.cshtml"
if (field.Item1 == DeviceImportFieldTypes.DeviceSerialNumber.ToString())
{
@@ -291,7 +300,7 @@ WriteLiteral("\r\n");
WriteLiteral(" <strong>Required</strong>\r\n");
#line 83 "..\..\Views\Device\Import.cshtml"
#line 89 "..\..\Views\Device\Import.cshtml"
}
else if (field.Item1 == DeviceImportFieldTypes.ModelId.ToString())
{
@@ -308,7 +317,7 @@ WriteLiteral(" id=\"Devices_Import_Documentation_DeviceModels_Button\"");
WriteLiteral(">Show IDs</a>)</span>\r\n");
#line 87 "..\..\Views\Device\Import.cshtml"
#line 93 "..\..\Views\Device\Import.cshtml"
}
else if (field.Item1 == DeviceImportFieldTypes.ProfileId.ToString())
{
@@ -325,7 +334,7 @@ WriteLiteral(" id=\"Devices_Import_Documentation_DeviceProfiles_Button\"");
WriteLiteral(">Show IDs</a>)</span>\r\n");
#line 91 "..\..\Views\Device\Import.cshtml"
#line 97 "..\..\Views\Device\Import.cshtml"
}
else if (field.Item1 == DeviceImportFieldTypes.BatchId.ToString())
{
@@ -342,7 +351,7 @@ WriteLiteral(" id=\"Devices_Import_Documentation_DeviceBatches_Button\"");
WriteLiteral(">Show IDs</a>)</span>\r\n");
#line 95 "..\..\Views\Device\Import.cshtml"
#line 101 "..\..\Views\Device\Import.cshtml"
}
@@ -351,7 +360,7 @@ WriteLiteral(">Show IDs</a>)</span>\r\n");
WriteLiteral("\r\n </td>\r\n </tr> \r\n");
#line 99 "..\..\Views\Device\Import.cshtml"
#line 105 "..\..\Views\Device\Import.cshtml"
}
@@ -382,13 +391,13 @@ WriteLiteral(@">
");
#line 115 "..\..\Views\Device\Import.cshtml"
#line 121 "..\..\Views\Device\Import.cshtml"
#line default
#line hidden
#line 115 "..\..\Views\Device\Import.cshtml"
#line 121 "..\..\Views\Device\Import.cshtml"
foreach (var dm in Model.DeviceModels)
{
@@ -398,7 +407,7 @@ WriteLiteral(@">
WriteLiteral(" <tr>\r\n <td>");
#line 118 "..\..\Views\Device\Import.cshtml"
#line 124 "..\..\Views\Device\Import.cshtml"
Write(Html.ActionLink(dm.Id.ToString(), MVC.Config.DeviceModel.Index(dm.Id)));
@@ -407,7 +416,7 @@ WriteLiteral(" <tr>\r\n <td>")
WriteLiteral("</td>\r\n <td>");
#line 119 "..\..\Views\Device\Import.cshtml"
#line 125 "..\..\Views\Device\Import.cshtml"
Write(dm.ToString());
@@ -416,7 +425,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n <td>");
#line 120 "..\..\Views\Device\Import.cshtml"
#line 126 "..\..\Views\Device\Import.cshtml"
Write(dm.Manufacturer);
@@ -425,7 +434,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n <td>");
#line 121 "..\..\Views\Device\Import.cshtml"
#line 127 "..\..\Views\Device\Import.cshtml"
Write(dm.Model);
@@ -434,7 +443,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n </tr>\r\n");
#line 123 "..\..\Views\Device\Import.cshtml"
#line 129 "..\..\Views\Device\Import.cshtml"
}
@@ -465,13 +474,13 @@ WriteLiteral(@">
");
#line 139 "..\..\Views\Device\Import.cshtml"
#line 145 "..\..\Views\Device\Import.cshtml"
#line default
#line hidden
#line 139 "..\..\Views\Device\Import.cshtml"
#line 145 "..\..\Views\Device\Import.cshtml"
foreach (var dp in Model.DeviceProfiles)
{
@@ -481,7 +490,7 @@ WriteLiteral(@">
WriteLiteral(" <tr>\r\n <td>");
#line 142 "..\..\Views\Device\Import.cshtml"
#line 148 "..\..\Views\Device\Import.cshtml"
Write(Html.ActionLink(dp.Id.ToString(), MVC.Config.DeviceProfile.Index(dp.Id)));
@@ -490,7 +499,7 @@ WriteLiteral(" <tr>\r\n <td>")
WriteLiteral("</td>\r\n <td>");
#line 143 "..\..\Views\Device\Import.cshtml"
#line 149 "..\..\Views\Device\Import.cshtml"
Write(dp.Name);
@@ -499,7 +508,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n <td>");
#line 144 "..\..\Views\Device\Import.cshtml"
#line 150 "..\..\Views\Device\Import.cshtml"
Write(dp.ShortName);
@@ -508,7 +517,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n <td>");
#line 145 "..\..\Views\Device\Import.cshtml"
#line 151 "..\..\Views\Device\Import.cshtml"
Write(dp.Description);
@@ -517,7 +526,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n </tr>\r\n");
#line 147 "..\..\Views\Device\Import.cshtml"
#line 153 "..\..\Views\Device\Import.cshtml"
}
@@ -547,13 +556,13 @@ WriteLiteral(@">
");
#line 162 "..\..\Views\Device\Import.cshtml"
#line 168 "..\..\Views\Device\Import.cshtml"
#line default
#line hidden
#line 162 "..\..\Views\Device\Import.cshtml"
#line 168 "..\..\Views\Device\Import.cshtml"
foreach (var db in Model.DeviceBatches)
{
@@ -563,7 +572,7 @@ WriteLiteral(@">
WriteLiteral(" <tr>\r\n <td>");
#line 165 "..\..\Views\Device\Import.cshtml"
#line 171 "..\..\Views\Device\Import.cshtml"
Write(Html.ActionLink(db.Id.ToString(), MVC.Config.DeviceBatch.Index(db.Id)));
@@ -572,7 +581,7 @@ WriteLiteral(" <tr>\r\n <td>")
WriteLiteral("</td>\r\n <td>");
#line 166 "..\..\Views\Device\Import.cshtml"
#line 172 "..\..\Views\Device\Import.cshtml"
Write(db.Name);
@@ -581,7 +590,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n <td>");
#line 167 "..\..\Views\Device\Import.cshtml"
#line 173 "..\..\Views\Device\Import.cshtml"
Write(CommonHelpers.FriendlyDate(db.PurchaseDate));
@@ -590,7 +599,7 @@ WriteLiteral("</td>\r\n <td>");
WriteLiteral("</td>\r\n </tr>\r\n");
#line 169 "..\..\Views\Device\Import.cshtml"
#line 175 "..\..\Views\Device\Import.cshtml"
}
@@ -620,7 +629,7 @@ WriteLiteral(" </tbody>\r\n </table>\r\n </div>
" </script>\r\n </div>\r\n</div>\r\n");
#line 210 "..\..\Views\Device\Import.cshtml"
#line 216 "..\..\Views\Device\Import.cshtml"
if (Model.CompletedImportSessionContext != null)
{
@@ -642,7 +651,7 @@ WriteLiteral(" class=\"fa fa-lg fa-check\"");
WriteLiteral("></i>Successfully imported/updated ");
#line 213 "..\..\Views\Device\Import.cshtml"
#line 219 "..\..\Views\Device\Import.cshtml"
Write(Model.CompletedImportSessionContext.AffectedRecords);
@@ -651,7 +660,7 @@ WriteLiteral("></i>Successfully imported/updated ");
WriteLiteral(" device");
#line 213 "..\..\Views\Device\Import.cshtml"
#line 219 "..\..\Views\Device\Import.cshtml"
Write(Model.CompletedImportSessionContext.AffectedRecords != 1 ? "s" : null);
@@ -660,7 +669,7 @@ WriteLiteral(" device");
WriteLiteral(".</h3>\r\n <div>File: <code>");
#line 214 "..\..\Views\Device\Import.cshtml"
#line 220 "..\..\Views\Device\Import.cshtml"
Write(Model.CompletedImportSessionContext.Filename);
@@ -687,7 +696,7 @@ WriteLiteral(@" <script>
");
#line 232 "..\..\Views\Device\Import.cshtml"
#line 238 "..\..\Views\Device\Import.cshtml"
}
#line default
+20 -16
View File
@@ -2,15 +2,15 @@
@{
Authorization.Require(Claims.Device.Actions.Import);
ViewBag.Title = Html.ToBreadcrumb("Devices", MVC.Device.Index(), "Import Devices", MVC.Device.Import(), string.Format("File: {0}", Model.Context.Filename));
ViewBag.Title = Html.ToBreadcrumb("Devices", MVC.Device.Index(), "Import Devices", MVC.Device.Import(), string.Format("File: {0}", Model.Context.DatasetName));
}
<div id="Devices_Import_Headers">
<h2>Define Import Columns</h2>
@if (Model.Context.RawData.Count > 10)
@if (Model.Context.RecordCount > 10)
{
<h4 class="alert">@Model.Context.RawData.Count records were loaded, only the first 10 are shown here.</h4>
<h4 class="alert">@Model.Context.RecordCount records were loaded, only the first 10 are shown here.</h4>
}
<h4 id="Devices_Import_Headers_DeviceSerialNumberRequired" class="error">The Device Serial Number column must be defined.</h4>
@@ -19,17 +19,17 @@
<table class="tableData">
<thead>
<tr>
@foreach (var header in Model.Context.Header.Select((h, i) => Tuple.Create(h, i)))
@foreach (var header in Model.Context.Columns)
{
<th data-headerindex="@header.Item2" class="header@(header.Item1.Item2.ToString())">@header.Item1.Item1</th>
<th data-headerindex="@header.Index" class="header@(header.Type.ToString())">@header.Name</th>
}
</tr>
<tr>
@foreach (var header in Model.Context.Header.Select((h, i) => Tuple.Create(h, i)))
@foreach (var header in Model.Context.Columns)
{
<td data-headerindex="@header.Item2" class="header@(header.Item1.Item2.ToString())">
<ul class="importHeaderType" data-headerindex="@header.Item2" data-headertype="@header.Item1.Item2.ToString()">
<li><a href="#"><span class="headerTypeTitle">@(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Item1.Item2).Item2)</span></a>
<td data-headerindex="@header.Index" class="header@(header.Type.ToString())">
<ul class="importHeaderType" data-headerindex="@header.Index" data-headertype="@header.Type.ToString()">
<li><a href="#"><span class="headerTypeTitle">@(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Type).Item2)</span></a>
<ul>
@foreach (var headerType in Model.HeaderTypes)
{
@@ -43,14 +43,18 @@
</tr>
</thead>
<tbody>
@foreach (var record in Model.Context.RawData.Take(10))
@using (var dataReader = Model.Context.GetDataReader())
{
<tr>
@foreach (var field in record.Select((h, i) => Tuple.Create(h, i)))
{
<td data-headerindex="@field.Item2">@field.Item1</td>
}
</tr>
for (int r = 0; r < Math.Min(10, Model.Context.RecordCount); r++)
{
dataReader.Read();
<tr>
@for (int c = 0; c < Model.Context.ColumnCount; c++)
{
<td data-headerindex="@c">@dataReader.GetString(c)</td>
}
</tr>
}
}
</tbody>
</table>
@@ -2,7 +2,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34014
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@@ -27,7 +27,6 @@ namespace Disco.Web.Views.Device
using System.Web.UI;
using System.Web.WebPages;
using Disco;
using Disco.BI.Extensions;
using Disco.Models.Repository;
using Disco.Services;
using Disco.Services.Authorization;
@@ -49,7 +48,7 @@ namespace Disco.Web.Views.Device
Authorization.Require(Claims.Device.Actions.Import);
ViewBag.Title = Html.ToBreadcrumb("Devices", MVC.Device.Index(), "Import Devices", MVC.Device.Import(), string.Format("File: {0}", Model.Context.Filename));
ViewBag.Title = Html.ToBreadcrumb("Devices", MVC.Device.Index(), "Import Devices", MVC.Device.Import(), string.Format("File: {0}", Model.Context.DatasetName));
#line default
@@ -68,7 +67,7 @@ WriteLiteral(">\r\n\r\n <h2>Define Import Columns</h2>\r\n\r\n");
#line hidden
#line 11 "..\..\Views\Device\ImportHeaders.cshtml"
if (Model.Context.RawData.Count > 10)
if (Model.Context.RecordCount > 10)
{
@@ -82,7 +81,7 @@ WriteLiteral(">");
#line 13 "..\..\Views\Device\ImportHeaders.cshtml"
Write(Model.Context.RawData.Count);
Write(Model.Context.RecordCount);
#line default
@@ -120,7 +119,7 @@ WriteLiteral(">\r\n <thead>\r\n <tr>\r\n");
#line hidden
#line 22 "..\..\Views\Device\ImportHeaders.cshtml"
foreach (var header in Model.Context.Header.Select((h, i) => Tuple.Create(h, i)))
foreach (var header in Model.Context.Columns)
{
@@ -132,29 +131,29 @@ WriteLiteral(" data-headerindex=\"");
#line 24 "..\..\Views\Device\ImportHeaders.cshtml"
Write(header.Item2);
Write(header.Index);
#line default
#line hidden
WriteLiteral("\"");
WriteAttribute("class", Tuple.Create(" class=\"", 984), Tuple.Create("\"", 1030)
, Tuple.Create(Tuple.Create("", 992), Tuple.Create("header", 992), true)
WriteAttribute("class", Tuple.Create(" class=\"", 947), Tuple.Create("\"", 986)
, Tuple.Create(Tuple.Create("", 955), Tuple.Create("header", 955), true)
#line 24 "..\..\Views\Device\ImportHeaders.cshtml"
, Tuple.Create(Tuple.Create("", 998), Tuple.Create<System.Object, System.Int32>(header.Item1.Item2.ToString()
, Tuple.Create(Tuple.Create("", 961), Tuple.Create<System.Object, System.Int32>(header.Type.ToString()
#line default
#line hidden
, 998), false)
, 961), false)
);
WriteLiteral(">");
#line 24 "..\..\Views\Device\ImportHeaders.cshtml"
Write(header.Item1.Item1);
Write(header.Name);
#line default
@@ -178,7 +177,7 @@ WriteLiteral(" </tr>\r\n <tr>\r\n");
#line hidden
#line 28 "..\..\Views\Device\ImportHeaders.cshtml"
foreach (var header in Model.Context.Header.Select((h, i) => Tuple.Create(h, i)))
foreach (var header in Model.Context.Columns)
{
@@ -190,22 +189,22 @@ WriteLiteral(" data-headerindex=\"");
#line 30 "..\..\Views\Device\ImportHeaders.cshtml"
Write(header.Item2);
Write(header.Index);
#line default
#line hidden
WriteLiteral("\"");
WriteAttribute("class", Tuple.Create(" class=\"", 1313), Tuple.Create("\"", 1359)
, Tuple.Create(Tuple.Create("", 1321), Tuple.Create("header", 1321), true)
WriteAttribute("class", Tuple.Create(" class=\"", 1226), Tuple.Create("\"", 1265)
, Tuple.Create(Tuple.Create("", 1234), Tuple.Create("header", 1234), true)
#line 30 "..\..\Views\Device\ImportHeaders.cshtml"
, Tuple.Create(Tuple.Create("", 1327), Tuple.Create<System.Object, System.Int32>(header.Item1.Item2.ToString()
, Tuple.Create(Tuple.Create("", 1240), Tuple.Create<System.Object, System.Int32>(header.Type.ToString()
#line default
#line hidden
, 1327), false)
, 1240), false)
);
WriteLiteral(">\r\n <ul");
@@ -216,7 +215,7 @@ WriteLiteral(" data-headerindex=\"");
#line 31 "..\..\Views\Device\ImportHeaders.cshtml"
Write(header.Item2);
Write(header.Index);
#line default
@@ -227,7 +226,7 @@ WriteLiteral(" data-headertype=\"");
#line 31 "..\..\Views\Device\ImportHeaders.cshtml"
Write(header.Item1.Item2.ToString());
Write(header.Type.ToString());
#line default
@@ -246,7 +245,7 @@ WriteLiteral(">");
#line 32 "..\..\Views\Device\ImportHeaders.cshtml"
Write(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Item1.Item2).Item2);
Write(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Type).Item2);
#line default
@@ -322,35 +321,38 @@ WriteLiteral(" </tr>\r\n </thead>\r\n <tbod
#line hidden
#line 46 "..\..\Views\Device\ImportHeaders.cshtml"
foreach (var record in Model.Context.RawData.Take(10))
using (var dataReader = Model.Context.GetDataReader())
{
for (int r = 0; r < Math.Min(10, Model.Context.RecordCount); r++)
{
dataReader.Read();
#line default
#line hidden
WriteLiteral(" <tr>\r\n");
WriteLiteral(" <tr>\r\n");
#line 49 "..\..\Views\Device\ImportHeaders.cshtml"
#line 52 "..\..\Views\Device\ImportHeaders.cshtml"
#line default
#line hidden
#line 49 "..\..\Views\Device\ImportHeaders.cshtml"
foreach (var field in record.Select((h, i) => Tuple.Create(h, i)))
{
#line 52 "..\..\Views\Device\ImportHeaders.cshtml"
for (int c = 0; c < Model.Context.ColumnCount; c++)
{
#line default
#line hidden
WriteLiteral(" <td");
WriteLiteral(" <td");
WriteLiteral(" data-headerindex=\"");
#line 51 "..\..\Views\Device\ImportHeaders.cshtml"
Write(field.Item2);
#line 54 "..\..\Views\Device\ImportHeaders.cshtml"
Write(c);
#line default
@@ -360,8 +362,8 @@ WriteLiteral("\"");
WriteLiteral(">");
#line 51 "..\..\Views\Device\ImportHeaders.cshtml"
Write(field.Item1);
#line 54 "..\..\Views\Device\ImportHeaders.cshtml"
Write(dataReader.GetString(c));
#line default
@@ -369,16 +371,17 @@ WriteLiteral(">");
WriteLiteral("</td>\r\n");
#line 52 "..\..\Views\Device\ImportHeaders.cshtml"
}
#line 55 "..\..\Views\Device\ImportHeaders.cshtml"
}
#line default
#line hidden
WriteLiteral(" </tr>\r\n");
WriteLiteral(" </tr>\r\n");
#line 54 "..\..\Views\Device\ImportHeaders.cshtml"
#line 57 "..\..\Views\Device\ImportHeaders.cshtml"
}
}
@@ -391,13 +394,13 @@ WriteLiteral(" class=\"actionBar\"");
WriteLiteral(">\r\n");
#line 59 "..\..\Views\Device\ImportHeaders.cshtml"
#line 63 "..\..\Views\Device\ImportHeaders.cshtml"
#line default
#line hidden
#line 59 "..\..\Views\Device\ImportHeaders.cshtml"
#line 63 "..\..\Views\Device\ImportHeaders.cshtml"
using (Html.BeginForm(MVC.API.Device.ImportParse(Model.Context.SessionId, null)))
{
@@ -415,7 +418,7 @@ WriteLiteral(" class=\"button\"");
WriteLiteral(">Parse Device Import</a> \r\n");
#line 62 "..\..\Views\Device\ImportHeaders.cshtml"
#line 66 "..\..\Views\Device\ImportHeaders.cshtml"
}
@@ -439,13 +442,13 @@ WriteLiteral("></i>Parsing device import...</h4>\r\n</div>\r\n<script>\r\n $(
" var headerTypes = {\r\n");
#line 71 "..\..\Views\Device\ImportHeaders.cshtml"
#line 75 "..\..\Views\Device\ImportHeaders.cshtml"
#line default
#line hidden
#line 71 "..\..\Views\Device\ImportHeaders.cshtml"
#line 75 "..\..\Views\Device\ImportHeaders.cshtml"
foreach (var h in Model.HeaderTypes)
{
@@ -457,7 +460,7 @@ WriteLiteral(" ");
WriteLiteral("\'");
#line 73 "..\..\Views\Device\ImportHeaders.cshtml"
#line 77 "..\..\Views\Device\ImportHeaders.cshtml"
Write(h.Item1);
@@ -466,7 +469,7 @@ WriteLiteral("\'");
WriteLiteral("\': \'");
#line 73 "..\..\Views\Device\ImportHeaders.cshtml"
#line 77 "..\..\Views\Device\ImportHeaders.cshtml"
Write(h.Item2);
@@ -477,7 +480,7 @@ WriteLiteral("\',");
WriteLiteral("\r\n");
#line 74 "..\..\Views\Device\ImportHeaders.cshtml"
#line 78 "..\..\Views\Device\ImportHeaders.cshtml"
}
+4 -245
View File
@@ -64,17 +64,17 @@
<tr>
<th>Action</th>
<th>Row</th>
@foreach (var header in Model.Context.ParsedHeaders)
@foreach (var header in Model.Context.Columns.Where(c => c.Type != DeviceImportFieldTypes.IgnoreColumn))
{
<th>@(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Item2).Item2)</th>
<th>@(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Type).Item2)</th>
}
</tr>
<tr>
<th>&nbsp;</th>
<th>&nbsp;</th>
@foreach (var header in Model.Context.ParsedHeaders)
@foreach (var header in Model.Context.Columns.Where(c => c.Type != DeviceImportFieldTypes.IgnoreColumn))
{
<th>@header.Item1</th>
<th>@header.Name</th>
}
</tr>
</thead>
@@ -164,244 +164,3 @@
});
</script>
@*
<div id="deviceImportReview">
@if (Model.ImportDevices.Count > 0)
{
<h2>Parsed @Model.ImportDevices.Count Device Record@(Model.ImportDevices.Count != 1 ? "s" : null)</h2>
<h4>
@importDeviceOkCount of @Model.ImportDevices.Count Device@(Model.ImportDevices.Count != 1 ? "s" : null) are ready for import.
</h4>
if (importDeviceErrorCount > 0)
{
<h4 id="errorMessage">
@(importDeviceErrorCount) Record@(importDeviceErrorCount != 1 ? "s" : null) will be skipped if the import continues
</h4>
}
<div id="devicesNavigation">
<ul class="none">
@if (importDeviceNewCount > 0)
{<li class="statusNew">
<input id="devicesNavigationNew" type="checkbox" checked="checked" /><label for="devicesNavigationNew">Show New Devices (@(Model.ImportDevices.Count(id => id.Errors.Count == 0 && id.Device == null)))</label>
</li>}@if (importDeviceUpdateCount > 0)
{<li class="statusUpdate">
<input id="devicesNavigationUpdate" type="checkbox" checked="checked" /><label for="devicesNavigationUpdate">Show Updates (@(Model.ImportDevices.Count(id => id.Errors.Count == 0 && id.Device != null)))</label>
</li>}@if (importDeviceErrorCount > 0)
{<li class="statusError">
<input id="devicesNavigationError" type="checkbox" checked="checked" /><label for="devicesNavigationError">Show Errors (@(Model.ImportDevices.Count(id => id.Errors.Count != 0)))</label>
</li>}
</ul>
<script>
$(function () {
$devices = $('#devices');
$rows = $devices.children('tbody').children('tr');
$('#devicesNavigationNew').change(function () {
if ($(this).is(':checked'))
$rows.filter('.statusNew').show();
else
$rows.filter('.statusNew').hide();
});
$('#devicesNavigationUpdate').change(function () {
if ($(this).is(':checked'))
$rows.filter('.statusUpdate').show();
else
$rows.filter('.statusUpdate').hide();
});
$('#devicesNavigationError').change(function () {
if ($(this).is(':checked'))
$rows.filter('.statusError').show();
else
$rows.filter('.statusError').hide();
});
});
</script>
</div>
<table id="devices">
<thead>
<tr>
<th class="row">Row</th>
<th class="action">Action</th>
<th class="serialNumber">Serial Number</th>
<th class="model">Model</th>
<th class="profile">Profile</th>
<th class="batch">Batch</th>
<th class="assignedUser">Assigned User</th>
<th class="location">Location</th>
<th class="assetNumber">Asset Number</th>
</tr>
</thead>
<tbody>
@foreach (var device in Model.ImportDevices)
{
bool isUpdate = device.Device != null;
string error;
<tr class="status@(device.ImportStatus())">
<td class="row">
@((Model.ImportDevices.IndexOf(device) + 1))
</td>
<td class="action">
@(device.ImportStatus())
</td>
<td class="serialNumber">
@if (device.Device == null)
{
@device.SerialNumber
}
else
{
@Html.ActionLink(device.SerialNumber, MVC.Device.Show(device.SerialNumber), new { target = "_blank" })
}
@if (device.Errors.TryGetValue("SerialNumber", out error))
{
<div class="error">@error</div>
}
</td>
<td class="model">
@if (device.Errors.TryGetValue("DeviceModelId", out error))
{
<div class="error">@error</div>
}
else
{
if (!isUpdate || device.DeviceModelId != device.Device.DeviceModelId)
{
<img class="modelImage" alt="Model Image" src="@Url.Action(MVC.API.DeviceModel.Image(device.DeviceModel.Id, device.DeviceModel.ImageHash()))" />
@device.DeviceModel.ToString()
}
else
{
<span class="smallMessage">No Change</span>
}
}</td>
<td class="profile">
@if (device.Errors.TryGetValue("DeviceProfileId", out error))
{
<div class="error">@error</div>
}
else
{
if (!isUpdate || device.DeviceProfileId != device.Device.DeviceProfileId)
{
@device.DeviceProfile.ToString()
}
else
{
<span class="smallMessage">No Change</span>
}
}</td>
<td class="batch">
@if (device.Errors.TryGetValue("DeviceBatchId", out error))
{
<div class="error">@error</div>
}
else
{
if (!isUpdate || device.DeviceBatchId != device.Device.DeviceBatchId)
{
if (device.DeviceBatch == null)
{
<text>&lt;None&gt;</text>
}
else
{
@device.DeviceBatch.ToString()
}
}
else
{
<span class="smallMessage">No Change</span>
}
}</td>
<td class="assignedUser">
@if (device.Errors.TryGetValue("AssignedUserId", out error))
{
<div class="error">@error</div>
}
else
{
if (!isUpdate || device.AssignedUserId != device.Device.AssignedUserId)
{
if (device.AssignedUser == null)
{
<text>&lt;None&gt;</text>
}
else
{
@device.AssignedUser.ToString()
}
}
else
{
<span class="smallMessage">No Change</span>
}
}</td>
<td class="location">
@if (device.Errors.TryGetValue("Location", out error))
{
<div class="error">@error</div>
}
else
{
if (!isUpdate || device.Location != device.Device.Location)
{
if (device.Location == null)
{
<text>&lt;None&gt;</text>
}
else
{
@device.Location
}
}
else
{
<span class="smallMessage">No Change</span>
}
}</td>
<td class="assetNumber">
@if (device.Errors.TryGetValue("AssetNumber", out error))
{
<div class="error">@error</div>
}
else
{
if (!isUpdate || device.AssetNumber != device.Device.AssetNumber)
{
if (device.AssetNumber == null)
{
<text>&lt;None&gt;</text>
}
else
{
@device.AssetNumber
}
}
else
{
<span class="smallMessage">No Change</span>
}
}</td>
</tr>
}
</tbody>
</table>
if (importDeviceOkCount > 0)
{
<div class="actionBar">
@Html.ActionLinkButton(string.Format("Import {0} Device{1}", importDeviceOkCount, importDeviceOkCount != 1 ? "s" : null), MVC.API.Device.ImportProcess(Model.ImportParseTaskId))
</div>
}
}
else
{
<h2>No Devices were found in this file</h2>
}
</div>*@
@@ -2,7 +2,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34014
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@@ -33,7 +33,6 @@ namespace Disco.Web.Views.Device
using System.Web.UI;
using System.Web.WebPages;
using Disco;
using Disco.BI.Extensions;
using Disco.Models.Repository;
#line 2 "..\..\Views\Device\ImportReview.cshtml"
@@ -377,7 +376,7 @@ WriteLiteral(">\r\n <thead>\r\n <tr>\r\n
#line hidden
#line 67 "..\..\Views\Device\ImportReview.cshtml"
foreach (var header in Model.Context.ParsedHeaders)
foreach (var header in Model.Context.Columns.Where(c => c.Type != DeviceImportFieldTypes.IgnoreColumn))
{
@@ -387,7 +386,7 @@ WriteLiteral(" <th>");
#line 69 "..\..\Views\Device\ImportReview.cshtml"
Write(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Item2).Item2);
Write(Model.HeaderTypes.FirstOrDefault(h => h.Item1 == header.Type).Item2);
#line default
@@ -412,7 +411,7 @@ WriteLiteral(" </tr>\r\n <tr>\r\n
#line hidden
#line 75 "..\..\Views\Device\ImportReview.cshtml"
foreach (var header in Model.Context.ParsedHeaders)
foreach (var header in Model.Context.Columns.Where(c => c.Type != DeviceImportFieldTypes.IgnoreColumn))
{
@@ -422,7 +421,7 @@ WriteLiteral(" <th>");
#line 77 "..\..\Views\Device\ImportReview.cshtml"
Write(header.Item1);
Write(header.Name);
#line default
@@ -455,15 +454,15 @@ WriteLiteral(" </tr>\r\n </thead>\r\n <tbod
#line hidden
WriteLiteral(" <tr");
WriteAttribute("class", Tuple.Create(" class=\"", 4299), Tuple.Create("\"", 4335)
, Tuple.Create(Tuple.Create("", 4307), Tuple.Create("action", 4307), true)
WriteAttribute("class", Tuple.Create(" class=\"", 4401), Tuple.Create("\"", 4437)
, Tuple.Create(Tuple.Create("", 4409), Tuple.Create("action", 4409), true)
#line 85 "..\..\Views\Device\ImportReview.cshtml"
, Tuple.Create(Tuple.Create("", 4313), Tuple.Create<System.Object, System.Int32>(record.RecordAction
, Tuple.Create(Tuple.Create("", 4415), Tuple.Create<System.Object, System.Int32>(record.RecordAction
#line default
#line hidden
, 4313), false)
, 4415), false)
);
WriteLiteral(">\r\n <td");
@@ -502,23 +501,23 @@ WriteLiteral("</td>\r\n");
#line hidden
WriteLiteral(" <td");
WriteAttribute("class", Tuple.Create(" class=\"", 4663), Tuple.Create("\"", 4772)
, Tuple.Create(Tuple.Create("", 4671), Tuple.Create("header", 4671), true)
WriteAttribute("class", Tuple.Create(" class=\"", 4765), Tuple.Create("\"", 4874)
, Tuple.Create(Tuple.Create("", 4773), Tuple.Create("header", 4773), true)
#line 91 "..\..\Views\Device\ImportReview.cshtml"
, Tuple.Create(Tuple.Create("", 4677), Tuple.Create<System.Object, System.Int32>(field.FieldType
, Tuple.Create(Tuple.Create("", 4779), Tuple.Create<System.Object, System.Int32>(field.FieldType
#line default
#line hidden
, 4677), false)
, Tuple.Create(Tuple.Create(" ", 4695), Tuple.Create("action", 4696), true)
, 4779), false)
, Tuple.Create(Tuple.Create(" ", 4797), Tuple.Create("action", 4798), true)
#line 91 "..\..\Views\Device\ImportReview.cshtml"
, Tuple.Create(Tuple.Create("", 4702), Tuple.Create<System.Object, System.Int32>(field.FieldAction.HasValue ? field.FieldAction.ToString() : "Error"
, Tuple.Create(Tuple.Create("", 4804), Tuple.Create<System.Object, System.Int32>(field.FieldAction.HasValue ? field.FieldAction.ToString() : "Error"
#line default
#line hidden
, 4702), false)
, 4804), false)
);
WriteLiteral(" data-previousvalue=\"");
@@ -671,14 +670,14 @@ WriteLiteral(">\r\n <a");
WriteLiteral(" id=\"Devices_Import_Review_ChangeHeaders\"");
WriteAttribute("href", Tuple.Create(" href=\"", 6466), Tuple.Create("\"", 6535)
WriteAttribute("href", Tuple.Create(" href=\"", 6568), Tuple.Create("\"", 6637)
#line 115 "..\..\Views\Device\ImportReview.cshtml"
, Tuple.Create(Tuple.Create("", 6473), Tuple.Create<System.Object, System.Int32>(Url.Action(MVC.Device.ImportHeaders(Model.Context.SessionId))
, Tuple.Create(Tuple.Create("", 6575), Tuple.Create<System.Object, System.Int32>(Url.Action(MVC.Device.ImportHeaders(Model.Context.SessionId))
#line default
#line hidden
, 6473), false)
, 6575), false)
);
WriteLiteral(" class=\"button\"");
@@ -730,14 +729,14 @@ WriteLiteral(" <a");
WriteLiteral(" id=\"Devices_Import_Review_Apply\"");
WriteAttribute("href", Tuple.Create(" href=\"", 6897), Tuple.Create("\"", 6968)
WriteAttribute("href", Tuple.Create(" href=\"", 6999), Tuple.Create("\"", 7070)
#line 122 "..\..\Views\Device\ImportReview.cshtml"
, Tuple.Create(Tuple.Create("", 6904), Tuple.Create<System.Object, System.Int32>(Url.Action(MVC.API.Device.ImportApply(Model.Context.SessionId))
, Tuple.Create(Tuple.Create("", 7006), Tuple.Create<System.Object, System.Int32>(Url.Action(MVC.API.Device.ImportApply(Model.Context.SessionId))
#line default
#line hidden
, 6904), false)
, 7006), false)
);
WriteLiteral(" class=\"button\"");
@@ -781,9 +780,7 @@ WriteLiteral(" </div>\r\n</div>\r\n<script>\r\n $(function () {\r\n
"em>&lt;None&gt;</em>\';\r\n }\r\n }\r\n }," +
"\r\n position: {\r\n my: \"left top\",\r\n at: " +
"\"left bottom\",\r\n collision: \"flipfit flip\"\r\n }\r\n " +
" });\r\n\r\n });\r\n</script>\r\n\r\n");
WriteLiteral("\r\n");
" });\r\n\r\n });\r\n</script>\r\n");
}
}