TeamMentor.CoreLib.TM_Xml_Database_ExtensionMethods_XmlDataSources_GuidanceItem.xmlBD_resolveDirectMapping C# (CSharp) Method

xmlBD_resolveDirectMapping() public static method

public static xmlBD_resolveDirectMapping ( this tmDatabase, string mapping ) : System.Guid
tmDatabase this
mapping string
return System.Guid
        public static Guid xmlBD_resolveDirectMapping(this TM_Xml_Database tmDatabase, string mapping)
        {
            if (mapping.inValid())
                return Guid.Empty;

            /*foreach(var item in TM_Xml_Database.Current.Cached_GuidanceItems)
                if(item.Value.Metadata.DirectLink.lower() == mapping ||item.Value.Metadata.Title.lower() == mapping)
                    return item.Key;
            */
            mapping = mapping.lower();

            //first resolve by direct link
            var directLinkResult = (from item in TM_Xml_Database.Current.Cached_GuidanceItems
                where (item.Value.Metadata.DirectLink.notNull() && item.Value.Metadata.DirectLink.lower() == mapping)
                select item.Key).first();
            if (directLinkResult != Guid.Empty)
                return directLinkResult;

            var mapping_Segments = mapping.split("^");

            //if there are no ^ on the title: resolve by title
            if (mapping_Segments.size() == 1)
            {
                var mapping_Extra = mapping.Replace(" ", "_");
                var titleResult = (from item in TM_Xml_Database.Current.Cached_GuidanceItems
                    where titleMatch(item.Value, mapping, mapping_Extra)
                    select item.Key).first();
                if (titleResult != Guid.Empty)
                    return titleResult;
            }
                //if there are ^ on the title: resolve by title^library^technology^phase^type^category
            else
            {
                var title       = mapping_Segments.value(0);
                var title_Extra = title.valid() ? title.Replace(" ", "_") : title;
                var library     = mapping_Segments.value(1);
                var technology  = mapping_Segments.value(2);
                var phase       = mapping_Segments.value(3);
                var type        = mapping_Segments.value(4);
                var category    = mapping_Segments.value(5);

                //var libraryNames = tmDatabase.tmLibraries().names().lower();//pre calculate this to make it faster

                foreach (var item in TM_Xml_Database.Current.Cached_GuidanceItems)
                {
                    if (titleMatch(item.Value, title, title_Extra))             // check Title
                    {
                        if (library.inValid())
                            return item.Key;
                        if (tmDatabase.tmLibrary(item.Value.Metadata.Library_Id).Caption.lower() == library)                     // check Library
                        {
                            if (technology.inValid())
                                return item.Key;
                            if (item.Value.Metadata.Technology.lower() == technology)   // check Technology
                            {
                                if (phase.inValid())
                                    return item.Key;
                                if (item.Value.Metadata.Phase.lower() == phase)         // check Phase
                                {
                                    if (type.inValid())
                                        return item.Key;
                                    if (item.Value.Metadata.Type.lower() == type)      // check type
                                    {
                                        if (category.inValid())
                                            return item.Key;
                                        if (item.Value.Metadata.Category.lower() == category) // check category
                                            return item.Key;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return Guid.Empty;
        }