Beispiel #1
0
        protected override int OnStart(StudyLoaderArgs studyLoaderArgs)
        {
            _sops = null;

            EventResult result          = EventResult.Success;
            var         loadedInstances = new AuditedInstances();

            try
            {
                using (var context = new DataAccessContext())
                {
                    IStudy study = context.GetStudyBroker().GetStudy(studyLoaderArgs.StudyInstanceUid);
                    if (study == null)
                    {
                        result = EventResult.MajorFailure;
                        loadedInstances.AddInstance(studyLoaderArgs.StudyInstanceUid);
                        throw new NotFoundLoadStudyException(studyLoaderArgs.StudyInstanceUid);
                    }
                    loadedInstances.AddInstance(study.PatientId, study.PatientsName, study.StudyInstanceUid);

                    _sops = study.GetSopInstances().GetEnumerator();
                    return(study.NumberOfStudyRelatedInstances);
                }
            }
            finally
            {
                AuditHelper.LogOpenStudies(new[] { AuditHelper.LocalAETitle }, loadedInstances, EventSource.CurrentUser, result);
            }
        }
Beispiel #2
0
        public void TestGetReindexStudies()
        {
            DeleteAllStudies();

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();
                broker.AddStudy(CreateStudy("12.3", "A123"));
                broker.AddStudy(CreateStudy("45.6", "A456", deleted: true, reindex: true));
                broker.AddStudy(CreateStudy("78.9", "A789", reindex: true));
                broker.AddStudy(CreateStudy("", "A", deleted: true));

                context.Commit();
            }

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                var studies = broker.GetReindexStudies();

                Assert.AreEqual(2, studies.Count);
                Assert.IsTrue(studies.Any(s => s.StudyInstanceUid == "45.6"), "Missing study 45.6");
                Assert.IsTrue(studies.Any(s => s.StudyInstanceUid == "78.9"), "Missing study 78.9");
            }
        }
Beispiel #3
0
        protected override void OnExecute(CommandProcessor theProcessor)
        {
            if (Context.ContextStudy == null)
            {
                var broker = DataAccessContext.GetStudyBroker();
                Context.ContextStudy = broker.GetStudy(_studyInstanceUid);

                if (Context.ContextStudy == null)
                {
                    // This is a bit of a hack to handle batch processing of studies
                    Context.ContextStudy = _location.Study;
                    broker.AddStudy(Context.ContextStudy);
                }
            }

            //Only update the store time if the study is actively being received/imported.
            if (_reason == UpdateReason.LiveImport || Context.ContextStudy.StoreTime == null)
            {
                Context.ContextStudy.StoreTime = Platform.Time;
            }

            if (_reason != UpdateReason.SopsDeleted)
            {
                //Only update these if the study is being updated in an "additive" way (import/receive/re-index).
                //A series deletion, for example, should not update these.
                Context.ContextStudy.Deleted = false;
                Context.ContextStudy.Reindex = false;
            }

            Context.ContextStudy.Update(_studyXml);
        }
            private void ApplyDefaultDeletionRule(RulesEngineOptions context, StudyEntry study)
            {
                if (!context.ApplyDeleteActions)
                {
                    return;
                }

                // TODO (CR Jun 2012): Again, seem to use "work item" mutex for all database updates. Should just pass in a boolean.
                using (var dac = new DataAccessContext(DataAccessContext.WorkItemMutex))
                {
                    var broker  = dac.GetStudyBroker();
                    var dbStudy = broker.GetStudy(study.Study.StudyInstanceUid);

                    var storageConfiguration = StudyStore.GetConfiguration();
                    var defaultRule          = storageConfiguration.DefaultDeletionRule;
                    if (defaultRule.Enabled)
                    {
                        dbStudy.SetDeleteTime(defaultRule.TimeValue, defaultRule.TimeUnit, TimeOrigin.ReceivedDate, false);
                    }
                    else
                    {
                        dbStudy.ClearDeleteTime();
                    }

                    dac.Commit();
                }
            }
Beispiel #5
0
        public void TestGetStudies()
        {
            DeleteAllStudies();

            long oid123, oid456, oid789, oidBlank;

            using (var context = new DataAccessContext())
            {
                Study study123, study456, study789, studyBlank;

                var broker = context.GetStudyBroker();
                broker.AddStudy(study123   = CreateStudy("12.3", "A123"));
                broker.AddStudy(study456   = CreateStudy("45.6", "A456", deleted: true));
                broker.AddStudy(study789   = CreateStudy("78.9", "A789"));
                broker.AddStudy(studyBlank = CreateStudy("", "A"));

                context.Commit();

                oid123   = study123.Oid;
                oid456   = study456.Oid;
                oid789   = study789.Oid;
                oidBlank = studyBlank.Oid;
            }

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                var studies = broker.GetStudies();

                Assert.AreEqual(new[] { oid123, oid456, oid789, oidBlank }.OrderBy(x => x), studies.Select(x => x.Oid).OrderBy(x => x));
            }
        }
Beispiel #6
0
        private void ResetReindexStudies()
        {
            var resetStudyUids = new List <string>();

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var studyBroker = context.GetStudyBroker();

                var studyList = studyBroker.GetReindexStudies();
                foreach (var study in studyList)
                {
                    if (study.Reindex)
                    {
                        resetStudyUids.Add(study.StudyInstanceUid);
                        study.Reindex = false;
                    }
                }
                context.Commit();
            }

            if (resetStudyUids.Count > 0)
            {
                EventsHelper.Fire(_studiesRestoredEvent, this, new StudiesEventArgs {
                    StudyInstanceUids = resetStudyUids
                });
            }
        }
        protected override int OnStart(StudyLoaderArgs studyLoaderArgs)
        {
            _sops = null;

            EventResult result          = EventResult.Success;
            var         loadedInstances = new AuditedInstances();

            try
            {
                using (var context = new DataAccessContext())
                {
                    if (!studyLoaderArgs.Options.IgnoreInUse)
                    {
                        var workItemStatusFilter = WorkItemStatusFilter.StatusIn(
                            WorkItemStatusEnum.Pending,
                            WorkItemStatusEnum.InProgress,
                            WorkItemStatusEnum.Idle,
                            WorkItemStatusEnum.Failed);

                        var updateItems = context.GetWorkItemBroker().GetWorkItems(
                            WorkItemConcurrency.StudyUpdate,
                            workItemStatusFilter,
                            studyLoaderArgs.StudyInstanceUid);

                        var deleteItems = context.GetWorkItemBroker().GetWorkItems(
                            WorkItemConcurrency.StudyDelete,
                            workItemStatusFilter,
                            studyLoaderArgs.StudyInstanceUid);

                        var updateTriggerItems = context.GetWorkItemBroker().GetWorkItems(
                            WorkItemConcurrency.StudyUpdateTrigger,
                            workItemStatusFilter,
                            studyLoaderArgs.StudyInstanceUid);

                        if (updateItems.Any() || deleteItems.Any() || updateTriggerItems.Any())
                        {
                            var message = string.Format("There are work items actively modifying the study with UID '{0}'.", studyLoaderArgs.StudyInstanceUid);
                            throw new InUseLoadStudyException(studyLoaderArgs.StudyInstanceUid, message);
                        }
                    }

                    IStudy study = context.GetStudyBroker().GetStudy(studyLoaderArgs.StudyInstanceUid);
                    if (study == null)
                    {
                        result = EventResult.MajorFailure;
                        loadedInstances.AddInstance(studyLoaderArgs.StudyInstanceUid);
                        throw new NotFoundLoadStudyException(studyLoaderArgs.StudyInstanceUid);
                    }
                    loadedInstances.AddInstance(study.PatientId, study.PatientsName, study.StudyInstanceUid);

                    _sops = study.GetSopInstances().GetEnumerator();
                    return(study.NumberOfStudyRelatedInstances);
                }
            }
            finally
            {
                AuditHelper.LogOpenStudies(new[] { AuditHelper.LocalAETitle }, loadedInstances, EventSource.CurrentUser, result);
            }
        }
Beispiel #8
0
 protected override IList <Study> GetNextBatch(int batchSize)
 {
     using (var context = new DataAccessContext())
     {
         var broker = context.GetStudyBroker();
         return(broker.GetStudiesForDeletion(Platform.Time, batchSize));
     }
 }
Beispiel #9
0
 private static void DeleteAllStudies()
 {
     using (var context = new DataAccessContext())
     {
         var broker = context.GetStudyBroker();
         broker.DeleteAll();
         context.Commit();
     }
 }
        /// <summary>
        /// Initialize the Reapply Rules.  Loast a list of studies.
        /// </summary>
        public void Initialize()
        {
            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var broker = context.GetStudyBroker();

                StudyOidList = broker.GetStudyOids();
            }

            DatabaseStudiesToScan = StudyOidList.Count;
        }
 private void CheckDeleteStudyCanceled(DataAccessContext context, WorkItem workItem)
 {
     // Force the study to be visible again if its a DeleteStudyRequest we're canceling
     if (workItem.Type.Equals(DeleteStudyRequest.WorkItemTypeString))
     {
         var studyBroker = context.GetStudyBroker();
         var study       = studyBroker.GetStudy(workItem.StudyInstanceUid);
         if (study != null)
         {
             study.Deleted = false;
         }
     }
 }
        protected Study LoadRelatedStudy()
        {
            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                if (!string.IsNullOrEmpty(Proxy.Item.StudyInstanceUid))
                {
                    return(broker.GetStudy(Proxy.Item.StudyInstanceUid));
                }

                return(null);
            }
        }
        private bool CheckIfStudyExists()
        {
            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();
                var study  = broker.GetStudy(Location.Study.StudyInstanceUid);

                if (study != null)
                {
                    Location.Study = study;
                    return(true);
                }
            }
            return(false);
        }
Beispiel #14
0
        public void Initialize(StudyLocation location)
        {
            _location = location;

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var studyBroker = context.GetStudyBroker();
                var study       = studyBroker.GetStudy(_location.Study.StudyInstanceUid);
                if (study != null)
                {
                    _location.Study = study;
                    if (study.NumberOfStudyRelatedInstances.HasValue)
                    {
                        NumberOfStudyRelatedInstances = study.NumberOfStudyRelatedInstances.Value;
                    }
                }
            }
        }
        private void ProcessStudiesInDatabase()
        {
            var rulesEngine = RulesEngine.Create();

            foreach (var oid in StudyOidList)
            {
                try
                {
                    // TODO (CR Jun 2012): We don't modify any work items - do we need the mutex?
                    using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
                    {
                        var broker = context.GetStudyBroker();

                        var study = broker.GetStudy(oid);


                        var studyEntry         = study.ToStoreEntry();
                        var rulesEngineOptions = new RulesEngineOptions
                        {
                            ApplyDeleteActions = _request.ApplyDeleteActions,
                            ApplyRouteActions  = _request.ApplyRouteActions
                        };
                        if (!string.IsNullOrEmpty(_request.RuleId))
                        {
                            rulesEngine.ApplyStudyRule(studyEntry, _request.RuleId, rulesEngineOptions);
                        }
                        else
                        {
                            rulesEngine.ApplyStudyRules(studyEntry, rulesEngineOptions);
                        }

                        EventsHelper.Fire(_studyProcessedEvent, this, new StudyEventArgs {
                            StudyInstanceUid = study.StudyInstanceUid
                        });
                    }
                }
                catch (Exception x)
                {
                    Platform.Log(LogLevel.Warn, "Unexpected exception attempting to reapply rules for StudyOid {0}: {1}", oid, x.Message);
                }
            }
        }
Beispiel #16
0
        private void ProcessStudiesInDatabase()
        {
            foreach (long oid in StudyOidList)
            {
                try
                {
                    using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
                    {
                        var broker = context.GetStudyBroker();
                        var study  = broker.GetStudy(oid);

                        var location = new StudyLocation(study.StudyInstanceUid);
                        if (!Directory.Exists(location.StudyFolder))
                        {
                            broker.Delete(study);
                            context.Commit();

                            EventsHelper.Fire(_studyDeletedEvent, this, new StudyEventArgs {
                                StudyInstanceUid = study.StudyInstanceUid
                            });
                            Platform.Log(LogLevel.Info, "Deleted Study that wasn't on disk, but in the database: {0}",
                                         study.StudyInstanceUid);
                        }
                        else
                        {
                            EventsHelper.Fire(_studyProcessedEvent, this, new StudyEventArgs {
                                StudyInstanceUid = study.StudyInstanceUid
                            });
                        }
                    }
                }
                catch (Exception x)
                {
                    Platform.Log(LogLevel.Warn, "Unexpected exception attempting to reindex StudyOid {0}: {1}", oid, x.Message);
                }

                if (_cancelRequested)
                {
                    return;
                }
            }
        }
Beispiel #17
0
        protected override void OnExecute(CommandProcessor theProcessor)
        {
            if (Context.ContextStudy == null)
            {
                var broker = DataAccessContext.GetStudyBroker();
                Context.ContextStudy = broker.GetStudy(_studyInstanceUid);

                if (Context.ContextStudy == null)
                {
                    // This is a bit of a hack to handle batch processing of studies
                    Context.ContextStudy = _location.Study;
                    broker.AddStudy(Context.ContextStudy);
                }
            }

            //Only update the store time if the study is actively being received/imported.
            if (_reason == UpdateReason.LiveImport || Context.ContextStudy.StoreTime == null)
            {
                Context.ContextStudy.StoreTime = Platform.Time;
            }

            if (_reason != UpdateReason.SopsDeleted)
            {
                //Only update these if the study is being updated in an "additive" way (import/receive/re-index).
                //A series deletion, for example, should not update these.
                Context.ContextStudy.Deleted = false;
                Context.ContextStudy.Reindex = false;
            }

            Context.ContextStudy.Update(_studyXml);

            // TODO (2014-01-11) Rigel - Deal with this better in the database, converted due to ticket #11593
            if (Context.ContextStudy.StudyDate < (DateTime)SqlDateTime.MinValue)
            {
                Context.ContextStudy.StudyDate = null;
            }
            if (Context.ContextStudy.PatientsBirthDate < (DateTime)SqlDateTime.MinValue)
            {
                Context.ContextStudy.PatientsBirthDate = null;
            }
        }
Beispiel #18
0
        public void TestGetStudiesForDeletion()
        {
            DeleteAllStudies();

            var time       = DateTime.Now.Truncate();
            var pastTime   = time.AddMinutes(-30);
            var futureTime = time.AddMinutes(30);

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();
                broker.AddStudy(CreateStudy("12.3", "A123"));
                broker.AddStudy(CreateStudy("45.6", "A456", deleted: true, deleteTime: pastTime));
                broker.AddStudy(CreateStudy("78.9", "A789", deleteTime: pastTime.AddMinutes(-1)));
                broker.AddStudy(CreateStudy("89.0", "A890", deleteTime: futureTime));
                broker.AddStudy(CreateStudy("", "A", deleted: true));

                context.Commit();
            }

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                var deletableOld    = broker.GetStudiesForDeletion(time, 5);
                var deletableOldest = broker.GetStudiesForDeletion(time, 1).SingleOrDefault();

                var deletableAny = broker.GetStudiesForDeletion(futureTime.AddMinutes(100), 9);

                Assert.AreEqual(1, deletableOld.Count);
                Assert.AreEqual("78.9", deletableOld[0].StudyInstanceUid, "Missing deletable old study 78.9");

                Assert.IsNotNull(deletableOldest, "Missing oldest deletable");
                Assert.AreEqual("78.9", deletableOldest.StudyInstanceUid, "missing deletable oldest study");

                Assert.AreEqual(2, deletableAny.Count);
                Assert.AreEqual("78.9", deletableAny[0].StudyInstanceUid, "Missing deletable old study 78.9");
                Assert.AreEqual("89.0", deletableAny[1].StudyInstanceUid, "Missing deletable newer study 89.0");
            }
        }
Beispiel #19
0
        public bool Process()
        {
            // Decided not to use the command processor here, since we're just removing everything and want to be as forgiving as possible.
            try
            {
                DirectoryUtility.DeleteIfExists(_location.StudyFolder);
            }
            catch (Exception e)
            {
                Platform.Log(LogLevel.Error, e, "Unable to delete study folder: {0}", _location.StudyFolder);
            }

            try
            {
                using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
                {
                    var studyBroker = context.GetStudyBroker();
                    var study       = studyBroker.GetStudy(_location.Study.StudyInstanceUid);
                    if (study != null)
                    {
                        studyBroker.Delete(study);
                    }

                    context.Commit();
                }

                Platform.Log(LogLevel.Info, "Deleted study for: {0}:{1}", _location.Study.PatientsName, _location.Study.PatientId);
                return(true);
            }

            catch (Exception e)
            {
                Platform.Log(LogLevel.Error, e,
                             "Unexpected exception when {0} deleting Study related database entries for study: {0}",
                             _location.Study.StudyInstanceUid);

                return(false);
            }
        }
Beispiel #20
0
        public void TestGetStudyByUid()
        {
            DeleteAllStudies();

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();
                broker.AddStudy(CreateStudy("12.3", "A123"));
                broker.AddStudy(CreateStudy("45.6", "A456", deleted: true));
                broker.AddStudy(CreateStudy("78.9", "A789"));
                broker.AddStudy(CreateStudy("", "A"));

                context.Commit();
            }

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                var study456   = broker.GetStudy("45.6");
                var study123   = broker.GetStudy("12.3");
                var study120   = broker.GetStudy("12.0");
                var study890   = broker.GetStudy("89.0");
                var studyBlank = broker.GetStudy("");

                Assert.IsNotNull(study456);
                Assert.AreEqual(study456.StudyInstanceUid, "45.6");
                Assert.AreEqual(study456.AccessionNumber, "A456");
                Assert.IsNotNull(study123);
                Assert.AreEqual(study123.StudyInstanceUid, "12.3");
                Assert.AreEqual(study123.AccessionNumber, "A123");
                Assert.IsNull(study120);
                Assert.IsNull(study890);
                Assert.IsNotNull(studyBlank);
                Assert.AreEqual(studyBlank.StudyInstanceUid, "");
                Assert.AreEqual(studyBlank.AccessionNumber, "A");
            }
        }
Beispiel #21
0
        /// <summary>
        /// Initialize the Reindex.  Determine the number of studies in the database and the number of folders on disk to be used
        /// for progress.
        /// </summary>
        public void Initialize()
        {
            // Before scanning the study folders, cleanup any empty directories.
            CleanupFilestoreDirectory();

            try
            {
                DirectoryList = new List <string>(Directory.GetDirectories(FilestoreDirectory));
            }
            catch (Exception x)
            {
                Platform.Log(LogLevel.Error, x);
                throw;
            }

            StudyFoldersToScan = DirectoryList.Count;

            // TODO (CR Jun 2012): Seems we're using the "work item" mutex for all updates to the database.
            // Should we just pass in a boolean specifying whether or not to use a mutex?
            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var broker = context.GetStudyBroker();

                StudyOidList = new List <long>();

                var studyList = broker.GetStudies();
                foreach (var study in studyList)
                {
                    study.Reindex = true;
                    StudyOidList.Add(study.Oid);
                }
                context.Commit();
            }

            DatabaseStudiesToScan = StudyOidList.Count;

            _threadPool.Start();
        }
Beispiel #22
0
        private static WorkItem ProcessStudy(long studyOid, DataAccessContext context)
        {
            var studyBroker = context.GetStudyBroker();
            var study       = studyBroker.GetStudy(studyOid);

            // on the off chance it was deleted by another thread/process, there's nothing to do here
            if (study.Deleted)
            {
                return(null);
            }

            // TODO (Marmot): this code was duplicated from DeleteClient.DeleteStudy
            var now  = Platform.Time;
            var item = new WorkItem
            {
                Request = new DeleteStudyRequest
                {
                    Study   = new WorkItemStudy(study),
                    Patient = new WorkItemPatient(study)
                },
                Type             = DeleteStudyRequest.WorkItemTypeString,
                Priority         = WorkItemPriorityEnum.Normal,
                ScheduledTime    = now.AddSeconds(WorkItemServiceSettings.Default.InsertDelaySeconds),
                ProcessTime      = now.AddSeconds(WorkItemServiceSettings.Default.InsertDelaySeconds),
                DeleteTime       = now.AddMinutes(WorkItemServiceSettings.Default.DeleteDelayMinutes),
                ExpirationTime   = now.AddSeconds(WorkItemServiceSettings.Default.ExpireDelaySeconds),
                RequestedTime    = now,
                Status           = WorkItemStatusEnum.Pending,
                StudyInstanceUid = study.StudyInstanceUid
            };

            var workItemBroker = context.GetWorkItemBroker();

            workItemBroker.AddWorkItem(item);
            study.Deleted = true;
            return(item);
        }
Beispiel #23
0
        public void TestGetStudyCount()
        {
            DeleteAllStudies();

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();
                broker.AddStudy(CreateStudy("12.3", "A123"));
                broker.AddStudy(CreateStudy("45.6", "A456", deleted: true));
                broker.AddStudy(CreateStudy("78.9", "A789"));
                broker.AddStudy(CreateStudy("", "A"));

                context.Commit();
            }

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                var count = broker.GetStudyCount();

                Assert.AreEqual(4, count);
            }
        }
		public void TestGetStudiesForDeletion()
		{
			DeleteAllStudies();

			var time = DateTime.Now.Truncate();
			var pastTime = time.AddMinutes(-30);
			var futureTime = time.AddMinutes(30);

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();
				broker.AddStudy(CreateStudy("12.3", "A123"));
				broker.AddStudy(CreateStudy("45.6", "A456", deleted : true, deleteTime : pastTime));
				broker.AddStudy(CreateStudy("78.9", "A789", deleteTime : pastTime.AddMinutes(-1)));
				broker.AddStudy(CreateStudy("89.0", "A890", deleteTime : futureTime));
				broker.AddStudy(CreateStudy("", "A", deleted : true));

				context.Commit();
			}

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();

				var deletableOld = broker.GetStudiesForDeletion(time, 5);
				var deletableOldest = broker.GetStudiesForDeletion(time, 1).SingleOrDefault();

				var deletableAny = broker.GetStudiesForDeletion(futureTime.AddMinutes(100), 9);

				Assert.AreEqual(1, deletableOld.Count);
				Assert.AreEqual("78.9", deletableOld[0].StudyInstanceUid, "Missing deletable old study 78.9");

				Assert.IsNotNull(deletableOldest, "Missing oldest deletable");
				Assert.AreEqual("78.9", deletableOldest.StudyInstanceUid, "missing deletable oldest study");

				Assert.AreEqual(2, deletableAny.Count);
				Assert.AreEqual("78.9", deletableAny[0].StudyInstanceUid, "Missing deletable old study 78.9");
				Assert.AreEqual("89.0", deletableAny[1].StudyInstanceUid, "Missing deletable newer study 89.0");
			}
		}
        /// <summary>
        /// Called on startup to reset InProgress WorkItems back to Pending.
        /// </summary>
        private void ResetInProgressWorkItems()
        {
            bool reindexInProgress = false;

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var workItemBroker = context.GetWorkItemBroker();
                var list           = workItemBroker.GetWorkItems(null, WorkItemStatusEnum.InProgress, null);

                foreach (var item in list)
                {
                    item.Status = WorkItemStatusEnum.Pending;
                    if (item.Type.Equals(ReindexRequest.WorkItemTypeString))
                    {
                        reindexInProgress = true;
                    }
                }

                context.Commit();
            }

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var workItemBroker = context.GetWorkItemBroker();
                var list           = workItemBroker.GetWorkItems(null, WorkItemStatusEnum.DeleteInProgress, null);

                foreach (var item in list)
                {
                    item.Status = WorkItemStatusEnum.Deleted;
                }

                context.Commit();
            }

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var workItemBroker = context.GetWorkItemBroker();
                var list           = workItemBroker.GetWorkItems(null, WorkItemStatusEnum.Canceling, null);

                foreach (var item in list)
                {
                    item.Status = WorkItemStatusEnum.Canceled;
                    if (item.Type.Equals(ReindexRequest.WorkItemTypeString))
                    {
                        reindexInProgress = true;
                    }
                }

                context.Commit();
            }

            if (reindexInProgress)
            {
                using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
                {
                    var studyBroker = context.GetStudyBroker();
                    var studyList   = studyBroker.GetReindexStudies();

                    foreach (var item in studyList)
                    {
                        item.Reindex = false;
                    }

                    context.Commit();
                }
            }
        }
		public void TestAddAndGet()
		{
			DeleteAllStudies();

			var time = DateTime.Now.Truncate();

			long oid;
			string version;
			using (var context = new DataAccessContext())
			{
				var study = CreateStudy("123", "A#", "MRN", "BOBSON^BOB", new DateTime(2014, 07, 01), "1234", "DESU", @"CT\PT\KO\PR\SC", 6, 2101, time, true, time.AddMinutes(100));

				var broker = context.GetStudyBroker();
				broker.AddStudy(CreateStudy("456", "2"));
				broker.AddStudy(study);
				broker.AddStudy(CreateStudy("789", "3"));

				context.Commit();

				Assert.AreNotEqual(0, oid = study.Oid, "Oid should have been assigned on insert");
				Assert.IsNotNull(study.Version, "Version should have been assigned on insert");

				version = study.Version.ToString();
			}

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();

				var study = broker.GetStudy(oid);
				Assert.AreEqual("123", study.StudyInstanceUid, "StudyInstanceUid");
				Assert.AreEqual("A#", study.AccessionNumber, "AccessionNumber");
				Assert.AreEqual("MRN", study.PatientId, "PatientId");
				Assert.AreEqual("BOBSON^BOB", study.PatientsName, "PatientsName");
				Assert.AreEqual(new DateTime(2014, 07, 01), study.StudyDate, "StudyDate");
				Assert.AreEqual("20140701", study.StudyDateRaw, "StudyDateRaw");
				Assert.AreEqual("1234", study.StudyId, "StudyId");
				Assert.AreEqual("DESU", study.StudyDescription, "StudyDescription");
				Assert.AreEqual(@"CT\PT\KO\PR\SC", study.ModalitiesInStudy, "ModalitiesInStudy");
				Assert.AreEqual(6, study.NumberOfStudyRelatedSeries, "NumberOfStudyRelatedSeries");
				Assert.AreEqual(2101, study.NumberOfStudyRelatedInstances, "NumberOfStudyRelatedInstances");
				Assert.AreEqual(time, study.StoreTime, "StoreTime");
				Assert.AreEqual(true, study.Reindex, "Reindex");
				Assert.AreEqual(time.AddMinutes(100), study.DeleteTime, "DeleteTime");

				study.ModalitiesInStudy = @"CT\PT";
				context.Commit();

				Assert.AreNotEqual(version, study.Version, "Version should have been changed on update");
			}

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();

				var study = broker.GetStudy(oid);
				Assert.AreEqual("123", study.StudyInstanceUid, "StudyInstanceUid");
				Assert.AreEqual("A#", study.AccessionNumber, "AccessionNumber");
				Assert.AreEqual("MRN", study.PatientId, "PatientId");
				Assert.AreEqual("BOBSON^BOB", study.PatientsName, "PatientsName");
				Assert.AreEqual(new DateTime(2014, 07, 01), study.StudyDate, "StudyDate");
				Assert.AreEqual("20140701", study.StudyDateRaw, "StudyDateRaw");
				Assert.AreEqual("1234", study.StudyId, "StudyId");
				Assert.AreEqual("DESU", study.StudyDescription, "StudyDescription");
				Assert.AreEqual(@"CT\PT", study.ModalitiesInStudy, "ModalitiesInStudy");
				Assert.AreEqual(6, study.NumberOfStudyRelatedSeries, "NumberOfStudyRelatedSeries");
				Assert.AreEqual(2101, study.NumberOfStudyRelatedInstances, "NumberOfStudyRelatedInstances");
				Assert.AreEqual(time, study.StoreTime, "StoreTime");
				Assert.AreEqual(true, study.Reindex, "Reindex");
				Assert.AreEqual(time.AddMinutes(100), study.DeleteTime, "DeleteTime");
			}
		}
		public void TestGetStudies()
		{
			DeleteAllStudies();

			long oid123, oid456, oid789, oidBlank;
			using (var context = new DataAccessContext())
			{
				Study study123, study456, study789, studyBlank;

				var broker = context.GetStudyBroker();
				broker.AddStudy(study123 = CreateStudy("12.3", "A123"));
				broker.AddStudy(study456 = CreateStudy("45.6", "A456", deleted : true));
				broker.AddStudy(study789 = CreateStudy("78.9", "A789"));
				broker.AddStudy(studyBlank = CreateStudy("", "A"));

				context.Commit();

				oid123 = study123.Oid;
				oid456 = study456.Oid;
				oid789 = study789.Oid;
				oidBlank = studyBlank.Oid;
			}

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();

				var studies = broker.GetStudies();

				Assert.AreEqual(new[] {oid123, oid456, oid789, oidBlank}.OrderBy(x => x), studies.Select(x => x.Oid).OrderBy(x => x));
			}
		}
		public void TestGetStudyByUid()
		{
			DeleteAllStudies();

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();
				broker.AddStudy(CreateStudy("12.3", "A123"));
				broker.AddStudy(CreateStudy("45.6", "A456", deleted : true));
				broker.AddStudy(CreateStudy("78.9", "A789"));
				broker.AddStudy(CreateStudy("", "A"));

				context.Commit();
			}

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();

				var study456 = broker.GetStudy("45.6");
				var study123 = broker.GetStudy("12.3");
				var study120 = broker.GetStudy("12.0");
				var study890 = broker.GetStudy("89.0");
				var studyBlank = broker.GetStudy("");

				Assert.IsNotNull(study456);
				Assert.AreEqual(study456.StudyInstanceUid, "45.6");
				Assert.AreEqual(study456.AccessionNumber, "A456");
				Assert.IsNotNull(study123);
				Assert.AreEqual(study123.StudyInstanceUid, "12.3");
				Assert.AreEqual(study123.AccessionNumber, "A123");
				Assert.IsNull(study120);
				Assert.IsNull(study890);
				Assert.IsNotNull(studyBlank);
				Assert.AreEqual(studyBlank.StudyInstanceUid, "");
				Assert.AreEqual(studyBlank.AccessionNumber, "A");
			}
		}
        public WorkItemInsertResponse Insert(WorkItemInsertRequest request)
        {
            // TODO (CR Jun 2012): The fact that there is special processing in here for particular types of work items
            // indicates there is something wrong with the design that may make adding custom work item types difficult.
            // Maybe the different "processors" need to perform the insert, or at least have some kind of method (rule)
            // for processing the insert?

            var response = new WorkItemInsertResponse();

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                DateTime now    = Platform.Time;
                var      broker = context.GetWorkItemBroker();

                if (request.Request.WorkItemType.Equals(ReindexRequest.WorkItemTypeString))
                {
                    var list = broker.GetWorkItems(request.Request.WorkItemType, null, null);
                    foreach (var workItem in list)
                    {
                        if (workItem.Status == WorkItemStatusEnum.Pending ||
                            workItem.Status == WorkItemStatusEnum.InProgress)
                        {
                            response.Item = WorkItemDataHelper.FromWorkItem(workItem);
                            return(response);
                        }
                    }
                }

                var deleteStudyRequest = request.Request as DeleteStudyRequest;
                if (deleteStudyRequest != null)
                {
                    var list = broker.GetWorkItems(request.Request.WorkItemType, null, deleteStudyRequest.Study.StudyInstanceUid);
                    foreach (var workItem in list)
                    {
                        if (workItem.Status == WorkItemStatusEnum.Pending ||
                            workItem.Status == WorkItemStatusEnum.InProgress)
                        {
                            // Mark studies to delete as "deleted" in the database.
                            var studyBroker = context.GetStudyBroker();
                            var study       = studyBroker.GetStudy(deleteStudyRequest.Study.StudyInstanceUid);
                            if (study != null)
                            {
                                study.Deleted = true;
                                context.Commit();
                            }

                            response.Item = WorkItemDataHelper.FromWorkItem(workItem);
                            return(response);
                        }
                    }
                }

                var item = new WorkItem
                {
                    Request        = request.Request,
                    Progress       = request.Progress,
                    Type           = request.Request.WorkItemType,
                    Priority       = request.Request.Priority,
                    ScheduledTime  = now.AddSeconds(WorkItemServiceSettings.Default.InsertDelaySeconds),
                    ProcessTime    = now.AddSeconds(WorkItemServiceSettings.Default.InsertDelaySeconds),
                    DeleteTime     = now.AddMinutes(WorkItemServiceSettings.Default.DeleteDelayMinutes),
                    ExpirationTime = now.AddSeconds(WorkItemServiceSettings.Default.ExpireDelaySeconds),
                    RequestedTime  = now,
                    Status         = WorkItemStatusEnum.Pending
                };

                var studyRequest = request.Request as WorkItemStudyRequest;
                if (studyRequest != null)
                {
                    item.StudyInstanceUid = studyRequest.Study.StudyInstanceUid;

                    if (request.Request.WorkItemType.Equals(DeleteStudyRequest.WorkItemTypeString))
                    {
                        // Mark studies to delete as "deleted" in the database.
                        var studyBroker = context.GetStudyBroker();
                        var study       = studyBroker.GetStudy(studyRequest.Study.StudyInstanceUid);
                        if (study != null)
                        {
                            study.Deleted = true;
                        }
                    }
                }

                broker.AddWorkItem(item);

                context.Commit();

                response.Item = WorkItemDataHelper.FromWorkItem(item);
            }

            // Cache the UserIdentityContext for later use by the shred
            if (request.Request.WorkItemType.Equals(ImportFilesRequest.WorkItemTypeString))
            {
                UserIdentityCache.Put(response.Item.Identifier, UserIdentityContext.CreateFromCurrentThreadPrincipal());
            }

            WorkItemPublishSubscribeHelper.PublishWorkItemChanged(WorkItemsChangedEventType.Update, response.Item);
            if (WorkItemProcessor.Instance != null)
            {
                WorkItemProcessor.Instance.SignalThread();
            }

            return(response);
        }
Beispiel #30
0
        public void TestAddAndGet()
        {
            DeleteAllStudies();

            var time = DateTime.Now.Truncate();

            long   oid;
            string version;

            using (var context = new DataAccessContext())
            {
                var study = CreateStudy("123", "A#", "MRN", "BOBSON^BOB", new DateTime(2014, 07, 01), "1234", "DESU", @"CT\PT\KO\PR\SC", 6, 2101, time, true, time.AddMinutes(100));

                var broker = context.GetStudyBroker();
                broker.AddStudy(CreateStudy("456", "2"));
                broker.AddStudy(study);
                broker.AddStudy(CreateStudy("789", "3"));

                context.Commit();

                Assert.AreNotEqual(0, oid = study.Oid, "Oid should have been assigned on insert");
                Assert.IsNotNull(study.Version, "Version should have been assigned on insert");

                version = study.Version.ToString();
            }

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                var study = broker.GetStudy(oid);
                Assert.AreEqual("123", study.StudyInstanceUid, "StudyInstanceUid");
                Assert.AreEqual("A#", study.AccessionNumber, "AccessionNumber");
                Assert.AreEqual("MRN", study.PatientId, "PatientId");
                Assert.AreEqual("BOBSON^BOB", study.PatientsName, "PatientsName");
                Assert.AreEqual(new DateTime(2014, 07, 01), study.StudyDate, "StudyDate");
                Assert.AreEqual("20140701", study.StudyDateRaw, "StudyDateRaw");
                Assert.AreEqual("1234", study.StudyId, "StudyId");
                Assert.AreEqual("DESU", study.StudyDescription, "StudyDescription");
                Assert.AreEqual(@"CT\PT\KO\PR\SC", study.ModalitiesInStudy, "ModalitiesInStudy");
                Assert.AreEqual(6, study.NumberOfStudyRelatedSeries, "NumberOfStudyRelatedSeries");
                Assert.AreEqual(2101, study.NumberOfStudyRelatedInstances, "NumberOfStudyRelatedInstances");
                Assert.AreEqual(time, study.StoreTime, "StoreTime");
                Assert.AreEqual(true, study.Reindex, "Reindex");
                Assert.AreEqual(time.AddMinutes(100), study.DeleteTime, "DeleteTime");

                study.ModalitiesInStudy = @"CT\PT";
                context.Commit();

                Assert.AreNotEqual(version, study.Version, "Version should have been changed on update");
            }

            using (var context = new DataAccessContext())
            {
                var broker = context.GetStudyBroker();

                var study = broker.GetStudy(oid);
                Assert.AreEqual("123", study.StudyInstanceUid, "StudyInstanceUid");
                Assert.AreEqual("A#", study.AccessionNumber, "AccessionNumber");
                Assert.AreEqual("MRN", study.PatientId, "PatientId");
                Assert.AreEqual("BOBSON^BOB", study.PatientsName, "PatientsName");
                Assert.AreEqual(new DateTime(2014, 07, 01), study.StudyDate, "StudyDate");
                Assert.AreEqual("20140701", study.StudyDateRaw, "StudyDateRaw");
                Assert.AreEqual("1234", study.StudyId, "StudyId");
                Assert.AreEqual("DESU", study.StudyDescription, "StudyDescription");
                Assert.AreEqual(@"CT\PT", study.ModalitiesInStudy, "ModalitiesInStudy");
                Assert.AreEqual(6, study.NumberOfStudyRelatedSeries, "NumberOfStudyRelatedSeries");
                Assert.AreEqual(2101, study.NumberOfStudyRelatedInstances, "NumberOfStudyRelatedInstances");
                Assert.AreEqual(time, study.StoreTime, "StoreTime");
                Assert.AreEqual(true, study.Reindex, "Reindex");
                Assert.AreEqual(time.AddMinutes(100), study.DeleteTime, "DeleteTime");
            }
        }
		public void TestGetReindexStudies()
		{
			DeleteAllStudies();

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();
				broker.AddStudy(CreateStudy("12.3", "A123"));
				broker.AddStudy(CreateStudy("45.6", "A456", deleted : true, reindex : true));
				broker.AddStudy(CreateStudy("78.9", "A789", reindex : true));
				broker.AddStudy(CreateStudy("", "A", deleted : true));

				context.Commit();
			}

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();

				var studies = broker.GetReindexStudies();

				Assert.AreEqual(2, studies.Count);
				Assert.IsTrue(studies.Any(s => s.StudyInstanceUid == "45.6"), "Missing study 45.6");
				Assert.IsTrue(studies.Any(s => s.StudyInstanceUid == "78.9"), "Missing study 78.9");
			}
		}
Beispiel #32
0
        public void Open()
        {
            try
            {
                if (!Enabled)
                {
                    return;
                }

                int numberOfSelectedStudies = Context.SelectedStudies.Count;
                if (Context.SelectedStudies.Count == 0)
                {
                    return;
                }

                int numberOfLoadableStudies = GetNumberOfLoadableStudies();
                if (numberOfLoadableStudies != numberOfSelectedStudies)
                {
                    int    numberOfNonLoadableStudies = numberOfSelectedStudies - numberOfLoadableStudies;
                    string message;
                    if (numberOfSelectedStudies == 1)
                    {
                        message = SR.MessageCannotOpenNonStreamingStudy;
                    }
                    else
                    {
                        if (numberOfNonLoadableStudies == 1)
                        {
                            message = SR.MessageOneNonStreamingStudyCannotBeOpened;
                        }
                        else
                        {
                            message = String.Format(SR.MessageFormatXNonStreamingStudiesCannotBeOpened, numberOfNonLoadableStudies);
                        }
                    }

                    Context.DesktopWindow.ShowMessageBox(message, MessageBoxActions.Ok);
                    return;
                }

                UIStudyTree tree = new UIStudyTree();

                foreach (var item in Context.SelectedStudies)
                {
                    if (item.Server.IsLocal)
                    {
                        using (var context = new DataAccessContext())
                        {
                            IStudy study = context.GetStudyBroker().GetStudy(item.StudyInstanceUid);
                            if (study != null)
                            {
                                tree.AddStudy(study);
                            }
                        }
                    }
                }

                if (this.mediaIShelf != null)
                {
                    this.mediaIShelf.Activate();
                    component.Tree = tree.Tree;
                }
                else
                {
                    if (base.Context.DesktopWindow == null)
                    {
                        return;
                    }

                    component                = new MediaWriterComponent();
                    this.mediaIShelf         = ApplicationComponent.LaunchAsShelf(base.Context.DesktopWindow, component, "Media Writer", ShelfDisplayHint.DockAutoHide | ShelfDisplayHint.DockLeft);
                    this.mediaIShelf.Closed += new EventHandler <ClosedEventArgs>(this.ShelfClose);
                    this.mediaIShelf.Activate();
                    component.Tree = tree.Tree;
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.Report(exception, base.Context.DesktopWindow);
            }
        }
		public void TestGetStudyCount()
		{
			DeleteAllStudies();

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();
				broker.AddStudy(CreateStudy("12.3", "A123"));
				broker.AddStudy(CreateStudy("45.6", "A456", deleted : true));
				broker.AddStudy(CreateStudy("78.9", "A789"));
				broker.AddStudy(CreateStudy("", "A"));

				context.Commit();
			}

			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();

				var count = broker.GetStudyCount();

				Assert.AreEqual(4, count);
			}
		}
		private static void DeleteAllStudies()
		{
			using (var context = new DataAccessContext())
			{
				var broker = context.GetStudyBroker();
				broker.DeleteAll();
				context.Commit();
			}
		}