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

Last change on this file since 923 was 923, checked in by Sam Habiel, 14 years ago

Changes in handling of Divisions. Now prompts if more than one division assigned to user.

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