source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetRpcSingleThreadedSession.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: 19.3 KB
Line 
1using System;
2using System.Data;
3using System.Windows.Forms;
4using IndianHealthService.BMXNet.Model;
5using IndianHealthService.BMXNet.Ado;
6
7namespace IndianHealthService.BMXNet.Services
8{
9
10 /// <summary>
11 /// Extends BMX functionality for easier development and debugging.
12 /// Copied from Component Framework project IndianHealthService.Xo.Framework.Rpms
13 /// </summary>
14 public class BMXNetRpcSingleThreadedSession : RemoteSession, RemoteEventService
15 {
16
17 private BMXNetBroker m_broker;
18
19 public BMXNetBroker Broker
20 {
21 get { return m_broker; }
22 set { m_broker = value; }
23 }
24 private BMXNetDataAdapter m_cachedDataAdapter;
25
26 private String _debugLastRpcSignature;
27
28 public String DebugLastRpcSignature
29 {
30 get { return _debugLastRpcSignature; }
31 set { _debugLastRpcSignature = value; }
32 }
33
34 private String _debugLastResult;
35
36 public String DebugLastResult
37 {
38 get { return _debugLastResult; }
39 set { _debugLastResult = value; }
40 }
41
42
43 private String _rpcResult;
44
45
46 public BMXNetRpcSingleThreadedSession()
47 {
48 this.m_cachedDataAdapter = new BMXNetDataAdapter();
49 }
50
51 public void AuthenticatedBroker(BMXNetBroker aBroker)
52 {
53 this.Broker = aBroker;
54 this.StartPollingTimer();
55 }
56
57 private void StartPollingTimer()
58 {
59 this.PollingTimer = new System.Timers.Timer(this.EventPollingInterval);
60 this.PollingTimer.Enabled = this.IsEventPollingEnabled;
61 this.PollingTimer.Start();
62 }
63
64 public void AuthenticatedConnectionInfo(BMXNetBroker aBroker)
65 {
66 this.Broker = aBroker;
67 this.StartPollingTimer();
68 }
69
70 public virtual String TransmitRPC(String rpcCommand, String rpcParameter, String context)
71 {
72
73 if (rpcParameter.Length > 32600)
74 {
75 throw new Exception("RPC parameter length exceeds maximum allowable size.");
76 }
77
78 this.DebugLastRpcSignature = rpcCommand + "^" + rpcParameter + "^" + context;
79 this.Broker.AppContext = context;
80
81 this.RpcResult = this.Broker.TransmitRPC(rpcCommand, rpcParameter);
82
83 this.DebugLastResult = this.RpcResult;
84
85
86 return this.RpcResult;
87 }
88
89 public String SafelyTransmitRPC(String rpcCommand, String rpcParameter, String context)
90 {
91 try
92 {
93 return this.TransmitRPC(rpcCommand, rpcParameter, context);
94 }
95 catch (Exception exception)
96 {
97 return "Exception: " + exception.Message;
98 }
99 }
100
101 //TODO: Fix Broker - remove this stuff
102 private void FixUpBrokerBMXIENissue(DataTable aDataTable)
103 {
104 if (aDataTable.Columns.IndexOf("NEW ROW") == -1)
105 return;
106
107 foreach (DataRow each in aDataTable.Rows)
108 {
109 each["NEW ROW"] = each["BMXIEN"];
110 }
111 }
112
113
114 public bool SaveChanges(DataTable aDataTable)
115 {
116 BMXNetConnection bmxConnection;
117 BMXNetCommand bmxSelectCommand;
118 BMXNetCommand bmxUpdateCommand;
119 BMXNetDataAdapter bmxDataAdaptor;
120
121 DataTable changes = aDataTable.GetChanges();
122 if (changes == null)
123 return false;
124
125 this.FixUpBrokerBMXIENissue(changes);
126
127 this.Broker.AppContext = "BMXRPC";
128
129 bmxConnection = new BMXNetConnection(this.Broker);
130
131 bmxConnection.Open();
132 if (bmxConnection.State != ConnectionState.Open)
133 throw new BMXNetException("Unable to connect to RPMS.");
134
135 try
136 {
137 bmxDataAdaptor = this.m_cachedDataAdapter;
138
139 bmxSelectCommand = bmxConnection.CreateCommand() as BMXNetCommand;
140 bmxSelectCommand.CommandText = aDataTable.ExtendedProperties["BMXNetSelectStatementForUpdate"] as String;
141 bmxDataAdaptor.SelectCommand = bmxSelectCommand;
142
143 DataTable schema = bmxDataAdaptor.FillSchema(aDataTable, SchemaType.Source);
144 bmxUpdateCommand = bmxConnection.CreateCommand() as BMXNetCommand;
145 bmxUpdateCommand.BMXBuildUpdateCommand(schema);
146 bmxDataAdaptor.UpdateCommand = bmxUpdateCommand;
147
148 bmxDataAdaptor.Update(changes);
149 aDataTable.AcceptChanges();
150
151 return true;
152 }
153 catch (Exception exception)
154 {
155 throw new BMXNetException("Unable to save data to RPMS.", exception);
156 }
157 finally
158 {
159 if (bmxConnection != null)
160 bmxConnection.ToString();
161 }
162
163 }
164
165
166 public virtual DataTable TransmitTableGenerationRPC(String generationString)
167 {
168 return this.TransmitTableGenerationRPC(generationString, new DataSet());
169 }
170
171
172 public virtual DataTable TransmitTableGenerationRPC(String generationString, DataSet aDataSet)
173 {
174 BMXNetConnection bmxConnection;
175 BMXNetCommand bmxCommand;
176 BMXNetDataAdapter bmxDataAdaptor;
177
178 this.Broker.AppContext = "BMXRPC";
179 bmxConnection = new BMXNetConnection(this.Broker);
180
181 bmxConnection.Open();
182 if (bmxConnection.State != ConnectionState.Open)
183 throw new BMXNetException("Unable to connect to RPMS.");
184
185 try
186 {
187 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
188 bmxCommand.CommandText = generationString;
189 bmxDataAdaptor = new BMXNetDataAdapter();
190 bmxDataAdaptor.SelectCommand = bmxCommand;
191
192 DataSet dataSet = aDataSet;
193 bmxDataAdaptor.Fill(dataSet);
194 DataTable answer = dataSet.Tables[0];
195
196 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"] = bmxCommand.CommandText;
197 answer.ExtendedProperties["RpmsQuery"] = generationString;
198 answer.ExtendedProperties["RpmsSchema"] = this.RpmsSchema(generationString);
199
200 return answer;
201 }
202 catch (Exception exception)
203 {
204 throw new Exception("Unable to retrive data from RPMS.", exception);
205 }
206 finally
207 {
208 if (bmxConnection != null)
209 bmxConnection.ToString();
210 }
211
212 }
213
214
215
216 public virtual DataTable TransmitTableGenerationRPC(String generationString, DataSet aDataSet, String aTableName)
217 {
218 BMXNetConnection bmxConnection;
219 BMXNetCommand bmxCommand;
220 BMXNetDataAdapter bmxDataAdaptor;
221
222 String initialAppContext = this.Broker.AppContext;
223
224 if (generationString.StartsWith("BMX"))
225 {
226 this.Broker.AppContext = "BMXRPC";
227 }
228
229 bmxConnection = new BMXNetConnection(this.Broker);
230
231 bmxConnection.Open();
232 if (bmxConnection.State != ConnectionState.Open)
233 {
234 throw new BMXNetException("Unable to connect to RPMS.");
235 }
236
237 try
238 {
239 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
240 bmxCommand.CommandText = generationString;
241 bmxDataAdaptor = new BMXNetDataAdapter();
242 bmxDataAdaptor = this.m_cachedDataAdapter;
243 bmxDataAdaptor.SelectCommand = bmxCommand;
244
245 DataSet dataSet = aDataSet;
246 DataTable answer = aDataSet.Tables.Add(aTableName);
247 bmxDataAdaptor.Fill(answer);
248
249 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"] = bmxCommand.CommandText;
250 answer.ExtendedProperties["RpmsQuery"] = generationString;
251 answer.ExtendedProperties["RpmsSchema"] = this.RpmsSchema(generationString);
252
253 return answer;
254 }
255 catch (Exception exception)
256 {
257 throw new Exception("Unable to retrive data from RPMS.", exception);
258 }
259 finally
260 {
261 if (bmxConnection != null)
262 bmxConnection.ToString();
263
264 this.Broker.AppContext = initialAppContext;
265 }
266 }
267
268
269
270
271 private String RpmsSchema(String aString)
272 {
273 char[] carrotDelimiter = "^".ToCharArray();
274 String[] parts = aString.Split(carrotDelimiter);
275 return parts[1];
276 }
277
278 public DataTable TableFromRPC(String rpcCommand, String rpcParameter, String context)
279 {
280 return this.TableFromRPC(rpcCommand, rpcParameter, context, new DataSet());
281 }
282
283
284
285 public DataTable TableFromRPC(String rpcCommand, String rpcParameter, String context, DataSet aDataSet)
286 {
287 String tableGenerationRpc = this.TransmitRPC(rpcCommand, rpcParameter, context);
288
289 if (IsTableGenerationRpc(tableGenerationRpc))
290 return this.TransmitTableGenerationRPC(tableGenerationRpc, aDataSet);
291 else
292 {
293 return null;
294 }
295 }
296
297
298 /* BAD
299 public DataTable TableFromRPC(String rpcCommand,String rpcParameter,String context,DataSet aDataSet, String aTableName)
300 {
301 String tableGenerationRpc=this.TransmitRPC(rpcCommand,rpcParameter,context);
302
303 if (IsTableGenerationRpc(tableGenerationRpc))
304 return this.TransmitTableGenerationRPC(tableGenerationRpc, aDataSet, aTableName);
305 else
306 {
307 return null;
308 }
309 }
310 */
311 public String RpcResult
312 {
313 get { return this._rpcResult; }
314 set { this._rpcResult = value; }
315 }
316
317 public virtual DataTable TableFromSQL(String sql)
318 {
319 return this.TableFromSQL(sql, new DataSet());
320 }
321
322
323 public DataTable TableFromSQL(string sql, DataSet aDataSet)
324 {
325 return this.TableFromSQL(sql, aDataSet, "ResultTable");
326 }
327
328 public DataTable TableFromSQL(string sql, DataSet aDataSet, string aTableName)
329 {
330 BMXNetConnection bmxConnection;
331 BMXNetCommand bmxCommand;
332 BMXNetDataAdapter bmxDataAdaptor;
333
334 this.Broker.AppContext = "BSDXRPC";
335 bmxConnection = new BMXNetConnection(this.Broker);
336
337 bmxConnection.Open();
338 if (bmxConnection.State != ConnectionState.Open)
339 {
340 throw new BMXNetException("Unable to connect to RPMS.");
341 }
342
343 try
344 {
345 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
346 bmxCommand.CommandText = sql;
347 bmxDataAdaptor = new BMXNetDataAdapter();
348 bmxDataAdaptor = this.m_cachedDataAdapter;
349 bmxDataAdaptor.SelectCommand = bmxCommand;
350
351 bmxDataAdaptor.Fill(aDataSet,aTableName);
352
353 DataTable answer = aDataSet.Tables[aTableName];
354
355 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"] = bmxCommand.CommandText;
356 answer.ExtendedProperties["SqlQuery"] = sql;
357
358 return answer;
359 }
360 catch (Exception exception)
361 {
362 throw new Exception("Unable to retrive sql data from RPMS.", exception);
363 }
364 finally
365 {
366 if (bmxConnection != null)
367 bmxConnection.ToString();
368 //TODO: bmxConnection.Close();
369 //BMX framework lifecylce needs to be better defined.
370 }
371
372 }
373
374
375 public bool IsTableGenerationRpc(String aString)
376 {
377 String validFetchTransactionPrefix = "BMX ADO SS";
378
379 return aString.StartsWith(validFetchTransactionPrefix);
380 }
381
382
383 public virtual void Close()
384 {
385 this.Broker.CloseConnection();
386 this.PollingTimer.Close();
387 }
388
389
390
391 public string HostAddress
392 {
393 get { throw new NotImplementedException(); }
394 }
395
396 public string DUZ
397 {
398 get { return this.Broker.DUZ; }
399 }
400
401
402 public string UserName
403 {
404 get { return this.Broker.UserName; }
405 }
406
407 public string AppContext
408 {
409 get
410 {
411 return this.Broker.AppContext;
412 }
413 set
414 {
415 this.Broker.AppContext = value;
416 }
417 }
418
419 public string TransmitRPC(string rpcCommand, string rpcParameter)
420 {
421 return this.TransmitRPC(rpcCommand, rpcParameter, this.AppContext);
422 }
423
424 public string SafelyTransmitRPC(string rpcCommand, string rpcParameter)
425 {
426 return this.SafelyTransmitRPC(rpcCommand, rpcParameter, this.AppContext);
427 }
428
429 public System.Data.DataTable TableFromRPC(string rpcCommand, string rpcParameter)
430 {
431 return this.TableFromRPC(rpcCommand, rpcParameter, this.AppContext);
432 }
433
434 public System.Data.DataTable TableFromRPC(string rpcCommand, string rpcParameter, System.Data.DataSet aDataSet)
435 {
436 return this.TableFromRPC(rpcCommand, rpcParameter, aDataSet);
437 }
438
439
440
441 #region BMXNetSession Members
442
443
444 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter)
445 {
446 throw new NotImplementedException();
447 }
448
449 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet)
450 {
451 throw new NotImplementedException();
452 }
453
454 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, string context)
455 {
456 throw new NotImplementedException();
457 }
458
459 public DataTableFuture AsyncTableFromRPC(string rpcCommand, string rpcParameter, string context, DataSet aDataSet)
460 {
461 throw new NotImplementedException();
462 }
463
464 public event EventHandler<RemoteEventArgs> RemoteEvent;
465
466 #endregion
467
468
469
470 DataTableFuture RemoteSession.AsyncTableFromRPC(string rpcCommand, string rpcParameter)
471 {
472 throw new NotImplementedException();
473 }
474
475 DataTableFuture RemoteSession.AsyncTableFromRPC(string rpcCommand, string rpcParameter, DataSet aDataSet)
476 {
477 throw new NotImplementedException();
478 }
479
480 DataTableFuture RemoteSession.AsyncTableFromRPC(string rpcCommand, string rpcParameter, string context)
481 {
482 throw new NotImplementedException();
483 }
484
485 DataTableFuture RemoteSession.AsyncTableFromRPC(string rpcCommand, string rpcParameter, string context, DataSet aDataSet)
486 {
487 throw new NotImplementedException();
488 }
489
490
491
492 public bool HasSubscribers(string anEventName)
493 {
494 throw new NotImplementedException();
495 }
496
497 public int Subscribe(string anEventName)
498 {
499 return this.EventManagmentCall("BMX EVENT REGISTER^" + anEventName);
500 }
501
502 public int Unsubscribe(string anEventName)
503 {
504 return this.EventManagmentCall("BMX EVENT UNREGISTER^" + anEventName);
505 }
506
507 public int RaiseEvent(string EventName, string Param, bool RaiseBack)
508 {
509 return this.EventManagmentCall("BMX EVENT RAISE^" + EventName + "^" + Param + "^" + RaiseBack.ToString().ToUpper() + "^");
510 }
511
512
513 public int TriggerEvent(string anEventName, string someDetails)
514 {
515 throw new NotImplementedException();
516 }
517
518 protected int EventManagmentCall(String aCommand)
519 {
520 try
521 {
522 DataTable table = this.TransmitTableGenerationRPC(aCommand);
523 return (int)table.Rows[0]["ERRORID"];
524 }
525 catch (Exception ex)
526 {
527 // Debug.Write(ex.Message);
528 return 99;
529 }
530 }
531
532
533
534 // this.NativeNetLib.BMXRWL.ReleaseWriterLock();
535 // this.m_timerEvent.Enabled = true;
536 //Disable timer before and after this
537
538 protected void PollForEvent()
539 {
540 this.PollForTimerEvent();
541
542 if (this.RpmsEvent != null)
543 {
544 DataTable events = this.TransmitTableGenerationRPC("BMX EVENT POLL");
545 foreach (DataRow row in events.Rows)
546 {
547 try
548 {
549 RemoteEventArgs args = new RemoteEventArgs();
550 args.EventType = row["EVENT"].ToString();
551 args.Details = row["PARAM"].ToString();
552
553 this.RpmsEvent(this, args);
554 }
555 catch
556 {
557 }
558 }
559 }
560 }
561
562 private void PollForTimerEvent()
563 {
564 }
565
566 public RemoteEventService EventServices
567 {
568 get { return this; }
569 }
570
571 private System.Timers.Timer _pollingTimer = null;
572
573 public System.Timers.Timer PollingTimer
574 {
575 get { return _pollingTimer; }
576 set { _pollingTimer = value; }
577 }
578
579 public event EventHandler<RemoteEventArgs> RpmsEvent;
580 public event EventHandler TimerEvent;
581
582 private int _eventPollingInterval = 5000;
583
584 public int EventPollingInterval
585 {
586 get { return _eventPollingInterval; }
587 set
588 {
589 _eventPollingInterval = value;
590 if (this.PollingTimer != null)
591 {
592 this.PollingTimer.Interval = value;
593 }
594 }
595 }
596 private double _timerEventPollingMultiple = 1;
597
598 public double TimerEventPollingMultiple
599 {
600 get { return _timerEventPollingMultiple; }
601 set { _timerEventPollingMultiple = value; }
602 }
603
604 private bool _isEventPollingEnabled = false;
605
606 public bool IsEventPollingEnabled
607 {
608 get { return _isEventPollingEnabled; }
609 set
610 {
611 _isEventPollingEnabled = value;
612 if (this.PollingTimer != null)
613 {
614 this.PollingTimer.Enabled = value;
615 }
616 }
617 }
618
619
620
621 public DataTable VersionInfo
622 {
623 get { return this.TransmitTableGenerationRPC("BMX VERSION INFO^"); } //this.Broker.NameSpace); }
624 }
625
626
627
628
629 }
630}
Note: See TracBrowser for help on using the repository browser.