source: BMXNET_RPMS_dotNET_UTILITIES-BMX/trunk/cs/bmx_0200scr/BMX2/BMXNet/BMXNetConnectInfo.cs@ 1063

Last change on this file since 1063 was 1063, checked in by Sam Habiel, 13 years ago

BMXNetConnectInfo:
Pass up BMXNetException instead of recreating it as a new generic Exception; for more accurate error catching.
W32Handle for class not acquired. Don't even know why we needed that.
DSelectDivision:
Load event now has this.Activate to bring the window to the front. Needed b/c of new splash screen in Sched GUI which will show up front unless this form is activated.
BMXNetLib:
Better handling of exception in OpenConnectionCommon (pass inner exception when creating BMXNetException.

File size: 33.2 KB
Line 
1using System;
2using System.Data;
3using System.Diagnostics;
4using System.IO;
5using System.IO.IsolatedStorage;
6using System.Runtime.Serialization;
7using System.Runtime.Serialization.Formatters;
8using System.Runtime.Serialization.Formatters.Soap;
9using System.Windows.Forms;
10using System.Security.Principal;
11using System.Text;
12using System.Security.Cryptography;
13using System.Timers;
14using System.Threading;
15
16
17namespace IndianHealthService.BMXNet
18{
19 public class BMXNetEventArgs : EventArgs
20 {
21 public string BMXParam;
22 public string BMXEvent;
23 }
24
25 /// <summary>
26 /// Contains methods and properties to support RPMS Login for .NET applications
27 /// </summary>
28 public class BMXNetConnectInfo : System.Windows.Forms.Control
29 {
30
31 /// <summary>
32 /// Serializes RPMS server address and port
33 /// </summary>
34 [SerializableAttribute]
35 private class ServerData
36 {
37 public string m_sAddress = "";
38 public int m_nPort = 0;
39 public string m_sNamespace = "";
40
41 public ServerData()
42 {
43 }
44
45 public ServerData(string sAddress, int nPort)
46 {
47 this.m_nPort = nPort;
48 this.m_sAddress = sAddress;
49 this.m_sNamespace = "";
50 }
51 public ServerData(string sAddress, int nPort, string sNamespace)
52 {
53 this.m_nPort = nPort;
54 this.m_sAddress = sAddress;
55 this.m_sNamespace = sNamespace;
56 }
57 }
58
59 /// <summary>
60 /// BMXNetConnector With Default Encoding (invokes BMXNetLib)
61 /// </summary>
62 public BMXNetConnectInfo() : this("") { }
63
64 /// <summary>
65 /// BMXNetConnector with a specific encoding
66 /// </summary>
67 /// <param name="encoding">String representation of encoding (e.g. windows-1256 for arabic).
68 /// If encoding is not found, we fall back to the default windows encoding. A debug message
69 /// is printed to that effect.</param>
70 public BMXNetConnectInfo(string encoding)
71 {
72 if (encoding == String.Empty) m_BMXNetLib = new BMXNetLib();
73 else m_BMXNetLib = new BMXNetLib(encoding);
74
75 m_timerEvent = new System.Timers.Timer();
76 m_timerEvent.Elapsed += new ElapsedEventHandler(OnEventTimer);
77 m_timerEvent.Interval = 10000;
78 m_timerEvent.Enabled = false;
79 }
80
81 #region BMXNetEvent
82
83 private System.Timers.Timer m_timerEvent;
84 public delegate void BMXNetEventDelegate(Object obj, BMXNetEventArgs e);
85 public event BMXNetEventDelegate BMXNetEvent;
86
87 /// <summary>
88 /// Enables and disables event polling for the RPMS connection
89 /// </summary>
90 public bool EventPollingEnabled
91 {
92 get
93 {
94 return m_timerEvent.Enabled;
95 }
96 set
97 {
98 m_timerEvent.Enabled = value;
99 }
100 }
101
102 /// <summary>
103 /// Sets and retrieves the interval in milliseconds for RPMS event polling
104 /// </summary>
105 public double EventPollingInterval
106 {
107 get
108 {
109 return m_timerEvent.Interval;
110 }
111 set
112 {
113 m_timerEvent.Interval = value;
114 }
115 }
116
117 /// <summary>
118 /// Register interest in an RPMS event.
119 /// </summary>
120 /// <param name="EventName"></param>
121 /// <returns></returns>
122 public int SubscribeEvent(string EventName)
123 {
124 try
125 {
126 //System.IntPtr pHandle = this.Handle;
127 DataTable dt;
128 RPMSDataTableDelegate rdtd = new RPMSDataTableDelegate(RPMSDataTable);
129 if (this.IsHandleCreated == false)
130 {
131 this.CreateHandle();
132 }
133 dt = (DataTable) this.Invoke(rdtd, new object[] {"BMX EVENT REGISTER^" + EventName, "dt"});
134
135// dt = RPMSDataTable("BMX EVENT REGISTER^" + EventName, "dt");
136 DataRow dr = dt.Rows[0];
137 int nRet = (int) dr["ERRORID"];
138 return nRet;
139 }
140 catch (Exception ex)
141 {
142 Debug.Write(ex.Message);
143 return 99;
144 }
145 }
146
147 /// <summary>
148 /// Unregister an RPMS event
149 /// </summary>
150 /// <param name="EventName"></param>
151 /// <returns></returns>
152 public int UnSubscribeEvent(string EventName)
153 {
154 try
155 {
156 DataTable dt;
157 RPMSDataTableDelegate rdtd = new RPMSDataTableDelegate(RPMSDataTable);
158 if (this.IsHandleCreated == false)
159 {
160 this.CreateHandle();
161 }
162 dt = (DataTable) this.Invoke(rdtd, new object[] {"BMX EVENT UNREGISTER^" + EventName, "dt"});
163
164 DataRow dr = dt.Rows[0];
165 int nRet = (int) dr["ERRORID"];
166 return nRet;
167 }
168 catch (Exception ex)
169 {
170 Debug.Write(ex.Message);
171 return 99;
172 }
173 }
174
175 /// <summary>
176 /// Raise an RPMS event
177 /// </summary>
178 /// <param name="EventName">The name of the event to raise</param>
179 /// <param name="Param">Parameters associated with the event</param>
180 /// <param name="RaiseBack">True if the event should be raised back to the caller</param>
181 /// <returns></returns>
182 public int RaiseEvent(string EventName, string Param, bool RaiseBack)
183 {
184 string sBack = (RaiseBack == true)?"TRUE":"FALSE";
185 try
186 {
187 DataTable dt;
188 RPMSDataTableDelegate rdtd = new RPMSDataTableDelegate(RPMSDataTable);
189 if (this.IsHandleCreated == false)
190 {
191 this.CreateHandle();
192 }
193 dt = (DataTable) this.Invoke(rdtd, new object[] {"BMX EVENT RAISE^" + EventName + "^" + Param + "^" + sBack + "^", "dt"});
194
195 DataRow dr = dt.Rows[0];
196 int nRet = (int) dr["ERRORID"];
197 return nRet;
198 }
199 catch (Exception ex)
200 {
201 Debug.Write(ex.Message);
202 return 99;
203 }
204 }
205
206 /// <summary>
207 /// Sets and retrieves the number of times that the Event Timer will generage a BMXNet AutoFire event.
208 /// For example, if AutoFire == 3, then every 3rd time the Event Timer fires, it will generate an AutoFire event.
209 /// </summary>
210 public int AutoFire
211 {
212 get
213 {
214 return m_nAutoFireIncrements;
215 }
216 set
217 {
218 m_nAutoFireIncrements = value;
219 }
220 }
221
222 //Retrieve events registered by this session
223 private int m_nAutoFireIncrements = 0;
224 private int m_nAutoFireCount = 0;
225
226 private void OnEventTimer(object source, ElapsedEventArgs e)
227 {
228 try
229 {
230 this.bmxNetLib.BMXRWL.AcquireWriterLock(5);
231 try
232 {
233 this.m_timerEvent.Enabled = false;
234
235 Object obj = this;
236 BMXNetEventArgs args = new BMXNetEventArgs();
237 m_nAutoFireCount++;
238 if ((m_nAutoFireIncrements > 0)&&(m_nAutoFireCount >= m_nAutoFireIncrements))
239 {
240 m_nAutoFireCount = 0;
241 args.BMXEvent = "BMXNet AutoFire";
242 args.BMXParam = "";
243 if (BMXNetEvent != null)
244 {
245 Debug.Write("BMXNet AutoFire event raised from BMXNetConnectInfo");
246 BMXNetEvent(obj, args);
247 }
248 this.m_timerEvent.Enabled = true;
249 return;
250 }
251
252 if (m_BMXNetLib.Connected == false)
253 {
254 this.m_timerEvent.Enabled = true;
255 return;
256 }
257
258 DataTable dtEvents = new DataTable("BMXNetEvents");
259
260 try
261 {
262 if (this.IsHandleCreated == false)
263 {
264 this.CreateHandle();
265 }
266 RPMSDataTableDelegate rdtd = new RPMSDataTableDelegate(RPMSDataTable);
267 dtEvents = (DataTable) this.Invoke(rdtd, new object[] {"BMX EVENT POLL", "BMXNetEvents"});
268 }
269 catch (Exception ex)
270 {
271 string sMsg = ex.Message;
272 this.m_timerEvent.Enabled = true;
273 return;
274 }
275
276 try
277 {
278 if (dtEvents.Rows.Count == 0)
279 {
280 this.m_timerEvent.Enabled = true;
281 return;
282 }
283 }
284 catch(Exception ex)
285 {
286 Debug.Write("upper Exception in BMXNetConnectInfo.OnEventTimer: " + ex.Message + "\n");
287 }
288 try
289 {
290 //If events exist, raise BMXNetEvent
291 foreach (DataRow dr in dtEvents.Rows)
292 {
293 args.BMXEvent = dr["EVENT"].ToString();
294 args.BMXParam = dr["PARAM"].ToString();
295 if (BMXNetEvent != null)
296 {
297 BMXNetEvent(obj, args);
298 }
299 }
300 this.m_timerEvent.Enabled = true;
301 return;
302 }
303 catch(Exception ex)
304 {
305 Debug.Write("lower Exception in BMXNetConnectInfo.OnEventTimer: " + ex.Message + "\n");
306 }
307 }
308 catch(Exception ex)
309 {
310 Debug.Write("Exception in BMXNetConnectInfo.OnEventTimer: " + ex.Message + "\n");
311 }
312 finally
313 {
314 this.bmxNetLib.BMXRWL.ReleaseWriterLock();
315 this.m_timerEvent.Enabled = true;
316 }
317 }
318 catch
319 {
320 Debug.Write(" OnEventTimer failed to obtain lock.\n");
321 }
322 }
323
324 #endregion BMXNetEvent
325
326 #region Fields
327
328 private ServerData m_ServerData;
329 private string m_sServerAddress;
330 private int m_nServerPort;
331 private string m_sServerNamespace;
332 private string m_sDUZ2;
333 private int m_nDivisionCount = 0;
334 private string m_sUserName;
335 private string m_sDivision;
336 private string m_sAppcontext;
337 private BMXNetLib m_BMXNetLib;
338 private bool m_bAutoServer = false;
339 private bool m_bAutoLogin = false;
340
341 #endregion Fields
342
343 #region Properties
344
345// /// <summary>
346// /// Set and retrieve the timeout in milliseconds for locking the transmit port.
347// /// If the transmit port is unavailable an ApplicationException will be thrown.
348// /// </summary>
349// public int TransmitLockTimeout
350// {
351// get
352// {
353// return m_nTransmitLockTimeout;
354// }
355// set
356// {
357// m_nTransmitLockTimeout = value;
358// }
359// }
360
361 /// <summary>
362 /// Set and retrieve the timeout, in milliseconds, to receive a response from the RPMS server.
363 /// If the retrieve time exceeds the timeout, an exception will be thrown and the connection will be closed.
364 /// The default is 30 seconds.
365 /// </summary>
366 public int ReceiveTimeout
367 {
368 get { return m_BMXNetLib.ReceiveTimeout; }
369 set { m_BMXNetLib.ReceiveTimeout = value; }
370 }
371
372 public string MServerNameSpace
373 {
374 get
375 {
376 return m_BMXNetLib.MServerNamespace;
377 }
378 set
379 {
380 m_BMXNetLib.MServerNamespace = value;
381 }
382 }
383
384 public BMXNetLib bmxNetLib
385 {
386 get
387 {
388 return m_BMXNetLib;
389 }
390 }
391
392 public string AppContext
393 {
394 get
395 {
396 return m_sAppcontext;
397 }
398 set
399 {
400 if (m_BMXNetLib.Connected == true)
401 {
402 try
403 {
404 try
405 {
406 m_BMXNetLib.AppContext = value;
407 m_sAppcontext = value;
408 }
409 catch (Exception ex)
410 {
411 Debug.Write(ex.Message);
412 throw ex;
413 }
414 finally
415 {
416 }
417 }
418 catch (ApplicationException aex)
419 {
420 // The writer lock request timed out.
421 Debug.Write("BMXNetConnectInfo.AppContext lock request timed out.\n");
422 throw aex;
423 }
424 }//end if
425 }//end set
426 }
427
428 public bool Connected
429 {
430 get
431 {
432 return m_BMXNetLib.Connected;
433 }
434 }
435
436 public string UserName
437 {
438 get
439 {
440 return this.m_sUserName;
441 }
442 }
443
444 public string DivisionName
445 {
446 get
447 {
448 return this.m_sDivision;
449 }
450 }
451
452 /// <summary>
453 /// Returns a string representation of DUZ
454 /// </summary>
455 public string DUZ
456 {
457 get
458 {
459 return this.bmxNetLib.DUZ;
460 }
461 }
462
463 /// <summary>
464 /// Sets and Returns DUZ(2)
465 /// </summary>
466 public string DUZ2
467 {
468 get
469 {
470 return m_sDUZ2;
471 }
472 set
473 {
474 try
475 {
476 //Set DUZ(2) in M partition
477 DataTable dt = this.RPMSDataTable("BMXSetFac^" + value, "SetDUZ2");
478 Debug.Assert(dt.Rows.Count == 1);
479 DataRow dr = dt.Rows[0];
480 string sDUZ2 = dr["FACILITY_IEN"].ToString();
481 if (sDUZ2 != "0")
482 {
483 m_sDUZ2 = sDUZ2;
484 this.m_sDivision = dr["FACILITY_NAME"].ToString();
485 }
486 }
487 catch (Exception ex)
488 {
489 Debug.Write("DUZ2.Set failed: " + ex.Message + "\n");
490 }
491 }
492 }
493
494 /// <summary>
495 /// Gets the address of the RPMS Server
496 /// </summary>
497 public string MServerAddress
498 {
499 get
500 {
501 return this.m_sServerAddress;
502 }
503 }
504
505 /// <summary>
506 /// Gets the port on which the MServer is connected
507 /// </summary>
508 public int MServerPort
509 {
510 get
511 {
512 return this.m_nServerPort;
513 }
514 }
515
516 public DataTable UserDivisions
517 {
518 get
519 {
520 DataTable dt = this.GetUserDivisions();
521 return dt;
522 }
523 }
524
525 #endregion Properties
526
527 #region Methods
528
529 public void CloseConnection()
530 {
531 //TODO: Make thread safe
532 this.m_bAutoServer = false;
533 this.m_bAutoLogin = false;
534 m_BMXNetLib.CloseConnection();
535 }
536
537 /// <summary>
538 /// For backwards compatibility. Internally calls LoadConnectInfo()
539 /// </summary>
540 public void Login()
541 {
542 LoadConnectInfo();
543 }
544
545 /// <summary>
546 /// Change the internet address and port of the RPMS server
547 /// Throws a BMXNetException if user cancels
548 /// </summary>
549 public void ChangeServerInfo()
550 {
551 //Get existing values from isolated storage
552 ServerData serverData = new ServerData();
553 Stream stStorage = null;
554 try
555 {
556 IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForAssembly();
557 string sFileName = "mserver0200.dat";
558 stStorage = new IsolatedStorageFileStream(sFileName, FileMode.Open, isStore);
559 IFormatter formatter = new SoapFormatter();
560 serverData = (ServerData) formatter.Deserialize(stStorage);
561 stStorage.Close();
562 }
563 catch (Exception ex)
564 {
565 Debug.Write(ex.Message);
566 if (stStorage != null)
567 stStorage.Close();
568 }
569
570 try
571 {
572 DServerInfo dsi = new DServerInfo();
573 dsi.InitializePage(serverData.m_sAddress,serverData.m_nPort, serverData.m_sNamespace);
574 if (dsi.ShowDialog() != DialogResult.OK)
575 {
576 throw new BMXNetException("User cancelled.");
577 }
578 serverData.m_sAddress = dsi.MServerAddress;
579 serverData.m_nPort = dsi.MServerPort;
580 serverData.m_sNamespace = dsi.MServerNamespace;
581
582 this.m_sServerAddress = dsi.MServerAddress;
583 this.m_nServerPort = dsi.MServerPort;
584 this.m_sServerNamespace = dsi.MServerNamespace;
585
586 //Save port and address to isolated storage
587 try
588 {
589 string sFileName = "mserver0200.dat";
590 IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForAssembly();
591 stStorage = new IsolatedStorageFileStream(sFileName, FileMode.Create, isStore);
592 IFormatter formatter = new SoapFormatter();
593 formatter.Serialize(stStorage, serverData);
594 stStorage.Close();
595 }
596 catch (Exception ex)
597 {
598 Debug.Write(ex.Message);
599 if (stStorage != null)
600 stStorage.Close();
601 }
602
603 }
604 catch (Exception ex)
605 {
606 Debug.Write(ex.Message);
607 if (stStorage != null)
608 stStorage.Close();
609 if (ex.Message == "User cancelled.")
610 {
611 throw ex;
612 }
613 }
614
615 }
616
617 private void GetServerInfo(ref int nPort, ref string sAddress, ref string sNamespace)
618 {
619 //Get values from isolated storage
620 bool bLoaded = false;
621 Stream stStorage = null;
622 try
623 {
624 m_ServerData = new ServerData();
625 IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForAssembly();
626 string sFileName = "mserver0200.dat";
627 stStorage = new IsolatedStorageFileStream(sFileName, FileMode.Open, isStore);
628 IFormatter formatter = new SoapFormatter();
629 m_ServerData = (ServerData) formatter.Deserialize(stStorage);
630 stStorage.Close();
631 sAddress = m_ServerData.m_sAddress;
632 nPort = m_ServerData.m_nPort;
633 sNamespace = m_ServerData.m_sNamespace;
634
635 bLoaded = true;
636 return;
637 }
638 catch (Exception ex)
639 {
640 Debug.Write(ex.Message);
641 if (stStorage != null)
642 stStorage.Close();
643 }
644 try
645 {
646 //If unable to deserialize, display dialog to collect values
647 if (bLoaded == false)
648 {
649 DServerInfo dsi = new DServerInfo();
650 dsi.InitializePage("",10501,"");
651 if (dsi.ShowDialog() != DialogResult.OK)
652 {
653 throw new BMXNetException("Unable to get M Server information");
654 }
655 m_ServerData.m_sAddress = dsi.MServerAddress;
656 m_ServerData.m_nPort = dsi.MServerPort;
657 m_ServerData.m_sNamespace = dsi.MServerNamespace;
658 }
659
660 sAddress = m_ServerData.m_sAddress;
661 nPort = m_ServerData.m_nPort;
662 sNamespace = m_ServerData.m_sNamespace;
663
664 //Save port and address to isolated storage
665 try
666 {
667 string sFileName = "mserver0200.dat";
668 IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForAssembly();
669 stStorage = new IsolatedStorageFileStream(sFileName, FileMode.Create, isStore);
670 IFormatter formatter = new SoapFormatter();
671 formatter.Serialize(stStorage, m_ServerData);
672 stStorage.Close();
673 }
674 catch (Exception ex)
675 {
676 Debug.Write(ex.Message);
677 if (stStorage != null)
678 stStorage.Close();
679 }
680
681 return;
682 }
683 catch (Exception ex)
684 {
685 Debug.Write(ex.Message);
686 if (stStorage != null)
687 stStorage.Close();
688 throw ex;
689 }
690
691 }
692
693 /// <summary>
694 /// Called to connect to an M server
695 /// Server address, port, Access and Verify codes will be
696 /// prompted for depending on whether these values are
697 /// cached.
698 /// </summary>
699 public void LoadConnectInfo()
700 {
701 m_bAutoServer = true;
702 m_bAutoLogin = true;
703 LoadConnectInfo("",0,"","");
704 }
705
706 /// <summary>
707 /// Called to connect to the M server specified by
708 /// server address and listener port. The default namespace on the server will be used.
709 /// Access and Verify codes will be prompted if
710 /// valid values for the current Windows Identity are
711 /// not cached on the server.
712 /// </summary>
713 /// <param name="MServerAddress">The IP address or name of the MServer</param>
714 /// <param name="Port">The port on which the BMXNet Monitor is listening</param>
715 public void LoadConnectInfo(string MServerAddress, int Port)
716 {
717 LoadConnectInfo(MServerAddress, Port, "", "", "");
718 }
719
720 /// <summary>
721 /// Called to connect to the M server specified by
722 /// server address, listener port and namespace.
723 /// Access and Verify codes will be prompted if
724 /// valid values for the current Windows Identity are
725 /// not cached on the server.
726 /// </summary>
727 /// <param name="MServerAddress">The IP address or name of the MServer</param>
728 /// <param name="Port">The port on which the BMXNet Monitor is listening</param>
729 /// <param name="Namespace">The namespace in which the BMXNet application will run</param>
730 public void LoadConnectInfo(string MServerAddress, int Port, string Namespace)
731 {
732 m_bAutoServer = false;
733 m_bAutoLogin = true;
734 LoadConnectInfo(MServerAddress, Port,"","", Namespace);
735 }
736
737 /// <summary>
738 /// Called to connect to an M server
739 /// using specific Access and Verify codes.
740 /// Server address and port will be prompted if they
741 /// are not cached in local storage.
742 /// </summary>
743 /// <param name="AccessCode">The user's access code</param>
744 /// <param name="VerifyCode">The user's verify code</param>
745 public void LoadConnectInfo(string AccessCode, string VerifyCode)
746 {
747 m_bAutoServer = true;
748 m_bAutoLogin = false;
749 LoadConnectInfo("", 0,AccessCode,VerifyCode);
750 }
751
752 /// <summary>
753 /// Called to connect to a specific M server
754 /// using specific Access and Verify codes.
755 /// </summary>
756 /// <param name="AccessCode">The user's access code</param>
757 /// <param name="VerifyCode">The user's verify code</param>
758 /// <param name="MServerAddress">The IP address or name of the MServer</param>
759 /// <param name="Port">The port on which the BMXNet Monitor is listening</param>
760 public void LoadConnectInfo(string MServerAddress, int Port,
761 string AccessCode, string VerifyCode)
762 {
763 LoadConnectInfo(MServerAddress, Port, AccessCode, VerifyCode, "");
764 }
765
766 /// <summary>
767 /// Called to connect to a specific namespace on the M server
768 /// using specific Access and Verify codes.
769 /// </summary>
770 /// <param name="AccessCode">The user's access code</param>
771 /// <param name="VerifyCode">The user's verify code</param>
772 /// <param name="MServerAddress">The IP address or name of the MServer</param>
773 /// <param name="Port">The port on which the BMXNet Monitor is listening</param>
774 /// <param name="Namespace">The namespace in which the BMXNet application will run</param>
775 public void LoadConnectInfo(string MServerAddress, int Port,
776 string AccessCode, string VerifyCode, string Namespace)
777 {
778 //Throws exception if unable to connect to RPMS
779
780 /*
781 * Get RPMS Server Address and Port from local storage.
782 * Prompt for them if they're not there.
783 *
784 * Throw exception if unable to get address/port
785 */
786
787 if (m_bAutoServer == true)
788 {
789 string sAddress = "";
790 int nPort = 0;
791 string sNamespace = "";
792 try
793 {
794 GetServerInfo(ref nPort, ref sAddress, ref sNamespace);
795 m_nServerPort = nPort;
796 m_sServerAddress = sAddress;
797 m_sServerNamespace = sNamespace;
798 }
799 catch (Exception ex)
800 {
801 Debug.Write(ex.Message);
802 throw ex;
803 }
804 }
805 else
806 {
807 m_sServerAddress = MServerAddress;
808 m_nServerPort = Port;
809 m_sServerNamespace = Namespace;
810 }
811
812 /*
813 * Connect to RPMS using current windows identity
814 * Execute BMXNetGetCodes(NTDomain/UserName) to get encrypted AV codes
815 *
816 */
817
818 m_BMXNetLib.CloseConnection();
819 m_BMXNetLib.MServerPort = m_nServerPort;
820 m_BMXNetLib.MServerNamespace = m_sServerNamespace;
821
822 string sLoginError = "";
823 WindowsIdentity winIdentity = WindowsIdentity.GetCurrent();
824 string sIdentName = winIdentity.Name;
825 string sIdentType = winIdentity.AuthenticationType;
826 bool bIdentIsAuth = winIdentity.IsAuthenticated;
827 bool bRet = false;
828 if (m_bAutoLogin == true)
829 {
830 try
831 {
832 //Attempt Auto-login using WindowsIdentity
833
834 if (bIdentIsAuth == false)
835 {
836 throw new BMXNetException("Current Windows User is not authenticated");
837 }
838 bRet = m_BMXNetLib.OpenConnection(m_sServerAddress, winIdentity);
839
840 try
841 {
842 string sDuz = m_BMXNetLib.DUZ;
843 int nDuz = Convert.ToInt16(sDuz);
844 }
845 catch (Exception exCV)
846 {
847 Debug.Write("OpenConnection failed: " + exCV.Message);
848 //Debug.Assert(false);
849 throw new Exception(exCV.Message);
850 }
851 }
852 catch (Exception ex)
853 {
854 Debug.Write(ex.Message);
855 sLoginError = ex.Message;
856 }
857 }
858
859 if (m_BMXNetLib.Connected == false) //BMXNET Login failed or m_bAutoLogin == false
860 {
861 try
862 {
863 //If autologin was attempted and
864 //error message anything other than
865 //"invalid AV code pair"
866 // or "User BMXNET,APPLICATION does not have access to option BMXRPC",
867 // or current windows user is not authenticated or is a guest
868 //then rethrow exception.
869 if ((m_bAutoLogin == true)
870 &&(BMXNetLib.FindSubString(sLoginError, "Not a valid ACCESS CODE/VERIFY CODE pair.") == -1)
871 &&(BMXNetLib.FindSubString(sLoginError, "User BMXNET,APPLICATION does not have access to option BMXRPC") == -1)
872 &&(BMXNetLib.FindSubString(sLoginError, "Not a valid Windows Identity map value.") == -1)
873 &&(BMXNetLib.FindSubString(sLoginError, "Current Windows User is not authenticated") == -1)
874 &&(BMXNetLib.FindSubString(sLoginError, "Windows Integrated Security Not Allowed on this port.") == -1)
875 )
876 {
877 throw new BMXNetException(sLoginError);
878 }
879
880 //Display dialog to collect user-input AV
881
882 DLoginInfo dLog = new DLoginInfo();
883 DialogResult bStop = DialogResult.OK;
884 m_BMXNetLib.CloseConnection();
885 if ((AccessCode == "") && (VerifyCode == ""))
886 {
887 //nothing was passed in, so display a dialog to collect AV codes
888 do
889 {
890 dLog.InitializePage("","");
891 bStop = dLog.ShowDialog();
892 if (bStop == DialogResult.Cancel)
893 {
894 throw new BMXNetException("User cancelled login.");
895 }
896 try
897 {
898 string sTempAccess = dLog.AccessCode;
899 string sTempVerify = dLog.VerifyCode;
900 bRet = m_BMXNetLib.OpenConnection(m_sServerAddress, sTempAccess, sTempVerify);
901 }
902 catch (Exception ex)
903 {
904 Debug.Write(ex.Message);
905 //MessageBox.Show(ex.Message, "RPMS Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
906 throw ex;
907 }
908 }while ((bStop == DialogResult.OK) && (m_BMXNetLib.Connected == false));
909 }
910 else //caller passed in AV codes
911 {
912 try
913 {
914 //Connect using caller's AV
915 bRet = m_BMXNetLib.OpenConnection(m_sServerAddress, AccessCode, VerifyCode);
916 }
917 catch (Exception ex)
918 {
919 Debug.Write(ex.Message);
920 //MessageBox.Show(ex.Message, "RPMS Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
921 throw new BMXNetException(ex.Message);
922 }
923 }
924
925 //Map Windows Identity to logged-in RPMS user
926 if ((bIdentIsAuth == true) && (m_BMXNetLib.Connected == true))
927 {
928 m_BMXNetLib.AppContext = "BMXRPC";
929 string sRes = m_BMXNetLib.TransmitRPC("BMXNetSetUser", sIdentName);
930 Debug.Write("LoadConnectInfo BMXNetSetUser returned " + sRes + "\n");
931 }
932 }
933 catch (Exception ex)
934 {
935 Debug.Write(ex.Message);
936 m_BMXNetLib.CloseConnection();
937 throw ex; //this exception will be caught by the caller.
938 }
939 }//End if (m_BMXNetLib.Connected == false)
940
941 try
942 {
943 Debug.Assert(m_BMXNetLib.Connected == true);
944 m_BMXNetLib.AppContext = "BMXRPC";
945 string sRpc = "BMX USER";
946 Debug.Assert(m_BMXNetLib.AppContext == "BMXRPC");
947
948 bool bCtxt = false;
949 int nCtxt = 0;
950 do
951 {
952 try
953 {
954 m_sUserName = m_BMXNetLib.TransmitRPC(sRpc, this.DUZ);
955 bCtxt = true;
956 Debug.Write("BMXNet::LoadConnectInfo succeeded.\n");
957 }
958 catch (Exception ex)
959 {
960 Debug.Write("BMXNet::LoadConnectInfo retrying: " + ex.Message + "\n");
961 m_BMXNetLib.AppContext = "BMXRPC";
962 nCtxt++;
963 if (nCtxt > 4)
964 throw ex;
965 }
966 }while (bCtxt == false);
967
968
969 System.Data.DataTable rsDivisions;
970 rsDivisions = this.GetUserDivisions();
971 m_nDivisionCount = rsDivisions.Rows.Count;
972
973 // Must have at least one division in order for our code to work
974 // If user has no divisions, M routine will invent one for our sake (get it from kernel).
975 //
976 Debug.Assert(m_nDivisionCount > 0, "Must get at least one division from VISTA");
977
978 // if more than one division, have user pick
979 // if just one, get the DUZ(2) and then set it via the property DUZ2.
980
981 if (m_nDivisionCount > 1)
982 {
983 ChangeDivision(null);
984 // following true if user cancelled.
985 if (DUZ2 == null) throw new BMXNetException("No division chosen -- can't log in");
986 }
987 // if just one
988 else DUZ2 = m_sDUZ2 = rsDivisions.Rows[0]["FACILITY_IEN"].ToString();
989
990 }
991 catch(Exception bmxEx)
992 {
993 m_BMXNetLib.CloseConnection();
994 string sMessage = bmxEx.Message + bmxEx.StackTrace;
995 throw new BMXNetException(sMessage);
996 }
997 return;
998 }
999
1000 private DataTable GetUserDivisions()
1001 {
1002 try
1003 {
1004 DataTable tb = this.RPMSDataTable("BMXGetFacRS^" + this.DUZ, "DivisionTable");
1005 return tb;
1006 }
1007 catch (Exception bmxEx)
1008 {
1009 string sMessage = bmxEx.Message + bmxEx.StackTrace;
1010 throw new BMXNetException(sMessage);
1011
1012 }
1013 }
1014
1015 public void ChangeDivision(System.Windows.Forms.Form frmCaller)
1016 {
1017 DSelectDivision dsd = new DSelectDivision();
1018 dsd.InitializePage(UserDivisions);
1019
1020 if (dsd.ShowDialog(frmCaller) == DialogResult.Cancel)
1021 return;
1022
1023 if (dsd.DUZ2 != this.DUZ2)
1024 {
1025 DUZ2 = dsd.DUZ2;
1026 }
1027 }
1028
1029 public string GetDSN(string sAppContext)
1030 {
1031 string sDsn = "Data source=";
1032 if (sAppContext == "")
1033 sAppContext = "BMXRPC";
1034
1035 if (m_BMXNetLib.Connected == false)
1036 return sDsn.ToString();
1037
1038 sDsn += this.m_sServerAddress ;
1039 sDsn += ";Location=";
1040 sDsn += this.m_nServerPort.ToString();
1041 sDsn += ";Extended Properties=";
1042 sDsn += sAppContext;
1043
1044 return sDsn;
1045 }
1046
1047
1048 public bool Lock(string sVariable, string sIncrement, string sTimeOut)
1049 {
1050 bool bRet = false;
1051 string sErrorMessage = "";
1052
1053 if (m_BMXNetLib.Connected == false)
1054 {
1055 return bRet;
1056 }
1057 try
1058 {
1059 bRet = this.bmxNetLib.Lock(sVariable, sIncrement, sTimeOut);
1060 return bRet;
1061 }
1062 catch(Exception ex)
1063 {
1064 sErrorMessage = "CGDocumentManager.RPMSDataTable error: " + ex.Message;
1065 throw ex;
1066 }
1067 }
1068
1069 delegate DataTable RPMSDataTableDelegate(string CommandString, string TableName);
1070 delegate DataTable RPMSDataTableDelegate2(string CommandString, string TableName, DataSet dsDataSet);
1071
1072 /// <summary>
1073 /// Creates and names a DataTable using the command in CommandString
1074 /// and the name in TableName.
1075 /// </summary>
1076 /// <param name="CommandString"> The SQL or RPC call</param>
1077 /// <param name="TableName">The name of the resulting table</param>
1078 /// <returns>
1079 /// Returns the resulting DataTable.
1080 /// </returns>
1081 public DataTable RPMSDataTable(string CommandString, string TableName)
1082 {
1083 return this.RPMSDataTable(CommandString, TableName, null);
1084 }
1085
1086 /// <summary>
1087 /// Creates and names a DataTable using the command in CommandString
1088 /// and the name in TableName then adds it to DataSet.
1089 /// </summary>
1090 /// <param name="CommandString">The SQL or RPC call</param>
1091 /// <param name="TableName">The name of the resulting table</param>
1092 /// <param name="dsDataSet">The dataset in which to place the table</param>
1093 /// <returns>
1094 /// Returns the resulting DataTable.
1095 /// </returns>
1096 public DataTable RPMSDataTable(string CommandString, string TableName, DataSet dsDataSet)
1097 {
1098 //Retrieves a recordset from RPMS
1099 //Debug.Assert(this.InvokeRequired == false);
1100 string sErrorMessage = "";
1101 DataTable dtResult = new DataTable(TableName);
1102
1103 if (m_BMXNetLib.Connected == false)
1104 return dtResult;
1105
1106 try
1107 {
1108 BMXNetConnection rpmsConn = new BMXNetConnection(m_BMXNetLib);
1109 BMXNetCommand cmd = (BMXNetCommand) rpmsConn.CreateCommand();
1110 BMXNetDataAdapter da = new BMXNetDataAdapter();
1111
1112 cmd.CommandText = CommandString;
1113 da.SelectCommand = cmd;
1114 if (dsDataSet == null)
1115 {
1116 da.Fill(dtResult);
1117 }
1118 else
1119 {
1120 da.Fill(dsDataSet, TableName);
1121 dtResult = dsDataSet.Tables[TableName];
1122 }
1123 Debug.Write(dtResult.TableName + " DataTable retrieved\n");
1124 return dtResult;
1125 }
1126 catch (Exception ex)
1127 {
1128 sErrorMessage = "CGDocumentManager.RPMSDataTable error: " + ex.Message;
1129 throw ex;
1130 }
1131 }
1132
1133 public int RPMSDataTableAsyncQue(string CommandString, string EventName)
1134 {
1135 try
1136 {
1137 string sCommand = "BMX ASYNC QUEUE^";
1138 //replace ^'s in CommandString with $c(30)'s
1139 char[] cDelim = new char[1];
1140 cDelim[0] = (char) 30;
1141 string sDelim = cDelim[0].ToString();
1142 CommandString = CommandString.Replace("^", sDelim);
1143 sCommand = sCommand + CommandString + "^" + EventName;
1144
1145 DataTable dt = new DataTable();
1146 RPMSDataTableDelegate rdtd = new RPMSDataTableDelegate(RPMSDataTable);
1147 if (this.IsHandleCreated == false)
1148 {
1149 this.CreateHandle();
1150 }
1151
1152 dt = (DataTable) this.Invoke(rdtd, new object[] {sCommand, "Que"});
1153
1154 DataRow dr = dt.Rows[0];
1155 int nErrorID = Convert.ToInt32(dr["ERRORID"]);
1156 int nParam = Convert.ToInt32(dr["PARAM"]);
1157
1158 if (nErrorID == 0)
1159 {
1160 return 0;
1161 }
1162 else
1163 {
1164 return nParam;
1165 }
1166 }
1167 catch (Exception ex)
1168 {
1169 Debug.Write("RPMSDataTableAsyncQue error: " + ex.Message + "\n");
1170 throw ex;
1171 }
1172 }
1173
1174 public DataTable RPMSDataTableAsyncGet(string AsyncInfo, string TableName)
1175 {
1176 return RPMSDataTableAsyncGet(AsyncInfo, TableName, null);
1177 }
1178
1179 public DataTable RPMSDataTableAsyncGet(string AsyncInfo, string TableName, DataSet dsDataSet)
1180 {
1181 try
1182 {
1183 string sCommand = "BMX ASYNC GET^" + AsyncInfo;
1184
1185 DataTable dt;
1186 RPMSDataTableDelegate rdtd = new RPMSDataTableDelegate(RPMSDataTable);
1187 RPMSDataTableDelegate2 rdtd2 = new RPMSDataTableDelegate2(RPMSDataTable);
1188 if (this.IsHandleCreated == false)
1189 {
1190 this.CreateHandle();
1191 }
1192
1193 if (dsDataSet == null)
1194 {
1195 dt = (DataTable) this.Invoke(rdtd, new object[] {sCommand, TableName});
1196 }
1197 else
1198 {
1199 dt = (DataTable) this.Invoke(rdtd2, new object[] {sCommand, TableName, dsDataSet});
1200 }
1201 return dt;
1202 }
1203 catch (Exception ex)
1204 {
1205 Debug.Write("RPMSDataTableAsyncGet error: " + ex.Message + "\n");
1206 throw ex;
1207 }
1208 }
1209
1210 #endregion Methods
1211
1212 }
1213}
Note: See TracBrowser for help on using the repository browser.