-
Notifications
You must be signed in to change notification settings - Fork 0
/
Files.cs
268 lines (237 loc) · 8.1 KB
/
Files.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using Newtonsoft.Json;
using System;
using System.IO;
using Job_Application_Database.Singleton;
using Job_Application_Database.Classes;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using Job_Application_Database.Enum;
using System.Collections.Generic;
namespace Job_Application_Database.IO
{
/// <summary>
/// Class Handling All File Related Functions
/// </summary>
public class Files
{
/// <summary>
/// The Lazy Construction Of The Instance
/// </summary>
private static readonly Lazy<Files> _fm = new Lazy<Files>(() => CreateInstance());
/// <summary>
/// Reference To The Reps Singleton
/// </summary>
private Reps _rm;
/// <summary>
/// Reference To The Jobs Singleton
/// </summary>
private Jobs _jm;
/// <summary>
/// Reference To The Companies Singleton
/// </summary>
private Companies _cm;
/// <summary>
/// Reference To The JobBoards Singleton
/// </summary>
private JobBoards _bm;
/// <summary>
/// The Name Of The File To Save To
/// </summary>
private string _saveFile;
/// <summary>
/// Default Constructor
/// </summary>
private Files()
{
_jm = Jobs.Instance;
_rm = Reps.Instance;
_cm = Companies.Instance;
_bm = JobBoards.Instance;
}
/// <summary>
/// The Instance Of Files Singleton
/// </summary>
public static Files Instance
{
get
{
return _fm.Value;
}
}
/// <summary>
/// Creation Of The Instance
/// </summary>
/// <returns></returns>
private static Files CreateInstance()
{
return Activator.CreateInstance(typeof(Files), true) as Files;
}
/// <summary>
/// Loads The Rep File
/// </summary>
public void LoadRepFile()
{
LoadSingletonObject<Rep>(_rm, Properties.Settings.Default.RepSaveFile);
}
/// <summary>
/// Saves The Rep File
/// </summary>
public void SaveRepFile()
{
SaveSingletonObject(_rm.AllObjects(), Properties.Settings.Default.RepSaveFile);
}
/// <summary>
/// Loads The Job File
/// </summary>
public void LoadJobFile()
{
LoadSingletonObject<Job>(_jm, Properties.Settings.Default.JobSaveFile);
}
/// <summary>
/// Saves The Job File
/// </summary>
public void SaveJobFile()
{
SaveSingletonObject(_jm.AllObjects(), Properties.Settings.Default.JobSaveFile);
}
/// <summary>
/// Loads The Job Board File
/// </summary>
public void LoadBoardFile()
{
LoadSingletonObject<JobBoard>(_bm, Properties.Settings.Default.BoardSaveFile);
}
/// <summary>
/// Saves The Job Board File
/// </summary>
public void SaveBoardFile()
{
SaveSingletonObject(_bm.AllObjects(), Properties.Settings.Default.BoardSaveFile);
}
/// <summary>
/// Opens The Company File
/// </summary>
public void OpenCompanyFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.CurrentDirectory;
ofd.Filter = "All (*.*)|*.*|json (*.json)|*.json";
ofd.FilterIndex = 2;
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == true)
{
_saveFile = ofd.FileName;
Properties.Settings.Default.LastLoadedFile = ofd.FileName;
LoadCompanyFile(ofd.FileName);
}
}
/// <summary>
/// Loads A Specified File
/// </summary>
/// <param name="filepath">The File Path To Load</param>
public void LoadCompanyFile(string filepath)
{
_saveFile = filepath;
string content = new StreamReader(filepath).ReadToEnd();
string[] lines = content.Split('\n');
foreach (string line in lines)
{
Match match = Regex.Match(line, Properties.Settings.Default.CompanyLoadRegex);
if (match.Success)
{
string name = match.Groups["name"].Value;
string website = match.Groups["website"].Value;
string rep = match.Groups["repid"].Value;
string job = match.Groups["jobid"].Value;
string board = match.Groups["boardid"].Value;
string salery = match.Groups["salary"].Value;
string position = match.Groups["position"].Value;
Enum.PositionType p = Enums.ParsePositionType(Int32.Parse(position));
string status = match.Groups["status"].Value;
Enum.ApplicationStatus s = Enums.ParseStatus(Int32.Parse(status));
string date = match.Groups["date"].Value;
string[] dates = date.Split('-');
string location = match.Groups["location"].Value;
string notes = match.Groups["notes"].Value;
Company c = new Company(name, website, Int32.Parse(rep), Int32.Parse(job), Int32.Parse(board), s, new DateTime(Int32.Parse(dates[0]), Int32.Parse(dates[1]), Int32.Parse(dates[2])), location, p, notes);
_cm.AddObject(c);
}
}
}
/// <summary>
/// Saves The Company File
/// </summary>
public void SaveCompanyFile()
{
if (_saveFile == null)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All (*.*)|*.*|json (*.json)|*.json";
sfd.FilterIndex = 2;
sfd.AddExtension = true;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == true)
{
_saveFile = sfd.FileName;
}
else
{
return;
}
}
SaveSingletonObject(_cm.AllObjects(), _saveFile);
}
/// <summary>
/// Saves A Singleton's Object Data
/// </summary>
/// <param name="list">The Data To Save</param>
/// <param name="filename">The File Name</param>
private void SaveSingletonObject(List<BaseInfo> list, string filename)
{
string json = String.Empty;
foreach (BaseInfo b in list)
{
json += JsonConvert.SerializeObject(b);
json += "\n";
}
File.WriteAllText(filename, json);
}
/// <summary>
/// Loads A Singleton's Object Data
/// </summary>
/// <typeparam name="T">The Type Of Object Data</typeparam>
/// <param name="bs">The Singleton To Save To</param>
/// <param name="filename">The File To Load From</param>
private void LoadSingletonObject<T>(BaseSingleton bs, string filename)
{
if (File.Exists(filename))
{
string content = new StreamReader(filename).ReadToEnd();
string[] objs = content.Split('\n');
foreach (string obj in objs)
{
if (obj.Length > 0)
{
var b = JsonConvert.DeserializeObject<T>(obj);
bs.AddObject(b as BaseInfo);
}
}
}
else
{
bs.InitObjectList();
SaveSingletonObject(bs.AllObjects(), filename);
}
}
/// <summary>
/// Saves All Files
/// </summary>
public void FullSave()
{
SaveCompanyFile();
SaveJobFile();
SaveRepFile();
SaveBoardFile();
}
}
}