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);
        }
        /// <summary>
        /// Compares received duplicate with the existing copy in the filesystem and throw it into the SIQ if they differ.
        /// Otherwise, simply delete the duplicate and keep everything as it.
        /// </summary>
        /// <param name="dupFile"></param>
        /// <param name="baseFile"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        private ProcessDuplicateResult CompareDuplicates(DicomFile dupFile, DicomFile baseFile, WorkQueueUid uid)
        {
            var    result           = new ProcessDuplicateResult();
            string duplicateSopPath = ServerHelper.GetDuplicateUidPath(StorageLocation, uid);

            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);
        }