Пример #1
0
        /// <summary>
        /// Finds the study.
        /// </summary>
        /// <param name="addin">The addin.</param>
        /// <param name="connection">The connection.</param>
        /// <param name="ds">The ds.</param>
        /// <param name="response">The response.</param>
        /// <returns></returns>
        public static DicomCommandStatusType FindStudy(FindAddIn addin, string connection, DicomDataSet ds, DicomDataSet response)
        {
            string          filter = string.Empty;
            string          temp;
            SqlCeDataReader reader;

            temp = ds.GetValue <string>(DicomTag.PatientID, string.Empty);
            if (temp.Length > 0)
            {
                filter = CheckForWildcards("Studies.PatientId = '" + temp + "'");
            }

            temp = ds.GetValue <string>(DicomTag.PatientName, string.Empty);
            if (temp.Length > 0)
            {
                if (filter.Length > 0)
                {
                    filter += " AND ";
                }
                filter += CheckForWildcards("Name LIKE '" + temp + "'");
            }

            List <string> instances = ds.GetValue <List <string> >(DicomTag.StudyInstanceUID, new List <string>());

            foreach (string instance in instances)
            {
                if (filter.Length > 0)
                {
                    filter += " AND ";
                }

                filter += CheckForWildcards("StudyInstanceUid = '" + instance + "'");
            }

            DicomDateRangeValue[] d = ExtensionMethods.GetDateRange(ds, DicomTag.StudyDate);
            DicomTimeRangeValue[] t = ExtensionMethods.GetTimeRange(ds, DicomTag.StudyTime);
            int dCount     = (d == null ? 0 : d.Length);
            int tCount     = (t == null ? 0 : t.Length);
            int maxEntries = Math.Max(dCount, tCount);

            for (int i = 0; i < maxEntries; i++)
            {
                DateTime startDate      = new DateTime(1900, 1, 1, 0, 0, 0);
                DateTime endDate        = new DateTime(3000, 12, 31, 23, 59, 59);
                DateTime startTime      = DateTime.MinValue;
                DateTime endTime        = DateTime.MaxValue;
                string   sStartDateTime = string.Empty;
                string   sEndDateTime   = string.Empty;

                // Get the start date and end date
                if (i < dCount)
                {
                    // start date
                    if ((d[i].Type == DicomRangeType.Lower) || (d[i].Type == DicomRangeType.Both))
                    {
                        startDate = new DateTime(d[i].Date1.Year, d[i].Date1.Month, d[i].Date1.Day, 0, 0, 0);
                    }

                    // end date
                    if (d[i].Type == DicomRangeType.Upper)
                    {
                        endDate = new DateTime(d[i].Date1.Year, d[i].Date1.Month, d[i].Date1.Day, 23, 59, 59);
                    }
                    else if (d[i].Type == DicomRangeType.Both)
                    {
                        endDate = new DateTime(d[i].Date2.Year, d[i].Date2.Month, d[i].Date2.Day, 23, 59, 59);
                    }
                }

                // Get the start time and end time
                if (i < tCount)
                {
                    // start date
                    if ((t[i].Type == DicomRangeType.Lower) || (t[i].Type == DicomRangeType.Both))
                    {
                        startTime = new DateTime(1, 1, 1, t[i].Time1.Hours, t[i].Time1.Minutes, t[i].Time1.Seconds);
                    }

                    // end date
                    if (t[i].Type == DicomRangeType.Upper)
                    {
                        endTime = new DateTime(1, 1, 1, t[i].Time1.Hours, t[i].Time1.Minutes, t[i].Time1.Seconds);
                    }
                    else if (t[i].Type == DicomRangeType.Both)
                    {
                        endTime = new DateTime(1, 1, 1, t[i].Time2.Hours, t[i].Time2.Minutes, t[i].Time2.Seconds);
                    }
                }

                string sAdd = string.Format("( (StudyDate >= '{0}') and (StudyDate <= '{1}'))", startDate, endDate);
                string sTimeFormatString = "AND ((CONVERT(nvarchar,StudyDate,108) BETWEEN '{0}' AND '{1}'))";

                filter += sAdd;
                sAdd    = string.Format(sTimeFormatString, startTime.ToString("HH:mm:ss"), endTime.ToString("HH:mm:ss"));
                filter += sAdd;
            }

            temp = ds.GetValue <string>(DicomTag.AccessionNumber, string.Empty);
            if (temp.Length > 0)
            {
                if (filter.Length > 0)
                {
                    filter += " AND ";
                }
                filter += CheckForWildcards("AccessionNumber = '" + temp + "'");
            }

            temp = ds.GetValue <string>(DicomTag.StudyID, string.Empty);
            if (temp.Length > 0)
            {
                if (filter.Length > 0)
                {
                    filter += " AND ";
                }
                filter += CheckForWildcards("StudyId LIKE '" + temp + "'");
            }

            temp = ds.GetValue <string>(DicomTag.ReferringPhysicianName, string.Empty);
            if (temp.Length > 0)
            {
                temp = temp.Replace("*", "%");
                if (filter.Length > 0)
                {
                    filter += " AND ";
                }
                filter += CheckForWildcards("ReferDrName LIKE '" + temp + "'");
            }

            temp = FindElementModalitiesInStudy(ds);
            if (temp.Length > 0)
            {
                if (filter.Length > 0)
                {
                    filter += " AND ";
                }
                filter += temp;
            }

            if (string.IsNullOrEmpty(filter))
            {
                filter = "SELECT p.PatientId,Name,Studies.* FROM Studies JOIN Patients p ON p.PatientId = Studies.PatientId";
            }
            else
            {
                filter = "SELECT p.PatientId,Name,Studies.* FROM Studies JOIN Patients p ON p.PatientId = Studies.PatientId WHERE " + filter;
            }


            reader = SqlCeHelper.ExecuteReader(connection, filter);
            while (reader.Read())
            {
                response.InsertElementAndSetValue(DicomTag.StudyInstanceUID, reader["StudyInstanceUID"]);

                if (reader["StudyDate"] != System.DBNull.Value)
                {
                    DateTime date = DateTime.Parse(reader["StudyDate"].ToString());

                    response.InsertElementAndSetValue(DicomTag.StudyDate, date.ToShortDateString());
                    response.InsertElementAndSetValue(DicomTag.StudyTime, date.ToShortTimeString());
                }

                if (reader["AccessionNumber"] != null)
                {
                    response.InsertElementAndSetValue(DicomTag.AccessionNumber, reader["AccessionNumber"]);
                }
                if (reader["StudyId"] != null)
                {
                    response.InsertElementAndSetValue(DicomTag.StudyID, reader["StudyId"]);
                }
                if (reader["PatientID"] != null)
                {
                    response.InsertElementAndSetValue(DicomTag.PatientID, reader["PatientId"]);
                }
                if (reader["Name"] != null)
                {
                    response.InsertElementAndSetValue(DicomTag.PatientName, reader["Name"]);
                }
                if (reader["ReferDrName"] != null)
                {
                    response.InsertElementAndSetValue(DicomTag.ReferringPhysicianName, reader["ReferDrName"]);
                }
                if (reader["StudyDescription"] != null)
                {
                    response.InsertElementAndSetValue(DicomTag.StudyDescription, reader["StudyDescription"]);
                }

                try
                {
                    string sql = string.Format("SELECT Count(*) FROM Series WHERE StudyInstanceUid = '{0}'", reader["StudyInstanceUID"]);

                    response.InsertElementAndSetValue(DicomTag.NumberOfStudyRelatedSeries, (int)SqlCeHelper.ExecuteScalar(connection, sql));
                    sql = string.Format("SELECT Count(*) FROM Images WHERE StudyInstanceUid = '{0}'", reader["StudyInstanceUID"]);
                    response.InsertElementAndSetValue(DicomTag.NumberOfStudyRelatedInstances, (int)SqlCeHelper.ExecuteScalar(connection, sql));
                }
                catch { }

                if (addin.OnMatchFound(response))
                {
                    reader.Close();
                    return(DicomCommandStatusType.Cancel);
                }
                else if (addin.Cancel)
                {
                    if (addin.BreakType == BreakType.Cancel)
                    {
                        return(DicomCommandStatusType.Cancel);
                    }
                    else
                    {
                        return(DicomCommandStatusType.Failure);
                    }
                }

                //response.Clear();
            }
            return(DicomCommandStatusType.Success);
        }
Пример #2
0
        public DicomCommandStatusType OnFind(DicomClient Client, byte PresentationId, int MessageId, string AffectedClass,
                                             DicomCommandPriorityType Priority, DicomDataSet Request)
        {
            if (Request == null)
            {
                return(DicomCommandStatusType.InvalidArgumentValue);
            }

            try
            {
                string level = Request.GetValue <string>(DicomTag.QueryRetrieveLevel, string.Empty);
                DicomCommandStatusType status   = Module.GetAttributeStatus(level, AffectedClass, Request);
                DicomDataSet           response = new DicomDataSet(Client.Server.TemporaryDirectory);

                if (status != DicomCommandStatusType.Success)
                {
                    return(status);
                }

                response.Initialize(DicomClassType.Undefined, DicomDataSetInitializeFlags.ExplicitVR | DicomDataSetInitializeFlags.LittleEndian);
                switch (level.ToUpper())
                {
                case "PATIENT":
                    response.InsertElementAndSetValue(DicomTag.QueryRetrieveLevel, "PATIENT");

                    //
                    // Required Keys
                    //
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.PatientBirthDate);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.PatientBirthTime);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.PatientSex);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.EthnicGroup);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.PatientComments);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.NumberOfPatientRelatedStudies);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.NumberOfPatientRelatedSeries);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.NumberOfPatientRelatedInstances);
                    status = DB.FindPatient(this, Module.ServerInfo.ConnectionString, Request, response);
                    break;

                case "STUDY":
                    response.InsertElementAndSetValue(DicomTag.QueryRetrieveLevel, "STUDY");

                    // Required Keys
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.StudyDate);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.StudyTime);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.AccessionNumber);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.StudyID);

                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.PatientName);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.PatientID);

                    // Optional Keys
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.StudyDescription);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.ReferringPhysicianName);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.NumberOfStudyRelatedSeries);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.NumberOfStudyRelatedInstances);
                    status = DB.FindStudy(this, Module.ServerInfo.ConnectionString, Request, response);
                    break;

                case "SERIES":
                    response.InsertElementAndSetValue(DicomTag.QueryRetrieveLevel, "SERIES");

                    // Required Keys
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.StudyInstanceUID);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.Modality);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.SeriesNumber);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.SeriesDate);

                    // Optional Keys
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.SeriesDescription);
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.NumberOfSeriesRelatedInstances);
                    status = DB.FindSeries(this, AffectedClass, Module.ServerInfo.ConnectionString, Request, response);
                    break;

                case "IMAGE":
                    response.InsertElementAndSetValue(DicomTag.QueryRetrieveLevel, "IMAGE");

                    // Required Keys
                    ExtensionMethods.InsertKeyElement(response, Request, DicomTag.InstanceNumber);
                    status = DB.FindImage(this, AffectedClass, Module.ServerInfo.ConnectionString, Request, response);
                    break;

                default:
                    return(DicomCommandStatusType.InvalidAttributeValue);
                }
                return(status);
            }
            catch (Exception)
            {
                return(DicomCommandStatusType.ProcessingFailure);
            }
        }