Example #1
0
        public static List <Box> Validate(List <Box> boxes, bool examCollideCheck = false)
        {
            if (boxes == null)
            {
                throw new Exception();
            }

            if (boxes.Count == 1 && boxes[0].Rows.Count == 0)
            {
                return(null);
            }

            //foreach (var b in boxes){if (b.Rows.Count == 0) return null;}


            List <Box> bxs = new List <Box>();

            bxs.Add(boxes[0]);


            bool con0 = true;

            for (int i = 1; i < boxes.Count; i++)
            {
                var b1 = bxs[0];
                var b2 = boxes[i];

                Box newBox = new Box();

                for (int j = 0; j < b2.Rows.Count; j++)
                {
                    for (int k = 0; k < b1.Rows.Count; k++)
                    {
                        bool con = true;

                        Row newRow = new Row();


                        for (int m = 0; m < b1.Rows[k].Columns.Count; m++)
                        {
                            bool collide = OfferedCoursesServiceProvider.DoTheyCollide(b1.Rows[k].Columns[m].GoalVersionOfferedCourseRow.TimeAndSitesAndExam.TimeAndSites,
                                                                                       b2.Rows[j].Columns[0].GoalVersionOfferedCourseRow.TimeAndSitesAndExam.TimeAndSites
                                                                                       );
                            if (collide)
                            {
                                con = false;
                                break;
                            }

                            if (examCollideCheck &&
                                OfferedCoursesServiceProvider.DoTheyExamCollide(b1.Rows[k].Columns[m].GoalVersionOfferedCourseRow.TimeAndSitesAndExam.Exam,
                                                                                b2.Rows[j].Columns[0].GoalVersionOfferedCourseRow.TimeAndSitesAndExam.Exam
                                                                                )
                                )
                            {
                                //collide = true;
                                con = false;
                                break;
                            }


                            newRow.Columns.Add(b1.Rows[k].Columns[m]);
                        }

                        newRow.Columns.Add(b2.Rows[j].Columns[0]);

                        if (con)
                        {
                            newBox.Rows.Add(newRow);

                            //if just we want to say true/false we can un comment below lines of code

                            //if (i == boxes.Count - 1)
                            //{
                            //    bxs[0] = newBox;
                            //    return bxs;
                            //    break;
                            //}
                        }
                    }
                }

                if (newBox.Rows.Count == 0)
                {
                    con0 = false;
                    break;
                }
                else
                {
                    bxs[0] = newBox;
                }
            }

            if (con0)
            {
                return(bxs);
            }
            else
            {
                return(null);
            }
        }
        private async void btnTimeFilter_Click(object sender, RoutedEventArgs e)
        {
            if (!allowUserInteraction) return;
            allowUserInteraction = false;

            btnTimeFilter.IsEnabled = false;
            List<TimeFilteringModel> tfms = new List<TimeFilteringModel>();
            bool succ = false;
            try
            {
                bool[] days = new bool[7];

                ReductionStep2ColorStatus color = rdbRedColor.IsChecked == true ? ReductionStep2ColorStatus.RED : ReductionStep2ColorStatus.WHITE;

                #region r1

                if (Day0.IsChecked == true)
                    days[0] = true;
                if (Day1.IsChecked == true)
                    days[1] = true;
                if (Day2.IsChecked == true)
                    days[2] = true;
                if (Day3.IsChecked == true)
                    days[3] = true;
                if (Day4.IsChecked == true)
                    days[4] = true;
                if (Day5.IsChecked == true)
                    days[5] = true;
                if (Day6.IsChecked == true)
                    days[6] = true;

                string[] tFrom = txtStartTime.Text.Split(':');
                string[] tTo = txtFinishTime.Text.Split(':');

                int hStart;
                int mStart;

                int hFinish;
                int mFinish;

                if (!int.TryParse(tFrom[0], out hStart))
                {
                    throw new Exception();
                }
                if (!int.TryParse(tFrom[1], out mStart))
                {
                    throw new Exception();
                }


                if (!int.TryParse(tTo[0], out hFinish))
                {
                    throw new Exception();
                }
                if (!int.TryParse(tTo[1], out mFinish))
                {
                    throw new Exception();
                }

                #endregion


                List<TimeAndSite> ts = new List<TimeAndSite>();

                await Task.Run(() =>
                {

                    for (int i = 0; i < days.Length; i++)
                    {
                        if (days[i] == true)
                        {
                            TimeAndSite time = new TimeAndSite
                            {
                                StartTime = new LocalTime() { Hour = hStart, Minute = mStart, },
                                FinishTime = new LocalTime() { Hour = hFinish, Minute = mFinish, },
                                Day = (Harif.Infrastructures.GeneralEnums.Day)i
                            };
                            ts.Add(time);
                        }
                    }




                    foreach (var ofRow in offeredCourseRows)
                    {
                        if (ofRow.Color != color && OfferedCoursesServiceProvider.DoTheyCollide(ofRow.GoalVersionOfferedCourseRow.TimeAndSitesAndExam.TimeAndSites, ts))
                        {
                            TimeFilteringModel model = new TimeFilteringModel
                            {
                                Row = ofRow
                            };
                            model.TempOriginColor = model.Row.Color;
                            ofRow.Color = color;
                            tfms.Add(model);
                        }
                    }

                    if (greenCourses.Count > 0)
                    {
                        List<Box> boxes = new List<Box>();

                        foreach (var item in greenCourses)
                        {
                            boxes.Add(ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(item));
                        }

                        List<Box> res = null;
                        var task = Task.Run(() => ReductionStep2ServiceProvider.Validate(boxes, exampCollideChecking));
                        if (task.Wait(TimeSpan.FromMilliseconds(timeoutMs)))
                            res = task.Result;
                        else
                        {
                            res = null;
                        }

                        if (res == null)
                        {
                            throw new Exception();
                        }
                        else
                        {
                            //is ok
                        }
                    }
                    else
                    {
                        //is ok
                    }

                    succ = true;

                });
            }
            catch
            {

                foreach (var tfm in tfms)
                {
                    tfm.Row.Color = tfm.TempOriginColor;
                }
                lblMsgActionPerform.Foreground = Brushes.Red;
                lblMsgActionPerform.Content = "خطایی رخ داده است";
            }
            finally
            {
                btnTimeFilter.IsEnabled = true;
                allowUserInteraction = true;

                if (succ)
                {
                    lblMsgActionPerform.Foreground = Brushes.DarkGreen;
                    lblMsgActionPerform.Content = "فیلتر با موفقیت اعمال شد";

                    btnCourseNavigation_Click(selectedButton, null);
                }
            }



        }