Search in sources :

Example 31 with Template

use of groovy.text.Template in project cuba by cuba-platform.

the class UserManagementServiceBean method loadDefaultTemplate.

protected Template loadDefaultTemplate(String templatePath, SimpleTemplateEngine templateEngine) {
    String defaultTemplateContent = resources.getResourceAsString(templatePath);
    if (defaultTemplateContent == null) {
        throw new IllegalStateException("Not found default email template for reset passwords operation");
    }
    // noinspection UnnecessaryLocalVariable
    Template template = getTemplate(templateEngine, defaultTemplateContent);
    return template;
}
Also used : Template(groovy.text.Template)

Example 32 with Template

use of groovy.text.Template in project cuba by cuba-platform.

the class UserManagementServiceBean method getResetPasswordTemplate.

protected EmailTemplate getResetPasswordTemplate(User user, SimpleTemplateEngine templateEngine, String resetPasswordSubjectTemplate, String resetPasswordBodyTemplate, Template subjectDefaultTemplate, Template bodyDefaultTemplate, Map<String, Template> localizedSubjectTemplates, Map<String, Template> localizedBodyTemplates) {
    boolean userLocaleIsUnknown = StringUtils.isEmpty(user.getLanguage());
    String locale = userLocaleIsUnknown ? messageTools.getDefaultLocale().getLanguage() : user.getLanguage();
    Template bodyTemplate;
    if (userLocaleIsUnknown) {
        bodyTemplate = bodyDefaultTemplate;
    } else {
        if (localizedBodyTemplates.containsKey(locale))
            bodyTemplate = localizedBodyTemplates.get(locale);
        else {
            String templateString = getLocalizedTemplateContent(resetPasswordBodyTemplate, locale);
            if (templateString == null) {
                log.debug("Reset passwords: Not found email body template for locale: '{}'", locale);
                bodyTemplate = bodyDefaultTemplate;
            } else {
                bodyTemplate = getTemplate(templateEngine, templateString);
            }
            localizedBodyTemplates.put(locale, bodyTemplate);
        }
    }
    Template subjectTemplate;
    if (userLocaleIsUnknown) {
        subjectTemplate = subjectDefaultTemplate;
    } else {
        if (localizedSubjectTemplates.containsKey(locale))
            subjectTemplate = localizedSubjectTemplates.get(locale);
        else {
            String templateString = getLocalizedTemplateContent(resetPasswordSubjectTemplate, locale);
            if (templateString == null) {
                log.debug("Reset passwords: Not found email subject template for locale '{}'", locale);
                subjectTemplate = subjectDefaultTemplate;
            } else {
                subjectTemplate = getTemplate(templateEngine, templateString);
            }
            localizedSubjectTemplates.put(locale, subjectTemplate);
        }
    }
    return new EmailTemplate(subjectTemplate, bodyTemplate);
}
Also used : Template(groovy.text.Template)

Example 33 with Template

use of groovy.text.Template in project coprhd-controller by CoprHD.

the class TemplateEngine method getTemplate.

/**
 * Returns a cached template file
 */
private static synchronized Template getTemplate(File templateFile) {
    if (templates.containsKey(templateFile.getName())) {
        return templates.get(templateFile.getName());
    }
    SimpleTemplateEngine engine = new SimpleTemplateEngine();
    try {
        String templateContents = IOUtils.toString(new FileInputStream(templateFile));
        templateContents = preprocessTemplate(templateContents);
        Template template = engine.createTemplate(templateContents);
        templates.put(templateFile.getName(), template);
        return template;
    } catch (Exception e) {
        throw new RuntimeException("Unable to process template" + templateFile, e);
    }
}
Also used : SimpleTemplateEngine(groovy.text.SimpleTemplateEngine) Template(groovy.text.Template)

Example 34 with Template

use of groovy.text.Template in project coprhd-controller by CoprHD.

the class TemplateEngine method generateStringFromTemplate.

/**
 * Call the template with the parameters, but return the response as a string
 */
public static String generateStringFromTemplate(File templateFile, Map<String, Object> parameters) {
    try {
        Template template = getTemplate(templateFile);
        Writable finishedTemplate = template.make(parameters);
        return finishedTemplate.toString();
    } catch (Exception e) {
        DocReporter.printError("Error whilst generating page from template " + templateFile);
        DocReporter.printError(e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : Writable(groovy.lang.Writable) Template(groovy.text.Template)

Example 35 with Template

use of groovy.text.Template in project groovy-core by groovy.

the class TemplateServlet method getTemplate.

/**
 * Gets the template created by the underlying engine parsing the request.
 *
 * <p>
 * This method looks up a simple (weak) hash map for an existing template
 * object that matches the source URL. If there is no cache entry, a new one is
 * created by the underlying template engine. This new instance is put
 * to the cache for consecutive calls.
 *
 * @return The template that will produce the response text.
 * @param url The URL containing the template source..
 * @throws ServletException If the request specified an invalid template source URL
 */
protected Template getTemplate(URL url) throws ServletException {
    String key = url.toString();
    Template template = findCachedTemplate(key, null);
    // Template not cached or the source file changed - compile new template!
    if (template == null) {
        try {
            template = createAndStoreTemplate(key, url.openConnection().getInputStream(), null);
        } catch (Exception e) {
            throw new ServletException("Creation of template failed: " + e, e);
        }
    }
    return template;
}
Also used : ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Template(groovy.text.Template)

Aggregations

Template (groovy.text.Template)41 IOException (java.io.IOException)19 HashMap (java.util.HashMap)7 ServletException (javax.servlet.ServletException)7 Writable (groovy.lang.Writable)6 SimpleTemplateEngine (groovy.text.SimpleTemplateEngine)6 Writer (java.io.Writer)6 File (java.io.File)5 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 GroovyObject (groovy.lang.GroovyObject)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 GrailsTagException (org.grails.taglib.GrailsTagException)2 Test (org.junit.Test)2 WebConfig (com.haulmont.cuba.web.WebConfig)1 TestExplanation1 (eu.esdihumboldt.hale.common.align.model.impl.mdexpl.test.TestExplanation1)1