Esempio n. 1
0
        /// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
        /// <name>SaveAthleteSeasonData</name>
        /// <date>30/03/15</date>
        /// <summary>
        /// Reads the athlete season details xml from file and decodes it.
        /// </summary>
        /// <param name="fileName">name of xml file</param>
        /// <returns>decoded athlete's details</returns>
        /// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
        public List <IClubSeasonDetails> LoadClubSeasonData(string fileName)
        {
            List <IClubSeasonDetails> seasonDetails = new List <IClubSeasonDetails>();

            try
            {
                XDocument reader      = XDocument.Load(fileName);
                XElement  rootElement = reader.Root;

                var clubList = from Club in rootElement.Elements(c_clubElement)
                               select new
                {
                    name   = (string)Club.Attribute(nameAttribute),
                    points = from Points in Club.Elements(c_pointsElement)
                             select new
                    {
                        point = from Point in Points.Elements(c_eventPointsElement)
                                select new
                        {
                            finishing = (int)Point.Attribute(finishingPointsAttribute),
                            position  = (int)Point.Attribute(positionPointsAttribute),
                            best      = (int)Point.Attribute(bestPointsAttribute),
                            date      = (string)Point.Attribute(eventDateAttribute)
                        }
                    },
                    harmonyPoints = from HarmonyPoints in Club.Elements(HarmonyPointsElement)
                                    select new
                    {
                        events = from HarmonyEvent in HarmonyPoints.Elements(EventElement)
                                 select new
                        {
                            size         = (int)HarmonyEvent.Attribute(HarmonyTeamSizeAttribute),
                            virtualPoint = (int)HarmonyEvent.Attribute(HarmonyVirtualAthletePointAttribute),
                            date         = (string)HarmonyEvent.Attribute(HarmonyEventDateAttribute),
                            score        = (int)HarmonyEvent.Attribute(HarmonyScoreAttribute),
                            points       = from Point in HarmonyEvent.Elements(c_eventPointsElement)
                                           select new
                            {
                                value = (int)Point.Attribute(HarmonyPointAttribute),
                                key   = (int)Point.Attribute(AthleteKeyAttribute)
                            }
                        }
                    }
                };

                foreach (var club in clubList)
                {
                    ClubSeasonDetails clubDetails = new ClubSeasonDetails(club.name);

                    foreach (var points in club.points)
                    {
                        foreach (var point in points.point)
                        {
                            DateType date =
                                new DateType(
                                    point.date);
                            CommonPoints readPoints =
                                new CommonPoints(
                                    point.finishing,
                                    point.position,
                                    point.best,
                                    date);


                            clubDetails.ClubCompetition.AddNewEvent(readPoints);
                            // TODO, should probably check that there are the correct number read from the xml file.
                            // i.e. there is one for each event in the currently loaded season.
                        }
                    }

                    foreach (var harmonyPoints in club.harmonyPoints)
                    {
                        foreach (var harmonyEvent in harmonyPoints.events)
                        {
                            List <ICommonHarmonyPoints> pointsList = new List <ICommonHarmonyPoints>();
                            DateType date =
                                new DateType(
                                    harmonyEvent.date);

                            foreach (var point in harmonyEvent.points)
                            {
                                CommonHarmonyPoints readPoints =
                                    new CommonHarmonyPoints(
                                        point.value,
                                        string.Empty,
                                        point.key,
                                        true,
                                        date);

                                pointsList.Add(readPoints);
                            }

                            IHarmonyEvent readEvent =
                                new HarmonyEvent(
                                    date,
                                    pointsList,
                                    harmonyEvent.size,
                                    harmonyEvent.score);
                            readEvent.Complete(
                                harmonyEvent.size,
                                harmonyEvent.virtualPoint);

                            clubDetails.HarmonyCompetition.AddEvent(readEvent);
                        }
                    }


                    seasonDetails.Add(clubDetails);
                }
            }
            catch (Exception ex)
            {
                this.logger.WriteLog("Error reading athlete data: " + ex.ToString());

                seasonDetails = new List <IClubSeasonDetails>();
            }

            return(seasonDetails);
        }