private void BindLabel(CalibrationComponentTypeProperty calibrationComponentProperty, string path,int rowCount, int column, bool bindTooltip)
        {
            Label label = new Label();

            Binding labelBinding = new Binding(path)
            {
                Mode = BindingMode.OneTime,
                Source = calibrationComponentProperty
            };

            label.SetBinding(ContentControl.ContentProperty, labelBinding);
            label.Margin = new Thickness(1);

            if (bindTooltip)
            {
                Silverlight.Controls.ToolTips.ToolTip nameTip = new Silverlight.Controls.ToolTips.ToolTip
                {
                    Content = calibrationComponentProperty.CalibrationProperty.Name
                };
                ToolTipService.SetToolTip(label, nameTip);
            }

            PropertiesGrid.Children.Add(label);
            Grid.SetRow(label, rowCount);
            Grid.SetColumn(label, column);
        }
        private void AddExistingCalibrationComponentProperty(NodeView nodeView)
        {
            int calibrationEquipmentComponentTypeId = nodeView.Id;
            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            AddEditExistingCalibrationComponentPropertyDialog dialog;

            cmsWebServiceClient.GetCalibrationComponentTypeCompleted +=
                (s, e) =>
                {
                    dialog = new AddEditExistingCalibrationComponentPropertyDialog(e.Result);
                    dialog.Show();

                    dialog.Closed += (s1, e1) =>
                    {

                        if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                        {
                            EventHandler<AddUpdateCalibrationComponentTypePropertyCompletedEventArgs> addCompleted = null;
                            addCompleted = (s2, eventArgs) =>
                            {
                                CalibrationComponentTypeProperty pcpt = eventArgs.Result;

                                if (pcpt != null)
                                {
                                    NodeView child = new NodeView(nodeView)
                                    {
                                        Id = pcpt.Id,
                                        Name = dialog.CalibrationComponentTypeProperty.CalibrationProperty.Name,
                                        Description = dialog.CalibrationComponentTypeProperty.CalibrationProperty.Description,
                                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                        Type = NodeType.CalibrationComponentTypeProperty,
                                        HasChildren = false,
                                        SortField = dialog.CalibrationComponentTypeProperty.Ordinal.ToString()
                                    };
                                    if (nodeView.ChildrenLoaded)
                                    {
                                        nodeView.Children.Add(child);
                                        nodeView.Sort();
                                    }
                                }
                                cmsWebServiceClient.AddUpdateCalibrationComponentTypePropertyCompleted -= addCompleted;

                            };

                            cmsWebServiceClient.AddUpdateCalibrationComponentTypePropertyCompleted += addCompleted;

                            CalibrationComponentTypeProperty calibrationEquipmentComponentTypeProperty = new CalibrationComponentTypeProperty
                            {
                                CalibrationComponentTypeId = calibrationEquipmentComponentTypeId,
                                CalibrationPropertyId = dialog.CalibrationComponentTypeProperty.CalibrationPropertyId,
                                CalibrationEngineeringUnitId = dialog.CalibrationComponentTypeProperty.CalibrationEngineeringUnitId,
                                Ordinal = dialog.CalibrationComponentTypeProperty.Ordinal
                            };

                            cmsWebServiceClient.AddUpdateCalibrationComponentTypePropertyAsync(calibrationEquipmentComponentTypeProperty);
                        }
                    };
                };

            cmsWebServiceClient.GetCalibrationComponentTypeAsync(calibrationEquipmentComponentTypeId);
        }
        private void EditCalibrationComponentTypeProperty(NodeView nodeView)
        {
            AddEditExistingCalibrationComponentPropertyDialog dialog = new AddEditExistingCalibrationComponentPropertyDialog(nodeView.Id);
            dialog.Closed +=
                (s1, e1) =>
                {
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
                        EventHandler<AddUpdateCalibrationComponentTypePropertyCompletedEventArgs> addCompleted = null;
                        addCompleted = (s2, eventArgs) =>
                        {
                            CalibrationComponentTypeProperty pcpt = eventArgs.Result;

                            if (pcpt != null)
                            {
                                nodeView.Name = dialog.CalibrationComponentTypeProperty.CalibrationProperty.Name;
                                nodeView.Description = dialog.CalibrationComponentTypeProperty.CalibrationProperty.Description;
                                nodeView.SortField = dialog.CalibrationComponentTypeProperty.Ordinal.ToString();
                            }

                            cmsWebServiceClient.AddUpdateCalibrationComponentTypePropertyCompleted -= addCompleted;
                            nodeView.Parent.Sort();
                        };

                        cmsWebServiceClient.AddUpdateCalibrationComponentTypePropertyCompleted += addCompleted;

                        CalibrationComponentTypeProperty CalibrationEquipmentComponentTypeProperty = new CalibrationComponentTypeProperty
                        {
                            CalibrationComponentTypeId = dialog.CalibrationComponentTypeProperty.CalibrationComponentTypeId,
                            CalibrationPropertyId = dialog.CalibrationComponentTypeProperty.CalibrationPropertyId,
                            CalibrationEngineeringUnitId = dialog.CalibrationComponentTypeProperty.CalibrationEngineeringUnitId,
                            Ordinal = dialog.CalibrationComponentTypeProperty.Ordinal
                        };

                        cmsWebServiceClient.AddUpdateCalibrationComponentTypePropertyAsync(CalibrationEquipmentComponentTypeProperty);
                    }
                };
            dialog.Show();
        }
        private void BuildPropertyValues(CalibrationComponent componentIn, CalibrationComponentDataAdapter adapter)
        {
            foreach (CalibrationPropertyDto dto in adapter.PropertyValues)
            {

                CalibrationProperty property = (from x in mExistingProperties where x.Name.ToLower() == dto.PropertyName.ToLower() select x).FirstOrDefault();

                if (property == null)
                {
                    if (CanCreateProperties)
                    {
                        property = new CalibrationProperty { Name = dto.PropertyName, Type = "not used", Description = "(created by importer.)" };
                        mExistingProperties.Add(property);

                        //JOIN TABLE - joins the Property with the Type
                        CalibrationComponentTypeProperty typePropertyJoin = new CalibrationComponentTypeProperty();
                        typePropertyJoin.CalibrationComponentType = componentIn.CalibrationComponentType;

                        //EngineeringUnit
                        if (dto.NewUnit != null)
                        {
                            CalibrationEngineeringUnit unit = (from x in mExistingUnits where x.Name == dto.NewUnit.Name select x).FirstOrDefault();
                            if (unit == null)
                            {
                                typePropertyJoin.CalibrationEngineeringUnit = dto.NewUnit;
                            }
                            else
                            {
                                typePropertyJoin.CalibrationEngineeringUnit = unit;
                            }
                        }

                        property.CalibrationComponentTypeProperties.Add(typePropertyJoin);

                        mExistingProperties.Add(property);

                    }
                    else
                    {
                        RaiseMessage(CommonUtils.MessageType.Error, string.Format("WorkSheet '{0}' Line '{1} Tag {2} Component Name {3}' : The property does not exist.",
                            WorkSheetName, adapter.RowNumber, adapter.Tag, adapter.CalibrationName));
                        continue;
                    }
                }
                else
                {

                    //do we have a record in the join table for this property and comp type?
                    CalibrationComponentTypeProperty equipmentComponentTypeProperty =
                        (from x in Cee.CalibrationComponentTypeProperties
                         where x.CalibrationComponentType.Name.ToLower() == componentIn.CalibrationComponentType.Name.ToLower()
                         && x.CalibrationProperty.Name.ToLower() == property.Name.ToLower()
                         select x).FirstOrDefault();

                    if (equipmentComponentTypeProperty == null)
                    {
                        ////no row in join table - so create one.
                        CalibrationComponentTypeProperty typePropertyJoin = new CalibrationComponentTypeProperty();

                        if (dto.NewUnit != null)
                        {
                            CalibrationEngineeringUnit unit = (from x in mExistingUnits where x.Name == dto.NewUnit.Name select x).FirstOrDefault();

                            if (unit == null)
                            {
                                typePropertyJoin.CalibrationEngineeringUnit = dto.NewUnit;
                            }
                            else
                            {
                                typePropertyJoin.CalibrationEngineeringUnit = unit;
                            }
                        }

                        typePropertyJoin.CalibrationComponentType = componentIn.CalibrationComponentType;
                        property.CalibrationComponentTypeProperties.Add(typePropertyJoin);
                    }

                }

                CalibrationComponentPropertyValue propertyValue = (from x in Cee.CalibrationComponentPropertyValues
                                                                   where x.CalibrationComponentId == componentIn.Id
                                                                   && x.CalibrationPropertyId == property.Id
                                                                   select x).FirstOrDefault();
                if (propertyValue == null)
                {
                    propertyValue = new CalibrationComponentPropertyValue {CalibrationComponent = componentIn, CalibrationProperty = property};
                    property.Type = "Text";//not used but not nullable
                    property.EnableAsFound = false; //not used but not nullable
                }

                //Set AsFoundValue
                if (!string.IsNullOrEmpty(dto.AsFoundValue))
                {
                    propertyValue.Value = dto.AsFoundValue;
                }

                //Set AsLeftValue
                if (!string.IsNullOrEmpty(dto.AsLeftValue))
                {
                    propertyValue.Value2 = dto.AsLeftValue;
                }

                componentIn.CalibrationComponentPropertyValues.Add(propertyValue);

            }
        }
        public CalibrationComponentTypeProperty AddUpdateCalibrationComponentTypeProperty(CalibrationComponentTypeProperty ictp)
        {
            using (CmsEntities cee = new CmsEntities())
            {
                //Check if this component property already exist
                CalibrationComponentTypeProperty temp = (from x in cee.CalibrationComponentTypeProperties
                                                         where x.CalibrationPropertyId == ictp.CalibrationPropertyId &&
                                                               x.CalibrationComponentTypeId == ictp.CalibrationComponentTypeId
                                                         select x).FirstOrDefault();

                if (temp == null)
                {
                    temp = new CalibrationComponentTypeProperty();
                    cee.CalibrationComponentTypeProperties.Add(temp);
                }

                temp.CalibrationPropertyId = ictp.CalibrationPropertyId;
                temp.CalibrationComponentTypeId = ictp.CalibrationComponentTypeId;
                temp.CalibrationEngineeringUnitId = ictp.CalibrationEngineeringUnitId;
                temp.Ordinal = ictp.Ordinal;

                cee.SaveChanges();

                return temp;
            }
        }
        public CalibrationComponentTypeProperty AddCalibrationComponentTypeProperty(CalibrationComponentTypeProperty mecp)
        {
            using (CmsEntities cee = new CmsEntities())
            {
                //Check if this component property already exist
                CalibrationComponentTypeProperty temp = (from x in cee.CalibrationComponentTypeProperties
                                                         where x.CalibrationPropertyId == mecp.CalibrationPropertyId &&
                                                               x.CalibrationComponentTypeId == mecp.CalibrationComponentTypeId
                                                         select x).FirstOrDefault();

                if (temp == null)
                {
                    //Add new Component Type
                    temp = new CalibrationComponentTypeProperty();
                    temp.CalibrationPropertyId = mecp.CalibrationPropertyId;
                    temp.CalibrationComponentTypeId = mecp.CalibrationComponentTypeId;
                    temp.Ordinal = mecp.Ordinal;

                    cee.CalibrationComponentTypeProperties.Add(temp);
                    cee.SaveChanges();
                }
                else
                {
                    temp.Ordinal = mecp.Ordinal;
                    cee.SaveChanges();
                }
                return temp;
            }
        }
        private CalibrationComponentPropertyValue GetCalibrationComponentPropertyValue(CalibrationComponent calibrationComponent, CalibrationComponentTypeProperty calibrationComponentProperty)
        {
            var propertyValue = (from x in calibrationComponent.CalibrationComponentPropertyValues
                                 where x.CalibrationPropertyId == calibrationComponentProperty.CalibrationPropertyId &&
                                 x.CalibrationComponentId == calibrationComponent.Id
                                 select x).FirstOrDefault();

            if (propertyValue == null)
            {
                propertyValue = new CalibrationComponentPropertyValue
                    {
                        CalibrationComponentId = calibrationComponent.Id,
                        CalibrationPropertyId = calibrationComponentProperty.CalibrationPropertyId,
                        Value = calibrationComponentProperty.CalibrationProperty.DefaultValue, //As Found
                        Value2 = calibrationComponentProperty.CalibrationProperty.DefaultValue //As Left
                    };
                calibrationComponent.CalibrationComponentPropertyValues.Add(propertyValue);
            }

            return propertyValue;
        }