public AddEditCalibrationComponentTypeViewModel(CalibrationComponentType mct)
        {
            mCalibrationComponentType = mct;

            OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanModifyConfig);
            CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);
        }
 public AddEditCalibrationComponentTypeDialog()
 {
     InitializeComponent();
     ComponentType = new CalibrationComponentType();
     model = new AddEditCalibrationComponentTypeViewModel(ComponentType);
     model.View = this;
     DataContext = model;
     Utils.ResetOriginalValues(this);
 }
        public AddEditExistingCalibrationComponentPropertyDialog(CalibrationComponentType pcpt)
        {
            InitializeComponent();

            AddEditExistingCalibrationComponentPropertyViewModel model = new AddEditExistingCalibrationComponentPropertyViewModel(pcpt);
            model.View = this;
            model.DataLoaded +=
                () =>
                {
                    DataContext = model;
                    Utils.ResetOriginalValues(this);
                };
        }
        public AddEditExistingCalibrationComponentPropertyViewModel(CalibrationComponentType calibrationEquipmentComponentType)
        {
            mComponentTypeId = calibrationEquipmentComponentType.Id;
            OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanModify);
            CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);

            Properties = new List<CalibrationProperty>();

            var getCalibrationPropertiesByComponentTypeTask = DatabaseLoader.GetCalibrationProperties(calibrationEquipmentComponentType.Id);
            var getCalibrationPropertiesTask = DatabaseLoader.GetCalibrationProperties();
            var getCalibrationEngineeringUnitsTask = DatabaseLoader.GetCalibrationEngineeringUnits();

            List<Task> tasks = new List<Task>();
            tasks.Add(getCalibrationPropertiesByComponentTypeTask);
            tasks.Add(getCalibrationPropertiesTask);
            tasks.Add(getCalibrationEngineeringUnitsTask);

            Task.Factory.ContinueWhenAll(tasks.ToArray(), xx =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    Units = getCalibrationEngineeringUnitsTask.Result;

                    List<int> listOfAssignedPropertyIds = new List<int>();
                    getCalibrationPropertiesByComponentTypeTask.Result.ForEach(x => listOfAssignedPropertyIds.Add(x.Id));

                    foreach (var componentProperty in getCalibrationPropertiesTask.Result)
                    {
                        if (!listOfAssignedPropertyIds.Contains(componentProperty.Id))
                        {
                            Properties.Add(componentProperty);
                        }
                    }
                    Properties = Properties.OrderBy(x => x.Name).ToList();

                    if (Properties.Count > 0)
                    {
                        SelectedProperty = Properties[0];
                    }
                    SelectedUnit = (from x in Units select x).FirstOrDefault();

                    DataLoaded();

                });
            });
        }
        private bool GetCalibrationComponentType(string typeName, CalibrationComponent newComponent, int rowIndex)
        {
            bool skipRow = false;

            //Check if the Area exist in the database
            CalibrationComponentType componentType = (from x in Cee.CalibrationComponentTypes where x.Name.Equals(typeName, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();

            if (componentType != null)
            {
                newComponent.CalibrationComponentType = componentType;
            }
            else
            {
                if (CanCreateProperties)
                {
                    string code = typeName.Replace(" ", "_").ToUpper();
                    componentType = new CalibrationComponentType { Name = typeName, Code = code, Description = typeName + " (created by importer)", IsActive = true };
                    newComponent.CalibrationComponentType = componentType;
                }
                else
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format("WorkSheet '{0}' Line '{1}': Could not find CalibrationComponentType '{2}' in database. Skipping this row.", WorkSheetName, rowIndex, typeName));
                    skipRow = true;
                }
            }

            return skipRow;
        }
        public DbOperationResult<CalibrationComponentType> AddCalibrationComponentType(CalibrationComponentType calibrationComponentType)
        {
            DbOperationResult<CalibrationComponentType> result = new DbOperationResult<CalibrationComponentType>();

            try
            {
                CalibrationComponentType newCalibrationComponentType = new CalibrationComponentType();

                using (CmsEntities cee = new CmsEntities())
                {
                    //Check if this component type already exist
                    CalibrationComponentType originalCalibrationComponentType = (from x in cee.CalibrationComponentTypes
                                                                                 where x.Id == calibrationComponentType.Id
                                                                                 select x).FirstOrDefault();

                    if (originalCalibrationComponentType != null)
                    {

                        int nameCount = (from x in cee.CalibrationComponentTypes where x.Name.ToLower() == calibrationComponentType.Name.ToLower() && x.Id!= calibrationComponentType.Id select x.Id).Count();
                        if (nameCount > 0)
                        {
                            throw new Exception(string.Format("The Calibration Component Type with Name '{0}' already exists.", calibrationComponentType.Name));
                        }

                        int codeCount = (from x in cee.CalibrationComponentTypes where x.Code.ToLower() == calibrationComponentType.Code.ToLower() && x.Id!= calibrationComponentType.Id select x.Id).Count();
                        if (codeCount > 0)
                        {
                            throw new Exception(string.Format("The Calibration Component Type with Code '{0}' already exists.", calibrationComponentType.Code));
                        }

                        //Edit the Component Type
                        originalCalibrationComponentType.Name = calibrationComponentType.Name;
                        originalCalibrationComponentType.Description = calibrationComponentType.Description;
                        originalCalibrationComponentType.Ordinal = calibrationComponentType.Ordinal;
                        cee.SaveChanges();
                    }
                    else
                    {
                        int nameCount = (from x in cee.CalibrationComponentTypes where x.Name.ToLower() == calibrationComponentType.Name.ToLower() select x.Id).Count();
                        if (nameCount > 0)
                        {
                            throw new Exception(string.Format("The Calibration Component Type with Name '{0}' already exists.", calibrationComponentType.Name));
                        }

                        int codeCount = (from x in cee.CalibrationComponentTypes where x.Code.ToLower() == calibrationComponentType.Code.ToLower() select x.Id).Count();
                        if (codeCount > 0)
                        {
                            throw new Exception(string.Format("The Calibration Component Type with Code '{0}' already exists.", calibrationComponentType.Code));
                        }

                        //Add new Component Type
                        originalCalibrationComponentType = new CalibrationComponentType();
                        originalCalibrationComponentType.Name = calibrationComponentType.Name;
                        originalCalibrationComponentType.Description = calibrationComponentType.Description;
                        originalCalibrationComponentType.Code = calibrationComponentType.Name.Replace(" ", "");
                        originalCalibrationComponentType.Ordinal = calibrationComponentType.Ordinal;
                        originalCalibrationComponentType.IsActive = true;

                        cee.CalibrationComponentTypes.Add(originalCalibrationComponentType);
                        cee.SaveChanges();
                    }

                    newCalibrationComponentType.Id = originalCalibrationComponentType.Id;
                    newCalibrationComponentType.Name = originalCalibrationComponentType.Name;
                    newCalibrationComponentType.Description = originalCalibrationComponentType.Description;
                    newCalibrationComponentType.Ordinal = originalCalibrationComponentType.Ordinal;

                    result.EntityResult = newCalibrationComponentType;
                    return result;
                }
            }
            catch (Exception ex)
            {
                result = BuildOperationalErrorResults<CalibrationComponentType>(ex);
                return result;
            }
        }