using Disco.Data.Repository; using Disco.Models.Services.Jobs; using Disco.Models.UI.Config.JobPreferences; using Disco.Services.Extensions; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; namespace Disco.Web.Areas.Config.Models.JobPreferences { public class IndexModel : ConfigJobPreferencesIndexModel { [DataType(DataType.MultilineText)] public string InitialCommentsTemplate { get; set; } public int LongRunningJobDaysThreshold { get; set; } public int StaleJobMinutesThreshold { get; set; } public bool LodgmentIncludeAllAttachmentsByDefault { get; set; } public string DefaultNoticeboardTheme { get; set; } public LocationModes LocationMode { get; set; } public List LocationList { get; set; } [DataType(DataType.MultilineText)] public string OnCreateExpression { get; set; } [DataType(DataType.MultilineText)] public string OnDeviceReadyForReturnExpression { get; set; } [DataType(DataType.MultilineText)] public string OnCloseExpression { get; set; } public List> DefaultNoticeboardThemeOptions() { return UIHelpers.NoticeboardThemes.ToList(); } public Lazy> DeviceProfiles = new Lazy>(() => { using (var database = new DiscoDataContext()) { return database.DeviceProfiles.OrderBy(a => a.Description).ToList(); } }); public Lazy> OrganisationAddresses = new Lazy>(() => { using (var database = new DiscoDataContext()) { return database.DiscoConfiguration.OrganisationAddresses.Addresses.OrderBy(a => a.Name).ToList(); } }); public List> LongRunningJobDaysThresholdOptions() { var options = new List>() { new KeyValuePair(1, "1 Day"), new KeyValuePair(2, "2 Days"), new KeyValuePair(3, "3 Days"), new KeyValuePair(4, "4 Days"), new KeyValuePair(5, "5 Days"), new KeyValuePair(6, "6 Days"), new KeyValuePair(7, "1 Week"), new KeyValuePair(14, "2 Weeks"), new KeyValuePair(21, "3 Weeks"), new KeyValuePair(28, "4 Weeks"), new KeyValuePair(35, "5 Weeks"), new KeyValuePair(42, "6 Weeks"), new KeyValuePair(49, "7 Weeks"), new KeyValuePair(56, "8 Weeks") }; var current = LongRunningJobDaysThreshold; if (!options.Any(o => o.Key == current)) { options.Add(new KeyValuePair(current, string.Format("{0} Days", current))); options = options.OrderBy(o => o.Key).ToList(); } return options; } public List> StaleJobMinutesThresholdOptions() { var options = new List>() { new KeyValuePair(0, ""), new KeyValuePair(15, "15 minutes"), new KeyValuePair(30, "30 minutes"), new KeyValuePair(60, "1 hour"), new KeyValuePair(60 * 2, "2 hours"), new KeyValuePair(60 * 4, "4 hours"), new KeyValuePair(60 * 8, "8 hours"), new KeyValuePair(60 * 24, "1 day"), new KeyValuePair(60 * 24 * 2, "2 days"), new KeyValuePair(60 * 24 * 3, "3 days"), new KeyValuePair(60 * 24 * 4, "4 days"), new KeyValuePair(60 * 24 * 5, "5 days"), new KeyValuePair(60 * 24 * 6, "6 days"), new KeyValuePair(60 * 24 * 7, "1 week"), new KeyValuePair(60 * 24 * 7 * 2, "2 weeks"), new KeyValuePair(60 * 24 * 7 * 3, "3 weeks"), new KeyValuePair(60 * 24 * 7 * 4, "4 weeks"), new KeyValuePair(60 * 24 * 7 * 5, "5 weeks"), new KeyValuePair(60 * 24 * 7 * 6, "6 weeks"), new KeyValuePair(60 * 24 * 7 * 7, "7 weeks"), new KeyValuePair(60 * 24 * 7 * 8, "8 weeks") }; var current = StaleJobMinutesThreshold; if (!options.Any(o => o.Key == current)) { options.Add(new KeyValuePair(current, string.Format("{0} Minutes", current))); options = options.OrderBy(o => o.Key).ToList(); } return options; } public List> LocationModeOptions() { var type = typeof(LocationModes); var names = Enum.GetNames(type); return names.Select(n => { var at = type.GetMember(n)[0].GetCustomAttributes(typeof(DisplayAttribute), false); if (at != null && at.Length > 0) return new KeyValuePair(n, ((DisplayAttribute)at[0]).Name); else return new KeyValuePair(n, n); }).OrderBy(i => i.Key).ToList(); } } }