1 | using System;
|
---|
2 | using System.Data;
|
---|
3 | using System.Windows.Forms;
|
---|
4 | using System.Diagnostics;
|
---|
5 | using IndianHealthService.BMXNet;
|
---|
6 |
|
---|
7 | namespace IndianHealthService.BMXNet.Tools.SchemaBuilder
|
---|
8 | {
|
---|
9 | /// <summary>
|
---|
10 | /// Represents a BMX ADO SCHEMA entry
|
---|
11 | /// </summary>
|
---|
12 | [Serializable]
|
---|
13 | public class BMXSchemaDoc: System.Object
|
---|
14 | {
|
---|
15 | #region Fields
|
---|
16 |
|
---|
17 | private string m_sFileName;
|
---|
18 | private double m_nFileNumber;
|
---|
19 | private DataTable m_dtSchemaFields;
|
---|
20 | private DataTable m_dtSchemas;
|
---|
21 | private DataTable m_dtCurrentSchema; //the one we're editing
|
---|
22 | private double m_nSchemaIEN;
|
---|
23 | private string m_sSchemaName;
|
---|
24 | private bool m_bReadOnlyDataset = true;
|
---|
25 |
|
---|
26 | #endregion Fields
|
---|
27 |
|
---|
28 | #region Methods
|
---|
29 |
|
---|
30 | public BMXSchemaDoc()
|
---|
31 | {
|
---|
32 |
|
---|
33 | }
|
---|
34 |
|
---|
35 | private RemoteSession _remoteSession = null;
|
---|
36 |
|
---|
37 | public RemoteSession RemoteSession
|
---|
38 | {
|
---|
39 | get { return _remoteSession; }
|
---|
40 | set { _remoteSession = value; }
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | public void OpenSchema(double SchemaIEN, string SchemaName, double FileNumber)
|
---|
45 | {
|
---|
46 | try
|
---|
47 | {
|
---|
48 | m_nSchemaIEN = SchemaIEN;
|
---|
49 | m_sSchemaName = SchemaName;
|
---|
50 | m_nFileNumber = FileNumber;
|
---|
51 | string sRpc = "BMX ADO SS^FILEMAN FILEINFO^^~~~~~FNIT~BMXADOS1~" + m_nFileNumber.ToString() + "~";
|
---|
52 | DataTable dtFileName = this.RemoteSession.TableFromCommand(sRpc);
|
---|
53 | Debug.Assert(dtFileName.Rows.Count == 1);
|
---|
54 | DataRow dr = dtFileName.Rows[0];
|
---|
55 | m_sFileName = dr["NAME"].ToString();
|
---|
56 |
|
---|
57 | sRpc = "BMX ADO SS^SCHEMAS^^~" + m_nSchemaIEN.ToString() + "~" + m_nSchemaIEN.ToString();
|
---|
58 | m_dtCurrentSchema = this.RemoteSession.TableFromCommand(sRpc);
|
---|
59 | Debug.Assert(m_dtCurrentSchema.Rows.Count == 1);
|
---|
60 | dr = m_dtCurrentSchema.Rows[0];
|
---|
61 | string sReadOnly = dr["DATASET IS READ ONLY"].ToString();
|
---|
62 | m_bReadOnlyDataset = (sReadOnly == "YES")?true:false;
|
---|
63 |
|
---|
64 | sRpc = "BMX ADO SS^SCHEMA DEFINITION^";
|
---|
65 | sRpc += m_nSchemaIEN.ToString();
|
---|
66 | sRpc += ",^~~~";
|
---|
67 | m_dtSchemaFields = this.RemoteSession.TableFromCommand(sRpc);
|
---|
68 | m_dtSchemaFields.TableName="SCHEMA FIELDS";
|
---|
69 | }
|
---|
70 | catch (Exception ex)
|
---|
71 | {
|
---|
72 | MessageBox.Show(ex.Message, "BMX Schema Builder");
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void NewSchema()
|
---|
77 | {
|
---|
78 | //This code executes at app startup
|
---|
79 | m_sSchemaName = "NewSchema";
|
---|
80 | m_nSchemaIEN = 0;
|
---|
81 | m_sFileName = "";
|
---|
82 | m_nFileNumber = 0;
|
---|
83 |
|
---|
84 | m_dtSchemaFields = this.RemoteSession.TableFromCommand("BMX ADO SS^SCHEMA DEFINITION^0,^~~~");
|
---|
85 | m_dtSchemaFields.TableName="SCHEMA FIELDS";
|
---|
86 | m_dtSchemaFields.Columns.Add("BMXIEN1", typeof(System.Int16));
|
---|
87 |
|
---|
88 | m_dtSchemas = this.RemoteSession.TableFromCommand("BMX ADO SS^SCHEMAS^^B~~~");
|
---|
89 | m_dtSchemas.TableName="SCHEMAS";
|
---|
90 |
|
---|
91 | m_dtCurrentSchema = this.RemoteSession.TableFromCommand("BMX ADO SS^SCHEMAS^^~-1~");
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void SaveSchema()
|
---|
95 | {
|
---|
96 | SaveSchema(this.m_sSchemaName);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void SaveSchema(string SchemaName)
|
---|
100 | {
|
---|
101 | //If m_nSchemaIEN == 0 then save a new schema
|
---|
102 | //Otherwise, save existing schema at m_nSchemaIEN
|
---|
103 |
|
---|
104 | string sRpc;
|
---|
105 | this.m_sSchemaName = SchemaName;
|
---|
106 |
|
---|
107 | if (m_dtCurrentSchema.Rows.Count == 0)
|
---|
108 | {
|
---|
109 | //Adding a new SCHEMA entry
|
---|
110 |
|
---|
111 | Debug.Assert(m_nSchemaIEN == 0);
|
---|
112 | //Build a new row in the current schema record and
|
---|
113 | //add it to the SCHEMA file
|
---|
114 | DataRow dr = m_dtCurrentSchema.NewRow();
|
---|
115 | dr["SCHEMA"] = m_sSchemaName;
|
---|
116 | dr["DATASET IS READ ONLY"] = m_bReadOnlyDataset ?"YES":"NO";
|
---|
117 | dr["FILE OR SUBFILE NUMBER"] = this.m_nFileNumber;//.ToString("###############.##############");
|
---|
118 | m_dtCurrentSchema.Rows.Add(dr);
|
---|
119 |
|
---|
120 | //add it to the SCHEMA file
|
---|
121 | this.RemoteSession.SaveChanges(m_dtCurrentSchema);
|
---|
122 |
|
---|
123 | //Re-load the m_dtCurrentSchema table to get the IEN of the newly added SCHEMA entry
|
---|
124 | sRpc = "BMX ADO SS^SCHEMAS^^B~" + m_sSchemaName + "~" + m_sSchemaName;
|
---|
125 | m_dtCurrentSchema = this.RemoteSession.TableFromCommand(sRpc);
|
---|
126 | Debug.Assert(m_dtCurrentSchema.Rows.Count > 0);
|
---|
127 | dr = m_dtCurrentSchema.Rows[0];
|
---|
128 | m_nSchemaIEN = Convert.ToDouble(dr["BMXIEN"]);
|
---|
129 | Debug.Assert(m_nSchemaIEN > 0);
|
---|
130 |
|
---|
131 | //Insert SCHEMA ien into schema fields multiple at BMXIEN1
|
---|
132 | for (int j=0; j < m_dtSchemaFields.Rows.Count; j++)
|
---|
133 | {
|
---|
134 | dr = m_dtSchemaFields.Rows[j];
|
---|
135 | dr["BMXIEN1"] = m_nSchemaIEN;
|
---|
136 | }
|
---|
137 |
|
---|
138 | //Add the FIELDS to the SCHEMA entry
|
---|
139 | this.RemoteSession.SaveChanges(m_dtSchemaFields);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | Debug.Assert(m_nSchemaIEN > 0);
|
---|
144 | Debug.Assert(m_dtCurrentSchema.Rows.Count == 1);
|
---|
145 | //Build a new row in the current schema record and
|
---|
146 | //add it to the SCHEMA file
|
---|
147 | DataRow dr = m_dtCurrentSchema.Rows[0];
|
---|
148 | dr["SCHEMA"] = this.SchemaName;
|
---|
149 | dr["DATASET IS READ ONLY"] = this.SchemaReadOnlyDataset?"YES":"NO";
|
---|
150 | dr["FILE OR SUBFILE NUMBER"] = this.SchemaFileNumber; //.ToString("###############.##############");
|
---|
151 |
|
---|
152 | //update the SCHEMA file
|
---|
153 | this.RemoteSession.SaveChanges(m_dtCurrentSchema);
|
---|
154 |
|
---|
155 | //Insert SCHEMA ien into schema fields multiple at BMXIEN1
|
---|
156 | //to ensure that added fields have the correct BMXIEN1
|
---|
157 | for (int j=0; j < m_dtSchemaFields.Rows.Count; j++)
|
---|
158 | {
|
---|
159 | if (m_dtSchemaFields.Rows[j].RowState != DataRowState.Deleted)
|
---|
160 | {
|
---|
161 | dr = m_dtSchemaFields.Rows[j];
|
---|
162 | if (dr["BMXIEN1"].GetType() == typeof(System.DBNull))
|
---|
163 | dr["BMXIEN1"] = m_nSchemaIEN;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | //Add the FIELDS to the SCHEMA entry
|
---|
168 | this.RemoteSession.SaveChanges(m_dtSchemaFields);
|
---|
169 | }
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 | #endregion Methods
|
---|
174 |
|
---|
175 | #region Properties
|
---|
176 |
|
---|
177 |
|
---|
178 | public DataTable SchemaFields
|
---|
179 | {
|
---|
180 | get
|
---|
181 | {
|
---|
182 | return this.m_dtSchemaFields;
|
---|
183 | }
|
---|
184 | set
|
---|
185 | {
|
---|
186 | this.m_dtSchemaFields = value;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | public double SchemaIEN
|
---|
191 | {
|
---|
192 | get
|
---|
193 | {
|
---|
194 | return m_nSchemaIEN;
|
---|
195 | }
|
---|
196 | set
|
---|
197 | {
|
---|
198 | m_nSchemaIEN = value;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | public string SchemaName
|
---|
203 | {
|
---|
204 | get
|
---|
205 | {
|
---|
206 | return m_sSchemaName;
|
---|
207 | }
|
---|
208 | set
|
---|
209 | {
|
---|
210 | m_sSchemaName = value;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | public string SchemaFileName
|
---|
215 | {
|
---|
216 | get
|
---|
217 | {
|
---|
218 | return m_sFileName;
|
---|
219 | }
|
---|
220 | set
|
---|
221 | {
|
---|
222 | m_sFileName = value;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | public bool SchemaReadOnlyDataset
|
---|
227 | {
|
---|
228 | get
|
---|
229 | {
|
---|
230 | return m_bReadOnlyDataset;
|
---|
231 | }
|
---|
232 | set
|
---|
233 | {
|
---|
234 | m_bReadOnlyDataset = value;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | public double SchemaFileNumber
|
---|
238 | {
|
---|
239 | get
|
---|
240 | {
|
---|
241 | return m_nFileNumber;
|
---|
242 | }
|
---|
243 | set
|
---|
244 | {
|
---|
245 | m_nFileNumber = value;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | #endregion Properties
|
---|
250 | }
|
---|
251 | }
|
---|