private bool TryBuildElement(SimpleVirtualElement simpleVirtualElement, out Element result)
        {
            try
            {
                result = CreateElement(simpleVirtualElement);
            }
            catch (ConfigurationErrorsException exception)
            {
                string elementName = simpleVirtualElement.Name ?? "";

                lock (_notLoadedVirtualElements)
                {
                    if (!_notLoadedVirtualElements.Contains(elementName))
                    {
                        Log.LogError(LogTitle, "Failed to initialize virtual element/perspective. Label: '{0}', name: '{1}'\n" +
                                     "Remove the related configuration element from /App_Data/Composite/Composite.Config if it refers to a package that is no longer installed.",
                                     simpleVirtualElement.Label ?? "", elementName);

                        Log.LogError(LogTitle, exception);

                        _notLoadedVirtualElements.Add(elementName);
                    }
                }

                result = null;
            }

            return(result != null);
        }
        private SimpleVirtualElement FindElementNode(string name, IEnumerable <VirtualElementConfigurationElement> elements)
        {
            foreach (var element in elements.OfType <SimpleVirtualElement>())
            {
                if (element.Name == name)
                {
                    return(element);
                }

                SimpleVirtualElement foundNode = FindElementNode(name, element.Elements);

                if (foundNode != null)
                {
                    return(foundNode);
                }
            }

            return(null);
        }
        private Element CreateElement(SimpleVirtualElement simpleElementNode)
        {
            EntityToken entityToken = new VirtualElementProviderEntityToken(_context.ProviderName, simpleElementNode.Name);

            Element element = new Element(_context.CreateElementHandle(entityToken))
            {
                TagValue   = simpleElementNode.Tag,
                VisualData = new ElementVisualizedData
                {
                    Label       = StringResourceSystemFacade.ParseString(simpleElementNode.Label),
                    HasChildren = true // fixing refresh problem easy way... was: HasChildren(baseElementNode)
                }
            };



            Action <IEnumerable> collectProviders = null;

            // Recursively searching for attached providers
            var attachedProviders = new List <AttachProviderVirtualElement>();

            collectProviders = currentElements =>
            {
                attachedProviders.AddRange(currentElements.OfType <AttachProviderVirtualElement>());
                currentElements.OfType <SimpleVirtualElement>().ForEach(e => collectProviders(e.Elements));
            };
            collectProviders(simpleElementNode.Elements);


            foreach (var attachedProvider in attachedProviders)
            {
                if (ElementFacade.ContainsLocalizedData(new ElementProviderHandle(attachedProvider.ProviderName)))
                {
                    element.IsLocaleAware = true;
                }
            }


            if (element.VisualData.HasChildren)
            {
                ResourceHandle openHandle  = IconResourceSystemFacade.GetResourceHandle(simpleElementNode.OpenFolderIconName);
                ResourceHandle closeHandle = IconResourceSystemFacade.GetResourceHandle(simpleElementNode.CloseFolderIconName);

                closeHandle = closeHandle ?? openHandle;
                openHandle  = openHandle ?? closeHandle;

                if (openHandle == null)
                {
                    openHandle  = CommonElementIcons.Folder;
                    closeHandle = CommonElementIcons.FolderOpen;
                }

                element.VisualData.Icon       = openHandle;
                element.VisualData.OpenedIcon = closeHandle;
            }
            else
            {
                element.VisualData.Icon = CommonElementIcons.FolderDisabled;
            }


            return(element);
        }
        private IEnumerable <Element> GetChildren(EntityToken entityToken, SearchToken seachToken, bool useForeign)
        {
            IEnumerable <VirtualElementConfigurationElement> elementNodes;

            if (entityToken.Id == RootElementId)
            {
                elementNodes = _configuration.Perspectives;
            }
            else
            {
                SimpleVirtualElement node = FindElementNode(entityToken.Id, _configuration.Perspectives);

                Verify.IsNotNull(node, "No corresponding node was found with the id '{0}'", entityToken.Id);

                elementNodes = node.Elements;
            }

            var result = new List <Element>();

            foreach (var elementNode in elementNodes)
            {
                if (elementNode is SimpleVirtualElement)
                {
                    Element createdElement;
                    if (TryBuildElement(elementNode as SimpleVirtualElement, out createdElement))
                    {
                        result.Add(createdElement);
                    }
                    continue;
                }

                if (elementNode is AttachProviderVirtualElement)
                {
                    string providerName = (elementNode as AttachProviderVirtualElement).ProviderName;

                    var providerHandle = new ElementProviderHandle(providerName);

                    List <Element> elementsFromProvider;
                    if (useForeign && ElementFacade.IsLocaleAwareElementProvider(providerHandle))
                    {
                        elementsFromProvider = ElementFacade.GetForeignRoots(providerHandle, seachToken).ToList();
                    }
                    else
                    {
                        elementsFromProvider = ElementFacade.GetRoots(providerHandle, seachToken).ToList();
                    }

                    foreach (Element element in elementsFromProvider)
                    {
                        element.ElementExternalActionAdding = element.ElementExternalActionAdding.Remove(ElementExternalActionAdding.AllowGlobal);
                    }

                    result.AddRange(elementsFromProvider);
                    continue;
                }

                throw new NotSupportedException();
            }

            return(result);
        }