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