Ejemplo n.º 1
0
        public ActionResult ProcessParts()
        {
            DispatcherPartUploadResultModel model = null;

            string partsFile = Session[SessionFilePath] as string;
            string fileName  = Session[SessionFileName] as string;

            if (string.IsNullOrEmpty(partsFile))
            {
                model = new DispatcherPartUploadResultModel();
                model.Errors.Add("Unable to process uploaded file");
            }
            else
            {
                // Process the file.
                var factory = (new DispatcherWorkOrderFactory(CurrentCity));

                model = factory.ProcessPartImport(partsFile);

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

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

                // Delete the file.
                System.IO.File.Delete(partsFile);
            }
            return(View(model));
        }
        public DispatcherPartUploadResultModel ProcessPartImport(string file)
        {
            var model = new DispatcherPartUploadResultModel()
            {
                UploadedFileName = file
            };

            try
            {
                // Prep the LINQtoCSV context.
                _csvContext         = new CsvContext();
                _csvFileDescription = new CsvFileDescription()
                {
                    MaximumNbrExceptions = 100,
                    SeparatorChar        = ','
                };

                IEnumerable <UploadPartModel> items = _csvContext.Read <UploadPartModel>(file, _csvFileDescription);

                foreach (var item in items)
                {
                    bool canCreateItem = true;

                    // First, validate that certain data, if present, resolves to the
                    // associated referential table.

                    //check name first
                    if (string.IsNullOrEmpty(item.PartName))
                    {
                        model.Errors.Add(string.Format("Record {0}, Part Name is required.", item.Id));
                        canCreateItem = false;
                    }

                    //metergroup
                    var meterGroup = PemsEntities.MeterGroups.FirstOrDefault(m => m.MeterGroupId == item.MeterGroup);
                    if (meterGroup == null)
                    {
                        model.Errors.Add(string.Format("Record {0}, MeterGroup '{1}' is invalid.", item.Id, item.MeterGroup));
                        canCreateItem = false;
                    }

                    //Category - mech master
                    var category = PemsEntities.MechanismMasters.FirstOrDefault(m => m.MechanismId == item.Category);
                    if (category == null)
                    {
                        model.Errors.Add(string.Format("Record {0}, Category '{1}' is invalid.", item.Id, item.Category));
                        canCreateItem = false;
                    }

                    if (item.CostInCents < 0)
                    {
                        model.Errors.Add(string.Format("Record {0}, CostInCents '{1}' is invalid. Defaulting to 0.", item.Id, item.CostInCents));
                        item.CostInCents = 0;
                    }

                    if (item.Status != 1 && item.Status != 0)
                    {
                        model.Errors.Add(string.Format("Record {0}, Status '{1}' is invalid. Defaulting to 1.", item.Id, item.Status));
                        item.Status = 1;
                    }

                    item.PartDesc = item.PartDesc.Trim();
                    item.PartName = item.PartName.Trim();

                    // If a CashBox cannot be created continue on to next record.
                    if (!canCreateItem)
                    {
                        continue;
                    }

                    // Now should be able to create the item
                    // For each uplaoded model, create a new item o r update an existing one
                    //this is an update for create based on part name / meter group, cateegory.
                    var fmPart = MaintenanceEntities.FMParts.FirstOrDefault(x => x.MeterGroup == item.MeterGroup && x.Category == item.Category && x.PartName == item.PartName);
                    if (fmPart == null)
                    {
                        fmPart = CreateAvailablePart(item.Category, item.MeterGroup, item.CostInCents, item.PartDesc, item.PartName, item.Status);
                        model.Results.Add(string.Format("Record {0}, '{1}' added successfully.", item.Id, item.PartName));
                    }
                    //otherwise we want to update it - metergroup, cate, and name wont change
                    else
                    {
                        fmPart.CostInCents = item.CostInCents;
                        fmPart.PartDesc    = item.PartDesc;
                        fmPart.Status      = item.Status;
                        MaintenanceEntities.SaveChanges();
                        model.Results.Add(string.Format("Record {0}, '{1}' updated successfully.", item.Id, item.PartName));
                    }
                }
            }
            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);
        }