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 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();
        }