Click here to Skip to main content
15,896,278 members
Articles / Programming Languages / XML

SharePoint Reservations

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
29 Mar 2009CPOL8 min read 423K   4K   47  
A SharePoint calendar extension that prohibits overlapping appointments
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace RecurringClasses
{
    /// <summary>
    /// Concrete class to handle annually recurring events
    /// for example, 3rd of October
    /// J. Finn October 2008
    /// james.finn@gmail.com
    /// </summary>
    class YearlyRecurPeriod : absRecurPeriod
    {
        int yearFrequency = 0; // how many years between appointments
        int monthOfTheYear = 0; // month of the year
        int dayOfTheMonth = 0; // day of the month

        /// <summary>
        /// constructor sets up variables necessary to respond to GetNext
        /// </summary>
        /// <param name="StartDate">Start Date</param>
        /// <param name="EndDate">End Date</param>
        /// <param name="repeatNode">XML Node containing recurring definition</param>
        public YearlyRecurPeriod(ref DateTime StartDate, ref DateTime EndDate, XmlNode RepeatNode)
            : base(StartDate, EndDate)
        {

            // extracts values for yearFrequency, month of the year, and day of the month
            if (!Int32.TryParse(RepeatNode.Attributes["yearFrequency"].Value,
                out yearFrequency))
            {
                throw new Exception("Unexpected yearFrequency: " + RepeatNode.Attributes["yearFrequency"].Value);
            }
            
            if (!Int32.TryParse(RepeatNode.Attributes["month"].Value,
                out monthOfTheYear))
            {
                throw new Exception("Unexpected Day: " + RepeatNode.Attributes["day"].Value);
            }

            if (!Int32.TryParse(RepeatNode.Attributes["day"].Value, 
                out dayOfTheMonth))
            {
                throw new Exception("Unexpected Day: " + RepeatNode.Attributes["day"].Value);
            }
            

            // adjust start date to first selected day

            initialStartDate = new DateTime(this.initialStartDate.Year,
                                        monthOfTheYear, dayOfTheMonth,
                                        this.initialStartDate.Hour, this.initialStartDate.Minute,
                                        this.initialStartDate.Second);


            initialEndDate = new DateTime(this.initialEndDate.Year,
                                    monthOfTheYear, dayOfTheMonth,
                                    this.initialEndDate.Hour, this.initialEndDate.Minute,
                                    this.initialEndDate.Second);

            currentStartDate = initialStartDate;
            currentEndDate = initialEndDate;
        }

        /// <summary>
        /// Increments dates to get the next date in the recurrence pattern
        /// </summary>
        protected override void incrementDates()
        {
            currentStartDate = currentStartDate.AddYears(yearFrequency);
            currentEndDate = currentEndDate.AddYears(yearFrequency);

            if (currentStartDate.Day != dayOfTheMonth ||
                currentStartDate.Month != monthOfTheYear)
            {
                try
                {
                    currentStartDate = new DateTime(this.currentStartDate.Year,
                                            monthOfTheYear, dayOfTheMonth,
                                            this.currentStartDate.Hour, this.currentStartDate.Minute,
                                            this.currentStartDate.Second);


                    currentEndDate = new DateTime(this.currentEndDate.Year,
                                            monthOfTheYear, dayOfTheMonth,
                                            this.currentEndDate.Hour, this.currentEndDate.Minute,
                                            this.currentEndDate.Second);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    // invalid date leaves the original calculation
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions