/**
         * // Entferne alle Pfeile auf StockExchangeProvider zeigend
         *  // Entferne alle Pfeile von StockExchangeDemands weg zeigend
         *  //  --> übrig beleiben Pfeile zwischen StockExchangeProvider und StockExchangeDemands
         */
        private static void RemoveArrowsBelowStockExchangeDemandsAndAboveStockExchangeProviders(
            IDbTransactionData dbTransactionData, IAggregator aggregator)
        {
            List <ILinkDemandAndProvider> stockExchangeLinks = new List <ILinkDemandAndProvider>();

            foreach (var stockExchangeDemand in dbTransactionData.StockExchangeDemandsGetAll())
            {
                List <ILinkDemandAndProvider> fromStockExchanges =
                    aggregator.GetArrowsFrom(stockExchangeDemand);
                if (fromStockExchanges != null)
                {
                    stockExchangeLinks.AddRange(fromStockExchanges);
                }
            }

            foreach (var stockExchangeProvider in dbTransactionData.StockExchangeProvidersGetAll())
            {
                List <ILinkDemandAndProvider> toStockExchanges =
                    aggregator.GetArrowsTo(stockExchangeProvider);
                if (toStockExchanges != null)
                {
                    stockExchangeLinks.AddRange(toStockExchanges);
                }
            }

            dbTransactionData.DeleteAllFrom(stockExchangeLinks);
        }
        private static void HandleProductionOrderIsFinished(ProductionOrder productionOrder,
                                                            IAggregator aggregator, IDbTransactionData dbTransactionData)
        {
            IDbTransactionData dbTransactionDataArchive =
                ZppConfiguration.CacheManager.GetDbTransactionDataArchive();

            // archive operations
            ArchiveOperations(dbTransactionData, dbTransactionDataArchive, aggregator,
                              productionOrder);

            // collect demands Or providers
            List <IDemandOrProvider> demandOrProvidersToArchive =
                CreateProductionOrderSubGraph(false, productionOrder, aggregator);

            // delete & archive all collected entities
            foreach (var demandOrProvider in demandOrProvidersToArchive)
            {
                // arrow outside mus be removed
                List <ILinkDemandAndProvider> demandAndProviderLinks;
                if (demandOrProvider.GetType() == typeof(ProductionOrder))
                {
                    // archive only link from ProductionOrder
                    demandAndProviderLinks = aggregator.GetArrowsFrom(demandOrProvider);
                    dbTransactionDataArchive.AddAllFrom(demandAndProviderLinks);
                    dbTransactionData.DeleteAllFrom(demandAndProviderLinks);
                    demandAndProviderLinks = aggregator.GetArrowsTo(demandOrProvider);
                    dbTransactionData.DeleteAllFrom(demandAndProviderLinks);
                }
                else if (demandOrProvider.GetType() == typeof(ProductionOrderBom))
                {
                    // archive only link to ProductionOrderBom
                    demandAndProviderLinks = aggregator.GetArrowsTo(demandOrProvider);
                    dbTransactionDataArchive.AddAllFrom(demandAndProviderLinks);
                    dbTransactionData.DeleteAllFrom(demandAndProviderLinks);
                    demandAndProviderLinks = aggregator.GetArrowsFrom(demandOrProvider);
                    dbTransactionData.DeleteAllFrom(demandAndProviderLinks);
                }
                else
                {
                    throw new MrpRunException("In this case not possible");
                }


                dbTransactionDataArchive.AddA(demandOrProvider);
                dbTransactionData.DeleteA(demandOrProvider);
            }
        }
        private static Quantity CalculateReservedQuantity(Demand demand)
        {
            IAggregator aggregator       = ZppConfiguration.CacheManager.GetAggregator();
            Quantity    reservedQuantity = Quantity.Null();

            IEnumerable <ILinkDemandAndProvider> arrowsToDemand = aggregator.GetArrowsTo(demand);

            if (arrowsToDemand != null)
            {
                foreach (var arrowToDemand in arrowsToDemand)
                {
                    reservedQuantity.IncrementBy(arrowToDemand.GetQuantity());
                }
            }

            return(reservedQuantity);
        }