Example #1
0
        public override void ImportData()
        {
            if (fileInfo != null && !string.IsNullOrWhiteSpace(fileInfo.FilePath))
            {
                using (FileStream stream = File.Open(fileInfo.FilePath, FileMode.Open))
                {
                    if (csvValidateService.FileIsEmpty(stream))
                    {
                        throw new Exception("CSV file is empty");
                    }
                    if (!csvValidateService.IsFileCsvXls(fileInfo.FileName))
                    {
                        throw new Exception("Please, upload CSV file");
                    }
                    ParseResult <Models.Condor.KONDOR_DATA> KONDOR_DATAcsv = csvParseService.ParseData(stream, ",");
                    IList <Models.KONDOR_DATA> csvRecordList = new List <Models.KONDOR_DATA>();
                    if (!KONDOR_DATAcsv.IsValid)
                    {
                        var builder = new StringBuilder("There are some problem \n");

                        foreach (var error in KONDOR_DATAcsv.Errors)
                        {
                            foreach (var subError in error.Value)
                            {
                                builder.Append(string.Format("{0}\n", subError));
                            }
                        }
                        throw new Exception(builder.ToString());
                    }
                    foreach (var record in KONDOR_DATAcsv.Records)
                    {
                        var fileRecord = new Models.Condor.KONDOR_DATA().MapToKONDOR_DATAEntity(record);
                        csvRecordList.Add(fileRecord);
                    }
                    if (csvRecordList.Count > 0)
                    {
                        _Context.Database.ExecuteSqlCommand("truncate table KONDOR_DATA");
                        marketRRepo.AddRange(csvRecordList);
                        marketRRepo.UnitOfWork.SaveChanges();
                    }
                }
            }
        }
Example #2
0
        public override void Import()
        {
            FileInfo[] files = GetAllFiles(setting.FolderPath, new List <string> {
                ".csv"
            });
            if (files.Length > 0)
            {
                foreach (FileInfo file in files)
                {
                    try
                    {
                        DateTime fileDate;
                        IList <DAL.Models.KONDOR_DATA> csvRecordList = new List <DAL.Models.KONDOR_DATA>();
                        var splitFileName = file.Name.Split('_').ToList();
                        if (splitFileName != null && splitFileName.Count > 1)
                        {
                            var    fileDateStr = splitFileName.ElementAt(1).Substring(0, splitFileName.ElementAt(1).IndexOf('.')).Replace("-", "") + "00";
                            string format      = "yyyyMMddHHmmss";
                            try
                            {
                                fileDate = DateTime.ParseExact(fileDateStr, format, CultureInfo.InvariantCulture);
                            }
                            catch (Exception ex) { throw new Exception("Error! file name sholud be in formate MarketRisk_20180630-0331.xls or MarketRisk_20180630-0331.csv"); }
                        }
                        else
                        {
                            throw new Exception("Error! file name sholud be in formate MarketRisk_20180630-0331.xls or MarketRisk_20180630-0331.csv");
                        }
                        if (file == null)
                        {
                            throw new Exception("Please, choose CSV file");
                        }
                        bool IsValid = false;
                        using (FileStream stream = File.Open(setting.FolderPath + "/" + file.Name, FileMode.Open))
                        {
                            if (csvValidateService.FileIsEmpty(stream))
                            {
                                throw new Exception("CSV file is empty");
                            }
                            if (!csvValidateService.IsFileCsvXls(file.Name))
                            {
                                throw new Exception("Please, upload CSV file");
                            }
                            ParseResult <DAL.Models.Condor.KONDOR_DATA> KONDOR_DATAcsv = csvParseService.ParseData(stream, ",");

                            if (!KONDOR_DATAcsv.IsValid)
                            {
                                var builder = new StringBuilder("There are some problem \n");

                                foreach (var error in KONDOR_DATAcsv.Errors)
                                {
                                    foreach (var subError in error.Value)
                                    {
                                        builder.Append(string.Format("{0}\n", subError));
                                    }
                                }
                                throw new Exception(builder.ToString());
                            }
                            else
                            {
                                IsValid = true;
                            }
                        }
                        if (IsValid)
                        {
                            if (!Directory.Exists(setting.FileSavePath))
                            {
                                Directory.CreateDirectory(setting.FileSavePath);
                            }
                            if (File.Exists(Path.Combine(setting.FileSavePath, file.Name)))
                            {
                                File.Delete(Path.Combine(setting.FileSavePath, file.Name));
                            }
                            File.Copy(Path.Combine(setting.FolderPath, file.Name), Path.Combine(setting.FileSavePath, file.Name));

                            KondorFileHistory fileHistory = new KondorFileHistory();
                            fileHistory.FileName    = file.Name;
                            fileHistory.FilePath    = Path.Combine(setting.FileSavePath, file.Name);
                            fileHistory.CreatedDate = DateTime.Now;
                            fileHistory.FileDate    = fileDate;
                            AddFileHistory(fileHistory);
                        }
                        if (!Directory.Exists(setting.BackupFolderPath))
                        {
                            Directory.CreateDirectory(setting.BackupFolderPath);
                        }
                        File.Move(setting.FolderPath + "/" + file, setting.BackupFolderPath + "/" + file);
                    }
                    catch (Exception ex)
                    {
                        HandleValidationErrorException(ex, setting.NotificationEmail, file.Name);
                        throw ex;
                    }
                }
            }
        }
        public JsonResult FileManagerUpload(HttpPostedFileBase files)
        {
            DateTime fileDate;
            var      splitFileName = files.FileName.Split('_').ToList();

            if (splitFileName != null && splitFileName.Count > 1)
            {
                var    fileDateStr = splitFileName.ElementAt(1).Substring(0, splitFileName.ElementAt(1).IndexOf('.')).Replace("-", "") + "00";
                string format      = "yyyyMMddHHmmss";
                fileDate = DateTime.ParseExact(fileDateStr, format, CultureInfo.InvariantCulture);
                if (fileDate == DateTime.MinValue)
                {
                    return(Json(new { Success = false, Message = "Error! file name sholud be in formate MarketRisk_20180630-0331.xls or MarketRisk_20180630-0331.xlsx" }));
                }
            }
            else
            {
                return(Json(new { Success = false, Message = "Error! file name sholud be in formate MarketRisk_20180630-0331.xls or MarketRisk_20180630-0331.xlsx" }));
            }
            var calculatedData = new List <FileCalculationViewModel>();

            if (ModelState.IsValid)
            {
                if (files == null)
                {
                    return(Json(new { Success = false, Message = "Please, choose CSV file" }));
                }

                if (csvValidateService.FileIsEmpty(files.InputStream))
                {
                    eventLog.SaveEventLog(ConstantEvent.EmptyCsvFile, ConstantEvent.Failed);
                    return(Json(new { Success = false, Message = "CSV file is empty" }));
                }
                if (!csvValidateService.IsFileCsvXls(files.FileName))
                {
                    eventLog.SaveEventLog(ConstantEvent.InvalidFileType, ConstantEvent.Failed);
                    return(Json(new { Success = false, Message = "Please, upload CSV file" }));
                }
                try
                {
                    ParseResult <DAL.Models.Condor.KONDOR_DATA> KONDOR_DATAcsv = csvParseService.ParseData(files.InputStream, ",");
                    if (!KONDOR_DATAcsv.IsValid)
                    {
                        var builder = new StringBuilder("There are some problem \n");

                        foreach (var error in KONDOR_DATAcsv.Errors)
                        {
                            foreach (var subError in error.Value)
                            {
                                builder.Append(string.Format("{0}\n", subError));
                            }
                        }
                        eventLog.SaveEventLog(ConstantEvent.ErrorCsvParsing, ConstantEvent.Failed);
                        return(Json(new { Success = false, Message = builder.ToString() }));
                    }
                    #region Saving csv file and doing calculation
                    //saving file and it's path

                    var setting = analyticsRepo.GetImportSetting(fileType.Csv);
                    if (setting != null && !string.IsNullOrWhiteSpace(setting.FileSavePath))
                    {
                        string targetPath = Path.Combine(setting.FileSavePath, files.FileName);
                        files.SaveAs(targetPath);

                        KondorFileHistory fileHistory = new KondorFileHistory();
                        fileHistory.FileName    = files.FileName;
                        fileHistory.FilePath    = targetPath;
                        fileHistory.CreatedDate = DateTime.Now;
                        fileHistory.FileDate    = fileDate;
                        marketRRepo.Add <KondorFileHistory>(fileHistory);
                        marketRRepo.UnitOfWork.SaveChanges();

                        var calculation = marketRRepo.Find <FileCalculation>(x => x.FileID == fileHistory.FileID).ToList();
                        calculatedData = AutoMapper.Mapper.Map <List <FileCalculation>, List <FileCalculationViewModel> >(calculation);
                    }
                    #endregion
                }
                catch (CsvParseException ex)
                {
                    eventLog.SaveEventLog(ConstantEvent.CsvParseExceptionWhileUploading, ConstantEvent.Failed);
                    return(Json(new { Success = false, Message = "Could not read file!" }));
                }
                catch (Exception ex)
                {
                    eventLog.SaveEventLog(ConstantEvent.ExceptionWhileUploading, ConstantEvent.Failed);
                    var t = ex.GetType();
                    //todo:must be logger
                    return(Json(new { Success = false, Message = "Could not read file!" }));
                }
            }
            eventLog.SaveEventLog(ConstantEvent.FileUploaded, ConstantEvent.Successful);
            //return Json(new { Success = true, Message = "Uploaded successfully" });
            //return Json(calculatedData, JsonRequestBehavior.AllowGet);
            return(Json(new { calculatedData = calculatedData, Success = true }, JsonRequestBehavior.AllowGet));
        }