Fix: Delete Device Models
Also purge related Device Components and any Device Model Image
This commit is contained in:
@@ -1,101 +1,112 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Disco.Models.Repository;
|
using Disco.Models.Repository;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using Disco.Data.Repository;
|
using Disco.Data.Repository;
|
||||||
|
|
||||||
namespace Disco.BI.Extensions
|
namespace Disco.BI.Extensions
|
||||||
{
|
{
|
||||||
public static class DeviceModelExtensions
|
public static class DeviceModelExtensions
|
||||||
{
|
{
|
||||||
public static bool ImageImport(this DeviceModel deviceModel, Stream ImageStream)
|
public static bool ImageImport(this DeviceModel deviceModel, Stream ImageStream)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (Bitmap inputBitmap = new Bitmap(ImageStream))
|
using (Bitmap inputBitmap = new Bitmap(ImageStream))
|
||||||
{
|
{
|
||||||
using (Image outputBitmap = inputBitmap.ResizeImage(255, 255))
|
using (Image outputBitmap = inputBitmap.ResizeImage(255, 255))
|
||||||
{
|
{
|
||||||
using (MemoryStream ms = new MemoryStream())
|
using (MemoryStream ms = new MemoryStream())
|
||||||
{
|
{
|
||||||
outputBitmap.SavePng(ms);
|
outputBitmap.SavePng(ms);
|
||||||
ms.Position = 0;
|
ms.Position = 0;
|
||||||
|
|
||||||
var deviceModelImagePath = deviceModel.ImageFilePath();
|
var deviceModelImagePath = deviceModel.ImageFilePath();
|
||||||
|
|
||||||
|
|
||||||
using (var storeStream = new FileStream(deviceModelImagePath, FileMode.Create, FileAccess.Write, FileShare.None))
|
using (var storeStream = new FileStream(deviceModelImagePath, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||||
{
|
{
|
||||||
ms.CopyTo(storeStream);
|
ms.CopyTo(storeStream);
|
||||||
}
|
}
|
||||||
//deviceModel.Image = ms.ToArray();
|
//deviceModel.Image = ms.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FileStream Image(this DeviceModel deviceModel)
|
public static FileStream Image(this DeviceModel deviceModel)
|
||||||
{
|
{
|
||||||
var deviceModelImagePath = deviceModel.ImageFilePath();
|
var deviceModelImagePath = deviceModel.ImageFilePath();
|
||||||
|
|
||||||
if (File.Exists(deviceModelImagePath))
|
if (File.Exists(deviceModelImagePath))
|
||||||
return new FileStream(deviceModelImagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
return new FileStream(deviceModelImagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ImageFilePath(this DeviceModel deviceModel)
|
public static string ImageFilePath(this DeviceModel deviceModel)
|
||||||
{
|
{
|
||||||
var configCache = new Disco.Data.Configuration.ConfigurationContext(null);
|
var configCache = new Disco.Data.Configuration.ConfigurationContext(null);
|
||||||
|
|
||||||
var deviceModelImagesDataStore = DataStore.CreateLocation(configCache, "DeviceModelImages");
|
var deviceModelImagesDataStore = DataStore.CreateLocation(configCache, "DeviceModelImages");
|
||||||
|
|
||||||
return Path.Combine(deviceModelImagesDataStore, string.Format("{0}.png", deviceModel.Id));
|
return Path.Combine(deviceModelImagesDataStore, string.Format("{0}.png", deviceModel.Id));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ImageHash(this DeviceModel deviceModel)
|
public static string ImageHash(this DeviceModel deviceModel)
|
||||||
{
|
{
|
||||||
var deviceModelImagePath = deviceModel.ImageFilePath();
|
var deviceModelImagePath = deviceModel.ImageFilePath();
|
||||||
|
|
||||||
if (File.Exists(deviceModelImagePath))
|
if (File.Exists(deviceModelImagePath))
|
||||||
return File.GetLastWriteTimeUtc(deviceModelImagePath).ToBinary().ToString();
|
return File.GetLastWriteTimeUtc(deviceModelImagePath).ToBinary().ToString();
|
||||||
else
|
else
|
||||||
return "-1";
|
return "-1";
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Actions
|
#region Actions
|
||||||
// Added 2012-11-26 G# - Need ability to delete Device Models
|
// Added 2012-11-26 G# - Need ability to delete Device Models
|
||||||
public static bool CanDelete(this DeviceModel dm, DiscoDataContext dbContext)
|
public static bool CanDelete(this DeviceModel dm, DiscoDataContext dbContext)
|
||||||
{
|
{
|
||||||
// Can't Delete Default Model (Id: 1)
|
// Can't Delete Default Model (Id: 1)
|
||||||
if (dm.Id == 1)
|
if (dm.Id == 1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Can't Delete if Contains Devices
|
// Can't Delete if Contains Devices
|
||||||
if (dbContext.Devices.Count(d => d.DeviceModelId == dm.Id) > 0)
|
if (dbContext.Devices.Count(d => d.DeviceModelId == dm.Id) > 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public static void Delete(this DeviceModel dm, DiscoDataContext dbContext)
|
public static void Delete(this DeviceModel dm, DiscoDataContext dbContext)
|
||||||
{
|
{
|
||||||
if (!dm.CanDelete(dbContext))
|
if (!dm.CanDelete(dbContext))
|
||||||
throw new InvalidOperationException("The state of this Device Model doesn't allow it to be deleted");
|
throw new InvalidOperationException("The state of this Device Model doesn't allow it to be deleted");
|
||||||
|
|
||||||
// Delete Model
|
// Delete Image
|
||||||
dbContext.DeviceModels.Remove(dm);
|
var deviceModelImagePath = dm.ImageFilePath();
|
||||||
}
|
if (File.Exists(deviceModelImagePath))
|
||||||
// End Added 2012-11-26 G#
|
File.Delete(deviceModelImagePath);
|
||||||
#endregion
|
|
||||||
|
// Delete any Device Model Components
|
||||||
}
|
foreach (var deviceModelComponent in dbContext.DeviceComponents.Where(dc => dc.DeviceModelId == dm.Id).ToList())
|
||||||
}
|
{
|
||||||
|
dbContext.DeviceComponents.Remove(deviceModelComponent);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete Model
|
||||||
|
dbContext.DeviceModels.Remove(dm);
|
||||||
|
}
|
||||||
|
// End Added 2012-11-26 G#
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
|||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
[assembly: AssemblyVersion("1.2.0207.1727")]
|
[assembly: AssemblyVersion("1.2.0207.1853")]
|
||||||
[assembly: AssemblyFileVersion("1.2.0207.1727")]
|
[assembly: AssemblyFileVersion("1.2.0207.1853")]
|
||||||
|
|||||||
Reference in New Issue
Block a user