-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReader.cs
133 lines (112 loc) · 4.21 KB
/
FileReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using CWI_SalesAnalytics;
using CWI_SalesAnalytics.Models;
using CWI_SellAnalytics.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading;
namespace CWI_SellAnalytics
{
class FileReader
{
private string line;
DataProcess DataProcess = new DataProcess();
GenerateAnalytics generateAnalytics = new GenerateAnalytics();
public void fileReader(string path)
{
List<VendedorModel> Salesman = new List<VendedorModel>();
List<ClienteModel> Client = new List<ClienteModel>();
List<VendaModel> Sale = new List<VendaModel>();
Stream stream = null;
try
{
FileInfo fl = new FileInfo(path);
while (IsFileLocked(fl))
{
Thread.Sleep(2000);
}
stream = new FileStream(path, FileMode.OpenOrCreate);
// Open the text file using a stream reader.
using (var sr = new StreamReader(stream))
{
stream = null;
while ((line = sr.ReadLine()) != null)
{
var lineArr = line.Split("ç");
if (line.Length > 0)
{
switch (line.Split("ç")[0])
{
case "001":
Salesman.Add(DataProcess.SalesmanProcess(lineArr));
break;
case "002":
Client.Add(DataProcess.ClientProcess(lineArr));
break;
case "003":
Sale.Add(DataProcess.SaleProcess(lineArr));
break;
default:
Console.WriteLine("Default case");
break;
}
}
}
}
}
catch (IOException ex)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(ex.Message);
}
finally
{
if (stream != null)
stream.Dispose();
}
createOutFile(generateAnalytics.AnalyticProcess(Sale, Client, Salesman), Path.GetFileNameWithoutExtension(path));
}
private void createOutFile(SalesAnalyticsModel salesAnalyticsModel, string fileNameIN)
{
System.IO.Directory.CreateDirectory(string.Format(@"{0}\Data\Out", Environment.CurrentDirectory));
string pathString = string.Format(@"{0}\Data\Out", Environment.CurrentDirectory);
string fileName = string.Format("{0}_OUT.json", fileNameIN);
pathString = System.IO.Path.Combine(pathString, fileName);
if (!System.IO.File.Exists(pathString))
{
var responseData = salesAnalyticsModel;
string jsonData = JsonConvert.SerializeObject(responseData, Formatting.None);
System.IO.File.WriteAllText(pathString, jsonData);
}
else
{
Console.WriteLine("Aquivo \"{0}\" já existe.", fileName);
return;
}
}
protected virtual bool IsFileLocked(FileInfo file)
{
try
{
using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
{
stream.Close();
}
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
//file is not locked
return false;
}
}
}