Update: Configuration Framework +CallerMemberName

Simplified creation of configuration modules; Scope is obtained from
abstract, Property names become keys (via CallerMemberName); Simple
generic Get/Set methods are used; Helpers for Obsfucation and Json are
available.
This commit is contained in:
Gary Sharp
2013-05-03 20:48:15 +10:00
parent d19ad7d0dc
commit 8533ec2fe0
23 changed files with 545 additions and 684 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Disco.BI
{
return CreateLocation(dbContext.DiscoConfiguration, SubLocation, SubSubLocationTimestamp);
}
public static string CreateLocation(ConfigurationContext DiscoConfiguration, string SubLocation, DateTime? SubSubLocationTimestamp = null)
public static string CreateLocation(SystemConfiguration DiscoConfiguration, string SubLocation, DateTime? SubSubLocationTimestamp = null)
{
string SubSubLocation = string.Empty;
if (SubSubLocationTimestamp.HasValue)
@@ -21,7 +21,7 @@ namespace Disco.BI.Expressions.Extensions
}
public static FileImageExpressionResult ImageFromDataStoreFile(string RelativeFilePath)
{
var configCache = new Disco.Data.Configuration.ConfigurationContext(null);
var configCache = new Disco.Data.Configuration.SystemConfiguration(null);
string DataStoreLocation = configCache.DataStoreLocation;
string AbsoluteFilePath = System.IO.Path.Combine(DataStoreLocation, RelativeFilePath);
return new FileImageExpressionResult(AbsoluteFilePath);
@@ -126,7 +126,7 @@ namespace Disco.BI.Expressions.Extensions
}
public static BitmapImageExpressionResult OrganisationLogo()
{
var configCache = new Disco.Data.Configuration.ConfigurationContext(null);
var configCache = new Disco.Data.Configuration.SystemConfiguration(null);
BitmapImageExpressionResult result;
using (var orgLogo = configCache.OrganisationLogo)
{
@@ -55,7 +55,7 @@ namespace Disco.BI.Extensions
public static string ImageFilePath(this DeviceModel deviceModel)
{
var configCache = new Disco.Data.Configuration.ConfigurationContext(null);
var configCache = new Disco.Data.Configuration.SystemConfiguration(null);
var deviceModelImagesDataStore = DataStore.CreateLocation(configCache, "DeviceModelImages");
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0430.1219")]
[assembly: AssemblyFileVersion("1.2.0430.1219")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0429.1616")]
[assembly: AssemblyFileVersion("1.2.0429.1616")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0426.1841")]
[assembly: AssemblyFileVersion("1.2.0426.1841")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]
+65 -11
View File
@@ -1,35 +1,89 @@
using System;
using Disco.Data.Repository;
using Disco.Models.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace Disco.Data.Configuration
{
public abstract class ConfigurationBase
{
private ConfigurationContext _context;
private DiscoDataContext dbContext;
public ConfigurationContext Context
public abstract string Scope { get; }
public ConfigurationBase(DiscoDataContext dbContext)
{
this.dbContext = dbContext;
}
protected List<ConfigurationItem> Items
{
get
{
return _context;
return ConfigurationCache.GetConfigurationItems(dbContext, this.Scope);
}
}
public abstract string Scope { get; }
public ConfigurationBase(ConfigurationContext Context)
private void SetValue<ValueType>(string Key, ValueType Value)
{
this._context = Context;
ConfigurationCache.SetConfigurationValue(dbContext, this.Scope, Key, Value);
}
private ValueType GetValue<ValueType>(string Key, ValueType Default)
{
return ConfigurationCache.GetConfigurationValue(dbContext, this.Scope, Key, Default);
}
protected void SetValue<ValueType>(string Key, ValueType Value)
protected void Set<ValueType>(ValueType Value, [CallerMemberName] string Key = null)
{
this.Context.SetConfigurationValue(this.Scope, Key, Value);
if (Key == null)
throw new ArgumentNullException("Key");
this.SetValue(Key, Value);
}
protected ValueType GetValue<ValueType>(string Key, ValueType Default)
protected ValueType Get<ValueType>(ValueType Default, [CallerMemberName] string Key = null)
{
return this.Context.GetConfigurationValue(this.Scope, Key, Default);
if (Key == null)
throw new ArgumentNullException("Key");
return this.GetValue(Key, Default);
}
protected void SetObsfucated(string Value, [CallerMemberName] string Key = null)
{
this.Set(ConfigurationCache.ObsfucateValue(Value), Key);
}
protected string GetDeobsfucated(string Default, [CallerMemberName] string Key = null)
{
var obsfucatedValue = this.Get<string>(null, Key);
if (obsfucatedValue == null)
return Default;
else
return ConfigurationCache.DeobsfucateValue(obsfucatedValue);
}
protected void SetAsJson<ValueType>(ValueType Value, [CallerMemberName] string Key = null)
{
if (Value == null)
this.Set<string>(null, Key);
else
this.Set(JsonConvert.SerializeObject(Value), Key);
}
protected ValueType GetFromJson<ValueType>(ValueType Default, [CallerMemberName] string Key = null)
{
var jsonValue = this.Get<string>(null, Key);
if (jsonValue == null)
return Default;
else
return JsonConvert.DeserializeObject<ValueType>(jsonValue);
}
}
@@ -0,0 +1,150 @@
using Disco.Data.Repository;
using Disco.Models.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Data.Configuration
{
internal static class ConfigurationCache
{
#region Cache
private static Dictionary<String, Dictionary<String, ConfigurationItem>> configDictionary = new Dictionary<string, Dictionary<string, ConfigurationItem>>();
private static List<ConfigurationItem> configurationItems = new List<ConfigurationItem>();
private static object configurationItemsLock = new object();
private static void LoadConfigurationItems(DiscoDataContext dbContext, string Scope, bool Reload)
{
if (Reload || !configDictionary.ContainsKey(Scope))
{
lock (configurationItemsLock)
{
if (Reload || !configDictionary.ContainsKey(Scope))
{
if (dbContext == null)
throw new InvalidOperationException("Cache-miss where Configuration Item requested from Cache-Only Configuration Context");
var newItems = dbContext.ConfigurationItems.Where(ci => ci.Scope == Scope).ToArray();
if (configDictionary.ContainsKey(Scope))
{
var existingItems = configDictionary[Scope];
foreach (var existingItem in existingItems.Values)
{
configurationItems.Remove(existingItem);
}
}
configurationItems.AddRange(newItems);
configDictionary[Scope] = newItems.ToDictionary(ci => ci.Key);
}
}
}
}
private static Dictionary<string, Dictionary<string, ConfigurationItem>> ConfigurationDictionary(DiscoDataContext dbContext, string IncludingScope)
{
LoadConfigurationItems(dbContext, IncludingScope, false);
return configDictionary;
}
private static ConfigurationItem ConfigurationItem(DiscoDataContext dbContext, string Scope, string Key)
{
Dictionary<string, ConfigurationItem> scopeDict = default(Dictionary<string, ConfigurationItem>);
if (ConfigurationDictionary(dbContext, Scope).TryGetValue(Scope, out scopeDict))
{
ConfigurationItem item = default(ConfigurationItem);
if (scopeDict.TryGetValue(Key, out item))
return item;
}
return null;
}
private static List<ConfigurationItem> ConfigurationItems(DiscoDataContext dbContext, string IncludingScope)
{
LoadConfigurationItems(dbContext, IncludingScope, false);
return configurationItems;
}
#endregion
#region Public Helpers
internal static ValueType GetConfigurationValue<ValueType>(DiscoDataContext dbContext, string Scope, string Key, ValueType Default)
{
var ci = ConfigurationItem(dbContext, Scope, Key);
if (ci == null)
return Default;
else
return (ValueType)Convert.ChangeType(ci.Value, typeof(ValueType));
}
internal static void SetConfigurationValue<ValueType>(DiscoDataContext dbContext, string Scope, string Key, ValueType Value)
{
if (dbContext == null)
throw new InvalidOperationException("Cannot save changes with a CacheOnly Context");
var ci = ConfigurationItem(dbContext, Scope, Key);
if (ci == null && Value != null)
{
lock (configurationItemsLock)
{
ci = ConfigurationItem(dbContext, Scope, Key);
if (ci == null)
{
// Create Configuration Item
ci = new ConfigurationItem() { Scope = Scope, Key = Key, Value = Value.ToString() };
// Add Item to DB & Internal Collections
dbContext.ConfigurationItems.Add(ci);
ConfigurationItems(dbContext, Scope).Add(ci);
ConfigurationDictionary(dbContext, Scope)[Scope].Add(Key, ci);
ci = null;
}
}
}
if (ci != null)
{
lock (configurationItemsLock)
{
var entityInfo = dbContext.Entry(ci);
if (entityInfo.State == System.Data.EntityState.Detached)
{
// Reload Scope from DB
LoadConfigurationItems(dbContext, Scope, true);
ci = ConfigurationItem(dbContext, Scope, Key);
}
if (Value == null)
{
dbContext.ConfigurationItems.Remove(ci);
configurationItems.Remove(ci);
configDictionary[Scope].Remove(Key);
}
else
{
ci.Value = Value.ToString();
}
}
}
}
internal static List<ConfigurationItem> GetConfigurationItems(DiscoDataContext dbContext, string Scope)
{
return ConfigurationDictionary(dbContext, Scope)[Scope].Values.ToList();
}
internal static string ObsfucateValue(string Value)
{
if (string.IsNullOrEmpty(Value))
return Value;
else
return Convert.ToBase64String(Encoding.Unicode.GetBytes(Value));
}
internal static string DeobsfucateValue(string ObsfucatedValue)
{
if (string.IsNullOrEmpty(ObsfucatedValue))
return ObsfucatedValue;
else
return Encoding.Unicode.GetString(Convert.FromBase64String(ObsfucatedValue));
}
#endregion
}
}
@@ -1,483 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Data.Repository;
using Disco.Models.Repository;
using System.IO;
using System.Security.Cryptography;
using Disco.Models.BI.Interop.Community;
using Newtonsoft.Json;
namespace Disco.Data.Configuration
{
public class ConfigurationContext
{
private DiscoDataContext _dbContext;
private DiscoDataContext dbContext
{
get
{
if (_dbContext != null)
return _dbContext;
else
throw new InvalidOperationException("Cache-miss where Configuration Item requested from Cache-Only Configuration Context");
}
}
public bool CacheOnly
{
get
{
return _dbContext == null;
}
}
public ConfigurationContext(DiscoDataContext dbContext)
{
this._dbContext = dbContext;
// Init Modules
this.moduleBootstrapperConfiguration = new Lazy<Modules.BootstrapperConfiguration>(() => new Modules.BootstrapperConfiguration(this));
this.moduleDeviceProfilesConfiguration = new Lazy<Modules.DeviceProfilesConfiguration>(() => new Modules.DeviceProfilesConfiguration(this));
this.moduleOrganisationAddressesConfiguration = new Lazy<Modules.OrganisationAddressesConfiguration>(() => new Modules.OrganisationAddressesConfiguration(this));
this.moduleWirelessConfiguration = new Lazy<Modules.WirelessConfiguration>(() => new Modules.WirelessConfiguration(this));
}
#region Item Cache
private static Dictionary<String, Dictionary<String, ConfigurationItem>> configDictionary = new Dictionary<string, Dictionary<string, ConfigurationItem>>();
private static List<ConfigurationItem> configurationItems = new List<ConfigurationItem>();
private static object configurationItemsLock = new object();
private void loadConfigurationItems(string Scope, bool Reload)
{
if (Reload || !configDictionary.ContainsKey(Scope))
{
lock (configurationItemsLock)
{
if (Reload || !configDictionary.ContainsKey(Scope))
{
var newItems = this.dbContext.ConfigurationItems.Where(ci => ci.Scope == Scope).ToArray();
if (configDictionary.ContainsKey(Scope))
{
var existingItems = configDictionary[Scope];
foreach (var existingItem in existingItems.Values)
{
configurationItems.Remove(existingItem);
}
}
configurationItems.AddRange(newItems);
configDictionary[Scope] = newItems.ToDictionary(ci => ci.Key);
}
}
}
}
public Dictionary<string, Dictionary<string, ConfigurationItem>> ConfigurationDictionary(string IncludingScope)
{
this.loadConfigurationItems(IncludingScope, false);
return configDictionary;
}
public ConfigurationItem ConfigurationItem(string Scope, string Key)
{
Dictionary<string, ConfigurationItem> scopeDict = default(Dictionary<string, ConfigurationItem>);
if (this.ConfigurationDictionary(Scope).TryGetValue(Scope, out scopeDict))
{
ConfigurationItem item = default(ConfigurationItem);
if (scopeDict.TryGetValue(Key, out item))
return item;
}
return null;
}
private List<ConfigurationItem> ConfigurationItems(string IncludingScope)
{
this.loadConfigurationItems(IncludingScope, false);
return configurationItems;
}
#endregion
#region Helpers
public ValueType GetConfigurationValue<ValueType>(string Scope, string Key, ValueType Default)
{
var ci = this.ConfigurationItem(Scope, Key);
if (ci == null)
return Default;
else
return (ValueType)Convert.ChangeType(ci.Value, typeof(ValueType));
}
public void SetConfigurationValue<ValueType>(string Scope, string Key, ValueType Value)
{
if (CacheOnly)
throw new InvalidOperationException("Cannot save changes with a CacheOnly Context");
var ci = this.ConfigurationItem(Scope, Key);
if (ci == null && Value != null)
{
lock (configurationItemsLock)
{
ci = this.ConfigurationItem(Scope, Key);
if (ci == null)
{
// Create Configuration Item
ci = new ConfigurationItem() { Scope = Scope, Key = Key, Value = Value.ToString() };
// Add Item to DB & Internal Collections
this.dbContext.ConfigurationItems.Add(ci);
this.ConfigurationItems(Scope).Add(ci);
this.ConfigurationDictionary(Scope)[Scope].Add(Key, ci);
ci = null;
}
}
}
if (ci != null)
{
lock (configurationItemsLock)
{
var entityInfo = dbContext.Entry(ci);
if (entityInfo.State == System.Data.EntityState.Detached)
{
// Reload Scope from DB
this.loadConfigurationItems(Scope, true);
ci = this.ConfigurationItem(Scope, Key);
}
if (Value == null)
{
dbContext.ConfigurationItems.Remove(ci);
configurationItems.Remove(ci);
configDictionary[Scope].Remove(Key);
}
else
{
ci.Value = Value.ToString();
}
}
}
}
public static string ObsfucateValue(string Value)
{
if (string.IsNullOrEmpty(Value))
return Value;
else
return Convert.ToBase64String(Encoding.Unicode.GetBytes(Value));
}
public static string DeobsfucateValue(string ObsfucatedValue)
{
if (string.IsNullOrEmpty(ObsfucatedValue))
return ObsfucatedValue;
else
return Encoding.Unicode.GetString(Convert.FromBase64String(ObsfucatedValue));
}
#endregion
#region Configuration Modules
private Lazy<Modules.BootstrapperConfiguration> moduleBootstrapperConfiguration;
private Lazy<Modules.DeviceProfilesConfiguration> moduleDeviceProfilesConfiguration;
private Lazy<Modules.OrganisationAddressesConfiguration> moduleOrganisationAddressesConfiguration;
private Lazy<Modules.WirelessConfiguration> moduleWirelessConfiguration;
public Modules.BootstrapperConfiguration Bootstrapper
{
get
{
return moduleBootstrapperConfiguration.Value;
}
}
public Modules.DeviceProfilesConfiguration DeviceProfiles
{
get
{
return moduleDeviceProfilesConfiguration.Value;
}
}
public Modules.OrganisationAddressesConfiguration OrganisationAddresses
{
get
{
return moduleOrganisationAddressesConfiguration.Value;
}
}
public Modules.WirelessConfiguration Wireless
{
get
{
return moduleWirelessConfiguration.Value;
}
}
#endregion
#region System Configuration Items
public string Scope { get { return "System"; } }
public string DataStoreLocation
{
get
{
var result = this.GetConfigurationValue<string>(this.Scope, "DataStoreLocation", null);
if (result == null)
{
var appDataPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data");
if (appDataPath.EndsWith("\\"))
return appDataPath;
else
return string.Concat(appDataPath, '\\');
}
else
return result;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
if (!System.IO.Directory.Exists(value))
throw new System.IO.DirectoryNotFoundException(string.Format("DataStoreLocation: '{0}' could not be found", value));
string storePath;
if (value.EndsWith("\\"))
storePath = value;
else
storePath = string.Concat(value, '\\');
this.SetConfigurationValue(this.Scope, "DataStoreLocation", storePath);
}
}
public string PluginsLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"Plugins\");
}
}
public string PluginStorageLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"PluginStorage\");
}
}
public string PluginPackagesLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"PluginPackages\");
}
}
#region Organisation Logo
private string OrganisationLogoPath
{
get
{
return System.IO.Path.Combine(DataStoreLocation, "OrganisationLogo.png");
}
}
//private static string _OrganisationLogoHash;
//private static byte[] _OrganisationLogo;
//private static object _OrganisationLogoLock = new object();
//private static void LoadOrganisationLogo(ConfigurationContext context, bool reload = false)
//{
// if (_OrganisationLogoHash == null || reload)
// {
// lock (_OrganisationLogoLock)
// {
// if (_OrganisationLogoHash == null || reload)
// {
// _OrganisationLogo = null;
// _OrganisationLogoHash = null;
// string organisationLogoPath = context.OrganisationLogoPath;
// if (System.IO.File.Exists(organisationLogoPath))
// _OrganisationLogo = System.IO.File.ReadAllBytes(organisationLogoPath);
// }
// if (_OrganisationLogo == null || _OrganisationLogo.Length == 0)
// {
// _OrganisationLogo = Disco.Data.Properties.Resources.EmptyLogo;
// }
// if (_OrganisationLogoHash == null)
// {
// using (SHA256 h = SHA256.Create())
// {
// _OrganisationLogoHash = Convert.ToBase64String(h.ComputeHash(_OrganisationLogo));
// }
// }
// }
// }
//}
public string OrganisationLogoHash
{
get
{
var path = this.OrganisationLogoPath;
if (File.Exists(path))
return File.GetLastWriteTimeUtc(path).ToBinary().ToString();
else
return "-1";
}
}
public Stream OrganisationLogo
{
get
{
//LoadOrganisationLogo(this);
//if (_OrganisationLogo == null || _OrganisationLogo.Length != 0)
// return new MemoryStream(_OrganisationLogo);
//else
// return null;
var path = this.OrganisationLogoPath;
if (File.Exists(path))
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
else
return new MemoryStream(Disco.Data.Properties.Resources.EmptyLogo);
}
set
{
string organisationLogoPath = this.OrganisationLogoPath;
if (value == null)
{
if (System.IO.File.Exists(organisationLogoPath))
System.IO.File.Delete(organisationLogoPath);
}
else
{
using (FileStream fs = new FileStream(organisationLogoPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
value.CopyTo(fs);
}
}
//LoadOrganisationLogo(this, true);
}
}
#endregion
public string OrganisationName
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "OrganisationName", null);
}
set
{
this.SetConfigurationValue(this.Scope, "OrganisationName", value);
}
}
public bool MultiSiteMode
{
get
{
return this.GetConfigurationValue<bool>(this.Scope, "MultiSiteMode", false);
}
set
{
this.SetConfigurationValue(this.Scope, "MultiSiteMode", value);
}
}
#region Proxy Configuration
public string ProxyAddress
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "ProxyAddress", null);
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyAddress", value);
}
}
public int ProxyPort
{
get
{
return this.GetConfigurationValue(this.Scope, "ProxyPort", 8080);
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyPort", value);
}
}
public string ProxyUsername
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "ProxyUsername", null);
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyUsername", value);
}
}
public string ProxyPassword
{
get
{
return DeobsfucateValue(this.GetConfigurationValue<string>(this.Scope, "ProxyPassword", null));
}
set
{
this.SetConfigurationValue(this.Scope, "ProxyPassword", ObsfucateValue(value));
}
}
#endregion
#region UpdateCheck
public string DeploymentId
{
get
{
return this.GetConfigurationValue<string>(this.Scope, "DeploymentId", null);
}
}
public UpdateResponse UpdateLastCheck
{
get
{
var json = this.GetConfigurationValue<string>(this.Scope, "UpdateLastCheck", null);
if (json != null)
{
try
{
return JsonConvert.DeserializeObject<UpdateResponse>(json);
}
catch (Exception)
{ }// Ignore Serialization Issues
}
return null;
}
set
{
if (value == null)
this.SetConfigurationValue<string>(this.Scope, "UpdateLastCheck", null);
var json = JsonConvert.SerializeObject(value);
this.SetConfigurationValue<string>(this.Scope, "UpdateLastCheck", json);
}
}
public bool UpdateBetaDeployment
{
get
{
return this.GetConfigurationValue<bool>(this.Scope, "UpdateBetaDeployment", false);
}
}
public Version InstalledDatabaseVersion
{
get
{
var versionString = this.GetConfigurationValue<string>(this.Scope, "InstalledDatabaseVersion", null);
if (string.IsNullOrEmpty(versionString))
return null;
else
return Version.Parse(versionString);
}
set
{
this.SetConfigurationValue<string>(this.Scope, "InstalledDatabaseVersion", value.ToString(4));
}
}
#endregion
#endregion
}
}
@@ -1,4 +1,5 @@
using System;
using Disco.Data.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -7,7 +8,7 @@ namespace Disco.Data.Configuration.Modules
{
public class BootstrapperConfiguration : ConfigurationBase
{
public BootstrapperConfiguration(ConfigurationContext Context) : base(Context) { }
public BootstrapperConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
public override string Scope
{
@@ -18,11 +19,11 @@ namespace Disco.Data.Configuration.Modules
{
get
{
return this.GetValue("MacSshUsername", "root");
return this.Get("root");
}
set
{
this.SetValue("MacSshUsername", value);
this.Set(value);
}
}
@@ -30,11 +31,11 @@ namespace Disco.Data.Configuration.Modules
{
get
{
return ConfigurationContext.DeobsfucateValue(this.GetValue("MacSshPassword", string.Empty));
return this.GetDeobsfucated(string.Empty);
}
set
{
this.SetValue("MacSshPassword", ConfigurationContext.ObsfucateValue(value));
this.SetObsfucated(value);
}
}
}
@@ -3,43 +3,21 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Models.Repository;
using Disco.Data.Repository;
namespace Disco.Data.Configuration.Modules
{
public class DeviceProfilesConfiguration : ConfigurationBase
{
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//private Dictionary<int, DeviceProfileConfiguration> deviceProfileConfigurations;
public DeviceProfilesConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
public DeviceProfilesConfiguration(ConfigurationContext Context)
: base(Context)
{
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//this.deviceProfileConfigurations = new Dictionary<int, DeviceProfileConfiguration>();
}
public override string Scope
{
get { return "DeviceProfiles"; }
}
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//public DeviceProfileConfiguration DeviceProfile(DeviceProfile Profile)
//{
// DeviceProfileConfiguration dpc = default(DeviceProfileConfiguration);
// if (!this.deviceProfileConfigurations.TryGetValue(Profile.Id, out dpc))
// {
// dpc = new DeviceProfileConfiguration(this.Context, Profile);
// this.deviceProfileConfigurations[Profile.Id] = dpc;
// }
// return dpc;
//}
public override string Scope { get { return "DeviceProfiles"; } }
public int DefaultDeviceProfileId
{
get
{
var v = this.GetValue("DefaultDeviceProfileId", 1);
var v = this.Get(1);
if (v > 0)
return v;
else
@@ -49,20 +27,22 @@ namespace Disco.Data.Configuration.Modules
{
if (value < 1)
throw new ArgumentOutOfRangeException("value", "Expected >= 1");
this.SetValue("DefaultDeviceProfileId", value);
this.Set(value);
}
}
public int DefaultAddDeviceOfflineDeviceProfileId
{
get
{
return this.GetValue("DefaultAddDeviceOfflineDeviceProfileId", 0);
return this.Get(0);
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value", "Expected >= 0");
this.SetValue("DefaultAddDeviceOfflineDeviceProfileId", value);
this.Set(value);
}
}
@@ -5,22 +5,20 @@ using System.Text;
using Disco.Models.BI.Config;
using Disco.Models.Repository;
using Newtonsoft.Json;
using Disco.Data.Repository;
namespace Disco.Data.Configuration.Modules
{
public class OrganisationAddressesConfiguration : ConfigurationBase
{
public OrganisationAddressesConfiguration(ConfigurationContext Context) : base(Context) { }
public OrganisationAddressesConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
public override string Scope
{
get { return "OrganisationAddresses"; }
}
public override string Scope { get { return "OrganisationAddresses"; } }
public OrganisationAddress GetAddress(int Id)
{
var address = default(OrganisationAddress);
var addressString = this.GetValue<string>(Id.ToString(), null);
var addressString = this.Get<string>(null, Id.ToString());
if (addressString != null)
{
if (addressString.StartsWith("{"))
@@ -45,28 +43,23 @@ namespace Disco.Data.Configuration.Modules
string addressString = JsonConvert.SerializeObject(Address);
this.SetValue(Address.Id.ToString(), addressString); //Address.ToConfigurationEntry());
this.Set(addressString, Address.Id.ToString()); //Address.ToConfigurationEntry());
return Address;
}
public void RemoveAddress(int Id)
{
// Set Config Item to null = Remove Configuration Item
this.SetValue<string>(Id.ToString(), null);
this.Set<string>(null, Id.ToString());
}
public List<OrganisationAddress> Addresses
{
get
{
Dictionary<string, ConfigurationItem> configAddress = default(Dictionary<string, ConfigurationItem>);
if (this.Context.ConfigurationDictionary(this.Scope).TryGetValue(this.Scope, out configAddress))
return configAddress.Select(
ca => ca.Value.Value.StartsWith("{") ?
JsonConvert.DeserializeObject<OrganisationAddress>(ca.Value.Value) :
OrganisationAddress.FromConfigurationEntry(int.Parse(ca.Key), ca.Value.Value)
).ToList();
else
return new List<OrganisationAddress>(); // Empty List - No Addresses
return this.Items.Select(ca => ca.Value.StartsWith("{") ?
JsonConvert.DeserializeObject<OrganisationAddress>(ca.Value) :
OrganisationAddress.FromConfigurationEntry(int.Parse(ca.Key), ca.Value)
).ToList();
}
}
@@ -77,7 +70,7 @@ namespace Disco.Data.Configuration.Modules
int nextId = 0;
while (true)
{
if (this.Context.ConfigurationItem(this.Scope, nextId.ToString()) == null)
if (this.Get<string>(null, nextId.ToString()) == null)
break;
nextId++;
}
@@ -1,109 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Disco.Data.Configuration.Modules
{
public class WirelessConfiguration : ConfigurationBase
{
public const string Provider_eduSTAR = "eduSTAR";
public const string Provider_eduPaSS = "eduPaSS";
public WirelessConfiguration(ConfigurationContext Context) : base(Context) { }
public override string Scope
{
get { return "Wireless"; }
}
public int CertificateAutoBufferMax
{
get
{
return this.GetValue("CertificateAutoBufferMax", 50);
}
set
{
this.SetValue("CertificateAutoBufferMax", value);
}
}
public int CertificateAutoBufferLow
{
get
{
return this.GetValue("CertificateAutoBufferLow", 10);
}
set
{
this.SetValue("CertificateAutoBufferLow", value);
}
}
public string Provider
{
get
{
return this.GetValue("Provider", Provider_eduSTAR);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
if (value.Equals(Provider_eduSTAR, StringComparison.InvariantCultureIgnoreCase))
this.SetValue("Provider", Provider_eduSTAR);
else
throw new NotSupportedException(string.Format("Unsupported Wireless Provider: ", value));
}
}
#region eduSTAR Configuration
public string eduSTAR_Scope
{
get { return "Wireless_eduSTAR"; }
}
public string eduSTAR_ServiceAccountSchoolId
{
get
{
return this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountSchoolId", null);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountSchoolId", value);
}
}
public string eduSTAR_ServiceAccountUsername
{
get
{
return this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountUsername", null);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountUsername", value);
}
}
public string eduSTAR_ServiceAccountPassword
{
get
{
return ConfigurationContext.DeobsfucateValue(this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountPassword", null));
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountPassword", ConfigurationContext.ObsfucateValue(value));
}
}
#endregion
}
}
@@ -0,0 +1,275 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Data.Repository;
using Disco.Models.Repository;
using System.IO;
using System.Security.Cryptography;
using Disco.Models.BI.Interop.Community;
using Newtonsoft.Json;
namespace Disco.Data.Configuration
{
public class SystemConfiguration : ConfigurationBase
{
public SystemConfiguration(DiscoDataContext dbContext)
: base(dbContext)
{
// Init Modules
this.moduleBootstrapperConfiguration = new Lazy<Modules.BootstrapperConfiguration>(() => new Modules.BootstrapperConfiguration(dbContext));
this.moduleDeviceProfilesConfiguration = new Lazy<Modules.DeviceProfilesConfiguration>(() => new Modules.DeviceProfilesConfiguration(dbContext));
this.moduleOrganisationAddressesConfiguration = new Lazy<Modules.OrganisationAddressesConfiguration>(() => new Modules.OrganisationAddressesConfiguration(dbContext));
}
#region Configuration Modules
private Lazy<Modules.BootstrapperConfiguration> moduleBootstrapperConfiguration;
private Lazy<Modules.DeviceProfilesConfiguration> moduleDeviceProfilesConfiguration;
private Lazy<Modules.OrganisationAddressesConfiguration> moduleOrganisationAddressesConfiguration;
public Modules.BootstrapperConfiguration Bootstrapper
{
get
{
return moduleBootstrapperConfiguration.Value;
}
}
public Modules.DeviceProfilesConfiguration DeviceProfiles
{
get
{
return moduleDeviceProfilesConfiguration.Value;
}
}
public Modules.OrganisationAddressesConfiguration OrganisationAddresses
{
get
{
return moduleOrganisationAddressesConfiguration.Value;
}
}
#endregion
public override string Scope { get { return "System"; } }
public string DataStoreLocation
{
get
{
var result = this.Get<string>(null);
if (result == null)
{
var appDataPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data");
if (appDataPath.EndsWith("\\"))
return appDataPath;
else
return string.Concat(appDataPath, '\\');
}
else
return result;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
if (!System.IO.Directory.Exists(value))
throw new System.IO.DirectoryNotFoundException(string.Format("DataStoreLocation: '{0}' could not be found", value));
string storePath;
if (value.EndsWith("\\"))
storePath = value;
else
storePath = string.Concat(value, '\\');
this.Set(storePath);
}
}
#region Plugin Locations
public string PluginsLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"Plugins\");
}
}
public string PluginStorageLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"PluginStorage\");
}
}
public string PluginPackagesLocation
{
get
{
return System.IO.Path.Combine(this.DataStoreLocation, @"PluginPackages\");
}
}
#endregion
#region Organisation Details
#region Organisation Logo
private string OrganisationLogoPath
{
get
{
return System.IO.Path.Combine(DataStoreLocation, "OrganisationLogo.png");
}
}
public string OrganisationLogoHash
{
get
{
var path = this.OrganisationLogoPath;
if (File.Exists(path))
return File.GetLastWriteTimeUtc(path).ToBinary().ToString();
else
return "-1";
}
}
public Stream OrganisationLogo
{
get
{
var path = this.OrganisationLogoPath;
if (File.Exists(path))
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
else
return new MemoryStream(Disco.Data.Properties.Resources.EmptyLogo);
}
set
{
string organisationLogoPath = this.OrganisationLogoPath;
if (value == null)
{
if (System.IO.File.Exists(organisationLogoPath))
System.IO.File.Delete(organisationLogoPath);
}
else
{
using (FileStream fs = new FileStream(organisationLogoPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
value.CopyTo(fs);
}
}
}
}
#endregion
public string OrganisationName
{
get
{
return this.Get<string>(null);
}
set
{
this.Set(value);
}
}
public bool MultiSiteMode
{
get
{
return this.Get(false);
}
set
{
this.Set(value);
}
}
#endregion
#region Proxy Configuration
public string ProxyAddress
{
get
{
return this.Get<string>(null);
}
set
{
this.Set(value);
}
}
public int ProxyPort
{
get
{
return this.Get(8080);
}
set
{
this.Set(value);
}
}
public string ProxyUsername
{
get
{
return this.Get<string>(null);
}
set
{
this.Set(value);
}
}
public string ProxyPassword
{
get
{
return this.GetDeobsfucated(null);
}
set
{
this.SetObsfucated(value);
}
}
#endregion
#region UpdateCheck
public string DeploymentId
{
get
{
return this.Get<string>(null);
}
}
public UpdateResponse UpdateLastCheck
{
get
{
return this.GetFromJson<UpdateResponse>(null);
}
set
{
this.SetAsJson(value);
}
}
public bool UpdateBetaDeployment
{
get
{
return this.Get<bool>(false);
}
}
public Version InstalledDatabaseVersion
{
get
{
var versionString = this.Get<string>(null);
if (string.IsNullOrEmpty(versionString))
return null;
else
return Version.Parse(versionString);
}
set
{
this.Set<string>(value.ToString(4));
}
}
#endregion
}
}
+2 -2
View File
@@ -71,12 +71,12 @@
<ItemGroup>
<Compile Include="Configuration\CommunityHelpers.cs" />
<Compile Include="Configuration\ConfigurationBase.cs" />
<Compile Include="Configuration\ConfigurationContext.cs" />
<Compile Include="Configuration\ConfigurationCache.cs" />
<Compile Include="Configuration\SystemConfiguration.cs" />
<Compile Include="Configuration\Modules\BootstrapperConfiguration.cs" />
<Compile Include="Configuration\Modules\DeviceProfileConfiguration.cs" />
<Compile Include="Configuration\Modules\DeviceProfilesConfiguration.cs" />
<Compile Include="Configuration\Modules\OrganisationAddressesConfiguration.cs" />
<Compile Include="Configuration\Modules\WirelessConfiguration.cs" />
<Compile Include="Migrations\201204250418485_DBv0.cs" />
<Compile Include="Migrations\201204250418485_DBv0.Designer.cs">
<DependentUpon>201204250418485_DBv0.cs</DependentUpon>
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0430.1219")]
[assembly: AssemblyFileVersion("1.2.0430.1219")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]
+3 -3
View File
@@ -10,11 +10,11 @@ namespace Disco.Data.Repository
{
public class DiscoDataContext : DbContext
{
private Lazy<Configuration.ConfigurationContext> _Configuration;
private Lazy<Configuration.SystemConfiguration> _Configuration;
public DiscoDataContext()
{
this._Configuration = new Lazy<Configuration.ConfigurationContext>(() => new Configuration.ConfigurationContext(this));
this._Configuration = new Lazy<Configuration.SystemConfiguration>(() => new Configuration.SystemConfiguration(this));
}
public virtual DbSet<ConfigurationItem> ConfigurationItems { get; set; }
@@ -47,7 +47,7 @@ namespace Disco.Data.Repository
public virtual DbSet<JobMetaNonWarranty> JobMetaNonWarranties { get; set; }
public virtual DbSet<JobMetaInsurance> JobMetaInsurances { get; set; }
public Configuration.ConfigurationContext DiscoConfiguration
public Configuration.SystemConfiguration DiscoConfiguration
{
get
{
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0430.1219")]
[assembly: AssemblyFileVersion("1.2.0430.1219")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0430.1219")]
[assembly: AssemblyFileVersion("1.2.0430.1219")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0430.1219")]
[assembly: AssemblyFileVersion("1.2.0430.1219")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]
@@ -85,7 +85,7 @@ namespace Disco.Web.Areas.Config.Models.SystemConfig
public UpdateResponse UpdateLatestResponse { get; set; }
public bool UpdateBetaDeployment { get; set; }
public static IndexModel FromConfiguration(ConfigurationContext config)
public static IndexModel FromConfiguration(SystemConfiguration config)
{
return new IndexModel()
{
@@ -104,7 +104,7 @@ namespace Disco.Web.Areas.Config.Models.SystemConfig
public void ToConfiguration(DiscoDataContext db)
{
ConfigurationContext config = db.DiscoConfiguration;
SystemConfiguration config = db.DiscoConfiguration;
//config.DataStoreLocation = DataStoreLocation;
config.ProxyAddress = ProxyAddress;
config.ProxyPort = ProxyPort;
+1 -1
View File
@@ -70,7 +70,7 @@ namespace Disco.Web
}
public static MvcHtmlString OrganisationLogoUrl(this UrlHelper urlHelper, int Width = 256, int Height = 256)
{
var config = new Disco.Data.Configuration.ConfigurationContext(null);
var config = new Disco.Data.Configuration.SystemConfiguration(null);
return new MvcHtmlString(urlHelper.Action(MVC.API.System.OrganisationLogo(Width, Height, config.OrganisationLogoHash)));
}
#endregion
+2 -2
View File
@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.2.0430.1219")]
[assembly: AssemblyFileVersion("1.2.0430.1219")]
[assembly: AssemblyVersion("1.2.0503.2044")]
[assembly: AssemblyFileVersion("1.2.0503.2044")]