private void EditComponentTypeTest(NodeView nodeView)
        {
            var dialog = new AddEditExistingComponentTypeTestingPropertyDialog(nodeView.Id);
            dialog.Title = "Edit Component Type Testing Property";

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

                        var controlSystemEquipmentComponentTypeProperty = new ControlSystemComponentTypeTestingProperty
                        {
                            Id = dialog.ComponentTypeTestingProperty.Id,
                            ComponentTypeId = dialog.ComponentTypeTestingProperty.ComponentTypeId,
                            TestPropertyId = dialog.ComponentTypeTestingProperty.TestPropertyId,
                            Ordinal = dialog.ComponentTypeTestingProperty.Ordinal,
                            ComponentTypeGroupId = dialog.ComponentTypeTestingProperty.ComponentTypeGroupId,
                            GroupOrdinal = dialog.ComponentTypeTestingProperty.GroupOrdinal
                        };
                        cmsWebServiceClient.AddUpdateControlSystemComponentTypeTestingPropertyAsync(controlSystemEquipmentComponentTypeProperty);

                        var controlSystemTypeTestingPropertiesNode = nodeView.Parent;
                        if (controlSystemTypeTestingPropertiesNode.Type != NodeType.ComponentTypeTests)
                        {
                            controlSystemTypeTestingPropertiesNode = controlSystemTypeTestingPropertiesNode.Parent;
                        }

                        if (dialog.GroupChanged)
                        {
                            //Group has changed, reload the Nodes
                            ReloadComponentTypeEquipmentProperties(CommonUtils.EquipmentPropertyType.SystemTestingProperty,
                                controlSystemTypeTestingPropertiesNode, NodeType.ComponentTypeTests);
                        }
                    }
                };
            dialog.Show();
        }
        private void AddExistingComponentTypeTest(NodeView nodeView)
        {
            int? groupId = null;
            var componentTypeId = -1;

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

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

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

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

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

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

                            var controlSystemEquipmentComponentTypeProperty = new ControlSystemComponentTypeTestingProperty
                            {
                                ComponentTypeId = componentTypeId,
                                TestPropertyId = dialog.ComponentTypeTestingProperty.TestPropertyId,
                                ComponentTypeGroupId = dialog.ComponentTypeTestingProperty.ComponentTypeGroupId,
                                Ordinal = dialog.ComponentTypeTestingProperty.Ordinal,
                                GroupOrdinal = dialog.ComponentTypeTestingProperty.GroupOrdinal
                            };

                            if (dialog.GroupChanged)
                            {
                                //Group has changed, reload the Nodes
                                ReloadComponentTypeEquipmentProperties(CommonUtils.EquipmentPropertyType.SystemTestingProperty, nodeView, NodeType.ComponentTypeTests);
                            }

                            cmsWebServiceClient.AddUpdateControlSystemComponentTypeTestingPropertyAsync(controlSystemEquipmentComponentTypeProperty);
                        }
                    };
                };
            cmsWebServiceClient.GetControlSystemComponentTypeAsync(componentTypeId);
        }
        public ControlSystemComponentTypeTestingProperty AddUpdateControlSystemComponentTypeTestingProperty(ControlSystemComponentTypeTestingProperty csctep)
        {
            using (var cee = new CmsEntities())
            {

                ControlSystemComponentTypeTestingProperty returnRow = null;

                if (csctep.Id > 0) //Updating existing
                {
                    ControlSystemComponentTypeTestingProperty orginalRow = null;
                    orginalRow = (from x in cee.ControlSystemComponentTypeTestingProperties where x.Id == csctep.Id select x).FirstOrDefault();

                    if (!csctep.ComponentTypeGroupId.HasValue && orginalRow.ComponentTypeGroupId.HasValue) //Group was set to null
                    {
                        var groups = (from x in cee.ControlSystemComponentTypeTestingProperties
                                      where x.ComponentTypeId == orginalRow.ComponentTypeId &&
                                            x.ComponentTypeGroupId == orginalRow.ComponentTypeGroupId
                                      select x).ToList();

                        if (groups.Count == 1) //this means there is only one group and we are removing a property from it
                        {
                            //This is a last property for this group
                            //When we remove it, it will be gone
                            //Therefore it's needed to add new empty group so we dont loose it
                            var newGroupRow = new ControlSystemComponentTypeTestingProperty
                            {
                                TestPropertyId = null,
                                ComponentTypeId = csctep.ComponentTypeId,
                                ComponentTypeGroupId = orginalRow.ComponentTypeGroupId,
                                GroupOrdinal = 0,
                                Ordinal = 0,
                            };
                            cee.ControlSystemComponentTypeTestingProperties.Add(newGroupRow);

                            //Remove group - this will set the property without group
                            orginalRow.ComponentTypeGroupId = null; //Remove Group
                            orginalRow.Ordinal = csctep.Ordinal;
                            orginalRow.GroupOrdinal = 0;
                            returnRow = orginalRow;
                        }
                        else
                        {
                            //This is not last property in the group - simply set group to null to remove it
                            orginalRow.ComponentTypeGroupId = null; //Remove Group
                            orginalRow.Ordinal = csctep.Ordinal;
                            orginalRow.GroupOrdinal = 0;
                            returnRow = orginalRow;
                        }

                    }
                    else
                    {
                        var groups = (from x in cee.ControlSystemComponentTypeTestingProperties
                                      where x.ComponentTypeId == csctep.ComponentTypeId &&
                                            x.ComponentTypeGroupId == csctep.ComponentTypeGroupId
                                      select x).ToList();

                        if (csctep.ComponentTypeGroupId != orginalRow.ComponentTypeGroupId)
                        {
                            //Moving property to different group

                            //If moving property to empty group
                            if (groups.Count == 1 && groups[0].TestPropertyId == null)
                            {
                                //Remove empty Group as the current we will set he GroupId on the originalRow
                                cee.ControlSystemComponentTypeTestingProperties.Remove(groups[0]);

                            }

                            //Check if we are moving last property from group
                            if (orginalRow.ComponentTypeGroupId.HasValue)
                            {
                                var originalGroups = (from x in cee.ControlSystemComponentTypeTestingProperties
                                                      where x.ComponentTypeId == orginalRow.ComponentTypeId &&
                                                            x.ComponentTypeGroupId == orginalRow.ComponentTypeGroupId
                                                      select x).ToList();

                                if (originalGroups.Count == 1)
                                {
                                    //the property that we are moving is last in the group

                                    var newGroupRow = new ControlSystemComponentTypeTestingProperty
                                    {
                                        TestPropertyId = null,
                                        ComponentTypeId = orginalRow.ComponentTypeId,
                                        ComponentTypeGroupId = orginalRow.ComponentTypeGroupId,
                                        GroupOrdinal = 0,
                                        Ordinal = orginalRow.Ordinal,
                                    };
                                    cee.ControlSystemComponentTypeTestingProperties.Add(newGroupRow);

                                }
                            }
                            else
                            {
                                //make sure the ordinal of the group stays the same
                                if (groups.Any()) orginalRow.Ordinal = groups[0].Ordinal;
                            }

                            orginalRow.TestPropertyId = csctep.TestPropertyId;
                            orginalRow.ComponentTypeId = csctep.ComponentTypeId;
                            orginalRow.ComponentTypeGroupId = csctep.ComponentTypeGroupId;
                            orginalRow.GroupOrdinal = csctep.GroupOrdinal;
                            orginalRow.Ordinal = groups[0].Ordinal;
                        }
                        else
                        {
                            //Just updating ordinals
                            orginalRow.GroupOrdinal = csctep.GroupOrdinal;
                            orginalRow.Ordinal = csctep.Ordinal;
                        }
                        returnRow = orginalRow;
                    }
                }
                else
                {
                    //Adding row

                    //Check if adding a property to empty group
                    var groups = (from x in cee.ControlSystemComponentTypeTestingProperties
                                  where x.ComponentTypeId == csctep.ComponentTypeId &&
                                        x.ComponentTypeGroupId == csctep.ComponentTypeGroupId
                                  select x).ToList();

                    //Check if adding a property to empty group
                    if (groups.Count == 1 && groups[0].TestPropertyId.HasValue == false)
                    {
                        var emptyGroup = groups[0];
                        //adding a property to empty group so update just the row
                        emptyGroup.TestPropertyId = csctep.TestPropertyId;
                        emptyGroup.GroupOrdinal = csctep.Ordinal;
                        returnRow = emptyGroup;
                    }
                    else
                    {
                        //Adding row to a Group
                        var temp = new ControlSystemComponentTypeTestingProperty();
                        cee.ControlSystemComponentTypeTestingProperties.Add(temp);

                        temp.TestPropertyId = csctep.TestPropertyId;
                        temp.ComponentTypeId = csctep.ComponentTypeId;
                        temp.ComponentTypeGroupId = csctep.ComponentTypeGroupId;
                        if (csctep.ComponentTypeGroupId.HasValue)
                        {
                            temp.GroupOrdinal = csctep.GroupOrdinal;
                            temp.Ordinal = groups.Any() ? groups[0].Ordinal : 1;
                        }
                        else
                        {
                            temp.GroupOrdinal = 0;
                            temp.Ordinal = csctep.Ordinal;
                        }
                        returnRow = temp;
                    }
                }
                cee.SaveChanges();
                return returnRow;
            }
        }