예제 #1
0
        public InstrumentEditViewModel(IDataService <LabDbEntities> labDbdata,
                                       IEventAggregator aggregator,
                                       InstrumentService instrumentService) : base()
        {
            _labDbData         = labDbdata;
            _editMode          = false;
            _eventAggregator   = aggregator;
            _instrumentService = instrumentService;

            AreaList           = _labDbData.RunQuery(new InstrumentUtilizationAreasQuery()).ToList();
            InstrumentTypeList = _labDbData.RunQuery(new InstrumentTypesQuery()).ToList();
            ManufacturerList   = _labDbData.RunQuery(new OrganizationsQuery()
            {
                Role = OrganizationsQuery.OrganizationRoles.Manufacturer
            })
                                 .ToList();
            CalibrationLabList = _labDbData.RunQuery(new OrganizationsQuery()
            {
                Role = OrganizationsQuery.OrganizationRoles.CalibrationLab
            })
                                 .ToList();

            AddCalibrationCommand = new DelegateCommand(
                () =>
            {
                _instrumentService.ShowNewCalibrationDialog(_instance);
            },
                () => IsInstrumentAdmin);

            AddFileCommand = new DelegateCommand(
                () =>
            {
                OpenFileDialog fileDialog = new OpenFileDialog
                {
                    Multiselect = true
                };

                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    IEnumerable <string> fileList = fileDialog.FileNames;

                    _instance.AddFiles(fileList);
                    RaisePropertyChanged("FileList");
                }
            });

            AddMaintenanceEventCommand = new DelegateCommand(
                () =>
            {
                _instrumentService.ShowNewMaintenanceDialog(_instance);
                RaisePropertyChanged("MaintenanceEventList");
            },
                () => IsInstrumentAdmin);

            AddMethodAssociationCommand = new DelegateCommand(
                () =>
            {
                _instance.AddMethodAssociation(_selectedUnassociated);
                SelectedUnassociatedMethod = null;
                RaisePropertyChanged("AssociatedMethods");
                RaisePropertyChanged("UnassociatedMethods");
            },
                () => IsInstrumentAdmin && _selectedUnassociated != null);

            AddPropertyCommand = new DelegateCommand(
                () =>
            {
                Views.AddPropertyDialog propertyDialog = new Views.AddPropertyDialog();
                if (propertyDialog.ShowDialog() == true)
                {
                    InstrumentMeasurableProperty newIMP = new InstrumentMeasurableProperty()
                    {
                        CalibrationRangeLowerLimit = 0,
                        CalibrationRangeUpperLimit = 0,
                        Description          = "",
                        InstrumentID         = _instance.ID,
                        MeasurableQuantityID = propertyDialog.QuantityInstance.ID,
                        RangeLowerLimit      = 0,
                        RangeUpperLimit      = 0,
                        Resolution           = 0,
                        TargetUncertainty    = 0,
                        UnitID = propertyDialog.QuantityInstance.GetMeasurementUnits().First().ID
                    };

                    newIMP.Create();
                    RaisePropertyChanged("InstrumentMeasurablePropertyList");
                }
            },
                () => IsInstrumentAdmin);

            OpenFileCommand = new DelegateCommand(
                () =>
            {
                try
                {
                    System.Diagnostics.Process.Start(_selectedFile.Path);
                }
                catch (Exception)
                {
                    _eventAggregator.GetEvent <StatusNotificationIssued>().Publish("File non trovato");
                }
            },
                () => _selectedFile != null);

            RemoveFileCommand = new DelegateCommand(
                () =>
            {
                _selectedFile.Delete();

                RaisePropertyChanged("FileList");
                SelectedFile = null;
            },
                () => _selectedFile != null);

            RemoveMethodAssociationCommand = new DelegateCommand(
                () =>
            {
                _instance.RemoveMethodAssociation(_selectedAssociated);
                SelectedAssociatedMethod = null;
                RaisePropertyChanged("AssociatedMethods");
                RaisePropertyChanged("UnassociatedMethods");
            },
                () => IsInstrumentAdmin && _selectedAssociated != null);

            SaveCommand = new DelegateCommand(
                () =>
            {
                _instance.Update();
                foreach (InstrumentMeasurablePropertyWrapper impw in InstrumentMeasurablePropertyList.Where(imp => imp.IsModified))
                {
                    impw.PropertyInstance.Update();
                }

                EditMode = false;
                _eventAggregator.GetEvent <InstrumentListUpdateRequested>()
                .Publish();
            },
                () => _editMode);

            StartEditCommand = new DelegateCommand(
                () =>
            {
                EditMode = true;
            },
                () => IsInstrumentAdmin && !_editMode);

            #region Event Subscriptions

            _eventAggregator.GetEvent <MaintenanceEventCreated>()
            .Subscribe(
                maint =>
            {
                if (maint.InstrumentID == _instance?.ID)
                {
                    RaisePropertyChanged("MaintenanceEventList");
                }
            });

            _eventAggregator.GetEvent <CalibrationIssued>()
            .Subscribe(
                report =>
            {
                if (report.instrumentID == _instance?.ID)
                {
                    RaisePropertyChanged("CalibrationReportList");
                }
            });

            #endregion Event Subscriptions
        }