private void RunItemDeleted(Item item, ID formerParentId)
        {
            var rulesEngine = new RulesEngine(item.Database);

            try
            {
                using (new SecurityDisabler())
                {
                    if (item.IsInboundRuleItem() || item.IsSimpleRedirectItem())
                    {
                        Log.Info(this, item.Database, "Clearing inbound rules cache after delete event");

                        rulesEngine.DeleteRule(item, null);
                        //rulesEngine.ClearInboundRuleCache();
                    }
                    else if (item.IsInboundRuleItemChild(formerParentId))
                    {
                        Item itemParent = item.Parent;
                        if (itemParent == null && formerParentId != (ID)null)
                        {
                            itemParent = item.Database.GetItem(formerParentId);
                        }

                        Log.Info(this, item.Database, "Updating parent inbound rule ({0}) after child delete event",
                                 itemParent.Paths.Path);

                        rulesEngine.RefreshRule(itemParent, GetRedirectFolderItem(itemParent));
                    }
                    else if (item.IsOutboundRuleItem() || item.IsOutboundRuleItemChild())
                    {
                        Log.Info(this, item.Database, "Clearing outbound rules cache after delete event",
                                 item.Parent.Paths.FullPath);

                        rulesEngine.ClearOutboundRuleCache();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(this, ex, item.Database,
                          "Exception occured which deleting item after publish Item ID: {0} Item Path: {1}", item.ID,
                          item.Paths.FullPath);
            }
        }
        private void RunItemSaved(Item item, ItemChanges itemChanges)
        {
            var db          = item.Database;
            var rulesEngine = new RulesEngine(db);

            try
            {
                using (new SecurityDisabler())
                {
                    var redirectFolderItem = item.Axes.GetAncestors()
                                             .FirstOrDefault(a => a.TemplateID.Equals(new ID(RedirectFolderItem.TemplateId)));

                    if (redirectFolderItem == null)
                    {
                        return;
                    }

                    if (item.IsRedirectFolderItem())
                    {
                        Log.Info(this, db, "Refreshing Redirect Folder [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.GetCachedInboundRules();
                    }
                    else if (item.IsOutboundRuleItem())
                    {
                        Log.Info(this, db, "Refreshing Outbound Rule [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.RefreshRule(item, redirectFolderItem);
                    }
                    else if (item.IsSimpleRedirectItem())
                    {
                        Log.Info(this, db, "Refreshing Simple Redirect [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.RefreshRule(item, redirectFolderItem);
                    }
                    else if (item.IsInboundRuleItem())
                    {
                        Log.Info(this, db, "Refreshing Inbound Rule [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.RefreshRule(item, redirectFolderItem);
                    }
                    else if (item.IsRedirectType() && item.IsInboundRuleItemChild() && db.Name.Equals("web", StringComparison.CurrentCultureIgnoreCase))
                    {
                        var inboundRuleItem = item.Parent;
                        var inboundRule     = new InboundRuleItem(inboundRuleItem);

                        inboundRule.BeginEdit();
                        inboundRule.Action.InnerField.SetValue(item.ID.ToString(), false);
                        inboundRule.EndEdit();
                    }
                    else if (item.IsInboundRuleItemChild())
                    {
                        Log.Info(this, db, "Refreshing Inbound Rule [{0}] after save event", item.Parent.Paths.FullPath);

                        rulesEngine.RefreshRule(item.Parent, redirectFolderItem);
                    }
                    else if (item.IsOutboundRuleItemChild())
                    {
                        Log.Info(this, db, "Refreshing Outbound Rule [{0}] after save event", item.Parent.Paths.FullPath);

                        rulesEngine.RefreshRule(item.Parent, redirectFolderItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(this, ex, db, "Exception occured when saving item after save - Item ID: {0} Item Path: {1}", item.ID, item.Paths.FullPath);
            }
        }
        private void RunItemSaved(Item item, ItemChanges itemChanges)
        {
            var db = item.Database;
            var rulesEngine = new RulesEngine(db);

            try
            {
                using (new SecurityDisabler())
                {
                    var redirectFolderItem = item.Axes.GetAncestors()
                        .FirstOrDefault(a => a.TemplateID.Equals(new ID(RedirectFolderItem.TemplateId)));

                    if (redirectFolderItem == null) return;

                    if (item.IsRedirectFolderItem())
                    {
                        Log.Info(this, db, "Refreshing Redirect Folder [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.GetCachedInboundRules();
                    }
                    else if (item.IsOutboundRuleItem())
                    {
                        Log.Info(this, db, "Refreshing Outbound Rule [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.RefreshRule(item, redirectFolderItem);
                    }
                    else if (item.IsSimpleRedirectItem())
                    {
                        Log.Info(this, db, "Refreshing Simple Redirect [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.RefreshRule(item, redirectFolderItem);
                    }
                    else if (item.IsInboundRuleItem())
                    {
                        Log.Info(this, db, "Refreshing Inbound Rule [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.RefreshRule(item, redirectFolderItem);
                    }
                    else if (item.IsRedirectType() && item.IsInboundRuleItemChild() && db.Name.Equals("master", StringComparison.CurrentCultureIgnoreCase))
                    {
                        var inboundRuleItem = item.Parent;
                        var inboundRule = new InboundRuleItem(inboundRuleItem);

                        inboundRule.BeginEdit();
                        inboundRule.Action.InnerField.SetValue(item.ID.ToString(), false);
                        inboundRule.EndEdit();
                    }
                    else if (item.IsInboundRuleItemChild())
                    {
                        Log.Info(this, db, "Refreshing Inbound Rule [{0}] after save event", item.Parent.Paths.FullPath);

                        rulesEngine.RefreshRule(item.Parent, redirectFolderItem);
                    }
                    else if (item.IsOutboundRuleItemChild())
                    {
                        Log.Info(this, db, "Refreshing Outbound Rule [{0}] after save event", item.Parent.Paths.FullPath);

                        rulesEngine.RefreshRule(item.Parent, redirectFolderItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(this, ex, db, "Exception occured when saving item after save - Item ID: {0} Item Path: {1}", item.ID, item.Paths.FullPath);
            }

        }
        private void RunItemSaved(Item item, ItemChanges itemChanges)
        {
            var db          = item.Database;
            var rulesEngine = new RulesEngine(db);

            try
            {
                using (new SecurityDisabler())
                {
                    var redirectFolderItem = GetRedirectFolderItem(item);

                    if (redirectFolderItem == null)
                    {
                        return;
                    }

                    if (item.IsRedirectFolderItem())
                    {
                        Log.Info(this, db, "Clearing cached inbound rules (reason: Redirect Folder [{0}] saves)",
                                 item.Paths.FullPath);

                        rulesEngine.ClearInboundRuleCache();
                    }
                    else if (item.IsOutboundRuleItem())
                    {
                        Log.Info(this, db, "Refreshing Outbound Rule [{0}] after save event", item.Paths.FullPath);

                        rulesEngine.RefreshRule(item, redirectFolderItem);
                    }
                    else if (item.IsSimpleRedirectItem())
                    {
                        if (rulesEngine.CanRefreshInboundRule(item, redirectFolderItem))
                        {
                            Log.Info(this, db, "Refreshing Simple Redirect [{0}] after save event", item.Paths.FullPath);

                            rulesEngine.RefreshRule(item, redirectFolderItem);
                        }
                        else
                        {
                            Log.Info(this, db,
                                     "Simple Redirect [{0}] cannot be individually refreshed after save event. Clearing inbound rule cache.",
                                     item.Paths.FullPath);

                            rulesEngine.ClearInboundRuleCache();
                            ;
                        }
                    }
                    else if (item.IsInboundRuleItem())
                    {
                        if (rulesEngine.CanRefreshInboundRule(item, redirectFolderItem))
                        {
                            Log.Info(this, db, "Refreshing Inbound Rule [{0}] after save event", item.Paths.FullPath);

                            rulesEngine.RefreshRule(item, redirectFolderItem);
                        }
                        else
                        {
                            Log.Info(this, db,
                                     "Inbound Rule [{0}] cannot be individually refreshed after save event. Clearing inbound rule cache.",
                                     item.Paths.FullPath);

                            rulesEngine.ClearInboundRuleCache();
                        }
                    }
                    else if (item.IsRedirectType() && item.IsInboundRuleItemChild() &&
                             db.Name.Equals("master", StringComparison.CurrentCultureIgnoreCase))
                    {
                        var inboundRuleItem = item.Parent;
                        var inboundRule     = new InboundRuleItem(inboundRuleItem);

                        inboundRule.BeginEdit();
                        inboundRule.Action.InnerField.SetValue(item.ID.ToString(), false);
                        inboundRule.EndEdit();
                    }
                    else if (item.IsInboundRuleItemChild())
                    {
                        if (rulesEngine.CanRefreshInboundRule(item.Parent, redirectFolderItem))
                        {
                            Log.Info(this, db, "Refreshing Inbound Rule [{0}] after save event", item.Parent.Paths.FullPath);

                            rulesEngine.RefreshRule(item.Parent, redirectFolderItem);
                        }
                        else
                        {
                            Log.Info(this, db,
                                     "Inbound Rule [{0}] cannot be individually refreshed after save event. Clearing inbound rule cache.",
                                     item.Parent.Paths.FullPath);

                            rulesEngine.ClearInboundRuleCache();
                        }
                    }
                    else if (item.IsOutboundRuleItemChild())
                    {
                        Log.Info(this, db, "Refreshing Outbound Rule [{0}] after save event", item.Parent.Paths.FullPath);

                        rulesEngine.RefreshRule(item.Parent, redirectFolderItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(this, ex, db, "Exception occured when saving item after save - Item ID: {0} Item Path: {1}", item.ID,
                          item.Paths.FullPath);
            }
        }
        private void RunItemDeleted(Item item)
        {
            var rulesEngine = new RulesEngine(item.Database);

            try
            {

                using (new SecurityDisabler())
                {

                    var redirectFolderItem = item.Axes.GetAncestors()
                        .FirstOrDefault(a => a.TemplateID.Equals(new ID(RedirectFolderItem.TemplateId)));

                    if (redirectFolderItem != null)
                    {
                        if (item.IsInboundRuleItem() || item.IsSimpleRedirectItem() || item.IsOutboundRuleItem())
                        {
                            Log.Info(this, item.Database, "Removing Rule [{0}] after delete event",
                                item.Paths.FullPath);

                            rulesEngine.DeleteRule(item, redirectFolderItem);
                        }
                        else if (item.IsInboundRuleItemChild() || item.IsOutboundRuleItemChild())
                        {
                            Log.Info(this, item.Database, "Removing Rule [{0}] after delete event",
                                item.Parent.Paths.FullPath);

                            rulesEngine.RefreshRule(item.Parent, redirectFolderItem);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(this, ex, item.Database, "Exception occured which deleting item after publish Item ID: {0} Item Path: {1}", item.ID, item.Paths.FullPath);
            }
        }