-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtilitlies.cs
321 lines (306 loc) · 13.4 KB
/
Utilitlies.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace OCDataImporter
{
public static class Utilities
{
/// <summary>
/// Checks if a repeating event is correctly formated according to the format:
/// ITEMNAME_E{N}_G{N}. Throws a OCDataImporterException if the format is not correct
/// </summary>
/// <param name="theName">The item name</param>
/// <returns>the value of the repeat</returns>
///
public static string GetOID(string val)
{
val = val.Replace(" ", "^");
string[] vals = val.Split('^');
if (vals.Length == 2) return vals[1];
else return val;
}
public static string Get_SE_RepeatingKey_FromStudyDataColumn(string theName)
{
// Format: Adverse_Event_E1_G3 We must get the "1" here. It may not exist, as in: SurgeryPlan_E_G2 or, for any other reason. -> Then error!
string errtext = "Error while getting STUDYEVENT Repeating Key: Cant resolve the DataItemColumnName " + theName + ". The proper name should look like 'DataItem_E2_G3 Where E2 means Event repeating key = 2 and G3 means Group repeating key = 3. \r\nASSUMING 1 AS REPEATING KEY...If this is not what you meant, the generated files ARE INCOMPLETE AND CAN NOT BE USED. See documentation for more.";
string[] split = theName.Split('_');
int lengtOfName = split.Length;
int waarde = -1;
try
{
if (lengtOfName >= 2 && split[lengtOfName - 1].Length > 1 && split[lengtOfName - 1].StartsWith("E"))
{
waarde = System.Convert.ToInt16(split[lengtOfName - 1].Substring(1));
if (waarde > 0) return (waarde.ToString());
}
else if (lengtOfName >= 3 && split[lengtOfName - 2].Length > 1 && split[lengtOfName - 2].StartsWith("E"))
{
waarde = System.Convert.ToInt16(split[lengtOfName - 2].Substring(1));
if (waarde > 0) return (waarde.ToString());
}
}
catch (Exception ex)
{
//MessageBox.Show(errtext + " " + ex.Message, "OCDataImporter", MessageBoxButtons.OK, MessageBoxIcon.Stop);
//exit_error(errtext + " " + ex.Message);
throw new OCDataImporterException(errtext + " " + ex.Message);
}
// MessageBox.Show(errtext, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// exit_error(errtext); Version 2.0.1 -> Assume 1 if the repeating key cant be determined. (and dont exit) The error message tells the user about the caveats.
return ("1");
}
/// <summary>
/// Checks if a repeating group is correctly formated according to the format:
/// ITEMNAME_E{N}_G{N}. Throws a OCDataImporterException if the format is not correct
/// </summary>
/// <param name="theName">The item name</param>
/// <returns>the value of the repeat</returns>
public static string Get_GR_RepeatingKey_FromStudyDataColumn(string theName)
{
// Format: Adverse_Event_E1_G3 We must get the "3" here; G=group, C= WAS also group, not anymore as of 2.0.2 to avoid confusion
string errtext = "Error while getting GROUP Repeating Key: Cant resolve the DataItemColumnName " + theName + ". The proper name should look like 'DataItem_E2_G3 Where E2 means StudyEvent repeating key = 2 and G3 means Group repeating key = 3. \r\nExiting...The generated files ARE INCOMPLETE AND CAN NOT BE USED";
string[] split = theName.Split('_');
int lengtOfName = split.Length;
int waarde = -1;
try
{
if (lengtOfName >= 2 && split[lengtOfName - 1].Length > 1 && (split[lengtOfName - 1].StartsWith("G")))
{
waarde = System.Convert.ToInt16(split[lengtOfName - 1].Substring(1));
if (waarde > 0) return (waarde.ToString());
}
}
catch (Exception ex)
{
//MessageBox.Show(errtext + " " + ex.Message, "OCDataImporter", MessageBoxButtons.OK, MessageBoxIcon.Stop);
//exit_error(errtext + " " + ex.Message);
throw new OCDataImporterException(errtext + " " + ex.Message);
}
//MessageBox.Show(errtext, "OCDataImporter", MessageBoxButtons.OK, MessageBoxIcon.Stop);
//exit_error(errtext);
return ("N"); // this means: Cant determine but maybe going to get from row: the check will be done in DoWork
}
public static string FillTildes(string var, int len)
{
if (var.Length < len)
{
int aantalnullen = len - var.Length;
string voornullen = "";
for (int ii = 0; ii < aantalnullen; ii++) voornullen += "~";
return (var + voornullen);
}
return (var);
}
public static bool IsNumber(String s)
{
bool value = true;
foreach (Char c in s.ToCharArray())
{
value = value && Char.IsDigit(c);
}
return value;
}
public static bool CheckDay(string day, string month)
{
try
{
int theDay = System.Convert.ToInt16(day);
if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month == "12")
{
if (theDay < 1 || theDay > 31) return (false);
}
if (month == "04" || month == "06" || month == "09" || month == "11")
{
if (theDay < 1 || theDay > 30) return (false);
}
if (month == "02")
{
if (theDay < 1 || theDay > 29) return (false);
}
}
catch (Exception ex)
{
return (false);
}
return (true);
}
public static bool CheckYear(string year)
{
try
{
int theYear = System.Convert.ToInt16(year);
DateTime dt = DateTime.Now;
// changed to 9999. It is not up to the software to limit years. E.g. a study could want to register
// a next contact moment with a patient in 5, 6, N .... years. JR
if (theYear < 1700 || theYear > 9999) return false;
else return true;
}
catch (Exception ex)
{
return false;
}
}
public static string ConvertToODMPartial(string theSource)
{
if (theSource.Trim() == "") return ("");
theSource = theSource.Replace('/', '-');
try
{
string[] splitd = theSource.Split('-');
if (splitd.Length == 2)
{
if (splitd[0].Length == 4 && (splitd[1].Length == 3 || splitd[1].Length == 2)) // yyyy-mmm or yyyy-mm
{
string mon = Utilities.Get_maand(splitd[1]);
if (mon.StartsWith("Error")) return (mon);
else
{
if (Utilities.CheckYear(splitd[0])) return (mon + "-" + splitd[0]);
else return ("Error: Wrong Year in partial date");
}
}
if (splitd[1].Length == 4 && (splitd[0].Length == 3 || splitd[0].Length == 2)) // mmm-yyyy or mm-yyyy
{
string mon = Utilities.Get_maand(splitd[0]);
if (mon.StartsWith("Error")) return (mon);
else
{
if (Utilities.CheckYear(splitd[1])) return (mon + "-" + splitd[1]);
else return ("Error: Wrong Year in partial date");
}
}
}
if (splitd.Length == 1)
{
String year = splitd[0];
if (Utilities.CheckYear(year))
return year;
else
return ("Error: Wrong Year in partial date");
}
}
catch (Exception ex)
{
return ("Error: Unrecognised partial date and exception: " + ex.Message);
}
return ("Error: Unrecognised partial date");
}
public static string Get_maand(string inp)
{
inp = inp.ToUpper();
switch (inp)
{
case "JAN":
case "01":
case "1":
return ("Jan");
case "FEB":
case "02":
case "2":
return ("Feb");
case "MAR":
case "MRT":
case "03":
case "3":
return ("Mar");
case "APR":
case "04":
case "4":
return ("Apr");
case "MAY":
case "05":
case "MEI":
case "5":
return ("May");
case "JUN":
case "06":
case "6":
return ("Jun");
case "JUL":
case "07":
case "7":
return ("Jul");
case "AUG":
case "08":
case "8":
return ("Aug");
case "SEP":
case "09":
case "9":
return ("Sep");
case "OCT":
case "OKT":
case "10":
return ("Oct");
case "NOV":
case "11":
return ("Nov");
case "DEC":
case "12":
return ("Dec");
default:
return ("Error: month:" + inp + " not found");
}
}
public static string ConvertToODMFormat(string theSource, String selectedDateFormat)
{
if (theSource.Trim() == "") return (""); // 1.0f .Trim = 2.0.4
if (selectedDateFormat == "--select--") return (theSource);
theSource = theSource.Replace('/', '-');
if (selectedDateFormat == "day-month-year")
{
try
{
string[] splitd = theSource.Split('-');
string mon = Utilities.Get_maand(splitd[1]);
if (mon.StartsWith("Error")) return (mon);
string day = splitd[0];
if (day.Length == 1) day = "0" + day;
if (Utilities.CheckYear(splitd[2]) && Utilities.CheckDay(day, mon)) return (splitd[2] + "-" + mon + "-" + day);
else return ("Error: Wrong day or year");
}
catch (Exception ex)
{
return ("Error: date is not in day-month-yyyy format ");
}
}
if (selectedDateFormat == "month-day-year")
{
try
{
string[] splitd = theSource.Split('-');
string mon = Utilities.Get_maand(splitd[0]);
if (mon.StartsWith("Error")) return (mon);
string day = splitd[1];
if (day.Length == 1) day = "0" + day;
if (Utilities.CheckYear(splitd[2]) && Utilities.CheckDay(day, mon)) return (splitd[2] + "-" + mon + "-" + day);
else return ("Error: Wrong day or year");
}
catch (Exception ex)
{
return ("Error: date is not in month-day-yyyy format ");
}
}
if (selectedDateFormat == "year-month-day")
{
try
{
string[] splitd = theSource.Split('-');
string mon = Utilities.Get_maand(splitd[1]);
if (mon.StartsWith("Error")) return (mon);
string day = splitd[2];
if (day.Length == 1) day = "0" + day;
if (Utilities.CheckYear(splitd[0]) && Utilities.CheckDay(day, mon)) return (splitd[0] + "-" + mon + "-" + day);
else return ("Error: Wrong day or year");
}
catch (Exception ex)
{
return ("Error: date is not in yyyy-month-day format ");
}
}
return ("Error: unknown date format ");
}
}
}