Search in sources :

Example 71 with VelocityContext

use of org.apache.velocity.VelocityContext in project OpenOLAT by OpenOLAT.

the class QTI12ResultsExportMediaResource method createMemberListingHTML.

private String createMemberListingHTML(List<AssessedMember> assessedMembers) {
    Collections.sort(assessedMembers, new Comparator<AssessedMember>() {

        @Override
        public int compare(AssessedMember o1, AssessedMember o2) {
            return o1.getUsername().compareTo(o2.getUsername());
        }
    });
    // now put values to velocityContext
    VelocityContext ctx = new VelocityContext();
    ctx.put("t", translator);
    ctx.put("rootTitle", translator.translate("table.overview"));
    ctx.put("assessedMembers", assessedMembers);
    String template = FileUtils.load(QTI12ResultsExportMediaResource.class.getResourceAsStream("_content/qtiUserlisting.html"), "utf-8");
    return velocityHelper.evaluateVTL(template, ctx);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Example 72 with VelocityContext

use of org.apache.velocity.VelocityContext in project OpenOLAT by OpenOLAT.

the class VelocityTemplateTest method testTemplates.

private void testTemplates(String dir, File file, List<Exception> exs) {
    String name = file.getName();
    if ("_content".equals(name)) {
        File[] templates = file.listFiles();
        for (File template : templates) {
            String templateName = template.getName();
            if (templateName.endsWith(".html")) {
                try {
                    String path = dir + templateName;
                    StringWriter writer = new StringWriter();
                    Context context = new VelocityContext();
                    Template veloTemplate = engine.getTemplate(path);
                    veloTemplate.merge(context, writer);
                    count++;
                } catch (Exception e) {
                    exs.add(e);
                }
            }
        }
    } else if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File child : files) {
            String subDir = dir + child.getName() + "/";
            testTemplates(subDir, child, exs);
        }
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) Template(org.apache.velocity.Template)

Example 73 with VelocityContext

use of org.apache.velocity.VelocityContext in project OpenOLAT by OpenOLAT.

the class MailTest method testVelocityTemplate.

/**
 * this is more a playground method to understand the evaluate method than a
 * rela testcase
 */
@Test
public void testVelocityTemplate() {
    // 
    VelocityContext context = new VelocityContext();
    // good case
    StringWriter writer = new StringWriter();
    context.put("foo", "bar");
    String template = "foo $foo";
    MailerResult result = new MailerResult();
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.OK, result.getReturnCode());
    assertEquals("foo bar", writer.toString());
    writer = new StringWriter();
    template = "foo foo";
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.OK, result.getReturnCode());
    assertEquals("foo foo", writer.toString());
    writer = new StringWriter();
    template = "foo $$foo";
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.OK, result.getReturnCode());
    assertEquals("foo $bar", writer.toString());
    writer = new StringWriter();
    template = "foo $ foo";
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.OK, result.getReturnCode());
    assertEquals("foo $ foo", writer.toString());
    writer = new StringWriter();
    template = "foo $ foo";
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.OK, result.getReturnCode());
    assertEquals("foo $ foo", writer.toString());
    writer = new StringWriter();
    template = "foo #foo \n##sdf jubla";
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.OK, result.getReturnCode());
    assertEquals("foo #foo \n", writer.toString());
    writer = new StringWriter();
    template = "foo #if(true)\n#end";
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.OK, result.getReturnCode());
    assertEquals("foo ", writer.toString());
    // illegal templates: unclosed if-else statement
    writer = new StringWriter();
    template = "foo #if";
    ((MailManagerImpl) mailManager).evaluate(context, template, writer, result);
    assertEquals(MailerResult.TEMPLATE_PARSE_ERROR, result.getReturnCode());
    assertEquals("", writer.toString());
}
Also used : StringWriter(java.io.StringWriter) MailerResult(org.olat.core.util.mail.MailerResult) VelocityContext(org.apache.velocity.VelocityContext) Test(org.junit.Test)

Example 74 with VelocityContext

use of org.apache.velocity.VelocityContext in project OpenOLAT by OpenOLAT.

the class MailTest method testMailToCcBccTogether.

/**
 * Test for the mail template and the context variable methods
 */
@Test
public void testMailToCcBccTogether() {
    String subject = "Together Subject: Hello everybody";
    String body = "Together Body: \n\n You should go to \n\n'$coursename' @ $courseURL";
    final String coursename = "my course";
    final String courseURL = "http://www.mytrashmail.com/";
    MailTemplate template = new MailTemplate(subject, body, null) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity identity) {
            // identity is null in this mode - template parsed only once not for
            // everybody
            // Put variables from greater context, eg. course id, group name etc.
            context.put("coursename", coursename);
            context.put("courseURL", courseURL);
        }
    };
    // some recipients data
    List<Identity> recipients = new ArrayList<Identity>();
    recipients.add(id1);
    recipients.add(id2);
    recipients.add(id3);
    Identity recipientCC = id4;
    // tests with / witthout CC and BCC
    MailerResult result = new MailerResult();
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
    assertEquals(MailerResult.OK, result.getReturnCode());
    result = sendMailAsSeparateMails(null, recipients, recipientCC, template, id6, null);
    assertEquals(MailerResult.OK, result.getReturnCode());
    result = sendMailAsSeparateMails(null, recipients, null, template, id6, null);
    assertEquals(MailerResult.OK, result.getReturnCode());
}
Also used : MailerResult(org.olat.core.util.mail.MailerResult) VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) ArrayList(java.util.ArrayList) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Example 75 with VelocityContext

use of org.apache.velocity.VelocityContext in project OpenOLAT by OpenOLAT.

the class MailTest method testMailAttachments.

/**
 * Test for the mail template and the context variable methods
 */
@Test
public void testMailAttachments() {
    String subject = "Subject: Hello $firstname with attachment";
    String body = "Body: \n\n Hey $login, here's a file for you: ";
    // some attachemnts
    File[] attachments = new File[1];
    File file1;
    try {
        System.out.println("MailTest.testMailAttachments Url1=" + MailTest.class.getResource("MailTest.class"));
        file1 = new File(MailTest.class.getResource("MailTest.class").toURI());
        attachments[0] = file1;
    // TODO: cg Properties file is in olat_core.jar and not be lookup as resource (jar:file:...)
    // System.out.println("MailTest.testMailAttachments Url2=" + MailTest.class.getResource("_i18n/LocalStrings_de.properties") );
    // file2 = new File(MailTest.class.getResource("_i18n/LocalStrings_de.properties").toURI());
    // attachments[1] = file2;
    } catch (URISyntaxException e) {
        fail("ups, can't get testfiles from local path: MailTest.class and _i18n/LocalStrings_de.properties");
    }
    MailTemplate template = new MailTemplate(subject, body, attachments) {

        @Override
        public void putVariablesInMailContext(VelocityContext context, Identity identity) {
            // Put user variables
            User user = identity.getUser();
            context.put("firstname", user.getProperty(UserConstants.FIRSTNAME, null));
            context.put("login", identity.getName());
        }
    };
    // some recipients data
    List<Identity> recipients = new ArrayList<Identity>();
    recipients.add(id1);
    MailerResult result = new MailerResult();
    result = sendMailAsSeparateMails(null, recipients, null, template, id2, null);
    assertEquals(MailerResult.OK, result.getReturnCode());
}
Also used : User(org.olat.core.id.User) MailerResult(org.olat.core.util.mail.MailerResult) VelocityContext(org.apache.velocity.VelocityContext) MailTemplate(org.olat.core.util.mail.MailTemplate) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) Identity(org.olat.core.id.Identity) File(java.io.File) Test(org.junit.Test)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)492 StringWriter (java.io.StringWriter)156 Template (org.apache.velocity.Template)120 Test (org.junit.Test)72 IOException (java.io.IOException)60 VelocityEngine (org.apache.velocity.app.VelocityEngine)53 File (java.io.File)47 ArrayList (java.util.ArrayList)39 HashMap (java.util.HashMap)36 Map (java.util.Map)36 Identity (org.olat.core.id.Identity)36 Context (org.apache.velocity.context.Context)32 MailTemplate (org.olat.core.util.mail.MailTemplate)28 Writer (java.io.Writer)22 Properties (java.util.Properties)20 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)19 ParseErrorException (org.apache.velocity.exception.ParseErrorException)16 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)16 FileWriter (java.io.FileWriter)15 OutputStreamWriter (java.io.OutputStreamWriter)14