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 EditSystemTypeTest(NodeView nodeView)
        {
            var dialog = new AddEditExistingControlTypeTestingPropertyDialog(nodeView.Id) { Title = "Edit System Type Test" };

            dialog.Closed +=
                (s1, e1) =>
                {
                    if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                    {
                        var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
                        EventHandler<AddUpdateControlSystemTypeTestingPropertyCompletedEventArgs> editCompleted = null;
                        editCompleted = (s2, eventArgs) =>
                        {
                            var property = eventArgs.Result;
                            if (property != null)
                            {
                                nodeView.Name = dialog.SystemTypeTestingProperty.ControlSystemTestingProperty.Name;
                                nodeView.Description = dialog.SystemTypeTestingProperty.ControlSystemTestingProperty.Description;
                                nodeView.SortField = dialog.SystemTypeTestingProperty.ComponentTypeGroupId.HasValue
                                    ? dialog.SystemTypeTestingProperty.GroupOrdinal.ToString()
                                    : dialog.SystemTypeTestingProperty.Ordinal.ToString();
                            }
                            cmsWebServiceClient.AddUpdateControlSystemTypeTestingPropertyCompleted -= editCompleted;
                            nodeView.Parent.Sort();
                        };
                        cmsWebServiceClient.AddUpdateControlSystemTypeTestingPropertyCompleted += editCompleted;

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

                        cmsWebServiceClient.AddUpdateControlSystemTypeTestingPropertyAsync(typeProperty);

                        var propertiesNode = nodeView.Parent;
                        if (propertiesNode.Type != NodeType.SystemTypeTests)
                        {
                            propertiesNode = propertiesNode.Parent;
                        }

                        if (dialog.GroupChanged)
                        {
                            //Group has changed, reload the Nodes
                            ReloadComponentTypeEquipmentProperties(CommonUtils.EquipmentPropertyType.SystemTestingProperty,
                                propertiesNode, NodeType.SystemTypeTests);
                        }
                    }
                };
            dialog.Show();
        }
示例#3
0
        public void Run()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var dt = DateTime.Now;

            Console.WriteLine("Started Run at " + dt.ToString(DATE_FORMAT));

            try
            {
                if (AbortSetStartUpOptions()) return;

                List<int> controlModuleIdList;
                int count;
                using (var art = new ARTEntities())
                {
                    controlModuleIdList = (from x in art.ControlModules select x.Id).ToList();
                    //controlModuleIdList = (from x in art.ControlModules where x.Tag == "HV631442" select x.Id).ToList();
                    count = art.ControlModules.Count();
                }

                var index = 0;
                foreach (var id in controlModuleIdList)
                {
                    using (var art = new ARTEntities())
                    {

                        art.Database.CommandTimeout = 360;
                        using (var cee = new CmsEntities())
                        {

                            var artCm = (from x in art.ControlModules
                                .Include("Elements")
                                .Include("Elements.ElementTypical")
                                .Include("ControlModuleTypical")
                                         where x.Id == id
                                         select x).FirstOrDefault();

                            index++;

                            UpdateStatus(string.Format("{0} of {1})", index, count));

                            //Match ControlSystemType
                            ControlSystemType controlSystemType;
                            if (MatchControlSystemTypeFailed(cee, artCm, out controlSystemType)) continue;

                            //Match ControlSystem
                            ControlSystem controlSystem;
                            if (MatchControlSystemFailed(cee, artCm, controlSystemType, out controlSystem)) continue;

                            //Check Default View Exists in Elements
                            List<Element> defaultViewElements;
                            if (GetDefaultViewElementFailed(artCm, out defaultViewElements)) continue;

                            artCm = (from x in art.ControlModules
                                .Include("Elements")
                                .Include("Elements.ElementTypical")
                                .Include("ControlModuleTypical")
                                .Include("ControlModuleTypical.TestSets")
                                     where x.Id == id
                                     select x).FirstOrDefault();

                            if (!artCm.ControlModuleTypical.TestSets.Any())
                            {
                                Logger.Out(string.Format("Control {0} - No TestSets exist.", artCm.Tag));
                                continue;
                            }

                            artCm = (from x in art.ControlModules
                                .Include("Elements")
                                .Include("Elements.ElementTypical")
                                .Include("ControlModuleTypical")
                                .Include("ControlModuleTypical.TestSets")
                                .Include("ControlModuleTypical.TestSets.Test")
                                     where x.Id == id
                                     select x).FirstOrDefault();

                            List<ControlSystemTestingProperty> associatedProperties;
                            if (BuildTestPropertiesFailed(artCm.ControlModuleTypical.TestSets.ToList(), cee, out associatedProperties)) continue;

                            foreach (var property in associatedProperties)
                            {
                                var pair = new KeyValuePair<int, int>(property.Id, controlSystem.ControlSystemTypeId);

                                if (!mControlTypePropertyPairsDictionary.ContainsKey(pair))
                                {
                                    //insert new comp type propertid record.
                                    mControlTypePropertyPairsDictionary.Add(pair, artCm.Id);

                                    ControlSystemTypeTestingProperty typeTestingProperty = (from x in cee.ControlSystemTypeTestingProperties where x.TestPropertyId == property.Id && x.ControlSystemTypeId == controlSystem.ControlSystemTypeId select x).FirstOrDefault();

                                    if (typeTestingProperty == null)
                                    {
                                        var cptp = new ControlSystemTypeTestingProperty
                                        {
                                            TestPropertyId = property.Id,
                                            ControlSystemTypeId = controlSystem.ControlSystemTypeId,
                                            Ordinal = 0
                                        };

                                        cee.ControlSystemTypeTestingProperties.Add(cptp);

                                        //+++ SAVE +++
                                        cee.SaveChanges();
                                    }
                                }
                            }

                            var testResults = (from x in art.TestResults
                                                   .Include("Test")
                                                   .Include("ControlModule")
                                                   .Include("ControlModule.ControlModuleTypical")
                                               where x.ControlModuleId == id
                                               select x).ToList();

                            if (!testResults.Any())
                            {
                                Logger.Out(string.Format("Control {0} - No matchng TestResult.Test found in ART.TestResults using ControlModuleId = '{1}'.", artCm.Tag, artCm.Id));
                                continue;
                            }

                            //build property values and set ordinal on ControlSystemTypeTestingProperty
                            var k = 0;
                            foreach (var testResult in testResults)
                            {
                                if (testResult.TestId == null)
                                {
                                    continue;
                                }

                                //Match Test
                                Test test;
                                if (MatchTestFailed(art, testResult, artCm, out test)) continue;

                                //Match ControlSystemTestingProperty
                                ControlSystemTestingProperty testingProperty;
                                if (MatchTestingPropertyFailed(cee, test, artCm, out testingProperty)) continue;

                                //Build ControlSystemTestingPropertyValue
                                ControlSystemTestingPropertyValue pv;
                                if (BuildPropertyValueFailed(controlSystem, testingProperty, testResult, cee, artCm, out pv)) continue;

                                k++;
                                cee.ControlSystemTestingPropertyValues.Add(pv);

                                SetOrdinalAndGroupOnControlTypeTestingProperty(art, cee, testResult, artCm, controlSystem);
                            }

                            // +++ SAVE +++
                            cee.SaveChanges();
                            Logger.Out(string.Format("{0}: Saved {1} Property Values.", controlSystem.Name, k));

                        }//end using
                    }//end using
                }//end control module loop

            }
            catch (Exception ex)
            {
                Logger.Out("");
                Logger.Out("{0}\n\r\n\r{1}", ex.Message, ex.InnerException != null ? ex.InnerException.ToString() : string.Empty);
            }
            finally
            {
                Logger.WriteToFile();
                Logger.Out("");

                sw.Stop();
                Console.WriteLine("Time taken: {0} minutes, {1} secs", sw.Elapsed.Minutes, sw.Elapsed.Seconds);
                Logger.Out("Press Enter to continue...");
                Console.ReadLine();

            }
        }
        private ControlSystemTestingPropertyValue GetPropertyValue(ControlSystem controlSystem, ControlSystemTypeTestingProperty typeTestingProperty)
        {
            ControlSystemTestingPropertyValue propertyValue = (from x in controlSystem.ControlSystemTestingPropertyValues
                                                               where x.TestPropertyId == typeTestingProperty.TestPropertyId &&
                                                                     x.ContolSystemId == controlSystem.Id
                                                               select x).FirstOrDefault();

            if (propertyValue == null && typeTestingProperty.TestPropertyId.HasValue)
            {
                propertyValue = new ControlSystemTestingPropertyValue
                {
                    ContolSystemId = controlSystem.Id,
                    TestPropertyId = typeTestingProperty.TestPropertyId.Value,
                    Value = typeTestingProperty.ControlSystemTestingProperty.DefaultValue
                };

                controlSystem.ControlSystemTestingPropertyValues.Add(propertyValue);
            }
            return propertyValue;
        }
        public ControlSystemTypeTestingProperty AddUpdateControlSystemTypeTestingProperty(ControlSystemTypeTestingProperty property)
        {
            using (var cee = new CmsEntities())
            {

                ControlSystemTypeTestingProperty returnRow = null;

                if (property.Id > 0) //Updating existing
                {
                    ControlSystemTypeTestingProperty orginalRow = null;
                    orginalRow = (from x in cee.ControlSystemTypeTestingProperties where x.Id == property.Id select x).FirstOrDefault();

                    if (!property.ComponentTypeGroupId.HasValue && orginalRow.ComponentTypeGroupId.HasValue) //Group was set to null
                    {
                        var groups = (from x in cee.ControlSystemTypeTestingProperties
                                      where x.ControlSystemTypeId == orginalRow.ControlSystemTypeId &&
                                            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 ControlSystemTypeTestingProperty
                            {
                                TestPropertyId = null,
                                ControlSystemTypeId = property.ControlSystemTypeId,
                                ComponentTypeGroupId = orginalRow.ComponentTypeGroupId,
                                GroupOrdinal = 0,
                                Ordinal = 0,
                            };
                            cee.ControlSystemTypeTestingProperties.Add(newGroupRow);

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

                    }
                    else
                    {
                        var groups = (from x in cee.ControlSystemTypeTestingProperties
                                      where x.ControlSystemTypeId == property.ControlSystemTypeId &&
                                            x.ComponentTypeGroupId == property.ComponentTypeGroupId
                                      select x).ToList();

                        if (property.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.ControlSystemTypeTestingProperties.Remove(groups[0]);

                            }

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

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

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

                    //Check if adding a property to empty group
                    var groups = (from x in cee.ControlSystemTypeTestingProperties
                                  where x.ControlSystemTypeId == property.ControlSystemTypeId &&
                                        x.ComponentTypeGroupId == property.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 = property.TestPropertyId;
                        emptyGroup.GroupOrdinal = property.Ordinal;
                        returnRow = emptyGroup;
                    }
                    else
                    {
                        //Adding row to a Group
                        var temp = new ControlSystemTypeTestingProperty();
                        cee.ControlSystemTypeTestingProperties.Add(temp);

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