Search in sources :

Example 6 with Template

use of org.xwiki.template.Template in project xwiki-platform by xwiki.

the class DefaultNotificationRSSRenderer method renderNotification.

@Override
public SyndEntry renderNotification(CompositeEvent eventNotification) throws NotificationException {
    SyndEntry entry = new SyndEntryImpl();
    SyndContent entryDescription = new SyndContentImpl();
    // The users contained in the CompositeEvent are already stored in a Set, they are therefore necessarily unique
    List<SyndPerson> eventAuthors = new ArrayList<SyndPerson>();
    // Convert every author of the CompositeEvent to a SyndPerson and add it to the new entry
    for (DocumentReference author : eventNotification.getUsers()) {
        SyndPerson person = new SyndPersonImpl();
        person.setName(author.getName());
        eventAuthors.add(person);
    }
    entry.setAuthors(eventAuthors);
    // Define the GUID of the event
    entry.setUri(String.join("-", eventNotification.getEventIds()));
    // Set the entry title
    entry.setTitle(this.contextualLocalizationManager.getTranslationPlain(eventNotification.getEvents().get(0).getTitle(), eventNotification.getEvents().get(0).getDocumentTitle()));
    // Render the description (the main part) of the feed entry
    try {
        this.scriptContextManager.getCurrentScriptContext().setAttribute(COMPOSITE_EVENT_BUILDING_NAME, eventNotification, ScriptContext.ENGINE_SCOPE);
        // Try to get a template associated with the composite event
        Template template = this.templateManager.getTemplate(String.format("notification/rss/%s.vm", eventNotification.getType().replaceAll("\\/", ".")));
        // If no template is found, fallback on the default one
        if (template == null) {
            template = this.templateManager.getTemplate("notification/rss/default.vm");
        }
        XDOM descriptionXDOM = this.templateManager.execute(template);
        WikiPrinter printer = new DefaultWikiPrinter();
        blockRenderer.render(descriptionXDOM, printer);
        // Add the description to the entry
        entryDescription.setType("text/html");
        entryDescription.setValue(printer.toString());
        entry.setDescription(entryDescription);
    } catch (Exception e) {
        throw new NotificationException(String.format("Unable to render the description of the event [%s].", eventNotification), e);
    } finally {
        this.scriptContextManager.getCurrentScriptContext().removeAttribute(COMPOSITE_EVENT_BUILDING_NAME, ScriptContext.ENGINE_SCOPE);
    }
    // Dates are sorted in descending order in a CompositeEvent, the first date is then the most recent one
    entry.setUpdatedDate(eventNotification.getDates().get(0));
    return entry;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) NotificationException(org.xwiki.notifications.NotificationException) Template(org.xwiki.template.Template) SyndPerson(com.rometools.rome.feed.synd.SyndPerson) SyndPersonImpl(com.rometools.rome.feed.synd.SyndPersonImpl) SyndContent(com.rometools.rome.feed.synd.SyndContent) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 7 with Template

use of org.xwiki.template.Template in project xwiki-platform by xwiki.

the class PropertyClass method getDefaultCustomDisplayer.

/**
 * Method to find the default custom displayer to use for a specific Property Class.
 *
 * @param propertyClassName the type of the property; this is defined in each subclass, such as {@code boolean},
 *            {@code string} or {@code dblist}
 * @param context the current request context
 * @return An identifier for the location of a custom displayer. This can be {@code class} if there's custom display
 *         code specified in the class itself, {@code page:currentwiki:XWiki.BooleanDisplayer} if such a document
 *         exists in the current wiki, {@code page:xwiki:XWiki.StringDisplayer} if such a document exists in the
 *         main wiki, or {@code template:displayer_boolean.vm} if a template on the filesystem or in the current
 *         skin exists.
 */
protected String getDefaultCustomDisplayer(String propertyClassName, XWikiContext context) {
    LOGGER.debug("Looking up default custom displayer for property class name [{}]", propertyClassName);
    try {
        // First look into the current wiki
        String pageName = StringUtils.capitalize(propertyClassName) + "Displayer";
        DocumentReference reference = new DocumentReference(context.getWikiId(), "XWiki", pageName);
        if (context.getWiki().exists(reference, context)) {
            LOGGER.debug("Found default custom displayer for property class name in local wiki: [{}]", pageName);
            return DOCUMENT_DISPLAYER_IDENTIFIER_PREFIX + "XWiki." + pageName;
        }
        // Look in the main wiki
        if (!context.isMainWiki()) {
            reference = new DocumentReference(context.getMainXWiki(), "XWiki", pageName);
            if (context.getWiki().exists(reference, context)) {
                LOGGER.debug("Found default custom displayer for property class name in main wiki: [{}]", pageName);
                return DOCUMENT_DISPLAYER_IDENTIFIER_PREFIX + context.getMainXWiki() + ":XWiki." + pageName;
            }
        }
        // Look in templates
        String templateName = "displayer_" + propertyClassName + ".vm";
        TemplateManager templateManager = Utils.getComponent(TemplateManager.class);
        Template existingTemplate = templateManager.getTemplate(templateName);
        if (existingTemplate != null) {
            LOGGER.debug("Found default custom displayer for property class name as template: [{}]", templateName);
            return TEMPLATE_DISPLAYER_IDENTIFIER_PREFIX + templateName;
        }
    } catch (Throwable e) {
        // If we fail we consider there is no custom displayer
        LOGGER.error("Error finding if property [{}] has a custom displayer. " + "Considering that there's no custom displayer.", propertyClassName, e);
    }
    return null;
}
Also used : TemplateManager(org.xwiki.template.TemplateManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Template(org.xwiki.template.Template)

Example 8 with Template

use of org.xwiki.template.Template in project xwiki-platform by xwiki.

the class AbstractNotificationEmailRenderer method executeTemplate.

/**
 * Execute a template.
 *
 * @param event composite event to render
 * @param userId id of the user who will receive the email
 * @param templatePath path of the template to use (with a %s that the method will replace by the event type)
 * @param syntax syntax of the template and of the output
 * @return the rendered template
 * @throws NotificationException if something wrong happens
 */
protected Block executeTemplate(CompositeEvent event, String userId, String templatePath, Syntax syntax) throws NotificationException {
    // Generate the full template name
    String templateName = String.format(templatePath, event.getType().replaceAll("\\/", "."));
    // Get the template
    Template template = templateManager.getTemplate(templateName);
    if (template == null) {
        template = templateManager.getTemplate(String.format(templatePath, "default"));
    }
    return emailTemplateRenderer.executeTemplate(event, userId, template, syntax);
}
Also used : Template(org.xwiki.template.Template)

Example 9 with Template

use of org.xwiki.template.Template in project xwiki-platform by xwiki.

the class InternalTemplateManager method getTemplate.

public Template getTemplate(String templateName) {
    Template template = null;
    // Try from skin
    Skin skin = this.skins.getCurrentSkin(false);
    if (skin != null) {
        template = getTemplate(templateName, skin);
    }
    // Try from base skin if no skin is set
    if (skin == null) {
        Skin baseSkin = this.skins.getCurrentParentSkin(false);
        if (baseSkin != null) {
            template = getTemplate(templateName, baseSkin);
        }
    }
    // Try from /templates/ environment resources
    if (template == null) {
        template = getFileSystemTemplate("/templates/", templateName);
    }
    // Try from current Thread classloader
    if (template == null) {
        template = getClassloaderTemplate("templates/", templateName);
    }
    return template;
}
Also used : Skin(org.xwiki.skin.Skin) Template(org.xwiki.template.Template)

Example 10 with Template

use of org.xwiki.template.Template in project xwiki-platform by xwiki.

the class AbstractTemplateJobResourceReferenceHandler method tryTemplate.

protected boolean tryTemplate(String defaultContentType, String templateName) throws ResourceReferenceHandlerException {
    Template template = this.templates.getTemplate("job/" + templateName);
    if (template == null) {
        return false;
    }
    Response response = this.container.getResponse();
    try {
        // Set default content type (can be overwritten by the template itself)
        if (defaultContentType != null) {
            response.setContentType(defaultContentType);
        }
        Writer writer = new StringWriter();
        this.templates.render(template, writer);
        sendContent(writer.toString());
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException("Failed to execute template [" + templateName + "]", e);
    }
    return true;
}
Also used : Response(org.xwiki.container.Response) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) IOException(java.io.IOException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) Template(org.xwiki.template.Template)

Aggregations

Template (org.xwiki.template.Template)15 Test (org.junit.Test)4 NotificationException (org.xwiki.notifications.NotificationException)3 TemplateContent (org.xwiki.template.TemplateContent)3 StringWriter (java.io.StringWriter)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 Skin (org.xwiki.skin.Skin)2 SyndContent (com.rometools.rome.feed.synd.SyndContent)1 SyndContentImpl (com.rometools.rome.feed.synd.SyndContentImpl)1 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)1 SyndEntryImpl (com.rometools.rome.feed.synd.SyndEntryImpl)1 SyndPerson (com.rometools.rome.feed.synd.SyndPerson)1 SyndPersonImpl (com.rometools.rome.feed.synd.SyndPersonImpl)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ScriptContext (javax.script.ScriptContext)1