source: BMXNET_RPMS_dotNET_UTILITIES-BMX/trunk/cs/bmx_0200scr/BMX2/BMXNet/BMXNetAdapter.cs@ 815

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

Initial commit of C# Source Code. Now to try to get it to compile.

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