/**
         * covers parent StockExchangeProvider(and its parent CustomerOrderPart if exist), child PurchaseOrderPart if exist
         * --> 3 types of subgraphs: Production, Purchase, Customer
         */
        private static List <IDemandOrProvider> GetItemsOfStockExchangeDemandSubGraph(
            StockExchangeDemand stockExchangeDemand, IDbTransactionData dbTransactionData,
            IAggregator aggregator, bool includeStockExchangeProviderHavingMultipleChilds)
        {
            List <IDemandOrProvider> items = new List <IDemandOrProvider>();

            items.Add(stockExchangeDemand);

            Providers stockExchangeProviders =
                aggregator.GetAllParentProvidersOf(stockExchangeDemand);

            foreach (var stockExchangeProvider in stockExchangeProviders)
            {
                Demands childsOfStockExchangeProvider =
                    aggregator.GetAllChildDemandsOf(stockExchangeProvider);
                if (includeStockExchangeProviderHavingMultipleChilds ||
                    childsOfStockExchangeProvider.Count() == 1)
                {
                    items.Add(stockExchangeProvider);
                    Demands customerOrderParts =
                        aggregator.GetAllParentDemandsOf(stockExchangeProvider);
                    if (customerOrderParts.Count() > 1)
                    {
                        throw new MrpRunException(
                                  "A stockExchangeProvider can only have one parent.");
                    }

                    foreach (var customerOrderPart in customerOrderParts)
                    {
                        items.Add(customerOrderPart);
                    }
                }
            }

            Providers purchaseOrderParts = aggregator.GetAllChildProvidersOf(stockExchangeDemand);

            if (purchaseOrderParts.Count() > 1)
            {
                throw new MrpRunException("A stockExchangeDemand can only have one child.");
            }

            foreach (var purchaseOrderPart in purchaseOrderParts)
            {
                items.Add(purchaseOrderPart);
            }

            return(items);
        }
        private static void ArchiveStockExchangeProvidersWithoutChilds(
            IDbTransactionData dbTransactionData, IAggregator aggregator)
        {
            Providers copyOfStockExchangeProviders = new Providers();

            copyOfStockExchangeProviders.AddAll(dbTransactionData.StockExchangeProvidersGetAll());
            foreach (var stockExchangeProvider in copyOfStockExchangeProviders)
            {
                Demands stockExchangeDemands =
                    aggregator.GetAllChildDemandsOf(stockExchangeProvider);
                if (stockExchangeDemands == null || stockExchangeDemands.Any() == false)
                {
                    ArchiveDemandOrProvider(stockExchangeProvider, dbTransactionData, aggregator,
                                            false);
                }
            }
        }
        /*
         *  foreach sed in beendeten und geschlossenen StockExchangeDemands
         *      Archiviere sed und seine parents (StockExchangeProvider)
         *        und die Pfeile dazwischen
         */
        private static void ArchiveClosedStockExchangeDemandsAndItsParents(
            IDbTransactionData dbTransactionData, IAggregator aggregator)
        {
            IStackSet <IDemandOrProvider> stockExchangesToArchive =
                new StackSet <IDemandOrProvider>();

            foreach (var stockExchangeDemand in dbTransactionData.StockExchangeDemandsGetAll())
            {
                bool isOpen = OpenDemandManager.IsOpen((StockExchangeDemand)stockExchangeDemand);

                if (isOpen == false && stockExchangeDemand.IsFinished())
                {
                    stockExchangesToArchive.Push(stockExchangeDemand);

                    // parent (StockExchangeProviders)
                    Providers stockExchangeProviders =
                        aggregator.GetAllParentProvidersOf(stockExchangeDemand);
                    foreach (var stockExchangeProvider in stockExchangeProviders)
                    {
                        if (aggregator.GetAllChildDemandsOf(stockExchangeProvider).Count() == 1)
                        {
                            stockExchangesToArchive.Push(stockExchangeProvider);
                        }
                        else
                        {
                            // stockExchangeProvider must stay
                        }
                    }
                }
            }

            // archive collected stockexchanges
            foreach (var demandOrProviderToArchive in stockExchangesToArchive)
            {
                ArchiveDemandOrProvider(demandOrProviderToArchive, dbTransactionData, aggregator,
                                        true);
            }
        }
        /**
         * Subgraph of a productionOrder includes:
         * - parent (StockExchangeDemand) if includeStockExchanges true
         * - childs (ProductionOrderBoms)
         * - childs of childs (StockExchangeProvider) if includeStockExchanges true
         */
        private static List <IDemandOrProvider> CreateProductionOrderSubGraph(
            bool includeStockExchanges, ProductionOrder productionOrder, IAggregator aggregator)
        {
            List <IDemandOrProvider> demandOrProvidersOfProductionOrderSubGraph =
                new List <IDemandOrProvider>();

            demandOrProvidersOfProductionOrderSubGraph.Add(productionOrder);

            if (includeStockExchanges)
            {
                Demands stockExchangeDemands = aggregator.GetAllParentDemandsOf(productionOrder);
                if (stockExchangeDemands.Count() > 1)
                {
                    throw new MrpRunException(
                              "A productionOrder can only have one parentDemand (stockExchangeDemand).");
                }

                demandOrProvidersOfProductionOrderSubGraph.AddRange(stockExchangeDemands);
                foreach (var stockExchangeDemand in stockExchangeDemands)
                {
                    Providers parentStockExchangeProviders =
                        aggregator.GetAllParentProvidersOf(stockExchangeDemand);

                    foreach (var parentStockExchangeProvider in parentStockExchangeProviders)
                    {
                        if (aggregator.GetAllChildDemandsOf(parentStockExchangeProvider).Count() ==
                            1)
                        {
                            demandOrProvidersOfProductionOrderSubGraph.Add(
                                parentStockExchangeProvider);
                        }
                        else
                        {
                            // stockExchangeProvider must stay
                        }
                    }
                }
            }

            Demands productionOrderBoms = aggregator.GetAllChildDemandsOf(productionOrder);

            demandOrProvidersOfProductionOrderSubGraph.AddRange(productionOrderBoms);

            if (includeStockExchanges)
            {
                foreach (var productionOrderBom in productionOrderBoms)
                {
                    Providers stockExchangeProvider =
                        aggregator.GetAllChildProvidersOf(productionOrderBom);
                    if (stockExchangeProvider.Count() > 1)
                    {
                        throw new MrpRunException(
                                  "A ProductionOrderBom can only have one childProvider (stockExchangeProvider).");
                    }

                    demandOrProvidersOfProductionOrderSubGraph.AddRange(stockExchangeProvider);
                }
            }

            return(demandOrProvidersOfProductionOrderSubGraph);
        }
        public void TestApplyConfirmations(string testConfigurationFileName)
        {
            InitThisTest(testConfigurationFileName);

            IDbTransactionData dbTransactionData =
                ZppConfiguration.CacheManager.GetDbTransactionData();
            IAggregator aggregator = ZppConfiguration.CacheManager.GetAggregator();

            // Ein CustomerOrderParts darf kein Kind haben und darf nicht beendet sein
            Ids customerOrderIds = new Ids();

            foreach (var demand in dbTransactionData.CustomerOrderPartGetAll())
            {
                CustomerOrderPart customerOrderPart = (CustomerOrderPart)demand;
                customerOrderIds.Add(customerOrderPart.GetCustomerOrderId());
                Providers childs = aggregator.GetAllChildProvidersOf(customerOrderPart);
                Assert.False(childs.Any());
                Assert.False(customerOrderPart.IsFinished());
            }

            // Ein PurchaseOrderPart darf kein Elter haben und darf nicht beendet sein.
            Ids purchaseOrderIds = new Ids();

            foreach (var demand in dbTransactionData.PurchaseOrderPartGetAll())
            {
                PurchaseOrderPart purchaseOrderPart = (PurchaseOrderPart)demand;
                purchaseOrderIds.Add(purchaseOrderPart.GetPurchaseOrderId());

                Assert.False(purchaseOrderPart.IsFinished());
                Demands demands = aggregator.GetAllParentDemandsOf(demand);
                Assert.True(demands == null || demands.Any() == false);
            }

            // Für jede CustomerOrder muss es mind. noch ein CustomerOrderPart geben.
            foreach (var customerOrder in dbTransactionData.CustomerOrderGetAll())
            {
                Assert.True(customerOrderIds.Contains(customerOrder.GetId()));
            }

            // Für jede PurchaseOrder muss es mind. noch ein PurchaseOrderPart geben.
            foreach (var purchaseOrder in dbTransactionData.PurchaseOrderGetAll())
            {
                Assert.True(purchaseOrderIds.Contains(purchaseOrder.GetId()));
            }

            // Ein StockExchangeProvider muss mind. ein Kind haben.
            foreach (var stockExchangeProvider in dbTransactionData.StockExchangeProvidersGetAll())
            {
                Demands childs = aggregator.GetAllChildDemandsOf(stockExchangeProvider);
                Assert.True(childs.Any());
            }

            // Ein StockExchangeDemand darf nicht beendet und geschlossen sein.
            foreach (var stockExchangeDemand in dbTransactionData.StockExchangeDemandsGetAll())
            {
                bool isOpen = OpenDemandManager.IsOpen((StockExchangeDemand)stockExchangeDemand);
                Assert.False(stockExchangeDemand.IsFinished() && isOpen == false);
            }

            // Eine ProductionOrder darf nicht beendet sein und für eine ProductionOrder
            // muss es mind. eine  Operation geben.
            Ids productionOrderIds = new Ids();
            Ids operationIds       = new Ids();

            foreach (var operation in dbTransactionData.ProductionOrderOperationGetAll())
            {
                Id productionOrderId = operation.GetProductionOrderId();
                if (productionOrderIds.Contains(productionOrderId) == false)
                {
                    productionOrderIds.Add(productionOrderId);
                }

                operationIds.Add(operation.GetId());
            }
            foreach (var provider in dbTransactionData.ProductionOrderGetAll())
            {
                ProductionOrder productionOrder = (ProductionOrder)provider;
                Assert.False(productionOrder.DetermineProductionOrderState()
                             .Equals(State.Finished));
                Assert.True(productionOrderIds.Contains(productionOrder.GetId()));
            }

            // Für jede ProductionOrderBom muss die dazugehörige Operation da sein.
            foreach (var demand in dbTransactionData.ProductionOrderBomGetAll())
            {
                ProductionOrderBom productionOrderBom = (ProductionOrderBom)demand;
                operationIds.Contains(productionOrderBom.GetProductionOrderOperationId());
            }

            // Für jeden DemandToProvider und ProviderToDemand müssen die dazugehörigen
            // Demands und Provider existieren.
            foreach (var demandToProvider in dbTransactionData.DemandToProviderGetAll())
            {
                Demand   demand   = dbTransactionData.DemandsGetById(demandToProvider.GetDemandId());
                Provider provider =
                    dbTransactionData.ProvidersGetById(demandToProvider.GetProviderId());
                Assert.NotNull(demand);
                Assert.NotNull(provider);
            }

            foreach (var providerToDemand in dbTransactionData.ProviderToDemandGetAll())
            {
                Demand   demand   = dbTransactionData.DemandsGetById(providerToDemand.GetDemandId());
                Provider provider =
                    dbTransactionData.ProvidersGetById(providerToDemand.GetProviderId());
                Assert.NotNull(demand);
                Assert.NotNull(provider);
            }
        }