public ActionResult Edit(EventCodeEditModel model) { // Save values if (!ModelState.IsValid) { (new EventCodesFactory(Session[Constants.Security.ConnectionStringSessionVariableName].ToString())).RefreshEventCodeEditModel(model); return(View(model)); } (new EventCodesFactory(Session[Constants.Security.ConnectionStringSessionVariableName].ToString())).SetEventCodeEditModel(model); // Saved and stay on same page. if (model.FromDetailsPage) { return(RedirectToAction("Details", new { customerId = model.CustomerId, eventSourceId = model.SourceId, eventCodeId = model.Id })); } return(RedirectToAction("EditEventCodes", "Customers", new { customerId = model.CustomerId })); }
/// <summary> /// Check to see if there is already an EventCode for this Customer with the following attributes: /// SourceId, TypeId, CategoryId and AssetTypeId /// </summary> /// <param name="model"></param> /// <returns></returns> public bool CanCreateEventCode(EventCodeEditModel model) { // Check to see if there is already an EventCode for this Customer with the following attributes: // SourceId, TypeId, CategoryId and AssetTypeId EventCodeAssetType ecat = null; EventCode ecm = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventType == model.TypeId && m.EventCategory == model.CategoryId); if (ecm != null) { // Does this EventCode have the same asset type already assigned? ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == ecm.CustomerID && m.EventSource == ecm.EventSource && m.EventCode == ecm.EventCode1); } return(ecm != null && ecat != null); }
/// <summary> /// Updates the event code edit model /// </summary> /// <param name="model"></param> public void RefreshEventCodeEditModel(EventCodeEditModel model) { GetEventCodesCustomerModel(model); if (model != null) { model.Category = EventCategoryList(model.CategoryId); model.Type = EventTypeList(model.TypeId); model.Source = EventSourceList(model.SourceId); model.AlarmTier = AlarmTierList(model.AlarmTierId); // Get Asset Type Id var ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode == model.Id); if (ecat != null && ecat.MeterGroup != null) { model.AssetTypeId = ecat.MeterGroupId; } model.AssetType = MeterGroupList(model.CustomerId, model.AssetTypeId); } }
public ActionResult Create(EventCodeEditModel model) { // Save values if (!ModelState.IsValid) { (new EventCodesFactory(Session[Constants.Security.ConnectionStringSessionVariableName].ToString())).RefreshEventCodeEditModel(model); return(View(model)); } var eventCodesFactory = new EventCodesFactory(Session[Constants.Security.ConnectionStringSessionVariableName].ToString()); // Does the requested EventCode settings indicae an // EventCode that already exists? if (eventCodesFactory.CanCreateEventCode(model)) { ModelState.AddModelError("Exists", "Event Code with this same Event Source, Event Type, Event Category and Asset Type already exists."); (new EventCodesFactory(Session[Constants.Security.ConnectionStringSessionVariableName].ToString())).RefreshEventCodeEditModel(model); return(View(model)); } (new EventCodesFactory(Session[Constants.Security.ConnectionStringSessionVariableName].ToString())).CreateEventCode(model); return(RedirectToAction("EditEventCodes", "Customers", new { customerId = model.CustomerId })); }
/// <summary> /// sets the event code edit model with the event code data in the system /// </summary> /// <param name="model"></param> public void SetEventCodeEditModel(EventCodeEditModel model) { // Get the original EventCode var eventCode = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode1 == model.Id); if (eventCode != null) { eventCode.AlarmTier = model.AlarmTierId; eventCode.EventDescAbbrev = model.DescAbbrev; eventCode.EventDescVerbose = model.DescVerbose; eventCode.SLAMinutes = model.ApplySLA ? model.SLAMinutes : null; eventCode.ApplySLA = model.ApplySLA; eventCode.IsAlarm = model.IsAlarm; eventCode.EventType = model.TypeId; eventCode.EventCategory = model.CategoryId; // Insert/update [EventCodeAssetType] var ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode == model.Id); if (ecat == null) { ecat = new EventCodeAssetType() { CustomerID = model.CustomerId, EventSource = model.SourceId, EventCode = model.Id, }; PemsEntities.EventCodeAssetTypes.Add(ecat); } ecat.MeterGroupId = model.AssetTypeId; // Save changes PemsEntities.SaveChanges(); // Add audit record. Audit(eventCode); } }
/// <summary> /// Created an event code int he system /// </summary> /// <param name="model"></param> /// <returns></returns> public int CreateEventCode(EventCodeEditModel model) { EventCode eventCode = null; // Get next EventCode id for a given customer & EventSource. // If model.Id is non-zero then a particular event code id has been requested. int nextId = model.Id; eventCode = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode1 == nextId); if (eventCode == null) { if (nextId < 0) { nextId = 1; eventCode = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId); if (eventCode != null) { nextId = PemsEntities.EventCodes.Where(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId).Max(m => m.EventCode1) + 1; } } // Create the new EventCode eventCode = new EventCode() { CustomerID = model.CustomerId, EventSource = model.SourceId, EventCode1 = nextId, AlarmTier = model.AlarmTierId, EventDescAbbrev = model.DescAbbrev, EventDescVerbose = model.DescVerbose, SLAMinutes = model.ApplySLA ? model.SLAMinutes : null, ApplySLA = model.ApplySLA, IsAlarm = model.IsAlarm, EventType = model.TypeId, EventCategory = model.CategoryId }; PemsEntities.EventCodes.Add(eventCode); PemsEntities.SaveChanges(); } else { eventCode.AlarmTier = model.AlarmTierId; eventCode.EventDescAbbrev = model.DescAbbrev; eventCode.EventDescVerbose = model.DescVerbose; eventCode.SLAMinutes = model.ApplySLA ? model.SLAMinutes : null; eventCode.ApplySLA = model.ApplySLA; eventCode.IsAlarm = model.IsAlarm; eventCode.EventType = model.TypeId; eventCode.EventCategory = model.CategoryId; PemsEntities.SaveChanges(); } // Insert/update [EventCodeAssetType] var ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode == nextId); if (ecat == null) { ecat = new EventCodeAssetType() { CustomerID = model.CustomerId, EventSource = model.SourceId, EventCode = nextId, }; PemsEntities.EventCodeAssetTypes.Add(ecat); } ecat.MeterGroupId = model.AssetTypeId; // Save changes PemsEntities.SaveChanges(); // Add audit record. Audit(eventCode); // Return new EventCode id return(nextId); }
/// <summary> /// GEts an event code EDIT model /// </summary> /// <param name="customerId"></param> /// <param name="eventSourceId"></param> /// <param name="eventCodeId"></param> /// <returns></returns> public EventCodeEditModel GetEventCodeEditModel(int customerId, int eventSourceId = -1, int eventCodeId = -1) { EventCodeEditModel model = null; if (eventCodeId > -1) { // Get existing EventCode model = (from ec in PemsEntities.EventCodes where ec.CustomerID == customerId && ec.EventSource == eventSourceId && ec.EventCode1 == eventCodeId select new EventCodeEditModel() { CustomerId = customerId, SourceId = ec.EventSource, AlarmTierId = ec.AlarmTier, TypeId = ec.EventType ?? -1, DescAbbrev = ec.EventDescAbbrev, DescVerbose = ec.EventDescVerbose, SLAMinutes = ec.SLAMinutes, ApplySLA = ec.ApplySLA ?? false, IsAlarm = ec.IsAlarm ?? false, CategoryId = ec.EventCategory ?? -1, Id = ec.EventCode1, AssetTypeId = -1 }).FirstOrDefault(); } else { // Create a model that will be used to creat a new EventCode model = new EventCodeEditModel() { CustomerId = customerId, SourceId = -1, AlarmTierId = -1, TypeId = -1, CategoryId = -1, Id = -1, AssetTypeId = -1 }; } GetEventCodesCustomerModel(model); if (model != null) { model.Category = EventCategoryList(model.CategoryId); model.Type = EventTypeList(model.TypeId); // Determine the "Alarm Type" type Id model.AlarmTypeId = -1; foreach (var type in model.Type) { if (type.Text.Contains("Alarm") || type.Text.Contains("alarm")) { model.AlarmTypeId = int.Parse(type.Value); break; } } model.Source = EventSourceList(model.SourceId); // Get the present event code source foreach (var source in model.Source) { if (source.Selected) { model.SourceDisplay = source.Text; break; } } model.AlarmTier = AlarmTierList(model.AlarmTierId); // Get Asset Type Id var ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == customerId && m.EventSource == model.SourceId && m.EventCode == model.Id); if (ecat != null && ecat.MeterGroup != null) { model.AssetTypeId = ecat.MeterGroupId; } model.AssetType = MeterGroupList(customerId, model.AssetTypeId); } return(model); }
/// <summary> /// Uploads event codes for a customer /// </summary> /// <param name="customerId"></param> /// <param name="model"></param> private void ProcessEventCodes(int customerId, EventCodesUploadResultsModel model) { IEnumerable <UploadEventCodesModel> eventCodes = _csvContext.Read <UploadEventCodesModel>(model.UploadedFileName, _csvFileDescription); foreach (var code in eventCodes) { bool canCreateEventCode = true; // First, validate that certain data, if present, resolves to the // associated referential table. // "EventSource" == [EventSources] var eventSource = PemsEntities.EventSources.FirstOrDefault(m => m.EventSourceDesc.Equals(code.EventSource, StringComparison.CurrentCultureIgnoreCase)); if (eventSource == null) { model.Errors.Add(string.Format("Record {0}, EventSource '{1}' is invalid.", code.Id, code.EventSource)); canCreateEventCode = false; } // "AlarmTier" == [AlarmTier] var alarmTier = PemsEntities.AlarmTiers.FirstOrDefault(m => m.TierDesc.Equals(code.AlarmTier, StringComparison.CurrentCultureIgnoreCase)); if (alarmTier == null) { model.Errors.Add(string.Format("Record {0}, AlarmTier '{1}' is invalid.", code.Id, code.AlarmTier)); canCreateEventCode = false; } // "EventType" == [EventType] var eventType = PemsEntities.EventTypes.FirstOrDefault(m => m.EventTypeDesc.Equals(code.EventType, StringComparison.CurrentCultureIgnoreCase)); if (eventType == null) { model.Errors.Add(string.Format("Record {0}, EventType '{1}' is invalid.", code.Id, code.EventType)); canCreateEventCode = false; } // "EventCategory" == [EventCategory] var eventCategory = PemsEntities.EventCategories.FirstOrDefault(m => m.EventCategoryDesc.Equals(code.EventCategory, StringComparison.CurrentCultureIgnoreCase)); if (eventCategory == null) { model.Errors.Add(string.Format("Record {0}, EventCategory '{1}' is invalid.", code.Id, code.EventCategory)); canCreateEventCode = false; } // "AssetType" == [MeterGroup]/[AssetType] var meterGroup = (from at in PemsEntities.AssetTypes join mg in PemsEntities.MeterGroups on at.MeterGroupId equals mg.MeterGroupId where at.CustomerId == customerId && at.IsDisplay == true where mg.MeterGroupDesc.Equals(code.AssetType, StringComparison.CurrentCultureIgnoreCase) select mg).Distinct().FirstOrDefault(); if (meterGroup == null) { model.Errors.Add(string.Format("Record {0}, AssetType '{1}' is invalid.", code.Id, code.AssetType)); canCreateEventCode = false; } // Check if SLADuration was given and is greater than 0 and less than 1440. if (code.SLADuration != null) { if (code.SLADuration <= 0 || code.SLADuration > 1440) { model.Errors.Add(string.Format("Record {0}, SLADuration '{1}' is out of range. Range [1, 1440] or blank for no SLA.", code.Id, code.SLADuration)); canCreateEventCode = false; } } // Check if EventCode is in range [0, int.MaxValue] or a -1. if (code.EventCode < -1) { model.Errors.Add(string.Format("Record {0}, EventCode '{1}' is out of range. Range greater than 0 or -1 to generate an EventCode.", code.Id, code.EventCode)); canCreateEventCode = false; } // Can an Event Code be created? If not skip to next record. if (!canCreateEventCode) { continue; } // Now should be able to create or update an EventCode // Does this EventCode already exist? var eventCodesFactory = new EventCodesFactory(ConnectionStringName); bool isNewEventCode = false; EventCodeEditModel eventCode = null; // Does this Event Code already exist? if (code.EventCode >= 0) { eventCode = eventCodesFactory.GetEventCodeEditModel(customerId, eventSource.EventSourceCode, code.EventCode); } // Do I need to create a new event code? if (eventCode == null) { // A new event code needs to be created. eventCode = eventCodesFactory.GetEventCodeEditModel(customerId); // Record the event source id for the new event code. This field is // part of the primary key. eventCode.SourceId = eventSource.EventSourceCode; eventCode.Id = code.EventCode; isNewEventCode = true; } // Update the eventCode with the data from the CSV row. eventCode.AlarmTierId = alarmTier.Tier; eventCode.DescAbbrev = code.EventDescAbbrev.Trim().Substring(0, 16); eventCode.DescVerbose = code.EventDescVerbose.Trim().Substring(0, 50); eventCode.SLAMinutes = code.SLADuration; eventCode.ApplySLA = code.SLADuration != null && code.SLADuration > 0; eventCode.TypeId = eventType.EventTypeId; eventCode.IsAlarm = eventType.EventTypeId == eventCode.AlarmTypeId; eventCode.CategoryId = eventCategory.EventCategoryId; eventCode.AssetTypeId = meterGroup.MeterGroupId; // Wite the new/updated event code to [EventCodes] if (isNewEventCode) { int newId = eventCodesFactory.CreateEventCode(eventCode); model.Results.Add(string.Format("Record {0}, EventCode with id of '{1}' created successfully.", code.Id, newId)); } else { eventCodesFactory.SetEventCodeEditModel(eventCode); model.Results.Add(string.Format("Record {0}, EventCode '{1}' updated successfully.", code.Id, code.EventCode)); } } }