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);
}
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);
}
}
}
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());
}
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());
}
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());
}
Aggregations