GIT: perform LF normalization
This commit is contained in:
@@ -1,55 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI.Extensions;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class BootstrapperController : dbAdminController
|
||||
{
|
||||
|
||||
public virtual ActionResult MacSshUsername(string MacSshUsername)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(MacSshUsername))
|
||||
{
|
||||
dbContext.DiscoConfiguration.Bootstrapper.MacSshUsername = MacSshUsername;
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("The Username cannot be null or empty");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
public virtual ActionResult MacSshPassword(string MacSshPassword)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(MacSshPassword))
|
||||
{
|
||||
dbContext.DiscoConfiguration.Bootstrapper.MacSshPassword = MacSshPassword;
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("The Password cannot be null or empty");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI.Extensions;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class BootstrapperController : dbAdminController
|
||||
{
|
||||
|
||||
public virtual ActionResult MacSshUsername(string MacSshUsername)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(MacSshUsername))
|
||||
{
|
||||
dbContext.DiscoConfiguration.Bootstrapper.MacSshUsername = MacSshUsername;
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("The Username cannot be null or empty");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
public virtual ActionResult MacSshPassword(string MacSshPassword)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(MacSshPassword))
|
||||
{
|
||||
dbContext.DiscoConfiguration.Bootstrapper.MacSshPassword = MacSshPassword;
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("The Password cannot be null or empty");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,469 +1,469 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Web.Extensions;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceBatchController : dbAdminController
|
||||
{
|
||||
|
||||
const string pName = "name";
|
||||
const string pPurchaseDate = "purchasedate";
|
||||
const string pSupplier = "supplier";
|
||||
const string pPurchaseDetails = "purchasedetails";
|
||||
const string pUnitCost = "unitcost";
|
||||
const string pUnitQuantity = "unitquantity";
|
||||
const string pDefaultDeviceModelId = "defaultdevicemodelid";
|
||||
const string pWarrantyValidUntil = "warrantyvaliduntil";
|
||||
const string pWarrantyDetails = "warrantydetails";
|
||||
const string pInsuredDate = "insureddate";
|
||||
const string pInsuranceSupplier = "insurancesupplier";
|
||||
const string pInsuredUntil = "insureduntil";
|
||||
const string pInsuranceDetails = "insurancedetails";
|
||||
const string pComments = "comments";
|
||||
|
||||
public virtual ActionResult Update(int id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var deviceBatch = dbContext.DeviceBatches.Find(id);
|
||||
if (deviceBatch != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pName:
|
||||
UpdateName(deviceBatch, value);
|
||||
break;
|
||||
case pPurchaseDate:
|
||||
UpdatePurchaseDate(deviceBatch, value);
|
||||
break;
|
||||
case pSupplier:
|
||||
UpdateSupplier(deviceBatch, value);
|
||||
break;
|
||||
case pPurchaseDetails:
|
||||
UpdatePurchaseDetails(deviceBatch, value);
|
||||
break;
|
||||
case pUnitCost:
|
||||
UpdateUnitCost(deviceBatch, value);
|
||||
break;
|
||||
case pUnitQuantity:
|
||||
UpdateUnitQuantity(deviceBatch, value);
|
||||
break;
|
||||
case pDefaultDeviceModelId:
|
||||
UpdateDefaultDeviceModelId(deviceBatch, value);
|
||||
break;
|
||||
case pWarrantyValidUntil:
|
||||
UpdateWarrantyValidUntil(deviceBatch, value);
|
||||
break;
|
||||
case pWarrantyDetails:
|
||||
UpdateWarrantyDetails(deviceBatch, value);
|
||||
break;
|
||||
case pInsuredDate:
|
||||
UpdateInsuredDate(deviceBatch, value);
|
||||
break;
|
||||
case pInsuranceSupplier:
|
||||
UpdateInsuranceSupplier(deviceBatch, value);
|
||||
break;
|
||||
case pInsuredUntil:
|
||||
UpdateInsuredUntil(deviceBatch, value);
|
||||
break;
|
||||
case pInsuranceDetails:
|
||||
UpdateInsuranceDetails(deviceBatch, value);
|
||||
break;
|
||||
case pComments:
|
||||
UpdateComments(deviceBatch, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Invalid Device Batch Id", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceBatch.Index(deviceBatch.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateName(int id, string BatchName = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pName, BatchName, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdatePurchaseDate(int id, string PurchaseDate = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pPurchaseDate, PurchaseDate, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateSupplier(int id, string Supplier = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pSupplier, Supplier, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdatePurchaseDetails(int id, string PurchaseDetails = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pPurchaseDetails, PurchaseDetails, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateUnitCost(int id, string UnitCost = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pUnitCost, UnitCost, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateUnitQuantity(int id, string UnitQuantity = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pUnitQuantity, UnitQuantity, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultDeviceModelId(int id, string DefaultDeviceModelId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDefaultDeviceModelId, DefaultDeviceModelId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateWarrantyValidUntil(int id, string WarrantyValidUntil = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pWarrantyValidUntil, WarrantyValidUntil, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdateWarrantyDetails(int id, string WarrantyDetails = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pWarrantyDetails, WarrantyDetails, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateInsuredDate(int id, string InsuredDate = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuredDate, InsuredDate, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateInsuranceSupplier(int id, string InsuranceSupplier = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuranceSupplier, InsuranceSupplier, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateInsuredUntil(int id, string InsuredUntil = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuredUntil, InsuredUntil, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdateInsuranceDetails(int id, string InsuranceDetails = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuranceDetails, InsuranceDetails, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdateComments(int id, string Comments = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pComments, Comments, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateName(DeviceBatch deviceBatch, string Name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
throw new ArgumentNullException("Name", "Device Batch Name is required");
|
||||
else
|
||||
{
|
||||
// Check for Duplicates
|
||||
var d = dbContext.DeviceBatches.Where(db => db.Id != deviceBatch.Id && db.Name == Name).Count();
|
||||
if (d > 0)
|
||||
{
|
||||
throw new Exception("A Device Batch with that name already exists");
|
||||
}
|
||||
deviceBatch.Name = Name;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdatePurchaseDate(DeviceBatch deviceBatch, string PurchaseDate)
|
||||
{
|
||||
if (string.IsNullOrEmpty(PurchaseDate))
|
||||
throw new ArgumentNullException("PurchaseDate", "A Device Batch Purchase Date is required");
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(PurchaseDate, out ecd))
|
||||
{
|
||||
deviceBatch.PurchaseDate = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateSupplier(DeviceBatch deviceBatch, string Supplier)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Supplier))
|
||||
deviceBatch.Supplier = null;
|
||||
else
|
||||
deviceBatch.Supplier = Supplier;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdatePurchaseDetails(DeviceBatch deviceBatch, string PurchaseDetails)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(PurchaseDetails))
|
||||
deviceBatch.PurchaseDetails = null;
|
||||
else
|
||||
deviceBatch.PurchaseDetails = PurchaseDetails;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateUnitCost(DeviceBatch deviceBatch, string UnitCost)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(UnitCost))
|
||||
deviceBatch.UnitCost = null;
|
||||
else
|
||||
{
|
||||
decimal unitCost;
|
||||
if (decimal.TryParse(UnitCost, out unitCost))
|
||||
{
|
||||
deviceBatch.UnitCost = unitCost;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Currency Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateUnitQuantity(DeviceBatch deviceBatch, string UnitQuantity)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(UnitQuantity))
|
||||
deviceBatch.UnitQuantity = null;
|
||||
else
|
||||
{
|
||||
int unitQuantity;
|
||||
if (int.TryParse(UnitQuantity, out unitQuantity))
|
||||
{
|
||||
deviceBatch.UnitQuantity = unitQuantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Number");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateDefaultDeviceModelId(DeviceBatch deviceBatch, string DefaultDeviceModelId)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(DefaultDeviceModelId))
|
||||
{
|
||||
int bId;
|
||||
if (int.TryParse(DefaultDeviceModelId, out bId))
|
||||
{
|
||||
var dm = dbContext.DeviceModels.Find(bId);
|
||||
if (dm != null)
|
||||
{
|
||||
deviceBatch.DefaultDeviceModelId = dm.Id;
|
||||
deviceBatch.DefaultDeviceModel = dm;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Null Id - No Batch
|
||||
deviceBatch.DefaultDeviceModelId = null;
|
||||
deviceBatch.DefaultDeviceModel = null;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Device Model Id");
|
||||
}
|
||||
private void UpdateWarrantyValidUntil(DeviceBatch deviceBatch, string WarrantyValidUntil)
|
||||
{
|
||||
if (string.IsNullOrEmpty(WarrantyValidUntil))
|
||||
deviceBatch.WarrantyValidUntil = null;
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(WarrantyValidUntil, out ecd))
|
||||
{
|
||||
deviceBatch.WarrantyValidUntil = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateWarrantyDetails(DeviceBatch deviceBatch, string WarrantyDetails)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(WarrantyDetails))
|
||||
deviceBatch.WarrantyDetails = null;
|
||||
else
|
||||
deviceBatch.WarrantyDetails = WarrantyDetails;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuredDate(DeviceBatch deviceBatch, string InsuredDate)
|
||||
{
|
||||
if (string.IsNullOrEmpty(InsuredDate))
|
||||
deviceBatch.InsuredDate = null;
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(InsuredDate, out ecd))
|
||||
{
|
||||
deviceBatch.InsuredDate = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuranceSupplier(DeviceBatch deviceBatch, string InsuranceSupplier)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(InsuranceSupplier))
|
||||
deviceBatch.InsuranceSupplier = null;
|
||||
else
|
||||
deviceBatch.InsuranceSupplier = InsuranceSupplier;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuredUntil(DeviceBatch deviceBatch, string InsuredUntil)
|
||||
{
|
||||
if (string.IsNullOrEmpty(InsuredUntil))
|
||||
deviceBatch.InsuredUntil = null;
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(InsuredUntil, out ecd))
|
||||
{
|
||||
deviceBatch.InsuredUntil = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuranceDetails(DeviceBatch deviceBatch, string InsuranceDetails)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(InsuranceDetails))
|
||||
deviceBatch.InsuranceDetails = null;
|
||||
else
|
||||
deviceBatch.InsuranceDetails = InsuranceDetails;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateComments(DeviceBatch deviceBatch, string Comments)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Comments))
|
||||
deviceBatch.Comments = null;
|
||||
else
|
||||
deviceBatch.Comments = Comments;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Actions
|
||||
|
||||
public virtual ActionResult Delete(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = dbContext.DeviceBatches.Find(id);
|
||||
if (db != null)
|
||||
{
|
||||
db.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceBatch.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Batch Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Index
|
||||
public virtual ActionResult Index(int? id)
|
||||
{
|
||||
if (id.HasValue)
|
||||
{
|
||||
dbContext.Configuration.ProxyCreationEnabled = false;
|
||||
DeviceBatch deviceBatch = dbContext.DeviceBatches.FirstOrDefault(db => db.Id == id);
|
||||
return Json(deviceBatch, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
var deviceBatches = dbContext.DeviceBatches.ToArray();
|
||||
return Json(deviceBatches, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Timeline
|
||||
public virtual ActionResult Timeline()
|
||||
{
|
||||
|
||||
var batchesInformation = dbContext.DeviceBatches.Select(db => new
|
||||
{
|
||||
Name = db.Name,
|
||||
Comments = db.Comments,
|
||||
PurchaseDate = db.PurchaseDate,
|
||||
WarrantyValidUntil = db.WarrantyValidUntil,
|
||||
DeviceCount = db.Devices.Count(),
|
||||
DefaultModelId = db.DefaultDeviceModelId,
|
||||
DefaultModelDescription = db.DefaultDeviceModel.Description
|
||||
}).ToArray();
|
||||
|
||||
Color warningColour = Color.FromArgb(225, 70, 0);
|
||||
Color normalColour = Color.FromArgb(88, 160, 220);
|
||||
Color highlightColour = Color.FromArgb(0, 140, 0);
|
||||
|
||||
double mostDevices = batchesInformation.Max(bi => bi.DeviceCount);
|
||||
var events = new List<Models.DeviceBatch.DeviceBatchTimelineEvent>();
|
||||
|
||||
foreach (var bi in batchesInformation)
|
||||
{
|
||||
var color = warningColour; // No Devices
|
||||
if (bi.DeviceCount > 0)
|
||||
color = normalColour.InterpolateColours(highlightColour, bi.DeviceCount / mostDevices);
|
||||
|
||||
events.Add(new Models.DeviceBatch.DeviceBatchTimelineEvent()
|
||||
{
|
||||
start = bi.PurchaseDate,
|
||||
end = bi.WarrantyValidUntil,
|
||||
caption = bi.DefaultModelDescription,
|
||||
title = string.Format("{0} [{1} x{2}]", bi.Name, bi.DefaultModelDescription, bi.DeviceCount),
|
||||
textColor = "#000",
|
||||
description = bi.Comments ?? string.Empty,
|
||||
color = ColorTranslator.ToHtml(color),
|
||||
image = Url.Action(MVC.API.DeviceModel.Image(bi.DefaultModelId)),
|
||||
link = Url.Action(MVC.Config.DeviceBatch.Index(bi.DefaultModelId))
|
||||
});
|
||||
}
|
||||
|
||||
return this.JsonNet(new Models.DeviceBatch.DeviceBatchTimelineEventSource() { events = events.ToArray() }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Web.Extensions;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceBatchController : dbAdminController
|
||||
{
|
||||
|
||||
const string pName = "name";
|
||||
const string pPurchaseDate = "purchasedate";
|
||||
const string pSupplier = "supplier";
|
||||
const string pPurchaseDetails = "purchasedetails";
|
||||
const string pUnitCost = "unitcost";
|
||||
const string pUnitQuantity = "unitquantity";
|
||||
const string pDefaultDeviceModelId = "defaultdevicemodelid";
|
||||
const string pWarrantyValidUntil = "warrantyvaliduntil";
|
||||
const string pWarrantyDetails = "warrantydetails";
|
||||
const string pInsuredDate = "insureddate";
|
||||
const string pInsuranceSupplier = "insurancesupplier";
|
||||
const string pInsuredUntil = "insureduntil";
|
||||
const string pInsuranceDetails = "insurancedetails";
|
||||
const string pComments = "comments";
|
||||
|
||||
public virtual ActionResult Update(int id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var deviceBatch = dbContext.DeviceBatches.Find(id);
|
||||
if (deviceBatch != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pName:
|
||||
UpdateName(deviceBatch, value);
|
||||
break;
|
||||
case pPurchaseDate:
|
||||
UpdatePurchaseDate(deviceBatch, value);
|
||||
break;
|
||||
case pSupplier:
|
||||
UpdateSupplier(deviceBatch, value);
|
||||
break;
|
||||
case pPurchaseDetails:
|
||||
UpdatePurchaseDetails(deviceBatch, value);
|
||||
break;
|
||||
case pUnitCost:
|
||||
UpdateUnitCost(deviceBatch, value);
|
||||
break;
|
||||
case pUnitQuantity:
|
||||
UpdateUnitQuantity(deviceBatch, value);
|
||||
break;
|
||||
case pDefaultDeviceModelId:
|
||||
UpdateDefaultDeviceModelId(deviceBatch, value);
|
||||
break;
|
||||
case pWarrantyValidUntil:
|
||||
UpdateWarrantyValidUntil(deviceBatch, value);
|
||||
break;
|
||||
case pWarrantyDetails:
|
||||
UpdateWarrantyDetails(deviceBatch, value);
|
||||
break;
|
||||
case pInsuredDate:
|
||||
UpdateInsuredDate(deviceBatch, value);
|
||||
break;
|
||||
case pInsuranceSupplier:
|
||||
UpdateInsuranceSupplier(deviceBatch, value);
|
||||
break;
|
||||
case pInsuredUntil:
|
||||
UpdateInsuredUntil(deviceBatch, value);
|
||||
break;
|
||||
case pInsuranceDetails:
|
||||
UpdateInsuranceDetails(deviceBatch, value);
|
||||
break;
|
||||
case pComments:
|
||||
UpdateComments(deviceBatch, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Invalid Device Batch Id", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceBatch.Index(deviceBatch.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateName(int id, string BatchName = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pName, BatchName, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdatePurchaseDate(int id, string PurchaseDate = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pPurchaseDate, PurchaseDate, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateSupplier(int id, string Supplier = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pSupplier, Supplier, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdatePurchaseDetails(int id, string PurchaseDetails = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pPurchaseDetails, PurchaseDetails, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateUnitCost(int id, string UnitCost = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pUnitCost, UnitCost, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateUnitQuantity(int id, string UnitQuantity = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pUnitQuantity, UnitQuantity, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultDeviceModelId(int id, string DefaultDeviceModelId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDefaultDeviceModelId, DefaultDeviceModelId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateWarrantyValidUntil(int id, string WarrantyValidUntil = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pWarrantyValidUntil, WarrantyValidUntil, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdateWarrantyDetails(int id, string WarrantyDetails = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pWarrantyDetails, WarrantyDetails, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateInsuredDate(int id, string InsuredDate = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuredDate, InsuredDate, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateInsuranceSupplier(int id, string InsuranceSupplier = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuranceSupplier, InsuranceSupplier, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateInsuredUntil(int id, string InsuredUntil = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuredUntil, InsuredUntil, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdateInsuranceDetails(int id, string InsuranceDetails = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pInsuranceDetails, InsuranceDetails, redirect);
|
||||
}
|
||||
[ValidateInput(false)]
|
||||
public virtual ActionResult UpdateComments(int id, string Comments = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pComments, Comments, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateName(DeviceBatch deviceBatch, string Name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
throw new ArgumentNullException("Name", "Device Batch Name is required");
|
||||
else
|
||||
{
|
||||
// Check for Duplicates
|
||||
var d = dbContext.DeviceBatches.Where(db => db.Id != deviceBatch.Id && db.Name == Name).Count();
|
||||
if (d > 0)
|
||||
{
|
||||
throw new Exception("A Device Batch with that name already exists");
|
||||
}
|
||||
deviceBatch.Name = Name;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdatePurchaseDate(DeviceBatch deviceBatch, string PurchaseDate)
|
||||
{
|
||||
if (string.IsNullOrEmpty(PurchaseDate))
|
||||
throw new ArgumentNullException("PurchaseDate", "A Device Batch Purchase Date is required");
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(PurchaseDate, out ecd))
|
||||
{
|
||||
deviceBatch.PurchaseDate = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateSupplier(DeviceBatch deviceBatch, string Supplier)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Supplier))
|
||||
deviceBatch.Supplier = null;
|
||||
else
|
||||
deviceBatch.Supplier = Supplier;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdatePurchaseDetails(DeviceBatch deviceBatch, string PurchaseDetails)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(PurchaseDetails))
|
||||
deviceBatch.PurchaseDetails = null;
|
||||
else
|
||||
deviceBatch.PurchaseDetails = PurchaseDetails;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateUnitCost(DeviceBatch deviceBatch, string UnitCost)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(UnitCost))
|
||||
deviceBatch.UnitCost = null;
|
||||
else
|
||||
{
|
||||
decimal unitCost;
|
||||
if (decimal.TryParse(UnitCost, out unitCost))
|
||||
{
|
||||
deviceBatch.UnitCost = unitCost;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Currency Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateUnitQuantity(DeviceBatch deviceBatch, string UnitQuantity)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(UnitQuantity))
|
||||
deviceBatch.UnitQuantity = null;
|
||||
else
|
||||
{
|
||||
int unitQuantity;
|
||||
if (int.TryParse(UnitQuantity, out unitQuantity))
|
||||
{
|
||||
deviceBatch.UnitQuantity = unitQuantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Number");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateDefaultDeviceModelId(DeviceBatch deviceBatch, string DefaultDeviceModelId)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(DefaultDeviceModelId))
|
||||
{
|
||||
int bId;
|
||||
if (int.TryParse(DefaultDeviceModelId, out bId))
|
||||
{
|
||||
var dm = dbContext.DeviceModels.Find(bId);
|
||||
if (dm != null)
|
||||
{
|
||||
deviceBatch.DefaultDeviceModelId = dm.Id;
|
||||
deviceBatch.DefaultDeviceModel = dm;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Null Id - No Batch
|
||||
deviceBatch.DefaultDeviceModelId = null;
|
||||
deviceBatch.DefaultDeviceModel = null;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Device Model Id");
|
||||
}
|
||||
private void UpdateWarrantyValidUntil(DeviceBatch deviceBatch, string WarrantyValidUntil)
|
||||
{
|
||||
if (string.IsNullOrEmpty(WarrantyValidUntil))
|
||||
deviceBatch.WarrantyValidUntil = null;
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(WarrantyValidUntil, out ecd))
|
||||
{
|
||||
deviceBatch.WarrantyValidUntil = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateWarrantyDetails(DeviceBatch deviceBatch, string WarrantyDetails)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(WarrantyDetails))
|
||||
deviceBatch.WarrantyDetails = null;
|
||||
else
|
||||
deviceBatch.WarrantyDetails = WarrantyDetails;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuredDate(DeviceBatch deviceBatch, string InsuredDate)
|
||||
{
|
||||
if (string.IsNullOrEmpty(InsuredDate))
|
||||
deviceBatch.InsuredDate = null;
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(InsuredDate, out ecd))
|
||||
{
|
||||
deviceBatch.InsuredDate = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuranceSupplier(DeviceBatch deviceBatch, string InsuranceSupplier)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(InsuranceSupplier))
|
||||
deviceBatch.InsuranceSupplier = null;
|
||||
else
|
||||
deviceBatch.InsuranceSupplier = InsuranceSupplier;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuredUntil(DeviceBatch deviceBatch, string InsuredUntil)
|
||||
{
|
||||
if (string.IsNullOrEmpty(InsuredUntil))
|
||||
deviceBatch.InsuredUntil = null;
|
||||
else
|
||||
{
|
||||
DateTime ecd;
|
||||
if (DateTime.TryParse(InsuredUntil, out ecd))
|
||||
{
|
||||
deviceBatch.InsuredUntil = ecd.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateInsuranceDetails(DeviceBatch deviceBatch, string InsuranceDetails)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(InsuranceDetails))
|
||||
deviceBatch.InsuranceDetails = null;
|
||||
else
|
||||
deviceBatch.InsuranceDetails = InsuranceDetails;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateComments(DeviceBatch deviceBatch, string Comments)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Comments))
|
||||
deviceBatch.Comments = null;
|
||||
else
|
||||
deviceBatch.Comments = Comments;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Actions
|
||||
|
||||
public virtual ActionResult Delete(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = dbContext.DeviceBatches.Find(id);
|
||||
if (db != null)
|
||||
{
|
||||
db.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceBatch.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Batch Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Index
|
||||
public virtual ActionResult Index(int? id)
|
||||
{
|
||||
if (id.HasValue)
|
||||
{
|
||||
dbContext.Configuration.ProxyCreationEnabled = false;
|
||||
DeviceBatch deviceBatch = dbContext.DeviceBatches.FirstOrDefault(db => db.Id == id);
|
||||
return Json(deviceBatch, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
var deviceBatches = dbContext.DeviceBatches.ToArray();
|
||||
return Json(deviceBatches, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Timeline
|
||||
public virtual ActionResult Timeline()
|
||||
{
|
||||
|
||||
var batchesInformation = dbContext.DeviceBatches.Select(db => new
|
||||
{
|
||||
Name = db.Name,
|
||||
Comments = db.Comments,
|
||||
PurchaseDate = db.PurchaseDate,
|
||||
WarrantyValidUntil = db.WarrantyValidUntil,
|
||||
DeviceCount = db.Devices.Count(),
|
||||
DefaultModelId = db.DefaultDeviceModelId,
|
||||
DefaultModelDescription = db.DefaultDeviceModel.Description
|
||||
}).ToArray();
|
||||
|
||||
Color warningColour = Color.FromArgb(225, 70, 0);
|
||||
Color normalColour = Color.FromArgb(88, 160, 220);
|
||||
Color highlightColour = Color.FromArgb(0, 140, 0);
|
||||
|
||||
double mostDevices = batchesInformation.Max(bi => bi.DeviceCount);
|
||||
var events = new List<Models.DeviceBatch.DeviceBatchTimelineEvent>();
|
||||
|
||||
foreach (var bi in batchesInformation)
|
||||
{
|
||||
var color = warningColour; // No Devices
|
||||
if (bi.DeviceCount > 0)
|
||||
color = normalColour.InterpolateColours(highlightColour, bi.DeviceCount / mostDevices);
|
||||
|
||||
events.Add(new Models.DeviceBatch.DeviceBatchTimelineEvent()
|
||||
{
|
||||
start = bi.PurchaseDate,
|
||||
end = bi.WarrantyValidUntil,
|
||||
caption = bi.DefaultModelDescription,
|
||||
title = string.Format("{0} [{1} x{2}]", bi.Name, bi.DefaultModelDescription, bi.DeviceCount),
|
||||
textColor = "#000",
|
||||
description = bi.Comments ?? string.Empty,
|
||||
color = ColorTranslator.ToHtml(color),
|
||||
image = Url.Action(MVC.API.DeviceModel.Image(bi.DefaultModelId)),
|
||||
link = Url.Action(MVC.Config.DeviceBatch.Index(bi.DefaultModelId))
|
||||
});
|
||||
}
|
||||
|
||||
return this.JsonNet(new Models.DeviceBatch.DeviceBatchTimelineEventSource() { events = events.ToArray() }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,468 +1,468 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Interop.ActiveDirectory;
|
||||
using System.IO;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDeviceProfileId = "deviceprofileid";
|
||||
const string pDeviceBatchId = "devicebatchid";
|
||||
const string pAssetNumber = "assetnumber";
|
||||
const string pAssignedUserId = "assigneduserid";
|
||||
const string pLocation = "location";
|
||||
const string pAllowUnauthenticatedEnrol = "allowunauthenticatedenrol";
|
||||
|
||||
public virtual ActionResult Update(string id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var device = dbContext.Devices.Find(id);
|
||||
if (device != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDeviceProfileId:
|
||||
UpdateDeviceProfileId(device, value);
|
||||
break;
|
||||
case pDeviceBatchId:
|
||||
UpdateDeviceBatchId(device, value);
|
||||
break;
|
||||
case pAssetNumber:
|
||||
UpdateAssetNumber(device, value);
|
||||
break;
|
||||
case pAssignedUserId:
|
||||
UpdateAssignedUserId(device, value);
|
||||
break;
|
||||
case pLocation:
|
||||
UpdateLocation(device, value);
|
||||
break;
|
||||
case pAllowUnauthenticatedEnrol:
|
||||
UpdateAllowUnauthenticatedEnrol(device, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Serial Number or Device Profile Id");
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Show(device.SerialNumber));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDeviceProfileId(string id, string DeviceProfileId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDeviceProfileId, DeviceProfileId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDeviceBatchId(string id, string DeviceBatchId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDeviceBatchId, DeviceBatchId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateAssetNumber(string id, string AssetNumber = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pAssetNumber, AssetNumber, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateLocation(string id, string Location = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pLocation, Location, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateAssignedUserId(string id, string AssignedUserId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pAssignedUserId, AssignedUserId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateAllowUnauthenticatedEnrol(string id, string AllowUnauthenticatedEnrol = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pAllowUnauthenticatedEnrol, AllowUnauthenticatedEnrol, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDeviceProfileId(Disco.Models.Repository.Device device, string DeviceProfileId)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(DeviceProfileId))
|
||||
{
|
||||
int pId;
|
||||
if (int.TryParse(DeviceProfileId, out pId))
|
||||
{
|
||||
var p = dbContext.DeviceProfiles.Find(pId);
|
||||
if (p != null)
|
||||
{
|
||||
device.DeviceProfileId = p.Id;
|
||||
device.DeviceProfile = p;
|
||||
|
||||
// Update AD Account
|
||||
if (!string.IsNullOrEmpty(device.ComputerName) && device.ComputerName.Length <= 24)
|
||||
{
|
||||
var adMachineAccount = ActiveDirectory.GetMachineAccount(device.ComputerName);
|
||||
if (adMachineAccount != null)
|
||||
adMachineAccount.SetDescription(device);
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Exception("Invalid Device Profile Id");
|
||||
}
|
||||
private void UpdateDeviceBatchId(Disco.Models.Repository.Device device, string DeviceBatchId)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(DeviceBatchId))
|
||||
{
|
||||
int bId;
|
||||
if (int.TryParse(DeviceBatchId, out bId))
|
||||
{
|
||||
var b = dbContext.DeviceBatches.Find(bId);
|
||||
if (b != null)
|
||||
{
|
||||
device.DeviceBatchId = b.Id;
|
||||
device.DeviceBatch = b;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Null Id - No Batch
|
||||
device.DeviceBatchId = null;
|
||||
device.DeviceBatch = null;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Device Batch Id");
|
||||
}
|
||||
private void UpdateAssetNumber(Disco.Models.Repository.Device device, string AssetNumber)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(AssetNumber))
|
||||
device.AssetNumber = null;
|
||||
else
|
||||
device.AssetNumber = AssetNumber.Trim();
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateLocation(Disco.Models.Repository.Device device, string Location)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Location))
|
||||
device.Location = null;
|
||||
else
|
||||
device.Location = Location.Trim();
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateAssignedUserId(Disco.Models.Repository.Device device, string UserId)
|
||||
{
|
||||
var daus = dbContext.DeviceUserAssignments.Where(m => m.DeviceSerialNumber == device.SerialNumber && m.UnassignedDate == null);
|
||||
Disco.Models.Repository.User u = null;
|
||||
if (!string.IsNullOrEmpty(UserId))
|
||||
{
|
||||
// Changed 2012-12-13 G# - Stop error when assigning user - Force Refresh
|
||||
// http://www.discoict.com.au/forum/support/2012/11/error-when-assigning-multiple-devices-to-single-user.aspx
|
||||
//u = BI.UserBI.UserCache.GetUser(UserId, dbContext);
|
||||
u = BI.UserBI.UserCache.GetUser(UserId, dbContext, true);
|
||||
// End Changed 2012-12-13 G#
|
||||
if (u == null)
|
||||
{
|
||||
throw new Exception("Invalid Username");
|
||||
}
|
||||
}
|
||||
|
||||
device.AssignDevice(dbContext, u);
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateAllowUnauthenticatedEnrol(Disco.Models.Repository.Device device, string AllowUnauthenticatedEnrol)
|
||||
{
|
||||
bool bAllowUnauthenticatedEnrol;
|
||||
if (string.IsNullOrEmpty(AllowUnauthenticatedEnrol) || !bool.TryParse(AllowUnauthenticatedEnrol, out bAllowUnauthenticatedEnrol))
|
||||
{
|
||||
throw new Exception("Invalid AllowUnauthenticatedEnrol Value");
|
||||
}
|
||||
|
||||
if (device.AllowUnauthenticatedEnrol != bAllowUnauthenticatedEnrol)
|
||||
{
|
||||
device.AllowUnauthenticatedEnrol = bAllowUnauthenticatedEnrol;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Device Actions
|
||||
public virtual ActionResult Decommission(string id, bool redirect)
|
||||
{
|
||||
var d = dbContext.Devices.Find(id);
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
if (d != null)
|
||||
{
|
||||
if (d.CanDecommission())
|
||||
{
|
||||
d.OnDecommission();
|
||||
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Show(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Device's state doesn't allow this action", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
return Json("Invalid Device Serial Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Recommission(string id, bool redirect)
|
||||
{
|
||||
var d = dbContext.Devices.Find(id);
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
if (d != null)
|
||||
{
|
||||
if (d.CanRecommission())
|
||||
{
|
||||
d.OnRecommission();
|
||||
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Show(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Device's state doesn't allow this action", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
return Json("Invalid Device Serial Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Delete(string id, bool redirect)
|
||||
{
|
||||
var j = dbContext.Devices.Find(id);
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
if (j != null)
|
||||
{
|
||||
if (j.CanDelete())
|
||||
{
|
||||
j.OnDelete(dbContext);
|
||||
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Index());
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Job's state doesn't allow this action", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
return Json("Invalid Device Serial Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual ActionResult GeneratePdf(string id, string DocumentTemplateId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(DocumentTemplateId))
|
||||
throw new ArgumentNullException("AttachmentTypeId");
|
||||
var device = dbContext.Devices.Find(id);
|
||||
if (device != null)
|
||||
{
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(DocumentTemplateId);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
var timeStamp = DateTime.Now;
|
||||
Stream pdf;
|
||||
using (var generationState = Disco.Models.BI.DocumentTemplates.DocumentState.DefaultState()){
|
||||
pdf = documentTemplate.GeneratePdf(dbContext, device, DiscoApplication.CurrentUser, timeStamp, generationState);
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, device.SerialNumber, timeStamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Serial Number", "id");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual ActionResult LastNetworkLogonDate(string id)
|
||||
{
|
||||
var device = dbContext.Devices.Find(id);
|
||||
if (device == null)
|
||||
{
|
||||
return HttpNotFound("Invalid Device Serial Number");
|
||||
}
|
||||
|
||||
if (device.UpdateLastNetworkLogonDate())
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var result = new
|
||||
{
|
||||
Timestamp = device.LastNetworkLogonDate,
|
||||
Friendly = device.LastNetworkLogonDate.ToFuzzy("Unknown"),
|
||||
Formatted = device.LastNetworkLogonDate.ToFullDateTime("Unknown")
|
||||
};
|
||||
|
||||
return Json(result, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#region Device Attachements
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentDownload(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Find(id);
|
||||
if (da != null)
|
||||
{
|
||||
var filePath = da.RepositoryFilename(dbContext);
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
return File(filePath, da.MimeType, da.Filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
return HttpNotFound("Attachment reference exists, but file not found");
|
||||
}
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentThumbnail(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Find(id);
|
||||
if (da != null)
|
||||
{
|
||||
var thumbPath = da.RepositoryThumbnailFilename(dbContext);
|
||||
if (System.IO.File.Exists(thumbPath))
|
||||
{
|
||||
if (thumbPath.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
|
||||
return File(thumbPath, "image/png");
|
||||
else
|
||||
return File(thumbPath, "image/jpg");
|
||||
}
|
||||
else
|
||||
return File(ClientSource.Style.Images.AttachmentTypes.MimeTypeIcons.Icon(da.MimeType), "image/png");
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
public virtual ActionResult AttachmentUpload(string id, string Comments)
|
||||
{
|
||||
var d = dbContext.Devices.Find(id);
|
||||
if (d != null)
|
||||
{
|
||||
if (Request.Files.Count > 0)
|
||||
{
|
||||
var file = Request.Files.Get(0);
|
||||
if (file.ContentLength > 0)
|
||||
{
|
||||
var contentType = file.ContentType;
|
||||
if (string.IsNullOrEmpty(contentType) || contentType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
contentType = BI.Interop.MimeTypes.ResolveMimeType(file.FileName);
|
||||
|
||||
var da = new Disco.Models.Repository.DeviceAttachment()
|
||||
{
|
||||
DeviceSerialNumber = d.SerialNumber,
|
||||
TechUserId = DiscoApplication.CurrentUser.Id,
|
||||
Filename = file.FileName,
|
||||
MimeType = contentType,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = Comments
|
||||
};
|
||||
dbContext.DeviceAttachments.Add(da);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
da.SaveAttachment(dbContext, file.InputStream);
|
||||
|
||||
da.GenerateThumbnail(dbContext);
|
||||
|
||||
return Json(da.Id, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
throw new Exception("No Attachment Uploaded");
|
||||
}
|
||||
throw new Exception("Invalid Device Serial Number");
|
||||
}
|
||||
public virtual ActionResult Attachment(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (da != null)
|
||||
{
|
||||
|
||||
var m = new Models.Attachment.AttachmentModel()
|
||||
{
|
||||
Attachment = Models.Attachment._AttachmentModel.FromAttachment(da),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentModel() { Result = "Invalid Attachment Number" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Attachments(string id)
|
||||
{
|
||||
var d = dbContext.Devices.Include("DeviceAttachments.TechUser").Where(m => m.SerialNumber == id).FirstOrDefault();
|
||||
if (d != null)
|
||||
{
|
||||
var m = new Models.Attachment.AttachmentsModel()
|
||||
{
|
||||
Attachments = d.DeviceAttachments.Select(ua => Models.Attachment._AttachmentModel.FromAttachment(ua)).ToList(),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentsModel() { Result = "Invalid Device Serial Number" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult AttachmentRemove(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (da != null)
|
||||
{
|
||||
// 2012-02-17 G# Remove - 'Delete Own Comments' policy
|
||||
//if (da.TechUserId == DiscoApplication.CurrentUser.Id)
|
||||
//{
|
||||
da.OnDelete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// return Json("You can only delete your own attachments.", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
}
|
||||
return Json("Invalid Attachment Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Interop.ActiveDirectory;
|
||||
using System.IO;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDeviceProfileId = "deviceprofileid";
|
||||
const string pDeviceBatchId = "devicebatchid";
|
||||
const string pAssetNumber = "assetnumber";
|
||||
const string pAssignedUserId = "assigneduserid";
|
||||
const string pLocation = "location";
|
||||
const string pAllowUnauthenticatedEnrol = "allowunauthenticatedenrol";
|
||||
|
||||
public virtual ActionResult Update(string id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var device = dbContext.Devices.Find(id);
|
||||
if (device != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDeviceProfileId:
|
||||
UpdateDeviceProfileId(device, value);
|
||||
break;
|
||||
case pDeviceBatchId:
|
||||
UpdateDeviceBatchId(device, value);
|
||||
break;
|
||||
case pAssetNumber:
|
||||
UpdateAssetNumber(device, value);
|
||||
break;
|
||||
case pAssignedUserId:
|
||||
UpdateAssignedUserId(device, value);
|
||||
break;
|
||||
case pLocation:
|
||||
UpdateLocation(device, value);
|
||||
break;
|
||||
case pAllowUnauthenticatedEnrol:
|
||||
UpdateAllowUnauthenticatedEnrol(device, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Serial Number or Device Profile Id");
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Show(device.SerialNumber));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDeviceProfileId(string id, string DeviceProfileId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDeviceProfileId, DeviceProfileId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDeviceBatchId(string id, string DeviceBatchId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDeviceBatchId, DeviceBatchId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateAssetNumber(string id, string AssetNumber = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pAssetNumber, AssetNumber, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateLocation(string id, string Location = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pLocation, Location, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateAssignedUserId(string id, string AssignedUserId = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pAssignedUserId, AssignedUserId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateAllowUnauthenticatedEnrol(string id, string AllowUnauthenticatedEnrol = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pAllowUnauthenticatedEnrol, AllowUnauthenticatedEnrol, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDeviceProfileId(Disco.Models.Repository.Device device, string DeviceProfileId)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(DeviceProfileId))
|
||||
{
|
||||
int pId;
|
||||
if (int.TryParse(DeviceProfileId, out pId))
|
||||
{
|
||||
var p = dbContext.DeviceProfiles.Find(pId);
|
||||
if (p != null)
|
||||
{
|
||||
device.DeviceProfileId = p.Id;
|
||||
device.DeviceProfile = p;
|
||||
|
||||
// Update AD Account
|
||||
if (!string.IsNullOrEmpty(device.ComputerName) && device.ComputerName.Length <= 24)
|
||||
{
|
||||
var adMachineAccount = ActiveDirectory.GetMachineAccount(device.ComputerName);
|
||||
if (adMachineAccount != null)
|
||||
adMachineAccount.SetDescription(device);
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Exception("Invalid Device Profile Id");
|
||||
}
|
||||
private void UpdateDeviceBatchId(Disco.Models.Repository.Device device, string DeviceBatchId)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(DeviceBatchId))
|
||||
{
|
||||
int bId;
|
||||
if (int.TryParse(DeviceBatchId, out bId))
|
||||
{
|
||||
var b = dbContext.DeviceBatches.Find(bId);
|
||||
if (b != null)
|
||||
{
|
||||
device.DeviceBatchId = b.Id;
|
||||
device.DeviceBatch = b;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Null Id - No Batch
|
||||
device.DeviceBatchId = null;
|
||||
device.DeviceBatch = null;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Device Batch Id");
|
||||
}
|
||||
private void UpdateAssetNumber(Disco.Models.Repository.Device device, string AssetNumber)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(AssetNumber))
|
||||
device.AssetNumber = null;
|
||||
else
|
||||
device.AssetNumber = AssetNumber.Trim();
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateLocation(Disco.Models.Repository.Device device, string Location)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Location))
|
||||
device.Location = null;
|
||||
else
|
||||
device.Location = Location.Trim();
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateAssignedUserId(Disco.Models.Repository.Device device, string UserId)
|
||||
{
|
||||
var daus = dbContext.DeviceUserAssignments.Where(m => m.DeviceSerialNumber == device.SerialNumber && m.UnassignedDate == null);
|
||||
Disco.Models.Repository.User u = null;
|
||||
if (!string.IsNullOrEmpty(UserId))
|
||||
{
|
||||
// Changed 2012-12-13 G# - Stop error when assigning user - Force Refresh
|
||||
// http://www.discoict.com.au/forum/support/2012/11/error-when-assigning-multiple-devices-to-single-user.aspx
|
||||
//u = BI.UserBI.UserCache.GetUser(UserId, dbContext);
|
||||
u = BI.UserBI.UserCache.GetUser(UserId, dbContext, true);
|
||||
// End Changed 2012-12-13 G#
|
||||
if (u == null)
|
||||
{
|
||||
throw new Exception("Invalid Username");
|
||||
}
|
||||
}
|
||||
|
||||
device.AssignDevice(dbContext, u);
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateAllowUnauthenticatedEnrol(Disco.Models.Repository.Device device, string AllowUnauthenticatedEnrol)
|
||||
{
|
||||
bool bAllowUnauthenticatedEnrol;
|
||||
if (string.IsNullOrEmpty(AllowUnauthenticatedEnrol) || !bool.TryParse(AllowUnauthenticatedEnrol, out bAllowUnauthenticatedEnrol))
|
||||
{
|
||||
throw new Exception("Invalid AllowUnauthenticatedEnrol Value");
|
||||
}
|
||||
|
||||
if (device.AllowUnauthenticatedEnrol != bAllowUnauthenticatedEnrol)
|
||||
{
|
||||
device.AllowUnauthenticatedEnrol = bAllowUnauthenticatedEnrol;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Device Actions
|
||||
public virtual ActionResult Decommission(string id, bool redirect)
|
||||
{
|
||||
var d = dbContext.Devices.Find(id);
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
if (d != null)
|
||||
{
|
||||
if (d.CanDecommission())
|
||||
{
|
||||
d.OnDecommission();
|
||||
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Show(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Device's state doesn't allow this action", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
return Json("Invalid Device Serial Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Recommission(string id, bool redirect)
|
||||
{
|
||||
var d = dbContext.Devices.Find(id);
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
if (d != null)
|
||||
{
|
||||
if (d.CanRecommission())
|
||||
{
|
||||
d.OnRecommission();
|
||||
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Show(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Device's state doesn't allow this action", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
return Json("Invalid Device Serial Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Delete(string id, bool redirect)
|
||||
{
|
||||
var j = dbContext.Devices.Find(id);
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
if (j != null)
|
||||
{
|
||||
if (j.CanDelete())
|
||||
{
|
||||
j.OnDelete(dbContext);
|
||||
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Device.Index());
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Job's state doesn't allow this action", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
return Json("Invalid Device Serial Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual ActionResult GeneratePdf(string id, string DocumentTemplateId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(DocumentTemplateId))
|
||||
throw new ArgumentNullException("AttachmentTypeId");
|
||||
var device = dbContext.Devices.Find(id);
|
||||
if (device != null)
|
||||
{
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(DocumentTemplateId);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
var timeStamp = DateTime.Now;
|
||||
Stream pdf;
|
||||
using (var generationState = Disco.Models.BI.DocumentTemplates.DocumentState.DefaultState()){
|
||||
pdf = documentTemplate.GeneratePdf(dbContext, device, DiscoApplication.CurrentUser, timeStamp, generationState);
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, device.SerialNumber, timeStamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Serial Number", "id");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual ActionResult LastNetworkLogonDate(string id)
|
||||
{
|
||||
var device = dbContext.Devices.Find(id);
|
||||
if (device == null)
|
||||
{
|
||||
return HttpNotFound("Invalid Device Serial Number");
|
||||
}
|
||||
|
||||
if (device.UpdateLastNetworkLogonDate())
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var result = new
|
||||
{
|
||||
Timestamp = device.LastNetworkLogonDate,
|
||||
Friendly = device.LastNetworkLogonDate.ToFuzzy("Unknown"),
|
||||
Formatted = device.LastNetworkLogonDate.ToFullDateTime("Unknown")
|
||||
};
|
||||
|
||||
return Json(result, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#region Device Attachements
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentDownload(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Find(id);
|
||||
if (da != null)
|
||||
{
|
||||
var filePath = da.RepositoryFilename(dbContext);
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
return File(filePath, da.MimeType, da.Filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
return HttpNotFound("Attachment reference exists, but file not found");
|
||||
}
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentThumbnail(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Find(id);
|
||||
if (da != null)
|
||||
{
|
||||
var thumbPath = da.RepositoryThumbnailFilename(dbContext);
|
||||
if (System.IO.File.Exists(thumbPath))
|
||||
{
|
||||
if (thumbPath.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
|
||||
return File(thumbPath, "image/png");
|
||||
else
|
||||
return File(thumbPath, "image/jpg");
|
||||
}
|
||||
else
|
||||
return File(ClientSource.Style.Images.AttachmentTypes.MimeTypeIcons.Icon(da.MimeType), "image/png");
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
public virtual ActionResult AttachmentUpload(string id, string Comments)
|
||||
{
|
||||
var d = dbContext.Devices.Find(id);
|
||||
if (d != null)
|
||||
{
|
||||
if (Request.Files.Count > 0)
|
||||
{
|
||||
var file = Request.Files.Get(0);
|
||||
if (file.ContentLength > 0)
|
||||
{
|
||||
var contentType = file.ContentType;
|
||||
if (string.IsNullOrEmpty(contentType) || contentType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
contentType = BI.Interop.MimeTypes.ResolveMimeType(file.FileName);
|
||||
|
||||
var da = new Disco.Models.Repository.DeviceAttachment()
|
||||
{
|
||||
DeviceSerialNumber = d.SerialNumber,
|
||||
TechUserId = DiscoApplication.CurrentUser.Id,
|
||||
Filename = file.FileName,
|
||||
MimeType = contentType,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = Comments
|
||||
};
|
||||
dbContext.DeviceAttachments.Add(da);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
da.SaveAttachment(dbContext, file.InputStream);
|
||||
|
||||
da.GenerateThumbnail(dbContext);
|
||||
|
||||
return Json(da.Id, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
throw new Exception("No Attachment Uploaded");
|
||||
}
|
||||
throw new Exception("Invalid Device Serial Number");
|
||||
}
|
||||
public virtual ActionResult Attachment(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (da != null)
|
||||
{
|
||||
|
||||
var m = new Models.Attachment.AttachmentModel()
|
||||
{
|
||||
Attachment = Models.Attachment._AttachmentModel.FromAttachment(da),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentModel() { Result = "Invalid Attachment Number" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Attachments(string id)
|
||||
{
|
||||
var d = dbContext.Devices.Include("DeviceAttachments.TechUser").Where(m => m.SerialNumber == id).FirstOrDefault();
|
||||
if (d != null)
|
||||
{
|
||||
var m = new Models.Attachment.AttachmentsModel()
|
||||
{
|
||||
Attachments = d.DeviceAttachments.Select(ua => Models.Attachment._AttachmentModel.FromAttachment(ua)).ToList(),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentsModel() { Result = "Invalid Device Serial Number" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult AttachmentRemove(int id)
|
||||
{
|
||||
var da = dbContext.DeviceAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (da != null)
|
||||
{
|
||||
// 2012-02-17 G# Remove - 'Delete Own Comments' policy
|
||||
//if (da.TechUserId == DiscoApplication.CurrentUser.Id)
|
||||
//{
|
||||
da.OnDelete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// return Json("You can only delete your own attachments.", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
}
|
||||
return Json("Invalid Attachment Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,338 +1,338 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Plugins;
|
||||
using Disco.Services.Plugins.Features.WarrantyProvider;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceModelController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDescription = "description";
|
||||
const string pDefaultPurchaseDate = "defaultpurchasedate";
|
||||
const string pDefaultWarrantyProvider = "defaultwarrantyprovider";
|
||||
|
||||
public virtual ActionResult Update(int id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var deviceModel = dbContext.DeviceModels.Find(id);
|
||||
if (deviceModel != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDescription:
|
||||
UpdateDescription(deviceModel, value);
|
||||
break;
|
||||
case pDefaultPurchaseDate:
|
||||
UpdateDefaultPurchaseDate(deviceModel, value);
|
||||
break;
|
||||
case pDefaultWarrantyProvider:
|
||||
UpdateDefaultWarrantyProvider(deviceModel, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Invalid Device Model Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(deviceModel.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDescription(int id, string Description = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDescription, Description, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultPurchaseDate(int id, string DefaultPurchaseDate = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDefaultPurchaseDate, DefaultPurchaseDate, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultWarrantyProvider(int id, string DefaultWarrantyProvider = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDefaultWarrantyProvider, DefaultWarrantyProvider, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDescription(Disco.Models.Repository.DeviceModel deviceModel, string Description)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Description))
|
||||
deviceModel.Description = null;
|
||||
else
|
||||
deviceModel.Description = Description;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateDefaultPurchaseDate(Disco.Models.Repository.DeviceModel deviceModel, string DefaultPurchaseDate)
|
||||
{
|
||||
if (string.IsNullOrEmpty(DefaultPurchaseDate))
|
||||
{
|
||||
deviceModel.DefaultPurchaseDate = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
DateTime d;
|
||||
if (DateTime.TryParse(DefaultPurchaseDate, out d))
|
||||
{
|
||||
deviceModel.DefaultPurchaseDate = d;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateDefaultWarrantyProvider(Disco.Models.Repository.DeviceModel deviceModel, string DefaultWarrantyProvider)
|
||||
{
|
||||
if (string.IsNullOrEmpty(DefaultWarrantyProvider))
|
||||
{
|
||||
deviceModel.DefaultWarrantyProvider = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate
|
||||
var WarrantyProvider = Plugins.GetPluginFeature(DefaultWarrantyProvider, typeof(WarrantyProviderFeature));
|
||||
deviceModel.DefaultWarrantyProvider = WarrantyProvider.Id;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ModelImage
|
||||
[OutputCache(Duration = 31536000, Location = System.Web.UI.OutputCacheLocation.Any, VaryByParam = "*")]
|
||||
public virtual ActionResult Image(int? id, string v = null)
|
||||
{
|
||||
if (id.HasValue)
|
||||
{
|
||||
var m = dbContext.DeviceModels.Find(id.Value);
|
||||
if (m != null)
|
||||
{
|
||||
// Try From DataStore
|
||||
|
||||
var deviceModelImage = m.Image();
|
||||
if (deviceModelImage != null)
|
||||
return File(deviceModelImage, "image/png");
|
||||
//if ( m.Image != null)
|
||||
//{
|
||||
// return File(m.Image, "image/png");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// DataStore Failed - Use Generic Images
|
||||
if (m.ModelType != null)
|
||||
{
|
||||
var modelTypePath = Server.MapPath(string.Format("~/Content/Images/DeviceTypes/{0}.png", m.ModelType));
|
||||
if (System.IO.File.Exists(modelTypePath))
|
||||
{
|
||||
return File(modelTypePath, "image/png");
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
return File(Links.ClientSource.Style.Images.DeviceTypes.Unknown_png, "image/png");
|
||||
}
|
||||
[HttpPost]
|
||||
public virtual ActionResult Image(int id, bool redirect, HttpPostedFileBase Image)
|
||||
{
|
||||
if (Image != null && Image.ContentLength > 0)
|
||||
{
|
||||
var dm = dbContext.DeviceModels.Find(id);
|
||||
if (dm != null)
|
||||
{
|
||||
if (dm.ImageImport(Image.InputStream))
|
||||
{
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(dm.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(dm.Id));
|
||||
else
|
||||
return Json("Invalid Image Format", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index());
|
||||
else
|
||||
return Json("Invalid Device Model Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index());
|
||||
else
|
||||
return Json("No Image Supplied", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Actions
|
||||
|
||||
public virtual ActionResult Delete(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dm = dbContext.DeviceModels.Find(id);
|
||||
if (dm != null)
|
||||
{
|
||||
dm.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Model Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Device Model Components
|
||||
|
||||
public virtual ActionResult Component(int id)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(i => i.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Component Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
public virtual ActionResult ComponentAdd(int? id, string Description, string Cost)
|
||||
{
|
||||
DeviceModel dm = null;
|
||||
if (id.HasValue)
|
||||
{
|
||||
dm = dbContext.DeviceModels.Find(id.Value);
|
||||
if (dm == null)
|
||||
{
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Model Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
decimal cost = 0;
|
||||
if (string.IsNullOrEmpty(Description))
|
||||
Description = "?";
|
||||
if (!string.IsNullOrEmpty(Cost) && Cost.Contains("$"))
|
||||
Cost = Cost.Substring(Cost.IndexOf("$") + 1);
|
||||
decimal.TryParse(Cost, out cost);
|
||||
|
||||
var dc = new Disco.Models.Repository.DeviceComponent()
|
||||
{
|
||||
Description = Description,
|
||||
Cost = cost
|
||||
};
|
||||
if (dm != null)
|
||||
{
|
||||
dc.DeviceModelId = dm.Id;
|
||||
}
|
||||
dc.JobSubTypes = new List<JobSubType>();
|
||||
|
||||
dbContext.DeviceComponents.Add(dc);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ComponentUpdateJobSubTypes(int id, List<string> JobSubTypes)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(i => i.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
dc.JobSubTypes.Clear();
|
||||
|
||||
if (JobSubTypes != null)
|
||||
{
|
||||
var jsts = dbContext.JobSubTypes.Where(jst => JobSubTypes.Contains(jst.JobTypeId + "_" + jst.Id));
|
||||
foreach (var jst in jsts)
|
||||
{
|
||||
dc.JobSubTypes.Add(jst);
|
||||
}
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Component Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ComponentUpdate(int id, string Description, string Cost)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(i => i.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
decimal cost = 0;
|
||||
|
||||
if (string.IsNullOrEmpty(Description))
|
||||
Description = "?";
|
||||
if (!string.IsNullOrEmpty(Cost) && Cost.Contains("$"))
|
||||
Cost = Cost.Substring(Cost.IndexOf("$") + 1);
|
||||
decimal.TryParse(Cost, out cost);
|
||||
|
||||
dc.Description = Description;
|
||||
dc.Cost = cost;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Component Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ComponentRemove(int id)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(c => c.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
dc.JobSubTypes.Clear();
|
||||
dbContext.DeviceComponents.Remove(dc);
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json("Invalid Device Component Id", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Index
|
||||
public virtual ActionResult Index()
|
||||
{
|
||||
var deviceModels = dbContext.DeviceModels.ToArray().Select(dm => Models.DeviceModel._DeviceModel.FromDeviceModel(dm)).ToArray();
|
||||
return Json(deviceModels, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Plugins;
|
||||
using Disco.Services.Plugins.Features.WarrantyProvider;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceModelController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDescription = "description";
|
||||
const string pDefaultPurchaseDate = "defaultpurchasedate";
|
||||
const string pDefaultWarrantyProvider = "defaultwarrantyprovider";
|
||||
|
||||
public virtual ActionResult Update(int id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var deviceModel = dbContext.DeviceModels.Find(id);
|
||||
if (deviceModel != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDescription:
|
||||
UpdateDescription(deviceModel, value);
|
||||
break;
|
||||
case pDefaultPurchaseDate:
|
||||
UpdateDefaultPurchaseDate(deviceModel, value);
|
||||
break;
|
||||
case pDefaultWarrantyProvider:
|
||||
UpdateDefaultWarrantyProvider(deviceModel, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Invalid Device Model Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(deviceModel.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDescription(int id, string Description = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDescription, Description, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultPurchaseDate(int id, string DefaultPurchaseDate = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDefaultPurchaseDate, DefaultPurchaseDate, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultWarrantyProvider(int id, string DefaultWarrantyProvider = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDefaultWarrantyProvider, DefaultWarrantyProvider, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDescription(Disco.Models.Repository.DeviceModel deviceModel, string Description)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Description))
|
||||
deviceModel.Description = null;
|
||||
else
|
||||
deviceModel.Description = Description;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateDefaultPurchaseDate(Disco.Models.Repository.DeviceModel deviceModel, string DefaultPurchaseDate)
|
||||
{
|
||||
if (string.IsNullOrEmpty(DefaultPurchaseDate))
|
||||
{
|
||||
deviceModel.DefaultPurchaseDate = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
DateTime d;
|
||||
if (DateTime.TryParse(DefaultPurchaseDate, out d))
|
||||
{
|
||||
deviceModel.DefaultPurchaseDate = d;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Date Format");
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateDefaultWarrantyProvider(Disco.Models.Repository.DeviceModel deviceModel, string DefaultWarrantyProvider)
|
||||
{
|
||||
if (string.IsNullOrEmpty(DefaultWarrantyProvider))
|
||||
{
|
||||
deviceModel.DefaultWarrantyProvider = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate
|
||||
var WarrantyProvider = Plugins.GetPluginFeature(DefaultWarrantyProvider, typeof(WarrantyProviderFeature));
|
||||
deviceModel.DefaultWarrantyProvider = WarrantyProvider.Id;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ModelImage
|
||||
[OutputCache(Duration = 31536000, Location = System.Web.UI.OutputCacheLocation.Any, VaryByParam = "*")]
|
||||
public virtual ActionResult Image(int? id, string v = null)
|
||||
{
|
||||
if (id.HasValue)
|
||||
{
|
||||
var m = dbContext.DeviceModels.Find(id.Value);
|
||||
if (m != null)
|
||||
{
|
||||
// Try From DataStore
|
||||
|
||||
var deviceModelImage = m.Image();
|
||||
if (deviceModelImage != null)
|
||||
return File(deviceModelImage, "image/png");
|
||||
//if ( m.Image != null)
|
||||
//{
|
||||
// return File(m.Image, "image/png");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// DataStore Failed - Use Generic Images
|
||||
if (m.ModelType != null)
|
||||
{
|
||||
var modelTypePath = Server.MapPath(string.Format("~/Content/Images/DeviceTypes/{0}.png", m.ModelType));
|
||||
if (System.IO.File.Exists(modelTypePath))
|
||||
{
|
||||
return File(modelTypePath, "image/png");
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
return File(Links.ClientSource.Style.Images.DeviceTypes.Unknown_png, "image/png");
|
||||
}
|
||||
[HttpPost]
|
||||
public virtual ActionResult Image(int id, bool redirect, HttpPostedFileBase Image)
|
||||
{
|
||||
if (Image != null && Image.ContentLength > 0)
|
||||
{
|
||||
var dm = dbContext.DeviceModels.Find(id);
|
||||
if (dm != null)
|
||||
{
|
||||
if (dm.ImageImport(Image.InputStream))
|
||||
{
|
||||
dbContext.SaveChanges();
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(dm.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(dm.Id));
|
||||
else
|
||||
return Json("Invalid Image Format", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index());
|
||||
else
|
||||
return Json("Invalid Device Model Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index());
|
||||
else
|
||||
return Json("No Image Supplied", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Actions
|
||||
|
||||
public virtual ActionResult Delete(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dm = dbContext.DeviceModels.Find(id);
|
||||
if (dm != null)
|
||||
{
|
||||
dm.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Model Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Device Model Components
|
||||
|
||||
public virtual ActionResult Component(int id)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(i => i.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Component Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
public virtual ActionResult ComponentAdd(int? id, string Description, string Cost)
|
||||
{
|
||||
DeviceModel dm = null;
|
||||
if (id.HasValue)
|
||||
{
|
||||
dm = dbContext.DeviceModels.Find(id.Value);
|
||||
if (dm == null)
|
||||
{
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Model Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
decimal cost = 0;
|
||||
if (string.IsNullOrEmpty(Description))
|
||||
Description = "?";
|
||||
if (!string.IsNullOrEmpty(Cost) && Cost.Contains("$"))
|
||||
Cost = Cost.Substring(Cost.IndexOf("$") + 1);
|
||||
decimal.TryParse(Cost, out cost);
|
||||
|
||||
var dc = new Disco.Models.Repository.DeviceComponent()
|
||||
{
|
||||
Description = Description,
|
||||
Cost = cost
|
||||
};
|
||||
if (dm != null)
|
||||
{
|
||||
dc.DeviceModelId = dm.Id;
|
||||
}
|
||||
dc.JobSubTypes = new List<JobSubType>();
|
||||
|
||||
dbContext.DeviceComponents.Add(dc);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ComponentUpdateJobSubTypes(int id, List<string> JobSubTypes)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(i => i.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
dc.JobSubTypes.Clear();
|
||||
|
||||
if (JobSubTypes != null)
|
||||
{
|
||||
var jsts = dbContext.JobSubTypes.Where(jst => JobSubTypes.Contains(jst.JobTypeId + "_" + jst.Id));
|
||||
foreach (var jst in jsts)
|
||||
{
|
||||
dc.JobSubTypes.Add(jst);
|
||||
}
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Component Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ComponentUpdate(int id, string Description, string Cost)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(i => i.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
decimal cost = 0;
|
||||
|
||||
if (string.IsNullOrEmpty(Description))
|
||||
Description = "?";
|
||||
if (!string.IsNullOrEmpty(Cost) && Cost.Contains("$"))
|
||||
Cost = Cost.Substring(Cost.IndexOf("$") + 1);
|
||||
decimal.TryParse(Cost, out cost);
|
||||
|
||||
dc.Description = Description;
|
||||
dc.Cost = cost;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "OK", Component = Models.DeviceModel._ComponentModel.FromDeviceComponent(dc) }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.DeviceModel.ComponentModel { Result = "Invalid Device Component Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ComponentRemove(int id)
|
||||
{
|
||||
var dc = dbContext.DeviceComponents.Include("JobSubTypes").Where(c => c.Id == id).FirstOrDefault();
|
||||
if (dc != null)
|
||||
{
|
||||
dc.JobSubTypes.Clear();
|
||||
dbContext.DeviceComponents.Remove(dc);
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json("Invalid Device Component Id", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Index
|
||||
public virtual ActionResult Index()
|
||||
{
|
||||
var deviceModels = dbContext.DeviceModels.ToArray().Select(dm => Models.DeviceModel._DeviceModel.FromDeviceModel(dm)).ToArray();
|
||||
return Json(deviceModels, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,393 +1,393 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Data.Configuration.Modules;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceProfileController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDescription = "description";
|
||||
const string pName = "name";
|
||||
const string pShortName = "shortname";
|
||||
const string pDistributionType = "distributiontype";
|
||||
const string pCertificateProviderId = "certificateproviderid";
|
||||
const string pOrganisationalUnit = "organisationalunit";
|
||||
const string pDefaultOrganisationAddress = "defaultorganisationaddress";
|
||||
const string pComputerNameTemplate = "computernametemplate";
|
||||
const string pEnforceComputerNameConvention = "enforcecomputernameconvention";
|
||||
const string pEnforceOrganisationalUnit = "enforceorganisationalunit";
|
||||
const string pProvisionADAccount = "provisionadaccount";
|
||||
|
||||
public virtual ActionResult Update(int id, string key, string value = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var deviceProfile = dbContext.DeviceProfiles.Find(id);
|
||||
if (deviceProfile != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDescription:
|
||||
UpdateDescription(deviceProfile, value);
|
||||
break;
|
||||
case pName:
|
||||
UpdateName(deviceProfile, value);
|
||||
break;
|
||||
case pShortName:
|
||||
UpdateShortName(deviceProfile, value);
|
||||
break;
|
||||
case pDistributionType:
|
||||
UpdateDistributionType(deviceProfile, value);
|
||||
break;
|
||||
case pCertificateProviderId:
|
||||
UpdateCertificateProviderId(deviceProfile, value);
|
||||
break;
|
||||
case pOrganisationalUnit:
|
||||
UpdateOrganisationalUnit(deviceProfile, value);
|
||||
break;
|
||||
case pDefaultOrganisationAddress:
|
||||
UpdateDefaultOrganisationAddress(deviceProfile, value);
|
||||
break;
|
||||
case pComputerNameTemplate:
|
||||
UpdateComputerNameTemplate(deviceProfile, value);
|
||||
break;
|
||||
case pEnforceComputerNameConvention:
|
||||
UpdateEnforceComputerNameConvention(deviceProfile, value);
|
||||
break;
|
||||
case pEnforceOrganisationalUnit:
|
||||
UpdateEnforceOrganisationalUnit(deviceProfile, value);
|
||||
break;
|
||||
case pProvisionADAccount:
|
||||
UpdateProvisionADAccount(deviceProfile, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(deviceProfile.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDescription(int id, string Description = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pDescription, Description, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateName(int id, string ProfileName = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pName, ProfileName, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateShortName(int id, string ShortName = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pShortName, ShortName, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDistributionType(int id, string DistributionType = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pDistributionType, DistributionType, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateCertificateProviderId(int id, string CertificateProviderId = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pCertificateProviderId, CertificateProviderId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateOrganisationalUnit(int id, string OrganisationalUnit = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pOrganisationalUnit, OrganisationalUnit, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultOrganisationAddress(int id, string DefaultOrganisationAddress = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pDefaultOrganisationAddress, DefaultOrganisationAddress, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateComputerNameTemplate(int id, string ComputerNameTemplate = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pComputerNameTemplate, ComputerNameTemplate, redirect);
|
||||
}
|
||||
// Added 2012-06-14 G#
|
||||
public virtual ActionResult UpdateEnforceComputerNameConvention(int id, string EnforceComputerNameConvention = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pEnforceComputerNameConvention, EnforceComputerNameConvention, redirect);
|
||||
}
|
||||
// Added 2012-06-14 G#
|
||||
public virtual ActionResult UpdateEnforceOrganisationalUnit(int id, string EnforceOrganisationalUnit = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pEnforceOrganisationalUnit, EnforceOrganisationalUnit, redirect);
|
||||
}
|
||||
// Added 2012-06-28 G#
|
||||
public virtual ActionResult UpdateProvisionADAccount(int id, string ProvisionADAccount = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pProvisionADAccount, ProvisionADAccount, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDescription(Disco.Models.Repository.DeviceProfile deviceProfile, string Description)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Description))
|
||||
deviceProfile.Description = null;
|
||||
else
|
||||
deviceProfile.Description = Description;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateName(Disco.Models.Repository.DeviceProfile deviceProfile, string Name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
throw new Exception("Profile name cannot be empty");
|
||||
else
|
||||
deviceProfile.Name = Name;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateShortName(Disco.Models.Repository.DeviceProfile deviceProfile, string ShortName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ShortName))
|
||||
throw new Exception("Profile short name cannot be empty");
|
||||
else
|
||||
deviceProfile.ShortName = ShortName;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateDistributionType(Disco.Models.Repository.DeviceProfile deviceProfile, string DistributionType)
|
||||
{
|
||||
int iDt;
|
||||
if (int.TryParse(DistributionType, out iDt))
|
||||
{
|
||||
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
|
||||
//deviceProfile.Configuration(dbContext).DistributionType = (DeviceProfileConfiguration.DeviceProfileDistributionTypes)iDt;
|
||||
deviceProfile.DistributionType = (Disco.Models.Repository.DeviceProfile.DistributionTypes)iDt;
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Distribution Type Number");
|
||||
}
|
||||
|
||||
private void UpdateCertificateProviderId(Disco.Models.Repository.DeviceProfile deviceProfile, string CertificateProviderId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(CertificateProviderId))
|
||||
{
|
||||
deviceProfile.CertificateProviderId = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate
|
||||
var featureManifest = Disco.Services.Plugins.Plugins.GetPluginFeature(CertificateProviderId, typeof(Disco.Services.Plugins.Features.CertificateProvider.CertificateProviderFeature));
|
||||
if (featureManifest == null)
|
||||
throw new Exception(string.Format("Invalid Certificate Provider Plugin Id: [{0}]", CertificateProviderId));
|
||||
else
|
||||
deviceProfile.CertificateProviderId = featureManifest.Id;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateOrganisationalUnit(Disco.Models.Repository.DeviceProfile deviceProfile, string OrganisationalUnit)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(OrganisationalUnit))
|
||||
OrganisationalUnit = null;
|
||||
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
|
||||
//deviceProfile.Configuration(dbContext).OrganisationalUnit = OrganisationalUnit;
|
||||
deviceProfile.OrganisationalUnit = OrganisationalUnit;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateComputerNameTemplate(Disco.Models.Repository.DeviceProfile deviceProfile, string ComputerNameTemplate)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ComputerNameTemplate))
|
||||
throw new Exception("ComputerNameTemplate is Required");
|
||||
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
|
||||
//deviceProfile.Configuration(dbContext).ComputerNameTemplate = ComputerNameTemplate;
|
||||
deviceProfile.ComputerNameTemplate = ComputerNameTemplate;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateDefaultOrganisationAddress(Disco.Models.Repository.DeviceProfile deviceProfile, string DefaultOrganisationAddress)
|
||||
{
|
||||
if (string.IsNullOrEmpty(DefaultOrganisationAddress))
|
||||
{
|
||||
deviceProfile.DefaultOrganisationAddress = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate
|
||||
int daoId;
|
||||
if (int.TryParse(DefaultOrganisationAddress, out daoId))
|
||||
{
|
||||
var oa = dbContext.DiscoConfiguration.OrganisationAddresses.GetAddress(daoId);
|
||||
if (oa != null)
|
||||
{
|
||||
deviceProfile.DefaultOrganisationAddress = oa.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Default Organisation Address Id (No such Id)");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Default Organisation Address Id (Not Integer)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
// Added 2012-06-14 G#
|
||||
private void UpdateEnforceComputerNameConvention(Disco.Models.Repository.DeviceProfile deviceProfile, string EnforceComputerNameConvention)
|
||||
{
|
||||
bool bValue;
|
||||
if (bool.TryParse(EnforceComputerNameConvention, out bValue))
|
||||
{
|
||||
deviceProfile.EnforceComputerNameConvention = bValue;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Boolean Value");
|
||||
}
|
||||
// Added 2012-06-14 G#
|
||||
private void UpdateEnforceOrganisationalUnit(Disco.Models.Repository.DeviceProfile deviceProfile, string EnforceOrganisationalUnit)
|
||||
{
|
||||
bool bValue;
|
||||
if (bool.TryParse(EnforceOrganisationalUnit, out bValue))
|
||||
{
|
||||
deviceProfile.EnforceOrganisationalUnit = bValue;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Boolean Value");
|
||||
}
|
||||
// Added 2012-06-28 G#
|
||||
private void UpdateProvisionADAccount(Disco.Models.Repository.DeviceProfile deviceProfile, string ProvisionADAccount)
|
||||
{
|
||||
bool bValue;
|
||||
if (bool.TryParse(ProvisionADAccount, out bValue))
|
||||
{
|
||||
deviceProfile.ProvisionADAccount = bValue;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Boolean Value");
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual ActionResult OrganisationalUnits()
|
||||
{
|
||||
var OUs = BI.Interop.ActiveDirectory.ActiveDirectory.GetOrganisationalUnitStructure();
|
||||
return Json(OUs, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#region Actions
|
||||
|
||||
public virtual ActionResult Delete(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dp = dbContext.DeviceProfiles.Find(id);
|
||||
if (dp != null)
|
||||
{
|
||||
dp.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceProfile.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Defaults
|
||||
public virtual ActionResult Default(int id, Nullable<bool> redirect = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dp = dbContext.DeviceProfiles.Find(id);
|
||||
if (dp != null)
|
||||
{
|
||||
dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId = dp.Id;
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceProfile.Index(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
public virtual ActionResult DefaultAddDeviceOffline(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
int defaultValue = 0;
|
||||
if (id > 0)
|
||||
{
|
||||
var dp = dbContext.DeviceProfiles.Find(id);
|
||||
if (dp != null)
|
||||
{
|
||||
defaultValue = dp.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
}
|
||||
dbContext.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId = defaultValue;
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceProfile.Index(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Data.Configuration.Modules;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DeviceProfileController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDescription = "description";
|
||||
const string pName = "name";
|
||||
const string pShortName = "shortname";
|
||||
const string pDistributionType = "distributiontype";
|
||||
const string pCertificateProviderId = "certificateproviderid";
|
||||
const string pOrganisationalUnit = "organisationalunit";
|
||||
const string pDefaultOrganisationAddress = "defaultorganisationaddress";
|
||||
const string pComputerNameTemplate = "computernametemplate";
|
||||
const string pEnforceComputerNameConvention = "enforcecomputernameconvention";
|
||||
const string pEnforceOrganisationalUnit = "enforceorganisationalunit";
|
||||
const string pProvisionADAccount = "provisionadaccount";
|
||||
|
||||
public virtual ActionResult Update(int id, string key, string value = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id < 0)
|
||||
throw new ArgumentOutOfRangeException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var deviceProfile = dbContext.DeviceProfiles.Find(id);
|
||||
if (deviceProfile != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDescription:
|
||||
UpdateDescription(deviceProfile, value);
|
||||
break;
|
||||
case pName:
|
||||
UpdateName(deviceProfile, value);
|
||||
break;
|
||||
case pShortName:
|
||||
UpdateShortName(deviceProfile, value);
|
||||
break;
|
||||
case pDistributionType:
|
||||
UpdateDistributionType(deviceProfile, value);
|
||||
break;
|
||||
case pCertificateProviderId:
|
||||
UpdateCertificateProviderId(deviceProfile, value);
|
||||
break;
|
||||
case pOrganisationalUnit:
|
||||
UpdateOrganisationalUnit(deviceProfile, value);
|
||||
break;
|
||||
case pDefaultOrganisationAddress:
|
||||
UpdateDefaultOrganisationAddress(deviceProfile, value);
|
||||
break;
|
||||
case pComputerNameTemplate:
|
||||
UpdateComputerNameTemplate(deviceProfile, value);
|
||||
break;
|
||||
case pEnforceComputerNameConvention:
|
||||
UpdateEnforceComputerNameConvention(deviceProfile, value);
|
||||
break;
|
||||
case pEnforceOrganisationalUnit:
|
||||
UpdateEnforceOrganisationalUnit(deviceProfile, value);
|
||||
break;
|
||||
case pProvisionADAccount:
|
||||
UpdateProvisionADAccount(deviceProfile, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceModel.Index(deviceProfile.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDescription(int id, string Description = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pDescription, Description, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateName(int id, string ProfileName = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pName, ProfileName, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateShortName(int id, string ShortName = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pShortName, ShortName, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDistributionType(int id, string DistributionType = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pDistributionType, DistributionType, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateCertificateProviderId(int id, string CertificateProviderId = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pCertificateProviderId, CertificateProviderId, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateOrganisationalUnit(int id, string OrganisationalUnit = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pOrganisationalUnit, OrganisationalUnit, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateDefaultOrganisationAddress(int id, string DefaultOrganisationAddress = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pDefaultOrganisationAddress, DefaultOrganisationAddress, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateComputerNameTemplate(int id, string ComputerNameTemplate = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pComputerNameTemplate, ComputerNameTemplate, redirect);
|
||||
}
|
||||
// Added 2012-06-14 G#
|
||||
public virtual ActionResult UpdateEnforceComputerNameConvention(int id, string EnforceComputerNameConvention = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pEnforceComputerNameConvention, EnforceComputerNameConvention, redirect);
|
||||
}
|
||||
// Added 2012-06-14 G#
|
||||
public virtual ActionResult UpdateEnforceOrganisationalUnit(int id, string EnforceOrganisationalUnit = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pEnforceOrganisationalUnit, EnforceOrganisationalUnit, redirect);
|
||||
}
|
||||
// Added 2012-06-28 G#
|
||||
public virtual ActionResult UpdateProvisionADAccount(int id, string ProvisionADAccount = null, Nullable<bool> redirect = null)
|
||||
{
|
||||
return Update(id, pProvisionADAccount, ProvisionADAccount, redirect);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDescription(Disco.Models.Repository.DeviceProfile deviceProfile, string Description)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Description))
|
||||
deviceProfile.Description = null;
|
||||
else
|
||||
deviceProfile.Description = Description;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateName(Disco.Models.Repository.DeviceProfile deviceProfile, string Name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
throw new Exception("Profile name cannot be empty");
|
||||
else
|
||||
deviceProfile.Name = Name;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateShortName(Disco.Models.Repository.DeviceProfile deviceProfile, string ShortName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ShortName))
|
||||
throw new Exception("Profile short name cannot be empty");
|
||||
else
|
||||
deviceProfile.ShortName = ShortName;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateDistributionType(Disco.Models.Repository.DeviceProfile deviceProfile, string DistributionType)
|
||||
{
|
||||
int iDt;
|
||||
if (int.TryParse(DistributionType, out iDt))
|
||||
{
|
||||
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
|
||||
//deviceProfile.Configuration(dbContext).DistributionType = (DeviceProfileConfiguration.DeviceProfileDistributionTypes)iDt;
|
||||
deviceProfile.DistributionType = (Disco.Models.Repository.DeviceProfile.DistributionTypes)iDt;
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Distribution Type Number");
|
||||
}
|
||||
|
||||
private void UpdateCertificateProviderId(Disco.Models.Repository.DeviceProfile deviceProfile, string CertificateProviderId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(CertificateProviderId))
|
||||
{
|
||||
deviceProfile.CertificateProviderId = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate
|
||||
var featureManifest = Disco.Services.Plugins.Plugins.GetPluginFeature(CertificateProviderId, typeof(Disco.Services.Plugins.Features.CertificateProvider.CertificateProviderFeature));
|
||||
if (featureManifest == null)
|
||||
throw new Exception(string.Format("Invalid Certificate Provider Plugin Id: [{0}]", CertificateProviderId));
|
||||
else
|
||||
deviceProfile.CertificateProviderId = featureManifest.Id;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateOrganisationalUnit(Disco.Models.Repository.DeviceProfile deviceProfile, string OrganisationalUnit)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(OrganisationalUnit))
|
||||
OrganisationalUnit = null;
|
||||
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
|
||||
//deviceProfile.Configuration(dbContext).OrganisationalUnit = OrganisationalUnit;
|
||||
deviceProfile.OrganisationalUnit = OrganisationalUnit;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateComputerNameTemplate(Disco.Models.Repository.DeviceProfile deviceProfile, string ComputerNameTemplate)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ComputerNameTemplate))
|
||||
throw new Exception("ComputerNameTemplate is Required");
|
||||
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
|
||||
//deviceProfile.Configuration(dbContext).ComputerNameTemplate = ComputerNameTemplate;
|
||||
deviceProfile.ComputerNameTemplate = ComputerNameTemplate;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
private void UpdateDefaultOrganisationAddress(Disco.Models.Repository.DeviceProfile deviceProfile, string DefaultOrganisationAddress)
|
||||
{
|
||||
if (string.IsNullOrEmpty(DefaultOrganisationAddress))
|
||||
{
|
||||
deviceProfile.DefaultOrganisationAddress = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate
|
||||
int daoId;
|
||||
if (int.TryParse(DefaultOrganisationAddress, out daoId))
|
||||
{
|
||||
var oa = dbContext.DiscoConfiguration.OrganisationAddresses.GetAddress(daoId);
|
||||
if (oa != null)
|
||||
{
|
||||
deviceProfile.DefaultOrganisationAddress = oa.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Default Organisation Address Id (No such Id)");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Default Organisation Address Id (Not Integer)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
// Added 2012-06-14 G#
|
||||
private void UpdateEnforceComputerNameConvention(Disco.Models.Repository.DeviceProfile deviceProfile, string EnforceComputerNameConvention)
|
||||
{
|
||||
bool bValue;
|
||||
if (bool.TryParse(EnforceComputerNameConvention, out bValue))
|
||||
{
|
||||
deviceProfile.EnforceComputerNameConvention = bValue;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Boolean Value");
|
||||
}
|
||||
// Added 2012-06-14 G#
|
||||
private void UpdateEnforceOrganisationalUnit(Disco.Models.Repository.DeviceProfile deviceProfile, string EnforceOrganisationalUnit)
|
||||
{
|
||||
bool bValue;
|
||||
if (bool.TryParse(EnforceOrganisationalUnit, out bValue))
|
||||
{
|
||||
deviceProfile.EnforceOrganisationalUnit = bValue;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Boolean Value");
|
||||
}
|
||||
// Added 2012-06-28 G#
|
||||
private void UpdateProvisionADAccount(Disco.Models.Repository.DeviceProfile deviceProfile, string ProvisionADAccount)
|
||||
{
|
||||
bool bValue;
|
||||
if (bool.TryParse(ProvisionADAccount, out bValue))
|
||||
{
|
||||
deviceProfile.ProvisionADAccount = bValue;
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Boolean Value");
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual ActionResult OrganisationalUnits()
|
||||
{
|
||||
var OUs = BI.Interop.ActiveDirectory.ActiveDirectory.GetOrganisationalUnitStructure();
|
||||
return Json(OUs, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#region Actions
|
||||
|
||||
public virtual ActionResult Delete(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dp = dbContext.DeviceProfiles.Find(id);
|
||||
if (dp != null)
|
||||
{
|
||||
dp.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceProfile.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Defaults
|
||||
public virtual ActionResult Default(int id, Nullable<bool> redirect = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dp = dbContext.DeviceProfiles.Find(id);
|
||||
if (dp != null)
|
||||
{
|
||||
dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId = dp.Id;
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceProfile.Index(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
public virtual ActionResult DefaultAddDeviceOffline(int id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
int defaultValue = 0;
|
||||
if (id > 0)
|
||||
{
|
||||
var dp = dbContext.DeviceProfiles.Find(id);
|
||||
if (dp != null)
|
||||
{
|
||||
defaultValue = dp.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Device Profile Number");
|
||||
}
|
||||
}
|
||||
dbContext.DiscoConfiguration.DeviceProfiles.DefaultAddDeviceOfflineDeviceProfileId = defaultValue;
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DeviceProfile.Index(id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,469 +1,469 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DocumentTemplateController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDescription = "description";
|
||||
const string pScope = "scope";
|
||||
const string pFilterExpression = "filterexpression";
|
||||
const string pFlattenForm = "flattenform";
|
||||
|
||||
public virtual ActionResult Update(string id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDescription:
|
||||
UpdateDescription(documentTemplate, value);
|
||||
break;
|
||||
case pScope:
|
||||
UpdateScope(documentTemplate, value);
|
||||
break;
|
||||
case pFilterExpression:
|
||||
UpdateFilterExpression(documentTemplate, value);
|
||||
break;
|
||||
case pFlattenForm:
|
||||
UpdateFlattenForm(documentTemplate, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Document Template Id");
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DocumentTemplate.Index(documentTemplate.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public virtual ActionResult Template(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
|
||||
var filename = documentTemplate.RepositoryFilename(dbContext);
|
||||
if (System.IO.File.Exists(filename))
|
||||
{
|
||||
return File(filename, DocumentTemplate.PdfMimeType, string.Format("{0}.pdf", documentTemplate.Id));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Template not found");
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public virtual ActionResult Template(string id, bool redirect, HttpPostedFileBase Template)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
|
||||
documentTemplate.SavePdfTemplate(dbContext, Template.InputStream);
|
||||
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DocumentTemplate.Index(documentTemplate.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDescription(string id, string Description = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDescription, Description, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateFilterExpression(string id, string FilterExpression = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pFilterExpression, FilterExpression, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateFlattenForm(string id, string FlattenForm = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pFlattenForm, FlattenForm, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateScope(string id, string Scope = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pScope, Scope, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateSubTypes(string id, List<string> SubTypes = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
|
||||
UpdateSubTypes(documentTemplate, SubTypes);
|
||||
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDescription(Disco.Models.Repository.DocumentTemplate documentTemplate, string Description)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(Description))
|
||||
{
|
||||
documentTemplate.Description = Description.Trim();
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Description");
|
||||
}
|
||||
private void UpdateScope(Disco.Models.Repository.DocumentTemplate documentTemplate, string Scope)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(Scope))
|
||||
{
|
||||
if (Disco.Models.Repository.DocumentTemplate.DocumentTemplateScopes.ToList().Contains(Scope))
|
||||
{
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
|
||||
documentTemplate.Scope = Scope;
|
||||
|
||||
if (documentTemplate.Scope != Disco.Models.Repository.DocumentTemplate.DocumentTemplateScopes.Job &&
|
||||
documentTemplate.JobSubTypes != null)
|
||||
{
|
||||
foreach (var st in documentTemplate.JobSubTypes.ToArray())
|
||||
documentTemplate.JobSubTypes.Remove(st);
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Exception("Invalid Scope");
|
||||
}
|
||||
private void UpdateFilterExpression(Disco.Models.Repository.DocumentTemplate documentTemplate, string FilterExpression)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(FilterExpression))
|
||||
{
|
||||
documentTemplate.FilterExpression = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
documentTemplate.FilterExpression = FilterExpression.Trim();
|
||||
}
|
||||
// Invalidate Cache
|
||||
documentTemplate.FilterExpressionInvalidateCache();
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateFlattenForm(Disco.Models.Repository.DocumentTemplate documentTemplate, string FlattenForm)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(FlattenForm))
|
||||
{
|
||||
documentTemplate.FlattenForm = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
bool ff = default(bool);
|
||||
if (bool.TryParse(FlattenForm, out ff))
|
||||
documentTemplate.FlattenForm = ff;
|
||||
else
|
||||
throw new Exception("Invalid Boolean Format");
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateSubTypes(Disco.Models.Repository.DocumentTemplate documentTemplate, List<string> SubTypes)
|
||||
{
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
|
||||
// Remove All Existing
|
||||
if (documentTemplate.JobSubTypes != null)
|
||||
{
|
||||
foreach (var st in documentTemplate.JobSubTypes.ToArray())
|
||||
documentTemplate.JobSubTypes.Remove(st);
|
||||
}
|
||||
|
||||
// Add New
|
||||
if (SubTypes != null && SubTypes.Count > 0)
|
||||
{
|
||||
var subTypes = new List<Disco.Models.Repository.JobSubType>();
|
||||
foreach (var stId in SubTypes)
|
||||
{
|
||||
var typeId = stId.Substring(0, stId.IndexOf("_"));
|
||||
var subTypeId = stId.Substring(stId.IndexOf("_") + 1);
|
||||
var subType = dbContext.JobSubTypes.FirstOrDefault(jst => jst.JobTypeId == typeId && jst.Id == subTypeId);
|
||||
subTypes.Add(subType);
|
||||
}
|
||||
documentTemplate.JobSubTypes = subTypes;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region Actions
|
||||
|
||||
[OutputCache(NoStore = true, Duration = 0)]
|
||||
public virtual ActionResult ImporterThumbnail(string SessionId, int PageNumber)
|
||||
{
|
||||
// Load from Cache
|
||||
//var cacheKey = string.Format("Disco.BI.DocumentImporter-{0}-{1}", SessionId, PageNumber);
|
||||
//var cacheValue = HttpContext.Cache.Get(cacheKey);
|
||||
//if (cacheValue != null)
|
||||
//{
|
||||
// var cacheFile = cacheValue as byte[];
|
||||
// if (cacheFile != null)
|
||||
// {
|
||||
// return File(cacheFile, "image/png");
|
||||
// }
|
||||
//}
|
||||
|
||||
var dataStoreSessionPagesCacheLocation = DataStore.CreateLocation(dbContext, "Cache\\DocumentDropBox_SessionPages");
|
||||
var filename = System.IO.Path.Combine(dataStoreSessionPagesCacheLocation, string.Format("{0}-{1}", SessionId, PageNumber));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, "image/png");
|
||||
else
|
||||
return File("~/Content/Images/Status/fileBroken256.png", "image/png");
|
||||
}
|
||||
|
||||
public virtual ActionResult ImporterUndetectedFiles()
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
var undetectedDirectory = new System.IO.DirectoryInfo(undetectedLocation);
|
||||
var m = undetectedDirectory.GetFiles("*.pdf").Select(f => new Models.DocumentTemplate.ImporterUndetectedFilesModel()
|
||||
{
|
||||
Id = System.IO.Path.GetFileNameWithoutExtension(f.Name),
|
||||
Timestamp = f.CreationTime.ToString(),
|
||||
TimestampFuzzy = f.CreationTime.ToFuzzy()
|
||||
}).ToArray();
|
||||
|
||||
return Json(m);
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedDataIdLookup(string id, string term, int limitCount = 20)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(id) && !string.IsNullOrWhiteSpace(term))
|
||||
{
|
||||
string searchScope;
|
||||
if (id.StartsWith("--"))
|
||||
{
|
||||
switch (id.ToUpper())
|
||||
{
|
||||
case "--DEVICE":
|
||||
searchScope = DocumentTemplate.DocumentTemplateScopes.Device;
|
||||
break;
|
||||
case "--JOB":
|
||||
searchScope = DocumentTemplate.DocumentTemplateScopes.Job;
|
||||
break;
|
||||
case "--USER":
|
||||
searchScope = DocumentTemplate.DocumentTemplateScopes.User;
|
||||
break;
|
||||
default:
|
||||
searchScope = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate != null)
|
||||
searchScope = documentTemplate.Scope;
|
||||
else
|
||||
searchScope = null;
|
||||
}
|
||||
if (searchScope != null)
|
||||
{
|
||||
Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel[] results;
|
||||
switch (searchScope)
|
||||
{
|
||||
case DocumentTemplate.DocumentTemplateScopes.Device:
|
||||
results = BI.DeviceBI.Searching.Search(dbContext, term, limitCount).Select(sr => Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel.FromSearchResultItem(sr)).ToArray();
|
||||
break;
|
||||
case DocumentTemplate.DocumentTemplateScopes.Job:
|
||||
results = BI.JobBI.Searching.Search(dbContext, term, limitCount, false).Items.Select(sr => Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel.FromSearchResultItem(sr)).ToArray();
|
||||
break;
|
||||
case DocumentTemplate.DocumentTemplateScopes.User:
|
||||
results = BI.UserBI.Searching.Search(dbContext, term, limitCount).Select(sr => Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel.FromSearchResultItem(sr)).ToArray();
|
||||
break;
|
||||
default:
|
||||
results = null;
|
||||
break;
|
||||
}
|
||||
if (results != null)
|
||||
return Json(results, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
}
|
||||
return Json(null, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedFile(string id, Nullable<bool> Source, Nullable<bool> Thumbnail)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(id))
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
if (Source.HasValue && Source.Value)
|
||||
{
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".pdf"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, DocumentTemplate.PdfMimeType);
|
||||
else
|
||||
return HttpNotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Thumbnail.HasValue && Thumbnail.Value)
|
||||
{
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, "_thumbnail.png"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, "image/png");
|
||||
else
|
||||
return File(Links.ClientSource.Style.Images.Status.fileBroken256_png, "image/png");
|
||||
}
|
||||
else
|
||||
{
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".jpg"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, "image/jpeg");
|
||||
else
|
||||
return File(Links.ClientSource.Style.Images.Status.fileBroken256_png, "image/png");
|
||||
}
|
||||
}
|
||||
}
|
||||
return HttpNotFound();
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedAssign(string id, string DocumentTemplateId, string DataId)
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".pdf"));
|
||||
if (BI.Interop.Pdf.PdfImporter.ProcessPdfAttachment(filename, dbContext, DocumentTemplateId, DataId, DiscoApplication.CurrentUser.Id, DateTime.Now))
|
||||
{
|
||||
// Delete File
|
||||
System.IO.File.Delete(filename);
|
||||
|
||||
// Delete Thumbnail/Preview
|
||||
var thumbnailFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, "_thumbnail.png"));
|
||||
if (System.IO.File.Exists(thumbnailFilename))
|
||||
System.IO.File.Delete(thumbnailFilename);
|
||||
var previewFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".jpg"));
|
||||
if (System.IO.File.Exists(previewFilename))
|
||||
System.IO.File.Delete(previewFilename);
|
||||
|
||||
return Json("OK");
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Unable to Import File with the supplied parameters");
|
||||
}
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedDelete(string id)
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".pdf"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
{
|
||||
// Delete File
|
||||
System.IO.File.Delete(filename);
|
||||
|
||||
// Delete Thumbnail/Preview
|
||||
var thumbnailFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, "_thumbnail.png"));
|
||||
if (System.IO.File.Exists(thumbnailFilename))
|
||||
System.IO.File.Delete(thumbnailFilename);
|
||||
var previewFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".jpg"));
|
||||
if (System.IO.File.Exists(previewFilename))
|
||||
System.IO.File.Delete(previewFilename);
|
||||
|
||||
return Json("OK");
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("File Not Found");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual ActionResult BulkGenerate(string id, string DataIds = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(DataIds))
|
||||
throw new ArgumentNullException("DataIds");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
|
||||
var dataIds = DataIds.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var timeStamp = DateTime.Now;
|
||||
var pdf = documentTemplate.GeneratePdfBulk(dbContext, DiscoApplication.CurrentUser, timeStamp, dataIds);
|
||||
|
||||
return File(pdf, "application/pdf", string.Format("{0}_Bulk_{1:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, timeStamp));
|
||||
}
|
||||
|
||||
public virtual ActionResult Delete(string id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var at = dbContext.DocumentTemplates.Include("JobSubTypes").FirstOrDefault(a => a.Id == id);
|
||||
if (at != null)
|
||||
{
|
||||
at.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DocumentTemplate.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Document Template Id");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Models.Repository;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class DocumentTemplateController : dbAdminController
|
||||
{
|
||||
|
||||
const string pDescription = "description";
|
||||
const string pScope = "scope";
|
||||
const string pFilterExpression = "filterexpression";
|
||||
const string pFlattenForm = "flattenform";
|
||||
|
||||
public virtual ActionResult Update(string id, string key, string value = null, bool redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException("key");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
switch (key.ToLower())
|
||||
{
|
||||
case pDescription:
|
||||
UpdateDescription(documentTemplate, value);
|
||||
break;
|
||||
case pScope:
|
||||
UpdateScope(documentTemplate, value);
|
||||
break;
|
||||
case pFilterExpression:
|
||||
UpdateFilterExpression(documentTemplate, value);
|
||||
break;
|
||||
case pFlattenForm:
|
||||
UpdateFlattenForm(documentTemplate, value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Invalid Update Key");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Document Template Id");
|
||||
}
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DocumentTemplate.Index(documentTemplate.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public virtual ActionResult Template(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
|
||||
var filename = documentTemplate.RepositoryFilename(dbContext);
|
||||
if (System.IO.File.Exists(filename))
|
||||
{
|
||||
return File(filename, DocumentTemplate.PdfMimeType, string.Format("{0}.pdf", documentTemplate.Id));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Template not found");
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public virtual ActionResult Template(string id, bool redirect, HttpPostedFileBase Template)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
|
||||
documentTemplate.SavePdfTemplate(dbContext, Template.InputStream);
|
||||
|
||||
if (redirect)
|
||||
return RedirectToAction(MVC.Config.DocumentTemplate.Index(documentTemplate.Id));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#region Update Shortcut Methods
|
||||
public virtual ActionResult UpdateDescription(string id, string Description = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pDescription, Description, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateFilterExpression(string id, string FilterExpression = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pFilterExpression, FilterExpression, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateFlattenForm(string id, string FlattenForm = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pFlattenForm, FlattenForm, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateScope(string id, string Scope = null, bool redirect = false)
|
||||
{
|
||||
return Update(id, pScope, Scope, redirect);
|
||||
}
|
||||
public virtual ActionResult UpdateSubTypes(string id, List<string> SubTypes = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
|
||||
UpdateSubTypes(documentTemplate, SubTypes);
|
||||
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update Properties
|
||||
private void UpdateDescription(Disco.Models.Repository.DocumentTemplate documentTemplate, string Description)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(Description))
|
||||
{
|
||||
documentTemplate.Description = Description.Trim();
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
throw new Exception("Invalid Description");
|
||||
}
|
||||
private void UpdateScope(Disco.Models.Repository.DocumentTemplate documentTemplate, string Scope)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(Scope))
|
||||
{
|
||||
if (Disco.Models.Repository.DocumentTemplate.DocumentTemplateScopes.ToList().Contains(Scope))
|
||||
{
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
|
||||
documentTemplate.Scope = Scope;
|
||||
|
||||
if (documentTemplate.Scope != Disco.Models.Repository.DocumentTemplate.DocumentTemplateScopes.Job &&
|
||||
documentTemplate.JobSubTypes != null)
|
||||
{
|
||||
foreach (var st in documentTemplate.JobSubTypes.ToArray())
|
||||
documentTemplate.JobSubTypes.Remove(st);
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Exception("Invalid Scope");
|
||||
}
|
||||
private void UpdateFilterExpression(Disco.Models.Repository.DocumentTemplate documentTemplate, string FilterExpression)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(FilterExpression))
|
||||
{
|
||||
documentTemplate.FilterExpression = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
documentTemplate.FilterExpression = FilterExpression.Trim();
|
||||
}
|
||||
// Invalidate Cache
|
||||
documentTemplate.FilterExpressionInvalidateCache();
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateFlattenForm(Disco.Models.Repository.DocumentTemplate documentTemplate, string FlattenForm)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(FlattenForm))
|
||||
{
|
||||
documentTemplate.FlattenForm = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
bool ff = default(bool);
|
||||
if (bool.TryParse(FlattenForm, out ff))
|
||||
documentTemplate.FlattenForm = ff;
|
||||
else
|
||||
throw new Exception("Invalid Boolean Format");
|
||||
}
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
private void UpdateSubTypes(Disco.Models.Repository.DocumentTemplate documentTemplate, List<string> SubTypes)
|
||||
{
|
||||
dbContext.Configuration.LazyLoadingEnabled = true;
|
||||
|
||||
// Remove All Existing
|
||||
if (documentTemplate.JobSubTypes != null)
|
||||
{
|
||||
foreach (var st in documentTemplate.JobSubTypes.ToArray())
|
||||
documentTemplate.JobSubTypes.Remove(st);
|
||||
}
|
||||
|
||||
// Add New
|
||||
if (SubTypes != null && SubTypes.Count > 0)
|
||||
{
|
||||
var subTypes = new List<Disco.Models.Repository.JobSubType>();
|
||||
foreach (var stId in SubTypes)
|
||||
{
|
||||
var typeId = stId.Substring(0, stId.IndexOf("_"));
|
||||
var subTypeId = stId.Substring(stId.IndexOf("_") + 1);
|
||||
var subType = dbContext.JobSubTypes.FirstOrDefault(jst => jst.JobTypeId == typeId && jst.Id == subTypeId);
|
||||
subTypes.Add(subType);
|
||||
}
|
||||
documentTemplate.JobSubTypes = subTypes;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region Actions
|
||||
|
||||
[OutputCache(NoStore = true, Duration = 0)]
|
||||
public virtual ActionResult ImporterThumbnail(string SessionId, int PageNumber)
|
||||
{
|
||||
// Load from Cache
|
||||
//var cacheKey = string.Format("Disco.BI.DocumentImporter-{0}-{1}", SessionId, PageNumber);
|
||||
//var cacheValue = HttpContext.Cache.Get(cacheKey);
|
||||
//if (cacheValue != null)
|
||||
//{
|
||||
// var cacheFile = cacheValue as byte[];
|
||||
// if (cacheFile != null)
|
||||
// {
|
||||
// return File(cacheFile, "image/png");
|
||||
// }
|
||||
//}
|
||||
|
||||
var dataStoreSessionPagesCacheLocation = DataStore.CreateLocation(dbContext, "Cache\\DocumentDropBox_SessionPages");
|
||||
var filename = System.IO.Path.Combine(dataStoreSessionPagesCacheLocation, string.Format("{0}-{1}", SessionId, PageNumber));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, "image/png");
|
||||
else
|
||||
return File("~/Content/Images/Status/fileBroken256.png", "image/png");
|
||||
}
|
||||
|
||||
public virtual ActionResult ImporterUndetectedFiles()
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
var undetectedDirectory = new System.IO.DirectoryInfo(undetectedLocation);
|
||||
var m = undetectedDirectory.GetFiles("*.pdf").Select(f => new Models.DocumentTemplate.ImporterUndetectedFilesModel()
|
||||
{
|
||||
Id = System.IO.Path.GetFileNameWithoutExtension(f.Name),
|
||||
Timestamp = f.CreationTime.ToString(),
|
||||
TimestampFuzzy = f.CreationTime.ToFuzzy()
|
||||
}).ToArray();
|
||||
|
||||
return Json(m);
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedDataIdLookup(string id, string term, int limitCount = 20)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(id) && !string.IsNullOrWhiteSpace(term))
|
||||
{
|
||||
string searchScope;
|
||||
if (id.StartsWith("--"))
|
||||
{
|
||||
switch (id.ToUpper())
|
||||
{
|
||||
case "--DEVICE":
|
||||
searchScope = DocumentTemplate.DocumentTemplateScopes.Device;
|
||||
break;
|
||||
case "--JOB":
|
||||
searchScope = DocumentTemplate.DocumentTemplateScopes.Job;
|
||||
break;
|
||||
case "--USER":
|
||||
searchScope = DocumentTemplate.DocumentTemplateScopes.User;
|
||||
break;
|
||||
default:
|
||||
searchScope = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate != null)
|
||||
searchScope = documentTemplate.Scope;
|
||||
else
|
||||
searchScope = null;
|
||||
}
|
||||
if (searchScope != null)
|
||||
{
|
||||
Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel[] results;
|
||||
switch (searchScope)
|
||||
{
|
||||
case DocumentTemplate.DocumentTemplateScopes.Device:
|
||||
results = BI.DeviceBI.Searching.Search(dbContext, term, limitCount).Select(sr => Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel.FromSearchResultItem(sr)).ToArray();
|
||||
break;
|
||||
case DocumentTemplate.DocumentTemplateScopes.Job:
|
||||
results = BI.JobBI.Searching.Search(dbContext, term, limitCount, false).Items.Select(sr => Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel.FromSearchResultItem(sr)).ToArray();
|
||||
break;
|
||||
case DocumentTemplate.DocumentTemplateScopes.User:
|
||||
results = BI.UserBI.Searching.Search(dbContext, term, limitCount).Select(sr => Models.DocumentTemplate.ImporterUndetectedDataIdLookupModel.FromSearchResultItem(sr)).ToArray();
|
||||
break;
|
||||
default:
|
||||
results = null;
|
||||
break;
|
||||
}
|
||||
if (results != null)
|
||||
return Json(results, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
}
|
||||
return Json(null, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedFile(string id, Nullable<bool> Source, Nullable<bool> Thumbnail)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(id))
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
if (Source.HasValue && Source.Value)
|
||||
{
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".pdf"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, DocumentTemplate.PdfMimeType);
|
||||
else
|
||||
return HttpNotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Thumbnail.HasValue && Thumbnail.Value)
|
||||
{
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, "_thumbnail.png"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, "image/png");
|
||||
else
|
||||
return File(Links.ClientSource.Style.Images.Status.fileBroken256_png, "image/png");
|
||||
}
|
||||
else
|
||||
{
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".jpg"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
return File(filename, "image/jpeg");
|
||||
else
|
||||
return File(Links.ClientSource.Style.Images.Status.fileBroken256_png, "image/png");
|
||||
}
|
||||
}
|
||||
}
|
||||
return HttpNotFound();
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedAssign(string id, string DocumentTemplateId, string DataId)
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".pdf"));
|
||||
if (BI.Interop.Pdf.PdfImporter.ProcessPdfAttachment(filename, dbContext, DocumentTemplateId, DataId, DiscoApplication.CurrentUser.Id, DateTime.Now))
|
||||
{
|
||||
// Delete File
|
||||
System.IO.File.Delete(filename);
|
||||
|
||||
// Delete Thumbnail/Preview
|
||||
var thumbnailFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, "_thumbnail.png"));
|
||||
if (System.IO.File.Exists(thumbnailFilename))
|
||||
System.IO.File.Delete(thumbnailFilename);
|
||||
var previewFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".jpg"));
|
||||
if (System.IO.File.Exists(previewFilename))
|
||||
System.IO.File.Delete(previewFilename);
|
||||
|
||||
return Json("OK");
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("Unable to Import File with the supplied parameters");
|
||||
}
|
||||
}
|
||||
public virtual ActionResult ImporterUndetectedDelete(string id)
|
||||
{
|
||||
var undetectedLocation = DataStore.CreateLocation(dbContext, "DocumentDropBox_Unassigned");
|
||||
var filename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".pdf"));
|
||||
if (System.IO.File.Exists(filename))
|
||||
{
|
||||
// Delete File
|
||||
System.IO.File.Delete(filename);
|
||||
|
||||
// Delete Thumbnail/Preview
|
||||
var thumbnailFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, "_thumbnail.png"));
|
||||
if (System.IO.File.Exists(thumbnailFilename))
|
||||
System.IO.File.Delete(thumbnailFilename);
|
||||
var previewFilename = System.IO.Path.Combine(undetectedLocation, string.Concat(id, ".jpg"));
|
||||
if (System.IO.File.Exists(previewFilename))
|
||||
System.IO.File.Delete(previewFilename);
|
||||
|
||||
return Json("OK");
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json("File Not Found");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual ActionResult BulkGenerate(string id, string DataIds = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(DataIds))
|
||||
throw new ArgumentNullException("DataIds");
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(id);
|
||||
if (documentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
|
||||
var dataIds = DataIds.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var timeStamp = DateTime.Now;
|
||||
var pdf = documentTemplate.GeneratePdfBulk(dbContext, DiscoApplication.CurrentUser, timeStamp, dataIds);
|
||||
|
||||
return File(pdf, "application/pdf", string.Format("{0}_Bulk_{1:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, timeStamp));
|
||||
}
|
||||
|
||||
public virtual ActionResult Delete(string id, Nullable<bool> redirect = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var at = dbContext.DocumentTemplates.Include("JobSubTypes").FirstOrDefault(a => a.Id == id);
|
||||
if (at != null)
|
||||
{
|
||||
at.Delete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
return RedirectToAction(MVC.Config.DocumentTemplate.Index(null));
|
||||
else
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
throw new Exception("Invalid Document Template Id");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (redirect.HasValue && redirect.Value)
|
||||
throw;
|
||||
else
|
||||
return Json(string.Format("Error: {0}", ex.Message), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class ExpressionsController : dbAdminController
|
||||
{
|
||||
public virtual ActionResult ValidateExpression(string Expression)
|
||||
{
|
||||
var part = new BI.Expressions.EvaluateExpressionPart(Expression);
|
||||
return Json(Models.Expressions.ValidateExpressionModel.FromEvaluateExpressionPart(part), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class ExpressionsController : dbAdminController
|
||||
{
|
||||
public virtual ActionResult ValidateExpression(string Expression)
|
||||
{
|
||||
var part = new BI.Expressions.EvaluateExpressionPart(Expression);
|
||||
return Json(Models.Expressions.ValidateExpressionModel.FromEvaluateExpressionPart(part), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,62 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Services.Logging;
|
||||
using Disco.Services.Tasks;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class LoggingController : dbAdminController
|
||||
{
|
||||
public virtual ActionResult Modules()
|
||||
{
|
||||
var m = LogContext.LogModules.Values.Select(lm => Models.Logs.LogModuleModel.FromLogModule(lm)).ToList();
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult RetrieveEvents(string Format, DateTime? Start = null, DateTime? End = null, int? ModuleId = null, List<int> EventTypeIds = null, int? Take = null)
|
||||
{
|
||||
var logRetriever = new ReadLogContext()
|
||||
{
|
||||
Start = Start,
|
||||
End = End,
|
||||
Module = ModuleId,
|
||||
EventTypes = EventTypeIds,
|
||||
Take = Take
|
||||
};
|
||||
var results = logRetriever.Query(dbContext);
|
||||
|
||||
switch (Format.ToLower())
|
||||
{
|
||||
case "json":
|
||||
{
|
||||
return Json(results, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
case "csv":
|
||||
{
|
||||
return File(results.ToCsv(), "text/csv", "DiscoLogs.csv");
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentException("Unknown Format", "Format");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public virtual ActionResult ScheduledTaskStatus(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
|
||||
var status = ScheduledTasks.GetTaskStatus(id);
|
||||
|
||||
if (status == null)
|
||||
throw new ArgumentException("Invalid ScheduledTask SessionId", "id");
|
||||
|
||||
return Json(ScheduledTaskStatusLive.FromScheduledTaskStatus(status, null), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Services.Logging;
|
||||
using Disco.Services.Tasks;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class LoggingController : dbAdminController
|
||||
{
|
||||
public virtual ActionResult Modules()
|
||||
{
|
||||
var m = LogContext.LogModules.Values.Select(lm => Models.Logs.LogModuleModel.FromLogModule(lm)).ToList();
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult RetrieveEvents(string Format, DateTime? Start = null, DateTime? End = null, int? ModuleId = null, List<int> EventTypeIds = null, int? Take = null)
|
||||
{
|
||||
var logRetriever = new ReadLogContext()
|
||||
{
|
||||
Start = Start,
|
||||
End = End,
|
||||
Module = ModuleId,
|
||||
EventTypes = EventTypeIds,
|
||||
Take = Take
|
||||
};
|
||||
var results = logRetriever.Query(dbContext);
|
||||
|
||||
switch (Format.ToLower())
|
||||
{
|
||||
case "json":
|
||||
{
|
||||
return Json(results, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
case "csv":
|
||||
{
|
||||
return File(results.ToCsv(), "text/csv", "DiscoLogs.csv");
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentException("Unknown Format", "Format");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public virtual ActionResult ScheduledTaskStatus(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
|
||||
var status = ScheduledTasks.GetTaskStatus(id);
|
||||
|
||||
if (status == null)
|
||||
throw new ArgumentException("Invalid ScheduledTask SessionId", "id");
|
||||
|
||||
return Json(ScheduledTaskStatusLive.FromScheduledTaskStatus(status, null), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,180 +1,180 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using System.IO;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class UserController : dbAdminController
|
||||
{
|
||||
public virtual ActionResult UpstreamUsers(string term)
|
||||
{
|
||||
return Json(BI.UserBI.Searching.SearchUpstream(term), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#region User Attachements
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentDownload(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Find(id);
|
||||
if (ua != null)
|
||||
{
|
||||
var filePath = ua.RepositoryFilename(dbContext);
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
return File(filePath, ua.MimeType, ua.Filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
return HttpNotFound("Attachment reference exists, but file not found");
|
||||
}
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentThumbnail(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Find(id);
|
||||
if (ua != null)
|
||||
{
|
||||
var thumbPath = ua.RepositoryThumbnailFilename(dbContext);
|
||||
if (System.IO.File.Exists(thumbPath))
|
||||
{
|
||||
if (thumbPath.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
|
||||
return File(thumbPath, "image/png");
|
||||
else
|
||||
return File(thumbPath, "image/jpg");
|
||||
}
|
||||
else
|
||||
return File(ClientSource.Style.Images.AttachmentTypes.MimeTypeIcons.Icon(ua.MimeType), "image/png");
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
public virtual ActionResult AttachmentUpload(string id, string Comments)
|
||||
{
|
||||
var u = dbContext.Users.Find(id);
|
||||
if (u != null)
|
||||
{
|
||||
if (Request.Files.Count > 0)
|
||||
{
|
||||
var file = Request.Files.Get(0);
|
||||
if (file.ContentLength > 0)
|
||||
{
|
||||
var contentType = file.ContentType;
|
||||
if (string.IsNullOrEmpty(contentType) || contentType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
contentType = BI.Interop.MimeTypes.ResolveMimeType(file.FileName);
|
||||
|
||||
var ua = new Disco.Models.Repository.UserAttachment()
|
||||
{
|
||||
UserId = u.Id,
|
||||
TechUserId = DiscoApplication.CurrentUser.Id,
|
||||
Filename = file.FileName,
|
||||
MimeType = contentType,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = Comments
|
||||
};
|
||||
dbContext.UserAttachments.Add(ua);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
ua.SaveAttachment(dbContext, file.InputStream);
|
||||
|
||||
ua.GenerateThumbnail(dbContext);
|
||||
|
||||
return Json(ua.Id, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
throw new Exception("No Attachment Uploaded");
|
||||
}
|
||||
throw new Exception("Invalid User Id");
|
||||
}
|
||||
public virtual ActionResult Attachment(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (ua != null)
|
||||
{
|
||||
|
||||
var m = new Models.Attachment.AttachmentModel()
|
||||
{
|
||||
Attachment = Models.Attachment._AttachmentModel.FromAttachment(ua),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentModel() { Result = "Invalid Attachment Number" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Attachments(string id)
|
||||
{
|
||||
var u = dbContext.Users.Include("UserAttachments.TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (u != null)
|
||||
{
|
||||
var m = new Models.Attachment.AttachmentsModel()
|
||||
{
|
||||
Attachments = u.UserAttachments.Select(ua => Models.Attachment._AttachmentModel.FromAttachment(ua)).ToList(),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentsModel() { Result = "Invalid User Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult AttachmentRemove(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (ua != null)
|
||||
{
|
||||
// 2012-02-17 G# Remove - 'Delete Own Comments' policy
|
||||
//if (ua.TechUserId == DiscoApplication.CurrentUser.Id)
|
||||
//{
|
||||
ua.OnDelete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// return Json("You can only delete your own attachments.", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
}
|
||||
return Json("Invalid Attachment Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public virtual ActionResult GeneratePdf(string id, string DocumentTemplateId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(DocumentTemplateId))
|
||||
throw new ArgumentNullException("AttachmentTypeId");
|
||||
var user = dbContext.Users.Find(id);
|
||||
if (user != null)
|
||||
{
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(DocumentTemplateId);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
var timeStamp = DateTime.Now;
|
||||
Stream pdf;
|
||||
using (var generationState = Disco.Models.BI.DocumentTemplates.DocumentState.DefaultState())
|
||||
{
|
||||
pdf = documentTemplate.GeneratePdf(dbContext, user, DiscoApplication.CurrentUser, timeStamp, generationState);
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, user.Id, timeStamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid User Id", "id");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Disco.BI;
|
||||
using Disco.BI.Extensions;
|
||||
using System.IO;
|
||||
|
||||
namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
public partial class UserController : dbAdminController
|
||||
{
|
||||
public virtual ActionResult UpstreamUsers(string term)
|
||||
{
|
||||
return Json(BI.UserBI.Searching.SearchUpstream(term), JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#region User Attachements
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentDownload(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Find(id);
|
||||
if (ua != null)
|
||||
{
|
||||
var filePath = ua.RepositoryFilename(dbContext);
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
return File(filePath, ua.MimeType, ua.Filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
return HttpNotFound("Attachment reference exists, but file not found");
|
||||
}
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentThumbnail(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Find(id);
|
||||
if (ua != null)
|
||||
{
|
||||
var thumbPath = ua.RepositoryThumbnailFilename(dbContext);
|
||||
if (System.IO.File.Exists(thumbPath))
|
||||
{
|
||||
if (thumbPath.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
|
||||
return File(thumbPath, "image/png");
|
||||
else
|
||||
return File(thumbPath, "image/jpg");
|
||||
}
|
||||
else
|
||||
return File(ClientSource.Style.Images.AttachmentTypes.MimeTypeIcons.Icon(ua.MimeType), "image/png");
|
||||
}
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
public virtual ActionResult AttachmentUpload(string id, string Comments)
|
||||
{
|
||||
var u = dbContext.Users.Find(id);
|
||||
if (u != null)
|
||||
{
|
||||
if (Request.Files.Count > 0)
|
||||
{
|
||||
var file = Request.Files.Get(0);
|
||||
if (file.ContentLength > 0)
|
||||
{
|
||||
var contentType = file.ContentType;
|
||||
if (string.IsNullOrEmpty(contentType) || contentType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
contentType = BI.Interop.MimeTypes.ResolveMimeType(file.FileName);
|
||||
|
||||
var ua = new Disco.Models.Repository.UserAttachment()
|
||||
{
|
||||
UserId = u.Id,
|
||||
TechUserId = DiscoApplication.CurrentUser.Id,
|
||||
Filename = file.FileName,
|
||||
MimeType = contentType,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = Comments
|
||||
};
|
||||
dbContext.UserAttachments.Add(ua);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
ua.SaveAttachment(dbContext, file.InputStream);
|
||||
|
||||
ua.GenerateThumbnail(dbContext);
|
||||
|
||||
return Json(ua.Id, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
}
|
||||
throw new Exception("No Attachment Uploaded");
|
||||
}
|
||||
throw new Exception("Invalid User Id");
|
||||
}
|
||||
public virtual ActionResult Attachment(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (ua != null)
|
||||
{
|
||||
|
||||
var m = new Models.Attachment.AttachmentModel()
|
||||
{
|
||||
Attachment = Models.Attachment._AttachmentModel.FromAttachment(ua),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentModel() { Result = "Invalid Attachment Number" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult Attachments(string id)
|
||||
{
|
||||
var u = dbContext.Users.Include("UserAttachments.TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (u != null)
|
||||
{
|
||||
var m = new Models.Attachment.AttachmentsModel()
|
||||
{
|
||||
Attachments = u.UserAttachments.Select(ua => Models.Attachment._AttachmentModel.FromAttachment(ua)).ToList(),
|
||||
Result = "OK"
|
||||
};
|
||||
|
||||
return Json(m, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
return Json(new Models.Attachment.AttachmentsModel() { Result = "Invalid User Id" }, JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
public virtual ActionResult AttachmentRemove(int id)
|
||||
{
|
||||
var ua = dbContext.UserAttachments.Include("TechUser").Where(m => m.Id == id).FirstOrDefault();
|
||||
if (ua != null)
|
||||
{
|
||||
// 2012-02-17 G# Remove - 'Delete Own Comments' policy
|
||||
//if (ua.TechUserId == DiscoApplication.CurrentUser.Id)
|
||||
//{
|
||||
ua.OnDelete(dbContext);
|
||||
dbContext.SaveChanges();
|
||||
return Json("OK", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// return Json("You can only delete your own attachments.", JsonRequestBehavior.AllowGet);
|
||||
//}
|
||||
}
|
||||
return Json("Invalid Attachment Number", JsonRequestBehavior.AllowGet);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public virtual ActionResult GeneratePdf(string id, string DocumentTemplateId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id))
|
||||
throw new ArgumentNullException("id");
|
||||
if (string.IsNullOrEmpty(DocumentTemplateId))
|
||||
throw new ArgumentNullException("AttachmentTypeId");
|
||||
var user = dbContext.Users.Find(id);
|
||||
if (user != null)
|
||||
{
|
||||
var documentTemplate = dbContext.DocumentTemplates.Find(DocumentTemplateId);
|
||||
if (documentTemplate != null)
|
||||
{
|
||||
var timeStamp = DateTime.Now;
|
||||
Stream pdf;
|
||||
using (var generationState = Disco.Models.BI.DocumentTemplates.DocumentState.DefaultState())
|
||||
{
|
||||
pdf = documentTemplate.GeneratePdf(dbContext, user, DiscoApplication.CurrentUser, timeStamp, generationState);
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
return File(pdf, "application/pdf", string.Format("{0}_{1}_{2:yyyyMMdd-HHmmss}.pdf", documentTemplate.Id, user.Id, timeStamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid Document Template Id", "id");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid User Id", "id");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user