Esempio n. 1
0
        public static object Execute(Booking b, Resource r, BookingSystem bs, BookingRuleType brt, bool isRemoveEvent)
        {
            BookingRules br = new BookingRules();
            object       o  = null;

            foreach (BookingRule rule in br)
            {
                if (brt != rule.Type)
                {
                    continue;
                }

                bool matched = rule.ExecuteRule(b, r, bs, brt, isRemoveEvent, out o);
                if (matched && rule.StopProcessing)
                {
                    break;
                }
            }
            return(o);
        }
Esempio n. 2
0
        public bool ExecuteRule(Booking b, Resource r, BookingSystem bs, BookingRuleType brt, bool IsRemoveEvent, out object objo)
        {
            bool good = true;

            objo = null;
            foreach (BookingCondition con in Conditions)
            {
                switch (con.MasterOperation)
                {
                case BookingConditionOperation.And:
                    good = good && con.IsConditionMet(b, r, bs);
                    break;

                case BookingConditionOperation.Or:
                    good = good || con.IsConditionMet(b, r, bs);
                    break;
                }
            }
            if (good)
            {
                #region ProcessRule
                foreach (string a in this.Actions)
                {
                    try
                    {
                        if (a.ToLower().StartsWith("bookcharging(") || a.ToLower().StartsWith("bookunavailable("))
                        {
                            string   reason     = "";
                            string   c          = "";
                            int      repeat     = 1;
                            string[] conditions = null;
                            if (a.ToLower().StartsWith("bookcharging("))
                            {
                                conditions = a.Remove(0, "bookcharging(".Length).TrimStart(new char[] { '(' }).TrimEnd(new char[] { ')' }).Split(new char[] { ',' });
                                c          = a.Remove(0, "bookcharging(".Length).TrimEnd(new char[] { ')' });
                                reason     = "CHARGING";
                            }
                            else if (a.ToLower().StartsWith("bookunavailable("))
                            {
                                conditions = a.Remove(0, "bookunavailable(".Length).TrimStart(new char[] { '(' }).TrimEnd(new char[] { ')' }).Split(new char[] { ',' });
                                c          = a.Remove(0, "bookunavailable(".Length).TrimEnd(new char[] { ')' });
                                reason     = "UNAVAILABLE";
                            }

                            if (conditions.Length == 2)
                            {
                                c      = conditions[0];
                                repeat = Convert.ToInt32(BookingCondition.processCondition(conditions[1], b, r, bs));
                            }

                            object o = BookingCondition.processCondition(c, b, r, bs);
                            if (o is Booking)
                            {
                                Booking     ob     = o as Booking;
                                XmlDocument doc    = HAP.BookingSystem.BookingSystem.BookingsDoc;
                                hapConfig   config = hapConfig.Current;
                                if (!IsRemoveEvent)
                                {
                                    string lastlesson = (ob.Lesson.Contains(',') ? ob.Lesson.Split(new char[] { ',' }).Last() : ob.Lesson);
                                    int    index2     = config.BookingSystem.Lessons.FindIndex(l => l.Name == lastlesson);

                                    for (int x = 0; x < Math.Abs(repeat); x++)
                                    {
                                        int period = (repeat > 0) ? index2 + x : index2 - x;
                                        if (index2 < config.BookingSystem.Lessons.Count - x)
                                        {
                                            XmlElement node = doc.CreateElement("Booking");
                                            node.SetAttribute("date", b.Date.ToShortDateString());
                                            node.SetAttribute("lesson", config.BookingSystem.Lessons[period].Name);
                                            node.SetAttribute("room", b.Room);
                                            if (r.Type == ResourceType.Laptops)
                                            {
                                                node.SetAttribute("ltroom", "--");
                                                node.SetAttribute("ltheadphones", b.LTHeadPhones.ToString());
                                            }
                                            node.SetAttribute("username", "systemadmin");
                                            node.SetAttribute("uid", b.uid);
                                            node.SetAttribute("name", reason);
                                            doc.SelectSingleNode("/Bookings").AppendChild(node);
                                        }
                                    }
                                }
                                else
                                {
                                    string lastlesson = (ob.Lesson.Contains(',') ? ob.Lesson.Split(new char[] { ',' }).Last() : ob.Lesson);
                                    int    index2     = config.BookingSystem.Lessons.FindIndex(l => l.Name == lastlesson);
                                    for (int x = 0; x < Math.Abs(repeat); x++)
                                    {
                                        int period = (repeat > 0) ? index2 + x : index2 - x;
                                        if (index2 < config.BookingSystem.Lessons.Count - x)
                                        {
                                            if (doc.SelectSingleNode("/Bookings/Booking[@date='" + b.Date.ToShortDateString() + "' and @lesson='" + config.BookingSystem.Lessons[period].Name + "' and @room='" + b.Room + "' and @uid='" + b.uid + "']") != null)
                                            {
                                                doc.SelectSingleNode("/Bookings").RemoveChild(doc.SelectSingleNode("/Bookings/Booking[@date='" + b.Date.ToShortDateString() + "' and @lesson='" + config.BookingSystem.Lessons[period].Name + "' and @room='" + b.Room + "' and @uid='" + b.uid + "']"));
                                            }
                                        }
                                    }
                                }
                                HAP.BookingSystem.BookingSystem.BookingsDoc = doc;
                            }
                        }
                        else if (a.ToLower().StartsWith("book("))
                        {
                            string c = a.Remove(0, "book(".Length).TrimEnd(new char[] { ')' });
                            object o = BookingCondition.processCondition(c, b, r, bs);
                            if (o is Booking)
                            {
                                Booking     ob  = o as Booking;
                                XmlDocument doc = HAP.BookingSystem.BookingSystem.BookingsDoc;
                                if (!IsRemoveEvent)
                                {
                                    XmlElement node = doc.CreateElement("Booking");
                                    node.SetAttribute("date", b.Date.ToShortDateString());
                                    node.SetAttribute("lesson", ob.Lesson);
                                    node.SetAttribute("room", b.Room);
                                    if (r.Type == ResourceType.Laptops)
                                    {
                                        node.SetAttribute("ltroom", "--");
                                        node.SetAttribute("ltheadphones", b.LTHeadPhones.ToString());
                                    }
                                    else if (r.Type == ResourceType.Equipment)
                                    {
                                        node.SetAttribute("equiproom", b.EquipRoom);
                                    }
                                    node.SetAttribute("room", b.Room);
                                    node.SetAttribute("uid", b.uid);
                                    node.SetAttribute("username", b.Username);
                                    node.SetAttribute("name", b.Name);
                                    node.SetAttribute("count", b.Count.ToString());
                                    doc.SelectSingleNode("/Bookings").AppendChild(node);
                                }
                                else
                                {
                                    if (doc.SelectSingleNode("/Bookings/Booking[@date='" + b.Date.ToShortDateString() + "' and @lesson='" + ob.Lesson + "' and @room='" + b.Room + "' and @uid='" + b.uid + "']") != null)
                                    {
                                        doc.SelectSingleNode("/Bookings").RemoveChild(doc.SelectSingleNode("/Bookings/Booking[@date='" + b.Date.ToShortDateString() + "' and @lesson='" + ob.Lesson + "' and @room='" + b.Room + "' and @uid='" + b.uid + "']"));
                                    }
                                }
                                HAP.BookingSystem.BookingSystem.BookingsDoc = doc;
                            }
                        }
                        else if (a.ToLower().StartsWith("busy("))
                        {
                            string reason = a.Remove(0, "busy(".Length).TrimEnd(new char[] { ')' });
                            b.Name   = reason;
                            b.Static = true;
                            objo     = b;
                        }
                    }
                    catch (Exception ex)
                    {
                        HAP.Web.Logging.EventViewer.Log("BookingSystem.BookingRule", "Failed Action: " + Actions[0] + "\n\n" + ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
                    }
                }
                #endregion
                return(true);
            }
            return(false);
        }