public void Apply(ServerRuleApplyTimeEnum applyTime, CommandProcessor theProcessor)
        {
            _studyRulesEngine = new ServerRulesEngine(applyTime, _location.ServerPartitionKey);
            _studyRulesEngine.Load();

            List <string> files = GetFirstInstanceInEachStudySeries();

            if (files.Count == 0)
            {
                string message =
                    String.Format("Unexpectedly unable to find SOP instances for rules engine in each series in study: {0}",
                                  _location.StudyInstanceUid);
                Platform.Log(LogLevel.Error, message);
                throw new ApplicationException(message);
            }

            Platform.Log(LogLevel.Info, "Processing Study Level rules for study {0} on partition {1} at {2} apply time",
                         _location.StudyInstanceUid, _partition.Description, applyTime.Description);

            foreach (string seriesFilePath in files)
            {
                var theFile = new DicomFile(seriesFilePath);
                theFile.Load(DicomReadOptions.Default);
                var context =
                    new ServerActionContext(theFile, _location.FilesystemKey, _partition, _location.Key)
                {
                    CommandProcessor = theProcessor
                };
                _studyRulesEngine.Execute(context);

                ProcessSeriesRules(theFile, theProcessor);
            }

            if (applyTime.Equals(ServerRuleApplyTimeEnum.StudyProcessed))
            {
                // This is a bit kludgy, but we had a problem with studies with only 1 image incorectlly
                // having archive requests inserted when they were scheduled for deletion.  Calling
                // this command here so that if a delete is inserted at the study level, we will remove
                // the previously inserted archive request for the study.  Note also this has to be done
                // after the rules engine is executed.
                theProcessor.AddCommand(new InsertArchiveQueueCommand(_location.ServerPartitionKey, _location.Key));
            }
        }
        /// <summary>
        /// Method for applying rules when a new series has been inserted.
        /// </summary>
        /// <param name="file">The DICOM file being processed.</param>
        /// <param name="processor">The command processor</param>
        private void ProcessSeriesRules(DicomFile file, CommandProcessor processor)
        {
            if (_seriesRulesEngine == null)
            {
                _seriesRulesEngine = new ServerRulesEngine(ServerRuleApplyTimeEnum.SeriesProcessed, _location.ServerPartitionKey);
                _seriesRulesEngine.Load();
            }
            else
            {
                _seriesRulesEngine.Statistics.LoadTime.Reset();
                _seriesRulesEngine.Statistics.ExecutionTime.Reset();
            }

            var context = new ServerActionContext(file, _location.FilesystemKey, _partition, _location.Key)
            {
                CommandProcessor = processor
            };


            _seriesRulesEngine.Execute(context);
        }