-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFormImportSamplesSampReg.cs
307 lines (257 loc) · 10 KB
/
FormImportSamplesSampReg.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
/*
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.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;
namespace DSA_lims
{
public partial class FormImportSamplesSampReg : Form
{
private TreeView mTreeSampleTypes = null;
private Guid mFileId = Guid.Empty;
private string mProject = String.Empty;
private List<SampleImportEntry> mSamples = new List<SampleImportEntry>();
public List<SampleImportEntry> ImportedSamples { get { return mSamples; } }
private Guid mSelectedLaboratoryId { get; set; }
public Guid SelectedLaboratoryId { get { return mSelectedLaboratoryId; } }
private Guid mSelectedSubProjectId { get; set; }
public Guid SelectedSubProjectId { get { return mSelectedSubProjectId; } }
public FormImportSamplesSampReg(TreeView treeSampleTypes)
{
InitializeComponent();
mTreeSampleTypes = treeSampleTypes;
UI.PopulateSampleTypes(mTreeSampleTypes, cboxSampleTypes);
}
private void FormImportSamplesSampReg_Load(object sender, EventArgs e)
{
tableLayout_Resize(sender, e);
SqlConnection conn = null;
try
{
conn = DB.OpenConnection();
UI.PopulateComboBoxes(conn, "csp_select_projects_main_short", new[] {
new SqlParameter("@instance_status_level", InstanceStatus.Active)
}, cboxProjectMain);
UI.PopulateComboBoxes(conn, "csp_select_laboratories_short", new[] {
new SqlParameter("@instance_status_level", InstanceStatus.Active)
}, cboxLaboratory);
}
catch(Exception ex)
{
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
}
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(cboxLaboratory.SelectedValue))
{
MessageBox.Show("You must select a laboratory first");
return;
}
if (!Utils.IsValidGuid(cboxProjectSub.SelectedValue))
{
MessageBox.Show("You must select a project first");
return;
}
if(mSamples.Count < 1)
{
MessageBox.Show("No samples to import");
return;
}
foreach (SampleImportEntry se in mSamples)
{
if(!Utils.IsValidGuid(se.LIMSSampleTypeId))
{
MessageBox.Show("Sample " + se.Number + " must have a LIMS sample type");
return;
}
}
mSelectedLaboratoryId = Utils.MakeGuid(cboxLaboratory.SelectedValue);
mSelectedSubProjectId = Utils.MakeGuid(cboxProjectSub.SelectedValue);
DialogResult = DialogResult.OK;
Close();
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
// import from sample registration
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "TXT files (*.txt)|*.txt|All files (*.*)|*.*";
if (dialog.ShowDialog() != DialogResult.OK)
return;
mFileId = Guid.Empty;
mSamples.Clear();
mProject = String.Empty;
String line = String.Empty;
StreamReader reader = null;
try
{
reader = new StreamReader(dialog.FileName);
line = reader.ReadLine();
if(line == null)
{
MessageBox.Show("File " + dialog.FileName + " does not appear to be valid");
return;
}
try
{
mFileId = Guid.Parse(line.Trim());
}
catch
{
MessageBox.Show("File " + dialog.FileName + " does not appear to be valid");
return;
}
while ((line = reader.ReadLine()) != null)
{
ParseLine(line);
}
Populate();
}
catch(Exception ex)
{
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
}
finally
{
reader?.Close();
}
}
private void Populate()
{
tbFileID.Text = mFileId.ToString();
tbProject.Text = mProject;
gridSamples.Rows.Clear();
foreach (SampleImportEntry se in mSamples)
{
if (se.LIMSSampleTypeId == Guid.Empty)
{
string st = se.SampleType.ToLower();
foreach (Lemma<Guid, string> l in cboxSampleTypes.Items)
{
string s = l.Name.ToLower();
if (s.StartsWith(st + " -> "))
{
se.LIMSSampleType = l.Name;
se.LIMSSampleTypeId = l.Id;
}
}
}
gridSamples.Rows.Add(se.Number, se.ExternalId, se.SamplingDate, se.Latitude, se.Longitude, se.Altitude, se.Location, se.SampleType, se.LIMSSampleType);
}
gridSamples.Columns["ColumnSamplingDate"].DefaultCellStyle.Format = Utils.DateTimeFormatNorwegian;
}
private void ParseLine(string line)
{
string[] items = line.Split(new char[] { '|' });
if (items.Length != 14)
throw new Exception("Wrong number of items in line. Expected 14, got " + items.Length);
SampleImportEntry s = new SampleImportEntry();
s.Number = Convert.ToInt32(items[2]);
s.ExternalId = items[0] + " - " + items[2];
mProject = items[1].Trim();
DateTimeOffset dto = DateTimeOffset.Parse(items[3], CultureInfo.InvariantCulture);
s.SamplingDate = dto.DateTime + dto.Offset;
s.Latitude = Convert.ToDouble(items[4].Trim());
s.Longitude = Convert.ToDouble(items[5].Trim());
s.Altitude = Convert.ToDouble(items[6].Trim());
s.Location = items[7].Trim();
s.SampleType = items[8].Trim();
if(items.Length >= 14)
s.Comment = items[13].Trim();
mSamples.Add(s);
}
private void tableLayout_Resize(object sender, EventArgs e)
{
cboxProjectMain.Width = tbFileID.Width / 2;
}
private void btnSetSampleType_Click(object sender, EventArgs e)
{
if(gridSamples.SelectedRows.Count < 1)
{
MessageBox.Show("You must select one or more samples first");
return;
}
foreach (DataGridViewRow row in gridSamples.SelectedRows)
{
int num = Convert.ToInt32(row.Cells["ColumnNumber"].Value);
SampleImportEntry se = mSamples.Find(x => x.Number == num);
if (se == null)
continue;
se.LIMSSampleType = cboxSampleTypes.Text;
se.LIMSSampleTypeId = Utils.MakeGuid(cboxSampleTypes.SelectedValue);
}
Populate();
}
private void cboxSampleTypes_Leave(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(cboxSampleTypes.Text.Trim()))
{
cboxSampleTypes.SelectedItem = Guid.Empty;
return;
}
if (!Utils.IsValidGuid(cboxSampleTypes.SelectedValue))
{
cboxSampleTypes.SelectedValue = Guid.Empty;
}
}
private void cboxProjectMain_SelectedIndexChanged(object sender, EventArgs e)
{
if (!Utils.IsValidGuid(cboxProjectMain.SelectedValue))
{
cboxProjectSub.DataSource = null;
return;
}
SqlConnection conn = null;
try
{
Guid projectId = Utils.MakeGuid(cboxProjectMain.SelectedValue);
conn = DB.OpenConnection();
UI.PopulateComboBoxes(conn, "csp_select_projects_sub_short", new[] {
new SqlParameter("@project_main_id", projectId),
new SqlParameter("@instance_status_level", InstanceStatus.Active)
}, cboxProjectSub);
}
catch (Exception ex)
{
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
}
finally
{
conn?.Close();
}
}
}
}