Beispiel #1
0
        /// <summary>
        /// Muestra los botones correspondientes en función de cada estado.
        /// </summary>
        /// <param name="state"></param>
        private void ConfigureByState(ToolbarStates state)
        {
            _currentState = state;
            switch (state)
            {
            case ToolbarStates.OnlyRead:
                stackStrip.Visible = false;
                break;

            case ToolbarStates.OnlyEdit:
                stackStrip.Visible        = true;
                editStackButton.Visible   = true;
                newStackButton.Visible    = false;
                saveStackButton.Visible   = false;
                cancelStackButton.Visible = false;
                break;

            case ToolbarStates.OnlyEditNew:
                stackStrip.Visible        = true;
                editStackButton.Visible   = true;
                newStackButton.Visible    = true;
                saveStackButton.Visible   = false;
                cancelStackButton.Visible = false;
                break;

            case ToolbarStates.Edit:
            case ToolbarStates.New:
                stackStrip.Visible        = true;
                editStackButton.Visible   = false;
                newStackButton.Visible    = false;
                saveStackButton.Visible   = true;
                cancelStackButton.Visible = true;
                break;
            }
        }
        private void DoSaveCommand()
        {
            if (this.IsSaveEnabled)
            {
                this.CurrentSaveImagePath = KarveToolBarViewModel.currentSaveImage;
                this.IsSaveEnabled        = false;
                DataPayLoad payLoad = _careKeeper.GetScheduledPayload();
                if ((_careKeeper.GetScheduledPayloadType() == DataPayLoad.Type.Insert) || (_states == ToolbarStates.Insert))
                {
                    InsertDataCommand dataCommand = new InsertDataCommand(this._dataServices,
                                                                          this._careKeeper,
                                                                          this._eventManager,
                                                                          this._configurationService)
                    {
                        ValidationRules = this._validationRules
                    };
                    _careKeeper.Do(new CommandWrapper(dataCommand));
                    _states = ToolbarStates.None;
                }
                else
                {
                    SaveDataCommand dataCommand = new SaveDataCommand(this._dataServices, this._careKeeper,
                                                                      this._eventManager, this._configurationService);

                    payLoad             = _careKeeper.Do(new CommandWrapper(dataCommand));
                    payLoad.PayloadType = DataPayLoad.Type.UpdateView;

                    _eventManager.NotifyObserverSubsystem(payLoad.SubsystemName, payLoad);
                    //DeliverIncomingNotify(payLoad.Subsystem, payLoad);
                }
            }
            this.CurrentSaveImagePath = KarveToolBarViewModel.currentSaveImage;
            this.IsSaveEnabled        = false;
        }
        private void DoNewCommand()
        {
            DataPayLoad payLoad = new DataPayLoad
            {
                PayloadType = DataPayLoad.Type.Insert
            };

            _states = ToolbarStates.Insert;
            // this send a message to the current control view model.
            DeliverIncomingNotify(_activeSubSystem, payLoad);
        }
        private void DoDeleteCommand()
        {
            string value = _configurationService.GetPrimaryKeyValue();

            _states = ToolbarStates.Delete;
            DataPayLoad payLoad = new DataPayLoad
            {
                PayloadType     = DataPayLoad.Type.Delete,
                PrimaryKeyValue = value
            };

            if (value.Length > 0)
            {
                DeliverIncomingNotify(_activeSubSystem, payLoad);
            }
        }
        /// <summary>
        ///  Each different subsytem call this method to notify a change in the system to the toolbar.
        /// </summary>
        /// <param name="payload"></param>
        public void IncomingPayload(DataPayLoad payload)
        {
            IsNewEnabled   = true;
            CurrentPayLoad = payload;
            switch (payload.PayloadType)
            {
            // a subsystem has opened a new window with data.
            case DataPayLoad.Type.RegistrationPayload:
            {
                _activeSubSystem = payload.Subsystem;
                IsNewEnabled     = true;
                break;
            }

            case DataPayLoad.Type.Delete:
            {
                string primaryKeyValue = payload.PrimaryKeyValue;
                _configurationService.CloseTab(primaryKeyValue);
                _activeSubSystem = payload.Subsystem;
                _states          = ToolbarStates.None;
                DataPayLoad payLoad = new DataPayLoad
                {
                    PayloadType = DataPayLoad.Type.UpdateView
                };
                DeliverIncomingNotify(_activeSubSystem, payLoad);
                break;
            }

            case DataPayLoad.Type.UpdateView:
            {
                break;
            }

            // a subsystem has updated a new window with data.
            case DataPayLoad.Type.Insert:
            case DataPayLoad.Type.Update:
            {
                this.CurrentSaveImagePath = currentSaveImageModified;
                this.IsSaveEnabled        = true;
                // this keeps the value for saving.
                //MessageBox.Show("Schedule Payload");
                _careKeeper.Schedule(payload);

                break;
            }
            }
        }
        private void DoNewCommand()
        {
            var payLoad = new DataPayLoad
            {
                PayloadType = DataPayLoad.Type.Insert,
                Sender      = ViewModelUri.ToString()
            };

            var activeView = _regionManager.Regions[RegionNames.TabRegion].ActiveViews.FirstOrDefault();

            string directSubsystem = string.Empty;

            if (activeView != null)
            {
                if (activeView is UserControl control)
                {
                    if (control.DataContext is KarveViewModelBase baseViewModel)
                    {
                        var subsystem = baseViewModel.SubSystem;

                        if (subsystem != DataSubSystem.None)
                        {
                            _activeSubSystem = subsystem;
                        }
                    }
                }
            }
            // one user that uses the command registration shall not use any new notification.
            Uri uri = null;

            if (IsCommandBaseOnly(ref uri))
            {
                DoNewAll(uri);
            }
            else
            {
                DeliverIncomingNotify(_activeSubSystem, payLoad);
            }
            _states = ToolbarStates.Insert;
        }
        /// <summary>
        /// KarveToolBarViewModel is a view model to modle the toolbar behaviour.
        /// </summary>
        /// <param name="dataServices">Service for fetching datas</param>
        /// <param name="eventManager">Service for communicate with other view models</param>
        /// <param name="careKeeper">Service for caring command and storing/undoing command</param>
        /// <param name="regionManager">Service for region handling</param>
        /// <param name="dialogService">Service for spotting the dialog</param>
        /// <param name="configurationService">Service for the configuraration parameters</param>
        public KarveToolBarViewModel(IDataServices dataServices,
                                     IEventManager eventManager,
                                     ICareKeeperService careKeeper,
                                     IRegionManager regionManager,
                                     IDialogService dialogService,
                                     IConfigurationService configurationService) : base(dataServices, null, dialogService, configurationService)
        {
            this._dictionary           = SubsystemFactory.GetSubsytem();
            this._dataServices         = dataServices;
            this._dialogService        = dialogService;
            this._configurationService = configurationService;
            this._eventManager         = eventManager;
            this._eventManager.RegisterObserverToolBar(this);
            this._careKeeper           = careKeeper;
            this.SaveCommand           = new DelegateCommand(DoSaveCommand);
            this.NewCommand            = new DelegateCommand(DoNewCommand);
            this.DeleteCommand         = new DelegateCommand <object>(DoDeleteCommand);
            this._dataServices         = dataServices;
            this._configurationService = configurationService;
            this._eventManager         = eventManager;
            this._eventManager.RegisterObserverToolBar(this);
            this.CurrentSaveImagePath = currentSaveImage;
            _regionManager            = regionManager;
            _states              = ToolbarStates.None;
            _noActiveValue       = string.Empty;
            this.IsSaveEnabled   = false;
            this.IsDeleteEnabled = false;
            this.IsNewEnabled    = false;
            ConfirmationRequest  = new InteractionRequest <IConfirmation>();
            Confirmation request = new Confirmation
            {
                Title   = "Confirmacion",
                Content = confirmDelete
            };

            ViewModelUri        = new Uri("karve://toolbar/viewmodel?id=" + UniqueId);
            ConfirmationCommand = new DelegateCommand(() =>
            {
                // string noActiveValue = configurationService.GetPrimaryKeyValue();
                request.Content = confirmDelete;
                ConfirmationRequest.Raise(request);
                if (request.Confirmed)
                {
                    string value   = string.Empty;
                    var singleView = _regionManager.Regions[RegionNames.TabRegion].ActiveViews.FirstOrDefault();
                    if (singleView != null)
                    {
                        var headerProp = singleView.GetType().GetProperty("Header");
                        if (headerProp != null)
                        {
                            if (headerProp.GetValue(singleView) is string header)
                            {
                                value = header.Split('.')[0];
                            }
                        }
                    }

                    DeleteCommand.Execute(value);
                }
            });
            SaveValueCommand = new DelegateCommand(() =>
            {
                request.Content = confirmSave;

                ConfirmationRequest.Raise(request);
                if (request.Confirmed)
                {
                    SaveCommand.Execute();
                }
                else
                {
                    this.CurrentSaveImagePath = KarveToolBarViewModel.currentSaveImage;
                }
            });

            AddValidationChain();
            _uniqueId = ObserverName + Guid.NewGuid();
        }
        private void DoDeleteCommand(object key)
        {
            var value = key as string;

            _states = ToolbarStates.Delete;
            DataPayLoad payLoad = new DataPayLoad
            {
                PayloadType     = DataPayLoad.Type.Delete,
                PrimaryKeyValue = value,
                Subsystem       = _activeSubSystem,
                PrimaryKey      = value,
                Sender          = ViewModelUri.ToString()
            };

            payLoad.PrimaryKeyValue = value;

            // 1. first of all we try to see if the active tab is a deleter view.

            var activeView = _regionManager.Regions[RegionNames.TabRegion].ActiveViews.FirstOrDefault();

            if (activeView is UserControl control)
            {
                if (control.DataContext is KarveViewModelBase baseViewModel)
                {
                    // The current view model has declarated that he is able to destroy himself.
                    // Looking forward for Hiroshima.

                    var isDeleter = baseViewModel.IsDeleter;
                    if (isDeleter)
                    {
                        baseViewModel.DeleteView(value);
                        // he destroy himself and communicate the stuff.
                        // no more work is needed.
                        return;
                    }
                }
            }

            /*
             * let's see if i am command base and i can still suicide me:
             * 1. If it is me.
             * 2. If my primary key is ok.
             */
            Uri uri = null;

            if (IsCommandBaseOnly(ref uri))
            {
                DoDeleteHimSelf(value, uri);
                return;
            }

            /*
             * I am not lucky. Nobody destroys himself.
             *
             */
            if (!string.IsNullOrEmpty(currentViewObjectId))
            {
                payLoad.ObjectPath = viewStack.Peek();

                _eventManager.SendMessage(currentViewObjectId, payLoad);
            }
            DeliverIncomingNotify(_activeSubSystem, payLoad);
        }
        private void DoDeleteCommand(object key)
        {
            var value = key as string;

            _states = ToolbarStates.Delete;
            DataPayLoad payLoad = new DataPayLoad
            {
                PayloadType     = DataPayLoad.Type.Delete,
                PrimaryKeyValue = value,
                Subsystem       = _activeSubSystem,
                PrimaryKey      = value,
                Sender          = ViewModelUri.ToString()
            };

            payLoad.PrimaryKeyValue = value;

            // 1. first of all we try to see if the active tab is a deleter view.

            var activeView = _regionManager.Regions[RegionNames.TabRegion].ActiveViews.FirstOrDefault();

            if (activeView is UserControl control)
            {
                if (control.DataContext is KarveViewModelBase baseViewModel)
                {
                    // The current view model has declarated that he is able to destroy himself.
                    // Looking forward for Hiroshima.

                    var isDeleter = baseViewModel.IsDeleter;
                    if (isDeleter)
                    {
                        baseViewModel.DeleteView(value);
                        // he destroy himself and communicate the stuff.
                        // no more work is needed.
                        return;
                    }
                }
            }

            /*
             * let's see if i am command base and i can still suicide me:
             * 1. If it is me.
             * 2. If my primary key is ok.
             */
            Uri uri = null;

            if (IsCommandBaseOnly(ref uri))
            {
                DoDeleteHimSelf(value, uri);
                return;
            }

            /* I am not lucky. Nobody destroys himself.
             *  On the top of the tsack it should be the last ctrated let's try to send a delete message to him.
             *  He will help to murder the right tab if he will be have a mailbox to receive my destruction message.
             *
             */
            if (!string.IsNullOrEmpty(currentViewObjectId))
            {
                payLoad.ObjectPath = viewStack.Peek();

                _eventManager.SendMessage(currentViewObjectId, payLoad);
            }
            // I dont trust anyone like Al Pacino in carlitos way..let's send the bomb to the active subsystem.
            // The baby to kill is the active window for sure we have a controller or himself.
            DeliverIncomingNotify(_activeSubSystem, payLoad);
        }
        /// <summary>
        /// KarveToolBarViewModel is a view model to modle the toolbar behaviour.
        /// </summary>
        /// <param name="dataServices"></param>
        /// <param name="eventManager"></param>
        /// <param name="careKeeper"></param>
        /// <param name="regionManager"></param>
        /// <param name="configurationService"></param>
        public KarveToolBarViewModel(IDataServices dataServices,
                                     IEventManager eventManager,
                                     ICareKeeperService careKeeper,
                                     IRegionManager regionManager,
                                     IConfigurationService configurationService)
        {
            this._dataServices         = dataServices;
            this._configurationService = configurationService;
            this._eventManager         = eventManager;
            this._eventManager.RegisterObserverToolBar(this);
            this._careKeeper           = careKeeper;
            this.SaveCommand           = new DelegateCommand(DoSaveCommand);
            this.NewCommand            = new DelegateCommand(DoNewCommand);
            this.DeleteCommand         = new DelegateCommand(DoDeleteCommand);
            this._dataServices         = dataServices;
            this._configurationService = configurationService;
            this._eventManager         = eventManager;
            this._eventManager.RegisterObserverToolBar(this);
            this.CurrentSaveImagePath = currentSaveImage;
            _regionManager            = regionManager;
            _states             = ToolbarStates.None;
            ConfirmationRequest = new InteractionRequest <IConfirmation>();
            Confirmation request = new Confirmation
            {
                Title   = "Confirmacion",
                Content = confirmDelete
            };

            Confirmed           = false;
            ConfirmationCommand = new DelegateCommand(() =>
            {
                string noActiveValue = configurationService.GetPrimaryKeyValue();
                if (string.IsNullOrEmpty(noActiveValue))
                {
                    InteractionRequest <INotification> ir = new InteractionRequest <INotification>();
                    Notification nt = new Notification
                    {
                        Content = "No puedo borrar la ficha de consulta",
                        Title   = "Error"
                    };
                    ir.Raise(nt);
                }
                else
                {
                    request.Content = confirmDelete;
                    ConfirmationRequest.Raise(request);
                    if (request.Confirmed)
                    {
                        DeleteCommand.Execute();
                        Confirmed = false;
                    }
                }
            });
            SaveValueCommand = new DelegateCommand(() =>
            {
                request.Content = confirmSave;

                ConfirmationRequest.Raise(request);
                if (request.Confirmed)
                {
                    SaveCommand.Execute();
                    Confirmed = false;
                }
                else
                {
                    this.CurrentSaveImagePath = KarveToolBarViewModel.currentSaveImage;
                }
            });

            SetInsertValidationChain();
            _uniqueId = ObserverName + Guid.NewGuid();
        }