예제 #1
0
        public static RecurrenceRule Parse(string serializedString)
        {
            var rr = new RecurrenceRule();
            var parts = serializedString.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries);
            foreach (var pair in parts)
            {
                var name = pair.Split('=')[0].Trim().ToLower();
                var val = pair.Split('=')[1].Trim().ToLower();
                switch (name)
                { 
                    case "freq":
                        rr.Freq = ParseFrequency(val);

                        break;
                    case "until":
                        DateTime d;
                        if (DateTime.TryParseExact(val.ToUpper(), _dateTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out d))
                            rr.Until = d;

                        break;

                    case "count":
                        rr.Count = Convert.ToInt32(val);
                        break;

                    case "interval":
                        rr.Interval= Convert.ToInt32(val);
                        break;

                    case "bysecond":
                        rr.BySecond = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "byminute":
                        rr.ByMinute = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "byhour":
                        rr.ByHour = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "byday":
                        rr.ByDay = val.Split(',').Select(v => RecurrenceRule.WeekDay.Parse(v)).ToArray();
                        break;

                    case "bymonthday":
                        rr.ByMonthDay = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "byyearday":
                        rr.ByYearDay = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "byweekno":
                        rr.ByWeekNo = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "bymonth":
                        rr.ByMonth = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "bysetpos":
                        rr.BySetPos = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                        break;

                    case "wkst":
                        rr.WKST =  RecurrenceRule.WeekDay.Parse(val);
                        break;

                    case "exdates":

                        foreach (var date in val.Split(','))
                        {
                            DateTime dt;
                            if (DateTime.TryParseExact(date.ToUpper(), _dateTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt))                            
                                rr.ExDates.Add(new ExDate() { Date = dt, isDateTime = (date.ToLower().IndexOf('t') >= 0) });

                        }
                        break;
                }
            }

            return rr;
        }
        public void HorlyRules()
        {
            //test 1
            var r1 = new RecurrenceRule()
            {
                Freq = Frequency.Hourly,
                Until = new DateTime(1997,9,2,17,0,0),
                Interval = 3
            };

            var dates = r1.GetDates(new DateTime(1997, 9, 2, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 2, 9, 0, 0), new DateTime(1997, 9, 2, 12, 0, 0),            
            new DateTime(1997, 9, 2, 15, 0, 0)}, dates);
        }
        public void MinutelyRules()
        {
            //test 1
            var r1 = new RecurrenceRule()
            {
                Freq = Frequency.Minutely,
                ByHour = new int[]{9,10,11,12,13,14,15,16},
                Count = 5,
                Interval = 20
            };

            var dates = r1.GetDates(new DateTime(1997, 9, 2, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 2, 9, 0, 0), new DateTime(1997, 9, 2, 9, 20, 0),            
            new DateTime(1997, 9, 2, 9, 40, 0),new DateTime(1997, 9, 2, 10, 0, 0),new DateTime(1997, 9, 2, 10, 20, 0)}, dates);
        }
        public void YearlyRules()
        {           

            //test 1
            var r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Count = 10,
                ByMonth = new int[] { 6, 7 }
            };

            var dates = r1.GetDates(new DateTime(1997, 6, 10, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){new DateTime(1997, 6, 10, 9, 0, 0), new DateTime(1997, 7, 10, 9, 0, 0),
            new DateTime(1998, 6, 10, 9, 0, 0),new DateTime(1998, 7, 10, 9, 0, 0),
            new DateTime(1999, 6, 10, 9, 0, 0),new DateTime(1999, 7, 10, 9, 0, 0),
            new DateTime(2000, 6, 10, 9, 0, 0),new DateTime(2000, 7, 10, 9, 0, 0),
            new DateTime(2001, 6, 10, 9, 0, 0),new DateTime(2001, 7, 10, 9, 0, 0)}, dates);


            //test 2
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Interval = 2,
                Count = 10,
                ByMonth = new int[] { 1, 2,3 }
            };
            dates = r1.GetDates(new DateTime(1997, 3, 10, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){new DateTime(1997, 3, 10, 9, 0, 0),
                new DateTime(1999, 1, 10, 9, 0, 0),new DateTime(1999, 2, 10, 9, 0, 0), new DateTime(1999, 3, 10, 9, 0, 0),
                new DateTime(2001, 1, 10, 9, 0, 0),new DateTime(2001, 2, 10, 9, 0, 0), new DateTime(2001, 3, 10, 9, 0, 0),
                new DateTime(2003, 1, 10, 9, 0, 0),new DateTime(2003, 2, 10, 9, 0, 0), new DateTime(2003, 3, 10, 9, 0, 0),
            }, dates);


            //test 3
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Interval = 3,
                Count = 10,
                ByYearDay = new int[] { 1, 100, 200 }
            };
            dates = r1.GetDates(new DateTime(1997, 1, 1, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
                new DateTime(1997, 1, 1, 9, 0, 0),new DateTime(1997, 4, 10, 9, 0, 0),new DateTime(1997, 7, 19, 9, 0, 0), 
                new DateTime(2000, 1, 1, 9, 0, 0),new DateTime(2000, 4, 9, 9, 0, 0),new DateTime(2000, 7, 18, 9, 0, 0), 
                new DateTime(2003, 1, 1, 9, 0, 0),new DateTime(2003, 4, 10, 9, 0, 0),new DateTime(2003, 7, 19, 9, 0, 0), 
                new DateTime(2006, 1, 1, 9, 0, 0)
            }, dates);


            //test 4
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Count = 3,
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("20MO") }
            };
            dates = r1.GetDates(new DateTime(1997, 5, 19, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
                new DateTime(1997, 5, 19, 9, 0, 0),new DateTime(1998, 5, 18, 9, 0, 0),new DateTime(1999, 5, 17, 9, 0, 0)
            }, dates);


            //test 5
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Count = 3,
                ByWeekNo = new int[]{20},
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("MO") }
            };
            dates = r1.GetDates(new DateTime(1997, 5, 12, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
                new DateTime(1997, 5, 12, 9, 0, 0),new DateTime(1998, 5, 11, 9, 0, 0),new DateTime(1999, 5, 17, 9, 0, 0)
            }, dates);

            //test 6
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Count = 11,
                ByMonth = new int[] { 3 },
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("TH") }
            };
            dates = r1.GetDates(new DateTime(1997, 3, 13, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
               new DateTime(1997, 3, 13, 9, 0, 0), new DateTime(1997, 3, 20, 9, 0, 0),new DateTime(1997, 3, 27, 9, 0, 0),
               new DateTime(1998, 3, 5, 9, 0, 0), new DateTime(1998, 3, 12, 9, 0, 0),new DateTime(1998, 3, 19, 9, 0, 0),new DateTime(1998, 3, 26, 9, 0, 0),
               new DateTime(1999, 3, 4, 9, 0, 0),new DateTime(1999, 3, 11, 9, 0, 0),new DateTime(1999, 3, 18, 9, 0, 0),new DateTime(1999, 3, 25, 9, 0, 0)
            }, dates);


            //test 7
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Count = 13,
                ByMonth = new int[] { 6,7,8 },
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("TH") }
            };
            dates = r1.GetDates(new DateTime(1997, 6, 5, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
               new DateTime(1997, 6, 5, 9, 0, 0), new DateTime(1997, 6, 12, 9, 0, 0),new DateTime(1997, 6, 19, 9, 0, 0),new DateTime(1997, 6, 26, 9, 0, 0),
               new DateTime(1997, 7, 3, 9, 0, 0), new DateTime(1997, 7, 10, 9, 0, 0),new DateTime(1997, 7, 17, 9, 0, 0),new DateTime(1997, 7, 24, 9, 0, 0),new DateTime(1997, 7, 31, 9, 0, 0),
               new DateTime(1997, 8, 7, 9, 0, 0), new DateTime(1997, 8, 14, 9, 0, 0),new DateTime(1997, 8, 21, 9, 0, 0),new DateTime(1997, 8, 28, 9, 0, 0)
               
            }, dates);

            //test 8
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Interval = 4,
                Count = 3,
                ByMonth = new int[] { 11 },
                ByMonthDay = new int[] { 2,3,4,5,6,7,8 },
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("TU") }
            };
            dates = r1.GetDates(new DateTime(1996, 11, 5, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
               new DateTime(1996, 11, 5, 9, 0, 0), 
               new DateTime(2000, 11, 7, 9, 0, 0),
               new DateTime(2004, 11, 2, 9, 0, 0)
            }, dates);


            //friday 13
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Yearly,
                Until = new DateTime(2013,1,1),
                ByMonthDay = new int[] { 13 },
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("fr") }
            };
            dates = r1.GetDates(new DateTime(2012, 1, 1, 0, 0, 0), _fromDate, new DateTime(2014, 1, 1, 0, 0, 0));
           


        }
        public void DailyRules()
        {
            //test 1
            var r1 = new RecurrenceRule()
            {
                Freq = Frequency.Daily,
                Count = 5,
                Interval = 10
            };

            var dates = r1.GetDates(new DateTime(1997, 9, 2, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 2, 9, 0, 0), new DateTime(1997, 9, 12, 9, 0, 0),
            new DateTime(1997, 9, 22, 9, 0, 0),new DateTime(1997, 10, 2, 9, 0, 0),
            new DateTime(1997, 10, 12, 9, 0, 0)}, dates);

        }
        public void MonthlyRules()
        {
            //test 1
            var r1 = new RecurrenceRule()
            {
                Freq = Frequency.Monthly,
                Count = 10,
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("1FR") }
            };

            var dates = r1.GetDates(new DateTime(1997, 9, 5, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 5, 9, 0, 0), new DateTime(1997, 10, 3, 9, 0, 0),
            new DateTime(1997, 11, 7, 9, 0, 0),new DateTime(1997, 12, 5, 9, 0, 0),
            new DateTime(1998, 1, 2, 9, 0, 0),new DateTime(1998, 2, 6, 9, 0, 0),
            new DateTime(1998, 3, 6, 9, 0, 0),new DateTime(1998, 4, 3, 9, 0, 0),
            new DateTime(1998, 5, 1, 9, 0, 0), new DateTime(1998, 6, 5, 9, 0, 0)}, dates);

            //test 2
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Monthly,
                Until = new DateTime(1997, 12, 24),
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("1FR") }
            };

            dates = r1.GetDates(new DateTime(1997, 9, 5, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 5, 9, 0, 0), new DateTime(1997, 10, 3, 9, 0, 0),
            new DateTime(1997, 11, 7, 9, 0, 0),new DateTime(1997, 12, 5, 9, 0, 0)}, dates);


            //test 3
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Monthly,
                Count = 10,
                Interval = 2,
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("1SU"), RecurrenceRule.WeekDay.Parse("-1SU") }
            };

            dates = r1.GetDates(new DateTime(1997, 9, 7, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 7, 9, 0, 0), new DateTime(1997, 9, 28, 9, 0, 0),
            new DateTime(1997, 11, 2, 9, 0, 0),new DateTime(1997, 11, 30, 9, 0, 0),
            new DateTime(1998, 1, 4, 9, 0, 0),new DateTime(1998, 1, 25, 9, 0, 0),
            new DateTime(1998, 3, 1, 9, 0, 0),new DateTime(1998, 3, 29, 9, 0, 0),
            new DateTime(1998, 5, 3, 9, 0, 0),new DateTime(1998, 5, 31, 9, 0, 0)
            }, dates);


            //test 4
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Monthly,
                Count = 6,                
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("-2MO")}
            };

            dates = r1.GetDates(new DateTime(1997, 9, 22, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 22, 9, 0, 0), new DateTime(1997, 10, 20, 9, 0, 0),
            new DateTime(1997, 11, 17, 9, 0, 0),new DateTime(1997, 12, 22, 9, 0, 0),
            new DateTime(1998, 1, 19, 9, 0, 0),new DateTime(1998, 2, 16, 9, 0, 0)
            
            }, dates);

            //test 5
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Monthly,
                Interval = 18,
                Count = 10,
                ByMonthDay = new int[] {10,11,12,13,14,15 }
            };

            dates = r1.GetDates(new DateTime(1997, 9, 10, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 10, 9, 0, 0), new DateTime(1997, 9, 11, 9, 0, 0),
            new DateTime(1997, 9, 12, 9, 0, 0),new DateTime(1997, 9, 13, 9, 0, 0),
            new DateTime(1997, 9, 14, 9, 0, 0),new DateTime(1997, 9, 15, 9, 0, 0),
            new DateTime(1999, 3, 10, 9, 0, 0),new DateTime(1999, 3, 11, 9, 0, 0),
            new DateTime(1999, 3, 12, 9, 0, 0),new DateTime(1999, 3, 13, 9, 0, 0)
            
            }, dates);

        }
        public void WeeklyRules()
        {
            //test 1
            var r1 = new RecurrenceRule()
            {
                Freq = Frequency.Weekly,
                Count = 10                
            };

            var dates = r1.GetDates(new DateTime(1997, 9, 2, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 2, 9, 0, 0), new DateTime(1997, 9, 9, 9, 0, 0),
            new DateTime(1997, 9, 16, 9, 0, 0),new DateTime(1997, 9, 23, 9, 0, 0),
            new DateTime(1997, 9, 30, 9, 0, 0),new DateTime(1997, 10, 7, 9, 0, 0),
            new DateTime(1997, 10, 14, 9, 0, 0),new DateTime(1997, 10, 21, 9, 0, 0),
            new DateTime(1997, 10, 28, 9, 0, 0), new DateTime(1997, 11, 4, 9, 0, 0)}, dates);


            //test 2
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Weekly,
                Interval = 2,
                WKST = RecurrenceRule.WeekDay.Parse("su"),
                Count = 10
            };

            dates = r1.GetDates(new DateTime(1997, 9, 2, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 2, 9, 0, 0), new DateTime(1997, 9, 16, 9, 0, 0),
            new DateTime(1997, 9, 30, 9, 0, 0),new DateTime(1997, 10, 14, 9, 0, 0),
            new DateTime(1997, 10, 28, 9, 0, 0),new DateTime(1997, 11, 11, 9, 0, 0),
            new DateTime(1997, 11, 25, 9, 0, 0),new DateTime(1997, 12, 9, 9, 0, 0),
            new DateTime(1997, 12, 23, 9, 0, 0), new DateTime(1998, 1, 6, 9, 0, 0)}, dates);

            //test 3
            r1 = new RecurrenceRule()
            {
                Freq = Frequency.Weekly,
                Interval = 2,
                WKST = RecurrenceRule.WeekDay.Parse("su"),
                Count = 8,
                ByDay = new RecurrenceRule.WeekDay[] { RecurrenceRule.WeekDay.Parse("tu"), RecurrenceRule.WeekDay.Parse("th") }
            };

            dates = r1.GetDates(new DateTime(1997, 9, 2, 9, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(1997, 9, 2, 9, 0, 0), new DateTime(1997, 9, 4, 9, 0, 0),
            new DateTime(1997, 9, 16, 9, 0, 0),new DateTime(1997, 9, 18, 9, 0, 0),
            new DateTime(1997, 9, 30, 9, 0, 0),new DateTime(1997, 10, 2, 9, 0, 0),
            new DateTime(1997, 10, 14, 9, 0, 0),new DateTime(1997, 10, 16, 9, 0, 0)}, dates);


            r1 = RecurrenceRule.Parse("freq=weekly;count=3;interval=2;byday=mo");
            dates = r1.GetDates(new DateTime(2012, 1, 2, 0, 0, 0), _fromDate, _toDate);

            CollectionAssert.AreEqual(new List<DateTime>(){
            new DateTime(2012, 1, 2, 0, 0, 0), new DateTime(2012, 1, 16, 0, 0, 0),
            new DateTime(2012, 1, 30, 0, 0, 0)}, dates);



        }
 public EventNotificationData()
 {
     RRule = new RecurrenceRule();
 }
예제 #9
0
        public static RecurrenceRule Parse(string serializedString)
        {
            var rr    = new RecurrenceRule();
            var parts = serializedString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var pair in parts)
            {
                var name = pair.Split('=')[0].Trim().ToLower();
                var val  = pair.Split('=')[1].Trim().ToLower();
                switch (name)
                {
                case "freq":
                    rr.Freq = ParseFrequency(val);

                    break;

                case "until":
                    DateTime d;
                    if (DateTime.TryParseExact(val.ToUpper(), _dateTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out d))
                    {
                        rr.Until = d;
                    }

                    break;

                case "count":
                    rr.Count = Convert.ToInt32(val);
                    break;

                case "interval":
                    rr.Interval = Convert.ToInt32(val);
                    break;

                case "bysecond":
                    rr.BySecond = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "byminute":
                    rr.ByMinute = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "byhour":
                    rr.ByHour = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "byday":
                    rr.ByDay = val.Split(',').Select(v => RecurrenceRule.WeekDay.Parse(v)).ToArray();
                    break;

                case "bymonthday":
                    rr.ByMonthDay = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "byyearday":
                    rr.ByYearDay = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "byweekno":
                    rr.ByWeekNo = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "bymonth":
                    rr.ByMonth = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "bysetpos":
                    rr.BySetPos = val.Split(',').Select(v => Convert.ToInt32(v)).ToArray();
                    break;

                case "wkst":
                    rr.WKST = RecurrenceRule.WeekDay.Parse(val);
                    break;

                case "exdates":

                    foreach (var date in val.Split(','))
                    {
                        DateTime dt;
                        if (DateTime.TryParseExact(date.ToUpper(), _dateTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt))
                        {
                            rr.ExDates.Add(new ExDate()
                            {
                                Date = dt, isDateTime = (date.ToLower().IndexOf('t') >= 0)
                            });
                        }
                    }
                    break;
                }
            }

            return(rr);
        }
예제 #10
0
        private void UpdateEventNotifications(int eventId, int calendarId, DateTime eventUtcStartDate, EventAlertType baseEventAlertType, RecurrenceRule rrule,
            IEnumerable<ASC.Web.Core.Calendars.SharingOptions.PublicItem> eventPublicItems,
            IEnumerable<ASC.Web.Core.Calendars.SharingOptions.PublicItem> calendarPublicItems)
        {
            var cc = new ColumnCollection();
            var userIdCol = cc.RegistryColumn("user_id");
            var alertTypeCol = cc.RegistryColumn("alert_type");
            var isUnsubscribeCol = cc.RegistryColumn("is_unsubscribe");

            var eventUsersData = DbManager.ExecuteList(new SqlQuery("calendar_event_user").Select(cc.SelectQuery).Where(Exp.Eq("event_id", eventId)));

            var calendarData = DbManager.ExecuteList(new SqlQuery("calendar_calendars").Select("alert_type", "owner_id", "time_zone").Where(Exp.Eq("id", calendarId)));
            var calendarAlertType = calendarData.Select(r => (EventAlertType)Convert.ToInt32(r[0])).First();
            Guid calendarOwner = calendarData.Select(r => new Guid(Convert.ToString(r[1]))).First();
            TimeZoneInfo calendarTimeZone = calendarData.Select(r => TimeZoneConverter.GetTimeZone(Convert.ToString(r[2]))).First();

            List<UserAlertType> eventUsers = new List<UserAlertType>();

            #region shared event's data

            if (eventPublicItems == null)
            {
                eventPublicItems = new List<SharingOptions.PublicItem>(DbManager.ExecuteList(new SqlQuery("calendar_event_item").Select("item_id", "is_group").Where(Exp.Eq("event_id", eventId)))
                                                                       .Select(r => new ASC.Web.Core.Calendars.SharingOptions.PublicItem() { Id = new Guid(Convert.ToString(r[0])), IsGroup = Convert.ToBoolean(r[1]) }));
            }

            foreach (var item in eventPublicItems)
            {
                if (item.IsGroup)
                    eventUsers.AddRange(CoreContext.UserManager.GetUsersByGroup(item.Id).Select(u => new UserAlertType(u.ID, baseEventAlertType, calendarTimeZone)));
                else
                    eventUsers.Add(new UserAlertType(item.Id, baseEventAlertType, calendarTimeZone));
            }

            //remove calendar owner
            eventUsers.RemoveAll(u => u.UserId.Equals(calendarOwner));

            //remove unsubscribed and exec personal alert_type
            if (eventUsers.Count > 0)
            {
                foreach (var r in eventUsersData)
                {
                    if (isUnsubscribeCol.Parse<bool>(r))
                        eventUsers.RemoveAll(u => u.UserId.Equals(userIdCol.Parse<Guid>(r)));
                    else
                        eventUsers.ForEach(u =>
                        {
                            if (u.UserId.Equals(userIdCol.Parse<Guid>(r)))
                                u.AlertType = (EventAlertType)alertTypeCol.Parse<int>(r);
                        });

                }
            }

            //remove and exec sharing calendar options
            if (eventUsers.Count > 0)
            {
                var extCalendarAlertTypes = DbManager.ExecuteList(new SqlQuery("calendar_calendar_user cu")
                                                        .Select("cu.user_id", "cu.alert_type", "cu.is_accepted", "cu.time_zone")
                                                        .Where(Exp.Eq("cu.ext_calendar_id", SharedEventsCalendar.CalendarId) & Exp.In("cu.user_id", eventUsers.Select(u => u.UserId).ToArray())));

                foreach (var r in extCalendarAlertTypes)
                {
                    if (!Convert.ToBoolean(r[2]))
                    {
                        //remove unsubscribed from shared events calendar
                        eventUsers.RemoveAll(u => u.UserId.Equals(new Guid(Convert.ToString(r[0]))));
                        continue;
                    }
                    eventUsers.ForEach(u =>
                    {
                        if (u.UserId.Equals(new Guid(Convert.ToString(r[0]))))
                            u.TimeZone = ((r[3] == null || r[3] == DBNull.Value) ? calendarTimeZone : TimeZoneConverter.GetTimeZone(Convert.ToString(r[3])));

                        if (u.AlertType == EventAlertType.Default && u.UserId.Equals(new Guid(Convert.ToString(r[0]))))
                            u.AlertType = (EventAlertType)Convert.ToInt32(r[1]);
                    });
                };

                eventUsers.ForEach(u =>
                {
                    if (u.AlertType == EventAlertType.Default)
                        u.AlertType = EventAlertType.Hour;
                });

            }
            #endregion

            #region calendar's data

            if (calendarPublicItems == null)
            {
                calendarPublicItems = new List<SharingOptions.PublicItem>(DbManager.ExecuteList(new SqlQuery("calendar_calendar_item").Select("item_id", "is_group").Where(Exp.Eq("calendar_id", calendarId)))
                                                                       .Select(r => new ASC.Web.Core.Calendars.SharingOptions.PublicItem() { Id = new Guid(Convert.ToString(r[0])), IsGroup = Convert.ToBoolean(r[1]) }));
            }

            //calendar users
            List<UserAlertType> calendarUsers = new List<UserAlertType>();
            foreach (var item in eventPublicItems)
            {
                if (item.IsGroup)
                    calendarUsers.AddRange(CoreContext.UserManager.GetUsersByGroup(item.Id).Select(u => new UserAlertType(u.ID, baseEventAlertType, calendarTimeZone)));
                else
                    calendarUsers.Add(new UserAlertType(item.Id, baseEventAlertType, calendarTimeZone));
            }

            calendarUsers.Add(new UserAlertType(calendarOwner, baseEventAlertType, calendarTimeZone));

            //remove event's users
            calendarUsers.RemoveAll(u => eventUsers.Exists(eu => eu.UserId.Equals(u.UserId)));

            //calendar options            
            if (calendarUsers.Count > 0)
            {
                //set personal alert_type
                foreach (var r in eventUsersData)
                {
                    eventUsers.ForEach(u =>
                    {
                        if (u.UserId.Equals(new Guid(Convert.ToString(r[0]))))
                            u.AlertType = (EventAlertType)(Convert.ToInt32(r[1]));
                    });

                }



                var calendarAlertTypes = DbManager.ExecuteList(new SqlQuery("calendar_calendar_user")
                                                      .Select("user_id", "alert_type", "is_accepted", "time_zone")
                                                      .Where(Exp.Eq("calendar_id", calendarId) & Exp.In("user_id", calendarUsers.Select(u => u.UserId).ToArray())));


                foreach (var r in calendarAlertTypes)
                {
                    if (!Convert.ToBoolean(r[2]))
                    {
                        //remove unsubscribed
                        calendarUsers.RemoveAll(u => u.UserId.Equals(new Guid(Convert.ToString(r[0]))));
                        continue;
                    }
                    calendarUsers.ForEach(u =>
                    {
                        if (u.UserId.Equals(new Guid(Convert.ToString(r[0]))))
                            u.TimeZone = ((r[3] == null || r[3] == DBNull.Value) ? calendarTimeZone : TimeZoneConverter.GetTimeZone(Convert.ToString(r[3])));

                        if (u.AlertType == EventAlertType.Default && u.UserId.Equals(new Guid(Convert.ToString(r[0]))))
                            u.AlertType = (EventAlertType)Convert.ToInt32(r[1]);
                    });
                };

                calendarUsers.ForEach(u =>
                {
                    if (u.AlertType == EventAlertType.Default)
                        u.AlertType = calendarAlertType;
                });
            }

            #endregion


            //clear notifications
            DbManager.ExecuteNonQuery(new SqlDelete("calendar_notifications").Where("event_id", eventId));

            eventUsers.AddRange(calendarUsers);

            foreach (var u in eventUsers)
            {
                //todo: recount
                var alertDate = GetNextAlertDate(eventUtcStartDate, rrule, u.AlertType, u.TimeZone);
                if (!alertDate.Equals(DateTime.MinValue))
                {
                    DbManager.ExecuteNonQuery(new SqlInsert("calendar_notifications", true).InColumnValue("user_id", u.UserId)
                                                                                     .InColumnValue("event_id", eventId)
                                                                                     .InColumnValue("rrule", rrule.ToString())
                                                                                     .InColumnValue("alert_type", (int)u.AlertType)
                                                                                     .InColumnValue("tenant", CoreContext.TenantManager.GetCurrentTenant().TenantId)
                                                                                     .InColumnValue("notify_date", alertDate)
                                                                                     .InColumnValue("time_zone", u.TimeZone.Id));
                }
            }
        }
예제 #11
0
        private DateTime GetNextAlertDate(DateTime utcStartDate, RecurrenceRule rrule, EventAlertType eventAlertType, TimeZoneInfo timeZone)
        {
            if (eventAlertType == EventAlertType.Never)
                return DateTime.MinValue;

            var localFromDate = DateTime.UtcNow.Add(timeZone.BaseUtcOffset);
            var localStartDate = utcStartDate.Kind == DateTimeKind.Local ? utcStartDate : utcStartDate.Add(timeZone.BaseUtcOffset);
            var dates = rrule.GetDates(localStartDate, localFromDate, 3);
            for (var i = 0; i < dates.Count; i++)
            {
                dates[i] = dates[i].AddMinutes((-1) * (int)timeZone.BaseUtcOffset.TotalMinutes);
            }

            foreach (var d in dates)
            {
                var dd = d.AddMinutes(GetBeforeMinutes(eventAlertType));
                if (dd > DateTime.UtcNow)
                    return dd;
            }

            return DateTime.MinValue;
        }
예제 #12
0
        public Event UpdateEvent(int eventId, int calendarId, Guid ownerId, string name, string description,
                DateTime utcStartDate, DateTime utcEndDate, RecurrenceRule rrule, EventAlertType alertType, bool isAllDayLong, List<ASC.Web.Core.Calendars.SharingOptions.PublicItem> publicItems)
        {

            using (var tr = DbManager.BeginTransaction())
            {
                var query = new SqlUpdate("calendar_events")
                                .Set("name", name)
                                .Set("description", description)
                                .Set("calendar_id", calendarId)
                                .Set("owner_id", ownerId)
                                .Set("start_date", utcStartDate.ToString("yyyy-MM-dd HH:mm:ss"))
                                .Set("end_date", utcEndDate.ToString("yyyy-MM-dd HH:mm:ss"))
                                .Set("all_day_long", isAllDayLong)
                                .Set("rrule", rrule.ToString())
                                .Where(Exp.Eq("id", eventId));

                if (ownerId.Equals(SecurityContext.CurrentAccount.ID))
                    query = query.Set("alert_type", (int)alertType);
                else
                    DbManager.ExecuteNonQuery(new SqlInsert("calendar_event_user", true).InColumnValue("event_id", eventId)
                                                                                    .InColumnValue("user_id", SecurityContext.CurrentAccount.ID)
                                                                                    .InColumnValue("alert_type", alertType));


                DbManager.ExecuteNonQuery(query);

                var userIds = DbManager.ExecuteList(new SqlQuery("calendar_event_user").Select("user_id").Where("event_id", eventId)).Select(r => new Guid(Convert.ToString(r[0])));
                foreach (var usrId in userIds)
                {
                    if (!publicItems.Exists(i => (i.IsGroup && CoreContext.UserManager.IsUserInGroup(usrId, i.Id))
                                              || (!i.IsGroup && i.Id.Equals(usrId))))
                    {
                        DbManager.ExecuteNonQuery(new SqlDelete("calendar_event_user").Where(Exp.Eq("user_id", usrId) & Exp.Eq("event_id", eventId)));
                    }
                }

                DbManager.ExecuteNonQuery(new SqlDelete("calendar_event_item").Where("event_id", eventId));
                foreach (var item in publicItems)
                {
                    DbManager.ExecuteNonQuery(new SqlInsert("calendar_event_item")
                                                   .InColumnValue("event_id", eventId)
                                                   .InColumnValue("item_id", item.Id)
                                                   .InColumnValue("is_group", item.IsGroup));


                }

                //update notifications
                var baseAlertType = DbManager.ExecuteList(new SqlQuery("calendar_events").Select("alert_type").Where("id", eventId))
                                    .Select(r => (EventAlertType)Convert.ToInt32(r[0])).First();
                UpdateEventNotifications(eventId, calendarId, utcStartDate, baseAlertType, rrule, publicItems, null);


                tr.Commit();
            }

            return GetEventById(eventId);
        }
예제 #13
0
        public Event CreateEvent(int calendarId, Guid ownerId, string name, string description,
                DateTime utcStartDate, DateTime utcEndDate, RecurrenceRule rrule, EventAlertType alertType, bool isAllDayLong, List<ASC.Web.Core.Calendars.SharingOptions.PublicItem> publicItems)
        {
            int eventId = 0;
            using (var tr = DbManager.BeginTransaction())
            {

                eventId = DbManager.ExecuteScalar<int>(new SqlInsert("calendar_events")
                                                    .InColumnValue("id", 0)
                                                    .InColumnValue("tenant", CoreContext.TenantManager.GetCurrentTenant().TenantId)
                                                    .InColumnValue("name", name)
                                                    .InColumnValue("description", description)
                                                    .InColumnValue("calendar_id", calendarId)
                                                    .InColumnValue("owner_id", ownerId)
                                                    .InColumnValue("start_date", utcStartDate.ToString("yyyy-MM-dd HH:mm:ss"))
                                                    .InColumnValue("end_date", utcEndDate.ToString("yyyy-MM-dd HH:mm:ss"))
                                                    .InColumnValue("all_day_long", isAllDayLong)
                                                    .InColumnValue("rrule", rrule.ToString())
                                                    .InColumnValue("alert_type", (int)alertType)
                                                    .Identity<int>(0, 0, true));

                foreach (var item in publicItems)
                {
                    DbManager.ExecuteNonQuery(new SqlInsert("calendar_event_item")
                                                .InColumnValue("event_id", eventId)
                                                .InColumnValue("item_id", item.Id)
                                                .InColumnValue("is_group", item.IsGroup));

                }

                //update notifications
                UpdateEventNotifications(eventId, calendarId, utcStartDate, alertType, rrule, publicItems, null);

                tr.Commit();
            }

            return GetEventById(eventId);
        }
예제 #14
0
        private List<EventWrapper> CreateEvent(int calendarId, string name, string description, DateTime utcStartDate, DateTime utcEndDate, RecurrenceRule rrule, EventAlertType alertType, bool isAllDayLong, List<SharingParam> sharingOptions)
        {
            var sharingOptionsList = sharingOptions ?? new List<SharingParam>();

            //check permissions
            CheckPermissions(_dataProvider.GetCalendarById(calendarId),
                              CalendarAccessRights.FullAccessAction);

            name = (HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(name)) ?? "").Trim();
            if (String.IsNullOrEmpty(name))
                throw new Exception(Resources.CalendarApiResource.ErrorEmptyName);

            description = (description ?? "").Trim();

            var evt = _dataProvider.CreateEvent(calendarId, SecurityContext.CurrentAccount.ID,
                                                name, description, utcStartDate, utcEndDate, rrule, alertType, isAllDayLong,
                                                sharingOptionsList.Select(o => o as SharingOptions.PublicItem).ToList());

            if (evt != null)
            {
                foreach (var opt in sharingOptionsList)
                    if (String.Equals(opt.actionId, AccessOption.FullAccessOption.Id, StringComparison.InvariantCultureIgnoreCase))
                        CoreContext.AuthorizationManager.AddAce(new AzRecord(opt.Id, CalendarAccessRights.FullAccessAction.ID, ASC.Common.Security.Authorizing.AceType.Allow, evt));

                //notify
                CalendarNotifyClient.NotifyAboutSharingEvent(evt);

                return new EventWrapper(evt, SecurityContext.CurrentAccount.ID,
                                        _dataProvider.GetTimeZoneForCalendar(SecurityContext.CurrentAccount.ID, calendarId))
                                        .GetList(utcStartDate, utcStartDate.AddMonths(_monthCount));
            }
            return null;
        }