Search in sources :

Example 11 with SiteItem

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

the class SiteItemScriptResolverImplTest method testGetScriptUrlError.

@Test
public void testGetScriptUrlError() throws Exception {
    SiteItem siteItem = mock(SiteItem.class);
    when(siteItem.queryValue("content-type")).thenReturn("/page/mypage3");
    when(siteItem.queryValues("scripts/item/key")).thenReturn(Arrays.asList("/scripts/pages/script1.groovy"));
    List<String> scriptUrls = scriptResolver.getScriptUrls(siteItem);
    assertNotNull(scriptUrls);
    assertEquals(1, scriptUrls.size());
    assertEquals("/scripts/pages/script1.groovy", scriptUrls.get(0));
}
Also used : SiteItem(org.craftercms.engine.model.SiteItem) Test(org.junit.Test)

Example 12 with SiteItem

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

the class ExecuteControllerDirective method createScriptVariables.

protected Map<String, Object> createScriptVariables(Environment env) throws TemplateException {
    Map<String, Object> variables = new HashMap<String, Object>();
    RequestContext context = RequestContext.getCurrent();
    SiteItem contentModel = getContentModel(env);
    Object templateModel = getTemplateModel(env);
    if (context != null) {
        GroovyScriptUtils.addSiteItemScriptVariables(variables, context.getRequest(), context.getResponse(), servletContext, contentModel, templateModel);
    } else {
        throw new IllegalStateException("No current request context found");
    }
    return variables;
}
Also used : HashMap(java.util.HashMap) RequestContext(org.craftercms.commons.http.RequestContext) SiteItem(org.craftercms.engine.model.SiteItem)

Example 13 with SiteItem

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

the class RenderComponentDirective method execute.

@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
    TemplateModel componentParentParam = (TemplateModel) params.get(COMPONENT_PARENT_PARAM_NAME);
    TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
    TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
    TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
    Map<String, Object> additionalModel = null;
    SiteItem component;
    if (componentParam == null && componentPathParam == null) {
        throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
    } else if (componentParam != null) {
        component = getComponentFromNode(componentParentParam, componentParam, env);
    } else {
        component = getComponentFromPath(componentPathParam, env);
    }
    if (additionalModelParam != null) {
        additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
    }
    Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
    SimpleHash model = getFullModel(component, templateModel, additionalModel);
    Template template = getTemplate(component, env);
    Writer output = env.getOut();
    processComponentTemplate(template, model, output, env);
}
Also used : TemplateException(freemarker.template.TemplateException) SimpleHash(freemarker.template.SimpleHash) TemplateModel(freemarker.template.TemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) SiteItem(org.craftercms.engine.model.SiteItem) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 14 with SiteItem

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

the class SiteItemServiceImpl method getSiteItem.

@Override
public SiteItem getSiteItem(String url, ItemProcessor processor, Predicate<Item> predicate) {
    SiteContext context = getSiteContext();
    if (!storeService.exists(context.getContext(), url)) {
        return null;
    }
    if (CollectionUtils.isNotEmpty(defaultPredicates)) {
        List<Predicate<Item>> predicates = new ArrayList<>(defaultPredicates);
        if (predicate != null) {
            predicates.add(predicate);
        }
        predicate = PredicateUtils.allPredicate(predicates);
    }
    if (CollectionUtils.isNotEmpty(defaultProcessors)) {
        ItemProcessorPipeline processorPipeline = new ItemProcessorPipeline(new ArrayList<>(defaultProcessors));
        if (processor != null) {
            processorPipeline.addProcessor(processor);
        }
        processor = processorPipeline;
    }
    Item item = storeService.findItem(context.getContext(), null, url, processor);
    if (item != null && (predicate == null || predicate.evaluate(item))) {
        return createItemWrapper(item);
    } else {
        return null;
    }
}
Also used : Item(org.craftercms.core.service.Item) SiteItem(org.craftercms.engine.model.SiteItem) DefaultSiteItem(org.craftercms.engine.model.DefaultSiteItem) EmbeddedSiteItem(org.craftercms.engine.model.EmbeddedSiteItem) ItemProcessorPipeline(org.craftercms.core.processors.impl.ItemProcessorPipeline) SiteContext(org.craftercms.engine.service.context.SiteContext) ArrayList(java.util.ArrayList) Predicate(org.apache.commons.collections4.Predicate)

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