-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
97 lines (81 loc) · 3.5 KB
/
Program.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
using Microsoft.Data.Sqlite;
using System;
using System.IO;
using System.IO.Compression;
using System.Reflection;
namespace MolioDocEFCore
{
class Program
{
static void Main(string[] args)
{
var outFilePath = Path.Combine(AppContext.BaseDirectory, "molio.db.gz");
// It's unfortunate the database must be placed physically on disc and can't just reside in
// memory. Using "Data Source=:memory:" as connection string makes no difference - there's
// no way to extract the memory stream.
var dbFilePath = Path.GetTempFileName();
try
{
using (var db = BlankDatabase(dbFilePath))
using (var doc = new MolioDoc(db))
WriteDoc(doc);
GZipDoc(dbFilePath, outFilePath);
}
finally
{
File.Delete(dbFilePath);
}
Console.WriteLine("Done");
Console.ReadKey();
}
static void WriteDoc(MolioDoc ctx)
{
var bygningsdelsbeskrivelse = new Bygningsdelsbeskrivelse
{
Name = "Test",
BygningsdelsbeskrivelseGuid = Guid.NewGuid(),
BasisbeskrivelseVersionGuid = Guid.NewGuid()
};
ctx.Bygningsdelsbeskrivelser.Add(bygningsdelsbeskrivelse);
var omfang =
new BygningsdelsbeskrivelseSection(1, "OMFANG");
var almeneSpecifikationer =
new BygningsdelsbeskrivelseSection(2, "ALMENE SPECIFIKATIONER");
var generelt =
new BygningsdelsbeskrivelseSection(almeneSpecifikationer, 1, "Generelt", "Noget tekst")
{ MolioSectionGuid = Guid.NewGuid() };
var thirdLevelSection =
new BygningsdelsbeskrivelseSection(generelt, 5, "Tredje niveau", "Lorem ipsum");
var referenceliste = Attachment.Json("referenceliste.json", "{ \"test\": 1 }");
thirdLevelSection.Attach(referenceliste);
using(var samplePdf = GetSamplePdf())
thirdLevelSection.Attach(Attachment.Pdf("basisbeskrivelse.pdf", samplePdf));
bygningsdelsbeskrivelse.Sections.AddRange(new[] {
omfang, almeneSpecifikationer, generelt, thirdLevelSection
});
ctx.SaveChanges();
}
static void GZipDoc(string dbFilePath, string outFilePath)
{
using (var dbFileHandle = File.OpenRead(dbFilePath))
using (var outFileHandle = File.Open(outFilePath, FileMode.Create, FileAccess.Write))
using (var gzip = new GZipStream(outFileHandle, CompressionMode.Compress))
dbFileHandle.CopyTo(gzip);
}
static SqliteConnection BlankDatabase(string dbFilePath)
{
var sqlite = new SqliteConnection("Data Source=" + dbFilePath);
sqlite.Open();
using (var template = new SqliteCommand(GetSqlTemplate(), sqlite))
template.ExecuteNonQuery();
return sqlite;
}
static string GetSqlTemplate()
{
using (var template = Assembly.GetExecutingAssembly().GetManifestResourceStream("MolioDocEFCore.Template.sql"))
using (var reader = new StreamReader(template))
return reader.ReadToEnd();
}
static Stream GetSamplePdf() => Assembly.GetExecutingAssembly().GetManifestResourceStream("MolioDocEFCore.Sample.pdf");
}
}