-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseStateCensusAnalyser.cs
194 lines (181 loc) · 7.19 KB
/
baseStateCensusAnalyser.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using CsvReader = LumenWorks.Framework.IO.Csv.CsvReader;
namespace CensusAnalyzer
{
public class CsvStateCensusReadRecord
{
// Variables declarations
string actualPath;
char delimeter;
int numberOfRecord;
//When there is no constructor then Default Constructor will called.
public CsvStateCensusReadRecord()
{
}
//Parameterised constructor
public CsvStateCensusReadRecord(string filePath = null)
{
this.actualPath = filePath;
}
// Declaration of ReadRecords Method
/// <summary>
/// Catches all type of exceptions generated.
/// </summary>
/// <param name="passHeader"></param>
/// <param name="in_delimeter"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public object ReadRecords(string[] passHeader = null, char in_delimeter = ',', string filePath = null)
{
try
{
if (!filePath.Contains(".csv"))
{
throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.INVALID_EXTENSION_OF_FILE, "Invalid Extension of file");
}
else if (!filePath.Contains(actualPath))
{
throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.FILE_NOT_FOUND, "Invalid file");
}
// streams are used to read/write data from csv files
//CsvReader is open source C# library to read CSV data from strings/textFiles
CsvReader csvRecords = new CsvReader(new StreamReader(filePath), true);
int fieldCount = csvRecords.FieldCount;
string[] headers = csvRecords.GetFieldHeaders();
delimeter = csvRecords.Delimiter;
// string ArrayList
List<string[]> record = new List<string[]>();
while (csvRecords.ReadNextRecord())
{
string[] tempRecord = new string[fieldCount];
csvRecords.CopyCurrentRecordTo(tempRecord);
record.Add(tempRecord);
numberOfRecord++;
}
if (numberOfRecord == 0)
{
throw new CSVException(CSVException.ExceptionType.FILE_IS_EMPTY, "This file does not contains any data");
}
if (!in_delimeter.Equals(delimeter))
{
throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.INCORRECT_DELIMETER, "Incorrect Delimeter");
}
else if (!IsHeaderSame(passHeader, headers))
{
throw new CensusAnalyserException(CensusAnalyserException.ExceptionType.INVALID_HEADER_ERROR, "Invalid Header");
}
return numberOfRecord;
}
catch (CensusAnalyserException file_not_found)
{
return file_not_found.Message;
}
catch (CSVException emptyFileException)
{
return emptyFileException.Message;
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
}
//method will compare two headers
//return true if same, otherwise return false
private bool IsHeaderSame(string[] passHeader, string[] headers)
{
if (passHeader.Length != headers.Length)
{
return false;
}
for (int i = 0; i < headers.Length; i++)
{
// ToLower() :- Returns a copy of string converted to lowercase
if (headers[i].ToLower().CompareTo(passHeader[i].ToLower()) != 0)
{
return false;
}
}
return true;
}
/// <summary>
/// Sorting json data based on key using bubble sort.
/// </summary>
/// <param name="jsonFilePath"></param>
/// <param name="key"></param>
/// <returns></returns>
public static JArray SortingJsonBasedOnKey(string jsonFilePath, string key)
{
string jsonFile = File.ReadAllText(jsonFilePath);
JArray CensusArray = JArray.Parse(jsonFile);
//bubble sort data in ascending order
for (int i = 0; i < CensusArray.Count - 1; i++)
{
for (int j = 0; j < CensusArray.Count - i - 1; j++)
{
if (CensusArray[j][key].ToString().CompareTo(CensusArray[j + 1][key].ToString()) > 0)
{
var temp = CensusArray[j + 1];
CensusArray[j + 1] = CensusArray[j];
CensusArray[j] = temp;
}
}
}
return CensusArray;
}
/// <summary>
/// Sort json data file in ascending order based on key value pairs.
/// </summary>
/// <param name="jsonPath"></param>
/// <param name="key"></param>
/// <returns></returns>
public static JArray SortJsonBasedOnKeyAndValueIsNumber(string jsonPath, string key)
{
string jsonFile = File.ReadAllText(jsonPath);
//parsing a json file
JArray CensusArray = JArray.Parse(jsonFile);
//using bubble sort to sort the data in ascending order.
for (int i = 0; i < CensusArray.Count - 1; i++)
{
for (int j = 0; j < CensusArray.Count - i - 1; j++)
{
if ((int)CensusArray[j][key] > (int)CensusArray[j + 1][key])
{
var temp = CensusArray[j + 1];
CensusArray[j + 1] = CensusArray[j];
CensusArray[j] = temp;
}
}
}
return CensusArray;
}
/// <summary>
/// Get first data from json format using key value pair.
/// </summary>
/// <param name="jsonPath"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string RetriveFirstDataOnKey(string jsonPath, string key)
{
string jsonFileText = File.ReadAllText(jsonPath);
JArray jArray = JArray.Parse(jsonFileText);
string firstValue = jArray[0][key].ToString();
return firstValue;
}
/// <summary>
/// Get first data from json format using key value pair.
/// </summary>
/// <param name="jsonPath"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string RetriveLastDataOnKey(string jsonPath, string key)
{
string jsonFileText = File.ReadAllText(jsonPath);
JArray jArray = JArray.Parse(jsonFileText);
string lastValue = jArray[jArray.Count - 1][key].ToString();
return lastValue;
}
}
}