public Dictionary <EntityToken, IEnumerable <EntityToken> > GetParents(IEnumerable <EntityToken> entityTokens)
        {
            var result = new Dictionary <EntityToken, IEnumerable <EntityToken> >();

            foreach (EntityToken entityToken in entityTokens)
            {
                var dataEntityToken = entityToken as DataEntityToken;

                if (dataEntityToken.InterfaceType == typeof(ISqlFunctionInfo))
                {
                    ISqlFunctionInfo sqlFunctionInfo = dataEntityToken.Data as ISqlFunctionInfo;

                    var parentEntityToken = new SqlFunctionProviderFolderEntityToken(sqlFunctionInfo.Namespace, _context.ProviderName, sqlFunctionInfo.ConnectionId.ToString());

                    result.Add(entityToken, new EntityToken[] { parentEntityToken });
                }
                else if (dataEntityToken.InterfaceType == typeof(ISqlConnection))
                {
                    var parentEntityToken = new SqlFunctionProviderRootEntityToken(_context.ProviderName, _context.ProviderName);

                    result.Add(entityToken, new EntityToken[] { parentEntityToken });
                }
            }

            return(result);
        }
        public IEnumerable <EntityToken> GetParents(EntityToken entityToken)
        {
            if ((entityToken is SqlFunctionProviderRootEntityToken))
            {
                return(new EntityToken[] { });
            }
            else if ((entityToken is SqlFunctionProviderFolderEntityToken))
            {
                SqlFunctionProviderFolderEntityToken token = entityToken as SqlFunctionProviderFolderEntityToken;

                NamespaceTreeBuilder builder = (NamespaceTreeBuilder)ElementFacade.GetData(new ElementProviderHandle(token.Source), token.ConnectionId);

                NamespaceTreeBuilderFolder folder = builder.FindFolder(f => StringExtensionMethods.CreateNamespace(f.Namespace, f.Name, '.') == token.Id);

                if (folder == null)
                {
                    return(null);
                }
                else
                {
                    int idx = token.Id.LastIndexOf('.');
                    if (idx != -1)
                    {
                        return(new EntityToken[] { new SqlFunctionProviderFolderEntityToken(token.Id.Remove(idx), token.Source, token.ConnectionId) });
                    }
                    else
                    {
                        Guid           id            = new Guid(token.ConnectionId);
                        ISqlConnection sqlConnection = DataFacade.GetData <ISqlConnection>(f => f.Id == id).SingleOrDefault();

                        if (sqlConnection == null)
                        {
                            return(new EntityToken[] { });
                        }

                        return(new EntityToken[] { sqlConnection.GetDataEntityToken() });
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
        private List <Element> GetFunctionElements(EntityToken entityToken, SearchToken searchToken)
        {
            Guid   connectionId;
            string namespaceName;

            if ((entityToken is DataEntityToken))
            {
                DataEntityToken dataEntityToken = (DataEntityToken)entityToken;

                if (dataEntityToken.Data is ISqlConnection)
                {
                    connectionId  = ((ISqlConnection)dataEntityToken.Data).Id;
                    namespaceName = "";
                }
                else
                {
                    // stuff has been deleted and we are refreshed on a dead function folder
                    return(new List <Element>());
                }
            }
            else if ((entityToken is SqlFunctionProviderFolderEntityToken))
            {
                SqlFunctionProviderFolderEntityToken sqlFunctionProviderFolderEntityToken = (SqlFunctionProviderFolderEntityToken)entityToken;

                connectionId  = new Guid(sqlFunctionProviderFolderEntityToken.ConnectionId);
                namespaceName = sqlFunctionProviderFolderEntityToken.Id;
            }
            else
            {
                throw new NotImplementedException();
            }


            IEnumerable <ISqlFunctionInfo> sqlFunctionInfoes;

            if (searchToken.IsValidKeyword() == false)
            {
                sqlFunctionInfoes =
                    from item in DataFacade.GetData <ISqlFunctionInfo>()
                    where item.ConnectionId == connectionId
                    select item;
            }
            else
            {
                string keyword = searchToken.Keyword.ToLower();

                sqlFunctionInfoes =
                    from item in DataFacade.GetData <ISqlFunctionInfo>()
                    where item.ConnectionId == connectionId &&
                    (((item.Name != null) && (item.Name.ToLower().Contains(keyword))) ||
                     ((item.Namespace != null) && (item.Namespace.ToLower().Contains(keyword))) ||
                     ((item.Command != null) && (item.Command.ToLower().Contains(keyword))))
                    select item;
            }

            NamespaceTreeBuilder builder = new NamespaceTreeBuilder(sqlFunctionInfoes.Select(q => (INamespaceTreeBuilderLeafInfo) new SqlNamespaceTreeBuilderLeafInfo(q)));

            NamespaceTreeBuilderFolder folderNode;

            if (namespaceName == "")
            {
                folderNode = builder.RootFolder;
            }
            else
            {
                folderNode = builder.GetFolder(namespaceName);
            }

            List <Element> result = new List <Element>();

            if (folderNode != null)
            {
                if (folderNode.SubFolders != null)
                {
                    foreach (NamespaceTreeBuilderFolder node in folderNode.SubFolders)
                    {
                        Element element = CreateFolderElement(node, connectionId.ToString());

                        result.Add(element);
                    }
                }

                if (folderNode.Leafs != null)
                {
                    foreach (INamespaceTreeBuilderLeafInfo leafInfo in folderNode.Leafs)
                    {
                        Element element = CreateXmlFunctionInfoElement(leafInfo, connectionId.ToString());

                        result.Add(element);
                    }
                }
            }

            return(result);
        }