public void TestNestedDemandsGetPutIntoDatabaseAndCanBeBroughtBack()
        {
            var pipe = new Pipeline(CatalogueRepository, "NestedPipe");
            var pc   = new PipelineComponent(CatalogueRepository, pipe, typeof(BasicDataReleaseDestination), -1,
                                             "Coconuts");

            pipe.DestinationPipelineComponent_ID = pc.ID;
            pipe.SaveToDatabase();

            //some of the DemandsInitialization on BasicDataReleaseDestination should be nested
            var f = new ArgumentFactory();

            Assert.True(
                f.GetRequiredProperties(typeof(BasicDataReleaseDestination)).Any(r => r.ParentPropertyInfo != null));

            //new pc should have no arguments
            Assert.That(pc.GetAllArguments(), Is.Empty);

            //we create them (the root and nested ones!)
            var args = pc.CreateArgumentsForClassIfNotExists <BasicDataReleaseDestination>();

            //and get all arguments / create arguments for class should have handled that
            Assert.That(pc.GetAllArguments().Any());

            var match = args.Single(a => a.Name == "ReleaseSettings.DeleteFilesOnSuccess");

            match.SetValue(true);
            match.SaveToDatabase();

            var useCase = ReleaseUseCase.DesignTime();

            var factory      = new DataFlowPipelineEngineFactory(useCase, RepositoryLocator.CatalogueRepository.MEF);
            var destInstance = factory.CreateDestinationIfExists(pipe);

            Assert.AreEqual(true, ((BasicDataReleaseDestination)destInstance).ReleaseSettings.DeleteFilesOnSuccess);
        }
        public override void Execute()
        {
            if (DicomSourceType == null)
            {
                SetImpossible("You must specify a Type for DicomSourceType");
                throw new ImpossibleCommandException(this, ReasonCommandImpossible);
            }

            base.Execute();

            List <DiscoveredTable> tablesCreated = new List <DiscoveredTable>();

            //Create with template?
            if (Template != null)
            {
                foreach (ImageTableTemplate table in Template.Tables)
                {
                    string tblName = GetNameWithPrefix(table.TableName);

                    var tbl = _databaseToCreateInto.ExpectTable(tblName);
                    var cmd = new ExecuteCommandCreateNewImagingDataset(_repositoryLocator, tbl, table);
                    cmd.Execute();

                    NewCataloguesCreated.Add(cmd.NewCatalogueCreated);
                    tablesCreated.Add(tbl);
                }
            }
            else
            {
                throw new Exception("No Template provided");
            }

            //that's us done if we aren't creating a load
            if (!CreateLoad)
            {
                return;
            }

            string loadName = GetNameWithPrefixInBracketsIfAny("SMI Image Loading");

            NewLoadMetadata = new LoadMetadata(_catalogueRepository, loadName);

            //tell all the catalogues that they are part of this load and where to log under the same task
            foreach (Catalogue c in NewCataloguesCreated)
            {
                c.LoadMetadata_ID      = NewLoadMetadata.ID;
                c.LoggingDataTask      = loadName;
                c.LiveLoggingServer_ID = _loggingServer.ID;
                c.SaveToDatabase();
            }

            //create the logging task
            new Core.Logging.LogManager(_loggingServer).CreateNewLoggingTaskIfNotExists(loadName);

            var projDir = LoadDirectory.CreateDirectoryStructure(_projectDirectory, "ImageLoading", true);

            NewLoadMetadata.LocationOfFlatFiles = projDir.RootPath.FullName;
            NewLoadMetadata.SaveToDatabase();

            /////////////////////////////////////////////Attacher////////////////////////////


            //Create a pipeline for reading from Dicom files and writing to any destination component (which must be fixed)
            var pipe = new Pipeline(_catalogueRepository, GetNameWithPrefixInBracketsIfAny("Image Loading Pipe"));

            DicomSourcePipelineComponent = new PipelineComponent(_catalogueRepository, pipe, DicomSourceType, 0, DicomSourceType.Name);
            DicomSourcePipelineComponent.CreateArgumentsForClassIfNotExists(DicomSourceType);

            // Set the argument for only populating tags who appear in the end tables of the load (no need for source to read all the tags only those we are actually loading)
            var arg = DicomSourcePipelineComponent.GetAllArguments().FirstOrDefault(a => a.Name.Equals(nameof(DicomSource.UseAllTableInfoInLoadAsFieldMap)));

            if (arg != null)
            {
                arg.SetValue(NewLoadMetadata);
                arg.SaveToDatabase();
            }

            pipe.SourcePipelineComponent_ID = DicomSourcePipelineComponent.ID;
            pipe.SaveToDatabase();


            //Create the load process task that uses the pipe to load RAW tables with data from the dicom files
            var pt = new ProcessTask(_catalogueRepository, NewLoadMetadata, LoadStage.Mounting)
            {
                Name            = "Auto Routing Attacher",
                ProcessTaskType = ProcessTaskType.Attacher,
                Path            = PersistentRaw
                    ? typeof(AutoRoutingAttacherWithPersistentRaw).FullName
                    : typeof(AutoRoutingAttacher).FullName,
                Order = 1
            };


            pt.SaveToDatabase();

            var args = PersistentRaw? pt.CreateArgumentsForClassIfNotExists <AutoRoutingAttacherWithPersistentRaw>() : pt.CreateArgumentsForClassIfNotExists <AutoRoutingAttacher>();

            SetArgument(args, "LoadPipeline", pipe);

            /////////////////////////////////////// Distinct tables on load /////////////////////////


            var distincter     = new ProcessTask(_catalogueRepository, NewLoadMetadata, LoadStage.AdjustRaw);
            var distincterArgs = distincter.CreateArgumentsForClassIfNotExists <Distincter>();

            distincter.Name            = "Distincter";
            distincter.ProcessTaskType = ProcessTaskType.MutilateDataTable;
            distincter.Path            = typeof(Distincter).FullName;
            distincter.Order           = 2;
            distincter.SaveToDatabase();
            SetArgument(distincterArgs, "TableRegexPattern", ".*");

            /////////////////////////////////////////////////////////////////////////////////////

            if (CreateCoalescer)
            {
                var coalescer = new ProcessTask(_catalogueRepository, NewLoadMetadata, LoadStage.AdjustRaw)
                {
                    Name            = "Coalescer",
                    ProcessTaskType = ProcessTaskType.MutilateDataTable,
                    Path            = typeof(Coalescer).FullName,
                    Order           = 3
                };
                coalescer.SaveToDatabase();

                StringBuilder regexPattern = new StringBuilder();

                foreach (var tbl in tablesCreated.Where(tbl => !tbl.DiscoverColumns().Any(c => c.GetRuntimeName().Equals("SOPInstanceUID", StringComparison.CurrentCultureIgnoreCase))))
                {
                    regexPattern.Append("(" + tbl.GetRuntimeName() + ")|");
                }


                var coalArgs = coalescer.CreateArgumentsForClassIfNotExists <Coalescer>();
                SetArgument(coalArgs, "TableRegexPattern", regexPattern.ToString().TrimEnd('|'));
                SetArgument(coalArgs, "CreateIndex", true);
            }

            ////////////////////////////////Load Ender (if no rows in load) ////////////////////////////

            var prematureLoadEnder = new ProcessTask(_catalogueRepository, NewLoadMetadata, LoadStage.Mounting)
            {
                Name            = "Premature Load Ender",
                ProcessTaskType = ProcessTaskType.MutilateDataTable,
                Path            = typeof(PrematureLoadEnder).FullName,
                Order           = 4
            };

            prematureLoadEnder.SaveToDatabase();

            args = prematureLoadEnder.CreateArgumentsForClassIfNotExists <PrematureLoadEnder>();
            SetArgument(args, "ExitCodeToReturnIfConditionMet", ExitCodeType.OperationNotRequired);
            SetArgument(args, "ConditionsToTerminateUnder", PrematureLoadEndCondition.NoRecordsInAnyTablesInDatabase);

            ////////////////////////////////////////////////////////////////////////////////////////////////

            var checker = new CheckEntireDataLoadProcess(NewLoadMetadata, new HICDatabaseConfiguration(NewLoadMetadata), new HICLoadConfigurationFlags(), _catalogueRepository.MEF);

            checker.Check(new AcceptAllCheckNotifier());
        }