using System.Windows; using BeWo.ServiceProxy; using BeWo.Validation; using BS.Shared; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; namespace BeWo.ViewModel { public class TextModuleVM : AbstractDCMapperVM { private int _Position; private string _Text; private string _Name; private CompactEmployeeDC _Employee; private ServiceCategoryDC _ServiceCategory; private bool _IsOnlyForEmployee; private bool _IsParent; private bool _IsExpanded; private TextModuleVM _Parent; private ActivationTypeId _ActivationType; public TextModuleVM(TextModuleDC pDataContract) : base(pDataContract, pDataContract.TextModuleOid == null) {} public bool IsExpanded { get => _IsExpanded; set { if(AreDifferent(_IsExpanded, value)) { _IsExpanded = value; StoreDirtyInformation(AreDifferent(DataContract.IsExpanded, value), nameof(IsExpanded)); FirePropertyChanged(nameof(IsExpanded)); } } } public ServiceCategoryDC ServiceCategory { get => _ServiceCategory; set { if (AreDifferent(_ServiceCategory, value)) { _ServiceCategory = value; StoreDirtyInformation(AreDifferent(DataContract.ServiceCategory, value), nameof(ServiceCategory)); FirePropertyChanged(nameof(ServiceCategory)); } } } public CompactEmployeeDC Employee { get => _Employee; set { if (AreDifferent(_Employee, value)) { _Employee = value; StoreDirtyInformation(AreDifferent(DataContract.Employee, value), nameof(Employee)); FirePropertyChanged(nameof(Employee)); } } } public ActivationTypeId ActivationType { get => _ActivationType; set { if(AreDifferent(_ActivationType, value)) { _ActivationType = value; StoreDirtyInformation(AreDifferent(DataContract.ActivationType, value), nameof(ActivationType)); FirePropertyChanged(nameof(ActivationType)); } } } public int Position { get => _Position; set { if (AreDifferent(_Position, value)) { _Position = value; StoreDirtyInformation(AreDifferent(DataContract.Position, value), nameof(Position)); FirePropertyChanged(nameof(Position)); } } } public string Text { get => _Text; set { if (AreDifferent(_Text, value)) { _Text = value; StoreDirtyInformation(AreDifferent(DataContract.Text, value), nameof(Text)); FirePropertyChanged(nameof(Text)); } } } public bool IsOnlyForEmployee { get => _IsOnlyForEmployee; set { if (AreDifferent(_IsOnlyForEmployee, value)) { _IsOnlyForEmployee = value; StoreDirtyInformation(AreDifferent(DataContract.IsOnlyForEmployee, value), nameof(IsOnlyForEmployee)); FirePropertyChanged(nameof(IsOnlyForEmployee)); } } } public bool IsParent { get => _IsParent; set { if(AreDifferent(_IsParent, value)) { _IsParent = value; StoreDirtyInformation(AreDifferent(DataContract.IsParent, value), nameof(IsParent)); FirePropertyChanged(nameof(IsParent)); } } } public TextModuleVM Parent { get => _Parent; set { if(AreDifferent(_Parent, value) && AreDifferent(value, this)) { _Parent = value; StoreDirtyInformation(AreDifferent(DataContract.Parent, value), nameof(Parent)); FirePropertyChanged(nameof(Parent)); } } } public FontWeight FontWeight => _IsParent ? FontWeights.Bold : FontWeights.Normal; public long? KeyField => DataContract?.TextModuleOid; public long? ParentField => Parent?.DataContract?.TextModuleOid; [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)] public string Name { get => _Name; set { if (AreDifferent(_Name, value)) { _Name = value; StoreDirtyInformation(AreDifferent(DataContract.Name, value), nameof(Name)); FirePropertyChanged(nameof(Name)); } } } protected override void InitByDataContract(TextModuleDC pDataContract) { if (!IsNew) { _ServiceCategory = pDataContract.ServiceCategory; _Text = pDataContract.Text; _Position = pDataContract.Position; _Name = pDataContract.Name; _Employee = pDataContract.Employee; _IsOnlyForEmployee = pDataContract.IsOnlyForEmployee; _IsParent = pDataContract.IsParent; _Parent = pDataContract.Parent != null ? new TextModuleVM(pDataContract.Parent) : null; _IsExpanded = pDataContract.IsExpanded; } else { ServiceFacade.DoEmployeeServiceSync(s => _Employee = s.LoadCompactEmployee(BeWoApp.LoggedOnEmployee.EmployeeOid.Value)); if (BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineNurEigeneBearbeiten) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineAlleBearbeiten)) { _IsOnlyForEmployee = true; } } } protected override TextModuleDC MapToDataContract(TextModuleDC pDataContract, bool doCommit) { pDataContract.Text = _Text; pDataContract.Position = _Position; pDataContract.Name = _Name; pDataContract.IsParent = _IsParent; pDataContract.IsExpanded = _IsExpanded; pDataContract.ActivationType = _ActivationType; pDataContract.IsOnlyForEmployee = _IsOnlyForEmployee; pDataContract.Parent = _Parent?.CommitToDataContract(); pDataContract.Employee = _Employee; pDataContract.ServiceCategory = _ServiceCategory; return pDataContract; } public override bool Equals(object pObj) { if (!(pObj is TextModuleVM)) { return false; } var obj = (TextModuleVM) pObj; return Equals(IsNew, obj.IsNew) && Equals(ServiceCategory, obj.ServiceCategory) && Equals(Text, obj.Text) && Equals(Position, obj.Position) && Equals(Parent, obj.Parent) && Equals(Name, obj.Name) && Equals(Employee, obj.Employee) && Equals(IsOnlyForEmployee, obj.IsOnlyForEmployee) && Equals(IsParent, obj.IsParent) && Equals(IsDirty, obj.IsDirty); } public override string ToString() { return _Name; } } }