source: Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocumentManager.cs@ 1091

Last change on this file since 1091 was 1091, checked in by Sam Habiel, 13 years ago
File size: 44.7 KB
Line 
1using System;
2using System.Windows.Forms;
3using System.Collections;
4using System.Data;
5using System.Diagnostics;
6using System.Threading;
7using IndianHealthService.BMXNet;
8using Mono.Options;
9using System.Runtime.InteropServices;
10
11namespace IndianHealthService.ClinicalScheduling
12{
13 /// <summary>
14 /// Main Worker. Handles sub-forms.
15 /// </summary>
16 public class CGDocumentManager //: System.Windows.Forms.Form
17 {
18 #region Member Variables
19
20 private static CGDocumentManager _current;
21 private Hashtable _views = new Hashtable(); //Returns the list of currently opened documents
22 private Hashtable m_AVViews = new Hashtable(); // List of currently opened CGAVViews
23 private string m_sWindowText = "Clinical Scheduling"; //Default Window Text
24 private bool m_bSchedManager = false; // Do you have the XUPROGMODE or BSDXZMGR?
25 private bool m_bExitOK = true; // Okay to exit program? Used to control Re-logins. Default true.
26 public string m_sHandle = "0"; // Not Used
27
28 //Connection variables (tied to command line parameters /a /v /s /p /e)
29 private string m_AccessCode="";
30 private string m_VerifyCode="";
31 private string m_Server="";
32 private int m_Port=0;
33 private string m_Encoding=""; //Encoding is "" by default;
34
35 //Data Access Layer
36 private DAL _dal = null;
37
38 //M Connection member variables
39 private DataSet m_dsGlobal = null; // Holds all user data
40 private BMXNetConnectInfo m_ConnectInfo = null; // Connection to VISTA object
41 private BMXNetConnectInfo.BMXNetEventDelegate CDocMgrEventDelegate; // Delegate to respond to messages from VISTA. Responds to event: BMXNetConnectInfo.BMXNetEvent
42
43 //Custom Printing
44 private CustomPrinting m_PrintingObject = null;
45 #endregion
46
47 #region Properties
48
49 /// <summary>
50 /// Returns the document manager's BMXNetConnectInfo member
51 /// </summary>
52 public BMXNetConnectInfo ConnectInfo
53 {
54 get
55 {
56 return m_ConnectInfo;
57 }
58 }
59
60 /// <summary>
61 /// True if the current user holds the BSDXZMGR or XUPROGMODE keys in RPMS
62 /// </summary>
63 public bool ScheduleManager
64 {
65 get
66 {
67 return m_bSchedManager;
68 }
69 }
70
71 /// <summary>
72 /// Holds the user and division
73 /// </summary>
74 public string WindowText
75 {
76 get
77 {
78 return m_sWindowText;
79 }
80 }
81
82 /// <summary>
83 /// This dataset contains tables used by the entire application
84 /// </summary>
85 public DataSet GlobalDataSet
86 {
87 get
88 {
89 return m_dsGlobal;
90 }
91 set
92 {
93 m_dsGlobal = value;
94 }
95 }
96
97 /// <summary>
98 /// Returns the single CGDocumentManager object
99 /// </summary>
100 public static CGDocumentManager Current
101 {
102 get
103 {
104 return _current;
105 }
106 }
107
108
109 /// <summary>
110 /// Returns the list of currently opened documents
111 /// </summary>
112 public Hashtable Views
113 {
114 get
115 {
116 return _views;
117 }
118 }
119
120 /// <summary>
121 /// Returns the list of currently opened CGAVViews
122 /// </summary>
123 public Hashtable AvailabilityViews
124 {
125 get
126 {
127 return this.m_AVViews;
128 }
129 }
130
131 public DAL DAL
132 {
133 get { return this._dal; }
134 }
135
136 public CustomPrinting PrintingObject
137 {
138 get
139 {
140 return this.m_PrintingObject;
141 }
142 }
143 #endregion
144
145 /// <summary>
146 /// Constructor. Does absolutely nothing at this point.
147 /// </summary>
148 public CGDocumentManager()
149 {
150 }
151
152
153#if DEBUG
154 //To write to the console
155 [DllImport("kernel32.dll")]
156 static extern bool AttachConsole(int dwProcessId);
157 private const int ATTACH_PARENT_PROCESS = -1;
158#endif
159 /// <summary>
160 /// Main Entry Point
161 /// </summary>
162 /// <param name="args">We accept the following Arguments:
163 /// /s or -s = Server ip address or name
164 /// /p or -p = port number (must be numeric)
165 /// /a or -a = Access Code
166 /// /v or -v = Verify Code
167 /// /e or -e = Encoding (name of encoding as known to windows, such as windows-1256)
168 /// </param>
169 /// <remarks>
170 /// Encoding decision is complex. This is the order of priority:
171 /// - If the M DB runs in UTF-8, that's what we are going to use.
172 /// - If that's not so, /e sets the default encoding. If /e is a non-existent encoding, move forward.
173 /// - If /e is not supplied or is not recognized, the default encoding is the Windows default Encoding for the user.
174 /// </remarks>
175 [STAThread()]
176 static void Main(string[] args)
177 {
178#if DEBUG
179 // Print console messages to console if launched from console
180 // Note: Imported From kernel32.dll
181 AttachConsole(ATTACH_PARENT_PROCESS);
182#endif
183
184#if TRACE
185 DateTime startLoadTime = DateTime.Now;
186#endif
187
188 //Store a class instance of manager. Actual constructor does nothing.
189 _current = new CGDocumentManager();
190
191 //Get command line options; store in private class wide variables
192 var opset = new OptionSet() {
193 { "s=", s => _current.m_Server = s },
194 { "p=", p => _current.m_Port = int.Parse(p) },
195 { "a=", a => _current.m_AccessCode = a },
196 { "v=", v => _current.m_VerifyCode = v },
197 { "e=", e => _current.m_Encoding = e}
198 };
199
200 opset.Parse(args);
201
202 //Init app
203 bool isEverythingOkay = _current.InitializeApp();
204
205 //if an error occurred, break out.
206 if (!isEverythingOkay) return;
207
208 //Create the first empty document
209 //A document holds the resources, appointments, and availabilites
210 //SAM: Good place for break point
211 CGDocument doc = new CGDocument();
212 doc.DocManager = _current;
213
214 //Create new View
215 //A view is a specific arrangement of appointments and availabilites that constitute a document
216 CGView view = new CGView();
217 view.InitializeDocView(doc, _current, doc.StartDate, _current.WindowText);
218
219 //Handle BMX Event
220 Application.DoEvents();
221
222 //Application wide error handler for unhandled errors
223 Application.ThreadException += new ThreadExceptionEventHandler(App_ThreadException);
224
225#if TRACE
226 DateTime EndLoadTime = DateTime.Now;
227 TimeSpan LoadTime = EndLoadTime - startLoadTime;
228 Debug.Write("Load Time for GUI is " + LoadTime.Seconds + " s & " + LoadTime.Milliseconds + " ms\n");
229#endif
230
231 view.Show();
232 view.Activate();
233
234 Application.Run();
235 }
236
237 /// <summary>
238 /// Exception handler for application errors. TODO: Test
239 /// </summary>
240 /// <param name="sender"></param>
241 /// <param name="e"></param>
242 static void App_ThreadException(object sender, ThreadExceptionEventArgs e)
243 {
244 if (e.Exception is System.Net.Sockets.SocketException)
245 {
246 MessageBox.Show("Looks like we lost our connection with the server\nClick OK to terminate the application.");
247 Application.Exit();
248 }
249
250 string msg = "A problem has occured in this applicaton. \r\n\r\n" +
251 "\t" + e.Exception.Message + "\r\n\r\n" +
252 "Would you like to continue the application?";
253
254 DialogResult res = MessageBox.Show(msg, "Unexpected Error", MessageBoxButtons.YesNo);
255
256 if (res == DialogResult.Yes) return;
257 else Application.Exit();
258 }
259
260
261 #region BMXNet Event Handler
262 private void CDocMgrEventHandler(Object obj, BMXNet.BMXNetEventArgs e)
263 {
264 if (e.BMXEvent == "BSDX CALL WORKSTATIONS")
265 {
266 string sParam = "";
267 string sDelim="~";
268 sParam += this.m_ConnectInfo.UserName + sDelim;
269 sParam += this.m_sHandle + sDelim;
270 sParam += Application.ProductVersion + sDelim;
271 sParam += this._views.Count.ToString();
272 _current.m_ConnectInfo.RaiseEvent("BSDX WORKSTATION REPORT", sParam, true);
273 }
274 if (e.BMXEvent == "BSDX ADMIN MESSAGE")
275 {
276 string sMsg = e.BMXParam;
277 ShowAdminMsgDelegate samd = new ShowAdminMsgDelegate(ShowAdminMsg);
278 //this.Invoke(samd, new object [] {sMsg});
279 samd.Invoke(sMsg);
280 }
281 if (e.BMXEvent == "BSDX ADMIN SHUTDOWN")
282 {
283 string sMsg = e.BMXParam;
284 CloseAllDelegate cad = new CloseAllDelegate(CloseAll);
285 //this.Invoke(cad, new object [] {sMsg});
286 cad.Invoke(sMsg);
287 }
288 }
289
290 delegate void ShowAdminMsgDelegate(string sMsg);
291
292 private void ShowAdminMsg(string sMsg)
293 {
294 MessageBox.Show(sMsg, "Message from Scheduling Administrator", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
295 }
296
297 #endregion BMXNet Event Handler
298
299
300 #region Methods & Events
301
302 /// <summary>
303 /// See InitializeApp(bool) below
304 /// </summary>
305 private bool InitializeApp()
306 {
307 return InitializeApp(false);
308 }
309
310 /// <summary>
311 /// Does a million things:
312 /// 1. Starts Connection and displays log-in dialogs
313 /// 2. Starts Splash screen
314 /// 3. Loads data tables
315 /// </summary>
316 /// <param name="bReLogin">Is the User logging in again from a currently running instance?
317 /// If so, display a dialog to collect access and verify codes.</param>
318 private bool InitializeApp(bool bReLogin)
319 {
320 //Set M connection info
321 m_ConnectInfo = new BMXNetConnectInfo(m_Encoding); // Encoding is "" unless passed in command line
322 _dal = new DAL(m_ConnectInfo); // Data access layer
323 //m_ConnectInfo.bmxNetLib.StartLog(); //This line turns on logging of messages
324
325 //Create a delegate to process events raised by BMX.
326 CDocMgrEventDelegate = new BMXNetConnectInfo.BMXNetEventDelegate(CDocMgrEventHandler);
327 //Tie delegate to Events generated by BMX.
328 m_ConnectInfo.BMXNetEvent += CDocMgrEventDelegate;
329 //Disable polling (But does this really work???? I don't see how it gets disabled)
330 m_ConnectInfo.EventPollingEnabled = false;
331
332 //Show a splash screen while initializing; define delegates to remote thread
333 DSplash m_ds = new DSplash();
334 DSplash.dSetStatus setStatusDelegate = new DSplash.dSetStatus(m_ds.SetStatus);
335 DSplash.dAny closeSplashDelegate = new DSplash.dAny(m_ds.RemoteClose);
336 DSplash.dProgressBarSet setMaxProgressDelegate = new DSplash.dProgressBarSet(m_ds.RemoteProgressBarMaxSet);
337 DSplash.dProgressBarSet setProgressDelegate = new DSplash.dProgressBarSet(m_ds.RemoteProgressBarValueSet);
338
339 //Start new thread for the Splash screen.
340 Thread threadSplash = new Thread(new ParameterizedThreadStart(frm => ((DSplash)frm).ShowDialog()));
341 threadSplash.IsBackground = true; //expendable thread -- exit even if still running.
342 threadSplash.Name = "Splash Thread";
343 threadSplash.Start(m_ds); // pass form as parameter.
344
345 //There are 19 steps to load the application. That's max for the progress bar.
346 setMaxProgressDelegate(19);
347
348 // smh--not used: System.Configuration.ConfigurationManager.GetSection("appSettings");
349
350 setStatusDelegate("Connecting to VISTA");
351
352 //Try to connect using supplied values for Server and Port
353 //Why am I doing this? The library BMX net uses prompts for access and verify code
354 //whether you can connect or not. Not good. So I test first whether
355 //we can connect at all by doing a simple connection and disconnect.
356 //TODO: Make this more robust by sending a TCPConnect message and seeing if you get a response
357 if (m_Server != "" && m_Port != 0)
358 {
359 System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient();
360 try
361 {
362 tcpClient.Connect(m_Server, m_Port); // open it
363 tcpClient.Close(); // then close it
364 }
365 catch (System.Net.Sockets.SocketException)
366 {
367 MessageBox.Show("Cannot connect to VistA. Network Error");
368 return false;
369 }
370 }
371
372
373 bool bRetry = true;
374
375 // Do block is Log-in logic
376 do
377 {
378 // login crap
379 try
380 {
381 // Not my code
382 if (bReLogin == true)
383 {
384 //Prompt for Access and Verify codes
385 _current.m_ConnectInfo.LoadConnectInfo("", "");
386 }
387 // My code -- buts looks so ugly!
388 // Checks the passed parameters stored in the class variables
389 else
390 {
391 if (m_Server != String.Empty && m_Port != 0 && m_AccessCode != String.Empty
392 && m_VerifyCode != String.Empty)
393 {
394 m_ConnectInfo.LoadConnectInfo(m_Server, m_Port, m_AccessCode, m_VerifyCode);
395 }
396 else if (m_Server != String.Empty && m_Port != 0)
397 m_ConnectInfo.LoadConnectInfo(m_Server, m_Port, "", "");
398 else
399 m_ConnectInfo.LoadConnectInfo();
400 }
401 bRetry = false;
402 }
403 catch (System.Net.Sockets.SocketException)
404 {
405 MessageBox.Show("Cannot connect to VistA. Network Error");
406 }
407 catch (BMXNetException ex)
408 {
409 if (MessageBox.Show("Unable to connect to VistA. " + ex.Message, "Clinical Scheduling", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
410 {
411 bRetry = true;
412 _current.m_ConnectInfo.ChangeServerInfo();
413 }
414 else
415 {
416 closeSplashDelegate();
417 bRetry = false;
418 return false; //tell main that it's a no go.
419 }
420 }
421 }while (bRetry == true);
422
423 //Printing
424
425 System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Application.StartupPath + @"\Printing\");
426 System.IO.FileInfo[] rgFiles = di.GetFiles("*.dll");
427 string DllLocation = string.Empty;
428 foreach (System.IO.FileInfo fi in rgFiles)
429 {
430 DllLocation = fi.FullName;
431 }
432
433 PrintingCreator Creator = null;
434 if (DllLocation == string.Empty)
435 {
436 this.m_PrintingObject = new CustomPrinting();
437 }
438 else
439 {
440 System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(DllLocation);
441 foreach (Type type in assembly.GetTypes())
442 {
443 if (type.IsClass == true & type.BaseType == typeof(PrintingCreator))
444 {
445 Creator = (PrintingCreator)Activator.CreateInstance(type);
446 break;
447 }
448 }
449 this.m_PrintingObject = Creator.PrintFactory();
450 }
451
452 //Create global dataset
453 _current.m_dsGlobal = new DataSet("GlobalDataSet");
454
455 //Version info
456 // Table #1
457 setProgressDelegate(1);
458 setStatusDelegate("Getting Version Info from Server...");
459
460 DataTable ver = _dal.GetVersion("BSDX");
461 ver.TableName = "VersionInfo";
462 m_dsGlobal.Tables.Add(ver);
463
464 //How to extract the version numbers:
465 DataTable dtVersion = m_dsGlobal.Tables["VersionInfo"];
466 Debug.Assert(dtVersion.Rows.Count == 1);
467 DataRow rVersion = dtVersion.Rows[0];
468 string sMajor = rVersion["MAJOR_VERSION"].ToString();
469 string sMinor = rVersion["MINOR_VERSION"].ToString();
470 string sBuild = rVersion["BUILD"].ToString();
471 decimal fBuild = Convert.ToDecimal(sBuild);
472
473 //Make sure that the server is running the same version the client is.
474 Version x = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
475
476 //if version numbers mismatch, don't continue.
477 //TODO: For future: Include in v. 1.5
478 /*
479 if (!(x.Major.ToString() == sMajor && x.Minor.ToString() + x.Build.ToString() == sMinor))
480 {
481 MessageBox.Show(
482 "Server runs version " + sMajor + "." + sMinor + "\r\n" +
483 "You are running " + x.ToString() + "\r\n\r\n" +
484 "Major, Minor and Build versions must match",
485 "Version Mismatch");
486 m_ds.Close();
487 return;
488 }
489 */
490
491
492 //Change encoding
493 // Call #2
494 setProgressDelegate(2);
495 setStatusDelegate("Setting encoding...");
496
497 if (m_Encoding == String.Empty)
498 {
499 string utf8_server_support = m_ConnectInfo.bmxNetLib.TransmitRPC("BMX UTF-8", "");
500 if (utf8_server_support == "1")
501 m_ConnectInfo.bmxNetLib.Encoder = System.Text.UTF8Encoding.UTF8;
502 }
503
504 //Set application context
505 // Call #3
506 setProgressDelegate(3);
507 setStatusDelegate("Setting Application Context to BSDXRPC...");
508 m_ConnectInfo.AppContext = "BSDXRPC";
509
510 //Load global recordsets
511 string statusConst = "Loading VistA data tables...";
512 setStatusDelegate(statusConst);
513
514 string sCommandText;
515
516 //Schedule User Info
517 // Table #4
518 setProgressDelegate(4);
519 setStatusDelegate(statusConst + " Schedule User");
520 DataTable dtUser = _dal.GetUserInfo(m_ConnectInfo.DUZ);
521 dtUser.TableName = "SchedulingUser";
522 m_dsGlobal.Tables.Add(dtUser);
523 Debug.Assert(dtUser.Rows.Count == 1);
524
525 // Only one row and one column named "MANAGER". Set local var m_bSchedManager to true if Manager.
526 DataRow rUser = dtUser.Rows[0];
527 Object oUser = rUser["MANAGER"];
528 string sUser = oUser.ToString();
529 m_bSchedManager = (sUser == "YES") ? true : false;
530
531 //Get Access Types
532 // Table #5
533 setProgressDelegate(5);
534 setStatusDelegate(statusConst + " Access Types");
535 DataTable dtAccessTypes = _dal.GetAccessTypes();
536 dtAccessTypes.TableName = "AccessTypes";
537 m_dsGlobal.Tables.Add(dtAccessTypes);
538
539 //Get Access Groups
540 // Table #6
541 setProgressDelegate(6);
542 setStatusDelegate(statusConst + " Access Groups");
543 LoadAccessGroupsTable();
544
545 //Build Primary Key for AccessGroup table
546 DataTable dtGroups = m_dsGlobal.Tables["AccessGroup"];
547 DataColumn dcKey = dtGroups.Columns["ACCESS_GROUP"];
548 DataColumn[] dcKeys = new DataColumn[1];
549 dcKeys[0] = dcKey;
550 dtGroups.PrimaryKey = dcKeys;
551
552 //Get Access Group Types (Combines Access Types and Groups)
553 //Optimization Note: Can eliminate Access type and Access Group Table
554 // But they are heavily referenced throughout the code.
555 // Table #7
556 setProgressDelegate(7);
557 setStatusDelegate(statusConst + " Access Group Types");
558 LoadAccessGroupTypesTable();
559
560 //Build Primary Key for AccessGroupType table
561 DataTable dtAGTypes = m_dsGlobal.Tables["AccessGroupType"];
562 DataColumn dcGTKey = dtAGTypes.Columns["ACCESS_GROUP_TYPEID"];
563 DataColumn[] dcGTKeys = new DataColumn[1];
564 dcGTKeys[0] = dcGTKey;
565 dtAGTypes.PrimaryKey = dcGTKeys;
566
567 //Build Data Relationship between AccessGroupType and AccessTypes tables
568 DataRelation dr = new DataRelation("AccessGroupType", //Relation Name
569 m_dsGlobal.Tables["AccessGroup"].Columns["BMXIEN"], //Parent
570 m_dsGlobal.Tables["AccessGroupType"].Columns["ACCESS_GROUP_ID"]); //Child
571 m_dsGlobal.Relations.Add(dr);
572
573 //ResourceGroup Table (Resource Groups by User)
574 // Table #8
575 // What shows up on the tree. The groups the user has access to.
576 setProgressDelegate(8);
577 setStatusDelegate(statusConst + " Resource Groups By User");
578 LoadResourceGroupTable();
579
580 //Resources by user
581 // Table #9
582 // Individual Resources
583 setProgressDelegate(9);
584 setStatusDelegate(statusConst + " Resources By User");
585 LoadBSDXResourcesTable();
586
587 //Build Primary Key for Resources table
588 DataColumn[] dc = new DataColumn[1];
589 dc[0] = m_dsGlobal.Tables["Resources"].Columns["RESOURCEID"];
590 m_dsGlobal.Tables["Resources"].PrimaryKey = dc;
591
592 //GroupResources table
593 // Table #10
594 // Resource Groups and Indivdual Resources together
595 setProgressDelegate(10);
596 setStatusDelegate(statusConst + " Group Resources");
597 LoadGroupResourcesTable();
598
599 //Build Primary Key for ResourceGroup table
600 dc = new DataColumn[1];
601 dc[0] = m_dsGlobal.Tables["ResourceGroup"].Columns["RESOURCE_GROUP"];
602 m_dsGlobal.Tables["ResourceGroup"].PrimaryKey = dc;
603
604 //Build Data Relationships between ResourceGroup and GroupResources tables
605 dr = new DataRelation("GroupResource", //Relation Name
606 m_dsGlobal.Tables["ResourceGroup"].Columns["RESOURCE_GROUP"], //Parent
607 m_dsGlobal.Tables["GroupResources"].Columns["RESOURCE_GROUP"]); //Child
608 CGSchedLib.OutputArray(m_dsGlobal.Tables["GroupResources"], "GroupResources");
609 m_dsGlobal.Relations.Add(dr);
610
611 //HospitalLocation table
612 //Table #11
613 setProgressDelegate(11);
614 setStatusDelegate(statusConst + " Clinics");
615 //cmd.CommandText = "SELECT BMXIEN 'HOSPITAL_LOCATION_ID', NAME 'HOSPITAL_LOCATION', DEFAULT_PROVIDER, STOP_CODE_NUMBER, INACTIVATE_DATE, REACTIVATE_DATE FROM HOSPITAL_LOCATION";
616 sCommandText = "BSDX HOSPITAL LOCATION";
617 ConnectInfo.RPMSDataTable(sCommandText, "HospitalLocation", m_dsGlobal);
618 Debug.Write("LoadGlobalRecordsets -- HospitalLocation loaded\n");
619
620 //Build Primary Key for HospitalLocation table
621 dc = new DataColumn[1];
622 DataTable dtTemp = m_dsGlobal.Tables["HospitalLocation"];
623 dc[0] = dtTemp.Columns["HOSPITAL_LOCATION_ID"];
624 m_dsGlobal.Tables["HospitalLocation"].PrimaryKey = dc;
625
626 //Build Data Relationships between Resources and HospitalLocation tables
627 dr = new DataRelation("HospitalLocationResource", //Relation Name
628 m_dsGlobal.Tables["HospitalLocation"].Columns["HOSPITAL_LOCATION_ID"], //Parent
629 m_dsGlobal.Tables["Resources"].Columns["HOSPITAL_LOCATION_ID"], false); //Child
630 m_dsGlobal.Relations.Add(dr);
631
632 //Build ScheduleUser table
633 //Table #12
634 setProgressDelegate(12);
635 setStatusDelegate(statusConst + " Schedule User");
636 this.LoadScheduleUserTable();
637
638 //Build Primary Key for ScheduleUser table
639 dc = new DataColumn[1];
640 dtTemp = m_dsGlobal.Tables["ScheduleUser"];
641 dc[0] = dtTemp.Columns["USERID"];
642 m_dsGlobal.Tables["ScheduleUser"].PrimaryKey = dc;
643
644 //Build ResourceUser table
645 //Table #13
646 //Acess to Resources by [this] User
647 setProgressDelegate(13);
648 setStatusDelegate(statusConst + " Resource User");
649 this.LoadResourceUserTable();
650
651 //Build Primary Key for ResourceUser table
652 dc = new DataColumn[1];
653 dtTemp = m_dsGlobal.Tables["ResourceUser"];
654 dc[0] = dtTemp.Columns["RESOURCEUSER_ID"];
655 m_dsGlobal.Tables["ResourceUser"].PrimaryKey = dc;
656
657 //Create relation between BSDX Resource and BSDX Resource User tables
658 dr = new DataRelation("ResourceUser", //Relation Name
659 m_dsGlobal.Tables["Resources"].Columns["RESOURCEID"], //Parent
660 m_dsGlobal.Tables["ResourceUser"].Columns["RESOURCEID"]); //Child
661 m_dsGlobal.Relations.Add(dr);
662
663 //Build active provider table
664 //Table #14
665 //TODO: Lazy load the provider table; no need to load in advance.
666 setProgressDelegate(14);
667 setStatusDelegate(statusConst + " Providers");
668 sCommandText = "SELECT BMXIEN, NAME FROM NEW_PERSON WHERE INACTIVE_DATE = '' AND BMXIEN > 1";
669 ConnectInfo.RPMSDataTable(sCommandText, "Provider", m_dsGlobal);
670 Debug.Write("LoadGlobalRecordsets -- Provider loaded\n");
671
672 //Build the HOLIDAY table
673 //Table #15
674 setProgressDelegate(15);
675 setStatusDelegate(statusConst + " Holiday");
676 sCommandText = "SELECT NAME, DATE FROM HOLIDAY WHERE DATE > '" + DateTime.Today.ToShortDateString() + "'";
677 ConnectInfo.RPMSDataTable(sCommandText, "HOLIDAY", m_dsGlobal);
678 Debug.Write("LoadingGlobalRecordsets -- Holidays loaded\n");
679
680
681 //Save the xml schema
682 //m_dsGlobal.WriteXmlSchema(@"..\..\csSchema20060526.xsd");
683 //----------------------------------------------
684
685 setStatusDelegate("Setting Receive Timeout");
686 _current.m_ConnectInfo.ReceiveTimeout = 30000; //30-second timeout
687
688#if DEBUG
689 _current.m_ConnectInfo.ReceiveTimeout = 600000; //longer timeout for debugging
690#endif
691 // Event Subsriptions
692 setStatusDelegate("Subscribing to Server Events");
693 //Table #16
694 setProgressDelegate(16);
695 _current.m_ConnectInfo.SubscribeEvent("BSDX SCHEDULE");
696 //Table #17
697 setProgressDelegate(17);
698 _current.m_ConnectInfo.SubscribeEvent("BSDX CALL WORKSTATIONS");
699 //Table #18
700 setProgressDelegate(18);
701 _current.m_ConnectInfo.SubscribeEvent("BSDX ADMIN MESSAGE");
702 //Table #19
703 setProgressDelegate(19);
704 _current.m_ConnectInfo.SubscribeEvent("BSDX ADMIN SHUTDOWN");
705
706 _current.m_ConnectInfo.EventPollingInterval = 5000; //in milliseconds
707 _current.m_ConnectInfo.EventPollingEnabled = true;
708 _current.m_ConnectInfo.AutoFire = 12; //AutoFire every 12*5 seconds
709
710 //Close Splash Screen
711 closeSplashDelegate();
712
713 return true;
714
715 }
716
717
718
719 public void LoadAccessGroupsTable()
720 {
721 string sCommandText = "SELECT * FROM BSDX_ACCESS_GROUP";
722 ConnectInfo.RPMSDataTable(sCommandText, "AccessGroup", m_dsGlobal);
723 Debug.Write("LoadGlobalRecordsets -- AccessGroups loaded\n");
724 }
725
726 public void LoadAccessGroupTypesTable()
727 {
728 string sCommandText = "BSDX GET ACCESS GROUP TYPES";
729 ConnectInfo.RPMSDataTable(sCommandText, "AccessGroupType", m_dsGlobal);
730 Debug.Write("LoadGlobalRecordsets -- AccessGroupTypes loaded\n");
731 }
732
733 public void LoadBSDXResourcesTable()
734 {
735 string sCommandText = "BSDX RESOURCES^" + m_ConnectInfo.DUZ;
736 ConnectInfo.RPMSDataTable(sCommandText, "Resources", m_dsGlobal);
737 Debug.Write("LoadGlobalRecordsets -- Resources loaded\n");
738 }
739
740 public void LoadResourceGroupTable()
741 {
742 //ResourceGroup Table (Resource Groups by User)
743 //Table "ResourceGroup" contains all resource group names
744 //to which user has access
745 //Fields are: RESOURCE_GROUPID, RESOURCE_GROUP
746 string sCommandText = "BSDX RESOURCE GROUPS BY USER^" + m_ConnectInfo.DUZ;
747 ConnectInfo.RPMSDataTable(sCommandText, "ResourceGroup", m_dsGlobal);
748 Debug.Write("LoadGlobalRecordsets -- ResourceGroup loaded\n");
749 }
750
751 public void LoadGroupResourcesTable()
752 {
753 //Table "GroupResources" contains all active GROUP/RESOURCE combinations
754 //to which user has access based on entries in BSDX RESOURCE USER file
755 //If user has BSDXZMGR or XUPROGMODE keys, then ALL Group/Resource combinstions
756 //are returned.
757 //Fields are: RESOURCE_GROUPID, RESOURCE_GROUP, RESOURCE_GROUP_ITEMID, RESOURCE_NAME, RESOURCE_ID
758 string sCommandText = "BSDX GROUP RESOURCE^" + m_ConnectInfo.DUZ;
759 ConnectInfo.RPMSDataTable(sCommandText, "GroupResources", m_dsGlobal);
760 Debug.Write("LoadGlobalRecordsets -- GroupResources loaded\n");
761 }
762
763 public void LoadScheduleUserTable()
764 {
765 //Table "ScheduleUser" contains an entry for each user in File 200 (NEW PERSON)
766 //who possesses the BSDXZMENU security key.
767 string sCommandText = "BSDX SCHEDULE USER";
768 ConnectInfo.RPMSDataTable(sCommandText, "ScheduleUser", m_dsGlobal);
769 Debug.Write("LoadGlobalRecordsets -- ScheduleUser loaded\n");
770 }
771
772 public void LoadResourceUserTable()
773 {
774 //Table "ResourceUser" duplicates the BSDX RESOURCE USER File.
775 //NOTE: Column names are RESOURCEUSER_ID, RESOURCEID,
776 // OVERBOOK, MODIFY_SCHEDULE, USERID, USERID1
777 //string sCommandText = "SELECT BMXIEN RESOURCEUSER_ID, INTERNAL[RESOURCENAME] RESOURCEID, OVERBOOK, MODIFY_SCHEDULE, USERNAME USERID, INTERNAL[USERNAME] FROM BSDX_RESOURCE_USER";
778 LoadResourceUserTable(false);
779 }
780
781 public void LoadResourceUserTable(bool bAllUsers)
782 {
783 string sCommandText = @"SELECT BMXIEN RESOURCEUSER_ID, RESOURCENAME, INTERNAL[RESOURCENAME] RESOURCEID, OVERBOOK, MODIFY_SCHEDULE, MODIFY_APPOINTMENTS, USERNAME, INTERNAL[USERNAME] USERID FROM BSDX_RESOURCE_USER"; // WHERE INTERNAL[INSTITUTION]=" + m_ConnectInfo.DUZ2;
784
785 if (!bAllUsers)
786 {
787 sCommandText += String.Format(" WHERE INTERNAL[USERNAME] = {0}", m_ConnectInfo.DUZ);
788 }
789
790 ConnectInfo.RPMSDataTable(sCommandText, "ResourceUser", m_dsGlobal);
791 Debug.Write("LoadGlobalRecordsets -- ResourceUser loaded\n");
792 }
793
794
795 public void RegisterDocumentView(CGDocument doc, CGView view)
796 {
797 //Store the view in the list of views
798 this.Views.Add(view, doc);
799
800 //Hook into the view's 'closed' event
801 view.Closed += new EventHandler(ViewClosed);
802
803 //Hook into the view's mnuRPMSServer.Click event
804 view.mnuRPMSServer.Click += new EventHandler(mnuRPMSServer_Click);
805
806 //Hook into the view's mnuRPMSLogin.Click event
807 view.mnuRPMSLogin.Click += new EventHandler(mnuRPMSLogin_Click);
808
809 }
810
811 public void RegisterAVDocumentView(CGAVDocument doc, CGAVView view)
812 {
813 //Store the view in the list of views
814 this.AvailabilityViews.Add(view, doc);
815
816 //Hook into the view's 'closed' event
817 view.Closed += new EventHandler(AVViewClosed);
818 }
819
820 public CGAVView GetAVViewByResource(ArrayList sResourceArray)
821 {
822 if (sResourceArray == null)
823 return null;
824
825 bool bEqual = true;
826 foreach (CGAVView v in m_AVViews.Keys)
827 {
828 CGAVDocument d = v.Document;
829
830 bEqual = false;
831 if (d.Resources.Count == sResourceArray.Count)
832 {
833 bEqual = true;
834 for (int j = 0; j < sResourceArray.Count; j++)
835 {
836 if (sResourceArray.Contains(d.Resources[j]) == false)
837 {
838 bEqual = false;
839 break;
840 }
841 if (d.Resources.Contains(sResourceArray[j]) == false)
842 {
843 bEqual = false;
844 break;
845 }
846 }
847 if (bEqual == true)
848 return v;
849 }
850 }
851 return null;
852 }
853 /// <summary>
854 /// Return the first view having a resource array matching sResourceArray
855 /// </summary>
856 /// <param name="sResourceArray"></param>
857 /// <returns></returns>
858 public CGView GetViewByResource(ArrayList sResourceArray)
859 {
860 if (sResourceArray == null)
861 return null;
862
863 bool bEqual = true;
864 foreach (CGView v in _views.Keys)
865 {
866 CGDocument d = v.Document;
867
868 bEqual = false;
869 if (d.Resources.Count == sResourceArray.Count)
870 {
871 bEqual = true;
872 for (int j = 0; j < sResourceArray.Count; j++)
873 {
874 if (sResourceArray.Contains(d.Resources[j]) == false)
875 {
876 bEqual = false;
877 break;
878 }
879 if (d.Resources.Contains(sResourceArray[j]) == false)
880 {
881 bEqual = false;
882 break;
883 }
884 }
885 if (bEqual == true)
886 return v;
887 }
888 }
889 return null;
890 }
891
892 /// <summary>
893 /// Removes view and Handles Disconnection from Database if no views are left.
894 /// </summary>
895 /// <param name="sender"></param>
896 /// <param name="e"></param>
897 private void ViewClosed(object sender, EventArgs e)
898 {
899 //Remove the sender from our document list
900 Views.Remove(sender);
901
902 //If no documents left, then close RPMS connection & exit the application
903 if ((Views.Count == 0)&&(this.AvailabilityViews.Count == 0)&&(m_bExitOK == true))
904 {
905 m_ConnectInfo.EventPollingEnabled = false;
906 m_ConnectInfo.UnSubscribeEvent("BSDX SCHEDULE");
907 m_ConnectInfo.CloseConnection();
908 Application.Exit();
909 }
910 }
911
912 private void AVViewClosed(object sender, EventArgs e)
913 {
914 //Remove the sender from our document list
915 this.AvailabilityViews.Remove(sender);
916
917 //If no documents left, then close RPMS connection & exit the application
918 if ((Views.Count == 0)&&(this.AvailabilityViews.Count == 0)&&(m_bExitOK == true))
919 {
920 m_ConnectInfo.bmxNetLib.CloseConnection();
921 Application.Exit();
922 }
923 }
924
925 private void KeepAlive()
926 {
927 foreach (CGView v in _views.Keys)
928 {
929 CGDocument d = v.Document;
930 DateTime dNow = DateTime.Now;
931 DateTime dLast = d.LastRefreshed;
932 TimeSpan tsDiff = dNow - dLast;
933 if (tsDiff.Seconds > 180)
934 {
935 for (int j = 0; j < d.Resources.Count; j++)
936 {
937 v.RaiseRPMSEvent("SCHEDULE-" + d.Resources[j].ToString(), "");
938 }
939
940 break;
941 }
942 }
943 }
944
945 /// <summary>
946 /// Propogate availability updates to all sRresource's doc/views
947 /// </summary>
948 public void UpdateViews(string sResource, string sOldResource)
949 {
950 if (sResource == null)
951 return;
952 foreach (CGView v in _views.Keys)
953 {
954 CGDocument d = v.Document;
955 for (int j = 0; j < d.Resources.Count; j++)
956 {
957 if ((sResource == "") || (sResource == ((string) d.Resources[j])) || (sOldResource == ((string) d.Resources[j])))
958 {
959 d.RefreshDocument();
960 break;
961 }
962 }
963 v.UpdateTree();
964 }
965 }
966
967 /// <summary>
968 /// Propogate availability updates to all doc/views
969 /// </summary>
970 public void UpdateViews()
971 {
972 UpdateViews("","");
973 foreach (CGView v in _views.Keys)
974 {
975 v.UpdateTree();
976 }
977 }
978
979 /// <summary>
980 /// Calls each view associated with document Doc and closes it.
981 /// </summary>
982 public void CloseAllViews(CGDocument doc)
983 {
984 //iterate through all views and call update.
985 Hashtable h = CGDocumentManager.Current.Views;
986
987 CGDocument d;
988 int nTempCount = h.Count;
989 do
990 {
991 nTempCount = h.Count;
992 foreach (CGView v in h.Keys)
993 {
994 d = (CGDocument) h[v];
995 if (d == doc)
996 {
997 v.Close();
998 break;
999 }
1000 }
1001 } while ((h.Count > 0) && (nTempCount != h.Count));
1002 }
1003
1004 /// <summary>
1005 /// Calls each view associated with Availability Doc and closes it.
1006 /// </summary>
1007 public void CloseAllViews(CGAVDocument doc)
1008 {
1009 //iterate through all views and call update.
1010 Hashtable h = CGDocumentManager.Current.AvailabilityViews;
1011
1012 CGAVDocument d;
1013 int nTempCount = h.Count;
1014 do
1015 {
1016 nTempCount = h.Count;
1017 foreach (CGAVView v in h.Keys)
1018 {
1019 d = (CGAVDocument) h[v];
1020 if (d == doc)
1021 {
1022 v.Close();
1023 break;
1024 }
1025 }
1026 } while ((h.Count > 0) && (nTempCount != h.Count));
1027
1028
1029 }
1030
1031 /// <summary>
1032 /// Accomplishes Changing the Server to which you connect
1033 /// </summary>
1034 /// <remarks>
1035 /// Parameter relog-in for InitializeApp forces initialize app to use
1036 /// 1. The server the user just picked and then BMX saved off to User Preferences
1037 /// 2. A new access and verify code pair
1038 /// </remarks>
1039 /// <param name="sender">unused</param>
1040 /// <param name="e">unused</param>
1041 private void mnuRPMSServer_Click(object sender, EventArgs e)
1042 {
1043 //Warn that changing servers will close all schedules
1044 if (MessageBox.Show("Are you sure you want to close all schedules and connect to a different VistA server?", "Clinical Scheduling", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
1045 return;
1046
1047 //Reconnect to RPMS and recreate all global recordsets
1048 try
1049 {
1050 // Close All, but tell the Close All method not to call Applicaiton.Exit since we still plan to continue.
1051 // Close All does not call Application.Exit, but CGView_Close handler does
1052 m_bExitOK = false;
1053 CloseAll();
1054 m_bExitOK = true;
1055
1056 //Used in Do loop
1057 bool bRetry = true;
1058
1059 // Do Loop to deal with changing the server and the vagaries of user choices.
1060 do
1061 {
1062 try
1063 {
1064 //ChangeServerInfo does not re-login the user
1065 //It only changes the saved server information in the %APPDATA% folder
1066 //so it can be re-used when BMX tries to log in again.
1067 //Access and Verify code are prompted for in InitializeApp
1068 m_ConnectInfo.ChangeServerInfo();
1069 bRetry = false;
1070 }
1071 catch (Exception ex)
1072 {
1073 if (ex.Message == "User cancelled.")
1074 {
1075 bRetry = false;
1076 Application.Exit();
1077 return;
1078 }
1079 if (MessageBox.Show("Unable to connect to VistA. " + ex.Message , "Clinical Scheduling", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
1080 {
1081 bRetry = true;
1082 }
1083 else
1084 {
1085 bRetry = false;
1086 Application.Exit();
1087 return;
1088 }
1089 }
1090 } while (bRetry == true);
1091
1092 //Parameter for initialize app tells it that this is a re-login and forces a new access and verify code.
1093 bool isEverythingOkay = this.InitializeApp(true);
1094
1095 //if an error occurred, break out. This time we need to call Application.Exit since it's already running.
1096 if (!isEverythingOkay)
1097 {
1098 Application.Exit();
1099 return;
1100 }
1101
1102 //Otherwise, everything is okay. So open document and view, then show and activate view.
1103 CGDocument doc = new CGDocument();
1104 doc.DocManager = _current;
1105
1106 CGView view = new CGView();
1107 view.InitializeDocView(doc, _current, doc.StartDate, _current.WindowText);
1108
1109 view.Show();
1110 view.Activate();
1111
1112 //Application.Run need not be called b/c it is already running.
1113 }
1114 catch (Exception ex)
1115 {
1116 throw ex;
1117 }
1118
1119 }
1120
1121 /// <summary>
1122 /// Accomplishes Re-login into RPMS/VISTA. Now all logic is in this event handler.
1123 /// </summary>
1124 /// <param name="sender">not used</param>
1125 /// <param name="e">not used</param>
1126 private void mnuRPMSLogin_Click(object sender, EventArgs e)
1127 {
1128 //Warn that changing login will close all schedules
1129 if (MessageBox.Show("Are you sure you want to close all schedules and login to VistA?", "Clinical Scheduling", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
1130 return;
1131
1132 //Reconnect to RPMS and recreate all global recordsets
1133 try
1134 {
1135 // Close All, but tell the Close All method not to call Applicaiton.Exit since we still plan to continue.
1136 // Close All does not call Application.Exit, but CGView_Close handler does
1137 m_bExitOK = false;
1138 CloseAll();
1139 m_bExitOK = true;
1140
1141 //Parameter for initialize app tells it that this is a re-login and forces a new access and verify code.
1142 bool isEverythingOkay = this.InitializeApp(true);
1143
1144 //if an error occurred, break out. This time we need to call Application.Exit since it's already running.
1145 if (!isEverythingOkay)
1146 {
1147 Application.Exit();
1148 return;
1149 }
1150
1151 //Otherwise, everything is okay. So open document and view, then show and activate view.
1152 CGDocument doc = new CGDocument();
1153 doc.DocManager = _current;
1154
1155 CGView view = new CGView();
1156 view.InitializeDocView(doc, _current, doc.StartDate, _current.WindowText);
1157
1158 view.Show();
1159 view.Activate();
1160
1161 //Application.Run need not be called b/c it is already running.
1162 }
1163 catch (Exception ex)
1164 {
1165 throw ex;
1166 }
1167
1168 }
1169
1170 delegate void CloseAllDelegate(string sMsg);
1171
1172 private void CloseAll(string sMsg)
1173 {
1174 if (sMsg == "")
1175 {
1176 sMsg = "Scheduling System Shutting Down Immediately for Maintenance.";
1177 }
1178
1179 MessageBox.Show(sMsg, "Clinical Scheduling Administrator -- System Shutdown Notification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
1180
1181 CloseAll();
1182 }
1183
1184 private void CloseAll()
1185 {
1186 //Close all documents, views and connections
1187 Hashtable h = CGDocumentManager.Current.Views;
1188 int nTempCount = h.Count;
1189 do
1190 {
1191 nTempCount = h.Count;
1192 foreach (CGView v in h.Keys)
1193 {
1194 v.Close();
1195 break;
1196 }
1197 } while ((h.Count > 0) && (nTempCount != h.Count));
1198
1199 h = CGDocumentManager.Current.AvailabilityViews;
1200 nTempCount = h.Count;
1201 do
1202 {
1203 nTempCount = h.Count;
1204 foreach (CGAVView v in h.Keys)
1205 {
1206 v.Close();
1207 break;
1208 }
1209 } while ((h.Count > 0) && (nTempCount != h.Count));
1210
1211 }
1212
1213 public delegate DataTable RPMSDataTableDelegate(string CommandString, string TableName);
1214
1215 public DataTable RPMSDataTable(string sSQL, string sTableName)
1216 {
1217 //Retrieves a recordset from RPMS
1218 string sErrorMessage = "";
1219 DataTable dtOut;
1220
1221 try
1222 {
1223 //System.IntPtr pHandle = this.Handle;
1224 RPMSDataTableDelegate rdtd = new RPMSDataTableDelegate(ConnectInfo.RPMSDataTable);
1225 //dtOut = (DataTable) this.Invoke(rdtd, new object[] {sSQL, sTableName});
1226 dtOut = rdtd.Invoke(sSQL, sTableName);
1227 }
1228
1229 catch (Exception ex)
1230 {
1231 sErrorMessage = "CGDocumentManager.RPMSDataTable error: " + ex.Message;
1232 throw ex;
1233 }
1234
1235 return dtOut;
1236
1237 }
1238
1239 public void ChangeDivision(System.Windows.Forms.Form frmCaller)
1240 {
1241 this.ConnectInfo.ChangeDivision(frmCaller);
1242 foreach (CGView v in _views.Keys)
1243 {
1244 v.InitializeDocView(v.Document.DocName);
1245 v.Document.RefreshDocument();
1246 }
1247 }
1248
1249 public void ViewRefresh()
1250 {
1251 foreach (CGView v in _views.Keys)
1252 {
1253 try
1254 {
1255 v.Document.RefreshDocument();
1256 }
1257 catch (Exception ex)
1258 {
1259 Debug.Write("CGDocumentManager.ViewRefresh Exception: " + ex.Message + "\n");
1260 }
1261 finally
1262 {
1263 }
1264 }
1265 Debug.Write("DocManager refreshed all views.\n");
1266 }
1267
1268 #endregion Methods & Events
1269
1270 }
1271}
Note: See TracBrowser for help on using the repository browser.