MatchingParameterCollection GenerateMatchingParameterCollection(ViewGenerator.ViewDataRow metadataRow, out string rowKeyValue, out string rowKeyName, out string viewName)
        {
            DataRow originalRow = metadataRow.OriginalRow;
            MatchingParameterCollection matchingParamCollection = new MatchingParameterCollection();
            MatchingParameterList       matchingParamList       = new MatchingParameterList();

            matchingParamCollection.Add(matchingParamList);

            if (string.Compare(originalRow.Table.TableName, DataTableHelper.InstanceTableName) == 0)
            {
                string         sSopInstanceUid = RegisteredDataRows.InstanceInfo.GetElementValue(originalRow, DicomTag.SOPInstanceUID);
                ICatalogEntity imageInstance   = RegisteredEntities.GetInstanceEntity(sSopInstanceUid);

                rowKeyValue = sSopInstanceUid;
                rowKeyName  = "SOPInstanceUID";
                viewName    = "Images";

                matchingParamList.Add(imageInstance);
            }
            else if (string.Compare(originalRow.Table.TableName, DataTableHelper.SeriesTableName) == 0)
            {
                string         sSeriesInstanceUid = RegisteredDataRows.SeriesInfo.GetElementValue(originalRow, DicomTag.SeriesInstanceUID);
                ICatalogEntity seriesEntity       = RegisteredEntities.GetSeriesEntity(sSeriesInstanceUid);

                rowKeyValue = sSeriesInstanceUid;
                rowKeyName  = "SeriesInstanceUID";
                viewName    = "Series";

                matchingParamList.Add(seriesEntity);
            }
            else if (string.Compare(originalRow.Table.TableName, DataTableHelper.StudyTableName) == 0)
            {
                string         sStudyInstanceUid = RegisteredDataRows.StudyInfo.GetElementValue(originalRow, DicomTag.StudyInstanceUID);
                ICatalogEntity studyEntity       = RegisteredEntities.GetStudyEntity(sStudyInstanceUid);

                rowKeyValue = sStudyInstanceUid;
                rowKeyName  = "StudyInstanceUID";
                viewName    = "Studies";

                matchingParamList.Add(studyEntity);
            }
            else if (string.Compare(originalRow.Table.TableName, DataTableHelper.PatientTableName) == 0)
            {
                string         sPatientId    = RegisteredDataRows.PatientInfo.GetElementValue(originalRow, DicomTag.PatientID);
                ICatalogEntity patientEntity = RegisteredEntities.GetPatientEntity(sPatientId);

                rowKeyValue = sPatientId;
                rowKeyName  = "PatientId";
                viewName    = "Patients";

                matchingParamList.Add(patientEntity);
            }
            else
            {
                throw new ApplicationException("Row is not a valid DICOM format.");
            }

            return(matchingParamCollection);
        }
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            int count = 0;
            int imagesToExportCount   = 0;
            InstanceCStoreCommand cmd = null;

            MatchingParameterCollection matchingParamCollection;
            MatchingParameterList       matchingParamList;

            if (this.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            ExportDicomWorkerArgs args = e.Argument as ExportDicomWorkerArgs;

            if (args == null)
            {
                throw new ArgumentException("Invalid ExportDicom argument");
            }

            if (args.CreateDicomDir)
            {
                DicomDirectory = new DicomDir(args.OutputFolder);
            }

            if (args.Anonymize)
            {
                Anonymizer.BeginBatch();
            }

            try
            {
                ViewGenerator.ViewDataRow[] exportRows = (args.Rows);

                imagesToExportCount = exportRows.Length;

                foreach (ViewGenerator.ViewDataRow exportRow in exportRows)
                {
                    try
                    {
                        string rowKeyValue;
                        string rowKeyName;
                        string viewName;

                        if (this.CancellationPending)
                        {
                            e.Cancel = true;

                            return;
                        }

                        DataRow originalRow = exportRow.OriginalRow;
                        matchingParamCollection = new MatchingParameterCollection( );
                        matchingParamList       = new MatchingParameterList( );

                        matchingParamCollection.Add(matchingParamList);

                        if (string.Compare(originalRow.Table.TableName, DataTableHelper.InstanceTableName) == 0)
                        {
                            string         sSopInstanceUid = RegisteredDataRows.InstanceInfo.GetElementValue(originalRow, DicomTag.SOPInstanceUID);
                            ICatalogEntity imageInstance   = RegisteredEntities.GetInstanceEntity(sSopInstanceUid);

                            rowKeyValue = sSopInstanceUid;
                            rowKeyName  = "SOPInstanceUID";
                            viewName    = "Images";

                            matchingParamList.Add(imageInstance);
                        }
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.SeriesTableName) == 0)
                        {
                            string         sSeriesInstanceUid = RegisteredDataRows.SeriesInfo.GetElementValue(originalRow, DicomTag.SeriesInstanceUID);
                            ICatalogEntity seriesEntity       = RegisteredEntities.GetSeriesEntity(sSeriesInstanceUid);

                            rowKeyValue = sSeriesInstanceUid;
                            rowKeyName  = "SeriesInstanceUID";
                            viewName    = "Series";

                            matchingParamList.Add(seriesEntity);
                        }
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.StudyTableName) == 0)
                        {
                            string         sStudyInstanceUid = RegisteredDataRows.StudyInfo.GetElementValue(originalRow, DicomTag.StudyInstanceUID);
                            ICatalogEntity studyEntity       = RegisteredEntities.GetStudyEntity(sStudyInstanceUid);

                            rowKeyValue = sStudyInstanceUid;
                            rowKeyName  = "StudyInstanceUID";
                            viewName    = "Studies";

                            matchingParamList.Add(studyEntity);
                        }
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.PatientTableName) == 0)
                        {
                            string         sPatientId    = RegisteredDataRows.PatientInfo.GetElementValue(originalRow, DicomTag.PatientID);
                            ICatalogEntity patientEntity = RegisteredEntities.GetPatientEntity(sPatientId);

                            rowKeyValue = sPatientId;
                            rowKeyName  = "PatientId";
                            viewName    = "Patients";

                            matchingParamList.Add(patientEntity);
                        }
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.HangingProtocolTableName) == 0)
                        {
                            string         sSopInstanceUid         = RegisteredDataRows.InstanceInfo.GetElementValue(originalRow, DicomTag.SOPInstanceUID);
                            ICatalogEntity hangingProtocolInstance = RegisteredEntities.GetHangingProtocolEntity(sSopInstanceUid);

                            rowKeyValue = sSopInstanceUid;
                            rowKeyName  = "SOPInstanceUID";
                            viewName    = "Hanging Protocol";

                            matchingParamList.Add(hangingProtocolInstance);
                        }
                        else
                        {
                            throw new ApplicationException("Deleted row is not a valid DICOM format.");
                        }

                        DataSet   exportDataSet = null;
                        DataRow[] rows          = null;

                        if (string.Compare(originalRow.Table.TableName, DataTableHelper.HangingProtocolTableName) == 0 && _dataAccessAgent3 != null)
                        {
                            exportDataSet = _dataAccessAgent3.QueryHangingProtocol(matchingParamCollection);
                            rows          = exportDataSet.Tables[DataTableHelper.HangingProtocolTableName].Select();
                        }
                        else
                        {
                            exportDataSet = _dataAccessAgent.QueryCompositeInstances(matchingParamCollection);
                            rows          = exportDataSet.Tables[DataTableHelper.InstanceTableName].Select();
                        }

                        foreach (DataRow row in rows)
                        {
                            DicomDataSet dicomDataSet = RegisteredDataRows.InstanceInfo.LoadDicomDataSet(row);

                            if (args.Anonymize)
                            {
                                Anonymizer.Anonymize(dicomDataSet);
                            }

                            AddDicomBackgroundWorker.StoreClientSessionProxy proxy = new AddDicomBackgroundWorker.StoreClientSessionProxy();
                            proxy.AffectedSOPInstance = dicomDataSet.GetValue <string>(DicomTag.SOPInstanceUID, string.Empty);
                            proxy.AbstractClass       = dicomDataSet.GetValue <string>(DicomTag.SOPClassUID, string.Empty);


                            try
                            {
                                cmd = new InstanceCStoreCommand(proxy, dicomDataSet, _dataAccessAgent);

                                OnStoreCommandCreated(this, new StoreCommandEventArgs(cmd));

                                cmd.Configuration.DataSetStorageLocation = args.OutputFolder;
#if (LEADTOOLS_V19_OR_LATER)
                                cmd.Configuration.HangingProtocolLocation = args.OutputFolder;
#endif

                                string fileLocation = CStoreCommand.GetStorageFullPath(cmd.Configuration, dicomDataSet);
                                if (args.Overwrite || !File.Exists(fileLocation))
                                {
                                    // Only exporting, so do not validate SopInstance and do not add to database
                                    cmd.DoValidateSopInstance      = false;
                                    cmd.DoUpdateDatabase           = false;
                                    cmd.DoUseExternalStoreSettings = false;
                                    cmd.DataSetStored += cmd_DataSetStored;

                                    cmd.Execute();
                                }
                                else
                                {
                                    // File already exists -- it is not overwritten
                                    DataSetStoredEventArgs storedArgs = new DataSetStoredEventArgs(dicomDataSet, string.Empty, string.Empty, fileLocation);
                                    cmd_DataSetStored(this, storedArgs);
                                }
                            }
                            finally
                            {
                                if (cmd != null)
                                {
                                    cmd.DataSetStored -= cmd_DataSetStored;
                                }
                            }
                        }

                        count++;

                        ExportDicomWorkerProgressState state = new ExportDicomWorkerProgressState( );

                        state.Error               = null;
                        state.CurrentCount        = count;
                        state.ExportRow           = exportRow;
                        state.ExportedImagesCount = 1;
                        state.TotalCount          = imagesToExportCount;
                        state.RowKeyValue         = rowKeyValue;
                        state.RowKeyName          = rowKeyName;
                        state.ViewName            = viewName;

                        ReportProgress((count * 100) / exportRows.Length, state);
                    }
                    catch (Exception exception)
                    {
                        ExportDicomWorkerProgressState state = new ExportDicomWorkerProgressState( );

                        count++;

                        state.Error               = exception;
                        state.CurrentCount        = count;
                        state.ExportRow           = exportRow;
                        state.TotalCount          = imagesToExportCount;
                        state.ExportedImagesCount = 0;

                        ReportProgress((count * 100) / exportRows.Length, state);
                    }
                }
            }
            finally
            {
                e.Result = imagesToExportCount;
                if (DicomDirectory != null)
                {
                    DicomDirectory.Save();
                    DicomDirectory = null;
                }

                if (Anonymizer != null)
                {
                    Anonymizer.EndBatch();
                    Anonymizer = null;
                }
            }
        }
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            int count;
            int totalDeletedImagesCount;

            ViewGenerator.ViewDataRow [] deleteRows;


            count = 0;
            totalDeletedImagesCount = 0;

            try
            {
                MatchingParameterCollection matchingParamCollection;
                MatchingParameterList       matchingParamList;


                if (this.CancellationPending)
                {
                    e.Cancel = true;

                    return;
                }

                if (!(e.Argument is ViewGenerator.ViewDataRow []))
                {
                    throw new ArgumentException("Invalid RemoveDicom argument");
                }

                deleteRows = (e.Argument as ViewGenerator.ViewDataRow []);

                foreach (ViewGenerator.ViewDataRow deletedRow in deleteRows)
                {
                    try
                    {
                        DataRow originalRow;
                        int     deletedImagesCount;
                        string  rowKeyValue;
                        string  rowKeyName;
                        string  viewName;


                        if (this.CancellationPending)
                        {
                            e.Cancel = true;

                            return;
                        }

                        originalRow             = deletedRow.OriginalRow;
                        matchingParamCollection = new MatchingParameterCollection( );
                        matchingParamList       = new MatchingParameterList( );

                        matchingParamCollection.Add(matchingParamList);

                        // if ( originalRow is CompositeInstanceDataSet.InstanceRow )
                        if (string.Compare(originalRow.Table.TableName, DataTableHelper.InstanceTableName) == 0)
                        {
                            // imageInstance = new Instance( ( ( CompositeInstanceDataSet.InstanceRow ) originalRow ).SOPInstanceUID ) ;
                            string         sSopInstanceUid = RegisteredDataRows.InstanceInfo.GetElementValue(originalRow, DicomTag.SOPInstanceUID);
                            ICatalogEntity imageInstance   = RegisteredEntities.GetInstanceEntity(sSopInstanceUid);


                            rowKeyValue = sSopInstanceUid;
                            rowKeyName  = "SOPInstanceUID";
                            viewName    = "Images";

                            matchingParamList.Add(imageInstance);
                        }
                        // else if ( originalRow is CompositeInstanceDataSet.SeriesRow )
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.SeriesTableName) == 0)
                        {
                            // ICatalogEntity seriesEntity ;


                            // seriesEntity = new Series ( ( ( CompositeInstanceDataSet.SeriesRow ) originalRow ).SeriesInstanceUID ) ;
                            string         sSeriesInstanceUid = RegisteredDataRows.SeriesInfo.GetElementValue(originalRow, DicomTag.SeriesInstanceUID);
                            ICatalogEntity seriesEntity       = RegisteredEntities.GetSeriesEntity(sSeriesInstanceUid);
                            // seriesEntity = new Series ( ( ( CompositeInstanceDataSet.SeriesRow ) originalRow ).SeriesInstanceUID ) ;


                            rowKeyValue = sSeriesInstanceUid;
                            rowKeyName  = "SeriesInstanceUID";
                            viewName    = "Series";

                            matchingParamList.Add(seriesEntity);
                        }
                        // else if ( originalRow is CompositeInstanceDataSet.StudyRow )
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.StudyTableName) == 0)
                        {
                            // Study studyEntity = new Study ( ( ( CompositeInstanceDataSet.StudyRow ) originalRow ).StudyInstanceUID ) ;
                            string         sStudyInstanceUid = RegisteredDataRows.StudyInfo.GetElementValue(originalRow, DicomTag.StudyInstanceUID);
                            ICatalogEntity studyEntity       = RegisteredEntities.GetStudyEntity(sStudyInstanceUid);

                            rowKeyValue = sStudyInstanceUid;
                            rowKeyName  = "StudyInstanceUID";
                            viewName    = "Studies";

                            matchingParamList.Add(studyEntity);
                        }
                        // else if ( originalRow is CompositeInstanceDataSet.PatientRow )
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.PatientTableName) == 0)
                        {
                            // Patient patientEntity = new Patient ( ( ( CompositeInstanceDataSet.PatientRow ) originalRow ).PatientID ) ;
                            string         sPatientId    = RegisteredDataRows.PatientInfo.GetElementValue(originalRow, DicomTag.PatientID);
                            ICatalogEntity patientEntity = RegisteredEntities.GetPatientEntity(sPatientId);

                            rowKeyValue = sPatientId;
                            rowKeyName  = "PatientId";
                            viewName    = "Patients";

                            matchingParamList.Add(patientEntity);
                        }
#if (LEADTOOLS_V19_OR_LATER)
                        else if (string.Compare(originalRow.Table.TableName, DataTableHelper.HangingProtocolTableName) == 0)
                        {
                            string         sSopInstanceUid         = RegisteredDataRows.InstanceInfo.GetElementValue(originalRow, DicomTag.SOPInstanceUID);
                            ICatalogEntity hangingProtocolInstance = RegisteredEntities.GetHangingProtocolEntity(sSopInstanceUid);


                            rowKeyValue = sSopInstanceUid;
                            rowKeyName  = "SOPInstanceUID";
                            viewName    = "Hanging Protocol";

                            matchingParamList.Add(hangingProtocolInstance);
                        }
#endif
                        else
                        {
                            throw new ApplicationException("Deleted row is not a valid DICOM format.");
                        }

#if (LEADTOOLS_V19_OR_LATER)
                        if (string.Compare(originalRow.Table.TableName, DataTableHelper.HangingProtocolTableName) == 0 && _dataAccessAgent3 != null)
                        {
                            deletedImagesCount = _dataAccessAgent3.DeleteHangingProtocol(matchingParamCollection);
                        }
                        else
                        {
                            deletedImagesCount = _dataAccessAgent.DeleteInstance(matchingParamCollection);
                        }
#else
                        deletedImagesCount = _dataAccessAgent.DeleteInstance(matchingParamCollection);
#endif

                        count++;

                        RemoveDiconWorkerProgressState state;


                        state = new RemoveDiconWorkerProgressState( );

                        totalDeletedImagesCount += deletedImagesCount;

                        state.Error              = null;
                        state.CurrentCount       = count;
                        state.DeletedRow         = deletedRow;
                        state.RemovedImagesCount = deletedImagesCount;
                        state.TotalCount         = totalDeletedImagesCount;
                        state.RowKeyValue        = rowKeyValue;
                        state.RowKeyName         = rowKeyName;
                        state.ViewName           = viewName;

                        ReportProgress((count * 100) / deleteRows.Length, state);
                    }
                    catch (Exception exception)
                    {
                        RemoveDiconWorkerProgressState state;


                        state = new RemoveDiconWorkerProgressState( );

                        count++;

                        state.Error              = exception;
                        state.CurrentCount       = count;
                        state.DeletedRow         = deletedRow;
                        state.TotalCount         = totalDeletedImagesCount;
                        state.RemovedImagesCount = 0;

                        ReportProgress((count * 100) / deleteRows.Length, state);
                    }
                }
            }
            finally
            {
                e.Result = totalDeletedImagesCount;
            }
        }