private void ImportComponentTypeAlarmProperties(CmsEntities cee, OldCmsEntities old) { var sw = new Stopwatch(); sw.Start(); Logger.Out("ImportControlSystemComponentTypeAlarmProperties - start"); var alarmProperties = (from x in cee.ControlSystemAlarmProperties select x).ToList(); var oldAlarms = (from x in old.Alarms .Include("AlarmType") .Include("AlarmConsequence") .Include("AlarmPurpose") .Include("AlarmResponse") .Include("Element") .Include("Element.ElementTypical") select x).ToList(); List<KeyValuePair<int, int>> dicKeyValuePairs = LoadExistingPropertyComponentTypes(cee); int index = 0; int dup = 0; foreach (var oldAlarm in oldAlarms.OrderBy(x => x.Id)) { //AlarmProperty ControlSystemAlarmProperty matchedAlarmProperty; if (MatchAlarmPropertyFailed(alarmProperties, oldAlarm, out matchedAlarmProperty)) continue; ControlSystemComponentType matchedComponentType; if (MatchComponentTypeFailed(cee, old, oldAlarm, out matchedComponentType)) continue; var kvp = new KeyValuePair<int, int>(matchedAlarmProperty.Id, matchedComponentType.Id); if (!dicKeyValuePairs.Contains(kvp)) { dicKeyValuePairs.Add(kvp); var ctap = new ControlSystemComponentTypeAlarmProperty { ComponentTypeId = matchedComponentType.Id, AlarmPropertyId = matchedAlarmProperty.Id, Ordinal = 0 }; cee.ControlSystemComponentTypeAlarmProperties.Add(ctap); index++; } else { dup++; //Logger.Out(string.Format("Skipping Alarm ID {0} - duplicate on ComponentTypeId '{1}' and AlarmPropertyId '{2}'.", oldAlarm.Id, matchedComponentType.Id, matchedAlarmProperty.Id)); continue; } UpdateStatus(string.Format("ComponentType AlarmProperties: Mapped Alarm ID {0} ({1}).", oldAlarm.Id, index)); } Logger.Out("Saving ComponentTypeAlarmProperties..."); cee.SaveChanges(); sw.Stop(); Logger.Out(string.Format("Imported {0} out of {1} ComponentTypeAlarmProperties in {2} min, {3} sec. ", index, (oldAlarms.Count - dup), sw.Elapsed.Minutes, sw.Elapsed.Seconds)); Logger.Out(""); Logger.Out(""); }
private bool MatchComponentTypeFailed(CmsEntities cee, OldCmsEntities old, Alarm oldAlarm, out ControlSystemComponentType matchedComponentType) { matchedComponentType = null; var element = (from x in old.Elements.Include("ElementTypical") where x.Id == oldAlarm.ElementId select x).FirstOrDefault(); if (element == null) { Logger.Out(string.Format("MatchComponentTypeFailed on Alarm ID {0}:- failed to match 'Element' on ID '{1}'.", oldAlarm.Id, oldAlarm.ElementId)); return true; } matchedComponentType = (from x in cee.ControlSystemComponentTypes where x.Name.Equals(element.ElementTypical.Name, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); if (matchedComponentType == null) { Logger.Out(string.Format("MatchComponentTypeFailed on Alarm ID {0}:- failed to match new CMS 'ControlSystemComponentType' on Name '{1}'.", oldAlarm.Id, oldAlarm.AlarmType)); return true; } return false; }
public void Run() { try { //using (var transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { Timeout = TimeSpan.MaxValue })) //{ //1 alarm type -> alarm properties Logger.Out("Start " + DateTime.Now.ToString("dd-MM-yy hh:mm:ss tt")); using (var old = new OldCmsEntities(9999)) { using (var cee = new CmsEntities()) { cee.Configuration.AutoDetectChangesEnabled = false; cee.Configuration.ValidateOnSaveEnabled = false; return; //DELETES var rowsAffected = cee.Database.ExecuteSqlCommand("DELETE FROM [Control].[ControlSystemAlarmPropertyValue]"); Logger.Out(rowsAffected + " ControlSystemAlarmPropertyValue rows deleted."); rowsAffected = cee.Database.ExecuteSqlCommand("DELETE FROM [Control].[ControlSystemComponentTypeAlarmProperty]"); Logger.Out(rowsAffected + " ControlSystemComponentTypeAlarmProperty rows deleted."); rowsAffected = cee.Database.ExecuteSqlCommand("DELETE FROM [Control].[ControlSystemAlarmMappingCentum]"); Logger.Out(rowsAffected + " ControlSystemAlarmMappingCentum rows deleted."); rowsAffected = cee.Database.ExecuteSqlCommand("DELETE FROM [Control].[ControlSystemAlarmProperty]"); Logger.Out(rowsAffected + " ControlSystemAlarmProperty rows deleted."); InsertUpdateAlarmColours(cee); ImportAlarmProperties(cee, old); ImportCentumMappings(cee, old); ImportAlarmPropertyValues(cee, old); ImportComponentTypeAlarmProperties(cee, old); } } //transaction.Dispose(); //.Complete(); Logger.Out("End " + DateTime.Now.ToString("dd-MM-yy hh:mm:ss tt")); Logger.Out(""); //Logger.Out("Transaction Complete"); //} } catch (Exception ex) { Logger.Out(""); Logger.Out("{0}\n\r\n\r{1}", ex.Message, ex.InnerException != null ? ex.InnerException.ToString() : string.Empty); //Logger.Out("Transaction was not commited."); } finally { Logger.WriteToFile(); Logger.Out(""); Logger.Out("Press Enter to continue..."); Console.ReadLine(); } }
private void ImportCentumMappings(CmsEntities cee, OldCmsEntities old) { Logger.Out("ImportPropertiesColoursPriorities - start"); var sw = new Stopwatch(); sw.Start(); var k = 0; var prioritiesListNames = (from x in cee.ControlSystemAlarmPriorities select x).ToList(); var alarmTables = (from x in old.AlarmTables .Include("AlarmColour") .Include("AlarmPriority") .Include("AlarmType") select x).ToList(); foreach (var alarmTable in alarmTables) { var priority = (from x in prioritiesListNames where x.Name.Equals(alarmTable.AlarmPriority.Priority, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); var matchColour = (from x in cee.ControlSystemAlarmColours where x.Description.Equals(alarmTable.AlarmColour.Description, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); if (matchColour == null) { Logger.Out(string.Format("Skipping {0}", alarmTable.AlarmColour.Name)); Logger.Out(string.Format("InsertAlarmMappingsForPropertiesColoursPriorities: Matching Colour in new CMS not found for {0}", alarmTable.AlarmColour.Name)); continue; } var matchPriority = (from x in prioritiesListNames where x.Name.Equals(alarmTable.AlarmPriority.Priority, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); if (matchPriority == null) { Logger.Out(string.Format("Skipping {0}", alarmTable.AlarmPriority.Priority)); Logger.Out(string.Format("InsertAlarmMappingsForPropertiesColoursPriorities: Matching Priority not found in new CMS for {0}", alarmTable.AlarmPriority.Priority)); continue; } var matchProperty = (from x in cee.ControlSystemAlarmProperties where x.Name.Equals(alarmTable.AlarmType.Name, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); if (matchProperty == null) { Logger.Out(string.Format("Skipping {0}", alarmTable.AlarmType.Name)); Logger.Out(string.Format("InsertAlarmMappingsForPropertiesColoursPriorities: Matching Property not in new CMS found for {0}", alarmTable.AlarmType.Name)); continue; } var checkDup = (from x in cee.ControlSystemAlarmMappingCentums where x.PriorityId == priority.Id && x.ColourId == matchColour.Id && x.PropertyId == matchProperty.Id && x.Level == alarmTable.AlarmLevel select x).FirstOrDefault(); if (checkDup != null) { Logger.Out(string.Format("Early exit due to duplicate found in new CMS Table 'ControlSystemAlarmMappingCentum' for on Alarm Table: " + "Type:{0} Colour:{1} Priority:{2} Level:{3}", alarmTable.AlarmType.Name, alarmTable.AlarmColour.Name, alarmTable.AlarmPriority.Priority, alarmTable.AlarmLevel)); continue; } var pcp = new ControlSystemAlarmMappingCentum { PropertyId = matchProperty.Id, ColourId = matchColour.Id, PriorityId = matchPriority.Id, Level = alarmTable.AlarmLevel //straight copy. }; cee.ControlSystemAlarmMappingCentums.Add(pcp); k++; UpdateStatus(string.Format("Mapped - Colour:{0}, Priority:{1}, Level:{2}. ({3} of {4}).", matchColour.Description, matchPriority.Name, alarmTable.AlarmLevel, k, alarmTables.Count)); } Logger.Out("Saving PropertyColourPriorities..."); cee.SaveChanges(); sw.Stop(); Logger.Out(string.Format("Imported {0} out of {1} PropertiesColoursPriorities in {2} min, {3} sec.", k, alarmTables.Count, sw.Elapsed.Minutes, sw.Elapsed.Seconds)); Logger.Out(""); Logger.Out(""); }
private void ImportAlarmPropertyValues(CmsEntities cee, OldCmsEntities old) { Logger.Out("ImportControlSystemAlarmPropertyValues - start"); var sw = new Stopwatch(); sw.Start(); var dicKeyValuePairs = LoadExistingPropertyValueKeys(cee); var newResponses = (from x in cee.ControlSystemAlarmResponses select x).ToList(); var newPurposeListNames = (from x in cee.ControlSystemAlarmPurposes select x).ToList(); var newConseqListNames = (from x in cee.ControlSystemAlarmConsequences select x).ToList(); var newPriorityNames = (from x in cee.ControlSystemAlarmPriorities select x).ToList(); var alarmProperties = (from x in cee.ControlSystemAlarmProperties select x).ToList(); var oldAlarms = (from x in old.Alarms .Include("AlarmType") .Include("AlarmConsequence") .Include("AlarmPurpose") .Include("AlarmResponse") .Include("Element") .Include("Element.ElementTypical") .Include("Element.ControlModule") select x).ToList(); var index = 0; foreach (var oldAlarm in oldAlarms.OrderBy(x => x.Id)) { var apv = new ControlSystemAlarmPropertyValue(); //ControlSystemComponent ControlSystemComponent matchedNewCmsComponent; if (MatchNewComponentFailed(cee, oldAlarm, out matchedNewCmsComponent)) continue; //AlarmProperty ControlSystemAlarmProperty matchedAlarmProperty; if (MatchAlarmPropertyFailed(alarmProperties, oldAlarm, out matchedAlarmProperty)) continue; //checkforduplicate var kvp = new KeyValuePair<int, int>(matchedNewCmsComponent.Id, matchedAlarmProperty.Id); if (!dicKeyValuePairs.Contains(kvp)) { dicKeyValuePairs.Add(kvp); } else { Logger.Out(string.Format("Skipping Alarm ID {0} - duplicate on ComponentId '{1}' and AlarmPropertyId '{2}'.", oldAlarm.Id, matchedNewCmsComponent.Id, matchedAlarmProperty.Id)); continue; } //ControlSystemComponentType ControlSystemComponentType matchedComponentType; if (MatchComponentTypeFailed(cee, old, oldAlarm, out matchedComponentType)) continue; //ReviewCompleted if (oldAlarm.Element == null || oldAlarm.Element.ControlModule == null) { Logger.Out(string.Format("Element or ControlModule on Alarm ID {0} was null. Trying to set the Review Completed values.", oldAlarm.Id)); continue; } if (oldAlarm.Detection) { apv.ReviewCompleted = oldAlarm.Element.ControlModule.AlarmSettingsConfirmed; apv.ReviewedCompletedById = oldAlarm.Element.ControlModule.AlarmSettingsConfirmedUserId; apv.ReviewedCompletedDate = oldAlarm.Element.ControlModule.AlarmSettingsConfirmedDate; } //AlarmPurpose ControlSystemAlarmPurpose matchPurpose; if (MatchPurposeFailed(newPurposeListNames, oldAlarm, out matchPurpose)) continue; //AlarmConsequence ControlSystemAlarmConsequence matchConsequence; if (MatchConsequenceFailed(newConseqListNames, oldAlarm, out matchConsequence)) continue; //AlarmResponse ControlSystemAlarmResponse matchResponse; if (MatchResponseFailed(newResponses, oldAlarm, out matchResponse)) continue; //TestedBy if (oldAlarm.Tested) { User matchedTestedUser; if (!MatchTestebByUserFailed(cee, old, oldAlarm, out matchedTestedUser)) { if (matchedTestedUser!=null) { apv.TestedUserId = matchedTestedUser.Id; apv.TestedDate = oldAlarm.TestedDate; apv.Tested = oldAlarm.Tested; } } } //ModifiedBy User matchedLastModifiedByUser; if (!MatchModifiedByUserFailed(cee, old, oldAlarm, out matchedLastModifiedByUser)) { if (matchedLastModifiedByUser!=null) { apv.ModifiedUserId = matchedLastModifiedByUser.Id; apv.ModifiedDate = oldAlarm.ModifiedDate; } } //Enabled (JIRA comment - https://jira.issgroup.com.au/browse/BODCMS-1840?focusedCommentId=80527&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-80527) apv.Enabled = oldAlarm.Detection; //Priority //step 1 EngParameter matchedEngParam; if (MatchEngParamFailed(old, oldAlarm, out matchedEngParam)) { apv.PriorityId = null; } else { //step 2 AlarmTable matchedAlarmTable; if (MatchAlarmTableRecordFailed(old, oldAlarm, matchedEngParam, out matchedAlarmTable)) { apv.PriorityId = null; } else { //step 3 if (matchedAlarmTable == null) { apv.PriorityId = null; } else { ControlSystemAlarmPriority matchedPriority; if (MatchPriorityNameFailed(newPriorityNames, matchedAlarmTable, oldAlarm, out matchedPriority)) { apv.PriorityId = null; } if (matchedPriority != null) { apv.PriorityId = matchedPriority.Id; } } } } apv.ActivationExpr = oldAlarm.ActivationExpr; apv.AlarmCalcExpr = oldAlarm.AlarmCalcExpr; apv.AlarmCalcExprEnabled = oldAlarm.Calc; apv.ControlSystemAlarmPropertyId = matchedAlarmProperty.Id; apv.ControlSystemComponentId = matchedNewCmsComponent.Id; if (!string.IsNullOrEmpty(oldAlarm.Guidance)) { apv.Guidance = oldAlarm.Guidance.Trim(); } if (!string.IsNullOrEmpty(oldAlarm.Notes)) { apv.Notes = oldAlarm.Notes.Trim(); } apv.MaskingExpr = oldAlarm.MaskingCondition; //apv.Reportable = oldAlarm.Reportable; kim will do manually. apv.OnDelay = oldAlarm.OnDelay; apv.OffDelay = oldAlarm.OffDelay; if (matchConsequence != null) { apv.ConsequenceId = matchConsequence.Id; } if (matchPurpose != null) { apv.PurposeId = matchPurpose.Id; } if (matchResponse != null) { apv.ResponseTimeId = matchResponse.Id; } cee.ControlSystemAlarmPropertyValues.Add(apv); index++; UpdateStatus(string.Format("Alarm PropertyValues: Mapped Alarm ID {0} ({1} of {2}).", oldAlarm.Id, index, oldAlarms.Count)); } Logger.Out("Saving Alarm PropertyValues..."); cee.SaveChanges(); sw.Stop(); Logger.Out(string.Format("Imported {0} out of {1} AlarmPropertyValues in {2} min, {3} sec. ", index, oldAlarms.Count, sw.Elapsed.Minutes, sw.Elapsed.Seconds)); Logger.Out(""); Logger.Out(""); }
private void ImportAlarmProperties(CmsEntities cee, OldCmsEntities old) { Logger.Out("ImportAlarmProperties - start"); var sw = new Stopwatch(); sw.Start(); var index = 0; var alarmTypesCount = (from x in old.AlarmTypes select x).Count(); ControlSystemUserInterfacePlatform centumUIP = (from x in cee.ControlSystemUserInterfacePlatforms where x.Name.Equals("centum", StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); var newAlarmPropertyNames = cee.ControlSystemAlarmProperties.Select(x => x.Name).ToList(); foreach (var oldAlarmType in old.AlarmTypes.OrderBy(x => x.Id)) { if (!newAlarmPropertyNames.Contains(oldAlarmType.Name)) { var alarmProperty = new ControlSystemAlarmProperty { Name = oldAlarmType.Name, Description = oldAlarmType.Description, }; if (centumUIP != null) { alarmProperty.UserInterfacePlatformId = centumUIP.Id; } //TuningPropertyId ControlSystemTuningProperty tuningProperty = (from x in cee.ControlSystemTuningProperties where x.Name.Equals(oldAlarmType.TuningParameterTypeName, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); if (tuningProperty != null) { alarmProperty.TuningPropertyId = tuningProperty.Id; } cee.ControlSystemAlarmProperties.Add(alarmProperty); newAlarmPropertyNames.Add(alarmProperty.Name); index++; } else { Logger.Out(string.Format("Skipped Alarm Property {0} , {1} as it already exist ...", oldAlarmType.Id, oldAlarmType.Name)); } UpdateStatus(string.Format("Importing Alarm Property '{0}' ({1} of {2}).", oldAlarmType.Name, index, alarmTypesCount)); } Logger.Out("Saving AlarmProperties..."); cee.SaveChanges(); sw.Stop(); Logger.Out(string.Format("Imported {0} out of {1} AlarmProperties in {2} min, {3} sec.", index, alarmTypesCount, sw.Elapsed.Minutes, sw.Elapsed.Seconds)); Logger.Out(""); Logger.Out(""); }
private static bool MatchTestebByUserFailed(CmsEntities cee, OldCmsEntities old, Alarm oldAlarm, out User matchedTestedUser) { matchedTestedUser = null; if (oldAlarm.TestedUserId.HasValue) { matchedTestedUser = (from x in cee.Users where x.Id == oldAlarm.TestedUserId.Value select x).FirstOrDefault(); if (matchedTestedUser == null) { Logger.Out(string.Format("MatchTestebByUserFailed on Alarm ID {0}:- failed to match 'TestedUserId' in NEW CMS by id '{1}'. ", oldAlarm.Id, oldAlarm.TestedUserId.Value)); return true; } } return false; }
private static bool MatchEngParamFailed(OldCmsEntities old, Alarm oldAlarm, out EngParameter matchedEngParam) { matchedEngParam = (from x in old.EngParameters where x.ElementId == oldAlarm.ElementId select x).FirstOrDefault(); if (matchedEngParam == null) { //Logger.Out(string.Format("MatchEngParamFailed on Alarm ID {0}:- could not match EngParameter using ElementId '{1}'. Step 1 in trying to determine 'Priority' property.", oldAlarm.Id, oldAlarm.ElementId)); return true; } return false; }
private static bool MatchAlarmTableRecordFailed(OldCmsEntities old, Alarm oldAlarm, EngParameter matchedEngParam, out AlarmTable matchedAlarmTable) { matchedAlarmTable = (from x in old.AlarmTables.Include("AlarmPriority") where x.AlarmTypeId == oldAlarm.AlarmTypeId && x.AlarmLevel == matchedEngParam.AlarmLevel select x).FirstOrDefault(); if (matchedAlarmTable == null) { //Logger.Out(string.Format("MatchAlarmTableRecordFailed on Alarm ID {0}:- could not match AlarmTable using AlarmTypeId '{1}' and Alarm Level {2}. Step 2 in trying to detemine 'Priority' property.", oldAlarm.Id, oldAlarm.ElementId, matchedEngParam.AlarmLevel)); return true; } return false; }