/// <summary>
        /// Called after the specified <see cref="WorkQueueUid"/> has been processed
        /// </summary>
        /// <param name="item">The <see cref="WorkQueue"/> item being processed</param>
        /// <param name="uid">The <see cref="WorkQueueUid"/> being processed</param>
        protected virtual void OnProcessUidEnd(Model.WorkQueue item, WorkQueueUid uid)
        {
            Platform.CheckForNullReference(item, "item");
            Platform.CheckForNullReference(uid, "uid");

            if (uid.Duplicate)
            {
                String dupPath = ServerPlatform.GetDuplicateUidPath(StorageLocation, uid);
                // Delete the container if it's empty
                var f = new FileInfo(dupPath);

                if (f.Directory != null && DirectoryUtility.DeleteIfEmpty(f.Directory.FullName))
                {
                    DirectoryUtility.DeleteIfEmpty(ServerPlatform.GetDuplicateGroupPath(StorageLocation, uid));
                }
            }
        }
        /// <summary>
        /// Process a specified <see cref="WorkQueueUid"/>
        /// </summary>
        /// <param name="item">The <see cref="WorkQueue"/> item being processed</param>
        /// <param name="sop">The <see cref="WorkQueueUid"/> being processed</param>
        /// <param name="studyXml">The <see cref="StudyXml"/> object for the study being processed</param>
        /// <returns>true if the <see cref="WorkQueueUid"/> is successfully processed. false otherwise</returns>
        protected virtual bool ProcessWorkQueueUid(Model.WorkQueue item, WorkQueueUid sop, StudyXml studyXml)
        {
            Platform.CheckForNullReference(item, "item");
            Platform.CheckForNullReference(sop, "sop");
            Platform.CheckForNullReference(studyXml, "studyXml");

            OnProcessUidBegin(item, sop);

            string path = null;

            try
            {
                if (sop.Duplicate && sop.Extension != null)
                {
                    path = ServerPlatform.GetDuplicateUidPath(StorageLocation, sop);
                    var file = new DicomFile(path);
                    file.Load();

                    InstancePreProcessingResult result = PreProcessFile(sop, file);

                    if (false == file.DataSet[DicomTags.StudyInstanceUid].ToString().Equals(StorageLocation.StudyInstanceUid) ||
                        result.DiscardImage)
                    {
                        RemoveWorkQueueUid(sop, null);
                    }
                    else
                    {
                        var duplicateResult = ProcessDuplicate(file, sop, studyXml);
                        if (duplicateResult.ActionTaken == DuplicateProcessResultAction.Delete || duplicateResult.ActionTaken == DuplicateProcessResultAction.Accept)
                        {
                            // make sure the folder is also deleted if it's empty
                            string folder = Path.GetDirectoryName(path);

                            String reconcileRootFolder = ServerPlatform.GetDuplicateFolderRootPath(StorageLocation);
                            DirectoryUtility.DeleteIfEmpty(folder, reconcileRootFolder);
                        }
                    }
                }
                else
                {
                    try
                    {
                        path = StorageLocation.GetSopInstancePath(sop.SeriesInstanceUid, sop.SopInstanceUid);
                        var file = new DicomFile(path);
                        file.Load();

                        InstancePreProcessingResult result = PreProcessFile(sop, file);

                        if (false == file.DataSet[DicomTags.StudyInstanceUid].ToString().Equals(StorageLocation.StudyInstanceUid) ||
                            result.DiscardImage)
                        {
                            RemoveWorkQueueUid(sop, path);
                        }
                        else
                        {
                            ProcessFile(sop, file, studyXml, !result.AutoReconciled);
                        }
                    }
                    catch (DicomException ex)
                    {
                        // bad file. Remove it from the filesystem and the queue
                        RemoveBadDicomFile(path, ex.Message);
                        DeleteWorkQueueUid(sop);
                        return(false);
                    }
                }

                return(true);
            }
            catch (StudyIsNearlineException)
            {
                // handled by caller
                throw;
            }
            catch (Exception e)
            {
                Platform.Log(LogLevel.Error, e, "Unexpected exception when processing file: {0} SOP Instance: {1}", path, sop.SopInstanceUid);
                item.FailureDescription = e.InnerException != null?
                                          String.Format("{0}:{1}", e.GetType().Name, e.InnerException.Message) : String.Format("{0}:{1}", e.GetType().Name, e.Message);

                //No longer needed.  Update was moved into the SopInstanceProcessor
                //sop.FailureCount++;
                //UpdateWorkQueueUid(sop);
                return(false);
            }
            finally
            {
                OnProcessUidEnd(item, sop);
            }
        }
        private ProcessDuplicateResult ProcessDuplicate(DicomFile dupFile, WorkQueueUid uid, StudyXml studyXml)
        {
            var result = new ProcessDuplicateResult();


            string duplicateSopPath = ServerPlatform.GetDuplicateUidPath(StorageLocation, uid);
            string basePath         = StorageLocation.GetSopInstancePath(uid.SeriesInstanceUid, uid.SopInstanceUid);

            if (!File.Exists(basePath))
            {
                // NOTE: This is special case. The file which caused dicom service to think this sop is a duplicate
                // no longer exists in the study folder. Perhaps it has been moved to another folder during auto reconciliation.
                // We have nothing to compare against so let's just throw it into the SIQ queue.
                CreateDuplicateSIQEntry(uid, dupFile, null);
                result.ActionTaken = DuplicateProcessResultAction.Reconcile;
            }
            else
            {
                // Check if system is configured to override the rule for this study
                if (DuplicatePolicy.IsParitionDuplicatePolicyOverridden(this.StorageLocation))
                {
                    return(OverwriteDuplicate(dupFile, uid, studyXml));
                }
                else
                {
                    var baseFile = new DicomFile(basePath);
                    baseFile.Load();

                    if (DuplicateSopProcessorHelper.SopClassIsReport(dupFile.SopClass.Uid) && ServerPartition.AcceptLatestReport)
                    {
                        return(ProcessDuplicateReport(dupFile, baseFile, uid, studyXml));
                    }


                    if (!dupFile.TransferSyntax.Equals(baseFile.TransferSyntax))
                    {
                        // If they're compressed, and we have a codec, lets decompress and still do the comparison
                        if (dupFile.TransferSyntax.Encapsulated &&
                            !dupFile.TransferSyntax.LossyCompressed &&
                            DicomCodecRegistry.GetCodec(dupFile.TransferSyntax) != null)
                        {
                            dupFile.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
                        }

                        if (baseFile.TransferSyntax.Encapsulated &&
                            !baseFile.TransferSyntax.LossyCompressed &&
                            DicomCodecRegistry.GetCodec(baseFile.TransferSyntax) != null)
                        {
                            baseFile.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
                        }

                        if (dupFile.TransferSyntax.Encapsulated || baseFile.TransferSyntax.Encapsulated)
                        {
                            string failure = String.Format("Base file transfer syntax is '{0}' while duplicate file has '{1}'",
                                                           baseFile.TransferSyntax, dupFile.TransferSyntax);

                            var list          = new List <DicomAttributeComparisonResult>();
                            var compareResult = new DicomAttributeComparisonResult
                            {
                                ResultType = ComparisonResultType.DifferentValues,
                                TagName    = DicomTagDictionary.GetDicomTag(DicomTags.TransferSyntaxUid).Name,
                                Details    = failure
                            };
                            list.Add(compareResult);
                            CreateDuplicateSIQEntry(uid, dupFile, list);
                            result.ActionTaken = DuplicateProcessResultAction.Reconcile;
                            return(result);
                        }
                    }

                    var failureReason = new List <DicomAttributeComparisonResult>();
                    if (baseFile.DataSet.Equals(dupFile.DataSet, ref failureReason))
                    {
                        Platform.Log(LogLevel.Info,
                                     "Duplicate SOP being processed is identical.  Removing SOP: {0}",
                                     baseFile.MediaStorageSopInstanceUid);


                        RemoveWorkQueueUid(uid, duplicateSopPath);
                        result.ActionTaken = DuplicateProcessResultAction.Delete;
                    }
                    else
                    {
                        CreateDuplicateSIQEntry(uid, dupFile, failureReason);
                        result.ActionTaken = DuplicateProcessResultAction.Reconcile;
                    }
                }
            }

            return(result);
        }