Esempio n. 1
0
        public ActionResult CreateSchedule(AppointmentRecurrence appointment)
        {
            var _calendarUtility = new Calculator();
            var r      = Request["RecType"];
            var errors = new List <string>();
            //set daily by default
            var rType = RecurrenceCalculator.RecurrenceType.Daily;

            if (appointment.RecType == "weekly")
            {
                rType = RecurrenceType.Weekly;
            }
            else if (appointment.RecType == "monthly")
            {
                rType = RecurrenceType.Monthly;
            }
            else if (appointment.RecType == "yearly")
            {
                rType = RecurrenceType.Yearly;
            }
            else if (appointment.RecType == "daily")
            {
                rType = RecurrenceCalculator.RecurrenceType.Daily;
            }
            else
            {
                errors.Add("You must select a schedule type from the tabs above!");
            }
            if (appointment.Interval < 1)
            {
                errors.Add("There must be at least one interval!");
            }

            Appointment a = null;

            if (rType == RecurrenceType.Yearly)
            {
                var dayOfMonth  = appointment.StartDate.Day;
                var monthOfYear = appointment.StartDate.Month;
                a = new Appointment()
                {
                    RecurrenceType = rType,
                    Interval       = appointment.Interval,
                    StartDate      = appointment.StartDate,
                    Occurrences    = appointment.Occurrences,
                    DayOfMonth     = dayOfMonth,
                    MonthOfYear    = monthOfYear
                };
            }
            else if (rType == RecurrenceType.Monthly)
            {
                var dayOfMonth = appointment.StartDate.Day;
                a = new Appointment()
                {
                    RecurrenceType = rType,
                    Interval       = appointment.Interval,
                    StartDate      = appointment.StartDate,
                    Occurrences    = appointment.Occurrences,
                    //Sunday = false,
                    //Monday = true,
                    //Tuesday = true,
                    //Wednesday = true,
                    //Thursday = true,
                    //Friday = true,
                    //Saturday = false,
                    DayOfMonth = dayOfMonth
                                 //Day = 5
                };
            }
            else if (rType == RecurrenceType.Weekly)
            {
                a = new Appointment()
                {
                    RecurrenceType = rType,
                    Interval       = appointment.Interval,
                    StartDate      = appointment.StartDate,
                    Occurrences    = appointment.Occurrences,
                    Sunday         = appointment.Sunday,
                    Monday         = appointment.Monday,
                    Tuesday        = appointment.Tuesday,
                    Wednesday      = appointment.Wednesday,
                    Thursday       = appointment.Thursday,
                    Friday         = appointment.Friday,
                    Saturday       = appointment.Saturday
                };
            }
            else //daily with acceptable days
            {
                a = new Appointment()
                {
                    RecurrenceType = rType,
                    Interval       = appointment.Interval,
                    StartDate      = appointment.StartDate,
                    Occurrences    = appointment.Occurrences,
                    Sunday         = appointment.Sunday,
                    Monday         = appointment.Monday,
                    Tuesday        = appointment.Tuesday,
                    Wednesday      = appointment.Wednesday,
                    Thursday       = appointment.Thursday,
                    Friday         = appointment.Friday,
                    Saturday       = appointment.Saturday
                };
            }



            try
            {
                var type = appointment.RecType;
                if (type == "monthly" || type == "yearly")
                {
                    type = type + " on " + appointment.StartDate.Day + " of every month";
                }
                var occurences = _calendarUtility.CalculateOccurrences(a);
                //create schedule record
                var s = new mc_schedule
                {
                    date_created             = DateTime.Now,
                    date_modified            = DateTime.Now,
                    deleted                  = false,
                    interval                 = appointment.Interval.ToString(),
                    paused                   = false,
                    report_id                = appointment.ReportId,
                    start_datetime           = appointment.StartDate,
                    user_created             = "",
                    user_modified            = "",
                    sunday                   = appointment.Sunday,
                    monday                   = appointment.Monday,
                    tuesday                  = appointment.Tuesday,
                    wednesday                = appointment.Wednesday,
                    thursday                 = appointment.Thursday,
                    friday                   = appointment.Friday,
                    saturday                 = appointment.Saturday,
                    occurences               = appointment.Occurrences,
                    schedule_expire_datetime = occurences.Max(x => x),
                    job_type_code            = appointment.RecType,
                    job_type                 = type
                };

                db.mc_schedule.Add(s);
                db.SaveChanges();

                //create jobs
                foreach (var o in occurences)
                {
                    var j = new mc_scheduled_jobs
                    {
                        cancel         = false,
                        complete       = false,
                        date_created   = DateTime.Now,
                        deleted        = false,
                        in_progress    = false,
                        job_type       = "scheduled",
                        report_id      = appointment.ReportId,
                        schedule_id    = s.id,
                        start_datetime = o
                    };
                    db.mc_scheduled_jobs.Add(j);
                }

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                errors.Add("You must choose at least one day of the week when you occurence can run!");
                foreach (var e in errors)
                {
                    ModelState.AddModelError(string.Empty, e);
                }

                return(View(appointment));
            }

            return(RedirectToAction("Details", "Reports", new { id = appointment.ReportId }));
        }
Esempio n. 2
0
        public ActionResult CreateJob(mc_scheduled_jobs j)
        {
            var u = new mc_scheduled_jobs();

            try
            {
                if (j.start_datetime == null)
                {
                    ModelState.AddModelError(String.Empty, "You must choose a time for this job to start after.");
                }
                if (!ModelState.IsValid)
                {
                    throw new Exception("excption");
                }

                //add job
                u.report_id    = j.report_id;
                u.date_created = DateTime.Now;

                u.start_datetime = j.start_datetime;
                u.job_type       = "ondemand";
                u.complete       = false;
                u.deleted        = false;
                u.cancel         = false;

                db.mc_scheduled_jobs.Add(u);
                db.SaveChanges();

                //add parameters for the job
                var ps = db.mc_report_params.Where(x => x.report_id == u.report_id).Select(x => x);
                foreach (var p in ps)
                {
                    var i = new mc_scheduled_job_params();
                    i.report_id = p.report_id;
                    i.job_id    = u.id;
                    if (p.mc_global_variables == null)
                    {
                        i.name              = p.name;
                        i.type              = p.type;
                        i.value             = p.current_value;
                        i.value_modifier    = p.value_modifier;
                        i.modifier_interval = p.modifier_interval;
                    }
                    else
                    {
                        var g = p.mc_global_variables;
                        i.name              = p.name;
                        i.type              = p.type;
                        i.value             = g.current_value;
                        i.value_modifier    = g.value_modifier;
                        i.modifier_interval = g.modifier_interval;
                    }

                    db.mc_scheduled_job_params.Add(i);
                }
                db.SaveChanges();
                return(RedirectToAction("Details", "Jobs", new { id = u.id }));
            }
            catch (Exception ex)
            {
                return(View(j));
            }
        }