///** ************************
        public static FlowTerminationModel ResolveBackground(this IRepository<Background> repository, 
						     int? flowId, int directionId, int scenarioId)
        {
            var background = new FlowTerminationModel();
            int targetId;

            background = repository.GetRepository<BackgroundSubstitution>().Queryable()
                .Where(x => x.ScenarioID == scenarioId)
                .Where(x => x.FlowID == flowId)
                .Where(x => x.DirectionID == directionId)
               .Select(bg => new FlowTerminationModel
               {
                   NodeTypeID = bg.NodeTypeID,
                   ScenarioID = scenarioId,
                   ILCDEntityID = (int)bg.ILCDEntityID,
                   TermFlowID = (int)flowId
               }).FirstOrDefault();

            if (background == null)
            {
                background = repository.GetRepository<Background>().Queryable()
                .Where(x => x.FlowID == flowId)
                .Where(x => x.DirectionID == directionId)
                .Select(bg => new FlowTerminationModel
                {
                    NodeTypeID = bg.NodeTypeID,
                    ScenarioID = scenarioId,
                    ILCDEntityID = (int)bg.ILCDEntityID,
                    TermFlowID = (int)flowId
                }).FirstOrDefault();

                if (background == null)
                {
                    throw new ArgumentNullException("background is null");
                }
            }

            switch (background.NodeTypeID)
            {
                case 1:
                    targetId = repository.GetRepository<Process>().Queryable()
                        .Where(x => x.ILCDEntityID == background.ILCDEntityID)
                        .Select(z => (int)z.ProcessID).FirstOrDefault();
                    background.ProcessID = targetId;
                    break;
                case 2:
                    targetId = repository.GetRepository<Fragment>().Queryable()
                        .Where(x => x.ILCDEntityID == background.ILCDEntityID)
                        .Select(z => (int)z.FragmentID).FirstOrDefault();
                    background.SubFragmentID = targetId;
                    break;
            }
            return background;
        }
 public static double GetNodeScaling(this IRepositoryAsync<FragmentFlow> repository, 
     FlowTerminationModel term, int inFlowId, int directionId, int scenarioId )
 {
     var flow_conv = (double)repository.GetRepository<FlowFlowProperty>()
         .FlowConv(term.TermFlowID, inFlowId, scenarioId);
     if (term.NodeTypeID == 1)
         return flow_conv /
             (double)repository.GetRepository<ProcessFlow>()
             .FlowExchange((int)term.ProcessID, term.TermFlowID, directionId);
     else
         return flow_conv;
 }
 public static IEnumerable<FragmentFlowModel> LookupDependencies(this IRepository<ProcessFlow> repository,
     int fragmentFlowId, FlowTerminationModel term, int scenarioId)
 {
     return repository.Queryable()
         .Where(pf => pf.ProcessID == term.ProcessID)
         .Where(pf => pf.Flow.FlowTypeID == 1)
         .GroupJoin(repository.GetRepository<FragmentFlow>().Queryable()
             .Where(ff => ff.ParentFragmentFlowID == fragmentFlowId),
             pf => new { pf.FlowID, pf.DirectionID },
             ff => new { ff.FlowID, ff.DirectionID },
             (pf,ff) => new { baseflows = pf, fragflows = ff })
         .SelectMany(s => s.fragflows.DefaultIfEmpty(), (s,fragflows) => new { pfff = s, ffid = fragflows })
         .GroupJoin(repository.GetRepository<DependencyParam>().Queryable()
             .Where(dp => dp.Param.ScenarioID == scenarioId),
             s => s.ffid.FragmentFlowID,
             dp => dp.FragmentFlowID,
             (s,dp) => new { pfffdp = s, dp = dp })
         .SelectMany(k => k.dp.DefaultIfEmpty(), (k,dp) => new FragmentFlowModel()
         {
             FlowID = k.pfffdp.pfff.baseflows.FlowID,
             DirectionID = k.pfffdp.pfff.baseflows.DirectionID,
             Result = (dp == null) ? k.pfffdp.pfff.baseflows.Result
                                   : dp.Value,
             StDev = k.pfffdp.pfff.baseflows.STDev,
             FragmentFlowID = k.pfffdp.ffid.FragmentFlowID
         });
 }
        public static IEnumerable<ConservationModel> LookupConservationFlows(this IRepository<ProcessFlow> repository,
            int fragmentFlowId, FlowTerminationModel term, int scenarioId)
        {
            int refProp = repository.GetRepository<FragmentFlow>().Queryable().Where(ff => ff.FragmentFlowID == term.BalanceFFID)
                .Select(ff => ff.Flow.ReferenceFlowProperty).First();

            return repository.Queryable()
                .Where(pf => pf.ProcessID == term.ProcessID)
                .Where(pf => pf.Flow.FlowTypeID == 1)
                .GroupJoin(repository.GetRepository<FlowFlowProperty>().Queryable()
                    .Where(ffp => ffp.FlowPropertyID == refProp),
                    pf => pf.FlowID,
                    ffp => ffp.FlowID,
                    (pf, ffp) => new { pf, ffp })
                .SelectMany(r => r.ffp.DefaultIfEmpty(), (r, ffp) => new { pfs = r.pf, ffps = ffp })
                .GroupJoin(repository.GetRepository<FragmentFlow>().Queryable()
                    .Where(ff => ff.ParentFragmentFlowID == fragmentFlowId),
                    pf => new { pf.pfs.FlowID, pf.pfs.DirectionID },
                    ff => new { ff.FlowID, ff.DirectionID },
                    (pf, ff) => new { baseflows = pf, fragflows = ff })
                .SelectMany(s => s.fragflows.DefaultIfEmpty(), (s, fragflows) => new { pfff = s, ffid = fragflows })
                .GroupJoin(repository.GetRepository<DependencyParam>().Queryable()
                    .Where(dp => dp.Param.ScenarioID == scenarioId),
                    s => s.ffid.FragmentFlowID,
                    dp => dp.FragmentFlowID,
                    (s, dp) => new { pfffdp = s, dp = dp })
                .SelectMany(k => k.dp.DefaultIfEmpty(), (k, dp) => new ConservationModel()
                {
                    FlowID = k.pfffdp.pfff.baseflows.pfs.FlowID,
                    DirectionID = k.pfffdp.pfff.baseflows.pfs.DirectionID,
                    Result = (dp == null) ? k.pfffdp.pfff.baseflows.pfs.Result
                                          : dp.Value,
                    StDev = k.pfffdp.pfff.baseflows.pfs.STDev,
                    FragmentFlowID = k.pfffdp.ffid.FragmentFlowID,
                    FlowPropertyValue = k.pfffdp.pfff.baseflows.ffps == null ? 0
                                      : k.pfffdp.pfff.baseflows.ffps.MeanValue
                });
        }
        /// <summary>
        /// Lazy flow termination-- FNF and FNP are looked up now
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="ff"></param>
        /// <param name="scenarioId"></param>
        /// <param name="doBackground"></param>
        /// <returns></returns>
        public static FlowTerminationModel LTerminate(this IRepository<FragmentFlow> repository, 
						     NodeCacheModel ff, int scenarioId, bool doBackground)
        {
            var fragmentNode = new FlowTerminationModel();
            int inFlowId;
            switch (ff.NodeTypeID)
            {
                case 1:
                    {
                        fragmentNode = repository.LGetFragmentNodeProcess(ff.FragmentFlowID, scenarioId);
                        break;
                    }
                case 2:
                    {
                        fragmentNode = repository.LGetFragmentNodeFragment(ff.FragmentFlowID, scenarioId);
                        break;
                    }
                case 3:
                {
                    if (ff.FlowID == null)
                    {
                        throw new ArgumentNullException("FragmentFlow.FlowID must be set!");
                    }
                    fragmentNode.NodeTypeID = 3;
                    //fragmentNode.ILCDEntityID = ff.FragmentFlowID;
                    fragmentNode.ScenarioID = scenarioId;
                    fragmentNode.TermFlowID = (int)ff.FlowID;
                    break;
                }
                default:
                {
                    if (ff.FlowID == null)
                    {
                        throw new ArgumentNullException("FragmentFlow.FlowID must be set!");
                    }
                    else inFlowId = (int)ff.FlowID;
                    if (doBackground)
                    {
                        fragmentNode = repository.GetRepository<Background>()
                            .ResolveBackground(inFlowId, ff.DirectionID, scenarioId);
                    }
                    else
                    {
                        //fragmentNode.ILCDEntityID = ff.FragmentFlowID;
                        fragmentNode.ScenarioID = scenarioId;
                        fragmentNode.TermFlowID = inFlowId;
                    }
                    break;
                }
              	        }
            return fragmentNode;
        }