Search in sources :

Example 1 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class TemplateRepository method createAggregationEmailTemplate.

@Transactional
public AggregationEmailTemplate createAggregationEmailTemplate(AggregationEmailTemplate template) {
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    template.setSubscriptionType(template.getSubscriptionType());
    template.setSubjectTemplate(subjectTemplate);
    template.setBodyTemplate(bodyTemplate);
    if (template.getApplicationId() != null) {
        Application app = findApplication(template.getApplicationId());
        template.setApplication(app);
    }
    entityManager.persist(template);
    // The full application isn't needed in the REST response.
    template.filterOutApplication();
    // The full templates aren't needed in the REST response.
    template.filterOutTemplates();
    return template;
}
Also used : Application(com.redhat.cloud.notifications.models.Application) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Transactional(javax.transaction.Transactional)

Example 2 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class TemplateRepository method updateInstantEmailTemplate.

@Transactional
public boolean updateInstantEmailTemplate(UUID id, InstantEmailTemplate template) {
    String hql = "UPDATE InstantEmailTemplate SET eventType = :eventType, subjectTemplate = :subjectTemplate, " + "bodyTemplate = :bodyTemplate WHERE id = :id";
    EventType eventType;
    if (template.getEventTypeId() == null) {
        eventType = null;
    } else {
        eventType = findEventType(template.getEventTypeId());
    }
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    int rowCount = entityManager.createQuery(hql).setParameter("eventType", eventType).setParameter("subjectTemplate", subjectTemplate).setParameter("bodyTemplate", bodyTemplate).setParameter("id", id).executeUpdate();
    return rowCount > 0;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Transactional(javax.transaction.Transactional)

Example 3 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class TemplateRepository method deleteTemplate.

@Transactional
public boolean deleteTemplate(UUID id) {
    Template template = entityManager.find(Template.class, id);
    if (template == null) {
        throw new NotFoundException("Template not found");
    } else {
        String checkHql = "SELECT COUNT(*) FROM Template WHERE id != :id AND data LIKE :include";
        long count = entityManager.createQuery(checkHql, Long.class).setParameter("id", id).setParameter("include", "%{#include " + template.getName() + "%").getSingleResult();
        if (count > 0) {
            throw new BadRequestException("Included templates can't be deleted, remove the inclusion or delete the outer template first");
        } else {
            String deleteHql = "DELETE FROM Template WHERE id = :id";
            int rowCount = entityManager.createQuery(deleteHql).setParameter("id", id).executeUpdate();
            return rowCount > 0;
        }
    }
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Transactional(javax.transaction.Transactional)

Example 4 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class TemplateRepository method createInstantEmailTemplate.

@Transactional
public InstantEmailTemplate createInstantEmailTemplate(InstantEmailTemplate template) {
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    template.setSubjectTemplate(subjectTemplate);
    template.setBodyTemplate(bodyTemplate);
    if (template.getEventTypeId() != null) {
        EventType eventType = findEventType(template.getEventTypeId());
        template.setEventType(eventType);
    }
    entityManager.persist(template);
    // The full event type isn't needed in the REST response.
    template.filterOutEventType();
    // The full templates aren't needed in the REST response.
    template.filterOutTemplates();
    return template;
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Transactional(javax.transaction.Transactional)

Example 5 with Template

use of com.redhat.cloud.notifications.models.Template in project notifications-backend by RedHatInsights.

the class TemplateRepository method updateAggregationEmailTemplate.

@Transactional
public boolean updateAggregationEmailTemplate(UUID id, AggregationEmailTemplate template) {
    String hql = "UPDATE AggregationEmailTemplate SET application = :app, subjectTemplate = :subjectTemplate, " + "subscriptionType = :subscriptionType, bodyTemplate = :bodyTemplate WHERE id = :id";
    Application app;
    if (template.getApplicationId() == null) {
        app = null;
    } else {
        app = findApplication(template.getApplicationId());
    }
    Template subjectTemplate = findTemplate(template.getSubjectTemplateId(), SUBJECT_NOT_FOUND);
    Template bodyTemplate = findTemplate(template.getBodyTemplateId(), BODY_NOT_FOUND);
    int rowCount = entityManager.createQuery(hql).setParameter("app", app).setParameter("subscriptionType", template.getSubscriptionType()).setParameter("subjectTemplate", subjectTemplate).setParameter("bodyTemplate", bodyTemplate).setParameter("id", id).executeUpdate();
    return rowCount > 0;
}
Also used : Application(com.redhat.cloud.notifications.models.Application) AggregationEmailTemplate(com.redhat.cloud.notifications.models.AggregationEmailTemplate) Template(com.redhat.cloud.notifications.models.Template) InstantEmailTemplate(com.redhat.cloud.notifications.models.InstantEmailTemplate) Transactional(javax.transaction.Transactional)

Aggregations

Template (com.redhat.cloud.notifications.models.Template)25 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)14 InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)14 QuarkusTest (io.quarkus.test.junit.QuarkusTest)12 Test (org.junit.jupiter.api.Test)12 Transactional (javax.transaction.Transactional)8 TemplateInstance (io.quarkus.qute.TemplateInstance)6 CrudTestHelpers.createAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createAggregationEmailTemplate)5 CrudTestHelpers.createInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createInstantEmailTemplate)5 CrudTestHelpers.createTemplate (com.redhat.cloud.notifications.CrudTestHelpers.createTemplate)5 CrudTestHelpers.deleteAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteAggregationEmailTemplate)5 CrudTestHelpers.deleteInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteInstantEmailTemplate)5 CrudTestHelpers.deleteTemplate (com.redhat.cloud.notifications.CrudTestHelpers.deleteTemplate)5 CrudTestHelpers.updateAggregationEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateAggregationEmailTemplate)5 CrudTestHelpers.updateInstantEmailTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateInstantEmailTemplate)5 CrudTestHelpers.updateTemplate (com.redhat.cloud.notifications.CrudTestHelpers.updateTemplate)5 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)4 Header (io.restassured.http.Header)4 JsonObject (io.vertx.core.json.JsonObject)4 LocalDateTime (java.time.LocalDateTime)4