private static string ConvertDateCriteria(string dateCriteria, string columnName) { string fromDate, toDate; bool isRange; DateRangeHelper.Parse(dateCriteria, out fromDate, out toDate, out isRange); StringBuilder dateRangeCriteria = new StringBuilder(); if (fromDate != "") { //When a dicom date is specified with no '-', it is to be taken as an exact date. if (!isRange) { dateRangeCriteria.AppendFormat("( {0} = '{1}' )", columnName, fromDate); } else { dateRangeCriteria.AppendFormat("( {0} IS NOT NULL AND {0} >= '{1}' )", columnName, fromDate); } } if (toDate != "") { if (fromDate == "") { dateRangeCriteria.AppendFormat("( {0} IS NULL OR ", columnName); } else { dateRangeCriteria.AppendFormat(" AND ("); } dateRangeCriteria.AppendFormat("{0} <= '{1}' )", columnName, toDate); } //will only happen if the query string is bad. string returnCriteria = dateRangeCriteria.ToString(); if (String.IsNullOrEmpty(returnCriteria)) { return(returnCriteria); } return(String.Format("({0})", returnCriteria)); }
public void TestDateRangeParser() { string[,] tests = { { "20070101", "20070101", "", "False" }, { "20070101-", "20070101", "", "True" }, { "-20070101", "", "20070101", "True" }, { "20070101-20070101", "20070101", "20070101", "True" }, { "20070101-20070202", "20070101", "20070202", "True" }, //from date is after to { "20070102-20070101", "", "", "exception" }, //bad format, extra '-' { "20070101-20070101-", "", "", "exception" }, { "-20070101-20070101", "", "", "exception" }, //bad format, missing '-' { "2007010120070101", "", "", "exception" }, //bad format, missing character { "20070101-2007010", "", "", "exception" }, //bad format, bad characters { "2007010a-20070101", "", "", "exception" } }; for (int i = 0; i < tests.Length / 4; ++i) { string dateRange = tests[i, 0]; string expectedFromDate = tests[i, 1]; string expectedToDate = tests[i, 2]; string expectedResult = tests[i, 3]; bool isRange; try { DateTime?from, to; DateRangeHelper.Parse(dateRange, out from, out to, out isRange); if (expectedResult == "exception") { Assert.Fail("expected an exception"); } if (expectedFromDate == "") { Assert.IsNull(from); } else { Assert.AreEqual(((DateTime)from).ToString(DateParser.DicomDateFormat), expectedFromDate); } if (expectedToDate == "") { Assert.IsNull(to); } else { Assert.AreEqual(((DateTime)to).ToString(DateParser.DicomDateFormat), expectedToDate); } Assert.AreEqual(isRange.ToString(), expectedResult); } catch (Exception e) { if (expectedResult != "exception") { Assert.Fail(e.Message); } } try { string fromString, toString; DateRangeHelper.Parse(dateRange, out fromString, out toString, out isRange); if (expectedResult == "exception") { Assert.Fail("expected an exception"); } Assert.AreEqual(fromString, expectedFromDate); Assert.AreEqual(toString, expectedToDate); Assert.AreEqual(isRange.ToString(), expectedResult); } catch (Exception e) { if (expectedResult != "exception") { Assert.Fail(e.Message); } } try { int fromInt, toInt; DateRangeHelper.Parse(dateRange, out fromInt, out toInt, out isRange); if (expectedResult == "exception") { Assert.Fail("expected an exception"); } if (expectedFromDate == "") { Assert.AreEqual(fromInt, 0); } else { Assert.AreEqual(fromInt, Convert.ToInt32(expectedFromDate, CultureInfo.InvariantCulture)); } if (expectedToDate == "") { Assert.AreEqual(toInt, 0); } else { Assert.AreEqual(toInt, Convert.ToInt32(expectedToDate, CultureInfo.InvariantCulture)); } } catch (Exception e) { if (expectedResult != "exception") { Assert.Fail(e.Message); } } } }
public void OnStudyQuery(DicomMessage message, SelectCallback <IStudyData> callback) { // Supported Query Condition includes: // PatientsName, PatientsId, PatientsSex, PatientsBirthDate, PatientsBirthTime // StudyInstanceUid, StudyId, StudyDescription, AccessionNumber, ModalitiesInStudy // ReferringPhysiciansName, StudyDate, StudyTime var data = message.DataSet; using (var ctx = new PacsContext()) { #region Build Query //Linq query is lazy query mechanism var results = from study in ctx.Studies select study; foreach (var attrib in message.DataSet) { if (!attrib.IsNull) { switch (attrib.Tag.TagValue) { case DicomTags.PatientsName: { var patientName = data[DicomTags.PatientsName].GetString(0, string.Empty); if (patientName.Length == 0) { break; } var replaced = QueryHelper.ReplacsWildcard(patientName); if (replaced == null) { results = from study in results where study.PatientName.Equals(patientName) select study; } else { results = from study in results where study.PatientName.Contains(replaced) select study; } break; } case DicomTags.PatientId: { var patientId = data[DicomTags.PatientId].GetString(0, string.Empty); if (patientId.Length == 0) { break; } var replaced = QueryHelper.ReplacsWildcard(patientId); if (replaced == null) { results = from row in results where row.PatientId.Equals(patientId) select row; } else { results = from row in results where row.PatientId.Contains(replaced) select row; } break; } case DicomTags.PatientsSex: { var values = (string[])data[DicomTags.PatientsSex].Values; if (values == null || values.Length == 0) { break; } results = from study in results where values.Length == 1 ? study.PatientSex.Equals(values[0]) : values.Contains(study.PatientSex) select study; break; } case DicomTags.PatientsBirthDate: { var values = (string[])data[DicomTags.PatientsBirthDate].Values; if (values == null || values.Length == 0) { break; } var dt = DateParser.Parse(values[0]); if (dt == null) { break; } results = from study in results where values.Length == 1 ? study.PatientBirthday.Equals(dt.Value) : values.Contains(DateParser.ToDicomString(study.PatientBirthday)) select study; break; } case DicomTags.StudyDate: { var v = data[DicomTags.StudyDate].GetString(0, string.Empty); if (string.IsNullOrEmpty(v)) { break; } DateTime?startDate; DateTime?toDate; bool isRange; DateRangeHelper.Parse(v, out startDate, out toDate, out isRange); if (isRange) { results = from study in results where study.StudyDate >= startDate.Value && study.StudyDate <= toDate.Value select study; } else { if (startDate != null) { results = from study in results where study.StudyDate >= startDate.Value select study; } else { results = from study in results where study.StudyDate <= toDate.Value select study; } } break; } } } } #endregion Logger.Warn(results.ToString()); foreach (var source in results.ToList()) { callback(source); } } }
private void ParseCriterion() { _parsedCriterion = true; DateRangeHelper.Parse(Criterion.GetString(0, ""), out _date1, out _date2, out _isRange); }