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

            if (nodeView.Type == NodeType.ComponentTypeGroup)
            {
                groupId = nodeView.GroupId;
                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 AddEditExistingControlSystemComponentPropertyDialog(e.Result, groupId);
                    dialog.Show();

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

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

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

                            var controlSystemEquipmentComponentTypeProperty = new ControlSystemComponentTypeProperty
                            {
                                ControlSystemComponentTypeId = controlSystemEquipmentComponentTypeId,
                                ControlSystemPropertyId = dialog.ControlSystemComponentTypeProperty.ControlSystemPropertyId,
                                ComponentTypeGroupId = dialog.ControlSystemComponentTypeProperty.ComponentTypeGroupId,
                                Ordinal = dialog.ControlSystemComponentTypeProperty.Ordinal,
                                GroupOrdinal = dialog.ControlSystemComponentTypeProperty.GroupOrdinal
                            };

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

                            cmsWebServiceClient.AddUpdateControlSystemComponentTypePropertyAsync(controlSystemEquipmentComponentTypeProperty);
                        }
                    };
                };
            cmsWebServiceClient.GetControlSystemComponentTypeAsync(controlSystemEquipmentComponentTypeId);
        }
        private ControlSystemPropertyValue GetPropertyValue(ControlSystemComponent controlSystemComponent,
            ControlSystemComponentTypeProperty controlSystemComponentProperty)
        {
            var propertyValue = (from x in controlSystemComponent.ControlSystemPropertyValues
                                 where x.ControlSystemPropertyId == controlSystemComponentProperty.ControlSystemPropertyId &&
                                       x.ControlSystemComponentId == controlSystemComponent.Id
                                 select x).FirstOrDefault();

            if (propertyValue == null && controlSystemComponentProperty.ControlSystemPropertyId.HasValue)
            {
                propertyValue = new ControlSystemPropertyValue
                    {
                        ControlSystemComponentId = controlSystemComponent.Id,
                        ControlSystemPropertyId = controlSystemComponentProperty.ControlSystemPropertyId.Value,
                        Value = controlSystemComponentProperty.ControlSystemProperty.DefaultValue
                    };

                controlSystemComponent.ControlSystemPropertyValues.Add(propertyValue);
            }
            return propertyValue;
        }
        private void EditControlSystemComponentTypeProperty(NodeView nodeView)
        {
            var dialog = new AddEditExistingControlSystemComponentPropertyDialog(nodeView.Id);
            dialog.Title = "Edit ControlSystem Component Property";

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

                        var controlSystemEquipmentComponentTypeProperty = new ControlSystemComponentTypeProperty
                        {
                            Id = dialog.ControlSystemComponentTypeProperty.Id,
                            ControlSystemComponentTypeId = dialog.ControlSystemComponentTypeProperty.ControlSystemComponentTypeId,
                            ControlSystemPropertyId = dialog.ControlSystemComponentTypeProperty.ControlSystemPropertyId,
                            Ordinal = dialog.ControlSystemComponentTypeProperty.Ordinal,
                            ComponentTypeGroupId = dialog.ControlSystemComponentTypeProperty.ComponentTypeGroupId,
                            GroupOrdinal = dialog.ControlSystemComponentTypeProperty.GroupOrdinal
                        };
                        cmsWebServiceClient.AddUpdateControlSystemComponentTypePropertyAsync(controlSystemEquipmentComponentTypeProperty);

                        var controlSystemComponentTypeEngineeringPropertiesNode = nodeView.Parent;
                        if (controlSystemComponentTypeEngineeringPropertiesNode.Type != NodeType.ControlSystemComponentTypeEngineeringProperties)
                        {
                            controlSystemComponentTypeEngineeringPropertiesNode = controlSystemComponentTypeEngineeringPropertiesNode.Parent;
                        }

                        if (dialog.GroupChanged)
                        {
                            //Group has changed, reload the Nodes
                            ReloadComponentTypeEquipmentProperties(CommonUtils.EquipmentPropertyType.ControlEngineeringProperty,
                                controlSystemComponentTypeEngineeringPropertiesNode, NodeType.ControlSystemComponentTypeEngineeringProperties);
                        }
                    }
                };
            dialog.Show();
        }
        public ControlSystemComponentTypeProperty AddUpdateControlSystemComponentTypeProperty(ControlSystemComponentTypeProperty csctp)
        {
            using (var cee = new CmsEntities())
            {

                ControlSystemComponentTypeProperty returnRow = null;

                if (csctp.Id > 0) //Updating existing
                {
                    ControlSystemComponentTypeProperty orginalRow = null;
                    orginalRow = (from x in cee.ControlSystemComponentTypeProperties where x.Id == csctp.Id select x).FirstOrDefault();

                    if (!csctp.ComponentTypeGroupId.HasValue && orginalRow.ComponentTypeGroupId.HasValue) //Group was set to null
                    {
                        var groups = (from x in cee.ControlSystemComponentTypeProperties
                                      where x.ControlSystemComponentTypeId == orginalRow.ControlSystemComponentTypeId &&
                                            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 go
                            //Therefore it's needed to add new empty group so we dont loose it
                            var newGroupRow = new ControlSystemComponentTypeProperty
                            {
                                ControlSystemPropertyId = null,
                                ControlSystemComponentTypeId = csctp.ControlSystemComponentTypeId,
                                ComponentTypeGroupId = orginalRow.ComponentTypeGroupId,
                                GroupOrdinal = 0,
                                Ordinal = 0,
                            };
                            cee.ControlSystemComponentTypeProperties.Add(newGroupRow);

                            //Remove group - this will set the property without group
                            orginalRow.ComponentTypeGroupId = null; //Remove Group
                            orginalRow.Ordinal = csctp.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 = csctp.Ordinal;
                            orginalRow.GroupOrdinal = 0;
                            returnRow = orginalRow;
                        }

                    }
                    else
                    {
                        var groups = (from x in cee.ControlSystemComponentTypeProperties
                                      where x.ControlSystemComponentTypeId == csctp.ControlSystemComponentTypeId &&
                                            x.ComponentTypeGroupId == csctp.ComponentTypeGroupId
                                      select x).ToList();

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

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

                            }

                            //Check if we are moving last property from group
                            if (orginalRow.ComponentTypeGroupId.HasValue)
                            {
                                var originalGroups = (from x in cee.ControlSystemComponentTypeProperties
                                                      where x.ControlSystemComponentTypeId == orginalRow.ControlSystemComponentTypeId &&
                                                            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 ControlSystemComponentTypeProperty
                                    {
                                        ControlSystemPropertyId = null,
                                        ControlSystemComponentTypeId = orginalRow.ControlSystemComponentTypeId,
                                        ComponentTypeGroupId = orginalRow.ComponentTypeGroupId,
                                        GroupOrdinal = 0,
                                        Ordinal = orginalRow.Ordinal,
                                    };
                                    cee.ControlSystemComponentTypeProperties.Add(newGroupRow);

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

                            orginalRow.ControlSystemPropertyId = csctp.ControlSystemPropertyId;
                            orginalRow.ControlSystemComponentTypeId = csctp.ControlSystemComponentTypeId;
                            orginalRow.ComponentTypeGroupId = csctp.ComponentTypeGroupId;
                            orginalRow.GroupOrdinal = csctp.GroupOrdinal;
                            orginalRow.Ordinal = groups[0].Ordinal;
                        }
                        else
                        {
                            //Just updating ordinals
                            orginalRow.GroupOrdinal = csctp.GroupOrdinal;
                            orginalRow.Ordinal = csctp.Ordinal;
                        }
                        returnRow = orginalRow;
                    }
                }
                else
                {
                    //Adding row

                    var groups = (from x in cee.ControlSystemComponentTypeProperties
                                  where x.ControlSystemComponentTypeId == csctp.ControlSystemComponentTypeId &&
                                        x.ComponentTypeGroupId == csctp.ComponentTypeGroupId
                                  select x).ToList();

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

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