Exemplo n.º 1
0
		public override PriorStudyFinderResult FindPriorStudies()
		{
			_cancel = false;
			var results = new Dictionary<string, StudyItem>();

			IPatientReconciliationStrategy reconciliationStrategy = new DefaultPatientReconciliationStrategy();
			reconciliationStrategy.SetStudyTree(Viewer.StudyTree);

			var patientIds = new Dictionary<string, string>();
			foreach (Patient patient in Viewer.StudyTree.Patients)
			{
				if (_cancel)
					break;

				IPatientData reconciled = reconciliationStrategy.ReconcileSearchCriteria(patient);
				patientIds[reconciled.PatientId] = reconciled.PatientId;
			}

		    int failedCount = 0;
		    int successCount = 0;

            foreach (var priorsServer in ServerDirectory.GetPriorsServers(true))
		    {
                if (_cancel)
                    break;

		        try
		        {
                    using (var bridge = new StudyRootQueryBridge(priorsServer.GetService<IStudyRootQuery>()))
                    {
                        foreach (string patientId in patientIds.Keys)
                        {
                            var identifier = new StudyRootStudyIdentifier { PatientId = patientId };

                            IList<StudyRootStudyIdentifier> studies = bridge.StudyQuery(identifier);
                            
                            Platform.Log(LogLevel.Debug, "Found {0} prior studies on server '{1}'", studies.Count, priorsServer.Name);

                            foreach (StudyRootStudyIdentifier study in studies)
                            {
                                if (_cancel)
                                    break;

                                //Eliminate false positives right away.
                                IPatientData reconciled = reconciliationStrategy.ReconcilePatientInformation(study);
                                if (reconciled == null)
                                    continue;

                                StudyItem studyItem = ConvertToStudyItem(study);
                                if (studyItem == null || results.ContainsKey(studyItem.StudyInstanceUid))
                                    continue;

                                if (!results.ContainsKey(studyItem.StudyInstanceUid))
                                    results[studyItem.StudyInstanceUid] = studyItem;
                            }
                        }
                    }

		            ++successCount;
		        }
		        catch (Exception e)
		        {
		            ++failedCount;
                    Platform.Log(LogLevel.Error, e, "Failed to query server: {0}", priorsServer.Name);
		        }
		    }

            if (_cancel)
            {
                //Just pretend the query never happened.
                return new PriorStudyFinderResult(new StudyItemList(), true);
            }

            if (failedCount > 0)
            {
                PriorStudyLoaderExceptionPolicy.NotifyFailedQuery();

                if (successCount == 0)
                    throw new Exception("The search for prior studies has failed.");
            }
            else
            {
                //Even if success count is zero, we'll still consider it "successful".
                PriorStudyLoaderExceptionPolicy.NotifySuccessfulQuery();
            }

            Platform.Log(LogLevel.Debug, "Found {0} prior studies in total.", results.Count);

            return new PriorStudyFinderResult(new StudyItemList(results.Values), failedCount == 0);
        }
Exemplo n.º 2
0
        public override PriorStudyFinderResult FindPriorStudies()
        {
            _cancel = false;
            var results = new Dictionary <string, StudyItem>();

            IPatientReconciliationStrategy reconciliationStrategy = new DefaultPatientReconciliationStrategy();

            reconciliationStrategy.SetStudyTree(Viewer.StudyTree);

            var patientIds = new Dictionary <string, string>();

            foreach (Patient patient in Viewer.StudyTree.Patients)
            {
                if (_cancel)
                {
                    break;
                }

                IPatientData reconciled = reconciliationStrategy.ReconcileSearchCriteria(patient);
                patientIds[reconciled.PatientId] = reconciled.PatientId;
            }

            int failedCount  = 0;
            int successCount = 0;

            foreach (var priorsServer in ServerDirectory.GetPriorsServers(true))
            {
                if (_cancel)
                {
                    break;
                }

                try
                {
                    using (var bridge = new StudyRootQueryBridge(priorsServer.GetService <IStudyRootQuery>()))
                    {
                        foreach (string patientId in patientIds.Keys)
                        {
                            //#10790: don't search for priors if patient id is empty
                            if (string.IsNullOrEmpty(patientId))
                            {
                                continue;
                            }

                            var identifier = new StudyRootStudyIdentifier {
                                PatientId = patientId
                            };

                            IList <StudyRootStudyIdentifier> studies = bridge.StudyQuery(identifier);

                            Platform.Log(LogLevel.Debug, "Found {0} prior studies on server '{1}'", studies.Count, priorsServer.Name);

                            foreach (StudyRootStudyIdentifier study in studies)
                            {
                                if (_cancel)
                                {
                                    break;
                                }

                                //Eliminate false positives right away.
                                IPatientData reconciled = reconciliationStrategy.ReconcilePatientInformation(study);
                                if (reconciled == null)
                                {
                                    continue;
                                }

                                StudyItem studyItem = ConvertToStudyItem(study);
                                if (studyItem == null || results.ContainsKey(studyItem.StudyInstanceUid))
                                {
                                    continue;
                                }

                                if (!results.ContainsKey(studyItem.StudyInstanceUid))
                                {
                                    results[studyItem.StudyInstanceUid] = studyItem;
                                }
                            }
                        }
                    }

                    ++successCount;
                }
                catch (Exception e)
                {
                    ++failedCount;
                    Platform.Log(LogLevel.Error, e, "Failed to query server: {0}", priorsServer.Name);
                }
            }

            if (_cancel)
            {
                //Just pretend the query never happened.
                return(new PriorStudyFinderResult(new StudyItemList(), true));
            }

            if (failedCount > 0)
            {
                PriorStudyLoaderExceptionPolicy.NotifyFailedQuery();

                if (successCount == 0)
                {
                    throw new Exception("The search for prior studies has failed.");
                }
            }
            else
            {
                //Even if success count is zero, we'll still consider it "successful".
                PriorStudyLoaderExceptionPolicy.NotifySuccessfulQuery();
            }

            Platform.Log(LogLevel.Debug, "Found {0} prior studies in total.", results.Count);

            return(new PriorStudyFinderResult(new StudyItemList(results.Values), failedCount == 0));
        }
Exemplo n.º 3
0
        public override PriorStudyFinderResult FindPriorStudies()
		{
			_cancel = false;

            var priorsServerQueries = new List<IStudyRootQuery>();
            foreach (ServerPartition partition in ServerPartitionMonitor.Instance)
            {
                using (IReadContext ctx = PersistentStoreRegistry.GetDefaultStore().OpenReadContext())
                {
                    IDeviceEntityBroker broker = ctx.GetBroker<IDeviceEntityBroker>();
                    DeviceSelectCriteria criteria = new DeviceSelectCriteria();
                    criteria.DeviceTypeEnum.EqualTo(DeviceTypeEnum.PriorsServer);
                    criteria.ServerPartitionKey.EqualTo(partition.Key);
                    IList<Device> list = broker.Find(criteria);
                    foreach (Device theDevice in list)
                    {
                        // Check the settings and log for debug purposes
                        if (!theDevice.Enabled)
                        {
                            Platform.Log(LogLevel.Debug, "Prior Server '{0}' on partition '{1}' is disabled in the device setting",
                                    theDevice.AeTitle, partition.AeTitle);
                            continue;
                        }

                        DicomStudyRootQuery remoteQuery = new DicomStudyRootQuery(partition.AeTitle, theDevice.AeTitle,
                                                                                  theDevice.IpAddress, theDevice.Port);
                        priorsServerQueries.Add(remoteQuery);
                    }
                }
            }

            // Log the prior servers for debug purpose
            if (Platform.IsLogLevelEnabled(LogLevel.Debug) && priorsServerQueries.Count > 0)
            {
                StringBuilder log = new StringBuilder();
                log.Append("Searching for priors on the following servers:");

                StringBuilder serverList = new StringBuilder();
                foreach (DicomStudyRootQuery server in priorsServerQueries)
                {
                    if (serverList.Length > 0)
                        serverList.Append(",");
                    serverList.AppendFormat("{0}", server);
                }

                log.Append(serverList.ToString());
                Platform.Log(LogLevel.Debug, log.ToString());
            }

            StudyItemList results = new StudyItemList();

			DefaultPatientReconciliationStrategy reconciliationStrategy = new DefaultPatientReconciliationStrategy();
			List<string> patientIds = new List<string>();
			foreach (Patient patient in Viewer.StudyTree.Patients)
			{
				if (_cancel)
					break;

				IPatientData reconciled = reconciliationStrategy.ReconcileSearchCriteria(patient);
				if (!patientIds.Contains(reconciled.PatientId))
					patientIds.Add(reconciled.PatientId);
			}

            //Note: we don't catch the exception for this one because it's just querying the other
            //partitions, so if it fails we want an outright failure to occur.  Besides, it should never happen
            //if the server is functioning correctly.
			using (StudyRootQueryBridge bridge = new StudyRootQueryBridge(Platform.GetService<IStudyRootQuery>()))
			{
				foreach (string patientId in patientIds)
				{
					StudyRootStudyIdentifier identifier = new StudyRootStudyIdentifier {PatientId = patientId};

				    IList<StudyRootStudyIdentifier> studies = bridge.StudyQuery(identifier);
					foreach (StudyRootStudyIdentifier study in studies)
					{
						if (_cancel)
							break;

						StudyItem studyItem = ConvertToStudyItem(study);
						if (studyItem != null)
							results.Add(studyItem);
					}
				}
			}

            bool complete = true;
            foreach (IStudyRootQuery query in priorsServerQueries)
            {
                foreach (string patientId in patientIds)
                {

                    try
                    {
                        var identifier = new StudyRootStudyIdentifier
                                             {
                                                 PatientId = patientId
                                             };

                        IList<StudyRootStudyIdentifier> list = query.StudyQuery(identifier);
                        foreach (StudyRootStudyIdentifier i in list)
                        {
                            if (_cancel)
                                break;

                            StudyItem studyItem = ConvertToStudyItem(i);
                            if (studyItem != null)
                                results.Add(studyItem);
                        }
                    }
                    catch (FaultException<DataValidationFault> ex)
                    {
                        complete = false;
                        Platform.Log(LogLevel.Error, ex, "An error has occurred when searching for prior studies on server '{0}'", query.ToString());
                    }
                    catch (FaultException<QueryFailedFault> ex)
                    {
                        complete = false;
                        Platform.Log(LogLevel.Error, ex, "An error has occurred when searching for prior studies on server '{0}'", query.ToString());
                    }
                }
            }

            return new PriorStudyFinderResult(results, complete);
		}