-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFormSampleMerge.cs
335 lines (298 loc) · 13.7 KB
/
FormSampleMerge.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
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;
using Newtonsoft.Json;
using System.Linq;
namespace DSA_lims
{
public partial class FormSampleMerge : Form
{
private List<Guid> mSampleIds = null;
private string mSampleIdsCsv = String.Empty;
private List<Sample> mSamples = new List<Sample>();
private TreeView mSampleTypeTree = null;
public FormSampleMerge(List<Guid> sampleIds, TreeView sampleTypeTree)
{
InitializeComponent();
mSampleIds = sampleIds;
mSampleTypeTree = sampleTypeTree;
var sampleIdsArr = from item in mSampleIds select "'" + item + "'";
string sampleIdsCsv = string.Join(",", sampleIdsArr);
mSampleIdsCsv = sampleIdsCsv;
}
private void FormSampleMerge_Load(object sender, EventArgs e)
{
HashSet<string> samplePathSet = new HashSet<string>();
SqlConnection conn = null;
try
{
UI.PopulateSampleTypes(mSampleTypeTree, cboxSampleType);
conn = DB.OpenConnection();
foreach(Guid sid in mSampleIds)
{
Sample s = new Sample();
s.LoadFromDB(conn, null, sid);
s.Parameters.Clear();
foreach (Preparation p in s.Preparations)
p.Analyses.Clear();
s.Preparations.Clear();
s.SamplingDateFrom = null;
s.SamplingDateTo = null;
s.ReferenceDate = null;
mSamples.Add(s);
samplePathSet.Add(s.GetSampleTypePath(conn, null));
}
if(samplePathSet.Count == 0)
{
cboxSampleType.SelectedValue = Guid.Empty;
cboxSampleType.Enabled = true;
}
else if(samplePathSet.Count == 1)
{
SqlCommand cmd = new SqlCommand("select id from sample_type where path = @path", conn);
cmd.Parameters.AddWithValue("@path", samplePathSet.ElementAt(0));
Guid stId = (Guid)cmd.ExecuteScalar();
cboxSampleType.SelectedValue = stId;
cboxSampleType.Enabled = false;
}
else
{
string[] firstItems = samplePathSet.ElementAt(0).Split(new char[] { '/' });
int finalLevel = firstItems.Length;
for (int i = 1; i < samplePathSet.Count; i++)
{
int level = 0;
string[] items = samplePathSet.ElementAt(i).Split(new char[] { '/' });
int max = Math.Min(firstItems.Length, items.Length);
for(int j=0; j<items.Length && j<max; j++)
{
if (items[j] == firstItems[j])
level++;
else break;
}
if (level < finalLevel)
finalLevel = level;
}
string finalPath = String.Join("/", firstItems, 0, finalLevel);
if(finalPath == String.Empty)
{
cboxSampleType.SelectedValue = Guid.Empty;
cboxSampleType.Enabled = true;
}
else
{
SqlCommand cmd = new SqlCommand("select id from sample_type where path = @path", conn);
cmd.Parameters.AddWithValue("@path", finalPath);
Guid stId = (Guid)cmd.ExecuteScalar();
cboxSampleType.SelectedValue = stId;
cboxSampleType.Enabled = false;
}
}
List<Sample> mSamplesSortedBySamplingDate = mSamples.OrderBy(x => x.SamplingDateFrom).ToList();
mSamplesSortedBySamplingDate.RemoveAll(x => x.SamplingDateFrom == null);
if (mSamplesSortedBySamplingDate.Count == 0)
{
DateTime now = DateTime.Now;
dtSamplingDate.Value = now;
dtSamplingTime.Value = now;
}
else
{
int mid = mSamplesSortedBySamplingDate.Count / 2;
DateTime dtstime = mSamplesSortedBySamplingDate[mid].SamplingDateFrom.Value;
dtSamplingDate.Value = dtstime;
dtSamplingTime.Value = dtstime;
}
List<Sample> mSamplesSortedByRefDate = mSamples.OrderBy(x => x.ReferenceDate).ToList();
mSamplesSortedByRefDate.RemoveAll(x => x.ReferenceDate == null);
if (mSamplesSortedByRefDate.Count == 0)
{
DateTime now = DateTime.Now;
dtReferenceDate.Value = now;
dtReferenceTime.Value = now;
}
else
{
int mid = mSamplesSortedByRefDate.Count / 2;
DateTime dtrtime = mSamplesSortedByRefDate[mid].ReferenceDate.Value;
dtReferenceDate.Value = dtrtime;
dtReferenceTime.Value = dtrtime;
}
string query = String.Format(@"
select
s.id,
s.number as 'sample_number',
s.external_id,
st.name as 'sample_type_name',
sc.name as 'sample_component_name'
from sample s
inner join sample_type st on st.id = s.sample_type_id
left outer join sample_component sc on sc.id = s.sample_component_id
where s.id in({0})
order by s.number desc", mSampleIdsCsv);
gridSamples.DataSource = DB.GetDataTable(conn, null, query, CommandType.Text);
gridSamples.Columns["id"].Visible = false;
gridSamples.Columns["sample_number"].HeaderText = "Sample number";
gridSamples.Columns["external_id"].HeaderText = "External Id";
gridSamples.Columns["sample_type_name"].HeaderText = "Sample type";
gridSamples.Columns["sample_component_name"].HeaderText = "Sample component";
}
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(!Utils.IsValidGuid(cboxSampleType.SelectedValue))
{
MessageBox.Show("You must select a sample type");
return;
}
Dictionary<string, object> map = new Dictionary<string, object>();
SqlConnection conn = null;
SqlTransaction trans = null;
try
{
conn = DB.OpenConnection();
trans = conn.BeginTransaction();
DateTime currDate = DateTime.Now;
Guid oldSampleId = Utils.MakeGuid(gridSamples.Rows[0].Cells["id"].Value);
Sample newSample = new Sample();
newSample.LoadFromDB(conn, trans, oldSampleId);
newSample.Id = Guid.NewGuid();
foreach (Preparation p in newSample.Preparations)
p.Analyses.Clear();
newSample.Preparations.Clear();
newSample.Parameters.Clear();
newSample.Number = DB.GetNextSampleCount(conn, trans);
newSample.ExternalId = String.IsNullOrEmpty(tbExternalId.Text.Trim()) ? String.Empty : tbExternalId.Text.Trim();
newSample.SampleTypeId = Utils.MakeGuid(cboxSampleType.SelectedValue);
if (Utils.IsValidGuid(cboxSampleComponent.SelectedValue))
newSample.SampleComponentId = Utils.MakeGuid(cboxSampleComponent.SelectedValue);
else newSample.SampleComponentId = Guid.Empty;
newSample.TransformFromId = Guid.Empty;
newSample.MunicipalityId = Guid.Empty;
newSample.LocationType = String.Empty;
newSample.Location = String.Empty;
newSample.StationId = Guid.Empty;
newSample.SampleStorageId = Guid.Empty;
newSample.SamplerId = Guid.Empty;
newSample.SamplingMethodId = Guid.Empty;
newSample.Latitude = newSample.Longitude = newSample.Altitude = null;
newSample.InstanceStatusId = InstanceStatus.Active;
newSample.SamplingDateFrom = new DateTime(dtSamplingDate.Value.Year, dtSamplingDate.Value.Month, dtSamplingDate.Value.Day, dtSamplingTime.Value.Hour, dtSamplingTime.Value.Minute, dtSamplingTime.Value.Second);
newSample.SamplingDateTo = null;
newSample.ReferenceDate = new DateTime(dtReferenceDate.Value.Year, dtReferenceDate.Value.Month, dtReferenceDate.Value.Day, dtReferenceTime.Value.Hour, dtReferenceTime.Value.Minute, dtReferenceTime.Value.Second);
newSample.WetWeight_g = null;
newSample.DryWeight_g = null;
newSample.LodWeightStart = null;
newSample.LodWeightEnd = null;
newSample.LodTemperature = null;
newSample.LodWaterPercent = null;
newSample.LodFactor = null;
newSample.LodWeightStartAsh = null;
newSample.LodWeightEndAsh = null;
newSample.LodTemperatureAsh = null;
newSample.LodWaterPercentAsh = null;
newSample.LodFactorAsh = null;
newSample.LodWeightStartAsh2 = null;
newSample.LodWeightEndAsh2 = null;
newSample.LodTemperatureAsh2 = null;
newSample.LodWaterPercentAsh2 = null;
newSample.LodFactorAsh2 = null;
newSample.Comment = tbComment.Text.Trim();
newSample.LockedId = Guid.Empty;
newSample.CreateDate = currDate;
newSample.CreateId = Common.UserId;
newSample.UpdateDate = currDate;
newSample.UpdateId = Common.UserId;
newSample.StoreToDB(conn, trans);
string json = JsonConvert.SerializeObject(newSample);
DB.AddAuditMessage(conn, trans, "sample", newSample.Id, AuditOperationType.Insert, json, "");
SqlCommand cmd = new SqlCommand("update sample set transform_to_id = @transform_to_id where id in("+ mSampleIdsCsv + ")", conn, trans);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@transform_to_id", newSample.Id);
cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans?.Rollback();
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
return;
}
finally
{
conn?.Close();
}
DialogResult = DialogResult.OK;
Close();
}
private void cboxSampleType_SelectedIndexChanged(object sender, EventArgs e)
{
if (!Utils.IsValidGuid(cboxSampleType.SelectedValue))
{
cboxSampleComponent.DataSource = null;
return;
}
Guid sampleTypeId = Utils.MakeGuid(cboxSampleType.SelectedValue);
TreeNode[] tnodes = mSampleTypeTree.Nodes.Find(sampleTypeId.ToString(), true);
if (tnodes.Length < 1)
{
Common.Log.Error("Unable to find sample type with id: " + sampleTypeId);
MessageBox.Show("Error: Unable to find sample type with id: " + sampleTypeId);
return;
}
SqlConnection conn = null;
try
{
conn = DB.OpenConnection();
UI.PopulateSampleComponentsAscending(conn, sampleTypeId, tnodes[0], cboxSampleComponent);
}
catch (Exception ex)
{
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
return;
}
finally
{
conn?.Close();
}
}
}
}