-
Notifications
You must be signed in to change notification settings - Fork 3
/
LiteDB.cs
221 lines (180 loc) · 5.6 KB
/
LiteDB.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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using LiteDB;
using GrapeCity.ActiveReports;
using GrapeCity.ActiveReports.PageReportModel;
using GrapeCity.ActiveReports.Aspnetcore.Designer.Services;
using GrapeCity.ActiveReports.Aspnetcore.Designer.Utilities;
using WebDesignerCustomStore.Services;
using WebDesignerCustomStore.Implementation.Storage;
using WebDesignerCustomStore.Implementation.CustomStore;
using WebDesignerCustomStore.Implementation.CustomStore.Images;
using WebDesignerCustomStore.Implementation.CustomStore.Themes;
using WebDesignerCustomStore.Implementation.CustomStore.Reports;
using WebDesignerCustomStore.Implementation.CustomStore.Templates;
using System.Xml;
using System.Text;
namespace WebDesignerCustomStore.Implementation.Database
{
public class LiteDB : ICustomStorage
{
private const string IMAGES = "images";
private const string THEMES = "themes";
private const string REPORTS = "reports";
private const string TEMPLATES = "templates";
private LiteDatabase _lite;
public LiteDB(string databasePath)
{
_lite = new LiteDatabase(databasePath);
}
public void Dispose()
{
_lite.Dispose();
}
public byte[] GetImage(string imageId)
{
return _lite.GetCollection<ImageInfo>(IMAGES)
.FindById(imageId)
.Content;
}
public IEnumerable<IImageInfo> GetImagesList()
{
var imagesList = _lite
.GetCollection<ImageInfo>(IMAGES)
.FindAll()
.Select(img =>
{
img.Name = Uri.UnescapeDataString(img.Id);
img.Thumbnail = Util.GetImageThumbnail(img.Content);
return img;
});
return imagesList;
}
public Report GetReport(string reportId)
{
var (collection, reportName) = Util.GetCollectionAndName(reportId, REPORTS);
var reportItem = _lite.GetCollection<ReportInfo>(collection)
.FindById(reportName);
if (reportItem is null)
return null;
var report = ReportConverter.FromXML(reportItem.Content);
// Define our own resource locator for correct work with report resources (images, themes and so on)
report.Site = new ReportSite(new CustomStoreResourceLocator(this));
return report;
}
public void SaveReport(string reportId, Report report, bool isTemporary = false)
{
var reportXml = ReportConverter.ToXml(report);
var collection = isTemporary ? Util.TEMP_SUFFIX : REPORTS;
_lite
.GetCollection<ReportInfo>(collection)
.Upsert(new ReportInfo
{
Id = reportId,
Name = reportId,
Content = reportXml,
Type = Util.GetReportType(report),
});
}
public void DeleteReport(string id)
{
var (collection, reportName) = Util.GetCollectionAndName(id);
_lite.GetCollection<ReportInfo>(collection)
.Delete(reportName);
}
public IEnumerable<IReportInfo> GetReportsList()
{
return _lite.GetCollection<ReportInfo>(REPORTS)
.FindAll();
}
public Report GetTemplate(string templateId)
{
var template = _lite.GetCollection<Template>(TEMPLATES)
.FindById(templateId);
if (template is null)
return null;
return ReportConverter.FromXML(template.Content);
}
public IEnumerable<TemplateInfo> GetTemplatesList()
{
var templatesList = _lite.GetCollection<Template>(TEMPLATES)
.FindAll()
.Select(template => new TemplateInfo
{
Id = template.Id,
Name = Path.GetFileNameWithoutExtension(template.Id),
});
return templatesList;
}
public Theme GetTheme(string themeId)
{
var bson = _lite.GetCollection(THEMES)
.FindById(themeId);
if (bson is null)
return null;
return _lite.Mapper.Deserialize<Theme>(bson);
}
public IEnumerable<IThemeInfo> GetThemesList()
{
var themes = _lite.GetCollection(THEMES);
var themesList = themes
.FindAll()
.Select(bson =>
{
var theme = _lite.Mapper.Deserialize<Theme>(bson);
var themeId = bson["_id"];
return new ThemeInfo(theme)
{
Id = themeId,
Title = Path.GetFileNameWithoutExtension(themeId),
};
});
return themesList;
}
public Resource GetResource(string id)
{
var bson = _lite.GetCollectionNames()
.Select(c => _lite.GetCollection(c).FindById(id))
.FirstOrDefault(r => r != null);
if (bson == null)
return new Resource();
var type = CustomStoreResourceLocator.GetResourceType(id);
var result = _lite.Mapper.Deserialize(type, bson);
// Special case for themes due to different storage format in the database
var theme = result as Theme;
if (theme != null)
return new Resource(theme.ToStream(), null);
return new Resource(CustomStoreResourceLocator.ToStream(result), null);
}
public SectionReport GetSectionReport(string reportId)
{
var (collection, reportName) = Util.GetCollectionAndName(reportId, REPORTS);
var reportItem = _lite.GetCollection<ReportInfo>(collection)
.FindById(reportName);
if (reportItem is null)
return null;
var report = new SectionReport();
report.LoadLayout(XmlReader.Create(new MemoryStream(reportItem.Content)));
// Define our own resource locator for correct work with report resources (images, themes and so on)
report.ResourceLocator = new CustomStoreResourceLocator(this);
return report;
}
public void SaveSectionReport(string reportId, SectionReport report, bool isTemporary)
{
var reportXml = new MemoryStream();
report.SaveLayout(XmlWriter.Create(reportXml));
var collection = isTemporary ? Util.TEMP_SUFFIX : REPORTS;
_lite
.GetCollection<ReportInfo>(collection)
.Upsert(new ReportInfo
{
Id = reportId,
Name = reportId,
Content = reportXml.ToArray(),
Type = "RPX",
});
}
}
}