-
Notifications
You must be signed in to change notification settings - Fork 130
/
Persistence.cs
111 lines (88 loc) · 3.43 KB
/
Persistence.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Sdl.Community.InSource.Models;
namespace Sdl.Community.InSource
{
public class Persistence
{
private readonly string _persistancePath;
public Persistence()
{
_persistancePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
@"Trados AppStore\StudioAutomation\studioAutomation.json");
}
public void SaveProjectRequestList(List<ProjectRequest> persistenceListFolders)
{
var jsonText = File.ReadAllText(_persistancePath);
var request = JsonConvert.DeserializeObject<Request>(jsonText);
request.ProjectRequest = persistenceListFolders;
var json = JsonConvert.SerializeObject(request);
File.WriteAllText(_persistancePath, json);
}
public void SaveTimerSettings(TimerModel settings)
{
var jsonText = File.ReadAllText(_persistancePath);
var request = JsonConvert.DeserializeObject<Request>(jsonText);
request.Timer = settings;
var json = JsonConvert.SerializeObject(request);
File.WriteAllText(_persistancePath, json);
}
private void CreateNewJsonFile(Request recoveredJson)
{
if (!File.Exists(_persistancePath))
{
var directory = Path.GetDirectoryName(_persistancePath);
if (directory != null && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
recoveredJson.DeleteFolders = false;
var json = JsonConvert.SerializeObject(recoveredJson);
File.WriteAllText(_persistancePath, json);
}
public void Update(ProjectRequest projectRequest)
{
var projectRequestList = Load();
var projectToUpdate = projectRequestList.FirstOrDefault(p => p.Name == projectRequest.Name);
if (projectToUpdate != null) projectToUpdate.Files = new string[] {};
SaveProjectRequestList(projectRequestList);
}
public void UpdateDelete(bool delete)
{
var savedRequest = LoadRequest();
savedRequest.DeleteFolders = delete;
var json = JsonConvert.SerializeObject(savedRequest);
File.WriteAllText(_persistancePath, json);
}
public List<ProjectRequest> Load()
{
if (!File.Exists(_persistancePath)) return null;
var json = File.ReadAllText(_persistancePath);
var result = JsonConvert.DeserializeObject<Request>(json);
if (result != null) { return result.ProjectRequest; }
return new List<ProjectRequest>();
}
public Request LoadRequest()
{
if (!File.Exists(_persistancePath))
{
CreateNewJsonFile(new Request
{
ProjectRequest = new List<ProjectRequest>(),
Timer = new TimerModel
{
HasTimer = false
},
DeleteFolders = false
});
}
var json = File.ReadAllText(_persistancePath);
var savedData = JsonConvert.DeserializeObject<Request>(json);
return savedData;
}
}
}