UtilityCerner.cs 17.2 KB

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,
                     @"&nbsp;", " ",
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            result = System.Text.RegularExpressions.Regex.Replace(result,
                     @"&bull;", " * ",
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            result = System.Text.RegularExpressions.Regex.Replace(result,
                     @"&lsaquo;", "<",
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            result = System.Text.RegularExpressions.Regex.Replace(result,
                     @"&rsaquo;", ">",
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            result = System.Text.RegularExpressions.Regex.Replace(result,
                     @"&trade;", "(tm)",
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            result = System.Text.RegularExpressions.Regex.Replace(result,
                     @"&frasl;", "/",
                     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,
                     @"&copy;", "(c)",
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            result = System.Text.RegularExpressions.Regex.Replace(result,
                     @"&reg;", "(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);
        }
    }
}