private ScheduleOfRITEs BuildScheduleFromRITChars(string name, HashSet <long> EDSschedule)
        {
            ScheduleOfRITEs newSchedule = new ScheduleOfRITEs(name);

            foreach (long RITCharID in EDSschedule)
            {
                long RITEID = EDSDataExtract.CoverageIdAttrMap[RITCharID].RITExposureId;

                RITCharacteristic characteristicToAdd = _characteristics.Where(ritchar => ritchar.ID == RITCharID).FirstOrDefault();
                RITE ritetoAdd = _rites.Where(rite => rite.ExposureID == RITEID).FirstOrDefault();

                if (characteristicToAdd != null)
                {
                    newSchedule.AddCharacteristic(characteristicToAdd);
                }
                else
                {
                    throw new IndexOutOfRangeException("Error building schedule with RITCharacteristics: Cannot find RITCharacteristic with id = " + RITCharID + " in Exposure Data");
                }

                if (ritetoAdd != null)
                {
                    newSchedule.AddItem(ritetoAdd);
                }
                else
                {
                    throw new IndexOutOfRangeException("Error building schedule with RITEs: Cannot find RITE with id = " + RITEID + " in Exposure Data");
                }
            }

            return(newSchedule);
        }
        private ScheduleOfRITEs BuildScheduleFromRITs(string name, HashSet <long> EDSschedule)
        {
            ScheduleOfRITEs newSchedule = new ScheduleOfRITEs(name);

            foreach (long riteID in EDSschedule)
            {
                RITE ritetoAdd = _rites.Where(rite => rite.ExposureID == riteID).FirstOrDefault();

                if (ritetoAdd != null)
                {
                    newSchedule.AddItem(ritetoAdd);
                    newSchedule.RITChars.UnionWith(ritetoAdd.RiskCharacteristics);
                }
                else
                {
                    throw new IndexOutOfRangeException("Error building schedule with RITEs: Cannot find RITE with id = " + riteID + " in Exposure Data");
                }
            }

            return(newSchedule);
        }
        public void GetPrimaryData()
        {
            if (TreatyExposure)
            {
                throw new InvalidOperationException("Cannot use this method to get exposure data for reinsurance contract");
            }

            EDSDataExtract.ExtractRiteMap();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var LocationGroups = EDSDataExtract.CoverageIdAttrMap.GroupBy(pair => pair.Value.RITExposureId);

            foreach (var LocGroup in LocationGroups)
            {
                RITE newRITE = BuildRITE(LocGroup.First());
                _rites.Add(newRITE);
                _ritesDict.Add(LocGroup.Key, newRITE);

                foreach (KeyValuePair <long, RITEAttributes> pair in LocGroup)
                {
                    RITCharacteristic newRITChar = BuildRITCharacteristic(pair);
                    _characteristics.Add(newRITChar);
                    _characteristicsDict.Add(pair.Key, newRITChar);
                    newRITE.AddCharacteristic(newRITChar);
                }
            }


            foreach (KeyValuePair <string, Subschedule> EDSschedule in EDSDataExtract.SubSchedule)
            {
                ScheduleOfRITEs newSchedule;
                HashSet <long>  Ids = (EDSschedule.Value.Ids != null) ? EDSschedule.Value.Ids : new HashSet <long>(EDSschedule.Value.CompressedIds.Enumerable());
                if (EDSschedule.Value.Type == SubscheduleType.RITCHARACTERISTICS)
                {
                    newSchedule = BuildScheduleFromRITChars2(EDSschedule.Key, Ids);
                }
                else if (EDSschedule.Value.Type == SubscheduleType.RITEXPOSURES)
                {
                    newSchedule = BuildScheduleFromRITs2(EDSschedule.Key, Ids);
                }
                else
                {
                    throw new InvalidOperationException("Cannot support building schedules of type: " + EDSschedule.Value.Type.ToString());
                }

                _schedules.Add(newSchedule);
                _schedulesDict.Add(newSchedule.Name, newSchedule);
            }

            stopwatch.Stop();

            ////////////////////////OLD OLD OLD Version//////////////////////////////////
            //_schedules.Clear();
            //_rites.Clear();
            //_characteristics.Clear();

            //stopwatch.Reset();
            //stopwatch.Start();

            //foreach (KeyValuePair<long, RITEAttributes> RiteAttribute in EDSDataExtract.CoverageIdAttrMap)
            //{
            //    RITCharacteristic newRITChar = BuildRITCharacteristic(RiteAttribute);
            //    _characteristics.Add(newRITChar);
            //    RITE parentRITE = _rites.Where(rite => rite.ExposureID == RiteAttribute.Value.RITExposureId).FirstOrDefault();

            //    if (parentRITE != null)
            //    {
            //        parentRITE.AddCharacteristic(newRITChar);
            //    }
            //    else
            //    {
            //        RITE newRITE = BuildRITE(RiteAttribute);
            //        _rites.Add(newRITE);
            //        newRITE.AddCharacteristic(newRITChar);
            //    }

            //}

            //foreach (KeyValuePair<string, Subschedule> EDSschedule in EDSDataExtract.SubSchedule)
            //{
            //    ScheduleOfRITEs newSchedule;
            //    HashSet<long> Ids = (EDSschedule.Value.Ids != null) ? EDSschedule.Value.Ids : new HashSet<long>(EDSschedule.Value.CompressedIds.Enumerable());
            //    if (EDSschedule.Value.Type == SubscheduleType.RITCHARACTERISTICS)
            //        newSchedule = BuildScheduleFromRITChars(EDSschedule.Key, Ids);
            //    else if (EDSschedule.Value.Type == SubscheduleType.RITEXPOSURES)
            //        newSchedule = BuildScheduleFromRITs(EDSschedule.Key, Ids);
            //    else
            //        throw new InvalidOperationException("Cannot support building schedules of type: " + EDSschedule.Value.Type.ToString());

            //    _schedules.Add(newSchedule);
            //}

            //stopwatch.Stop();
        }