Пример #1
0
        //List of Approved Rules from Fixed and Dynamic Rules
        public IEnumerable <Report> GetIndRules(string loggedInUser)
        {
            List <Report> reports = new List <Report>();

            using (var context = new UTSDatabaseEntities())
            {
                var fixRules = configure.LoadFixedrules.Where(x => x.LastUpdatedby == loggedInUser).Select(s => new Report
                {
                    Query    = s.Query,
                    Response = s.Response,
                    Type     = s.Type,
                    Status   = s.Status
                }).ToList();

                var dyRules = configure.LoadDynamicRules.Where(x => x.LastUpdatedby == loggedInUser).Select(s => new Report
                {
                    Query    = s.Query,
                    Response = s.Response,
                    Type     = s.Type,
                    Status   = s.Status
                }).ToList();

                return(fixRules.Concat(dyRules));
            }
        }
Пример #2
0
        public IEnumerable <Report> GetAllRules()
        {
            List <Report> reports = new List <Report>();

            using (var context = new UTSDatabaseEntities())
            {
                var fixRules = configure.LoadFixedrules.Select(s => new Report
                {
                    Query         = s.Query,
                    Response      = s.Response,
                    Type          = s.Type,
                    Status        = s.Status,
                    LastUpdatedBy = s.LastUpdatedby
                }).ToList();

                var dyRules = configure.LoadDynamicRules.Select(s => new Report
                {
                    Query         = s.Query,
                    Response      = s.Response,
                    Type          = s.Type,
                    Status        = s.Status,
                    LastUpdatedBy = s.LastUpdatedby
                }).ToList();

                return(fixRules.Concat(dyRules));
            }
        }
Пример #3
0
        /// <summary>
        /// Remove a dynamic rule from the Database.
        /// </summary>
        /// <param name="dynamic">The dynamic rule to remove</param>
        /// <returns>true if the rule is successfully deleted and false otherwise.</returns>
        public bool DeleteDynamicRule(DyanamicRule dynamic)
        {
            bool value = false;

            try
            {
                if (dynamic.RuleId != 0)
                {
                    using (var context = new UTSDatabaseEntities())
                    {
                        var saved = context.DyanamicRules.Find(dynamic.RuleId);
                        context.DyanamicRules.Remove(saved);
                        context.SaveChanges();
                        value = true;
                        return(value);
                    }
                }
                else
                {
                    return(value);
                }
            }
            catch (NullReferenceException)
            {
                return(false);
            }
        }
Пример #4
0
        /// <summary>
        /// Remove a fixed rule from the Database.
        /// </summary>
        /// <param name="fixedrule">The fixed rule rule to remove</param>
        /// <returns>true if the rule is successfully deleted and false otherwise.</returns>
        public bool DeleteFixedRule(Fixedrule fixrule)
        {
            bool value = false;

            try
            {
                if (fixrule.RuleId != 0)
                {
                    using (var context = new UTSDatabaseEntities())
                    {
                        var saved = context.Fixedrules.Find(fixrule.RuleId);
                        context.Fixedrules.Remove(saved);
                        context.SaveChanges();
                        value = true;
                        return(value);
                    }
                }
                else
                {
                    return(value);
                }
            }
            catch (NullReferenceException)
            {
                return(false);
            }
        }
Пример #5
0
 /// <summary>
 /// Searches for fixed rule in the fixedrule table using the rule id provided
 /// </summary>
 /// <param name="id">the rule id of the rule to be searched</param>
 /// <returns>returns the rule searched. </returns>
 public Fixedrule FixedSearchByRuleId(int id)
 {
     using (var context = new UTSDatabaseEntities())
     {
         return((from c in context.Fixedrules
                 where c.RuleId == id
                 select c).First());
     }
 }
Пример #6
0
 /// <summary>
 /// Searches for dynamic rule in the Dynamicrule table using the rule id provided
 /// </summary>
 /// <param name="id">the rule id of the rule to be searched</param>
 /// <returns>returns the rule searched. </returns>
 public DyanamicRule DynamicSearchByRuleId(int id)
 {
     using (var context = new UTSDatabaseEntities())
     {
         return((from c in context.DyanamicRules
                 where c.RuleId == id
                 select c).First());
     }
 }
Пример #7
0
 /// <summary>
 /// This methods searches the dynamic rule in db by its status (approved, rejected, created, updated)
 /// </summary>
 /// <param name="rulestatus">This parameter accepts the status of which the rules are to be queried.</param>
 /// <returns>return dynamic rules searched by status</returns>
 public List <DyanamicRule> SearchDyRuleByStatus(string rulestatus)
 {
     using (var context = new UTSDatabaseEntities())
     {
         var filterRules = (from row in context.DyanamicRules
                            where row.Status == rulestatus
                            select row).ToList();
         return(filterRules);
     }
 }
Пример #8
0
 /// <summary>
 /// This fnuction approves or rejects dynamic rules as requested
 /// </summary>
 /// <param name="dyrule"> Is a refernece of Dynamic rules table which helps to access the values stored in the variables of dynamic rule and
 ///                        stores the value entered by a user and other values.</param>
 /// <param name="selectedValue">The value selected as approved or rejected</param>
 /// <returns>true if the chnages are done nd saved , false otherwise</returns>
 public bool ApproveRejectRule(DyanamicRule dyrule, string selectedValue)
 {
     using (var usersDbContext = new UsersEntities())
     {
         bool isSaved = false;
         using (var context = new UTSDatabaseEntities())
         {
             DyanamicRule storedRule = context.DyanamicRules.Find(dyrule.RuleId);
             storedRule.Status = selectedValue == "Approve" ? (RulesStatus.Approved).ToString() : (RulesStatus.Rejected).ToString();
             context.SaveChanges();
             isSaved = true;
         }
         return(isSaved);
     }
 }
Пример #9
0
        /// <summary>
        /// This funtion represts the logic to edit an exsiting dynamic rule which is requested by the user.
        /// </summary>
        /// <param name="dynamic"> Is a refernece of Dynamicrule which helps to access the values stored in the variables of dynamic rule and
        ///                        stores the value entered by a user and other values.</param>
        /// <param name="loggedinUser">The session email of the logged in user (user.identity.name)</param>
        /// <returns>true if the rule is editted and flase when the rule can not be editted</returns>
        public bool EditDynamicRule(DyanamicRule dynamic, string loggedinUser)
        {
            using (var usersDbContext = new UsersEntities())
            {
                string userid = (from row in usersDbContext.AspNetUsers
                                 where row.UserName == loggedinUser
                                 select row.Id).First();

                bool isSaved = false;
                if (dynamic.Query == "" && dynamic.Response == "")
                {
                    return(isSaved);
                }
                else if (dynamic.Query == null && dynamic.Response == null)
                {
                    return(isSaved);
                }
                else
                {
                    using (var context = new UTSDatabaseEntities())
                    {
                        var query_search = (from row in context.DyanamicRules.ToList()
                                            where row.Query == dynamic.Query
                                            select row.Query).FirstOrDefault();
                        if (query_search.ToLower() == dynamic.Query.ToLower())
                        {
                            return(isSaved);
                        }
                        else
                        {
                            var stored   = context.DyanamicRules.Find(dynamic.RuleId);
                            var oldvalue = stored;
                            stored.Status        = RulesStatus.Updated.ToString();
                            stored.Query         = dynamic.Query;
                            stored.Response      = dynamic.Response;
                            stored.Keyword       = dynamic.Keyword;
                            stored.Match         = dynamic.Match;
                            stored.LastUpdatedby = loggedinUser;
                            context.Entry(oldvalue).CurrentValues.SetValues(stored);
                            context.SaveChanges();
                            isSaved = true;
                            return(isSaved);
                        }
                    }
                }
            }
        }
Пример #10
0
        /// <summary>
        /// This funtion represts the logic to edit an exsiting fixed rule which is requested by the user.
        /// </summary>
        /// <param name="fixrule"> Is a refernece of Fixedrule which helps to access the values stored in the variables of fixed rule and
        ///                        stores the value entered by a user and other values.</param>
        /// <param name="loggedinUser">The session email of the logged in user (user.identity.name)</param>
        /// <returns>true if the rule is editted and flase when the rule can not be editted</returns>
        public bool EditFixedRule(Fixedrule fixrule, string loggedinUser)
        {
            using (var usersDbContext = new UsersEntities())
            {
                bool   isSaved = false;
                string userid  = (from row in usersDbContext.AspNetUsers
                                  where row.UserName == loggedinUser
                                  select row.Id).First();

                if (fixrule.Query == "" && fixrule.Response == "")
                {
                    return(isSaved);
                }

                /* else if (fixrule.Query == null && fixrule.Response == null)
                 *   return isSaved;*/
                else
                {
                    using (var context = new UTSDatabaseEntities())
                    {
                        var query_search = (from row in context.Fixedrules
                                            where row.Query == fixrule.Query
                                            select row.Query).FirstOrDefault();


                        if (query_search.ToLower() == fixrule.Query.ToLower())
                        {
                            return(isSaved);
                        }
                        else
                        {
                            var stored   = context.Fixedrules.Find(fixrule.RuleId);
                            var oldvalue = stored;
                            stored.Status        = RulesStatus.Updated.ToString();
                            stored.Query         = fixrule.Query;
                            stored.Response      = fixrule.Response;
                            stored.LastUpdatedby = loggedinUser;
                            context.Entry(oldvalue).CurrentValues.SetValues(stored);
                            context.SaveChanges();
                            isSaved = true;
                            return(isSaved);
                        }
                    }
                }
            }
        }
Пример #11
0
        /// <summary>
        /// Domain logic of deleting datatable entry
        /// </summary>
        /// <param name="topicInformation">The refernce to access topic information for deletting it.</param>
        /// <returns></returns>
        public bool DeleData(TopicInformation topicInformation)
        {
            using (var context = new UTSDatabaseEntities())
            {
                var key = context.TopicInfo.Where(m => m.WeekNumber == topicInformation.WeekNumber).FirstOrDefault();

                if (key != null)
                {
                    db.TopicInfo.Remove(topicInformation);
                    db.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Пример #12
0
        /// <summary>
        /// Domain logic of Editting datatable entry
        /// </summary>
        /// <param name="topicInformation">The refernce to access topic information for editting it</param>
        /// <param name="loggedinUser">The session of user that is logged in.</param>
        /// <returns></returns>
        public bool EditData(TopicInformation topicInformation, string loggedinUser)
        {
            using (var context = new UTSDatabaseEntities())
            {
                var key = context.TopicInfo.Where(m => m.WeekNumber == topicInformation.WeekNumber).FirstOrDefault();

                if (key != null)
                {
                    topicInformation.LastUpdatedBy   = loggedinUser;
                    db.Entry(topicInformation).State = EntityState.Modified;
                    db.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Пример #13
0
        /// <summary>
        /// This funtion represts the logic to save a fixed rule which is requested by the user.
        /// </summary>
        /// <param name="fixrule"> Is a refernece of Fixedrule which helps to access th values stored in the variables of fixed rule and
        /// stores the value entered by a user and other values.</param>
        /// <param name="loggedinUser">The session email of the logged in user (user.identity.name)</param>
        /// <returns>Returns true when the rule is saved and false when the rule is not saved or already exsits</returns>
        public bool SaveFixedRule(Fixedrule fixrule, string loggedinUser)
        {
            using (var usersDbContext = new UsersEntities())
            {
                string userid = (from row in usersDbContext.AspNetUsers
                                 where row.UserName == loggedinUser
                                 select row.Id).First();

                bool isSaved = false;
                if (fixrule.Query == "" && fixrule.Response == "")
                {
                    return(isSaved);
                }
                else if (fixrule.Query == null && fixrule.Response == null)
                {
                    return(isSaved);
                }
                else
                {
                    using (var context = new UTSDatabaseEntities())
                    {
                        var query = (from row in context.Fixedrules
                                     where row.Query == fixrule.Query
                                     select row.Query).FirstOrDefault();
                        //checking if the rule already exsists
                        if (query == fixrule.Query)
                        {
                            return(isSaved);
                        }
                        fixrule.Type          = "Fixed";
                        fixrule.Createdby     = loggedinUser;
                        fixrule.LastUpdatedby = loggedinUser;
                        fixrule.UserId        = userid;
                        fixrule.Status        = RulesStatus.Created.ToString();
                        context.Fixedrules.Add(fixrule);
                        context.SaveChanges();
                        isSaved = true;
                        return(isSaved);
                    }
                }
            }
        }
Пример #14
0
        /// <summary>
        /// This funtion represts the logic to save a dynamic rule which is requested by the user.
        /// </summary>
        /// <param name="dynamic"> Is a refernece of Dynamicrule which helps to access th values stored in the variables of dynamic rule and
        /// stores the value entered by a user and other values.</param>
        /// <param name="loggedinUser">The session email of the logged in user (user.identity.name)</param>
        /// <returns>Returns true when the rule is saved and false when the rule is not saved or already exsits.</returns>
        public bool SaveDynamicRule(DyanamicRule dynamic, string loggedinUser)
        {
            using (var usersDbContext = new UsersEntities())
            {
                string userid = (from row in usersDbContext.AspNetUsers
                                 where row.UserName == loggedinUser
                                 select row.Id).First();

                bool isSaved = false;
                if (dynamic.Query == "" && dynamic.Response == "")
                {
                    return(isSaved);
                }
                else
                {
                    using (var context = new UTSDatabaseEntities())
                    {
                        var query = (from row in context.DyanamicRules.ToList()
                                     where row.Query == dynamic.Query
                                     select row.Query).FirstOrDefault();
                        //Checking if the query already exsist
                        if (query == dynamic.Query)
                        {
                            return(isSaved);
                        }
                        dynamic.Type          = "Dynamic";
                        dynamic.Createdby     = loggedinUser;
                        dynamic.LastUpdatedby = loggedinUser;
                        dynamic.UserId        = userid;
                        dynamic.Status        = RulesStatus.Created.ToString();
                        context.DyanamicRules.Add(dynamic);
                        context.SaveChanges();
                        isSaved = true;
                    }
                }
                return(isSaved);
            }
        }
Пример #15
0
        /// <summary>
        /// Retrives response to a query entered by the user
        /// </summary>
        /// <param name="rules">a reference to SystemRules class to Accesses the Ienum rules from the Rules table</param>
        /// <param name="query">The query enetred by user for finding a response</param>
        /// <returns>Returns a string of response to the query asked by user</returns>
        public string ResponseToQuery(SystemRules rules, string query)
        {
            string   xmlfilepath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\DialogMVC.Business\CustomMessage.xml"));
            XElement xelement    = XElement.Load(xmlfilepath);

            try
            {
                if (query == "")
                {
                    return((from message in xelement.Elements("Message")
                            where (string)message.Element("key") == "Empty"
                            select message.Element("value").Value).FirstOrDefault());
                }
                else
                {
                    string userInput    = Regex.Replace(query, @"[^\w\#\@\$\&\*]", "").ToLower();
                    var    matchfixRule = rules.AllRules.OfType <Fixedrule>().ToList().Find(a => (Regex.Replace(a.Query, @"[^\w\#\@\$\&\*]", "").ToLower() == userInput));
                    if (matchfixRule != null)
                    {
                        return(matchfixRule.Response);
                    }
                    else
                    {
                        using (var context = new UTSDatabaseEntities())
                        {
                            //string pattern = Regex.Match(userInput.ToLower(), @"(learn)|(week(?=[\w\d]))").Value;
                            string pattern     = Regex.Replace(Regex.Match(userInput.ToLower(), @"^(?=.*what)(?=.*week).*$|^(?=.*which)(?=.*week).*$|^(?=.*what)(?=.*topic).*$|^(?=.*when)(?=.*learn).*$").Value, @"(\d+)(?![a-z])", "");
                            var    matchDyRule = rules.AllRules.OfType <DyanamicRule>().ToList().
                                                 Find(a => (pattern.Contains(Regex.Replace
                                                                                 (Regex.Replace(a.Query, @"[^\w\#\@\$\&\*]", "").ToLower(), @"(keyword)", ""))));

                            if (matchDyRule != null)
                            {
                                if (matchDyRule.Keyword == "WeekNumber")
                                {
                                    int    keywordValue = 0;
                                    bool   res          = int.TryParse(Regex.Match(userInput, @"(\d+)(?![a-z])").Value, out keywordValue);
                                    string matchValue   = (from tr in context.TopicInfo
                                                           where tr.WeekNumber == keywordValue
                                                           select tr.Topic).First();

                                    string        testString = matchDyRule.Response;
                                    StringBuilder sb         = new StringBuilder(testString);
                                    sb.Replace("[Match]", matchValue);
                                    return(sb.ToString());
                                }
                                else if (matchDyRule.Keyword == "Topic")
                                {
                                    string str          = (Regex.Replace(Regex.Replace(matchDyRule.Query, @"\[(Keyword)\]", ""), @"[^\w\#\@\$\&\*]", "")).ToLower();
                                    string keywordValue = userInput.Substring(str.Length);
                                    int    matchValue   = (from tr in context.TopicInfo.AsEnumerable()
                                                           where Regex.Replace(tr.Topic, @"[^\w\#\@\$\&\*]", "").ToLower() == keywordValue
                                                           select tr.WeekNumber).First();

                                    string        testString = matchDyRule.Response;
                                    StringBuilder sb         = new StringBuilder(testString);
                                    sb.Replace("[Match]", matchValue.ToString());
                                    return(sb.ToString());
                                }
                            }
                        }
                    }
                }
                {
                    return((from message in xelement.Elements("Message")
                            where (string)message.Element("key") == "Invalid"
                            select message.Element("value").Value).FirstOrDefault());

                    ;
                }
            }
            //Returns the invalid message from xelement
            catch (InvalidOperationException)
            {
                return((from message in xelement.Elements("Message")
                        where (string)message.Element("key") == "Invalid"
                        select message.Element("value").Value).FirstOrDefault());
            }
            catch (ArgumentNullException)
            {
                return((from message in xelement.Elements("Message")
                        where (string)message.Element("key") == "Empty"
                        select message.Element("value").Value).FirstOrDefault());
            }

            catch (FormatException)
            {
                return("Please enter a week number to search");
            }
        }