Ejemplo n.º 1
0
        /// <summary>
        /// Return Timesheet object based upon the contents of a HTML string representing the timesheet webpage.
        /// Return the viewstate as an out parameter for further operations
        /// </summary>
        /// <param name="timesheetHtml"></param>
        /// <param name="viewstate"></param>
        /// <returns></returns>
        /// <exception cref="ApplicationException">Invalid timesheet ID throws ApplicationException</exception>
        public override Timesheet ParseTimesheet(string timesheetHtml, out string viewstate)
        {
            var timesheet = new Timesheet();

            viewstate = string.Empty;

            HtmlNode.ElementsFlags.Remove("option");
            var htmlDoc      = new HtmlDocument();
            var stringReader = new StringReader(timesheetHtml);

            htmlDoc.Load(stringReader);

            if (htmlDoc.DocumentNode != null)
            {
                HtmlParser.AssertNoErrors(htmlDoc.DocumentNode, LoginErrorSelector);

                try
                {
                    // Timesheet title, e.g. '08 Jul 2013 to 14 Jul 2013 by Joe Bloggs (Draft)'
                    timesheet.Title = htmlDoc.DocumentNode.SelectSingleNode(TitleSelector).InnerText.Trim();

                    timesheet.TimesheetId =
                        htmlDoc.DocumentNode.SelectSingleNode(TimesheetIdSelector).Attributes["value"].Value;

                    if (timesheet.TimesheetId.Equals("0"))
                    {
                        throw new ApplicationException("The Timesheet has an invalid ID");
                    }

                    timesheet.ProjectTimeItems = GetTimesheetItems(htmlDoc.DocumentNode);

                    timesheet.NonProjectActivityItems = GetTimesheetItems(htmlDoc.DocumentNode);

                    timesheet.RequiredHours      = GetRequiredHours(htmlDoc.DocumentNode);
                    timesheet.TotalRequiredHours =
                        htmlDoc.DocumentNode.SelectSingleNode(TotalRequiredHoursSelector).Attributes["value"].Value
                        .ToTimeSpan
                            ();

                    viewstate = htmlDoc.DocumentNode.SelectSingleNode(HtmlParser.ViewstateSelector).Attributes["value"].Value;
                }
                catch (NullReferenceException ex)
                {
                    // Log exception here
                    throw new ApplicationException("Unable to load timesheet - an error has occurred");
                }
            }

            return(timesheet);
        }