-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFormLaboratory.cs
304 lines (263 loc) · 13.1 KB
/
FormLaboratory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
DSA Lims - Laboratory Information Management System
Copyright (C) 2018 Norwegian Radiation Protection Authority
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Authors: Dag Robole,
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DSA_lims
{
public partial class FormLaboratory : Form
{
private Dictionary<string, object> p = new Dictionary<string, object>();
public Guid LaboratoryId
{
get { return p.ContainsKey("id") ? (Guid)p["id"] : Guid.Empty; }
}
public string LaboratoryName
{
get { return p.ContainsKey("name") ? p["name"].ToString() : String.Empty; }
}
public FormLaboratory()
{
InitializeComponent();
Text = "Create laboratory";
lblLaboratoryLogoSize.Text = "";
lblAccreditedLogoSize.Text = "";
p["laboratory_logo"] = null;
p["accredited_logo"] = null;
}
public FormLaboratory(Guid lid)
{
InitializeComponent();
Text = "Update laboratory";
lblLaboratoryLogoSize.Text = "";
lblAccreditedLogoSize.Text = "";
p["id"] = lid;
}
private void FormLaboratory_Load(object sender, EventArgs e)
{
SqlConnection conn = null;
try
{
conn = DB.OpenConnection();
if (p.ContainsKey("id"))
{
cboxInstanceStatus.DataSource = DB.GetIntLemmata(conn, null, "csp_select_instance_status", false);
SqlCommand cmd = new SqlCommand("csp_select_laboratory", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", p["id"]);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (!reader.HasRows)
throw new Exception("Laboratory with ID " + p["id"] + " was not found");
reader.Read();
tbName.Text = reader.GetString("name");
tbPrefix.Text = reader.GetString("name_prefix");
tbAddress.Text = reader.GetString("address");
tbEmail.Text = reader.GetString("email");
tbPhone.Text = reader.GetString("phone");
cboxInstanceStatus.SelectedValue = reader.GetInt32("instance_status_id");
tbComment.Text = reader.GetString("comment");
if (DB.IsValidField(reader["laboratory_logo"]))
{
p["laboratory_logo"] = (byte[])reader["laboratory_logo"];
picLaboratoryLogo.Image = Image.FromStream(new MemoryStream((byte[])p["laboratory_logo"]));
lblLaboratoryLogoSize.Text = picLaboratoryLogo.Image.Width.ToString() + " x " + picLaboratoryLogo.Image.Height.ToString();
}
else
{
p["laboratory_logo"] = picLaboratoryLogo.Image = null;
}
if (DB.IsValidField(reader["accredited_logo"]))
{
p["accredited_logo"] = (byte[])reader["accredited_logo"];
picAccreditedLogo.Image = Image.FromStream(new MemoryStream((byte[])p["accredited_logo"]));
lblAccreditedLogoSize.Text = picAccreditedLogo.Image.Width.ToString() + " x " + picAccreditedLogo.Image.Height.ToString();
}
else
{
p["accredited_logo"] = picAccreditedLogo.Image = null;
}
p["assignment_counter"] = reader.GetInt32("assignment_counter");
p["create_date"] = reader.GetDateTime("create_date");
p["create_id"] = reader.GetGuid("create_id");
p["update_date"] = reader.GetDateTime("update_date");
p["update_id"] = reader.GetGuid("update_id");
}
}
else
{
cboxInstanceStatus.DataSource = DB.GetIntLemmata(conn, null, "csp_select_instance_status", false);
cboxInstanceStatus.SelectedValue = InstanceStatus.Active;
}
}
catch (Exception ex)
{
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
DialogResult = DialogResult.Abort;
Close();
}
finally
{
conn?.Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
if(String.IsNullOrEmpty(tbName.Text.Trim()))
{
MessageBox.Show("The name field is mandatory");
return;
}
if (String.IsNullOrEmpty(tbPrefix.Text.Trim()))
{
MessageBox.Show("The prefix field is mandatory");
return;
}
p["name"] = tbName.Text.Trim();
p["name_prefix"] = tbPrefix.Text.Trim().ToUpper();
p["address"] = tbAddress.Text.Trim();
p["email"] = tbEmail.Text.Trim();
p["phone"] = tbPhone.Text.Trim();
p["instance_status_id"] = cboxInstanceStatus.SelectedValue;
p["comment"] = tbComment.Text.Trim();
SqlConnection connection = null;
SqlTransaction transaction = null;
bool success = true;
try
{
connection = DB.OpenConnection();
transaction = connection.BeginTransaction();
if (DB.NameExists(connection, transaction, "laboratory", p["name"].ToString(), LaboratoryId))
{
MessageBox.Show("The laboratory '" + p["name"] + "' already exists");
return;
}
if (!p.ContainsKey("id"))
InsertLaboratory(connection, transaction);
else
UpdateLaboratory(connection, transaction);
transaction.Commit();
}
catch (Exception ex)
{
success = false;
transaction?.Rollback();
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
}
finally
{
connection?.Close();
}
DialogResult = success ? DialogResult.OK : DialogResult.Abort;
Close();
}
private void InsertLaboratory(SqlConnection conn, SqlTransaction trans)
{
p["last_assignment_counter_year"] = DateTime.Now.Year;
p["assignment_counter"] = 1;
p["create_date"] = DateTime.Now;
p["create_id"] = Common.UserId;
p["update_date"] = DateTime.Now;
p["update_id"] = Common.UserId;
SqlCommand cmd = new SqlCommand("csp_insert_laboratory", conn, trans);
cmd.CommandType = CommandType.StoredProcedure;
p["id"] = Guid.NewGuid();
cmd.Parameters.AddWithValue("@id", p["id"]);
cmd.Parameters.AddWithValue("@name", p["name"]);
cmd.Parameters.AddWithValue("@name_prefix", p["name_prefix"]);
cmd.Parameters.AddWithValue("@address", p["address"], String.Empty);
cmd.Parameters.AddWithValue("@email", p["email"], String.Empty);
cmd.Parameters.AddWithValue("@phone", p["phone"], String.Empty);
cmd.Parameters.AddWithValue("@last_assignment_counter_year", p["last_assignment_counter_year"]);
cmd.Parameters.AddWithValue("@assignment_counter", p["assignment_counter"]);
cmd.Parameters.AddWithValue("@instance_status_id", p["instance_status_id"]);
cmd.Parameters.AddWithValue("@comment", p["comment"], String.Empty);
if(p["laboratory_logo"] == null)
cmd.Parameters.Add("@laboratory_logo", SqlDbType.VarBinary, -1).Value = DBNull.Value;
else
cmd.Parameters.Add("@laboratory_logo", SqlDbType.VarBinary, -1).Value = p["laboratory_logo"];
if (p["accredited_logo"] == null)
cmd.Parameters.Add("@accredited_logo", SqlDbType.VarBinary, -1).Value = DBNull.Value;
else
cmd.Parameters.Add("@accredited_logo", SqlDbType.VarBinary, -1).Value = p["accredited_logo"];
cmd.Parameters.AddWithValue("@create_date", p["create_date"]);
cmd.Parameters.AddWithValue("@create_id", p["create_id"]);
cmd.Parameters.AddWithValue("@update_date", p["update_date"]);
cmd.Parameters.AddWithValue("@update_id", p["update_id"]);
cmd.ExecuteNonQuery();
}
private void UpdateLaboratory(SqlConnection conn, SqlTransaction trans)
{
p["update_date"] = DateTime.Now;
p["update_id"] = Common.UserId;
SqlCommand cmd = new SqlCommand("csp_update_laboratory", conn, trans);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", p["id"]);
cmd.Parameters.AddWithValue("@name", p["name"]);
cmd.Parameters.AddWithValue("@name_prefix", p["name_prefix"]);
cmd.Parameters.AddWithValue("@address", p["address"], String.Empty);
cmd.Parameters.AddWithValue("@email", p["email"], String.Empty);
cmd.Parameters.AddWithValue("@phone", p["phone"], String.Empty);
cmd.Parameters.AddWithValue("@instance_status_id", p["instance_status_id"]);
cmd.Parameters.AddWithValue("@comment", p["comment"], String.Empty);
if (p["laboratory_logo"] == null)
cmd.Parameters.Add("@laboratory_logo", SqlDbType.VarBinary, -1).Value = DBNull.Value;
else
cmd.Parameters.Add("@laboratory_logo", SqlDbType.VarBinary, -1).Value = p["laboratory_logo"];
if (p["accredited_logo"] == null)
cmd.Parameters.Add("@accredited_logo", SqlDbType.VarBinary, -1).Value = DBNull.Value;
else
cmd.Parameters.Add("@accredited_logo", SqlDbType.VarBinary, -1).Value = p["accredited_logo"];
cmd.Parameters.AddWithValue("@update_date", p["update_date"]);
cmd.Parameters.AddWithValue("@update_id", p["update_id"]);
cmd.ExecuteNonQuery();
}
private void picLaboratoryLogo_DoubleClick(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG)|*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG";
if (dialog.ShowDialog() == DialogResult.Cancel)
return;
p["laboratory_logo"] = File.ReadAllBytes(dialog.FileName);
picLaboratoryLogo.Image = Image.FromStream(new MemoryStream((byte[])p["laboratory_logo"]));
lblLaboratoryLogoSize.Text = picLaboratoryLogo.Image.Width.ToString() + " x " + picLaboratoryLogo.Image.Height.ToString();
}
private void picAccreditedLogo_DoubleClick(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG)|*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG";
if (dialog.ShowDialog() == DialogResult.Cancel)
return;
p["accredited_logo"] = File.ReadAllBytes(dialog.FileName);
picAccreditedLogo.Image = Image.FromStream(new MemoryStream((byte[])p["accredited_logo"]));
lblAccreditedLogoSize.Text = picAccreditedLogo.Image.Width.ToString() + " x " + picAccreditedLogo.Image.Height.ToString();
}
}
}