public TreeDefaultValueDO CreateNewTreeDefaultValue(String pProd)
        {
            TreeDefaultValueDO newTDV = new TreeDefaultValueDO();
            newTDV.DAL = this._cDal;
            newTDV.PrimaryProduct = pProd;
            newTDV.LiveDead = "L";

            if (this.ViewController.ShowEditTreeDefault(newTDV) == DialogResult.OK)
            {
                try
                {
                    newTDV.Save();
                    return newTDV;
                }
                catch (Exception e)
                {
                    ExceptionHandler.HandelEx(new UserFacingException("oops Tree Default save error", e));
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
 public override TreeDefaultValueDO ShowAddTreeDefault(TreeDefaultValueDO newTDV)
 {
     try
     {
         FormAddTreeDefault dialog = new FormAddTreeDefault(ApplicationController.SetupService.GetProductCodes());
         if (dialog.ShowDialog(newTDV) == DialogResult.OK)
         {
             newTDV.Save();
             return newTDV;
         }
         else
         {
             return null;
         }
     }
     catch (Exception ex)
     {
         if (!this.ApplicationController.ExceptionHandler.Handel(ex))
         {
             throw;
         }
         else
         {
             return null;
         }
     }
 }
        public override void ShowEditTreeDefault(TreeDefaultValueDO tdv)
        {
            TreeDefaultValueDO temp = new TreeDefaultValueDO(tdv);

            try
            {
                using (FormAddTreeDefault dialog = new FormAddTreeDefault(this.ApplicationController.SetupService.GetProductCodes()))
                {
                    if (dialog.ShowDialog(temp) == DialogResult.OK)
                    {
                        try
                        {
                            tdv.SetValues(temp);
                            tdv.Save();
                        }
                        catch (FMSC.ORM.UniqueConstraintException ex)
                        {
                            throw new UserFacingException("Values Conflict With Existing Tree Default", ex);
                        }
                        catch (FMSC.ORM.ConstraintException ex)
                        {
                            throw new UserFacingException("Invalid Values", ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (!this.ApplicationController.ExceptionHandler.Handel(ex))
                {
                    throw;
                }
            }
        }
        //*************************************************************************
        private void getSampleGroupStats()
        {
            calcStats statClass = new calcStats();
             stage = statClass.isTwoStage(Owner.currentStratum.Method);

             double comberr2 = 0;
             double totalVolume = 0;
             // for each sample group
             foreach (SampleGroupDO sg in Owner.histSampleGroup)
             {
            // create samplegroupstats
            Owner.currentSgStats = new SampleGroupStatsDO(Owner.cdDAL);
            //set foriegn key
            Owner.currentSgStats.StratumStats = Owner.currentStratumStats;
            Owner.currentSgStats.SgSet = 1;
            Owner.currentSgStats.Code = sg.Code;
            Owner.currentSgStats.CutLeave = sg.CutLeave;
            Owner.currentSgStats.UOM = sg.UOM;
            Owner.currentSgStats.PrimaryProduct = sg.PrimaryProduct;
            Owner.currentSgStats.SecondaryProduct = sg.SecondaryProduct;
            Owner.currentSgStats.DefaultLiveDead = sg.DefaultLiveDead;
            Owner.currentSgStats.Description = sg.Description;
            // get POP data
            //Owner.selectedPOP = new POPDO(Owner.hDAL);
            Owner.selectedPOP = Owner.hDAL.ReadSingleRow<POPDO>("POP", "WHERE Stratum = ? AND SampleGroup = ?", Owner.selectedStratum.Code, sg.Code);
            // calculate statistics (based on method)
            if (Owner.selectedPOP == null)
            {
               MessageBox.Show("Cruise Not Processed. Cannot Import Data.", "Warning");
               return;
            }
            n1 = Convert.ToInt32(Owner.selectedPOP.StageOneSamples);
            n2 = Convert.ToInt32(Owner.selectedPOP.StageTwoSamples);
            measTrees = Convert.ToInt32(Owner.selectedPOP.MeasuredTrees);
            talliedTrees = Convert.ToInt32(Owner.selectedPOP.TalliedTrees);
            sumKpi = Convert.ToInt32(Owner.selectedPOP.SumKPI);
            st1x = Owner.selectedPOP.Stg1NetXPP;
            st1x2 = Owner.selectedPOP.Stg1NetXsqrdPP;
            st2x = Owner.selectedPOP.Stg2NetXPP;
            st2x2 = Owner.selectedPOP.Stg2NetXsqrdPP;
            // trees per plot
            // find CVs
            cv1 = statClass.getCV(st1x, st1x2, n1);
            if (stage == 12 || stage == 22)
               cv2 = statClass.getCV(st2x, st2x2, n2);
            else
               cv2 = 0;
            // find errors stage 11=tree,single 12=tree,2 stage 21=plot,single 22=plot,2 stage
            if (stage == 11 || stage == 21)
               sampErr = statClass.getSampleError(cv1, n1, 0);
            else if (stage == 12 || stage == 22)
               sampErr = statClass.getTwoStageError(cv1, cv2, n1, n2);
            else
               sampErr = 0;
            Owner.currentSgStats.CV1 = Convert.ToSingle(cv1);
            Owner.currentSgStats.CV2 = Convert.ToSingle(cv2);
            Owner.currentSgStats.SampleSize1 = n1;
            Owner.currentSgStats.SampleSize2 = n2;
            // calculate frequency

            Owner.currentSgStats.SgError = Convert.ToSingle(sampErr);

            // get LCD data
            Owner.selectedLCD = Owner.hDAL.Read<LCDDO>("LCD", "WHERE Stratum = ? AND SampleGroup = ?", Owner.selectedStratum.Code, sg.Code);
            sumExpFac = 0;
            sumNetVol = 0;
            //foreach (SampleGroupDO sg in Owner.histSampleGroup)
            foreach (LCDDO lcd in Owner.selectedLCD)
            {
               // sum volume
               double expFac = lcd.SumExpanFactor;
               sumExpFac += expFac;
               // sum trees
               double netVol = lcd.SumNCUFT;
               sumNetVol += netVol;
            }
            comberr2 += (sampErr * sumNetVol) * (sampErr * sumNetVol);
            totalVolume += sumNetVol;
            // find volume/acre and trees/acre
            if (stage < 20)
            {
               Owner.currentSgStats.TreesPerAcre = Convert.ToSingle(Math.Round((sumExpFac / totalAcres), 2));
               Owner.currentSgStats.VolumePerAcre = Convert.ToSingle(Math.Round((sumNetVol / totalAcres), 2));
            }
            else
            {
               Owner.currentSgStats.TreesPerAcre = Convert.ToSingle(Math.Round((sumExpFac), 2));
               Owner.currentSgStats.VolumePerAcre = Convert.ToSingle(Math.Round((sumNetVol), 2));
               if(stage == 21)
                  Owner.currentSgStats.TreesPerPlot = Convert.ToSingle(Math.Round((Convert.ToSingle((float)measTrees / (float)n1)), 1));
               else
                  Owner.currentSgStats.TreesPerPlot = Convert.ToSingle(Math.Round((Convert.ToSingle((float)talliedTrees / (float) n1)), 1));
            }
            // find frequency/KZ/BigBAF values
            if ((stage == 11 || stage == 12 || stage == 22) && measTrees > 0)
            {
               freq = Convert.ToInt32((talliedTrees / measTrees));
               kz = Convert.ToInt32((sumKpi / measTrees));

               if (Owner.currentStratum.Method == "S3P")
               {
                  Owner.currentSgStats.SamplingFrequency = Convert.ToInt32((talliedTrees / n1));
                  Owner.currentSgStats.KZ = kz;
               }
               else
               {
                  Owner.currentSgStats.SamplingFrequency = freq;
                  Owner.currentSgStats.KZ = kz;
               }
            }

            // save samplegroupstats row
            Owner.currentSgStats.Save();
            // loop through TDV information
            sg.TreeDefaultValues.Populate();
            foreach (TreeDefaultValueDO tdv in sg.TreeDefaultValues)
            {
               // check with current TDV values
               Owner.currentTreeDefaults = Owner.cdDAL.ReadSingleRow<TreeDefaultValueDO>("TreeDefaultValue", "WHERE Species = ? AND PrimaryProduct = ? AND LiveDead = ?",tdv.Species,tdv.PrimaryProduct,tdv.LiveDead);
               if (Owner.currentTreeDefaults == null)
               {
                  // if null, add tdv to list then create link
                  TreeDefaultValueDO newTdv = new TreeDefaultValueDO(Owner.cdDAL);
                  newTdv.SetValues(tdv);
                  newTdv.Save();
                  Owner.currentSgStats.TreeDefaultValueStats.Add(newTdv);
               }
               else
               {
                  // if exists, create link
                  Owner.currentSgStats.TreeDefaultValueStats.Add(Owner.currentTreeDefaults);
               }
            }

            Owner.currentSgStats.Save();
            Owner.currentSgStats.TreeDefaultValueStats.Save();

             }
             // calculate stats for stratum
            // double stratumError = Math.Sqrt(comberr2) / totalVolume;
             // wted CVs
             //Owner.currentStratumStats.StrError = Convert.ToSingle(stratumError);

             //Owner.currentStratumStats.Save();
        }