source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetRemoteSession.cs@ 1177

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

Initial Import of BMX4

File size: 36.2 KB
Line 
1using System;
2using System.Data;
3using System.Windows.Forms;
4using IndianHealthService.BMXNet.Model;
5using IndianHealthService.BMXNet.Ado;
6using System.Collections.Generic;
7using System.Threading;
8
9namespace IndianHealthService.BMXNet.Services
10{
11 /// <summary>
12 /// Extends BMX functionality for easier development and debugging.
13 /// Copied from Component Framework project IndianHealthService.Xo.Framework.Rpms
14 /// </summary>
15 internal class BMXNetRemoteSession : RemoteSession, RemoteEventService
16 {
17 public int ReceiveTimeout
18 {
19 get { return this.SessionConnection.ReceiveTimeout; }
20 set { this.SessionConnection.ReceiveTimeout = value; }
21 }
22
23 public int SendTimeout
24 {
25 get { return this.SessionConnection.SendTimeout; }
26 set { this.SessionConnection.SendTimeout = value; }
27 }
28
29 private Log _log = new NullLog();
30
31 public Log Log
32 {
33 get { return _log; }
34 set { _log = value; }
35 }
36
37 private object _rpcLock = new object();
38
39 public object RpcLock
40 {
41 get { return _rpcLock; }
42 set { _rpcLock = value; }
43 }
44
45 private Guid _id = Guid.NewGuid();
46
47 public Guid Id
48 {
49 get { return _id; }
50 set { _id = value; }
51 }
52
53 private System.Timers.Timer _pollingTimer = null;
54
55 protected System.Timers.Timer PollingTimer
56 {
57 get { return _pollingTimer; }
58 set { _pollingTimer = value; }
59 }
60
61 private int _eventPollingInterval = 5000;
62
63 public int EventPollingInterval
64 {
65 get {
66 return _eventPollingInterval;
67 }
68 set
69 {
70 _eventPollingInterval = value;
71 if (this.IsEventPollingEnabled)
72 {
73 this.PollingTimer.Interval = value;
74 }
75 }
76 }
77
78 public bool IsEventPollingEnabled
79 {
80 get
81 {
82 return this.PollingTimer != null;
83 }
84 set
85 {
86 if (value)
87 {
88 this.StartPollingTimer();
89 }
90 else
91 {
92 this.StopPollingTimer();
93 }
94 }
95 }
96
97 private void AssertPrimarySession()
98 {
99 if (!this.IsPrimary)
100 {
101 throw new NotSupportedException("This behavior is not supported on non-primary sessions. See documentation.");
102 }
103 }
104
105
106 private void StartPollingTimer()
107 {
108 if (this.PollingTimer == null)
109 {
110 this.PollingTimer = new System.Timers.Timer(this.EventPollingInterval);
111 this.PollingTimer.Enabled = true;
112 this.PollingTimer.Elapsed += new System.Timers.ElapsedEventHandler(PollingTimer_Elapsed);
113 this.PollingTimer.Start();
114 }
115 }
116
117 private bool _inPollForEvent = false;
118
119 protected bool InPollForEvent
120 {
121 get { return _inPollForEvent; }
122 set { _inPollForEvent = value; }
123 }
124
125
126 void PollingTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
127 {
128 if (!this.InPollForEvent)
129 {
130 try
131 {
132 this.InPollForEvent = true;
133 this.PollForEvent();
134 }
135 finally
136 {
137 this.InPollForEvent = false;
138 }
139 }
140 }
141
142 private void StopPollingTimer()
143 {
144 if (this.PollingTimer != null)
145 {
146 this.PollingTimer.Enabled = false;
147 this.PollingTimer.Stop();
148 this.PollingTimer.Elapsed -= new System.Timers.ElapsedEventHandler(PollingTimer_Elapsed);
149 this.PollingTimer = null;
150 }
151 }
152
153 public String Job
154 {
155 get { return this.SessionConnection == null ? "" : this.SessionConnection.Job; }
156 }
157
158 public override string ToString()
159 {
160 return "RemoteSession [Process " + this.Job + (this.IsPrimary ? ", Primary Session]" : "]");
161 }
162 private bool _isPrimary = false;
163
164 public bool IsPrimary
165 {
166 get { return _isPrimary; }
167 set { _isPrimary = value; }
168 }
169
170 private bool _isConnected = false;
171
172 public bool IsConnected
173 {
174 get
175 {
176 return (_isConnected || this.IsPrimary) && this.SessionConnection.IsConnected;
177 }
178 set { _isConnected = value; }
179 }
180
181 public User User
182 {
183 get { return this.Broker.User; }
184 }
185
186 public BMXNetBroker Broker
187 {
188 get { return this.SessionConnection.Broker; }
189
190 }
191 private BMXNetSessionConnection _sessionConnection;
192
193 internal BMXNetSessionConnection SessionConnection
194 {
195 get { return _sessionConnection; }
196 set { _sessionConnection = value; }
197 }
198
199 private BMXNetDataAdapter m_cachedDataAdapter;
200
201 private String _debugLastRpcSignature;
202
203 public String DebugLastRpcSignature
204 {
205 get { return _debugLastRpcSignature; }
206 set { _debugLastRpcSignature = value; }
207 }
208
209 private String _debugLastRpcResult;
210
211 public String DebugLastRpcResult
212 {
213 get { return _debugLastRpcResult; }
214 set { _debugLastRpcResult = value; }
215 }
216
217 private DataTable _debugLastTableResult;
218
219 public DataTable DebugLastTableResult
220 {
221 get { return _debugLastTableResult; }
222 set { _debugLastTableResult = value; }
223 }
224
225 private String _rpcResult;
226
227 public BMXNetRemoteSession()
228 {
229 this.m_cachedDataAdapter = new BMXNetDataAdapter();
230 }
231
232 internal void Begin(BMXNetSessionConnection aConnection, BMXRemoteSessionPool aPool)
233 {
234 this.SessionConnection = aConnection;
235 this.SessionPool = aPool;
236 this.IsConnected = true;
237 }
238
239 private void PollForTimerEvent()
240 {
241 if (this.TimerEvent != null)
242 {
243 try
244 {
245 this.TimerEvent(this, new EventArgs());
246 }
247 catch (Exception problemToIgnore)
248 {
249 this.Log.Log("BMXNetRemoteSession", "Exception", problemToIgnore.Message);
250 }
251 }
252 }
253
254 public RemoteEventService EventServices
255 {
256 get { return this; }
257 }
258
259 private Control _invokedControl = null;
260
261 public Control InvokedControl
262 {
263 get { return _invokedControl; }
264 set { _invokedControl = value; }
265 }
266
267
268 private DataTable _debugLastTableChanges = null;
269
270 public DataTable DebugLastTableChanges
271 {
272 get { return _debugLastTableChanges; }
273 set { _debugLastTableChanges = value; }
274 }
275
276 public event EventHandler<RemoteEventArgs> RpmsEvent;
277 public event EventHandler TimerEvent;
278
279 internal void AuthenticatedConnectionInfo(BMXNetSessionConnection aSessionConnection)
280 {
281 this.SessionConnection = aSessionConnection;
282 }
283
284 public virtual String TransmitRPC(String rpcCommand, String rpcParameter, String aContext)
285 {
286 this.DebugLastRpcSignature = rpcCommand + "^" + rpcParameter + "^" + aContext;
287 this.DebugLastRpcResult = null;
288
289 lock (this.RpcLock)
290 {
291 this.AppContext = aContext;
292 this.RpcResult = this.SessionConnection.TransmitRPC(rpcCommand, rpcParameter);
293 this.DebugLastRpcResult = this.RpcResult;
294 }
295
296 return this.RpcResult;
297 }
298
299 private void AssertConnected()
300 {
301 if (!this.IsConnected)
302 {
303 throw new BMXNetException("Session is not connected or has been closed.");
304 }
305 }
306
307 public String SafelyTransmitRPC(String rpcCommand, String rpcParameter, String context)
308 {
309 this.AssertConnected();
310
311 try
312 {
313 return this.TransmitRPC(rpcCommand, rpcParameter, context);
314 }
315 catch (Exception exception)
316 {
317 return "Exception: " + exception.Message;
318 }
319 }
320
321
322
323 public bool SaveChanges(DataTable aDataTable)
324 {
325 this.AssertConnected();
326
327 DataTable changes = aDataTable.GetChanges();
328
329 this.DebugLastTableChanges = changes;
330
331 if (changes == null)
332 return false;
333
334 BMXNetConnection bmxConnection = null;
335
336 lock (this.RpcLock)
337 {
338 try
339 {
340 bmxConnection = new BMXNetConnection(this.SessionConnection);
341 bmxConnection.Open();
342
343 if (bmxConnection.State == ConnectionState.Open)
344 {
345 BMXNetDataAdapter bmxDataAdaptor = this.m_cachedDataAdapter;
346
347 BMXNetCommand bmxSelectCommand = bmxConnection.CreateCommand() as BMXNetCommand;
348 bmxSelectCommand.CommandText = aDataTable.ExtendedProperties["BMXNetSelectStatementForUpdate"] as String;
349 bmxDataAdaptor.SelectCommand = bmxSelectCommand;
350
351 DataTable schema = bmxDataAdaptor.FillSchema(aDataTable, SchemaType.Source);
352 BMXNetCommand bmxUpdateCommand = bmxConnection.CreateCommand() as BMXNetCommand;
353 bmxUpdateCommand.BMXBuildUpdateCommand(schema);
354 bmxDataAdaptor.UpdateCommand = bmxUpdateCommand;
355
356 bmxDataAdaptor.Update(changes);
357 aDataTable.AcceptChanges();
358
359 return true;
360 }
361 else
362 {
363 throw new BMXNetException("Unable to connect to RPMS.");
364 }
365 }
366
367 catch (Exception exception)
368 {
369 throw new BMXNetException("Unable to save data to RPMS.", exception);
370 }
371 finally
372 {
373 if (bmxConnection != null)
374 {
375 bmxConnection.Close();
376 }
377 }
378 }
379 }
380
381
382 public virtual DataTable TableFromCommand(String aCommand)
383 {
384 return this.TableFromCommand(aCommand, this.AppContext);
385 }
386
387 public DataTable TableFromCommand(string aCommand, string aContext)
388 {
389 return this.TableFromCommand(aCommand, null, null,aContext);
390 }
391
392
393 public DataTable TableFromCommand(String aCommand, DataSet aDataSet, string aTableName)
394 {
395 return this.TableFromCommand(aCommand, aDataSet, aTableName, this.AppContext);
396 }
397
398 public virtual DataTable TableFromCommand(String aCommand, DataSet aDataSet, string aTableName, string aContext)
399 {
400 this.Log.Log("BMX NET", "Debug", "TableFromCommand >> " + aCommand, aTableName == null ? "Null Table" : aTableName, aContext == null ? "Null Context" : aContext);
401 this.DebugLastTableResult = null;
402
403 this.AssertConnected();
404
405 DataSet dataSet = aDataSet == null ? new DataSet() : aDataSet;
406
407 BMXNetConnection bmxConnection = new BMXNetConnection(this.SessionConnection);
408 bmxConnection.Open();
409
410 if (bmxConnection.State != ConnectionState.Open)
411 throw new BMXNetException("Unable to connect to RPMS.");
412
413 lock (this.RpcLock)
414 {
415 try
416 {
417 this.AppContext = aContext;
418 BMXNetCommand bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
419 bmxCommand.CommandText = aCommand;
420 BMXNetDataAdapter bmxDataAdaptor = new BMXNetDataAdapter();
421 bmxDataAdaptor.SelectCommand = bmxCommand;
422
423
424 DataTable answer = null;
425 String validTableName = aTableName == null ? this.DefaultTableName : aTableName;
426 DateTime stopWatch = DateTime.Now;
427
428 if (aDataSet == null)
429 {
430 answer = new DataTable(validTableName);
431 bmxDataAdaptor.Fill(answer);
432 }
433 else
434 {
435 DataTable dataTable = aDataSet.Tables.Contains(validTableName) ? aDataSet.Tables[validTableName] : aDataSet.Tables.Add(validTableName);
436 bmxDataAdaptor.Fill(aDataSet, validTableName);
437 answer = aDataSet.Tables[validTableName];
438 }
439
440 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"] = bmxCommand.CommandText;
441 answer.ExtendedProperties["BMXQueryStart"] = stopWatch;
442 answer.ExtendedProperties["BMXQueryTime"] = (DateTime.Now - stopWatch).TotalMilliseconds;
443 answer.ExtendedProperties["RpmsQuery"] = aCommand;
444 answer.ExtendedProperties["RpmsSchema"] = this.RpmsSchema(aCommand);
445
446 this.DebugLastTableResult = answer;
447
448 return answer;
449 }
450
451 catch (Exception exception)
452 {
453 throw new BMXNetException("Unable to retrive data from RPMS.", exception);
454 }
455 finally
456 {
457 if (bmxConnection != null)
458 {
459 bmxConnection.Close();
460 }
461 }
462 }
463 }
464
465 private String RpmsSchema(String aString)
466 {
467 char[] carrotDelimiter = "^".ToCharArray();
468 String[] parts = aString.Split(carrotDelimiter);
469 return (parts.Length > 2) ? parts[1] : "";
470 }
471
472 public DataTable TableFromRPC(String rpcCommand, String rpcParameter)
473 {
474 return this.TableFromRPC(rpcCommand, rpcParameter, this.AppContext);
475 }
476
477 public DataTable TableFromRPC(string rpcCommand, string rpcParameter, string aContext)
478 {
479 return this.TableFromRPC(rpcCommand, rpcParameter, new DataSet(), this.DefaultTableName, aContext);
480
481 }
482
483 public DataTable TableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet, string aTableName)
484 {
485 return this.TableFromRPC(rpcCommand, rpcParameter, aDataSet, aTableName, this.AppContext);
486 }
487
488 public DataTable TableFromRPC(String rpcCommand, String rpcParameter, DataSet aDataSet, String aTableName, String aContext)
489 {
490 String generatedCommand = this.TransmitRPC(rpcCommand, rpcParameter, aContext);
491
492 return this.IsBmxAdoCommand(generatedCommand) ? this.TableFromCommand(generatedCommand, aDataSet, aTableName, aContext) : null;
493 }
494
495 public String RpcResult
496 {
497 get { return this._rpcResult; }
498 set { this._rpcResult = value; }
499 }
500
501 public virtual DataTable TableFromSQL(String sql)
502 {
503 return this.TableFromSQL(sql, this.AppContext);
504 }
505
506 public DataTable TableFromSQL(string sql, string aContext)
507 {
508 return this.TableFromSQL(sql, new DataSet(), this.DefaultTableName,aContext);
509 }
510
511 public DataTable TableFromSQL(string sql, DataSet aDataSet, string aTableName)
512 {
513 return this.TableFromSQL(sql, new DataSet(), this.DefaultTableName, this.AppContext);
514 }
515
516 public DataTable TableFromSQL(string sql, DataSet aDataSet, string aTableName, String aContext)
517 {
518 this.Log.Log("BMX NET", "Debug", "TableFromSQL>> " + sql, aTableName == null ? "Null Table" : aTableName, aContext == null ? "Null Context" : aContext);
519 this.DebugLastTableResult = null;
520
521 this.AssertConnected();
522
523 BMXNetConnection bmxConnection;
524 BMXNetCommand bmxCommand;
525 BMXNetDataAdapter bmxDataAdaptor;
526
527 bmxConnection = new BMXNetConnection(this.SessionConnection);
528 bmxConnection.Open();
529 if (bmxConnection.State != ConnectionState.Open)
530 {
531 throw new BMXNetException("Unable to connect to RPMS.");
532 }
533
534 lock (this.RpcLock)
535 {
536 try
537 {
538 this.AppContext = aContext;
539
540 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
541 bmxCommand.CommandText = sql;
542 bmxDataAdaptor = new BMXNetDataAdapter();
543 bmxDataAdaptor = this.m_cachedDataAdapter;
544 bmxDataAdaptor.SelectCommand = bmxCommand;
545
546 DataTable answer = null;
547 String validTableName = aTableName == null ? this.DefaultTableName : aTableName;
548 DateTime stopWatch = DateTime.Now;
549
550 if (aDataSet == null)
551 {
552 answer = new DataTable(validTableName);
553 bmxDataAdaptor.Fill(answer);
554 }
555 else
556 {
557 DataTable dataTable = aDataSet.Tables.Contains(validTableName) ? aDataSet.Tables[validTableName] : aDataSet.Tables.Add(validTableName);
558 bmxDataAdaptor.Fill(aDataSet, validTableName);
559 answer = aDataSet.Tables[validTableName];
560 }
561
562
563 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"] = bmxCommand.CommandText;
564 answer.ExtendedProperties["SqlQuery"] = sql;
565 answer.ExtendedProperties["BMXQueryStart"] = stopWatch;
566 answer.ExtendedProperties["BMXQueryTime"] = (DateTime.Now - stopWatch).TotalMilliseconds;
567
568 this.DebugLastTableResult = answer;
569
570 return answer;
571 }
572
573 catch (Exception exception)
574 {
575 throw new Exception("Unable to retrive sql data from RPMS.", exception);
576 }
577 finally
578 {
579 if (bmxConnection != null)
580 bmxConnection.ToString();
581 }
582 }
583 }
584
585 public bool IsBmxAdoCommand(String aString)
586 {
587 String validFetchTransactionPrefix = "BMX ADO SS";
588
589 return aString.StartsWith(validFetchTransactionPrefix);
590 }
591
592 private BMXRemoteSessionPool _sessionPool = null;
593
594 internal BMXRemoteSessionPool SessionPool
595 {
596 get { return _sessionPool; }
597 set { _sessionPool = value; }
598 }
599
600 private bool _hasSubscribedForAsyncEvents = false;
601
602 internal bool HasSubscribedForAsyncEvents
603 {
604 get { return _hasSubscribedForAsyncEvents; }
605 set { _hasSubscribedForAsyncEvents = value; }
606 }
607
608 public virtual void Close()
609 {
610 if (this.HasSubscribedForAsyncEvents)
611 {
612 this.EventServices.Unsubscribe(this.AsyncFinishedEventName);
613 }
614 this.StopPollingTimer();
615 this.SessionPool.CloseSession(this);
616 }
617
618 public string HostAddress
619 {
620 get { throw new NotImplementedException(); }
621 }
622
623 public string AuthenicatedDuz
624 {
625 get { return this.SessionConnection.DUZ; }
626 }
627
628
629 public string AuthenicatedUserName
630 {
631 get { return this.SessionConnection.UserName; }
632 }
633
634 public string AppContext
635 {
636 get
637 {
638 return this.SessionConnection.AppContext;
639 }
640 set
641 {
642 this.SessionConnection.AppContext = value;
643 }
644 }
645
646 public string TransmitRPC(string rpcCommand, string rpcParameter)
647 {
648 return this.TransmitRPC(rpcCommand, rpcParameter, this.AppContext);
649 }
650
651 public string SafelyTransmitRPC(string rpcCommand, string rpcParameter)
652 {
653 return this.SafelyTransmitRPC(rpcCommand, rpcParameter, this.AppContext);
654 }
655
656 public System.Data.DataTable TableFromRPC(string rpcCommand, string rpcParameter, System.Data.DataSet aDataSet)
657 {
658 return this.TableFromRPC(rpcCommand, rpcParameter, aDataSet);
659 }
660
661 public int Subscribe(string anEventName)
662 {
663 int result=this.EventManagmentCall("BMX EVENT REGISTER^" + anEventName);
664 if (result == 0)
665 {
666 this.Log.Log("BMX NET", "Events", "Subscribed to " + anEventName);
667 }
668 else
669 {
670 this.Log.Log("BMX NET", "Events", "Unable to subscribe to " + anEventName);
671 }
672 return result;
673 }
674
675 public int Unsubscribe(string anEventName)
676 {
677 int result=this.EventManagmentCall("BMX EVENT UNREGISTER^" + anEventName);
678 if (result == 0)
679 {
680 this.Log.Log("BMX NET", "Events", "Unsubscribed from " + anEventName);
681 }
682 else
683 {
684 this.Log.Log("BMX NET", "Events", "Unable to unsubscribe from " + anEventName);
685 }
686 return result;
687 }
688
689 public int TriggerEvent(string anEventName, string aParameter, bool doRaiseBack)
690 {
691 this.Log.Log("BMX NET", "Remote Events", "Triggered " + anEventName);
692 return this.EventManagmentCall("BMX EVENT RAISE^" + anEventName + "^" + aParameter + "^" + doRaiseBack.ToString().ToUpper() + "^");
693 }
694
695 protected int EventManagmentCall(String aCommand)
696 {
697 try
698 {
699 DataTable table = this.TableFromCommand(aCommand);
700 return (int)table.Rows[0]["ERRORID"];
701 }
702 catch
703 {
704 return 99;
705 }
706 }
707
708 delegate void UiCallPollForEvent();
709
710 protected void PollForEvent()
711 {
712 if (this.InvokedControl != null && this.InvokedControl.InvokeRequired)
713 {
714 this.InvokedControl.Invoke(new UiCallPollForEvent(SafePollForEvent));
715 }
716 else
717 {
718 this.SafePollForEvent();
719 }
720 }
721
722 protected void SafePollForEvent()
723 {
724 this.Log.Log("BMX NET", "Events", "Polling for event (" + this.PollingTimer.Interval.ToString() + ")");
725
726 this.PollForTimerEvent();
727
728 if (this.RpmsEvent != null || this.AsyncFutureLookup.Count > 0)
729 {
730 if (Monitor.TryEnter(this.RpcLock))
731 {
732 Monitor.Exit(this.RpcLock);
733 }
734 else
735 {
736 return;
737 }
738 DataTable events = this.TableFromCommand("BMX EVENT POLL");
739
740 foreach (DataRow row in events.Rows)
741 {
742 try
743 {
744 RemoteEventArgs args = new RemoteEventArgs();
745 args.EventType = row["EVENT"].ToString();
746 args.Details = row["PARAM"].ToString();
747 this.Log.Log("BMX NET", "Remote Events", "Raised: " + args.EventType + "(" + args.Details + ")");
748 if (args.EventType.Equals(this.AsyncFinishedEventName))
749 {
750 this.AsyncTableRequestReturned(args);
751 }
752 else
753 {
754 this.TriggerRpmsEvent(args);
755 }
756 }
757 catch
758 {
759 }
760 }
761 }
762 }
763
764
765 delegate void UiCallTriggerRpmsEvent(RemoteEventArgs args);
766
767 protected void TriggerRpmsEvent(RemoteEventArgs args)
768 {
769 if (this.RpmsEvent != null)
770 {
771 if (this.InvokedControl != null && this.InvokedControl.InvokeRequired)
772 {
773 this.InvokedControl.Invoke(new UiCallTriggerRpmsEvent(SafeTriggerRpmsEvent), args);
774 }
775 else
776 {
777 this.SafeTriggerRpmsEvent(args);
778 }
779 }
780 }
781
782 private void SafeTriggerRpmsEvent(RemoteEventArgs args)
783 {
784 this.RpmsEvent.Invoke(this, args);
785 }
786
787 private void AsyncTableRequestReturned(RemoteEventArgs args)
788 {
789 int key = 0;
790 if (int.TryParse(M.Piece(args.Details,"~",1), out key) && this.AsyncFutureLookup.ContainsKey(key))
791 {
792 DataTableFuture future = this.AsyncFutureLookup[key];
793 this.RemoveFromFutures(future);
794 future.FutureHasReturned(this, M.Piece(args.Details, "~", 2));
795 }
796 }
797
798 private double _timerEventPollingMultiple = 1;
799
800 public double TimerEventPollingMultiple
801 {
802 get { return _timerEventPollingMultiple; }
803 set { _timerEventPollingMultiple = value; }
804 }
805
806 public DataTable VersionInfo
807 {
808 get { return this.TableFromCommand("BMX VERSION INFO^EBCI"); }//"+this.Broker.NameSpace); }
809 }
810
811 internal void Resume(BMXRemoteSessionPool bMXRemoteSessionPool)
812 {
813 this._isConnected = true;
814 }
815
816 internal void Suspend(BMXRemoteSessionPool bMXRemoteSessionPool)
817 {
818 this._isConnected = false;
819 if (this.EventSourcePrimarySession != null)
820 {
821 this.EventSourcePrimarySession.Unsubscribe(this.AsyncFinishedEventName);
822 this.EventSourcePrimarySession.RpmsEvent -= new EventHandler<RemoteEventArgs>(aSession_RpmsEvent);
823 }
824 this.IsEventPollingEnabled = false;
825 }
826
827
828
829 #region RemoteSession Members
830
831 private String _defaultTableName = "BmxResultTable";
832
833 public String DefaultTableName
834 {
835 get { return _defaultTableName; }
836 set { _defaultTableName = value; }
837 }
838
839
840 public DataTableFuture AsyncTableFromCommand(string aCommand)
841 {
842 return this.AsyncTableFromCommand(aCommand, this.AppContext);
843 }
844
845 public DataTableFuture AsyncTableFromCommand(string aCommand, string aContext)
846 {
847 return this.AsyncTableFromCommand(aCommand, new DataSet(), this.DefaultTableName, aContext);
848 }
849
850 public DataTableFuture AsyncTableFromCommand(string aCommand, DataSet aDataSet, string aTableName)
851 {
852 return this.AsyncTableFromCommand(aCommand, aDataSet, aTableName, this.AppContext);
853 }
854
855 private String _asyncFinishedEventName=null;
856
857 public String AsyncFinishedEventName
858 {
859 get
860 {
861 if (_asyncFinishedEventName == null)
862 {
863 _asyncFinishedEventName = "BMX ASYNC FINISHED " + this.Id.ToString();
864 }
865 return _asyncFinishedEventName;
866 }
867
868 set { _asyncFinishedEventName = value; }
869 }
870
871 private Dictionary<int, DataTableFuture> _asyncFutureLookup = new Dictionary<int, DataTableFuture>();
872
873 protected Dictionary<int, DataTableFuture> AsyncFutureLookup
874 {
875 get { return _asyncFutureLookup; }
876 set { _asyncFutureLookup = value; }
877 }
878
879 public DataTableFuture AsyncTableFromCommand(string aCommand, DataSet aDataSet, string aTableName, string aContext)
880 {
881 int errorCode = -1;
882
883 try
884 {
885 if (!this.HasSubscribedForAsyncEvents)
886 {
887 this.HasSubscribedForAsyncEvents = 0 == this.EventServices.Subscribe(this.AsyncFinishedEventName);
888 }
889
890 //replace ^'s in CommandString with $c(30)'s
891 DataTable infoTable = this.TableFromCommand("BMX ASYNC QUEUE^"+aCommand.Replace('^', (char)30) + "^" + this.AsyncFinishedEventName,aContext);
892
893 if (infoTable.Rows.Count == 1)
894 {
895 DataRow queueingInfo = infoTable.Rows[0];
896 errorCode = int.Parse(queueingInfo["ERRORID"].ToString());
897 if (errorCode == 1)
898 {
899 int resultId = int.Parse(queueingInfo["PARAM"].ToString());
900
901 DataTableFuture answer = new DataTableFuture();
902 answer.InvokedControl = this.InvokedControl;
903 answer.ResultDataSet = aDataSet;
904 answer.ResultTableName = aTableName;
905 answer.ResultAppContext = aContext;
906 answer.ResultId = resultId;
907 this.AddToFutures(answer);
908 return answer;
909 }
910 else
911 {
912 throw new BMXNetException("Async call failed: " + queueingInfo["PARAM"].ToString());
913 }
914
915 }
916 else
917 {
918 throw new BMXNetException("Error code not available.");
919 }
920
921 }
922 catch (Exception problem)
923 {
924 String codeString = (errorCode == -1) ? "" : "Error code " + errorCode.ToString();
925 throw new BMXNetException("BMX Unable to places async request on queue. " + codeString,problem);
926 }
927 }
928
929 private void AddToFutures(DataTableFuture aFuture)
930 {
931 if (this.AsyncFutureLookup.ContainsKey(aFuture.ResultId))
932 {
933 DataTableFuture toCancel = this.AsyncFutureLookup[aFuture.ResultId];
934 toCancel.Cancel();
935 }
936
937 lock (this.AsyncFutureLookup)
938 {
939 aFuture.Session = this;
940 this.AsyncFutureLookup[aFuture.ResultId] = aFuture;
941 }
942
943 }
944
945 internal void RemoveFromFutures(DataTableFuture aFuture)
946 {
947 if (this.AsyncFutureLookup.ContainsKey(aFuture.ResultId))
948 {
949 lock (this.AsyncFutureLookup)
950 {
951 this.AsyncFutureLookup.Remove(aFuture.ResultId);
952 }
953 }
954 }
955
956
957 public DataTableFuture AsyncTableFromSQL(string sql)
958 {
959 return this.AsyncTableFromSQL(sql, this.AppContext);
960 }
961
962 public DataTableFuture AsyncTableFromSQL(string sql, string aContext)
963 {
964 return this.AsyncTableFromSQL(sql, new DataSet(),this.DefaultTableName, this.AppContext);
965 }
966
967 public DataTableFuture AsyncTableFromSQL(string sql, DataSet aDataSet, string aTableName)
968 {
969 return this.AsyncTableFromSQL(sql, aDataSet, aTableName, this.AppContext);
970 }
971
972
973 public DataTableFuture AsyncTableFromSQL(string sql, DataSet aDataSet, string aTableName, string aContext)
974 {
975 int errorCode = -1;
976
977 try
978 {
979 if (!this.HasSubscribedForAsyncEvents)
980 {
981 this.HasSubscribedForAsyncEvents = 0== this.EventServices.Subscribe(this.AsyncFinishedEventName);
982 }
983
984 //replace ^'s in CommandString with $c(30)'s
985 DataTable infoTable = this.TableFromCommand("BMX ASYNC QUEUE^" + sql.Replace('^', (char)30) + "^" + this.AsyncFinishedEventName, aContext);
986
987 DataRow queueingInfo = infoTable.Rows[0];
988 errorCode = int.Parse(queueingInfo["ERRORID"].ToString());
989 if (errorCode == 1)
990 {
991 int resultId = int.Parse(queueingInfo["PARAM"].ToString());
992
993 DataTableFuture answer = new DataTableFuture();
994 answer.ResultDataSet = aDataSet;
995 answer.ResultTableName = aTableName;
996 answer.ResultAppContext = aContext;
997 answer.ResultId = resultId;
998 this.AddToFutures(answer);
999 return answer;
1000 }
1001 else
1002 {
1003 throw new BMXNetException("Unexcepted error code");
1004 }
1005
1006
1007 }
1008 catch (Exception problem)
1009 {
1010 String codeString = (errorCode == -1) ? "" : "Error code " + errorCode.ToString();
1011 throw new BMXNetException("BMX Unable to places async request on queue. " + codeString, problem);
1012 }
1013
1014 }
1015
1016
1017 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter)
1018 {
1019 return this.AsyncTableFromRPC(rpcCommand, rpcParameter, this.AppContext);
1020 }
1021
1022
1023 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, string aContext)
1024 {
1025 return this.AsyncTableFromRPC(rpcParameter, rpcParameter, new DataSet(), this.DefaultTableName, aContext);
1026 }
1027
1028
1029 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet, string aTableName)
1030 {
1031 return this.AsyncTableFromRPC(rpcCommand, rpcParameter, aDataSet, aTableName, this.AppContext);
1032 }
1033
1034 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet, string aTableName, string aContext)
1035 {
1036 String generatedCommand = this.TransmitRPC(rpcCommand, rpcParameter, aContext);
1037
1038 return this.IsBmxAdoCommand(generatedCommand) ? this.AsyncTableFromCommand(generatedCommand, aDataSet, aTableName, aContext) : null;
1039
1040 }
1041
1042 #endregion
1043
1044 private BMXNetRemoteSession _eventSourcePrimarySession = null;
1045
1046 public BMXNetRemoteSession EventSourcePrimarySession
1047 {
1048 get { return _eventSourcePrimarySession; }
1049 set { _eventSourcePrimarySession = value; }
1050 }
1051
1052
1053 internal void PrimarySession(BMXNetRemoteSession aSession)
1054 {
1055 this.EventSourcePrimarySession = aSession;
1056 // this.EventSourcePrimarySession.RpmsEvent += new EventHandler<RemoteEventArgs>(aSession_RpmsEvent);
1057 // this.EventSourcePrimarySession.Subscribe(this.AsyncFinishedEventName);
1058 }
1059
1060 void aSession_RpmsEvent(object sender, RemoteEventArgs e)
1061 {
1062 this.Log.Log("BMX NET", "Remote Events", "Raised: " + e.EventType + "(" + e.Details + ")");
1063 if (e.EventType.Equals(this.AsyncFinishedEventName))
1064 {
1065 this.AsyncTableRequestReturned(e);
1066 }
1067 else if (this.RpmsEvent != null)
1068 {
1069 this.RpmsEvent(this, e);
1070 }
1071 }
1072 }
1073}
Note: See TracBrowser for help on using the repository browser.