Search in sources :

Example 1 with SiteItem

use of org.craftercms.engine.model.SiteItem in project engine by craftercms.

the class RenderComponentDirective method getComponentFromNode.

protected SiteItem getComponentFromNode(TemplateModel parentParam, TemplateModel componentParam, Environment env) throws TemplateException {
    SiteItem parentItem = unwrap(COMPONENT_PARENT_PARAM_NAME, parentParam, SiteItem.class, env);
    Element includeElementParent = unwrap(COMPONENT_PARAM_NAME, componentParam, Element.class, env);
    Element includeElement = includeElementParent.element(includeElementName);
    Element componentElement = includeElementParent.element(componentElementName);
    if (includeElement != null) {
        logger.debug("Using the include element to load the site item");
        String componentPath = includeElement.getTextTrim();
        return getComponent(componentPath, env);
    } else if (componentElement != null) {
        logger.debug("Using an embedded site item");
        if (parentItem == null) {
            logger.debug("Using default parent component");
            parentItem = unwrap(KEY_CONTENT_MODEL, env.getVariable(CrafterPageView.KEY_CONTENT_MODEL), SiteItem.class, env);
        }
        return siteItemService.getSiteItem(parentItem, componentElement);
    } else {
        throw new IllegalStateException("No '" + includeElementName + "' or '" + componentElementName + "' element " + "found under component " + includeElementParent.getUniquePath());
    }
}
Also used : Element(org.dom4j.Element) SiteItem(org.craftercms.engine.model.SiteItem)

Example 2 with SiteItem

use of org.craftercms.engine.model.SiteItem in project engine by craftercms.

the class RenderComponentDirective method getComponent.

protected SiteItem getComponent(String componentPath, Environment env) throws TemplateException {
    SiteItem currentPage = unwrap(KEY_CONTENT_MODEL, env.getVariable(CrafterPageView.KEY_CONTENT_MODEL), SiteItem.class, env);
    if (currentPage != null) {
        String basePath = FilenameUtils.getFullPath(currentPage.getStoreUrl());
        URI baseUri = URI.create(basePath);
        try {
            componentPath = baseUri.resolve(componentPath).toString();
        } catch (IllegalArgumentException e) {
            throw new TemplateException("Invalid relative component URL " + componentPath, e, env);
        }
    }
    SiteItem component;
    try {
        component = siteItemService.getSiteItem(componentPath);
    } catch (Exception e) {
        throw new TemplateException("Unable to load component " + componentPath + " from the underlying repository", e, env);
    }
    if (component == null) {
        throw new TemplateException("No component found at path '" + componentPath + "'", env);
    }
    return component;
}
Also used : TemplateException(freemarker.template.TemplateException) URI(java.net.URI) SiteItem(org.craftercms.engine.model.SiteItem) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 3 with SiteItem

use of org.craftercms.engine.model.SiteItem in project engine by craftercms.

the class NavBreadcrumbBuilderImpl method getBreadcrumb.

@Override
public List<NavItem> getBreadcrumb(String url, String root, Converter<SiteItem, NavItem> itemConverter) {
    if (itemConverter == null) {
        itemConverter = defaultItemConverter;
    }
    List<NavItem> breadcrumb = new ArrayList<>();
    String breadcrumbUrl = extractBreadcrumbUrl(url, root);
    String[] breadcrumbUrlComponents = breadcrumbUrl.split("/");
    String currentUrl = root;
    for (String breadcrumbUrlComponent : breadcrumbUrlComponents) {
        currentUrl = UrlUtils.concat(currentUrl, breadcrumbUrlComponent);
        SiteItem siteItem = siteItemService.getSiteItem(currentUrl, processor);
        if (siteItem != null) {
            NavItem navItem = itemConverter.convert(siteItem);
            if (navItem != null) {
                navItem.setActive(url.equals(siteItem.getItem().getDescriptorUrl()));
                breadcrumb.add(navItem);
            }
        }
    }
    return breadcrumb;
}
Also used : ArrayList(java.util.ArrayList) NavItem(org.craftercms.engine.navigation.NavItem) SiteItem(org.craftercms.engine.model.SiteItem)

Example 4 with SiteItem

use of org.craftercms.engine.model.SiteItem in project engine by craftercms.

the class NavTreeBuilderImplTest method getSiteItemService.

private SiteItemService getSiteItemService() {
    SiteItemService siteItemService = mock(SiteItemService.class);
    SiteItem item1 = createSiteItem(URL1);
    SiteItem item3 = createSiteItem(URL3);
    SiteItem item2 = createSiteItem(URL2, item3);
    SiteItem rootItem = createSiteItem(ROOT_URL, item1, item2);
    when(siteItemService.getSiteTree(ROOT_URL, -1, (ItemFilter) null, null)).thenReturn(rootItem);
    return siteItemService;
}
Also used : SiteItemService(org.craftercms.engine.service.SiteItemService) SiteItem(org.craftercms.engine.model.SiteItem)

Example 5 with SiteItem

use of org.craftercms.engine.model.SiteItem in project engine by craftercms.

the class NavTreeBuilderImplTest method getItemConverter.

private Converter<SiteItem, NavItem> getItemConverter() {
    Converter<SiteItem, NavItem> converter = mock(Converter.class);
    doAnswer((Answer<NavItem>) invocation -> {
        SiteItem siteItem = (SiteItem) invocation.getArguments()[0];
        NavItem navItem = new NavItem();
        navItem.setLabel(siteItem.getStoreName());
        navItem.setUrl(siteItem.getStoreUrl());
        return navItem;
    }).when(converter).convert(any(SiteItem.class));
    return converter;
}
Also used : ItemFilter(org.craftercms.core.service.ItemFilter) Answer(org.mockito.stubbing.Answer) Mockito(org.mockito.Mockito) Arrays(java.util.Arrays) Converter(org.craftercms.commons.converters.Converter) SiteItem(org.craftercms.engine.model.SiteItem) SiteItemService(org.craftercms.engine.service.SiteItemService) Test(org.junit.Test) NavItem(org.craftercms.engine.navigation.NavItem) Assert(org.junit.Assert) FilenameUtils(org.apache.commons.io.FilenameUtils) Before(org.junit.Before) NavItem(org.craftercms.engine.navigation.NavItem) SiteItem(org.craftercms.engine.model.SiteItem)

Aggregations

SiteItem (org.craftercms.engine.model.SiteItem)14 Test (org.junit.Test)4 NavItem (org.craftercms.engine.navigation.NavItem)3 SiteItemService (org.craftercms.engine.service.SiteItemService)3 TemplateException (freemarker.template.TemplateException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Item (org.craftercms.core.service.Item)2 SiteContext (org.craftercms.engine.service.context.SiteContext)2 SimpleHash (freemarker.template.SimpleHash)1 Template (freemarker.template.Template)1 TemplateModel (freemarker.template.TemplateModel)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Predicate (org.apache.commons.collections4.Predicate)1 FilenameUtils (org.apache.commons.io.FilenameUtils)1 Converter (org.craftercms.commons.converters.Converter)1