using System;
using System.Globalization;
using System.Reflection;
using Umbraco.Core.Models.Entities;
namespace Umbraco.Core.Models
{
///
/// Represents a registered server in a multiple-servers environment.
///
public class ServerRegistration : EntityBase, IServerRegistration
{
private string _serverAddress;
private string _serverIdentity;
private bool _isActive;
private bool _isMaster;
private static readonly Lazy Ps = new Lazy();
private class PropertySelectors
{
public readonly PropertyInfo ServerAddressSelector = ExpressionHelper.GetPropertyInfo(x => x.ServerAddress);
public readonly PropertyInfo ServerIdentitySelector = ExpressionHelper.GetPropertyInfo(x => x.ServerIdentity);
public readonly PropertyInfo IsActiveSelector = ExpressionHelper.GetPropertyInfo(x => x.IsActive);
public readonly PropertyInfo IsMasterSelector = ExpressionHelper.GetPropertyInfo(x => x.IsMaster);
}
///
/// Initialiazes a new instance of the class.
///
public ServerRegistration()
{ }
///
/// Initialiazes a new instance of the class.
///
/// The unique id of the server registration.
/// The server url.
/// The unique server identity.
/// The date and time the registration was created.
/// The date and time the registration was last accessed.
/// A value indicating whether the registration is active.
/// A value indicating whether the registration is master.
public ServerRegistration(int id, string serverAddress, string serverIdentity, DateTime registered, DateTime accessed, bool isActive, bool isMaster)
{
UpdateDate = accessed;
CreateDate = registered;
Key = id.ToString(CultureInfo.InvariantCulture).EncodeAsGuid();
Id = id;
ServerAddress = serverAddress;
ServerIdentity = serverIdentity;
IsActive = isActive;
IsMaster = isMaster;
}
///
/// Initialiazes a new instance of the class.
///
/// The server url.
/// The unique server identity.
/// The date and time the registration was created.
public ServerRegistration(string serverAddress, string serverIdentity, DateTime registered)
{
CreateDate = registered;
UpdateDate = registered;
Key = 0.ToString(CultureInfo.InvariantCulture).EncodeAsGuid();
ServerAddress = serverAddress;
ServerIdentity = serverIdentity;
}
///
/// Gets or sets the server url.
///
public string ServerAddress
{
get { return _serverAddress; }
set { SetPropertyValueAndDetectChanges(value, ref _serverAddress, Ps.Value.ServerAddressSelector); }
}
///
/// Gets or sets the server unique identity.
///
public string ServerIdentity
{
get { return _serverIdentity; }
set { SetPropertyValueAndDetectChanges(value, ref _serverIdentity, Ps.Value.ServerIdentitySelector); }
}
///
/// Gets or sets a value indicating whether the server is active.
///
public bool IsActive
{
get { return _isActive; }
set { SetPropertyValueAndDetectChanges(value, ref _isActive, Ps.Value.IsActiveSelector); }
}
///
/// Gets or sets a value indicating whether the server is master.
///
public bool IsMaster
{
get { return _isMaster; }
set { SetPropertyValueAndDetectChanges(value, ref _isMaster, Ps.Value.IsMasterSelector); }
}
///
/// Gets the date and time the registration was created.
///
public DateTime Registered { get { return CreateDate; } set { CreateDate = value; }}
///
/// Gets the date and time the registration was last accessed.
///
public DateTime Accessed { get { return UpdateDate; } set { UpdateDate = value; }}
///
/// Converts the value of this instance to its equivalent string representation.
///
///
public override string ToString()
{
return string.Format("{{\"{0}\", \"{1}\", {2}active, {3}master}}", ServerAddress, ServerIdentity, IsActive ? "" : "!", IsMaster ? "" : "!");
}
}
}