use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class EmailTemplateMigrationService method createInstantEmailTemplate.
/*
* Creates an instant email template and the underlying templates only if:
* - the event type exists in the DB
* - the instant email template does not already exist in the DB
* Existing instant email templates are never updated by this migration service.
*/
private void createInstantEmailTemplate(List<String> warnings, String bundleName, String appName, List<String> eventTypeNames, String subjectTemplateName, String subjectTemplateExtension, String subjectTemplateDescription, String bodyTemplateName, String bodyTemplateExtension, String bodyTemplateDescription) {
for (String eventTypeName : eventTypeNames) {
Optional<EventType> eventType = findEventType(warnings, bundleName, appName, eventTypeName);
if (eventType.isPresent()) {
if (instantEmailTemplateExists(eventType.get())) {
warnings.add(String.format("Instant email template found in DB for event type: %s/%s/%s", bundleName, appName, eventTypeName));
} else {
Template subjectTemplate = getOrCreateTemplate(warnings, subjectTemplateName, subjectTemplateExtension, subjectTemplateDescription);
Template bodyTemplate = getOrCreateTemplate(warnings, bodyTemplateName, bodyTemplateExtension, bodyTemplateDescription);
LOGGER.infof("Creating instant email template for event type: %s/%s/%s", bundleName, appName, eventTypeName);
InstantEmailTemplate emailTemplate = new InstantEmailTemplate();
emailTemplate.setEventType(eventType.get());
emailTemplate.setSubjectTemplate(subjectTemplate);
emailTemplate.setSubjectTemplateId(subjectTemplate.getId());
emailTemplate.setBodyTemplate(bodyTemplate);
emailTemplate.setBodyTemplateId(bodyTemplate.getId());
entityManager.persist(emailTemplate);
}
}
}
}
use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class DbTemplateLocator method locate.
@Override
public Optional<TemplateLocation> locate(String name) {
String hql = "FROM Template WHERE name = :name";
try {
Template template = statelessSessionFactory.getCurrentSession().createQuery(hql, Template.class).setParameter("name", name).getSingleResult();
LOGGER.tracef("Template with [name=%s] found in the database", name);
return Optional.of(buildTemplateLocation(template.getData()));
} catch (NoResultException e) {
LOGGER.tracef("Template with [name=%s] not found in the database", name);
return Optional.empty();
}
}
use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class DbQuteEngineTest method testToUtcFormatExtension.
@Test
void testToUtcFormatExtension() {
Template template = createTemplate("to-utc-format-template", "{date.toUtcFormat()}");
LocalDateTime date = LocalDateTime.of(2022, Month.JANUARY, 1, 0, 0);
TemplateInstance templateInstance = templateService.compileTemplate(template.getData(), template.getName());
assertEquals("01 Jan 2022 00:00 UTC", templateInstance.data("date", date).render());
assertEquals("01 Jan 2022 00:00 UTC", templateInstance.data("date", date.toString()).render());
}
use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class DbQuteEngineTest method testIncludeExistingTemplate.
@Test
void testIncludeExistingTemplate() {
Template outerTemplate = createTemplate("outer-template", "Hello, {#include inner-template /}");
Template innerTemplate = createTemplate("inner-template", "World!");
statelessSessionFactory.withSession(statelessSession -> {
String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
assertEquals("Hello, World!", renderedOuterTemplate);
});
/*
* Any change to the inner template should be reflected when the outer template is rendered as long as the old
* version of the inner template was removed from the Qute internal cache.
*/
updateTemplateData(innerTemplate.getId(), "Red Hat!");
statelessSessionFactory.withSession(statelessSession -> {
String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
assertEquals("Hello, World!", renderedOuterTemplate);
});
templateService.clearTemplates();
statelessSessionFactory.withSession(statelessSession -> {
String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
assertEquals("Hello, Red Hat!", renderedOuterTemplate);
});
/*
* If the inner template is deleted, the outer template rendering should fail as long as the old version of the
* inner template was removed from the Qute internal cache.
*/
deleteTemplate(innerTemplate.getId());
statelessSessionFactory.withSession(statelessSession -> {
String renderedOuterTemplate = templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
assertEquals("Hello, Red Hat!", renderedOuterTemplate);
});
templateService.clearTemplates();
TemplateException e = assertThrows(TemplateException.class, () -> {
statelessSessionFactory.withSession(statelessSession -> {
templateService.compileTemplate(outerTemplate.getData(), outerTemplate.getName()).render();
});
});
assertEquals("Included template [inner-template] not found in template [outer-template] on line 1", e.getMessage());
}
use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.
the class DbQuteEngineTest method testToStringFormatExtension.
@Test
void testToStringFormatExtension() {
Template template = createTemplate("to-string-format-template", "{date.toStringFormat()}");
LocalDateTime date = LocalDateTime.of(2022, Month.JANUARY, 1, 0, 0);
TemplateInstance templateInstance = templateService.compileTemplate(template.getData(), template.getName());
assertEquals("01 Jan 2022", templateInstance.data("date", date).render());
assertEquals("01 Jan 2022", templateInstance.data("date", date.toString()).render());
}
Aggregations