using System.Collections.ObjectModel; using System.Linq; using BeWo.Validation; using BeWo.ViewModel.ListViewModel; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using BS.Shared.Extensions; namespace BeWo.ViewModel { public class TeamVM : AbstractDCMapperVM { public static string PropertyName_Leader = "Leader"; public static string PropertyName_LeaderString = "LeaderString"; public static string PropertyName_Member = "Member"; public static string PropertyName_Name = "Name"; public static string PropertyName_Notice = "Notice"; public static string PropertyName_VarFields = "VarFields"; private CompactEmployeeDC _Leader; private ObservableCollection _Member; private ObservableCollection _OwnChatMember; private bool _IsOwnChatTeam; private string _Name; private string _Notice; private VarFieldListVM _VarFields; public TeamVM(TeamDC pDataContract) : base(pDataContract, pDataContract.TeamOid == null) { } public override bool IsDirty { get { return base.IsDirty || (_VarFields != null && _VarFields.IsDirty); } } public bool IsOwnChatTeam { get { return _IsOwnChatTeam; } set { if (AreDifferent(_IsOwnChatTeam, value)) { _IsOwnChatTeam = value; StoreDirtyInformation(AreDifferent(DataContract.IsOwnChatTeam, value), nameof(IsOwnChatTeam)); FirePropertyChanged(nameof(IsOwnChatTeam)); } } } public CompactEmployeeDC Leader { get { return this._Leader; } set { if (this.AreDifferent(this._Leader, value)) { this._Leader = value; bool lSame = (this.DataContract.Leader == null && value == null) || (this.DataContract.Leader != null && value != null && this.DataContract.Leader.EmployeeOid == value.EmployeeOid); this.StoreDirtyInformation(!lSame, PropertyName_Leader); this.FirePropertyChanged(PropertyName_Leader); this.FirePropertyChanged(PropertyName_LeaderString); } } } public string LeaderString { get { return this._Leader.ToStringNullCheck(); } // Validation Workaround set { } } public ObservableCollection Member { get { return this._Member; } set { if (this.AreDifferent(this._Member, value)) { this._Member = value; this.FirePropertyChanged(PropertyName_Member); } } } public ObservableCollection OwnChatMember { get { return this._OwnChatMember; } set { if (this.AreDifferent(this._OwnChatMember, value)) { this._OwnChatMember = value; this.FirePropertyChanged(nameof(OwnChatMember)); } } } public void OwnChatMemberRoleChanged(bool changed) { this.StoreDirtyInformation(changed, "OwnChatMemberRole"); } [ValidationAttribute(ValidationRule = ValidationRules.NotNullOrStringEmpty)] public string Name { get { return this._Name; } set { if (this.AreDifferent(this._Name, value)) { this._Name = value; this.StoreDirtyInformation(this.DataContract.Name != value, PropertyName_Name); this.FirePropertyChanged(PropertyName_Name); } } } public string Notice { get { return this._Notice; } set { if (this.AreDifferent(this._Notice, value)) { this._Notice = value; this.StoreDirtyInformation(this.DataContract.Notice != value, PropertyName_Notice); this.FirePropertyChanged(PropertyName_Notice); } } } public VarFieldListVM VarFields { get { return _VarFields; } set { if (AreDifferent(_VarFields, value)) { _VarFields = value; FirePropertyChanged(PropertyName_VarFields); } } } protected override void InitByDataContract(TeamDC pDataContract) { this._Member = new ObservableCollection(); this._OwnChatMember = new ObservableCollection(); if (!this.IsNew) { this._IsOwnChatTeam = pDataContract.IsOwnChatTeam; this._Name = pDataContract.Name; this._Notice = pDataContract.Notice; this._Leader = pDataContract.Leader; if (pDataContract.Member != null) { this._Member.AddRange(pDataContract.Member.OrderBy(m => string.Format("{0},{1}", m.LastName, m.FirstName))); } if (pDataContract.OwnChatMember != null) { this._OwnChatMember.AddRange(pDataContract.OwnChatMember.OrderBy(m => string.Format("{0},{1}", m.Person.LastName, m.Person.FirstName))); } } _VarFields = new VarFieldListVM(pDataContract.TeamVarFields); this._Member.CollectionChanged += (s, e) => this.StoreDirtyInformation(!pDataContract.Member.ContainsSameItemsAs(this._Member), PropertyName_Member); this._OwnChatMember.CollectionChanged += (s, e) => this.StoreDirtyInformation(!pDataContract.OwnChatMember.ContainsSameItemsAs(this._OwnChatMember), nameof(OwnChatMember)); } protected override TeamDC MapToDataContract(TeamDC pDataContract, bool doCommit) { pDataContract.IsOwnChatTeam = this._IsOwnChatTeam; pDataContract.Leader = this._Leader; pDataContract.Name = this._Name; pDataContract.Notice = this._Notice; pDataContract.Member = this._Member.ToList(); pDataContract.OwnChatMember = this._OwnChatMember.ToList(); if (_VarFields != null) { pDataContract.TeamVarFields = _VarFields.CopyToDCList(doCommit); } return pDataContract; } } }