public InterlockRiskReviewDialog(Interlock interlock) { if (DesignerProperties.IsInDesignTool){return;} // Required to initialize variables InitializeComponent(); InterlockRiskReviewDialogViewModel riskReviewDialogViewModel = new InterlockRiskReviewDialogViewModel(interlock, this); DataContext = riskReviewDialogViewModel; }
public AddEditInterlockDialog(Interlock interlock, List<Interlock> existingInterlocks) { // Required to initialize variables InitializeComponent(); Title = "Edit Interlock"; AddEditInterlockViewModel addEditInterlockViewModel = new AddEditInterlockViewModel(interlock, existingInterlocks); addEditInterlockViewModel.Loaded += () => { addEditInterlockViewModel.View = this; DataContext = addEditInterlockViewModel; Utils.ResetOriginalValues(LayoutRoot); }; }
public InterlockPropertiesViewModel(Interlock interlock) { mInterlock = interlock; }
private void InsertData(IList<InterlockDataAdapter> importData) { if (importData.Count == 0) { RaiseMessage(CommonUtils.MessageType.Warning, NoDataFoundForWorkSheetMessage()); return; } for (int i = 0; i < importData.Count; i++) { InterlockDataAdapter adapter = importData[i]; var newInterlock = new Interlock(); //ControlSystem - must already exist ControlSystem matchControl = (from x in Cee.ControlSystems where x.Name.Equals(adapter.ControlSystemName, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault(); if (matchControl == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("ControlSystem", adapter.ControlSystemName, i + 1))); continue; } newInterlock.ControlSystemId = matchControl.Id; //InterlockTypeCode - must find existing InterlockType matchInterlockType = Cee.InterlockTypes.FirstOrDefault(x => string.Compare(x.Name, adapter.InterlockTypeName, true, CultureInfo.CurrentCulture) == 0); if (matchInterlockType == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNameDoesNotExistInDbMessage(adapter.InterlockTypeName, i + 1))); continue; } newInterlock.InterlockTypeId = matchInterlockType.Id; //NUMBER int intNumber; if (!int.TryParse(adapter.Number, out intNumber)) { RaiseMessage(CommonUtils.MessageType.Error, string.Format("Number '{0}' is not in a valid numeric format for Interlock Type '{1}' at Row {2}", adapter.Number, adapter.InterlockTypeName, i + 1)); continue; } newInterlock.Number = intNumber; //duplicate check = unique constraint on ControlId and Interlock typeId Interlock duplicateInterlock = (from x in Cee.Interlocks where x.ControlSystemId == matchControl.Id && x.InterlockTypeId == matchInterlockType.Id && x.Number == intNumber select x).FirstOrDefault(); if (duplicateInterlock != null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildDuplicateNameExistsInDbMessage(string.Format("Control:{0}, InterlockTypeCode:{1}, Number:{2}", matchControl.Name, adapter.InterlockTypeName, adapter.Number), i + 1))); continue; } //CAUSE if (adapter.Cause.Trim().Length > 200) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyTooLong("Cause", 200, i + 1))); } newInterlock.Cause = adapter.Cause.Trim(); //DESCRIPTION if (adapter.Description.Trim().Length > 4000) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyTooLong("Description ", 4000, i + 1))); } newInterlock.Description = adapter.Description.Trim(); newInterlock.LastModifiedDate = DateTime.Now; newInterlock.LastModifiedById = MetaData.UserId; //Dynamic Properties bool failed = false; List<PropertyNameComponentNamePair> summaryResults = AddDynamicProperties(adapter, matchInterlockType, newInterlock, matchControl.Name, i, out failed); if (failed) { continue; } InsertRevisionHistory(matchInterlockType,intNumber, matchControl); Cee.Interlocks.Add(newInterlock); var result = new InterlockResult { ControlName = matchControl.Name, InterlockTypeName = matchInterlockType.Name, InterlockNumber = newInterlock.Number, Properties = summaryResults }; mSavedResults.Add(result); } if (mSavedResults.Count == 0) { RaiseMessage(CommonUtils.MessageType.Warning, string.Format("No Interlocks were added from from worksheet {0}.", WorkSheetName)); } else { //SAVE int rowsaffected = Cee.SaveChanges(); RaiseMessage(CommonUtils.MessageType.Added, string.Format("Processed {0} Interlocks.", mSavedResults.Count)); foreach (InterlockResult result in mSavedResults) { RaiseMessage(CommonUtils.MessageType.Added, result.ToString()); } } }
private List<PropertyNameComponentNamePair> UpdateDynamicProperties(InterlockDataAdapter adapter, ControlSystem matchingControl, Interlock matchingInterlock, int i, out bool failed) { failed = false; var results = new List<PropertyNameComponentNamePair>(); foreach (DynamicProperty dynamicProperty in adapter.DynamicProperties) { //MATCH THE PROPERTY InterlockProperty matchProperty = (from x in Cee.InterlockProperties where string.Compare(x.Name, dynamicProperty.PropertyName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (matchProperty == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("InterlockProperty", dynamicProperty.PropertyName, i + 1))); failed = true; continue; } //CHECK ASSOCIATEION TO INTERLOCK TYPE InterlockTypeProperty matchCompTypeProperty = (from x in Cee.InterlockTypeProperties where x.InterlockPropertyId == matchProperty.Id && x.InterlockTypeId == matchingInterlock.InterlockTypeId select x).FirstOrDefault(); if (matchCompTypeProperty == null) { InterlockType matchingInterlockType = (from x in Cee.InterlockTypes where x.Id == matchingInterlock.InterlockTypeId select x).FirstOrDefault(); string message = string.Format("Missing a entry in Database for the Table 'InterlockTypeProperty' Where InterlockPropertyId = '{0}'({1}) And InterlockTypeId = '{2}' ({3}). Row {4}.", matchProperty.Id, matchProperty.Name, matchingInterlockType.Id, matchingInterlockType.Name, i); RaiseMessage(CommonUtils.MessageType.Error, message); failed = true; continue; } //CHECK IF THE PROP VALUE ALREADY EXISTS InterlockPropertyValue propertyValue = (from x in Cee.InterlockPropertyValues where (x.InterlockId == matchingInterlock.Id) && (x.InterlockPropertyId == matchProperty.Id) select x).FirstOrDefault(); if (dynamicProperty.PropertyValue.ToLower().Trim() == NULLTEXT) { if (propertyValue == null) { //do nothing continue; } //delete Cee.InterlockPropertyValues.Remove(propertyValue); } else { if (PropertyValueToPropertyTypeMismatched(matchProperty.PropertyListId, matchProperty.Type, dynamicProperty, i + 1)) { continue; } if (propertyValue == null) { //create propertyValue = new InterlockPropertyValue { InterlockPropertyId = matchProperty.Id, InterlockId = matchingInterlock.Id, Value = dynamicProperty.PropertyValue, VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName) }; Cee.InterlockPropertyValues.Add(propertyValue); } else { //update propertyValue.Value = dynamicProperty.PropertyValue; propertyValue.VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName); } } var pair = new PropertyNameComponentNamePair { ComponentName = matchingControl.Name, PropertyName = matchProperty.Name, Value = dynamicProperty.PropertyValue }; results.Add(pair); } return results; }
public void CopyInterlocks(List<int> interlocksToCopy, List<int> controlSystemsToCopyTo) { using (var cee = new CmsEntities()) { //Get all control systems that we will copy interlock to var controlSystems = (from x in cee.ControlSystems.Include("Interlocks") where controlSystemsToCopyTo.Contains(x.Id) select x).ToList(); //Get all interlocks that we can to copy var interlocks = (from x in cee.Interlocks where interlocksToCopy.Contains(x.Id) select x).ToList(); foreach (var controlSystem in controlSystems) { foreach (var interlock in interlocks) { //If the control system doesnt already have this interlock add it var exisitingInterlock = controlSystem.Interlocks.FirstOrDefault(x => x.ControlSystemId == controlSystem.Id && x.InterlockTypeId == interlock.InterlockTypeId && x.Number == interlock.Number); if (exisitingInterlock == null) { var newInterlock = new Interlock { ControlSystemId = controlSystem.Id, InterlockTypeId = interlock.InterlockTypeId, Cause = interlock.Cause, Number = interlock.Number, Description = interlock.Description }; cee.Interlocks.Add(newInterlock); } } } cee.SaveChanges(); } }
private List<PropertyNameComponentNamePair> AddDynamicProperties(InterlockDataAdapter adapter, InterlockType interlockType, Interlock interlock, string controlSystemName, int i, out bool failed) { failed = false; var results = new List<PropertyNameComponentNamePair>(); foreach (DynamicProperty dynamicProperty in adapter.DynamicProperties) { //InterlockProperty InterlockProperty matchProperty = (from x in Cee.InterlockProperties where string.Compare(x.Name, dynamicProperty.PropertyName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (matchProperty == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("InterlockProperty", dynamicProperty.PropertyName, i + 1))); failed = true; continue; } //InterlockTypeProperty InterlockTypeProperty matchTypeProperty = (from x in Cee.InterlockTypeProperties where x.InterlockPropertyId == matchProperty.Id && x.InterlockTypeId == interlockType.Id select x).FirstOrDefault(); if (matchTypeProperty == null) { string message = string.Format("Missing a entry in Database for the Table 'InterlockTypeProperty' Where InterlockTypeId = '{0}'({1}) And InterlockPropertyId = '{2}' ({3}). Row {4}.", interlockType.Id, interlockType.Name, matchProperty.Id, matchProperty.Name, i); RaiseMessage(CommonUtils.MessageType.Error, message); failed = true; continue; } if (PropertyValueToPropertyTypeMismatched(matchProperty.PropertyListId, matchProperty.Type, dynamicProperty, i + 1)) { continue; } //NEW var propertyValue = new InterlockPropertyValue { InterlockPropertyId = matchProperty.Id, Value = dynamicProperty.PropertyValue, VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName) }; //add to interlock interlock.InterlockPropertyValues.Add(propertyValue); var pair = new PropertyNameComponentNamePair { ComponentName = string.Format("Control:{0}- Interlock:{1}", controlSystemName, interlockType.Name), PropertyName = matchProperty.Name, Value = dynamicProperty.PropertyValue }; results.Add(pair); } return results; }
private void SaveInterlockRisks(Interlock interlock, CmsEntities cee) { //Delete all existing risks that belongs to the interlock cee.DeleteWhere<InterlockRisk>(cee, x => x.InterlockId == interlock.Id); cee.Configuration.AutoDetectChangesEnabled = false; foreach (var item in interlock.InterlockRisks) { var newInterlockRisk = new InterlockRisk(); newInterlockRisk.InterlockId = item.InterlockId; newInterlockRisk.CategoryId = item.CategoryId; newInterlockRisk.RiskLikelihoodId = item.RiskLikelihoodId; newInterlockRisk.ConsequenceId = item.ConsequenceId; newInterlockRisk.RiskMatrixId = item.RiskMatrixId; newInterlockRisk.Description = item.Description; cee.InterlockRisks.Add(newInterlockRisk); } cee.Configuration.AutoDetectChangesEnabled = true; }
private string GetPropertiesFromInterlock(Interlock interlock, CmsEntities cee) { var propertyValues = (from x in interlock.InterlockPropertyValues select x).ToList(); var list = new List<SimpleInterlockProperty>(); foreach (var propertyValue in propertyValues) { if (propertyValue.InterlockProperty == null) { var match = (from x in cee.InterlockProperties where x.Id == propertyValue.InterlockPropertyId select x).FirstOrDefault(); if (match != null) { var dto = new SimpleInterlockProperty(); dto.Name = match.Name; dto.Value = propertyValue.Value; list.Add(dto); } } } var propertyMessages = list.Select(propertyValue => string.Format("{0} = {1}", propertyValue.Name, propertyValue.Value)).ToList(); var result = string.Join(", ", propertyMessages); return result; }
private void DeleteIssueIssueRelatedOverrides(ControlSystem controlSystem, Interlock interlock, CmsEntities cee, int userId) { const string Date24HTimestamp = "dd/MM/yyyy HH:mm"; var issueRelatedOverrides = (from x in cee.IssueRelatedOverrides.Include("Issue") where x.InterlockId == interlock.Id select x).ToList(); var user = (from x in cee.Users where x.Id == userId select x).FirstOrDefault(); foreach (var relatedOverride in issueRelatedOverrides) { var appliedByUser = (from x in cee.Users where x.Id == relatedOverride.AppliedById select x).FirstOrDefault(); var responseText = string.Format("Related Override {0}-{1}-{2} deleted from Control Module {3} by {4} on {5}. Override Applied on {6} by {7}. Override Removed on {8} by {4}", interlock.InterlockType.Name, interlock.Number, interlock.Cause, controlSystem.Name, user.FirstLastName, DateTime.Now.ToString(Date24HTimestamp), relatedOverride.AppliedDate, appliedByUser.FirstLastName, DateTime.Now.ToString(Date24HTimestamp)); var issueResponse = new IssueResponse { IssueId = relatedOverride.IssueId, PriorityId = relatedOverride.Issue.IssuePriorityId, UserId = user.Id, StatusId = relatedOverride.Issue.IssueStatusId, AssignedToId = relatedOverride.Issue.CurrentlyAssignedToId, Date = DateTime.Now, ResponseText = responseText }; cee.IssueRelatedOverrides.Remove(relatedOverride); cee.IssueResponses.Add(issueResponse); } }
private void DeleteInterlock(ControlSystem controlSystem, Interlock interlock, CmsEntities cee, int userId) { //InterlockPropertyValue var interlockPropertyValues = (from x in cee.InterlockPropertyValues where x.InterlockId == interlock.Id select x).ToList(); foreach (var interlockPropertyValue in interlockPropertyValues) { cee.InterlockPropertyValues.Remove(interlockPropertyValue); } DeleteIssueIssueRelatedOverrides(controlSystem, interlock, cee, userId); cee.SaveChanges(); //InterlockRisk var interlockRisks = (from x in cee.InterlockRisks where x.InterlockId == interlock.Id select x).ToList(); foreach (var interlockRisk in interlockRisks) { cee.InterlockRisks.Remove(interlockRisk); } cee.SaveChanges(); //Interlocks var q = (from x in cee.Interlocks where x.Id == interlock.Id select x).FirstOrDefault(); if (q != null) { cee.Interlocks.Remove(q); cee.SaveChanges(); } }
public DbOperationResult SaveControlSystemInterlocks(List<Interlock> controlSystemInterlocks) { var result = new DbOperationResult(); using (var cee = new CmsEntities()) { try { foreach (var interlock in controlSystemInterlocks) { var q = (from x in cee.Interlocks where x.Id == interlock.Id select x).FirstOrDefault(); if (q != null) { //Update Interlock cee.Entry(q).CurrentValues.SetValues(interlock); } else { q = new Interlock { ControlSystemId = interlock.ControlSystemId, InterlockTypeId = interlock.InterlockTypeId, Number = interlock.Number, Cause = interlock.Cause, Ordinal = interlock.Ordinal, LastModifiedById = interlock.LastModifiedById, LastModifiedDate = interlock.LastModifiedDate, Description = interlock.Description }; //Add new Interlock cee.Interlocks.Add(q); } foreach (var interlockPropertyValue in interlock.InterlockPropertyValues) { var qq = (from x in cee.InterlockPropertyValues where x.Id == interlockPropertyValue.Id select x).FirstOrDefault(); if (qq != null) { cee.Entry(qq).CurrentValues.SetValues(interlockPropertyValue); } else { cee.InterlockPropertyValues.Add(interlockPropertyValue); } } SaveInterlockRisks(interlock, cee); cee.SaveChanges(); } } catch (Exception ex) { result.ServerErrorMessages.Add(String.Format("Error Saving Control System Interlocks: {0}", ex.Message)); } } return result; }