using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using BeWo.Scheduler.Resources; using BeWo.Scheduler.View; using BeWo.ViewModel; using BeWo.ViewModel.ListViewModel; using BS.Shared.DataContracts; using DevExpress.Xpf.Scheduler; using DevExpress.XtraScheduler; using SchedulerControl = DevExpress.Xpf.Scheduler.SchedulerControl; using SchedulerStorage = DevExpress.Xpf.Scheduler.SchedulerStorage; namespace BeWo.Scheduler.ViewModel { public abstract class AbstractAppointmentListVM : AbstractDCListMapperVM, ISchedulerViewModel where VMType : AbstractDCMapperVM, IBeWoAppointment where DCType : new() { private List mAppointmentCategoryList; private BindingList mAppointmentList; private List mAppointmentResourceList; public AbstractAppointmentListVM(IEnumerable appList) : base(appList) { } public IEnumerable Categories { get { return mAppointmentCategoryList; } } public IList Resources { get { return mAppointmentResourceList; } } public Dictionary>> ChangedOccurencyCustomFields { get; private set; } public object SelectedFilterObject { get; set; } public virtual bool FilterAppointment(string filterCategory, object selectedObject, Appointment appointment, bool showPrivateAppointments) { return false; } public void InitChangedOccurrencyCustomFields(IEnumerable appList) { } public BindingList Appointments { get { if (mAppointmentList != null) return mAppointmentList; mAppointmentList = new BindingList(); foreach (var vm in VMList) mAppointmentList.Add(vm); mAppointmentList.AddingNew += mAppointmentList_AddingNew; return mAppointmentList; } } public abstract SchedulerSettings GetDefaultSchedulerSettings(); public void SaveAppointments(SchedulerControl control, IList list) { var dcList = new List(); foreach (var appointment in list) { var vm = appointment.GetSourceObject(control.GetCoreStorage()) as VMType; if (vm == null) continue; UpdateAppointment(appointment, vm); dcList.Add(vm.CommitToDataContract()); } if (dcList.Count > 0) SaveAppointments(dcList); } public void DeleteAppointments(SchedulerControl control, IList list) { var dcList = (from appointment in list select appointment.GetSourceObject(control.GetCoreStorage()) as VMType into vm where vm != null && !vm.IsNew select vm.CommitToDataContract()).ToList(); if (dcList.Count > 0) DeleteAppointments(dcList); } public void InsertAppointments(SchedulerControl control, IList list) { var dcList = new List(); foreach (var appointment in list) { var vm = appointment.GetSourceObject(control.GetCoreStorage()) as VMType; if (vm == null) continue; UpdateAppointment(appointment, vm); dcList.Add(vm.CommitToDataContract()); } if (dcList.Count > 0) InsertNewAppointments(dcList); } public virtual AbstractAppointmentEditForm GetEditAppointmentForm(SchedulerControl control, Appointment appointment) { return null; } public virtual void AddCustomFieldsMapping(SchedulerStorage schedulerStorage) { } public virtual void InitNewAppointment(Appointment appointment) { appointment.StatusId = 0; } protected void InitViewModel() { mAppointmentCategoryList = new List(); mAppointmentResourceList = new List(); InitCategoryList(mAppointmentCategoryList); InitResourceList(mAppointmentResourceList); InitAppointmentList(); mAppointmentResourceList.Sort((a, b) => a.Caption.CompareTo(b.Caption)); } protected virtual void InitCategoryList(List categoryList) { // } protected virtual void InitResourceList(List resourceList) { // } protected virtual void InitAppointmentList() { // } private void mAppointmentList_AddingNew(object sender, AddingNewEventArgs e) { var newApp = CreateNewAppointmentVM(); e.NewObject = newApp; } protected abstract IBeWoAppointment CreateNewAppointmentVM(); protected abstract void SaveAppointments(List dcList); protected abstract void DeleteAppointments(IEnumerable dcList); protected abstract void InsertNewAppointments(List dcList); protected virtual void UpdateAppointment(Appointment app, VMType vm) { } protected virtual void UpdateViewModel(IEnumerable updatedDcList) { foreach (DCType dc in updatedDcList) { var found = default(VMType); foreach (var vm in mAppointmentList.Cast().Where(vm => vm.DataContract.Equals(dc))) found = vm; if (found != null) found.DataContract = dc; } } protected virtual void UpdateResourceList(List resourceList, long pOid) { mAppointmentResourceList.Clear(); mAppointmentResourceList.Add(resourceList.Find(f => f.Id.Equals(pOid))); } } }