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);
                }
            }
        }
        private void ReprocessFolder()
        {
            try
            {
                var studyXml = Location.LoadStudyXml();
                var fileList = new List <ProcessStudyUtility.ProcessorFile>();

                // This code will cleanup a folder and move images around to the proper location.
                // It in essence allows you to just copy a bunch of files into the filestore, and reindex will clean them up and organize them.
                FileProcessor.Process(Location.StudyFolder, "*.dcm", delegate(string file, out bool cancel)
                {
                    cancel = _cancelRequested;
                    try
                    {
                        var dicomFile = new DicomFile(file);

                        dicomFile.Load(DicomReadOptions.StorePixelDataReferences | DicomReadOptions.Default);
                        String studyInstanceUid = dicomFile.DataSet[DicomTags.StudyInstanceUid].GetString(0, string.Empty);

                        if (!Location.Study.StudyInstanceUid.Equals(studyInstanceUid))
                        {
                            Platform.Log(LogLevel.Warn,
                                         "Importing file that was in the wrong study folder: {0}",
                                         file);
                            var context =
                                new ImportStudyContext(
                                    dicomFile.SourceApplicationEntityTitle, StudyStore.GetConfiguration(), EventSource.CurrentProcess);
                            var importer = new ImportFilesUtility(context);
                            var result   = importer.Import(dicomFile, BadFileBehaviourEnum.Delete, FileImportBehaviourEnum.Move);
                            if (!result.DicomStatus.Equals(DicomStatuses.Success))
                            {
                                try
                                {
                                    Platform.Log(LogLevel.Error, "Unable to import file: {0}, deleting: {1}", result.ErrorMessage, file);
                                    FileUtils.Delete(file);
                                }
                                catch (Exception x)
                                {
                                    Platform.Log(LogLevel.Warn, x, "Unexpected exception deleting file: {0}", file);
                                    Failed         = true;
                                    FailureMessage = x.Message;
                                }
                            }
                        }
                        else
                        {
                            fileList.Add(new ProcessStudyUtility.ProcessorFile(dicomFile, null));

                            if (fileList.Count > 19)
                            {
                                var p = new ProcessStudyUtility(Location)
                                {
                                    IsReprocess = true
                                };

                                p.ProcessBatch(fileList, studyXml);

                                fileList.Clear();
                            }
                        }
                    }
                    catch (Exception x)
                    {
                        Platform.Log(LogLevel.Error, "Exception when reindexing {0} files, last file: {1}: {2}", fileList.Count, file, x.Message);
                        fileList.Clear();                                            // Clear out the failed entries
                        Failed         = true;
                        FailureMessage = x.Message;
                    }
                }, true);
                if (fileList.Count > 0)
                {
                    var p = new ProcessStudyUtility(Location)
                    {
                        IsReprocess = true
                    };

                    p.ProcessBatch(fileList, studyXml);

                    // Now apply Deletion rules

                    var ruleContext = new RulesEngineOptions
                    {
                        ApplyDeleteActions = true,
                        ApplyRouteActions  = false
                    };
                    RulesEngine.Create().ApplyStudyRules(p.StudyLocation.Study.ToStoreEntry(), ruleContext);
                }
            }
            catch (Exception x)
            {
                Platform.Log(LogLevel.Error, x, "Unexpected exception reindexing folder: {0}", Location.StudyFolder);
                Failed         = true;
                FailureMessage = x.Message;
            }

            if (_cancelRequested)
            {
                Platform.Log(LogLevel.Info, "Cancel requested while reprocessing folder: {0}", Location.StudyFolder);
            }
            else
            {
                Platform.Log(LogLevel.Info, "Completed reprocessing study folder: {0}", Location.Study.StudyInstanceUid);
            }
        }