Search in sources :

Example 1 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.

the class MacroInstruction method resolveMacroDescriptor.

private MacroDescriptor resolveMacroDescriptor(final SiteConfigs siteConfigs, final String macroName) {
    // Searches for the macro in the applications associated to the site
    MacroDescriptor macroDescriptor = siteConfigs.stream().map(siteConfig -> MacroKey.from(siteConfig.getApplicationKey(), macroName)).map(macroDescriptorService::getByKey).filter(Objects::nonNull).findFirst().orElse(null);
    if (macroDescriptor == null) {
        macroDescriptor = resolveMacroDescriptorCaseInsensitive(siteConfigs, macroName);
    }
    // If there is no corresponding macro
    if (macroDescriptor == null) {
        // Searches in the builtin macros
        final MacroKey macroKey = MacroKey.from(ApplicationKey.SYSTEM, macroName);
        macroDescriptor = macroDescriptorService.getByKey(macroKey);
    }
    return macroDescriptor;
}
Also used : MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Objects(java.util.Objects)

Example 2 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.

the class MacroInstruction method evaluate.

@Override
public PortalResponse evaluate(final PortalRequest portalRequest, final String instruction) {
    if (!Instruction.isInstruction(instruction, "MACRO")) {
        return null;
    }
    // parse instruction
    final Instruction macroInstruction;
    try {
        macroInstruction = new InstructionParser().parse(instruction);
    } catch (RenderException e) {
        return null;
    }
    final String macroName = macroInstruction.attribute(MACRO_NAME);
    if (macroName == null) {
        return null;
    }
    // resolve macro processor
    final Site site = portalRequest.getSite();
    if (site == null) {
        throw new RenderException("Macro controller script could not be resolved, context site could not be found.");
    }
    final MacroDescriptor macroDescriptor = resolveMacroDescriptor(site.getSiteConfigs(), macroName);
    if (macroDescriptor == null) {
        final String editModeMacro = toMacroInstruction(macroInstruction);
        return PortalResponse.create().body(editModeMacro).build();
    }
    final MacroProcessor macroProcessor = resolveMacroProcessor(macroDescriptor);
    if (macroProcessor == null) {
        throw new RenderException("Macro controller not found: " + macroName);
    }
    // execute macro
    final MacroContext context = createContext(macroInstruction, macroDescriptor, portalRequest);
    final ApplicationKey previousAppKey = portalRequest.getApplicationKey();
    try {
        portalRequest.setApplicationKey(macroDescriptor.getKey().getApplicationKey());
        return macroProcessor.process(context);
    } finally {
        portalRequest.setApplicationKey(previousAppKey);
    }
}
Also used : Site(com.enonic.xp.site.Site) ApplicationKey(com.enonic.xp.app.ApplicationKey) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) MacroProcessor(com.enonic.xp.portal.macro.MacroProcessor) MacroContext(com.enonic.xp.portal.macro.MacroContext) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction)

Example 3 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.

the class MacroDescriptorServiceTest method testGetBySystemKey.

@Test
public void testGetBySystemKey() throws Exception {
    final MacroKey macroKey = MacroKey.from(ApplicationKey.SYSTEM, "disable");
    final MacroDescriptor descriptor = this.service.getByKey(macroKey);
    assertNotNull(descriptor);
    assertTrue(descriptor.getKey().equals(macroKey));
    assertEquals("Disable macros", descriptor.getDisplayName());
    assertEquals("Contents of this macro will not be formatted", descriptor.getDescription());
    assertNotNull(descriptor.getForm());
}
Also used : MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Test(org.junit.jupiter.api.Test)

Example 4 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.

the class MacroDescriptorServiceTest method testGetByKey.

@Test
public void testGetByKey() throws Exception {
    final MacroKey macroKey = MacroKey.from(ApplicationKey.from("myapp1"), "macro1");
    final MacroDescriptor descriptor = this.service.getByKey(macroKey);
    assertNotNull(descriptor);
    assertTrue(descriptor.getKey().equals(macroKey));
}
Also used : MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Test(org.junit.jupiter.api.Test)

Example 5 with MacroDescriptor

use of com.enonic.xp.macro.MacroDescriptor in project xp by enonic.

the class MacroDescriptorServiceTest method testIconAdded.

@Test
public void testIconAdded() throws Exception {
    final MacroKey macroKey = MacroKey.from(ApplicationKey.SYSTEM, "disable");
    final MacroDescriptor descriptor = this.service.getByKey(macroKey);
    assertNotNull(descriptor);
    assertTrue(descriptor.getKey().equals(macroKey));
    assertNotNull(descriptor.getIcon());
}
Also used : MacroKey(com.enonic.xp.macro.MacroKey) MacroDescriptor(com.enonic.xp.macro.MacroDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

MacroDescriptor (com.enonic.xp.macro.MacroDescriptor)17 MacroKey (com.enonic.xp.macro.MacroKey)12 Test (org.junit.jupiter.api.Test)9 MacroDescriptors (com.enonic.xp.macro.MacroDescriptors)7 RenderException (com.enonic.xp.portal.impl.rendering.RenderException)6 ApplicationKey (com.enonic.xp.app.ApplicationKey)5 Form (com.enonic.xp.form.Form)5 Input (com.enonic.xp.form.Input)5 MacroProcessor (com.enonic.xp.portal.macro.MacroProcessor)5 Site (com.enonic.xp.site.Site)5 SiteConfig (com.enonic.xp.site.SiteConfig)5 ContentId (com.enonic.xp.content.ContentId)4 ContentPath (com.enonic.xp.content.ContentPath)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 InputTypeName (com.enonic.xp.inputtype.InputTypeName)4 MacroDescriptorService (com.enonic.xp.macro.MacroDescriptorService)4 PortalRequest (com.enonic.xp.portal.PortalRequest)4 PortalResponse (com.enonic.xp.portal.PortalResponse)4 MacroProcessorFactory (com.enonic.xp.portal.macro.MacroProcessorFactory)4 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)4