private void AddExistingControlSystemComponentTypeTuningProperty(NodeView nodeView)
        {
            int? groupId = null;
            var controlSystemEquipmentComponentTypeId = -1;

            if (nodeView.Type == NodeType.ComponentTypeGroup)
            {
                groupId = nodeView.Id;
                controlSystemEquipmentComponentTypeId = nodeView.Parent.Parent.Id;
            }
            else
            {
                controlSystemEquipmentComponentTypeId = nodeView.Parent.Id;
            }

            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            cmsWebServiceClient.GetControlSystemComponentTypeCompleted +=
                (s, e) =>
                {
                    var dialog = new AddEditExistingControlSystemComponentTuningPropertyDialog(e.Result);
                    dialog.Show();

                    dialog.Closed += (s1, e1) =>
                    {
                        if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                        {
                            EventHandler<AddUpdateControlSystemComponentTypeTuningPropertyCompletedEventArgs> addCompleted = null;
                            addCompleted = (s2, eventArgs) =>
                            {
                                var pcpt = eventArgs.Result;

                                if (pcpt != null)
                                {
                                    var child = new NodeView(nodeView)
                                    {
                                        Id = pcpt.Id,
                                        Name = dialog.ControlSystemComponentTypeTuningProperty.ControlSystemTuningProperty.Name,
                                        Description = dialog.ControlSystemComponentTypeTuningProperty.ControlSystemTuningProperty.Description,
                                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                        Type = NodeType.ControlSystemComponentTypeTuningProperty,
                                        HasChildren = false,
                                        SortField = groupId.HasValue ? pcpt.GroupOrdinal.ToString() : pcpt.Ordinal.ToString()
                                    };
                                    if (nodeView.ChildrenLoaded)
                                    {
                                        nodeView.Children.Add(child);
                                        nodeView.Sort();
                                    }
                                }

                                cmsWebServiceClient.AddUpdateControlSystemComponentTypeTuningPropertyCompleted -= addCompleted;
                            };
                            cmsWebServiceClient.AddUpdateControlSystemComponentTypeTuningPropertyCompleted += addCompleted;

                            var systemComponentTypeTuningProperty = new ControlSystemComponentTypeTuningProperty
                            {
                                ControlSystemComponentTypeId = controlSystemEquipmentComponentTypeId,
                                ControlSystemTuningPropertyId = dialog.ControlSystemComponentTypeTuningProperty.ControlSystemTuningPropertyId,
                                ComponentTypeGroupId = groupId,
                                Ordinal = groupId.HasValue ? int.Parse(nodeView.SortField) : dialog.ControlSystemComponentTypeTuningProperty.Ordinal,
                                GroupOrdinal = groupId.HasValue ? dialog.ControlSystemComponentTypeTuningProperty.Ordinal : 0
                            };

                            cmsWebServiceClient.AddUpdateControlSystemComponentTypeTuningPropertyAsync(systemComponentTypeTuningProperty);
                        }
                    };
                };
            cmsWebServiceClient.GetControlSystemComponentTypeAsync(controlSystemEquipmentComponentTypeId);
        }
        private ControlSystemTuningPropertyValue GetPropertyValue(ControlSystemComponent controlSystemComponent,
            ControlSystemComponentTypeTuningProperty controlSystemComponentTuningProperty)
        {
            var propertyValue = (from x in controlSystemComponent.ControlSystemTuningPropertyValues
                                 where x.ControlSystemTuningPropertyId == controlSystemComponentTuningProperty.ControlSystemTuningPropertyId &&
                                       x.ControlSystemComponentId == controlSystemComponent.Id
                                 select x).FirstOrDefault();

            if (propertyValue == null && controlSystemComponentTuningProperty.ControlSystemTuningPropertyId.HasValue)
            {
                propertyValue = new ControlSystemTuningPropertyValue
                    {
                        ControlSystemComponentId = controlSystemComponent.Id,
                        ControlSystemTuningPropertyId = controlSystemComponentTuningProperty.ControlSystemTuningPropertyId.Value,
                        Value = controlSystemComponentTuningProperty.ControlSystemTuningProperty.DefaultValue,
                        Notes = controlSystemComponentTuningProperty.ControlSystemTuningProperty.Description
                    };

                controlSystemComponent.ControlSystemTuningPropertyValues.Add(propertyValue);
            }
            return propertyValue;
        }
        private void EditControlSystemComponentTypeTuningProperty(NodeView nodeView)
        {
            int? groupId = null;

            if (nodeView.Parent.Type == NodeType.ComponentTypeGroup)
            {
                groupId = nodeView.Parent.Id;
            }

            var dialog = new AddEditExistingControlSystemComponentTuningPropertyDialog(nodeView.Id, groupId);
            dialog.Title = "Edit ControlSystem Component Tuning Property";

            dialog.Closed +=
                (s1, e1) =>
                {
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
                        EventHandler<AddUpdateControlSystemComponentTypeTuningPropertyCompletedEventArgs> addCompleted = null;
                        addCompleted = (s2, eventArgs) =>
                        {
                            var pcpt = eventArgs.Result;
                            if (pcpt != null)
                            {
                                nodeView.Name = dialog.ControlSystemComponentTypeTuningProperty.ControlSystemTuningProperty.Name;
                                nodeView.Description = dialog.ControlSystemComponentTypeTuningProperty.ControlSystemTuningProperty.Description;
                                nodeView.SortField = groupId.HasValue ? dialog.ControlSystemComponentTypeTuningProperty.GroupOrdinal.ToString() :
                                    dialog.ControlSystemComponentTypeTuningProperty.Ordinal.ToString();
                            }
                            cmsWebServiceClient.AddUpdateControlSystemComponentTypeTuningPropertyCompleted -= addCompleted;
                            nodeView.Parent.Sort();
                        };
                        cmsWebServiceClient.AddUpdateControlSystemComponentTypeTuningPropertyCompleted += addCompleted;

                        var controlSystemEquipmentComponentTypeProperty = new ControlSystemComponentTypeTuningProperty
                        {
                            ControlSystemComponentTypeId = dialog.ControlSystemComponentTypeTuningProperty.ControlSystemComponentTypeId,
                            ControlSystemTuningPropertyId = dialog.ControlSystemComponentTypeTuningProperty.ControlSystemTuningPropertyId,
                            Ordinal = dialog.ControlSystemComponentTypeTuningProperty.Ordinal,
                            ComponentTypeGroupId = groupId,
                            GroupOrdinal = dialog.ControlSystemComponentTypeTuningProperty.GroupOrdinal
                        };
                        cmsWebServiceClient.AddUpdateControlSystemComponentTypeTuningPropertyAsync(controlSystemEquipmentComponentTypeProperty);
                    }
                };
            dialog.Show();
        }
        public ControlSystemComponentTypeTuningProperty AddUpdateControlSystemComponentTypeTuningProperty(ControlSystemComponentTypeTuningProperty csctp)
        {
            using (var cee = new CmsEntities())
            {
                //Check if this component property already exist
                var temp = (from x in cee.ControlSystemComponentTypeTuningProperties
                            where x.ControlSystemTuningPropertyId == csctp.ControlSystemTuningPropertyId &&
                                  x.ControlSystemComponentTypeId == csctp.ControlSystemComponentTypeId
                            select x).FirstOrDefault();

                if (temp == null)
                {
                    temp = new ControlSystemComponentTypeTuningProperty();
                    cee.ControlSystemComponentTypeTuningProperties.Add(temp);
                }

                temp.ControlSystemTuningPropertyId = csctp.ControlSystemTuningPropertyId;
                temp.ControlSystemComponentTypeId = csctp.ControlSystemComponentTypeId;
                temp.Ordinal = csctp.Ordinal;

                cee.SaveChanges();

                return temp;
            }
        }