source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/BMXNetRpcSingleThreadedSession.cs@ 1146

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

Initial Import of BMX4

File size: 10.0 KB
Line 
1using System;
2using System.Data;
3using System.Windows.Forms;
4
5namespace IndianHealthService.BMXNet
6{
7
8 /// <summary>
9 /// Extends BMX functionality for easier development and debugging.
10 /// Copied from Component Framework project IndianHealthService.Xo.Framework.Rpms
11 /// </summary>
12 public class BMXNetRpcSingleThreadedSession:BMXNetSession
13 {
14
15 private BMXNetLib m_brokerLibrary;
16 private BMXNetDataAdapter m_cachedDataAdapter;
17
18 private String _debugLastRpcSignature;
19
20 public String DebugLastRpcSignature
21 {
22 get { return _debugLastRpcSignature; }
23 set { _debugLastRpcSignature = value; }
24 }
25
26 private String _debugLastResult;
27
28 public String DebugLastResult
29 {
30 get { return _debugLastResult; }
31 set { _debugLastResult = value; }
32 }
33
34
35 private String _rpcResult;
36
37
38 public BMXNetRpcSingleThreadedSession()
39 {
40 this.m_cachedDataAdapter= new BMXNetDataAdapter();
41 }
42
43 public void AuthenticatedBroker(BMXNetLib aConnectedLibrary)
44 {
45 this.m_brokerLibrary=aConnectedLibrary;
46 }
47
48 public void AuthenticatedConnectionInfo(BMXNetLib aNetLib)
49 {
50 this.m_brokerLibrary = aNetLib;
51 }
52
53 public virtual String TransmitRPC(String rpcCommand,String rpcParameter,String context){
54
55 if (rpcParameter.Length > 32600)
56 {
57 throw new Exception("RPC parameter length exceeds maximum allowable size.");
58 }
59
60 this.DebugLastRpcSignature=rpcCommand+"^"+rpcParameter+"^"+context;
61 this.m_brokerLibrary.AppContext=context;
62
63 this.RpcResult=this.m_brokerLibrary.TransmitRPC(rpcCommand,rpcParameter);
64
65 this.DebugLastResult=this.RpcResult;
66
67
68 return this.RpcResult;
69 }
70
71 public String SafelyTransmitRPC(String rpcCommand,String rpcParameter,String context)
72 {
73 try
74 {
75 return this.TransmitRPC(rpcCommand,rpcParameter,context);
76 }
77 catch (Exception exception)
78 {
79 return "Exception: "+exception.Message;
80 }
81 }
82
83 //TODO: Fix Broker - remove this stuff
84 private void FixUpBrokerBMXIENissue(DataTable aDataTable)
85 {
86 if (aDataTable.Columns.IndexOf("NEW ROW")==-1)
87 return;
88
89 foreach (DataRow each in aDataTable.Rows)
90 {
91 each["NEW ROW"]=each["BMXIEN"];
92 }
93 }
94
95
96 public bool SaveChanges(DataTable aDataTable)
97 {
98 BMXNetConnection bmxConnection;
99 BMXNetCommand bmxSelectCommand;
100 BMXNetCommand bmxUpdateCommand;
101 BMXNetDataAdapter bmxDataAdaptor;
102
103 DataTable changes=aDataTable.GetChanges();
104 if (changes==null)
105 return false;
106
107 this.FixUpBrokerBMXIENissue(changes);
108
109 this.m_brokerLibrary.AppContext="BMXRPC";
110 bmxConnection= new BMXNetConnection(this.m_brokerLibrary);
111
112 bmxConnection.Open();
113 if (bmxConnection.State != ConnectionState.Open)
114 throw new BMXNetException("Unable to connect to RPMS.");
115
116 try
117 {
118 bmxDataAdaptor=this.m_cachedDataAdapter;
119
120 bmxSelectCommand=bmxConnection.CreateCommand() as BMXNetCommand;
121 bmxSelectCommand.CommandText=aDataTable.ExtendedProperties["BMXNetSelectStatementForUpdate"] as String;
122 bmxDataAdaptor.SelectCommand=bmxSelectCommand;
123
124 DataTable schema=bmxDataAdaptor.FillSchema(aDataTable,SchemaType.Source);
125 bmxUpdateCommand = bmxConnection.CreateCommand() as BMXNetCommand;
126 bmxUpdateCommand.BMXBuildUpdateCommand(schema);
127 bmxDataAdaptor.UpdateCommand=bmxUpdateCommand;
128
129 bmxDataAdaptor.Update(changes);
130 aDataTable.AcceptChanges();
131
132 return true;
133 }
134 catch (Exception exception)
135 {
136 throw new BMXNetException("Unable to save data to RPMS.",exception);
137 }
138 finally
139 {
140 if (bmxConnection != null)
141 bmxConnection.ToString();
142 }
143
144 }
145
146
147 public virtual DataTable TransmitTableGenerationRPC(String generationString)
148 {
149 return this.TransmitTableGenerationRPC(generationString,new DataSet());
150 }
151
152
153 public virtual DataTable TransmitTableGenerationRPC(String generationString, DataSet aDataSet)
154 {
155 BMXNetConnection bmxConnection;
156 BMXNetCommand bmxCommand;
157 BMXNetDataAdapter bmxDataAdaptor;
158
159 this.m_brokerLibrary.AppContext="BMXRPC";
160 bmxConnection= new BMXNetConnection(this.m_brokerLibrary);
161
162 bmxConnection.Open();
163 if (bmxConnection.State != ConnectionState.Open)
164 throw new BMXNetException("Unable to connect to RPMS.");
165
166 try
167 {
168 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
169 bmxCommand.CommandText = generationString;
170 bmxDataAdaptor = new BMXNetDataAdapter();
171 bmxDataAdaptor=this.m_cachedDataAdapter;
172 bmxDataAdaptor.SelectCommand=bmxCommand;
173
174 DataSet dataSet = aDataSet;
175 bmxDataAdaptor.Fill(dataSet);
176 DataTable answer=dataSet.Tables[0];
177
178 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"]=bmxCommand.CommandText;
179 answer.ExtendedProperties["RpmsQuery"]=generationString;
180 answer.ExtendedProperties["RpmsSchema"]=this.RpmsSchema(generationString);
181
182 return answer;
183 }
184 catch (Exception exception)
185 {
186 throw new Exception("Unable to retrive data from RPMS.",exception);
187 }
188 finally
189 {
190 if (bmxConnection != null)
191 bmxConnection.ToString();
192 }
193
194 }
195
196
197
198 public virtual DataTable TransmitTableGenerationRPC(String generationString, DataSet aDataSet, String aTableName)
199 {
200 BMXNetConnection bmxConnection;
201 BMXNetCommand bmxCommand;
202 BMXNetDataAdapter bmxDataAdaptor;
203
204 this.m_brokerLibrary.AppContext="BMXRPC";
205 bmxConnection= new BMXNetConnection(this.m_brokerLibrary);
206
207 bmxConnection.Open();
208 if (bmxConnection.State != ConnectionState.Open)
209 {
210 throw new BMXNetException("Unable to connect to RPMS.");
211 }
212
213 try
214 {
215 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
216 bmxCommand.CommandText = generationString;
217 bmxDataAdaptor = new BMXNetDataAdapter();
218 bmxDataAdaptor=this.m_cachedDataAdapter;
219 bmxDataAdaptor.SelectCommand=bmxCommand;
220
221 DataSet dataSet = aDataSet;
222 DataTable answer=aDataSet.Tables.Add(aTableName);
223 bmxDataAdaptor.Fill(answer);
224
225 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"]=bmxCommand.CommandText;
226 answer.ExtendedProperties["RpmsQuery"]=generationString;
227 answer.ExtendedProperties["RpmsSchema"]=this.RpmsSchema(generationString);
228
229 return answer;
230 }
231 catch (Exception exception)
232 {
233 throw new Exception("Unable to retrive data from RPMS.", exception);
234 }
235 finally
236 {
237 if (bmxConnection != null)
238 bmxConnection.ToString();
239 }
240 }
241
242
243
244
245 private String RpmsSchema(String aString)
246 {
247 char[] carrotDelimiter="^".ToCharArray();
248 String[] parts=aString.Split(carrotDelimiter);
249 return parts[1];
250 }
251
252 public DataTable TableFromRPC(String rpcCommand,String rpcParameter,String context)
253 {
254 return this.TableFromRPC(rpcCommand,rpcParameter,context,new DataSet());
255 }
256
257
258
259 public DataTable TableFromRPC(String rpcCommand,String rpcParameter,String context,DataSet aDataSet)
260 {
261 String tableGenerationRpc = this.TransmitRPC(rpcCommand, rpcParameter, context);
262
263 if (IsTableGenerationRpc(tableGenerationRpc))
264 return this.TransmitTableGenerationRPC(tableGenerationRpc, aDataSet);
265 else
266 {
267 return null;
268 }
269 }
270
271
272/* BAD
273 public DataTable TableFromRPC(String rpcCommand,String rpcParameter,String context,DataSet aDataSet, String aTableName)
274 {
275 String tableGenerationRpc=this.TransmitRPC(rpcCommand,rpcParameter,context);
276
277 if (IsTableGenerationRpc(tableGenerationRpc))
278 return this.TransmitTableGenerationRPC(tableGenerationRpc, aDataSet, aTableName);
279 else
280 {
281 return null;
282 }
283 }
284*/
285 public String RpcResult
286 {
287 get { return this._rpcResult; }
288 set { this._rpcResult=value; }
289 }
290
291
292
293 public virtual DataTable TableFromSQL(String sql)
294 {
295 BMXNetConnection bmxConnection;
296 BMXNetCommand bmxCommand;
297 BMXNetDataAdapter bmxDataAdaptor;
298
299 this.m_brokerLibrary.AppContext="BSDXRPC";
300 bmxConnection= new BMXNetConnection(this.m_brokerLibrary);
301
302 bmxConnection.Open();
303 if (bmxConnection.State != ConnectionState.Open)
304 {
305 throw new BMXNetException("Unable to connect to RPMS.");
306 }
307
308 try
309 {
310 bmxCommand = bmxConnection.CreateCommand() as BMXNetCommand;
311 bmxCommand.CommandText = sql;
312 bmxDataAdaptor = new BMXNetDataAdapter();
313 bmxDataAdaptor=this.m_cachedDataAdapter;
314 bmxDataAdaptor.SelectCommand=bmxCommand;
315
316 DataSet dataSet = new DataSet();
317 bmxDataAdaptor.Fill(dataSet);
318
319 DataTable answer=dataSet.Tables[0];
320
321 answer.ExtendedProperties["BMXNetSelectStatementForUpdate"]=bmxCommand.CommandText;
322 answer.ExtendedProperties["SqlQuery"]=sql;
323
324 return answer;
325 }
326 catch (Exception exception)
327 {
328 throw new Exception("Unable to retrive sql data from RPMS.", exception);
329 }
330 finally
331 {
332 if (bmxConnection != null)
333 bmxConnection.ToString();
334 //TODO: bmxConnection.Close();
335 //BMX framework lifecylce needs to be better defined.
336 }
337
338 }
339
340
341 public bool IsTableGenerationRpc(String aString)
342 {
343 String validFetchTransactionPrefix="BMX ADO SS";
344
345 return aString.StartsWith(validFetchTransactionPrefix);
346 }
347
348
349 public virtual void Close()
350 {
351 this.m_brokerLibrary.CloseConnection();
352 }
353
354
355
356 public string HostAddress
357 {
358 get { throw new NotImplementedException(); }
359 }
360
361 public string DUZ
362 {
363 get { return this.m_brokerLibrary.DUZ; }
364 }
365
366
367 public string UserName
368 {
369 get { return this.m_brokerLibrary.UserName; }
370 }
371
372 }
373}
Note: See TracBrowser for help on using the repository browser.