-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFormSamplingMeth.cs
199 lines (172 loc) · 7.47 KB
/
FormSamplingMeth.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
/*
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
namespace DSA_lims
{
public partial class FormSamplingMeth : Form
{
private Dictionary<string, object> p = new Dictionary<string, object>();
public Guid SamplingMethodId
{
get { return p.ContainsKey("id") ? (Guid)p["id"] : Guid.Empty; }
}
public string SamplingMethodName
{
get { return p.ContainsKey("name") ? p["name"].ToString() : String.Empty; }
}
public FormSamplingMeth()
{
InitializeComponent();
Text = "Create sampling method";
}
public FormSamplingMeth(Guid smid)
{
InitializeComponent();
Text = "Update sampling method";
p["id"] = smid;
}
private void FormSamplingMeth_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_sampling_method", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", p["id"]);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (!reader.HasRows)
throw new Exception("Sampler with ID " + p["id"] + " was not found");
reader.Read();
tbName.Text = reader.GetString("name");
cboxInstanceStatus.SelectedValue = reader.GetInt32("instance_status_id");
tbComment.Text = reader.GetString("comment");
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("Name is mandatory");
return;
}
p["name"] = tbName.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, "sampling_method", p["name"].ToString(), SamplingMethodId))
{
MessageBox.Show("The sampling method '" + p["name"] + "' already exists");
return;
}
if (!p.ContainsKey("id"))
InsertSamplingMethod(connection, transaction);
else
UpdateSamplingMethod(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 InsertSamplingMethod(SqlConnection conn, SqlTransaction trans)
{
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_sampling_method", 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("@instance_status_id", p["instance_status_id"]);
cmd.Parameters.AddWithValue("@comment", p["comment"], String.Empty);
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 UpdateSamplingMethod(SqlConnection conn, SqlTransaction trans)
{
p["update_date"] = DateTime.Now;
p["update_id"] = Common.UserId;
SqlCommand cmd = new SqlCommand("csp_update_sampling_method", conn, trans);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", p["id"]);
cmd.Parameters.AddWithValue("@name", p["name"]);
cmd.Parameters.AddWithValue("@instance_status_id", p["instance_status_id"]);
cmd.Parameters.AddWithValue("@comment", p["comment"], String.Empty);
cmd.Parameters.AddWithValue("@update_date", p["update_date"]);
cmd.Parameters.AddWithValue("@update_id", p["update_id"]);
cmd.ExecuteNonQuery();
}
}
}