source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Ado/BMXNetAdapter.cs@ 1180

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

Initial Import of BMX4

File size: 13.7 KB
Line 
1using System;
2using System.Data;
3using System.Data.Common;
4using System.Text;
5
6namespace IndianHealthService.BMXNet.Ado
7{
8 /// <summary>
9 /// BMXNET implementaiton of DbDataAdaptor. This class can be used like
10 /// any ADO.NET DbDataAdaptor implementation or the suggested RemoteSession API's can
11 /// be used.
12 ///
13 /// See ADO.NET documentation for details of this class.
14 /// </summary>
15 public class BMXNetDataAdapter : DbDataAdapter, IDbDataAdapter
16 {
17 private BMXNetCommand m_selectCommand;
18 private BMXNetCommand m_insertCommand;
19 private BMXNetCommand m_updateCommand;
20 private BMXNetCommand m_deleteCommand;
21
22 /*
23 * Inherit from Component through DbDataAdapter. The event
24 * mechanism is designed to work with the Component.Events
25 * property. These variables are the keys used to find the
26 * events in the components list of events.
27 */
28 static private readonly object EventRowUpdated = new object();
29 static private readonly object EventRowUpdating = new object();
30
31 public BMXNetDataAdapter()
32 {
33 }
34
35 new public BMXNetCommand SelectCommand
36 {
37 get { return m_selectCommand; }
38 set { m_selectCommand = value; }
39 }
40
41 IDbCommand IDbDataAdapter.SelectCommand
42 {
43 get { return m_selectCommand; }
44 set { m_selectCommand = (BMXNetCommand)value; }
45 }
46
47 new public BMXNetCommand InsertCommand
48 {
49 get { return m_insertCommand; }
50 set { m_insertCommand = value; }
51 }
52
53 IDbCommand IDbDataAdapter.InsertCommand
54 {
55 get { return m_insertCommand; }
56 set { m_insertCommand = (BMXNetCommand)value; }
57 }
58
59 new public BMXNetCommand UpdateCommand
60 {
61 get { return m_updateCommand; }
62 set { m_updateCommand = value; }
63 }
64
65 IDbCommand IDbDataAdapter.UpdateCommand
66 {
67 get { return m_updateCommand; }
68 set { m_updateCommand = (BMXNetCommand)value; }
69 }
70
71 new public BMXNetCommand DeleteCommand
72 {
73 get { return m_deleteCommand; }
74 set { m_deleteCommand = value; }
75 }
76
77 IDbCommand IDbDataAdapter.DeleteCommand
78 {
79 get { return m_deleteCommand; }
80 set { m_deleteCommand = (BMXNetCommand)value; }
81 }
82
83 /*
84 * Implement abstract methods inherited from DbDataAdapter.
85 */
86
87 public override int Fill(DataSet ds)
88 {
89 //The inital call to base.fill calls the RPC which loads up the array
90 //After base.fill returns, create a datareader
91 BMXNetConnection bmxConn = (BMXNetConnection) this.SelectCommand.Connection;
92 RPMSDb bmxDB = bmxConn.RPMSDb;
93
94
95 DataTable dt = ds.Tables.Count == 0 ? new DataTable() : ds.Tables[0];
96
97 //Execute the RPC call
98 base.Fill(dt);
99 //Get the table name
100 this.ApplyMetaData(bmxDB.ResultSets[0].metaData, dt);
101 dt.TableName = dt.TableName == null ? bmxDB.ResultSets[0].fmFileID : dt.TableName;
102 dt.ExtendedProperties["fmSeed"] = bmxDB.ResultSets[0].fmSeed;
103 dt.ExtendedProperties["BMXParentPath"]=bmxDB.ResultSets[0].fmParentPath;
104 //dt.ExtendedProperties.Add("fmSeed", bmxDB.ResultSets[0].fmSeed);
105 //dt.ExtendedProperties.Add("BMXParentPath", bmxDB.ResultSets[0].fmParentPath);
106
107
108 string sParentTable = dt.TableName;
109
110 //Add the first table to the DataSet
111 if (!ds.Tables.Contains(dt.TableName))
112 {
113 ds.Tables.Add(dt);
114 }
115
116 //If bmxDB resultset array count is greater than 1
117 int nSets = bmxDB.ResultSets.GetUpperBound(0) + 1;
118
119 if (nSets > 1)
120 {
121 //Set primary key for first table
122 string sKeyField = bmxDB.ResultSets[0].fmKeyField;
123 DataColumn dcKey = dt.Columns[sKeyField];
124 DataColumn[] dcKeys = new DataColumn[1];
125 dcKeys[0] = dcKey;
126 dt.PrimaryKey = dcKeys;
127
128 string[] sRelations = new string[nSets];
129
130 //loop and get the rest of the tables
131 for (int k = 1; k < nSets; k++)
132 {
133 //Increment the current recordset counter in bmxDB
134 bmxDB.CurrentRecordSet++;
135 //Create the next table
136 dt = new DataTable();
137 //Fill it
138 base.Fill(dt);
139 //Get the table name
140 string sChildTable = bmxDB.ResultSets[k].fmFileID;
141 dt.TableName = sChildTable;
142 //Add it to the dataset
143 ds.Tables.Add(dt);
144
145 //Get the foreign key field
146 string sForeignKey = bmxDB.ResultSets[k].fmForeignKey;
147
148 //Set the data relationship
149 string sParentKey;
150 sParentKey = "BMXIEN";
151 sParentKey = "PATIENT_IEN";
152
153 DataRelation dr = new DataRelation("Relation" + k.ToString() , //Relation Name
154 ds.Tables[sParentTable].Columns[sParentKey], //; //parent
155 ds.Tables[sChildTable].Columns[sForeignKey]) ;//child
156 ds.Relations.Add(dr);
157 }
158 bmxDB.CurrentRecordSet = 0;
159 }
160 return dt.Rows.Count;
161 }
162
163 private void ApplyMetaData(RPMSDb.RPMSDbResultSet.MetaData[] metaData, DataTable aTable)
164 {
165 foreach (RPMSDb.RPMSDbResultSet.MetaData each in metaData)
166 {
167 aTable.Columns[each.name].ReadOnly=each.fmReadOnly;
168 }
169 }
170
171 override protected DataTable FillSchema(
172 DataTable dataTable,
173 SchemaType schemaType,
174 IDbCommand command,
175 CommandBehavior behavior
176 )
177 {
178 behavior = CommandBehavior.SchemaOnly;
179 BMXNetDataReader dReader =(BMXNetDataReader) command.ExecuteReader(behavior);
180 DataTable dtSchema = dReader.GetSchemaTable();
181 return dtSchema;
182 }
183
184 override protected int Update(
185 DataRow[] dataRows,
186 DataTableMapping tableMapping
187 )
188 {
189 //Build UpdateCommand's m_sCmdText using UpdateCommand's parameters
190 // and data in dataRows;
191 // execute non query and increment nRet;
192
193 string sCmd = "";
194 int nRecordsAffected = 0;
195
196 //Get recordset-level info from parameters
197 BMXNetParameter parm = (BMXNetParameter)UpdateCommand.BmxNetParameters[0];
198 string sFileID = parm.SourceColumn;
199 parm = (BMXNetParameter) UpdateCommand.BmxNetParameters[1];
200 string sKeyField = parm.SourceColumn;
201 String parentPath= dataRows[0].Table.ExtendedProperties.ContainsKey("BMXParentPath") ? dataRows[0].Table.ExtendedProperties["BMXParentPath"].ToString() :"";
202 String[] parentPathPeices = parentPath.Split(new char[] { ',' });
203 string[] saKeyFields = sKeyField.Split(new char[] { ',' });
204 string sKeyID = null;
205 char[] cRecDelim = new char[1];
206 cRecDelim[0] = (char) 30;
207
208 string sValue = "";
209 string sFMFieldID;
210 string sColumnName;
211 int nColIndex;
212
213 //Process deletions
214 foreach (DataRow r in dataRows)
215 {
216 if (r.RowState == DataRowState.Deleted)
217 {
218 r.RejectChanges(); //so that I can get to the row id
219 //Build DAS
220 string sTmp = "";
221 for (int j = 0; j < saKeyFields.GetLength(0); j++)
222 {
223 if (saKeyFields[j] != "")
224 {
225 if (j > 0)
226 sTmp = sTmp + ",";
227 if (j == saKeyFields.GetLength(0) - 1)
228 sTmp += "-";
229 sTmp += r[saKeyFields[j]];
230 }
231 }
232 sCmd = sTmp;
233 sCmd = sFileID + "^" + sCmd + "^";
234 UpdateCommand.CommandText = "UPDATE " + sCmd;
235 int nRet = this.UpdateCommand.ExecuteNonQuery();
236 r.Delete();
237 nRecordsAffected += nRet;
238 }
239 }
240
241 //Process Edits and Adds
242 foreach (DataRow r in dataRows)
243 {
244 sKeyID = null;
245 if (r.RowState != DataRowState.Deleted)
246 {
247 string sMsg = "";
248 for (int j=2; j < UpdateCommand.BmxNetParameters.Count; j++)
249 {
250
251 parm = (BMXNetParameter)UpdateCommand.BmxNetParameters[j];
252 sColumnName = parm.ParameterName;
253 sFMFieldID = parm.SourceColumn;
254 //Find a column id in r whose column name is sColumnName
255 nColIndex = -1;
256 for (int k = 0; k < r.Table.Columns.Count; k ++)
257 {
258 if (r.Table.Columns[k].ColumnName == sColumnName)
259 {
260 nColIndex = k;
261 break;
262 }
263 }
264 if (nColIndex > -1)
265 {
266 if (r.ItemArray[nColIndex].GetType() == typeof(System.DateTime))
267 {
268 DateTime dValue = (DateTime) r.ItemArray[nColIndex];
269 if (dValue.Second != 0)
270 {
271 throw new BMXNetException("Seconds are not allowed in a Date");
272 }
273 if ((dValue.Minute == 0) && (dValue.Hour == 0))
274 {
275 sValue = dValue.ToString("M-d-yyyy");
276 }
277 else
278 {
279 sValue = dValue.ToString("M-d-yyyy@HH:mm");
280 }
281 }
282 else
283 {
284 sValue = r.ItemArray[nColIndex].ToString();
285 }
286 if (parm.IsKey == false)
287 {
288 if (sMsg != "")
289 sMsg += (char) 30;
290 sMsg += sFMFieldID + "|" + sValue;
291 }
292 }
293
294
295 if (sKeyID == null)
296 {
297 StringBuilder keyBuilder = new StringBuilder();
298 bool needsDelimiter = false;
299
300 for (int i = 0; i < saKeyFields.Length; i++)
301 {
302 String currentKey = saKeyFields[i];
303 if (currentKey != "")
304 {
305 String currentValue = r[currentKey].ToString().Trim();
306 if (currentValue.Length == 0)
307 {
308 if (parentPathPeices.Length >= (i + 1))
309 {
310 currentValue = parentPathPeices[i];
311 }
312 }
313
314 if (needsDelimiter)
315 keyBuilder.Append(',');
316
317 keyBuilder.Append(currentValue);
318 needsDelimiter = true;
319 }
320 }
321 sKeyID = keyBuilder.ToString();
322 }
323
324
325 /*
326 switch (sFMFieldID)
327 {
328 case ".0001":
329 if (sKeyID == "")
330 {
331 sKeyID = sValue + ",";
332 }
333 else
334 {
335 sKeyID = sValue + "," + sKeyID;
336 }
337 break;
338 case ".001":
339 if (sKeyID == "")
340 {
341 sKeyID = sValue;
342 }
343 else
344 {
345 sKeyID = sKeyID + sValue;
346 }
347 break;
348 default:
349 break;
350
351 }
352 **/
353
354 }
355 sCmd = sFileID + "^" + sKeyID + "^" + sMsg;
356 UpdateCommand.CommandText = "UPDATE " + sCmd;
357 int nRet = this.UpdateCommand.ExecuteNonQuery();
358 nRecordsAffected += nRet;
359 }//end if RowState != deleted
360 }//end for
361
362 return nRecordsAffected;
363 }
364
365 override protected RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
366 {
367 return new BMXNetRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
368 }
369
370 override protected RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
371 {
372 return new BMXNetRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
373 }
374
375 override protected void OnRowUpdating(RowUpdatingEventArgs value)
376 {
377 BMXNetRowUpdatingEventHandler handler = (BMXNetRowUpdatingEventHandler) Events[EventRowUpdating];
378 if ((null != handler) && (value is BMXNetRowUpdatingEventArgs))
379 {
380 handler(this, (BMXNetRowUpdatingEventArgs) value);
381 }
382 }
383
384 override protected void OnRowUpdated(RowUpdatedEventArgs value)
385 {
386 BMXNetRowUpdatedEventHandler handler = (BMXNetRowUpdatedEventHandler) Events[EventRowUpdated];
387 if ((null != handler) && (value is BMXNetRowUpdatedEventArgs))
388 {
389 handler(this, (BMXNetRowUpdatedEventArgs) value);
390 }
391 }
392
393 internal event BMXNetRowUpdatingEventHandler RowUpdating
394 {
395 add { Events.AddHandler(EventRowUpdating, value); }
396 remove { Events.RemoveHandler(EventRowUpdating, value); }
397 }
398
399 internal event BMXNetRowUpdatedEventHandler RowUpdated
400 {
401 add { Events.AddHandler(EventRowUpdated, value); }
402 remove { Events.RemoveHandler(EventRowUpdated, value); }
403 }
404 }
405
406 internal delegate void BMXNetRowUpdatingEventHandler(object sender, BMXNetRowUpdatingEventArgs e);
407 internal delegate void BMXNetRowUpdatedEventHandler(object sender, BMXNetRowUpdatedEventArgs e);
408
409 internal class BMXNetRowUpdatingEventArgs : RowUpdatingEventArgs
410 {
411 public BMXNetRowUpdatingEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
412 : base(row, command, statementType, tableMapping)
413 {
414 }
415
416 // Hide the inherited implementation of the command property.
417 new public BMXNetCommand Command
418 {
419 get { return (BMXNetCommand)base.Command; }
420 set { base.Command = value; }
421 }
422 }
423
424 internal class BMXNetRowUpdatedEventArgs : RowUpdatedEventArgs
425 {
426 public BMXNetRowUpdatedEventArgs(DataRow row, IDbCommand command, StatementType statementType, DataTableMapping tableMapping)
427 : base(row, command, statementType, tableMapping)
428 {
429 }
430
431 // Hide the inherited implementation of the command property.
432 new public BMXNetCommand Command
433 {
434 get { return (BMXNetCommand)base.Command; }
435 }
436 }
437}
Note: See TracBrowser for help on using the repository browser.