private void AddExistingControlSystemComponentTypeAlarmProperty(NodeView nodeView)
        {
            var controlSystemEquipmentComponentTypeId = nodeView.Parent.Id;
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

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

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

                                if (eventArgs.Result.HasErrors)
                                {
                                    var popup = new PopupDialog(PopupDialogType.Error, Utils.DisplayErrorMessages(eventArgs.Result.ServerErrorMessages));
                                    popup.Show();
                                    return;
                                }

                                if (entityResult != null)
                                {
                                    var child = new NodeView(nodeView)
                                    {
                                        Id = entityResult.Id,
                                        Name = dialog.ControlSystemComponentTypeAlarmProperty.ControlSystemAlarmProperty.Name,
                                        Description = dialog.ControlSystemComponentTypeAlarmProperty.ControlSystemAlarmProperty.Description,
                                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                        Type = NodeType.ControlSystemComponentTypeAlarmProperty,
                                        HasChildren = false,
                                        SortField = entityResult.Ordinal.ToString()
                                    };
                                    if (nodeView.ChildrenLoaded)
                                    {
                                        nodeView.Children.Add(child);
                                        nodeView.Sort();
                                    }
                                }

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

                            var systemComponentTypeTuningProperty = new ControlSystemComponentTypeAlarmProperty
                            {
                                ComponentTypeId = controlSystemEquipmentComponentTypeId,
                                AlarmPropertyId = dialog.ControlSystemComponentTypeAlarmProperty.AlarmPropertyId,
                                Ordinal = dialog.ControlSystemComponentTypeAlarmProperty.Ordinal
                            };

                            cmsWebServiceClient.SaveControlSystemComponentTypeAlarmPropertyAsync(systemComponentTypeTuningProperty);
                        }
                    };
                };
            cmsWebServiceClient.GetControlSystemComponentTypeAsync(controlSystemEquipmentComponentTypeId);
        }
        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 EditControlSystemComponentType(NodeView nodeView)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            EventHandler<GetControlSystemComponentTypeCompletedEventArgs> getControlSystemComponentCompleted = null;
            getControlSystemComponentCompleted = (s, eventArgs) =>
            {
                var mc = eventArgs.Result;

                var dialog = new AddEditControlSystemComponentTypeDialog(mc);
                dialog.Show();
                dialog.Closed += (s1, e1) =>
                {
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        EventHandler<AddControlSystemComponentTypeCompletedEventArgs> addCompleted = null;
                        addCompleted = (s2, e2) =>
                        {
                            var controlSystemEquipmentComponentType = e2.Result;
                            nodeView.Name = controlSystemEquipmentComponentType.Name;
                            nodeView.Description = controlSystemEquipmentComponentType.Description;
                            nodeView.SortField = controlSystemEquipmentComponentType.Ordinal.ToString();

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

                        cmsWebServiceClient.AddControlSystemComponentTypeCompleted += addCompleted;
                        cmsWebServiceClient.AddControlSystemComponentTypeAsync(dialog.ComponentType);
                    }
                };
                cmsWebServiceClient.GetControlSystemComponentTypeCompleted -= getControlSystemComponentCompleted;
            };
            cmsWebServiceClient.GetControlSystemComponentTypeCompleted += getControlSystemComponentCompleted;
            cmsWebServiceClient.GetControlSystemComponentTypeAsync(nodeView.Id);
        }