} // end ClearCalculatedTables public void MakePopulationIDs(List <SampleGroupDO> sgList, List <TreeDefaultValueDO> tdvList) { CPbusinessLayer bslyr = new CPbusinessLayer(); bslyr.DAL = DAL; bslyr.fileName = fileName; // Load ID info into tables List <LCDDO> lcdList = new List <LCDDO>(); List <POPDO> popList = new List <POPDO>(); List <PRODO> proList = new List <PRODO>(); List <TreeDO> tList = bslyr.getTrees(); // need to check Contract Species in TDV table before doing unique // reset to a blank if it is null -- September 2016 List <TreeDefaultValueDO> treeDefaults = bslyr.getTreeDefaults(); foreach (TreeDefaultValueDO tdv in treeDefaults) { if (tdv.ContractSpecies == null) { tdv.ContractSpecies = " "; } else if (tdv.ContractSpecies == "") { tdv.ContractSpecies = " "; } } // end foreach bslyr.SaveTreeDefaults(treeDefaults); foreach (SampleGroupDO sgd in sgList) { // Load LCD population IDs // Need unique species, livedead and grade from Tree table // not sure about the count table -- need IDs from there? 11/2012 List <TreeDO> distinctSpecies = bslyr.GetDistinctSpecies((long)sgd.SampleGroup_CN); foreach (TreeDO t in distinctSpecies) { LCDDO lcd = new LCDDO(); lcd.CutLeave = sgd.CutLeave; lcd.Stratum = sgd.Stratum.Code; lcd.SampleGroup = sgd.Code; lcd.PrimaryProduct = sgd.PrimaryProduct; lcd.SecondaryProduct = sgd.SecondaryProduct; lcd.UOM = sgd.UOM; lcd.Species = t.Species; lcd.LiveDead = t.LiveDead; if (t.Grade == null) { lcd.TreeGrade = ""; } else { lcd.TreeGrade = t.Grade; } lcd.STM = t.STM; // per K.Cormier, because a null value in contract species causes // causes problems, it is being dropped from the population ID for LCD // Howeverm it will be stored as null but not used as part of the ID // September 2016 // Found a slicker way to address this -- default contract species to a blank // however, this would be in the TreeDefaultValue table and only affects // new cruises. CP still needs to set CS in TDV to blank (see above). lcd.ContractSpecies = t.TreeDefaultValue.ContractSpecies; //if (t.TreeDefaultValue.ContractSpecies != null) // lcd.ContractSpecies = t.TreeDefaultValue.ContractSpecies; //else lcd.ContractSpecies = ""; if (t.Stratum.YieldComponent != null) { lcd.Yield = t.Stratum.YieldComponent; } else { lcd.Yield = ""; } //} // endif nthRow lcdList.Add(lcd); } // end foreach loop on species // Load POP population IDs POPDO pop = new POPDO(); // Don't need unique species for this group pop.CutLeave = sgd.CutLeave; pop.Stratum = sgd.Stratum.Code; pop.SampleGroup = sgd.Code; pop.PrimaryProduct = sgd.PrimaryProduct; pop.SecondaryProduct = sgd.SecondaryProduct; pop.UOM = sgd.UOM; // check for sure-to-measure trees for this group // Add non-sure-to-measure first and then STM pop.STM = "N"; popList.Add(pop); List <TreeDO> justSTM = tList.FindAll( delegate(TreeDO td) { return(sgd.CutLeave == td.SampleGroup.CutLeave && sgd.Stratum.Code == td.Stratum.Code && sgd.Code == td.SampleGroup.Code && sgd.PrimaryProduct == td.SampleGroup.PrimaryProduct && sgd.SecondaryProduct == td.SampleGroup.SecondaryProduct && sgd.UOM == td.SampleGroup.UOM && td.STM == "Y"); }); if (justSTM.Count > 0) { POPDO popSTM = new POPDO(); popSTM.CutLeave = sgd.CutLeave; popSTM.Stratum = sgd.Stratum.Code; popSTM.SampleGroup = sgd.Code; popSTM.PrimaryProduct = sgd.PrimaryProduct; popSTM.SecondaryProduct = sgd.SecondaryProduct; popSTM.UOM = sgd.UOM; popSTM.STM = "Y"; popList.Add(popSTM); } // endif // Load PRO population IDs // These need cutting unit numbers -- from Cutting Unit List <CuttingUnitStratumDO> strataUnits = bslyr.getCuttingUnitStratum((long)sgd.Stratum_CN); foreach (CuttingUnitStratumDO cudo in strataUnits) { PRODO pro = new PRODO(); pro.CutLeave = sgd.CutLeave; pro.Stratum = sgd.Stratum.Code; pro.CuttingUnit = cudo.CuttingUnit.Code; pro.SampleGroup = sgd.Code; pro.PrimaryProduct = sgd.PrimaryProduct; pro.SecondaryProduct = sgd.SecondaryProduct; pro.UOM = sgd.UOM; // check for sure-to-measure trees for this group // Add non-sure-to-measure first and then STM pro.STM = "N"; proList.Add(pro); justSTM = tList.FindAll( delegate(TreeDO td) { return(sgd.CutLeave == td.SampleGroup.CutLeave && sgd.Stratum.Code == td.Stratum.Code && cudo.CuttingUnit.Code == td.CuttingUnit.Code && sgd.Code == td.SampleGroup.Code && sgd.PrimaryProduct == td.SampleGroup.PrimaryProduct && sgd.SecondaryProduct == td.SampleGroup.SecondaryProduct && sgd.UOM == td.SampleGroup.UOM && td.STM == "Y"); }); if (justSTM.Count > 0) { PRODO proSTM = new PRODO(); proSTM.CutLeave = sgd.CutLeave; proSTM.Stratum = sgd.Stratum.Code; proSTM.CuttingUnit = cudo.CuttingUnit.Code; proSTM.SampleGroup = sgd.Code; proSTM.PrimaryProduct = sgd.PrimaryProduct; proSTM.SecondaryProduct = sgd.SecondaryProduct; proSTM.UOM = sgd.UOM; proSTM.STM = "Y"; proList.Add(proSTM); } // endif } // end foreach loop on strataUnits } // end foreach loop on SampleGroup bslyr.SaveLCD(lcdList); bslyr.SavePOP(popList); bslyr.SavePRO(proList); return; } // end MakePopulationIDs
} // end SumTreeCountsLCD public void SumTreeCountsPOP(string currST, List <CountTreeDO> ctList, List <PlotDO> justPlots, List <POPDO> justCurrentPOP, string currMethod, List <POPDO> popList) { CPbusinessLayer bslyr = new CPbusinessLayer(); bslyr.DAL = DAL; bslyr.fileName = fileName; // also need first and second stage sample counts double stage1 = 0.0; double stage2 = 0.0; // Sums trees and counts from either the Count Tree table or tree count records foreach (POPDO pop in justCurrentPOP) { firstSum = 0.0; talliedSum = 0.0; totalKPI = 0.0; totalMeasuredKPI = 0.0; // find measured trees for current POP group List <TreeDO> popTrees = bslyr.getPOPtrees(pop, "M"); // measured tree is just a count of the trees for the current group pop.MeasuredTrees = popTrees.Count(); // now need count trees from tree count records List <TreeDO> popCntTrees = bslyr.getPOPtrees(pop, "C"); // sum all tree counts for first stage and total tallied if (currMethod == "STR" || currMethod == "100") { firstSum = popTrees.Count(); // I thought tree count was supposed to default to 1 for 100% -- Feb 2014 // Apparently, FScruiser can put whatever it wants in tree count } else if (currMethod != "3P") { firstSum = popTrees.Sum(tdo => tdo.TreeCount); firstSum += popCntTrees.Sum(tdo => tdo.TreeCount); } if (pop.STM == "Y") { firstSum += pop.MeasuredTrees; talliedSum += pop.MeasuredTrees; //talliedSum = popTrees.Sum(tdo => tdo.TreeCount); } else { // see note above on 100% method //if (currMethod == "3P" || currMethod == "100") if (currMethod == "100") { talliedSum += pop.MeasuredTrees; } else { talliedSum = popTrees.Sum(tdo => tdo.TreeCount); talliedSum += popCntTrees.Sum(tdo => tdo.TreeCount); } // endif on current method } // endif // insurance trees go into just the tallied tree count List <TreeDO> lcdInsurance = bslyr.getPOPtrees(pop, "I"); talliedSum += lcdInsurance.Sum(tdo => tdo.TreeCount); // Complete totals for tree count and stage samples and KPIs based on method stage1 = justPlots.Count(); List <CountTreeDO> popCountCounts = new List <CountTreeDO>(); switch (currMethod) { case "100": stage1 = popTrees.Count(); break; case "STR": stage1 = popTrees.Count(); popCountCounts = ctList.FindAll( delegate(CountTreeDO ctd) { return(pop.CutLeave == ctd.SampleGroup.CutLeave && pop.Stratum == ctd.SampleGroup.Stratum.Code && pop.SampleGroup == ctd.SampleGroup.Code && pop.PrimaryProduct == ctd.SampleGroup.PrimaryProduct && pop.SecondaryProduct == ctd.SampleGroup.SecondaryProduct && pop.UOM == ctd.SampleGroup.UOM); }); talliedSum += popCountCounts.Sum(ctd => ctd.TreeCount); break; case "S3P": case "3P": if (currMethod == "S3P") { stage2 = popTrees.Count(); } popCountCounts = ctList.FindAll( delegate(CountTreeDO ctd) { return(pop.CutLeave == ctd.SampleGroup.CutLeave && pop.Stratum == ctd.SampleGroup.Stratum.Code && pop.SampleGroup == ctd.SampleGroup.Code && pop.PrimaryProduct == ctd.SampleGroup.PrimaryProduct && pop.SecondaryProduct == ctd.SampleGroup.SecondaryProduct && pop.UOM == ctd.SampleGroup.UOM); }); if (pop.STM == "N") { stage1 = popTrees.Count(); firstSum += pop.MeasuredTrees; talliedSum += popCountCounts.Sum(ctd => ctd.TreeCount); totalKPI += popCountCounts.Sum(ctd => ctd.SumKPI); } // Sum measured KPI from trees totalMeasuredKPI = popTrees.Sum(tdo => tdo.KPI); // for S3P and 3P, sum of KPI is total of count and measured KPI from the trees == November 2013 // even though individual KPI may be zero for either one. // however, countTree has sum of count and measured KPI for 3P so adding measured and count // from trees will double count for 3P ONLY -- December 2013 if (currMethod == "S3P") { totalKPI += popCntTrees.Sum(lc => lc.KPI); totalKPI += popTrees.Sum(lt => lt.KPI); } // endif on method break; case "PCM": case "PCMTRE": stage2 = popTrees.Count(); break; case "F3P": case "P3P": stage2 = popTrees.Count(); totalMeasuredKPI = popTrees.Sum(tdo => tdo.KPI); totalKPI = popCntTrees.Sum(tdo => tdo.KPI); totalKPI += totalMeasuredKPI; totalKPI += popCountCounts.Sum(ctd => ctd.SumKPI); break; case "FCM": // August 2014 -- according to Ken C. this needs to be just the total measured trees // and not the sum of tree counts. Some tree counts could be zero when they should be one. //stage2 = popTrees.Sum(p => p.TreeCount); stage2 = pop.MeasuredTrees; break; case "3PPNT": // Stage 2 samples foreach (PlotDO pdo in justPlots) { int nthRow = popTrees.FindIndex( delegate(TreeDO tdo) { return(tdo.Plot_CN == pdo.Plot_CN); }); if (nthRow >= 0) { stage2++; nthRow = -1; } // endif nthRow } // end foreach loop // this will probably no longer work on legacy data until // the conversion program handles this change if (popTrees.Count > 0) { // means there are measured trees so this was a measured plot // KPI in the plot table is measured totalMeasuredKPI = justPlots.Sum(pd => pd.KPI); // plus add to total KPI totalKPI = justPlots.Sum(pd => pd.KPI); } else { // no measured trees means this is a count plot // just add plot KPI to total KPI totalKPI += justPlots.Sum(pd => pd.KPI); } // endif // this piece of code will no longer work -- September 2013 //totalMeasuredKPI = lcdTrees.Sum(tdo => tdo.KPI); //totalKPI += lcdCountCounts.Sum(ctd => ctd.SumKPI); // Add plot KPI to total KPI //totalKPI += justPlots.Sum(pd => pd.KPI); //totalMeasuredKPI = popTrees.Sum(tdo => tdo.KPI); //totalKPI += popCntTrees.Sum(tdo => tdo.KPI); // Also need plot KPI for this method //totalKPI += justPlots.Sum(pd => pd.KPI); break; } // end switch on method pop.FirstStageTrees = firstSum; pop.TalliedTrees = talliedSum; pop.SumKPI = totalKPI; pop.SumMeasuredKPI = totalMeasuredKPI; pop.StageOneSamples = stage1; pop.StageTwoSamples = stage2; } // end foreach loop // Save list before continuing bslyr.SavePOP(popList); return; } // end SumTreeCountsPOP