using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using BeWo.View; using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using BS.Shared.Extensions; namespace BeWo.ViewModel { public class UserVM : AbstractDCMapperVM { public static string PropertyName_Employee = "Employee"; public static string PropertyName_EmployeeString = "EmployeeString"; public static string PropertyName_LoginName = "LoginName"; public static string PropertyName_PossibleGroups = "PossibleGroups"; public static string PropertyName_RightsString = "RightsString"; public static string PropertyName_UserGrous = "UserGrous"; public static string PropertyName_Zeitstempel = "TimeStamp"; public static string PropertyName_LicenseType = "LicenseType"; private readonly List _AllGroups; private CompactEmployeeDC _Employee; private string _LoginName; private string _LicenseType; private ObservableSortCollection _UserGroups; private ObservableSortCollection _AllLicenseTypes; private DateTime _Zeitstempel; public UserVM(UserDC pDataContract, List pAllUserGroups) : base(pDataContract, pDataContract.UserOid == null) { _AllGroups = pAllUserGroups; } public CompactEmployeeDC Employee { get { return _Employee; } set { if (AreDifferent(_Employee, value)) { _Employee = value; StoreDirtyInformation(!AreEqual(DataContract.Employee, value), PropertyName_Employee); FirePropertyChanged(PropertyName_Employee); FirePropertyChanged(PropertyName_EmployeeString); } } } public string EmployeeString { get { return _Employee != null ? _Employee.LastName + ", " + _Employee.FirstName : string.Empty; } } public string LoginName { get { return _LoginName; } set { if (AreDifferent(_LoginName, value)) { _LoginName = value; StoreDirtyInformation(AreDifferent(DataContract.LoginName, value), PropertyName_LoginName); FirePropertyChanged(PropertyName_LoginName); } } } public string LicenseType { get { return _LicenseType; } set { if (AreDifferent(_LicenseType, value)) { _LicenseType = value; StoreDirtyInformation(AreDifferent(DataContract.LicenseType, value), PropertyName_LicenseType); FirePropertyChanged(PropertyName_LicenseType); } } } public List PossibleGroups { get { return _AllGroups.Where(x => _UserGroups.FirstOrDefault(y => x.UserGroupOid == y.UserGroupOid) == null).ToList(); } } public string RightsString { get { var lRights = new List(); string lResult = string.Empty; foreach (UserGroupDC IGroup in _UserGroups) { foreach (UserRightType iRight in IGroup.Rights) { if (!lRights.Contains(iRight)) { lRights.Add(iRight); lResult += EnumTranslations.UserRightType2Translations[iRight]; lResult += ", "; } } } if (lResult != string.Empty) { return lResult.Remove(lResult.LastIndexOf(",")); } return "-"; } } public ObservableSortCollection UserGroups { get { return _UserGroups ?? (_UserGroups = new ObservableSortCollection()); } set { if (AreDifferent(_UserGroups, value)) { _UserGroups = value; StoreDirtyInformation(!UserGroups.ContainsSameItemsAs(DataContract.UserGroups), PropertyName_UserGrous); FirePropertyChanged(PropertyName_UserGrous); } } } public ObservableSortCollection AllLicenseTypes { get { if (_AllLicenseTypes == null) { this._AllLicenseTypes = new ObservableSortCollection(); } return this._AllLicenseTypes; } } protected override void InitByDataContract(UserDC pDataContract) { _LoginName = pDataContract.LoginName; _LicenseType = pDataContract.LicenseType; if (pDataContract.UserGroups != null) { _UserGroups = pDataContract.UserGroups.ToObservableSortCollection(); } else { _UserGroups = new ObservableSortCollection(); } _UserGroups.CollectionChanged += (s, e) => { StoreDirtyInformation(!_UserGroups.ContainsSameItemsAs(pDataContract.UserGroups), PropertyName_UserGrous); FirePropertyChanged(PropertyName_PossibleGroups); FirePropertyChanged(PropertyName_RightsString); }; _Employee = pDataContract.Employee; if (pDataContract.AllLicenseTypes != null) { _AllLicenseTypes = pDataContract.AllLicenseTypes.ToObservableSortCollection(); } } protected override UserDC MapToDataContract(UserDC pDataContract, bool doCommit) { pDataContract.LoginName = _LoginName; pDataContract.Employee = _Employee; pDataContract.UserGroups = _UserGroups.ToList(); pDataContract.LicenseType = _LicenseType; //pDataContract.NewPassword = this.CheckOldPassword( return pDataContract; } public DateTime Zeitstempel { get { return _Zeitstempel; } set { if (AreDifferent(_Zeitstempel, value)) { _Zeitstempel = value; StoreDirtyInformation(AreDifferent(DataContract.LoginName, value), PropertyName_Zeitstempel); FirePropertyChanged(PropertyName_Zeitstempel); } } } } }