private Component FindComponent(Keyword kw)
        {
            XmlElement usingItems = kw.GetListUsingItems(new UsingItemsFilter(Engine.GetSession()) { ItemTypes = new List<ItemType>() { ItemType.Component }, IncludedVersions = VersionCondition.OnlyLatestVersions });

            Component component = null;

            List<Component> matchingComponents = new List<Component>();
            List<Component> otherComponents = new List<Component>();

            if (kw.Title.Contains("Web Content Management"))
                Logger.Debug(string.Format("looking for component for keyword {0}", kw.Title));
            foreach (XmlElement item in usingItems.SelectNodes("/*/*"))
            {
                Component c = (Component)Engine.GetObject(item.GetAttribute("ID"));
                if (kw.Title.Contains("Web Content Management"))
                    Logger.Debug(string.Format("round 1: component {0}, schema title {2}", c.Title, c.Id, c.Schema.Title));

                //The component which is based on the header schema related to the current taxonomy takes precedence
                if (c.Schema.Title.ToLower().Contains(kw.OrganizationalItem.Title.ToLower())) // e.g. if the keyword is in the category 'Product', a component with schema 'Header For Product' matches!
                    matchingComponents.Add(c);
                else
                    //The first one that is returned by the system takes precedence
                    otherComponents.Add(c);
            }

            if (matchingComponents.Count == 0 && otherComponents.Count == 0)
            {
                if (kw.Title.Contains("Web Content Management"))
                    Logger.Debug("no components found, returning null");
                return null;
            }

            if (TryFindComponentWithinMatchingComponents(matchingComponents, kw, out component))
                return component;

            matchingComponents.Clear();

            foreach (Component c in otherComponents)
            {
                if (kw.Title.Contains("Web Content Management"))
                    Logger.Debug(string.Format("round 2: component {0}, schema {1}", c.Title, c.Schema.Title)); 

                //The component which is based on any other header schema takes precedence
                if (c.Schema.Title.StartsWith("Header")) // TODO: make configurable
                    matchingComponents.Add(c);

            }

            if (TryFindComponentWithinMatchingComponents(matchingComponents, kw, out component))
                return component;

            if (kw.Title.Contains("Web Content Management"))
                Logger.Debug(string.Format("no rules matched, returning first component in list ({0})", otherComponents[0].Title));

            if (TryFindComponentWithinMatchingComponents(otherComponents, kw, out component))
                return component;




            return null;
        }
Esempio n. 2
0
        private Component FindComponent(Keyword kw)
        {
            XmlElement usingItems = kw.GetListUsingItems(new UsingItemsFilter(Engine.GetSession())
            {
                ItemTypes = new List <ItemType>()
                {
                    ItemType.Component
                }, IncludedVersions = VersionCondition.OnlyLatestVersions
            });

            Component component = null;

            List <Component> matchingComponents = new List <Component>();
            List <Component> otherComponents    = new List <Component>();

            if (kw.Title.Contains("Web Content Management"))
            {
                Logger.Debug(string.Format("looking for component for keyword {0}", kw.Title));
            }
            foreach (XmlElement item in usingItems.SelectNodes("/*/*"))
            {
                Component c = (Component)Engine.GetObject(item.GetAttribute("ID"));
                if (kw.Title.Contains("Web Content Management"))
                {
                    Logger.Debug(string.Format("round 1: component {0}, schema title {2}", c.Title, c.Id, c.Schema.Title));
                }

                //The component which is based on the header schema related to the current taxonomy takes precedence
                if (c.Schema.Title.ToLower().Contains(kw.OrganizationalItem.Title.ToLower())) // e.g. if the keyword is in the category 'Product', a component with schema 'Header For Product' matches!
                {
                    matchingComponents.Add(c);
                }
                else
                {
                    //The first one that is returned by the system takes precedence
                    otherComponents.Add(c);
                }
            }

            if (matchingComponents.Count == 0 && otherComponents.Count == 0)
            {
                if (kw.Title.Contains("Web Content Management"))
                {
                    Logger.Debug("no components found, returning null");
                }
                return(null);
            }

            if (TryFindComponentWithinMatchingComponents(matchingComponents, kw, out component))
            {
                return(component);
            }

            matchingComponents.Clear();

            foreach (Component c in otherComponents)
            {
                if (kw.Title.Contains("Web Content Management"))
                {
                    Logger.Debug(string.Format("round 2: component {0}, schema {1}", c.Title, c.Schema.Title));
                }

                //The component which is based on any other header schema takes precedence
                if (c.Schema.Title.StartsWith("Header")) // TODO: make configurable
                {
                    matchingComponents.Add(c);
                }
            }

            if (TryFindComponentWithinMatchingComponents(matchingComponents, kw, out component))
            {
                return(component);
            }

            if (kw.Title.Contains("Web Content Management"))
            {
                Logger.Debug(string.Format("no rules matched, returning first component in list ({0})", otherComponents[0].Title));
            }

            if (TryFindComponentWithinMatchingComponents(otherComponents, kw, out component))
            {
                return(component);
            }



            return(null);
        }