private void AddElectricalComponentProperty(NodeView nodeView)
        {
            AddEditEquipmentPropertyDialog dialog = new AddEditEquipmentPropertyDialog(CommonUtils.EquipmentPropertyType.ElectricalProperty);
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value && dialog.ComponentProperty!=null)
                    {
                        NodeView child = new NodeView(nodeView)
                        {
                            Id = dialog.ComponentProperty.Id,
                            Name = dialog.ComponentProperty.Name,
                            Description = dialog.ComponentProperty.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.ElectricalProperty,
                            HasChildren = false,
                            SortField = dialog.ComponentProperty.Name
                        };
                        if (nodeView.ChildrenLoaded)
                        {
                            nodeView.Children.Add(child);
                            nodeView.Sort();
                        }
                    }
                };
        }
        private void AddNewDocumentAuthor(NodeView nodeView)
        {
            AddEditDocumentAuthorDialog dialog = new AddEditDocumentAuthorDialog();
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        NodeView child = new NodeView(nodeView)
                        {
                            Id = dialog.DocumentAuthor.Id,
                            Name = dialog.DocumentAuthor.Name,
                            Description = dialog.DocumentAuthor.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.DocumentAuthorNode,
                            SortField = dialog.DocumentAuthor.Ordinal.ToString(),
                            HasChildren = false
                        };
                         if (nodeView.ChildrenLoaded)
                        {
                            nodeView.Children.Add(child);
                            nodeView.Sort();
                        }

                    }
                };
        }
        private void AddInterlockProperty(NodeView nodeView)
        {
            var dialog = new AddEditEquipmentPropertyDialog(CommonUtils.EquipmentPropertyType.ControlInterlockProperty);
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    var parentNode = nodeView;
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        var child = new NodeView(parentNode)
                        {
                            Id = dialog.ComponentProperty.Id,
                            Name = dialog.ComponentProperty.Name,
                            Description = dialog.ComponentProperty.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.InterlockProperty,
                            HasChildren = false,
                            SortField = dialog.ComponentProperty.Name
                        };
                        if (nodeView.ChildrenLoaded)
                        {
                            parentNode.Children.Add(child);
                            nodeView.Sort();
                        }
                    }
                };
        }
        private void AddExistingInterlockProperty(NodeView nodeView)
        {
            var interlockTypeId = nodeView.Id;
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            cmsWebServiceClient.GetInterlockTypeCompleted +=
                (s, e) =>
                {
                    var addEditExistingInterlockProperty = new AddEditExistingInterlockPropertyDialog(e.Result);
                    addEditExistingInterlockProperty.Show();

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

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

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

                            var interlockTypeProperty = new InterlockTypeProperty
                            {
                                InterlockTypeId = interlockTypeId,
                                InterlockPropertyId = addEditExistingInterlockProperty.InterlockTypeProperty.InterlockPropertyId,
                                Ordinal = addEditExistingInterlockProperty.InterlockTypeProperty.Ordinal
                            };

                            cmsWebServiceClient.AddUpdateInterlockTypePropertyAsync(interlockTypeProperty);
                        }
                    };
                };
            cmsWebServiceClient.GetInterlockTypeAsync(interlockTypeId);
        }
        private void LoadSystemTests(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetAllControlSystemTestingPropertiesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var properties = eventArgs.Result;
                if (properties != null)
                {
                    foreach (var property in properties)
                    {
                        var child = new NodeView(expandedNode)
                        {
                            Id = property.Id,
                            Name = property.Name,
                            Description = property.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.SystemTest,
                            HasChildren = false,
                            SortField = property.Name
                        };
                        expandedNode.Children.Add(child);
                        cmsWebServiceClient.GetAllControlSystemTestingPropertiesCompleted -= fetchCompleted;
                    }
                    expandedNode.Sort();
                }
            };
            cmsWebServiceClient.GetAllControlSystemTestingPropertiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetAllControlSystemTestingPropertiesAsync();
        }
        private void LoadManufacturers(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetManufacturersCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var manufacturers = eventArgs.Result;
                foreach (var manufacturer in manufacturers)
                {
                    var child = new NodeView(expandedNode)
                    {
                        Id = manufacturer.Id,
                        Name = manufacturer.Name,
                        Description = manufacturer.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.ManufacturerNode,
                        HasChildren = true,
                        SortField = manufacturer.Ordinal.ToString()
                    };
                    expandedNode.Children.Add(child);
                }
                Utils.HideSpinner(expandedNode);

                cmsWebServiceClient.GetManufacturersCompleted -= fetchCompleted;
                expandedNode.Sort();
            };
            cmsWebServiceClient.GetManufacturersCompleted += fetchCompleted;
            cmsWebServiceClient.GetManufacturersAsync((int)CommonUtils.EquipmentTypeCode.CONTR);
        }
        private void LoadInterlockTypeProperties(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetInterlockTypePropertiesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var interlockTypeProperties = eventArgs.Result;
                foreach (var interlockTypeProperty in interlockTypeProperties)
                {
                    var desc = interlockTypeProperty.InterlockProperty.Name;
                    if (!string.IsNullOrEmpty(interlockTypeProperty.InterlockProperty.Description))
                    {
                        desc = string.Format("{0} ({1})", interlockTypeProperty.InterlockProperty.Name,
                            interlockTypeProperty.InterlockProperty.Description);
                    }

                    var child = new NodeView(expandedNode)
                    {
                        Id = interlockTypeProperty.Id,
                        Name = desc,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.InterlockTypeProperty,
                        HasChildren = false,
                        SortField = interlockTypeProperty.Ordinal.ToString()
                    };
                    expandedNode.Children.Add(child);
                }
                Utils.HideSpinner(expandedNode);
                expandedNode.Sort();

                cmsWebServiceClient.GetInterlockTypePropertiesCompleted -= fetchCompleted;
            };

            cmsWebServiceClient.GetInterlockTypePropertiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetInterlockTypePropertiesAsync(expandedNode.Id);
        }
        private void LoadControlSystemTypes(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetControlSystemTypesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var equipmentTypes = eventArgs.Result;
                foreach (var equipmentType in equipmentTypes)
                {
                    var systemTypeNode = new NodeView(expandedNode)
                    {
                        Id = equipmentType.Id,
                        Name = equipmentType.Name,
                        Description = equipmentType.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.ControlSystemTypeNode,
                        HasChildren = true,
                        SortField = equipmentType.Name
                    };

                    var systemTestsNode = new NodeView(systemTypeNode)
                    {
                        Name = "System Type Tests",
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.SystemTypeTests,
                        HasChildren = true
                    };

                    expandedNode.Children.Add(systemTypeNode);
                    systemTypeNode.Children.Add(systemTestsNode);

                    Utils.HideSpinner(expandedNode);
                    Utils.HideSpinner(systemTestsNode);
                }

                cmsWebServiceClient.GetControlSystemTypesCompleted -= fetchCompleted;
                expandedNode.Sort();
            };
            cmsWebServiceClient.GetControlSystemTypesCompleted += fetchCompleted;
            cmsWebServiceClient.GetControlSystemTypesAsync(false);
        }
        private void AddType(NodeView nodeView)
        {
            AddEditMobilePlantTypeDialog dialog = new AddEditMobilePlantTypeDialog();
            AddEditMobilePlantTypeModel addEditMobilePlantTypeModel = new AddEditMobilePlantTypeModel();
            addEditMobilePlantTypeModel.View = dialog;
            dialog.DataContext = addEditMobilePlantTypeModel;
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    NodeView parentNode = nodeView;
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {

                        NodeView child = new NodeView(parentNode)
                            {
                                Id = dialog.MobilePlantType.Id,
                                Name = dialog.MobilePlantType.Name,
                                Description = dialog.MobilePlantType.Description,
                                Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                Type = NodeType.MobilePlantTypeNode,
                                SortField = dialog.MobilePlantType.Ordinal.ToString(),
                                IsActive = dialog.MobilePlantType.IsActive,
                                HasChildren = true
                            };
                        parentNode.Children.Add(child);
                        nodeView.Sort(true);
                    }
                };
        }
        private void AddProperty(NodeView nodeView)
        {
            AddEditEquipmentPropertyDialog dialog = new AddEditEquipmentPropertyDialog(CommonUtils.EquipmentPropertyType.MobilePlantProperty);
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    NodeView parentNode = nodeView;
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {

                        NodeView child = new NodeView(parentNode)
                        {
                            Id = dialog.ComponentProperty.Id,
                            Name = dialog.ComponentProperty.Name,
                            Description = dialog.ComponentProperty.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.MobilePropertyNode,
                            SortField = dialog.ComponentProperty.Name.ToString(),
                            HasChildren = false
                        };
                        parentNode.Children.Add(child);
                        nodeView.Sort();
                    }
                };
        }
        private void AddOwner(NodeView nodeView)
        {
            AddEditMobileOwnerDialog dialog = new AddEditMobileOwnerDialog();
            AddEditMobileOwnerModel addEditMobileOwnerModel = new AddEditMobileOwnerModel();
            addEditMobileOwnerModel.View = dialog;
            dialog.DataContext = addEditMobileOwnerModel;
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    NodeView parentNode = nodeView;
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        NodeView child = new NodeView(parentNode)
                        {
                            Id = dialog.MobileOwner.Id,
                            Name = dialog.MobileOwner.Name,
                            Description = dialog.MobileOwner.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.MobileOwnerNode,
                            SortField = dialog.MobileOwner.Ordinal.ToString(),
                            HasChildren = false
                        };
                        if (nodeView.ChildrenLoaded)
                        {
                            parentNode.Children.Add(child);
                            nodeView.Sort();
                        }
                    }
                };
        }
        private void LoadModels(NodeView expandedNode)
        {
            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetModelsCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                List<Model> models = eventArgs.Result;
                foreach (Model model in models)
                {
                    NodeView child = new NodeView(expandedNode)
                    {
                        Id = model.Id,
                        Name = model.Name,
                        Description = model.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.ModelNode,
                        HasChildren = false,
                        SortField = model.Ordinal.ToString()
                    };
                    expandedNode.Children.Add(child);
                }
                Utils.HideSpinner(expandedNode);

                cmsWebServiceClient.GetModelsCompleted -= fetchCompleted;
                expandedNode.Sort();
            };
            cmsWebServiceClient.GetModelsCompleted += fetchCompleted;
            cmsWebServiceClient.GetModelsAsync(expandedNode.Id);
        }
        private void LoadMobileProperties(NodeView expandedNode)
        {
            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetAllMobilePlantPropertiesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                List<MobilePlantProperty> mobileProperties = eventArgs.Result as List<MobilePlantProperty>;
                foreach (MobilePlantProperty mobileProperty in mobileProperties)
                {
                    NodeView child = new NodeView(expandedNode)
                    {
                        Id = mobileProperty.Id,
                        Name = mobileProperty.Name,
                        Description = mobileProperty.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.MobilePropertyNode,
                        SortField = mobileProperty.Name.ToString(),
                        HasChildren = false
                    };
                    expandedNode.Children.Add(child);
                    cmsWebServiceClient.GetAllMobilePlantPropertiesCompleted -= fetchCompleted;
                }

                expandedNode.Sort();
            };
            cmsWebServiceClient.GetAllMobilePlantPropertiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetAllMobilePlantPropertiesAsync();
        }
        private void LoadComponentTypeProperties(NodeView expandedNode)
        {
            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetMobilePlantComponentTypePropertiesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                List<MobilePlantComponentTypeProperty> electricalEquipmentComponentTypeProperties = eventArgs.Result;
                foreach (MobilePlantComponentTypeProperty electricalEquipmentComponentProperty in electricalEquipmentComponentTypeProperties)
                {

                    NodeView child = new NodeView(expandedNode)
                    {
                        Id = electricalEquipmentComponentProperty.Id,
                        Name = electricalEquipmentComponentProperty.MobilePlantProperty.Name,
                        Description = electricalEquipmentComponentProperty.MobilePlantProperty.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.MobilePlantComponentTypeProperty,
                        HasChildren = false,
                        SortField = electricalEquipmentComponentProperty.Ordinal.ToString()
                    };
                    expandedNode.Children.Add(child);
                }

                Utils.HideSpinner(expandedNode);
                expandedNode.Sort();
                cmsWebServiceClient.GetMobilePlantComponentTypePropertiesCompleted -= fetchCompleted;
            };

            cmsWebServiceClient.GetMobilePlantComponentTypePropertiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetMobilePlantComponentTypePropertiesAsync(expandedNode.Id);
        }
        private void AddCalibrationComponentType(NodeView nodeView)
        {
            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            AddEditCalibrationComponentTypeDialog dialog = new AddEditCalibrationComponentTypeDialog();
            dialog.Show();

            dialog.Closed += (s1, e1) =>
                                 {
                                     if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                                     {
                                         EventHandler<AddCalibrationComponentTypeCompletedEventArgs> addCompleted = null;
                                         addCompleted = (s2, e2) =>
                                                            {

                                                                if (e2.Result.HasErrors)
                                                                {
                                                                    var errorDialog = new PopupDialog(PopupDialogType.Error, Utils.DisplayErrorMessages(e2.Result.ServerErrorMessages));
                                                                    errorDialog.Show();
                                                                }
                                                                else
                                                                {
                                                                    CalibrationComponentType calibrationComponentType = e2.Result.EntityResult;

                                                                    if (calibrationComponentType != null)
                                                                    {
                                                                        NodeView child = new NodeView(nodeView)
                                                                        {
                                                                            Id = calibrationComponentType.Id,
                                                                            Name = dialog.ComponentType.Name,
                                                                            Description = dialog.ComponentType.Description,
                                                                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                                                            Type = NodeType.CalibrationComponentType,
                                                                            HasChildren = true,
                                                                            SortField = dialog.ComponentType.Ordinal.ToString()
                                                                        };
                                                                        if (nodeView.ChildrenLoaded)
                                                                        {
                                                                            nodeView.Children.Add(child);
                                                                            nodeView.Sort();
                                                                        }
                                                                    }
                                                                    cmsWebServiceClient.AddCalibrationComponentTypeCompleted -= addCompleted;
                                                                }
                                                            };

                                         cmsWebServiceClient.AddCalibrationComponentTypeCompleted += addCompleted;
                                         cmsWebServiceClient.AddCalibrationComponentTypeAsync(dialog.ComponentType);
                                     }
                                 };
        }
        private void LoadAlarmPriorities(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetControlSystemAlarmPrioritiesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var alarmPriorities = eventArgs.Result;
                if (alarmPriorities != null)
                {
                    foreach (var priority in alarmPriorities)
                    {
                        var child = new NodeView(expandedNode)
                        {
                            Id = priority.Id,
                            Name = priority.Name,
                            Description = priority.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.AlarmPriorityNode,
                            HasChildren = false,
                            SortField = priority.Ordinal.ToString()
                        };
                        expandedNode.Children.Add(child);
                    }
                    Utils.HideSpinner(expandedNode);
                    expandedNode.Sort();
                }
            };
            cmsWebServiceClient.GetControlSystemAlarmPrioritiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetControlSystemAlarmPrioritiesAsync();
        }
        private void LoadComponentTypeTuningProperties(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetControlSystemComponentTypeTuningPropertiesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var componentTypeProperties = eventArgs.Result;
                componentTypeProperties = componentTypeProperties.OrderBy(x => x.Ordinal).ThenBy(x => x.GroupOrdinal).ToList();

                foreach (var componentProperty in componentTypeProperties)
                {
                    var desc = componentProperty.ControlSystemTuningProperty.Name;
                    if (!string.IsNullOrEmpty(componentProperty.ControlSystemTuningProperty.Description))
                    {
                        desc = string.Format("{0} ({1})", componentProperty.ControlSystemTuningProperty.Name,
                            componentProperty.ControlSystemTuningProperty.Description);
                    }

                    if (componentProperty.ComponentTypeGroupId.HasValue)
                    {
                        var childGroup = expandedNode.Children.FirstOrDefault(x => x.Id == componentProperty.ComponentTypeGroupId.Value);

                        if (childGroup == null)
                        {
                            childGroup = new NodeView(expandedNode)
                            {
                                Id = componentProperty.ComponentTypeGroupId.Value,
                                Name = componentProperty.ComponentTypeGroup.Name,
                                Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                Type = NodeType.ComponentTypeGroup,
                                HasChildren = true,
                                SortField = componentProperty.Ordinal.ToString()
                            };
                            expandedNode.Children.Add(childGroup);
                        }

                        var child = new NodeView(childGroup)
                        {
                            Id = componentProperty.Id,
                            Name = desc,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.ControlSystemComponentTypeTuningProperty,
                            HasChildren = false,
                            SortField = componentProperty.GroupOrdinal.ToString()
                        };
                        childGroup.Children.Add(child);
                    }
                    else
                    {
                        var child = new NodeView(expandedNode)
                        {
                            Id = componentProperty.Id,
                            Name = desc,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.ControlSystemComponentTypeTuningProperty,
                            HasChildren = false,
                            SortField = componentProperty.Ordinal.ToString()
                        };
                        expandedNode.Children.Add(child);
                    }
                }
                Utils.HideSpinner(expandedNode);
                expandedNode.Sort();

                cmsWebServiceClient.GetControlSystemComponentTypeTuningPropertiesCompleted -= fetchCompleted;
            };

            cmsWebServiceClient.GetControlSystemComponentTypeTuningPropertiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetControlSystemComponentTypeTuningPropertiesAsync(expandedNode.Parent.Id);
        }
        private void AddExistingMobilePlantComponentProperty(NodeView nodeView)
        {
            int electricalEquipmentComponentTypeId = nodeView.Id;
            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            cmsWebServiceClient.GetMobilePlantComponentTypeCompleted +=
                (s, e) =>
                {
                    AddEditExistingMobilePlantComponentPropertyDialog dialog = new AddEditExistingMobilePlantComponentPropertyDialog(e.Result);
                    dialog.Show();

                    dialog.Closed += (s1, e1) =>
                    {

                        if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                        {
                            EventHandler<AddMobilePlantComponentTypePropertyCompletedEventArgs> addCompleted = null;
                            addCompleted = (s2, eventArgs) =>
                            {
                                MobilePlantComponentTypeProperty pcpt = eventArgs.Result;

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

                                cmsWebServiceClient.AddMobilePlantComponentTypePropertyCompleted -= addCompleted;

                            };

                            cmsWebServiceClient.AddMobilePlantComponentTypePropertyCompleted += addCompleted;

                            MobilePlantComponentTypeProperty electricalEquipmentComponentTypeProperty = new MobilePlantComponentTypeProperty
                            {
                                MobilePlantComponentTypeId = electricalEquipmentComponentTypeId,
                                MobilePlantPropertyId = dialog.MobilePlantComponentTypeProperty.MobilePlantPropertyId,
                                Ordinal = dialog.MobilePlantComponentTypeProperty.Ordinal
                            };

                            cmsWebServiceClient.AddMobilePlantComponentTypePropertyAsync(electricalEquipmentComponentTypeProperty);
                        }
                    };
                };

            cmsWebServiceClient.GetMobilePlantComponentTypeAsync(electricalEquipmentComponentTypeId);
        }
        private void LoadGraphics(NodeView expandedNode)
        {
            DatabaseLoader.GetGraphics().ContinueWith(getGraphicsTask =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    var graphics = getGraphicsTask.Result;

                    var topparents = (from x in graphics where !x.ParentGraphicId.HasValue orderby x.Ordinal select x).ToList();

                    foreach (var graphic in topparents)
                    {
                        var linked = (from x in graphics where x.ParentGraphicId.HasValue && x.ParentGraphicId.Value == graphic.Id orderby x.Ordinal select x).ToList();

                        var childNode = new NodeView(expandedNode)
                        {
                            Id = graphic.Id,
                            Name = graphic.Name,
                            Description = graphic.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.GraphicNode,
                            IsActive = true,
                            SortField = graphic.Ordinal.ToString(),
                            HasChildren = linked.Any()
                        };

                        expandedNode.Children.Add(childNode);
                        Utils.HideSpinner(childNode);

                        foreach (var childGraphic in linked)
                        {
                            RecursiveLoadGraphics(graphics, childGraphic, childNode);
                        }
                    }

                    expandedNode.Sort();
                    Utils.HideSpinner(expandedNode);
                });
            });
        }
        private void AddNewGraphic(NodeView nodeView)
        {
            var dialog = new AddEditGraphicDialog();
            dialog.Show();

            dialog.Closed += (s1, e1) =>
            {
                if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                {
                    var child = new NodeView(nodeView)
                    {
                        Id = dialog.Graphic.Id,
                        Name = dialog.Graphic.Name,
                        Description = dialog.Graphic.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Area.png",
                        Type = NodeType.GraphicNode,
                        SortField = dialog.Graphic.Ordinal.ToString(),
                        IsActive = true,
                        HasChildren = false
                    };

                    if (nodeView.ChildrenLoaded)
                    {
                        nodeView.Children.Add(child);
                        nodeView.Sort(true);
                    }

                    mGraphicsTopNode.Children.Clear();
                    LoadGraphics(mGraphicsTopNode);
                }
            };
        }
        private void LoadInterlockTypes(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetInterlockTypesCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var interlockTypes = eventArgs.Result;
                if (interlockTypes != null)
                {
                    foreach (var interlockType in interlockTypes)
                    {
                        var child = new NodeView(expandedNode)
                        {
                            Id = interlockType.Id,
                            Name = interlockType.Name,
                            Description = interlockType.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.InterlockType,
                            HasChildren = true,
                            SortField = interlockType.Ordinal.ToString()
                        };
                        expandedNode.Children.Add(child);
                    }
                    Utils.HideSpinner(expandedNode);
                    expandedNode.Sort();
                }
            };
            cmsWebServiceClient.GetInterlockTypesCompleted += fetchCompleted;
            cmsWebServiceClient.GetInterlockTypesAsync();
        }
        private void AddNewGroup(NodeView nodeView)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            var existingGroupNames = nodeView.Children.Where(x => x.Type == NodeType.ComponentTypeGroup).Select(x => x.Name).ToList();

            var dialog = new AddEditComponentTypeGroupDialog(existingGroupNames);
            dialog.Show();

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

                        if (componentTypeGroup != null)
                        {
                            cmsWebServiceClient.AddGroupComponentTypePropertyCompleted += (s, e) =>
                            {
                                var child = new NodeView(nodeView)
                                {
                                    Id = e.Result, //ComponentTypePropertyId
                                    GroupId = componentTypeGroup.Id,
                                    Name = dialog.ComponentTypeGroup.Name,
                                    Description = dialog.ComponentTypeGroup.Description,
                                    Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                    Type = NodeType.ComponentTypeGroup,
                                    HasChildren = true,
                                    SortField = dialog.Ordinal.ToString()
                                };
                                if (nodeView.ChildrenLoaded)
                                {
                                    nodeView.Children.Add(child);
                                    nodeView.Sort();
                                }
                            };

                            var controlSystemComponentTypeId = nodeView.Parent.Id;
                            var ordinal = dialog.Ordinal;
                            var componentTypeGroupId = componentTypeGroup.Id;

                            //This will add new row to the Equipment ComponentTypeProperty and mark it as Group
                            cmsWebServiceClient.AddGroupComponentTypePropertyAsync(GetEquipmentComponentPropertyType(nodeView), controlSystemComponentTypeId, componentTypeGroupId, ordinal);
                        }
                        cmsWebServiceClient.AddEditComponentTypeGroupCompleted -= addCompleted;
                    };

                    cmsWebServiceClient.AddEditComponentTypeGroupCompleted += addCompleted;
                    cmsWebServiceClient.AddEditComponentTypeGroupAsync(GetEquipmentComponentPropertyType(nodeView), dialog.ComponentTypeGroup);
                }
            };
        }
        private void LoadReports(NodeView expandedNode)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            EventHandler<GetReportLinksByTabIdCompletedEventArgs> fetchCompleted = null;
            fetchCompleted = (s, eventArgs) =>
            {
                var reportLinks = eventArgs.Result;

                foreach (var reportLink in reportLinks)
                {
                    var child = new NodeView(expandedNode)
                    {
                        Id = reportLink.Id,
                        Name = reportLink.ReportName,
                        Description = reportLink.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.ReportNode,
                        HasChildren = false,
                        SortField = reportLink.Ordinal.ToString()
                    };

                    expandedNode.Children.Add(child);
                    expandedNode.Sort();

                    cmsWebServiceClient.GetReportLinksByTabIdCompleted -= fetchCompleted;
                }
                Utils.HideSpinner(expandedNode);
            };

            cmsWebServiceClient.GetReportLinksByTabIdCompleted += fetchCompleted;
            cmsWebServiceClient.GetReportLinksByTabIdAsync(mTab.Id);
        }
        private void AddNewReport(NodeView nodeView)
        {
            var dialog = new AddEditReportDialog();
            dialog.DataContext = new AddEditReportModel(mTab) { View = dialog };
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        var child = new NodeView(nodeView)
                        {
                            Id = dialog.ReportLink.Id,
                            Name = dialog.ReportLink.ReportName,
                            Description = dialog.ReportLink.Description,
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.ReportNode,
                            HasChildren = false,
                            SortField = dialog.ReportLink.Ordinal.ToString()
                        };
                        if (nodeView.ChildrenLoaded)
                        {
                            nodeView.Children.Add(child);
                            nodeView.Sort();
                        }
                    }
                };
        }
        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 AddType(NodeView nodeView)
        {
            var dialog = new AddEditControlSystemTypeDialog();
            dialog.Show();

            dialog.Closed +=
                (s1, e1) =>
                {
                    var parentNode = nodeView;
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        var child = new NodeView(parentNode)
                        {
                            Id = dialog.ControlSystemType.Id,
                            Name = string.Format("{0} ({1})", dialog.ControlSystemType.Name, dialog.ControlSystemType.Description),
                            Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                            Type = NodeType.ControlSystemTypeNode,
                            Description = dialog.ControlSystemType.Description,
                            HasChildren = false,
                            SortField = dialog.ControlSystemType.Name
                        };
                        if (nodeView.ChildrenLoaded)
                        {
                            parentNode.Children.Add(child);
                            nodeView.Sort();
                        }
                    }
                };
        }
        private void AddExistingSystemTypeTest(NodeView nodeView)
        {
            int? groupId = null;
            var controlSystemTypeId = -1;

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

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

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

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

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

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

                            var controlSystemEquipmentComponentTypeProperty = new ControlSystemTypeTestingProperty
                            {
                                ControlSystemTypeId = controlSystemTypeId,
                                TestPropertyId = dialog.SystemTypeTestingProperty.TestPropertyId,
                                ComponentTypeGroupId = dialog.SystemTypeTestingProperty.ComponentTypeGroupId,
                                Ordinal = dialog.SystemTypeTestingProperty.Ordinal,
                                GroupOrdinal = dialog.SystemTypeTestingProperty.GroupOrdinal
                            };

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

                            cmsWebServiceClient.AddUpdateControlSystemTypeTestingPropertyAsync(controlSystemEquipmentComponentTypeProperty);
                        }
                    };
                };
            cmsWebServiceClient.GetControlSystemTypeAsync(controlSystemTypeId);
        }
        private void EditGraphic(NodeView nodeView)
        {
            var dialog = new AddEditGraphicDialog(nodeView.Id);
            dialog.Show();

            dialog.Closed += (s1, e1) =>
            {
                if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                {
                    nodeView.Name = dialog.Graphic.Name;
                    nodeView.Description = dialog.Graphic.Description;
                    nodeView.IsActive = true;
                    nodeView.SortField = dialog.Graphic.Ordinal.ToString();
                    nodeView.Parent.Sort();

                    // todo:does this need to be reloaded???
                    //mGraphicsTopNode.Children.Clear();
                    //LoadGraphics(mGraphicsTopNode);

                    nodeView.Sort(true);
                    mGraphicsTopNode.ChildrenLoaded = true;
                }
            };
        }
        private void AddInterlockType(NodeView nodeView)
        {
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            var dialog = new AddEditInterlockTypeDialog();
            dialog.Show();

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

                        if (mect != null)
                        {
                            var child = new NodeView(nodeView)
                            {
                                Id = mect.Id,
                                Name = dialog.InterlockType.Name,
                                Description = dialog.InterlockType.Description,
                                Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                Type = NodeType.InterlockType,
                                HasChildren = true,
                                SortField = dialog.InterlockType.Ordinal.ToString()
                            };
                            if (nodeView.ChildrenLoaded)
                            {
                                nodeView.Children.Add(child);
                                nodeView.Sort();
                            }
                        }
                        cmsWebServiceClient.AddInterlockTypeCompleted -= addCompleted;
                    };

                    cmsWebServiceClient.AddInterlockTypeCompleted += addCompleted;
                    cmsWebServiceClient.AddInterlockTypeAsync(dialog.InterlockType);
                }
            };
        }
        private void AddAlarmResponse(NodeView nodeView)
        {
            var dialog = new AddEditAlarmResponseDialog();
            dialog.Show();

            dialog.Closed += (s1, e1) =>
            {
                if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                {
                    var child = new NodeView(nodeView)
                    {
                        Id = dialog.AlarmResponse.Id,
                        Name = dialog.AlarmResponse.Name,
                        Description = dialog.AlarmResponse.Description,
                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                        Type = NodeType.AlarmResponseNode,
                        HasChildren = false,
                        SortField = dialog.AlarmResponse.Name
                    };
                    if (nodeView.ChildrenLoaded)
                    {
                        nodeView.Children.Add(child);
                        nodeView.Sort();
                    }
                }
            };
        }