Skip to content

Instantly share code, notes, and snippets.

@jnimmo
Last active May 1, 2019 04:23
Show Gist options
  • Save jnimmo/02c7dcbfc870791ff89f to your computer and use it in GitHub Desktop.
Save jnimmo/02c7dcbfc870791ff89f to your computer and use it in GitHub Desktop.
Intrahealth Profile URL Handler, creates a custom URL scheme ihprofile:// to allow you to open patient EHR/encounters from another application or web browser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using System.Windows.Forms;
using Profile;
using IHProfBL;
using Microsoft.Win32;
namespace IHProfile_URL_Handler
{
class Program
{
static ISProfile profile;
static ProfileClient client;
static string urlScheme = "ihprofile";
/**
* Processes strings in the format of:
* ihprofile://encounters/?NHI=
*/
static void Main(string[] args)
{
profile = null;
client = null;
if (args.Length == 0)
{
DialogResult res= MessageBox.Show("USAGE: \r\n - Run with argument /install in elevated command prompt to install\r\n - Run with argument /uninstall in elevated command prompt to remove.\r\nWould you like to install now?","Intrahealth Profile URL Handler",MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
{
InstallURLHandler();
}
Application.Exit();
}
foreach (string s in args)
{
if (s.Contains("/uninstall") || s.Contains("-uninstall"))
{
UninstallURLHandler();
Application.Exit();
}
else if (s.Contains("/install") || s.Contains("-install"))
{
InstallURLHandler();
Application.Exit();
}
Uri url = null;
Uri.TryCreate(s,UriKind.Absolute,out url);
if (url != null && url.Scheme == urlScheme)
{
string area = url.Authority;
NameValueCollection query = HttpUtility.ParseQueryString(url.Query);
if (area == "encounters")
{
if (query["NHI"] != null)
{
OpenProfile();
if (profile != null)
{
ISPatient patient = profile.FindPatientByNHI(query["NHI"]);
if (patient != null)
{
// Open patient encounters
profile.ShowIntoClinicalReview(2, false, 0, true, "", patient.ID, 0, "");
}
else
{
MessageBox.Show("Couldn't find a patient with the NHI/PHN number " + query["NHI"]);
}
}
}
else if (query["filenum"] != null)
{
OpenProfile();
if (profile != null)
{
ISPatient patient = profile.LoadPatientByFilenum(query["filenum"]);
if (patient != null)
{
// Open patient encounters
profile.ShowIntoClinicalReview(2, false, 0, true, "", patient.ID, 0, "");
}
else
{
MessageBox.Show("Couldn't find a patient with the file number " + query["filenum"]);
}
}
}
else if (query["pid"] != null && query["record"] != null)
{
OpenProfile();
if (profile != null)
{
// This is a link from REDCap
string registry_key = "REDCAP_PID_" + query["pid"];
ISPatient patient = profile.GetPatientByRegistryParams(registry_key, query["record"]);
if (patient != null)
{
profile.ShowIntoClinicalReview(2, false, 0, true, "", patient.ID, 0, "");
}
else
{
MessageBox.Show("Couldn't find a patient with the registry key " + registry_key + " and value " + query["record"] + "\r\n\r\nGo to Alter Patient > Registry and assign to a patient.");
}
}
#region
/** Code I couldn't get to work, to add a registry key to the given patient
else
{
/**MessageBox.Show("There is currently no patient associated with that record ID.\r\n\r\nIf you would like to assign a patient, find the patient, otherwise click cancel.\r\n\r\nIMPORTANT: Only do this if you know what you are doing");
try
{
patient = profile.SelectPatient();
if (patient != null)
{
ISMapTransaction trans = profile.StartMapTransaction();
//ISEntityID id = patient.AddEntityId(registry_key, "record", query["record"]);
profile.LoadPatient(patient.ID).AddRegistryKey(registry_key, query["record"]);
//reg.SourceRef = Environment.UserName + " " + new DateTime().ToString();
trans.Commit();
}
}
catch (Exception e)
{
Console.WriteLine("Caught exception " + e.StackTrace);
}
} **/
#endregion
}
else
{
MessageBox.Show("No valid patient search parameters were set. URL must be formatted like one of the following:\r\n\r\n - ihprofile://encounters/review&NHI=AAA0000\r\n - ihprofile://encounters/review&filenum=12345\r\n - ihprofile://encounters/review&pid=123&record=12345");
}
}
else
{
MessageBox.Show("Unsupported command, currently only " + urlScheme + "://encounters/ is supported");
}
}
else
{
MessageBox.Show("Invalid URL scheme " + url.Scheme);
}
}
}
static void InstallURLHandler()
{
try
{
RegistryKey ihprofilekey = Registry.ClassesRoot.CreateSubKey(urlScheme, RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistryKey shellKey = ihprofilekey.CreateSubKey("shell", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistryKey openKey = shellKey.CreateSubKey("open", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistryKey cmdKey = openKey.CreateSubKey("command", RegistryKeyPermissionCheck.ReadWriteSubTree);
ihprofilekey.SetValue("", "URL:" + urlScheme + " Protocol", RegistryValueKind.String);
ihprofilekey.SetValue("URL Protocol", "", RegistryValueKind.String);
cmdKey.SetValue("", "\"" + Application.ExecutablePath + "\" \"%1\"");
}
catch (Exception e)
{
MessageBox.Show("Installation Error:\r\n\r\nCould not create the required registry key HKEY_CLASSES_ROOT\\" + urlScheme + ". Try running again as Administrator");
}
}
static void UninstallURLHandler()
{
try
{
Registry.ClassesRoot.DeleteSubKeyTree(urlScheme);
}
catch (Exception e)
{
MessageBox.Show("Uninstall Error:\r\n\r\nEither the application is not installed or does not have access to the registry. Try running again as administrator.");
}
}
static void OpenProfile()
{
if (client == null || profile == null)
{
try
{
client = new ProfileClient();
profile = client.Login("", "", "");
}
catch (Exception e)
{
Console.WriteLine("Caught exception " + e.StackTrace);
}
}
}
}
}
@meet-mrmaker
Copy link

try: trans.Snapshot();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment