-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.cs
207 lines (167 loc) · 6.39 KB
/
Utilities.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
195
196
197
198
199
200
201
202
203
204
205
206
207
using FinSearchDataAccessLibrary.Models.Database;
using FinSearchDataAcessLibrary.DataAccess;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.FileIO;
namespace FinSearchDataAPI
{
public static class Utilities
{
static ILogger Logger = new LoggerFactory().CreateLogger("Utilies");
public static bool DatabaseISeeded { get; private set; }
public static string Serialize(this object metaToken)
{
if (metaToken == null)
{
return null;
}
return JsonConvert.SerializeObject(metaToken);
}
public static JObject Deserialize(this object metaToken)
{
if (metaToken == null)
{
return null;
}
return JObject.FromObject(metaToken);
}
public static IDictionary<string, string> ToKeyValue(this object metaToken)
{
if (metaToken == null)
{
return null;
}
JToken token = metaToken as JToken;
if (token == null)
{
return ToKeyValue(JObject.FromObject(metaToken));
}
if (token.HasValues)
{
var contentData = new Dictionary<string, string>();
foreach (var child in token.Children().ToList())
{
var childContent = child.ToKeyValue();
if (childContent != null)
{
contentData = contentData.Concat(childContent)
.ToDictionary(k => k.Key, v => v.Value);
}
}
return contentData;
}
var jValue = token as JValue;
if (jValue?.Value == null)
{
return null;
}
var value = jValue?.Type == JTokenType.Date ?
jValue?.ToString("o", CultureInfo.InvariantCulture) :
jValue?.ToString(CultureInfo.InvariantCulture);
return new Dictionary<string, string> { { token.Path, value } };
}
/// <summary>
/// Takes the full name of a resource and loads it in to a stream.
/// </summary>
/// <param name="resourceName">Assuming an embedded resource is a file
/// called info.png and is located in a folder called Resources, it
/// will be compiled in to the assembly with this fully qualified
/// name: Full.Assembly.Name.Resources.info.png. That is the string
/// that you should pass to this method.</param>
/// <returns></returns>
public static Stream GetEmbeddedResourceStream(string resourceName)
{
return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
/// <summary>
/// Get the list of all emdedded resources in the assembly.
/// </summary>
/// <returns>An array of fully qualified resource names</returns>
public static string[] GetEmbeddedResourceNames()
{
return Assembly.GetExecutingAssembly().GetManifestResourceNames();
}
public static void SeedLookUpData(string path, FinSearchDBContext FinSearchDbContext)
{
if (DatabaseISeeded)
return;
//Should only be used on creation of database from local file.
string json = GetCSVJson(path);
// and add to db
AddLookUpDataFromJson(json, FinSearchDbContext);
DatabaseISeeded = true;
}
private static string GetCSVJson(string path)
{
//For loading csv lookupTable data at the creation of database
var g = GetDataTableFromCSVFile(path);
// serialize datatable
string json = JsonConvert.SerializeObject(g, Formatting.Indented);
return json;
}
public static void AddLookUpDataFromJson(string json, FinSearchDBContext context)
{
List<LookUpRow> dataCollection = GetLookUpRowCollection(json);
using (context)
{
context.BloomBergLookUp.AddRange(dataCollection);
context.SaveChanges();
}
context.DisposeAsync();
}
public static List<LookUpRow> GetExcelData(string path)
{
return GetLookUpRowCollection(GetCSVJson(path));
}
private static List<LookUpRow> GetLookUpRowCollection(string json)
{
/// Serialize json Add look up data to database.
return (JsonConvert.DeserializeObject<List<LookUpRow>>(json)).Where(x => x.CORPEXCHANGE != null).ToList();
}
private static DataTable GetDataTableFromCSVFile(string csv_file_path)
{
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
}
return csvData;
}
}
}