-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkLog.cs
130 lines (119 loc) · 5.22 KB
/
WorkLog.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
using System;
using System.Data.Odbc;
using System.Drawing.Imaging;
using System.IO;
using System.Text.RegularExpressions;
namespace KlpToJekyll
{
/// <summary>
/// Loads information from the KitLog MsAccess database so that it can be provided to the renderer.
/// </summary>
public class WorkLog
{
public string shortDescription { get; private set; }
public string comments { get; private set; }
public string image1Caption { get; private set; }
public string image1FileName { get; private set; }
public System.Drawing.Image image1 { get; private set; }
public string image2Caption { get; private set; }
public string image2FileName { get; private set; }
public System.Drawing.Image image2 { get; private set; }
public string image3Caption { get; private set; }
public string image3FileName { get; private set; }
public System.Drawing.Image image3 { get; private set; }
public DateTime dateOfWork { get; private set; }
public string category { get; private set; }
public string customCategory { get; set; }
public decimal hours { get; private set; }
public string manualRefNumber { get; private set; }
private string postFileName;
/// <summary>
/// Loads data from the current row in the kitlog database
/// </summary>
/// <param name="reader"></param>
public void Load(OdbcDataReader reader)
{
shortDescription = reader.GetString(reader.GetOrdinal("short_description"));
comments = reader.GetString(reader.GetOrdinal("comments"));
image1Caption = reader.GetString(reader.GetOrdinal("caption_1"));
image2Caption = reader.GetString(reader.GetOrdinal("caption_2"));
image3Caption = reader.GetString(reader.GetOrdinal("caption_3"));
dateOfWork = reader.GetDate(reader.GetOrdinal("date_of_work"));
image1 = ImageFromColumn("photo_1", reader);
image2 = ImageFromColumn("photo_2", reader);
image3 = ImageFromColumn("photo_3", reader);
category = reader.GetString(reader.GetOrdinal("expense_category_name"));
manualRefNumber = reader.GetString(reader.GetOrdinal("manual_ref_number"));
hours = Decimal.Round(Convert.ToDecimal(reader.GetDouble(reader.GetOrdinal("number_of_hours"))), 2, MidpointRounding.AwayFromZero);
string dateOfWorkString = dateOfWork.ToString("yyyy-MM-dd");
string fileDescription = Regex.Replace(shortDescription.Replace(' ', '-'), "[^a-zA-Z0-9-]", "");
string baseName = dateOfWorkString + "-" + fileDescription;
postFileName = baseName + ".md";
image1FileName = baseName + "-" + 1 + ".jpg";
image2FileName = baseName + "-" + 2 + ".jpg";
image3FileName = baseName + "-" + 3 + ".jpg";
}
/// <summary>
/// Writes the post and images in the current worklog to the Jekyll site
/// </summary>
public void Write( string jekyllDirectory)
{
string postsDirectory = jekyllDirectory + "\\_posts\\";
Directory.CreateDirectory(postsDirectory);
WritePost(postsDirectory);
string imagesDirectory = jekyllDirectory + "\\assets\\images\\";
Directory.CreateDirectory(imagesDirectory);
WriteImage(image1, imagesDirectory + image1FileName);
WriteImage(image2, imagesDirectory + image2FileName);
WriteImage(image3, imagesDirectory + image3FileName);
}
private void WritePost( String directory)
{
PostRenderer renderer = new PostRenderer(this);
string rendered = renderer.TransformText();
File.WriteAllText( directory + postFileName, rendered);
}
/// <summary>
/// Writes an image, if it exists, to the Jekyll image folder
/// </summary>
/// <param name="image"></param>
/// <param name="name"></param>
private void WriteImage(System.Drawing.Image image, string name)
{
if (image != null)
{
using (FileStream fs = File.Create(name))
{
image.Save(fs, ImageFormat.Jpeg);
}
}
}
/// <summary>
/// Loads an image from a column with the given name if one exists
/// </summary>
/// <param name="column"></param>
/// <param name="reader"></param>
/// <returns></returns>
private System.Drawing.Image ImageFromColumn(string column, OdbcDataReader reader)
{
try
{
int ordinal = reader.GetOrdinal(column);
if (!reader.IsDBNull(ordinal))
{
MemoryStream ms = new MemoryStream();
using (Stream dbs = reader.GetStream(ordinal))
{
dbs.CopyTo(ms);
}
return System.Drawing.Image.FromStream(ms);
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
return null;
}
}
}