source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/BMX41000/IHS BMX Framework/IndianHealthService.BMXNet.Test/RpcSession.cs@ 1764

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

Initial Import of BMX4

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