Feature #2: Implement Repair Provider

Logging Repair for Non-Warranty jobs has been brought into line with
Logging Warranty. RepairProviderFeature implemented which allows plugins
to be used in submitting jobs to third-parties for repair.
This commit is contained in:
Gary Sharp
2014-07-10 17:45:13 +10:00
parent 5ba9fde10f
commit f4394fe2a0
47 changed files with 4471 additions and 1163 deletions
-154
View File
@@ -1,154 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Disco.Models;
using System.ComponentModel.DataAnnotations;
using Disco.Data.Repository;
namespace Disco.Web.Models.Job
{
[CustomValidation(typeof(CreateModelValidation), "ValidateCreateModel")]
public class CreateModelOld
{
private Disco.Models.Repository.Device _Device;
private Disco.Models.Repository.User _User;
public Disco.Models.Repository.Device Device
{
get
{
return _Device;
}
set
{
_Device = value;
DeviceSerialNumber = value.SerialNumber;
}
}
public Disco.Models.Repository.User User
{
get
{
return _User;
}
set
{
_User = value;
UserId = value.Id;
}
}
public string DeviceSerialNumber { get; set; }
public string UserId { get; set; }
[Required]
public string Type { get; set; }
[Required]
public List<string> SubTypes { get; set; }
public List<Disco.Models.Repository.JobType> JobTypes { get; set; }
public List<Disco.Models.Repository.JobSubType> JobSubTypes { get; set; }
public Disco.Models.Repository.JobType GetJobType
{
get
{
if (!string.IsNullOrEmpty(this.Type))
{
return this.JobTypes.FirstOrDefault(m => m.Id == this.Type);
}
return null;
}
}
public List<Disco.Models.Repository.JobSubType> GetJobSubTypes
{
get
{
if (SubTypes != null)
{
var subTypes = this.SubTypes;
return this.JobSubTypes.Where(m => subTypes.Contains(String.Format("{0}_{1}", m.JobTypeId, m.Id))).ToList();
}
return null;
}
}
public void UpdateModel(DiscoDataContext dbContext)
{
if (this.JobTypes == null)
JobTypes = dbContext.JobTypes.ToList();
if (this.JobSubTypes == null)
JobSubTypes = dbContext.JobSubTypes.ToList();
if (!string.IsNullOrEmpty(DeviceSerialNumber))
{
this.Device = dbContext.Devices.Include("DeviceModel").Where(d => d.SerialNumber == DeviceSerialNumber).FirstOrDefault();
if (this.Device == null)
{
throw new ArgumentException("Invalid Device Serial Number Specified", "DeviceSerialNumber");
}
if (string.IsNullOrEmpty(this.UserId) && !string.IsNullOrEmpty(this.Device.AssignedUserId))
{
this.UserId = this.Device.AssignedUserId;
}
if (string.IsNullOrEmpty(this.Type))
this.Type = this.JobTypes.First(jt => jt.Id == "HWar").Id;
}
else
{
// No Device - Remove Hardware Types
foreach (var jobType in JobTypes.ToArray())
{
if (jobType.Id != Disco.Models.Repository.JobType.JobTypeIds.SApp)
{
JobTypes.Remove(jobType);
JobSubTypes.RemoveAll(jst => jst.JobType == jobType);
}
}
}
if (!string.IsNullOrEmpty(UserId))
{
this.User = dbContext.Users.Find(UserId);
if (this.User == null)
{
throw new ArgumentException("Invalid User Id Specified", "UserId");
}
if (string.IsNullOrEmpty(this.Type))
this.Type = Disco.Models.Repository.JobType.JobTypeIds.SApp;
}
if (this.User == null && this.Device == null)
{
throw new InvalidOperationException("A Job must reference a Device and/or a User");
}
}
}
public class CreateModelValidation
{
public static ValidationResult ValidateCreateModel(CreateModelOld model)
{
// Device && User both can't be null
if (string.IsNullOrEmpty(model.DeviceSerialNumber) && string.IsNullOrEmpty(model.UserId))
return new ValidationResult("A Job must reference a Device and/or a User");
if (!string.IsNullOrEmpty(model.Type) && model.SubTypes != null)
{
var typeId = string.Format("{0}_", model.Type);
model.SubTypes = model.SubTypes.Where(m => m.StartsWith(typeId)).ToList();
if (model.SubTypes.Count == 0)
{
model.SubTypes = null;
return new ValidationResult("At least one Sub Type is required", new string[] { "SubTypes" });
}
}
return ValidationResult.Success;
}
}
}
+121
View File
@@ -0,0 +1,121 @@
using Disco.Data.Repository;
using Disco.Services.Plugins;
using Disco.Services.Plugins.Features.RepairProvider;
using Disco.Services.Users;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Disco.Web.Models.Job
{
public class LogRepairModel
{
public Disco.Models.Repository.Job Job { get; set; }
public List<PluginFeatureManifest> RepairProviders { get; set; }
public PluginFeatureManifest RepairProvider { get; set; }
public List<Disco.Models.BI.Config.OrganisationAddress> OrganisationAddresses { get; set; }
public Disco.Models.BI.Config.OrganisationAddress OrganisationAddress { get; set; }
public Disco.Models.Repository.User TechUser { get; set; }
[Required]
public int JobId { get; set; }
[Required(ErrorMessage = "Please specify a Repair Address")]
public Nullable<int> OrganisationAddressId { get; set; }
[Required(ErrorMessage = "Please specify a Repair Provider")]
public string RepairProviderId { get; set; }
[Required(ErrorMessage = "A fault description is required"), DataType(System.ComponentModel.DataAnnotations.DataType.MultilineText)]
public string RepairDescription { get; set; }
[Required]
public string SubmissionAction { get; set; }
public bool IsManualProvider
{
get
{
return RepairProviderId == "MANUAL";
}
}
public string ManualProviderName { get; set; }
public string ManualProviderReference { get; set; }
public Tuple<Type, object> RepairProviderSubmitJobBeginResult { get; set; }
public string ProviderPropertiesJson { get; set; }
public Dictionary<string, string> ProviderProperties()
{
Dictionary<string, string> p = default(Dictionary<string, string>);
if (!string.IsNullOrEmpty(this.ProviderPropertiesJson))
{
try
{
p = JsonConvert.DeserializeObject<Dictionary<string, string>>(this.ProviderPropertiesJson);
}
catch (Exception)
{
// Ignore Errors
}
}
return p;
}
public Dictionary<string, string> DiscloseProperties { get; set; }
public Exception Error { get; set; }
public void UpdateModel(DiscoDataContext Database, bool IsPostBack)
{
Database.Configuration.LazyLoadingEnabled = true;
if (Job == null)
{
// Update Job User's Details [#12]
string jobUserId = Database.Jobs.Where(j => j.Id == JobId).Select(j => j.UserId).FirstOrDefault();
if (jobUserId != null)
{
// Ignore update errors (Most commonly when the User Id no longer exists in AD)
try
{
UserService.GetUser(jobUserId, Database, true);
} catch (Exception) {}
}
Job = (from j in Database.Jobs.Include("Device.DeviceModel").Include("JobMetaNonWarranty").Include("JobSubTypes")
where (j.Id == JobId)
select j).FirstOrDefault();
if (Job == null)
{
throw new ArgumentException("Invalid Job Number Specified", "JobId");
}
}
// Update TechUser's Details [#12]
this.TechUser = UserService.GetUser(UserService.CurrentUserId, Database, true);
RepairProviders = Plugins.GetPluginFeatures(typeof(RepairProviderFeature));
if (!IsPostBack && string.IsNullOrEmpty(RepairProviderId))
{
RepairProviderId = Job.Device.DeviceModel.DefaultRepairProvider;
if (string.IsNullOrEmpty(RepairProviderId))
RepairProviderId = "MANUAL";
}
if (!string.IsNullOrEmpty(RepairProviderId) && RepairProviderId != "MANUAL")
RepairProvider = Plugins.GetPluginFeature(RepairProviderId, typeof(RepairProviderFeature));
this.OrganisationAddresses = Database.DiscoConfiguration.OrganisationAddresses.Addresses.OrderBy(a => a.Name).ToList();
if (!IsPostBack && !this.OrganisationAddressId.HasValue)
{
OrganisationAddressId = Job.Device.DeviceProfile.DefaultOrganisationAddress;
}
if (this.OrganisationAddressId.HasValue)
this.OrganisationAddress = this.OrganisationAddresses.FirstOrDefault(oa => oa.Id == this.OrganisationAddressId.Value);
if (!string.IsNullOrEmpty(RepairDescription))
RepairDescription = RepairDescription.Trim();
}
}
}
+19 -19
View File
@@ -1,15 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Disco.Data.Repository;
using System.ComponentModel.DataAnnotations;
using Disco.BI;
using System.Web.Script.Serialization;
using Disco.Data.Repository;
using Disco.Services.Plugins;
using Disco.Services.Plugins.Features.WarrantyProvider;
using Newtonsoft.Json;
using Disco.Services.Users;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Disco.Web.Models.Job
{
@@ -32,29 +29,29 @@ namespace Disco.Web.Models.Job
[Required(ErrorMessage = "A fault description is required"), DataType(System.ComponentModel.DataAnnotations.DataType.MultilineText)]
public string FaultDescription { get; set; }
[Required]
public string WarrantyAction { get; set; }
public string SubmissionAction { get; set; }
public bool IsCustomProvider
public bool IsManualProvider
{
get
{
return WarrantyProviderId == "CUSTOM";
return WarrantyProviderId == "MANUAL";
}
}
public string CustomProviderName { get; set; }
public string CustomProviderReference { get; set; }
public string ManualProviderName { get; set; }
public string ManualProviderReference { get; set; }
public Type WarrantyProviderSubmitJobViewType { get; set; }
public object WarrantyProviderSubmitJobModel { get; set; }
public string WarrantyProviderPropertiesJson { get; set; }
public Dictionary<string, string> WarrantyProviderProperties()
public string ProviderPropertiesJson { get; set; }
public Dictionary<string, string> ProviderProperties()
{
Dictionary<string, string> p = default(Dictionary<string, string>);
if (!string.IsNullOrEmpty(this.WarrantyProviderPropertiesJson))
if (!string.IsNullOrEmpty(this.ProviderPropertiesJson))
{
try
{
p = JsonConvert.DeserializeObject<Dictionary<string, string>>(this.WarrantyProviderPropertiesJson);
p = JsonConvert.DeserializeObject<Dictionary<string, string>>(this.ProviderPropertiesJson);
}
catch (Exception)
{
@@ -101,9 +98,12 @@ namespace Disco.Web.Models.Job
if (!IsPostBack && string.IsNullOrEmpty(WarrantyProviderId))
{
WarrantyProviderId = Job.Device.DeviceModel.DefaultWarrantyProvider;
if (string.IsNullOrEmpty(WarrantyProviderId))
WarrantyProviderId = "MANUAL";
}
if (!string.IsNullOrEmpty(WarrantyProviderId) && WarrantyProviderId != "CUSTOM")
if (!string.IsNullOrEmpty(WarrantyProviderId) && WarrantyProviderId != "MANUAL")
WarrantyProvider = Plugins.GetPluginFeature(WarrantyProviderId, typeof(WarrantyProviderFeature));
this.OrganisationAddresses = Database.DiscoConfiguration.OrganisationAddresses.Addresses.OrderBy(a => a.Name).ToList();
@@ -5,7 +5,7 @@ using System.Web;
namespace Disco.Web.Models.Job
{
public class WarrantyProviderJobDetailsModel
public class ProviderJobDetailsModel
{
public Type ViewType { get; set; }
public object ViewModel { get; set; }