예제 #1
0
        public ActionResult UploadProcess(int customerId)
        {
            EventCodesUploadResultsModel model;

            var eventCodesFile = Session[SessionFilePath] as string;
            var fileName       = Session[SessionFileName] as string;

            if (string.IsNullOrEmpty(eventCodesFile))
            {
                model = new EventCodesUploadResultsModel
                {
                    CustomerId = customerId
                };
                model.Errors.Add("Unable to process uploaded file");
            }
            else
            {
                // Process the file.
                model = (new UploadFactory(Session[Constants.Security.ConnectionStringSessionVariableName].ToString())).Process(customerId, eventCodesFile);

                // Note name of file in Results log.
                model.Results.Insert(0, "File Processed: " + fileName);

                // Clear the session variable.
                Session.Remove(SessionFilePath);
                Session.Remove(SessionFileName);

                // Delete the file.
                System.IO.File.Delete(eventCodesFile);
            }
            return(View(model));
        }
예제 #2
0
        /// <summary>
        /// Process a event code file uplaoded for a specific customer
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public EventCodesUploadResultsModel Process(int customerId, string file)
        {
            var model = new EventCodesUploadResultsModel
            {
                CustomerId       = customerId,
                UploadedFileName = file
            };

            // Prep the LINQtoCSV context.

            _csvContext         = new CsvContext();
            _csvFileDescription = new CsvFileDescription
            {
                MaximumNbrExceptions = 100,
                SeparatorChar        = ','
            };

            try
            {
                ProcessEventCodes(customerId, model);
            }
            catch (AggregatedException ae)
            {
                // Process all exceptions generated while processing the file
                var innerExceptionsList =
                    (List <Exception>)ae.Data["InnerExceptionsList"];

                foreach (Exception e in innerExceptionsList)
                {
                    model.Errors.Add(e.Message);
                }
            }
            catch (Exception ex)
            {
                model.Errors.Add("General exception");
                model.Errors.Add(ex.Message);
            }


            return(model);
        }
예제 #3
0
        /// <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));
                }
            }
        }