Exemplo n.º 1
0
        /// <summary>
        /// Read & validate the uploaded file
        /// </summary>
        /// <param name="filePath">file path on the server</param>
        /// <returns>List of CSVModels</returns>
        internal List <CSVModel> ProcessFile(string filePath)
        {
            var isDelimiterParsed = char.TryParse(CommonFunctions.GetApplicationSettingValue(Constants.Delimiter), out var result);
            var delimiter         = isDelimiterParsed ? result : ';';
            var records           = new List <CSVModel>();

            using (var stream = new StreamReader(filePath))
                using (var csv = new CsvReader(stream, true, delimiter))
                {
                    var fieldCount = csv.FieldCount;
                    var headers    = csv.GetFieldHeaders();
                    ValidateHeadersAndColumns(fieldCount, headers);
                    var i = 0;
                    while (csv.ReadNextRecord())
                    {
                        var item = new CSVModel
                        {
                            BeginDate = csv[i, headers[0]],
                            IRR_ACB   = csv[i, headers[1]],
                            Id        = ++i
                        };
                        records.Add(item);
                    }
                }

            return(records);
        }
Exemplo n.º 2
0
    private void OnEnable()
    {
        CSVModel model = new CSVModel();

        mCSVController = new CSVController(model);
        view           = new CSVView(mCSVController, "csv");
        view.Init();
    }
        public HttpResponseMessage UploadCSVByCode(HttpRequestMessage request, [FromBody] CSVModel csvModel)
        {
            return(GetHttpResponse(request, () =>
            {
                var results = _ExtractionProcessService.UploadCSVByCode(csvModel.UploadId, csvModel.Content);

                return request.CreateResponse <UploadResult[]>(HttpStatusCode.OK, results);
            }));
        }
Exemplo n.º 4
0
        public string AddCSV(string[] path)
        {
            foreach (var file in path)
            {
                string filename = Path.GetFileNameWithoutExtension(file);

                if (!this.CheckFileNameFormat(filename))
                {
                    var error = new Log()
                    {
                        message = filename + "does not have the correct format"
                    };
                    this._context.LOG.Add(error);
                    this._context.SaveChanges();
                    continue;
                }

                if (!this.CheckValidDate(filename))
                {
                    var error = new Log()
                    {
                        message = filename + "does not have a valid date format"
                    };
                    this._context.LOG.Add(error);
                    this._context.SaveChanges();
                    continue;
                }

                using (var reader = new StreamReader(file))
                {
                    while (!reader.EndOfStream)
                    {
                        var      cvsTemp = new CSVModel();
                        var      line    = reader.ReadLine();
                        string[] values  = line.Split(',');

                        if (values[1].ToString() == "John")
                        {
                            continue;
                        }

                        cvsTemp.csvId     = values[0].ToString();
                        cvsTemp.FirstName = values[1].ToString();
                        cvsTemp.LastName  = values[2].ToString();

                        if (CheckedDuplicate(cvsTemp))
                        {
                            cvsList.Add(cvsTemp);
                            _context.CSV.Add(cvsTemp);
                            this._context.SaveChanges();
                        }
                    }
                }
            }
            return("The CSV files information have been imported successfully.");
        }
        public HttpResponseMessage UploadCSV(HttpRequestMessage request, [FromBody] CSVModel csvModel)
        {
            return(GetHttpResponse(request, () =>
            {
                HttpContext.Current.Server.ScriptTimeout = 3600;
                var results = _ExtractionProcessService.UploadCSV(int.Parse(csvModel.UploadId), csvModel.Content);//csvModel.Truncate,csvModel.PostUploadAction

                return request.CreateResponse <UploadResult[]>(HttpStatusCode.OK, results);
            }));
        }
Exemplo n.º 6
0
        public static CSVModel FromCsv(string csvLine)
        {
            string[] values = csvLine.Split(',');
            CSVModel csv    = new CSVModel();

            csv.VisitorName = values[0];
            csv.NationalId  = values[1];
            csv.CompanyName = values[2];
            csv.Remark      = values[3];
            return(csv);
        }
Exemplo n.º 7
0
 public bool CheckedDuplicate(CSVModel cvs)
 {
     if (!cvsList.Any(c => c.csvId == cvs.csvId && c.FirstName == cvs.FirstName && c.LastName == cvs.LastName))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 8
0
 public CSVViewModel()
 {
     Model                = new CSVModel();
     OpenFileCommand      = new CommandBase(OpenFile);
     CleanFilePathCommand = new CommandBase(
         () => {
         CleanVM();
         Error = string.Empty;
     });
     SetDirectoriesCommand = new WindowStateCommand(new SetDirectories());
     base.SetPrevNext(null, new PDFTable());
 }
Exemplo n.º 9
0
        private void ExportHighFuncToCall(object context)
        {
            //Open file save dialog and save csv file
            Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.Filter = "CSV|*.csv";

            if (dialog.ShowDialog() == true)
            {
                List <CSVInstanceObject> testObjects = CSVModel.populateInstanceObjects();
                var longCsv = CreateCSVFromList.CreateCSV(testObjects);
                File.WriteAllText(dialog.FileName, longCsv.ToString());
            }
        }
Exemplo n.º 10
0
        public static string CreateCSV(string currentFolderPath, CSVModel model, string fileName)
        {
            var csvFilePath = Path.Combine(currentFolderPath, $"{fileName}.csv");

            using (var writer = new StreamWriter(csvFilePath, false))
            {
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteHeader <CSVModel>();
                    csv.NextRecord();
                    csv.WriteRecord(model);
                }
            }
            return(csvFilePath);
        }
Exemplo n.º 11
0
        private void ExportGraphFuncToCall(object context)
        {
            //write CSV files locally in order to export them to excell
            string fullFilepath    = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Data\\100Hz.csv");
            string sampledFilepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Data\\10Hz.csv");

            List <CSVInstanceObject> testObjects = CSVModel.populateInstanceObjects();
            var csvFull = CreateCSVFromList.CreateCSV(testObjects);

            File.WriteAllText(fullFilepath, csvFull.ToString());


            List <CSVInstanceObject> reducedSampleRateObjects = SampleRateConverter.ConvertSampleRate(testObjects);
            var shortCsv = CreateCSVFromList.CreateCSV(reducedSampleRateObjects);

            File.WriteAllText(sampledFilepath, shortCsv.ToString());

            AddExcelGraph.exportGraph(sampledFilepath, fullFilepath, reducedSampleRateObjects.Count, testObjects.Count);
        }
 public IActionResult Post([FromBody] CSVModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             _dataImporter.Import(model, EnumImportType.Coverholder);
             return(Ok());
         }
         else
         {
             return(StatusCode(StatusCodes.Status400BadRequest, ModelState.GetErrors()));
         }
     }
     catch (Exception ex)
     {
         LogHelper.Error(ex);
         return(StatusCode(StatusCodes.Status400BadRequest, Messages.Unexpected_Error_Occurred));
     }
 }
 private void ExportToCsv()
 {
     try
     {
         this.csvModel = new CSVModel();
         csvModel.fd   = new FolderBrowserDialog();
         if (DialogResult.OK == csvModel.fd.ShowDialog())
         {
             csvModel.path = csvModel.fd.SelectedPath;
             SetDataToCSV();
         }
     }
     catch (Exception)
     {
         var msxbxRes = MessageBox.Show("Error exporting to CSV.\nDo you want to try again?", "Error exporting to CSV", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
         if (msxbxRes == DialogResult.Yes)
         {
             ExportToCsv();
         }
     }
 }
Exemplo n.º 14
0
        private static CSVModel GetCsv(YDPostModel post)
        {
            try
            {
                CSVModel csv = new CSVModel()
                {
                    PostDate = post.Dtg.ToString(" dd.MM.yyyy"),
                    PostTime = post.Dtg.ToString(" HH:mm"),
                    PostText = ToUtf8(post.Text),
                    PostUrl  = post.PostUrl?.AbsoluteUri,

                    AuthorName             = ToUtf8(post.Author?.Name),
                    AuthorUrl              = post.Author?.Url?.AbsoluteUri,
                    AuthorSubscribersCount = post.Author?.FollowersCount.ToString(),

                    GroupName             = ToUtf8(post.Group?.Name),
                    GroupUrl              = post.Group.Url?.AbsoluteUri,
                    GroupSubscribersCount = post.Group?.FollowersCount.ToString(),

                    CommentsCount       = post.Stats?.CommentsCount.ToString(),
                    LikeCount           = post.Stats?.LikesCount.ToString(),
                    RepostsCount        = post.Stats?.RepostsCount.ToString(),
                    ViewsCount          = post.Stats?.ViewsCount.ToString(),
                    PotencialViewsCount = post.Group?.FollowersCount.ToString(),

                    PostRegion = ToUtf8(post.Group?.Name),
                };



                return(csv);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        /// <summary>
        /// LoadCSV
        /// Loading Data From CSV File.
        /// </summary>
        /// <param name="CSVPath">DATA path for CSV Extension</param>
        public CSVModel LoadCSV(string CSVPath)
        {
            try
            {
                StreamReader reader = new StreamReader(File.OpenRead(CSVPath));

                CSVModel csvModel = new CSVModel();

                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (!String.IsNullOrWhiteSpace(line))
                    {
                        string[] fields = line.Split(',');
                        if (fields.Length == 4)
                        {
                            var csvLine = new CSVLine();
                            csvLine.CityName     = fields[0];
                            csvLine.CityCode     = fields[1];
                            csvLine.DistrictName = fields[2];
                            csvLine.ZipCode      = fields[3];
                            csvModel.CSVLines.Add(csvLine);
                        }
                    }
                }

                reader.Close();
                reader.Dispose();

                return(csvModel);
            }
            catch (Exception ex)
            {
                throw new Exception("CSV Loading Exception\r\n" + ex.Message);
            }
        }
Exemplo n.º 16
0
 public CSVController(IModel model) : base(model)
 {
     mModel = model as CSVModel;
 }
Exemplo n.º 17
0
        public bool Import(CSVModel csvModel, EnumImportType enumImportType)
        {
            //create file
            var key = $"{csvModel.LegalName}_{csvModel.InternalRef}";

            var currentFolderPath = Path.Combine(_env.ContentRootPath, _appSettings.Value.CSVFolderName, key);

            if (!Directory.Exists(currentFolderPath))
            {
                Directory.CreateDirectory(currentFolderPath);
            }

            //create CSV file
            var csvFilePath = FileHelper.CreateCSV(currentFolderPath, csvModel, key);

            if (string.IsNullOrEmpty(csvFilePath))
            {
                throw new Exception($"{enumImportType.ToString()} csv file not created for {key}");
            }

            //create config template file
            var configTemplateFileName = ImportType.GetConfigTemplate(enumImportType);

            if (string.IsNullOrEmpty(configTemplateFileName))
            {
                throw new Exception($"{enumImportType.ToString()} config template not configured.");
            }

            var configTemplatePath = ConfigTemplateHelper.Create(_env.ContentRootPath, currentFolderPath, _appSettings.Value.ConfigTemplateFolderName, configTemplateFileName, csvFilePath);

            if (string.IsNullOrEmpty(configTemplatePath))
            {
                throw new Exception($"{enumImportType.ToString()} config template not created for {key}");
            }

            //create batch file
            var batchFileTemplateName = ImportType.GetBatchFileTemplate(enumImportType);

            if (string.IsNullOrEmpty(batchFileTemplateName))
            {
                throw new Exception($"{enumImportType.ToString()} batch file template not configured.");
            }

            var templateFilePath = Path.Combine(_env.ContentRootPath, _appSettings.Value.BatchFileTemplateFolderName, batchFileTemplateName);

            if (!File.Exists(templateFilePath))
            {
                throw new Exception($"Batch file template {batchFileTemplateName} does not exists.");
            }

            var batchFilePath = BatchFileTemplateHelper.Create(currentFolderPath, templateFilePath, enumImportType.ToString(), _appSettings.Value.TaskRunnerPath, configTemplatePath);

            if (string.IsNullOrEmpty(configTemplatePath))
            {
                throw new Exception($"{enumImportType.ToString()} batch file template not created for {key}");
            }

            //Execute batch file

            return(true);
        }