UtilityCerner.cs
17.2 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using ADAM.CernerFHIR.Helper;
using ADAM.CernerFHIR.Model;
using HtmlAgilityPack;
namespace eLearningPlayer
{
public class UtilityCerner
{
public void UploadDocumentToCerner(string content, string imageName)
{
var finalHtml = CreateXHtml(content, imageName);
finalHtml = AddSelfClosingTags(finalHtml);
finalHtml = ConvertHtmlToText(finalHtml);// ConvertToRtfDocument(finalHtml);
var base64EncodeHtmlData = Base64Encode(finalHtml);
////... get vocabulary codes for Adam articles from ADAM Database.
//var MedicalCodesAdamArticle = VlpContext.GetSmartCareDao().GetVocabularyCodes(int.Parse(PidGids[0]), PidGids[1]);
//var MedicalCodesCerner = (List<ResourceModel>)Session["MedicalCodesCerner"];
//var TempDataTableMedicalCodesCerner = new DataTable();
//TempDataTableMedicalCodesCerner.Columns.Add("Vocabulary");
//TempDataTableMedicalCodesCerner.Columns.Add("Code");
//for (int i = 0; i < MedicalCodesCerner.Count; i++)
//{
// DataRow temprow = TempDataTableMedicalCodesCerner.NewRow();
// temprow[0] = MedicalCodesCerner[i].Vocabulary.ToString();
// temprow[1] = MedicalCodesCerner[i].MedicalCode.ToString();
// TempDataTableMedicalCodesCerner.Rows.Add(temprow);
//}
//var VocabCode = new List<MedicalVocabulary>();
//if (MedicalCodesAdamArticle.Rows.Count > 0)
//{
// DataView DataTableView = new DataView(MedicalCodesAdamArticle);
// DataTable DataTableMedicalCodesAdamArticle = DataTableView.ToTable(true, "Vocabulary", "Code");
// var filteredList = from c in DataTableMedicalCodesAdamArticle.AsEnumerable()
// join d in TempDataTableMedicalCodesCerner.AsEnumerable()
// on (string)c["Code"] equals (string)d["Code"]
// select new MedicalVocabulary
// {
// Vocabulary = c.Field<string>("Vocabulary"),
// MedicalCode = c.Field<string>("Code")
// };
// VocabCode = filteredList.ToList();
//}
//if (VocabCode.Count == 0) //... If article level no VocabularyCodes found then send all codes from the search result.
//{
// var filteredList = from c in TempDataTableMedicalCodesCerner.AsEnumerable()
// select new MedicalVocabulary
// {
// Vocabulary = c.Field<string>("Vocabulary"),
// MedicalCode = c.Field<string>("Code")
// };
// VocabCode = filteredList.ToList();
//}
//... Save Data (DocumentReference resource) to cerner's Resource server.
//if (Session["CernerToken"] != null)
//{
// var loginResult = (LoginResult)Session["CernerToken"];
// var cernerInfoJWT = (CernerInfoJWT)Session["cernerInfoJWT"];
// var documentPath = Server.MapPath("~/content/DocumentReference.txt");
// string documentFormat = File.ReadAllText(documentPath);
// Task.Run(() => ADAM.CernerFHIR.Client.DocumentReferenceResource.PostDocumentReferenceAsync(base64EncodeHtmlData, PidGids, VocabCode, loginResult, cernerInfoJWT, documentFormat));
//}
}
private string CreateXHtml(string rawhtml, string title)
{
var meta = "<meta http-equiv=\"x-ua-compatible\" content=\"IE=edge\"/>";
StringWriter s = new StringWriter();
s.WriteLine("{0}", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
s.WriteLine("{0}", "<html lang=\"en\" xmlns:adam=\"urn:DataProcessor\" xmlns:ax=\"http://www.adam.com\" xmlns:exsl=\"http://exslt.org/common\">");
s.WriteLine("{0}", meta);
s.WriteLine("{0}", "<head>");
s.WriteLine("<title>{0}</title>", title);
s.WriteLine("{0}", "</head>");
s.WriteLine("{0}", "<body>");
s.WriteLine("{0}", rawhtml);
s.WriteLine("{0}", "</body>");
s.WriteLine("{0}", "</html>");
return s.ToString();
}
private string AddSelfClosingTags(string html)
{
string res = string.Empty;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
doc.DocumentNode.Descendants("script").ToList().ForEach(x => { x.Remove(); });
//Change all img instance src to Base64.
doc.DocumentNode.Descendants("img")
.Where(e =>
{
string src = e.GetAttributeValue("src", null) ?? "";
return !string.IsNullOrEmpty(src);
})
.ToList()
.ForEach(x =>
{
if (!(x.OuterHtml.Contains("ftradamlogo")))
x.SetAttributeValue("style", "max-width:800px;");
string currentSrcValue = x.GetAttributeValue("src", null);
//currentSrcValue = currentSrcValue.Replace("../..", baseUrl);
var imageBase64Data = ConvertImageURLToBase64(currentSrcValue);
x.SetAttributeValue("src", "data:image/jpeg;base64," + imageBase64Data);
});
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
doc.Save(xw);
return res = sw.ToString();
}
private static string ConvertHtmlToText(string source)
{
string result;
// Remove HTML Development formatting
// Replace line breaks with space
// because browsers inserts space
result = source.Replace("\r", " ");
// Replace line breaks with space
// because browsers inserts space
result = result.Replace("\n", " ");
// Remove step-formatting
result = result.Replace("\t", string.Empty);
// Remove repeating speces becuase browsers ignore them
result = System.Text.RegularExpressions.Regex.Replace(result,
@"( )+", " ");
// Remove the header (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*head([^>])*>", "<head>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"(<( )*(/)( )*head( )*>)", "</head>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
"(<head>).*(</head>)", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// remove all scripts (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*script([^>])*>", "<script>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"(<( )*(/)( )*script( )*>)", "</script>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//result = System.Text.RegularExpressions.Regex.Replace(result,
// @"(<script>)([^(<script>\.</script>)])*(</script>)",
// string.Empty,
// System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"(<script>).*(</script>)", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// remove all styles (prepare first by clearing attributes)
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*style([^>])*>", "<style>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"(<( )*(/)( )*style( )*>)", "</style>",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
"(<style>).*(</style>)", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// insert tabs in spaces of <td> tags
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*td([^>])*>", "\t",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// insert line breaks in places of <BR> and <LI> tags
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*br( )*>", "\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*li( )*>", "\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// insert line paragraphs (double line breaks) in place
// if <P>, <DIV> and <TR> tags
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*div([^>])*>", "\r\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*tr([^>])*>", "\r\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<( )*p([^>])*>", "\r\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// Remove remaining tags like <a>, links, images,
// comments etc - anything thats enclosed inside < >
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<[^>]*>", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// replace special characters:
result = System.Text.RegularExpressions.Regex.Replace(result,
@" ", " ",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"•", " * ",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"‹", "<",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"›", ">",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"™", "(tm)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"⁄", "/",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"<", "<",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@">", ">",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"©", "(c)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
@"®", "(r)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// Remove all others. More can be added, see
// http://hotwired.lycos.com/webmonkey/reference/special_characters/
result = System.Text.RegularExpressions.Regex.Replace(result,
@"&(.{2,6});", string.Empty,
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// make line breaking consistent
result = result.Replace("\n", "\r");
// Remove extra line breaks and tabs:
// replace over 2 breaks with 2 and over 4 tabs with 4.
// Prepare first to remove any whitespaces inbetween
// the escaped characters and remove redundant tabs inbetween linebreaks
result = System.Text.RegularExpressions.Regex.Replace(result,
"(\r)( )+(\r)", "\r\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
"(\t)( )+(\t)", "\t\t",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
"(\t)( )+(\r)", "\t\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result,
"(\r)( )+(\t)", "\r\t",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// Remove redundant tabs
result = System.Text.RegularExpressions.Regex.Replace(result,
"(\r)(\t)+(\r)", "\r\r",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// Remove multible tabs followind a linebreak with just one tab
result = System.Text.RegularExpressions.Regex.Replace(result,
"(\r)(\t)+", "\r\t",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// Initial replacement target string for linebreaks
string breaks = "\r\r\r";
// Initial replacement target string for tabs
string tabs = "\t\t\t\t\t";
for (int index = 0; index < result.Length; index++)
{
result = result.Replace(breaks, "\r\r");
result = result.Replace(tabs, "\t\t\t\t");
breaks = breaks + "\r";
tabs = tabs + "\t";
}
// Thats it.
return result;
}
private string Base64Encode(string plainText)
{
byte[] bytTemp = System.Text.Encoding.UTF8.GetBytes(plainText.ToString());
return Convert.ToBase64String(bytTemp);
}
private string ConvertImageURLToBase64(string url)
{
StringBuilder _sb = new StringBuilder();
Byte[] _byte = GetImage(url);
if (_byte != null)
_sb.Append(Convert.ToBase64String(_byte, 0, _byte.Length));
return _sb.ToString();
}
private byte[] GetImage(string imageUrl)
{
Stream stream = null;
byte[] buf;
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(imageUrl);
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| (SecurityProtocolType)768
| (SecurityProtocolType)3072
| SecurityProtocolType.Ssl3;
WebResponse webResponse = req.GetResponse();
stream = webResponse.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
using (BinaryReader br = new BinaryReader(stream))
{
int len = (int)(webResponse.ContentLength);
buf = br.ReadBytes(len);
br.Close();
}
stream.Close();
webResponse.Close();
}
catch (Exception exp)
{
buf = null;
}
return (buf);
}
}
}