Search in sources :

Example 1 with EventType

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

the class NotificationResource method getEventTypes.

@GET
@Path("/eventTypes")
@Produces(APPLICATION_JSON)
@Operation(summary = "Retrieve all event types. The returned list can be filtered by bundle or application.")
@RolesAllowed(ConsoleIdentityProvider.RBAC_READ_NOTIFICATIONS)
public Page<EventType> getEventTypes(@Context UriInfo uriInfo, @BeanParam Query query, @QueryParam("applicationIds") Set<UUID> applicationIds, @QueryParam("bundleId") UUID bundleId, @QueryParam("eventTypeName") String eventTypeName) {
    List<EventType> eventTypes = applicationRepository.getEventTypes(query, applicationIds, bundleId, eventTypeName);
    Long count = applicationRepository.getEventTypesCount(applicationIds, bundleId, eventTypeName);
    return new Page<>(eventTypes, PageLinksBuilder.build(uriInfo.getPath(), count, query.getLimit().getLimit(), query.getLimit().getOffset()), new Meta(count));
}
Also used : Meta(com.redhat.cloud.notifications.routers.models.Meta) EventType(com.redhat.cloud.notifications.models.EventType) Page(com.redhat.cloud.notifications.routers.models.Page) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Example 2 with EventType

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

the class BehaviorGroupRepository method updateEventTypeBehaviors.

/*
     * Returns true if the event type was found and successfully updated.
     * Returns false if the event type was not found.
     * If an exception other than NoResultException is thrown during the update, the DB transaction will be rolled back.
     */
@Transactional
public boolean updateEventTypeBehaviors(String accountId, UUID eventTypeId, Set<UUID> behaviorGroupIds) {
    // First, let's make sure the event type exists.
    EventType eventType = entityManager.find(EventType.class, eventTypeId);
    if (eventType == null) {
        throw new NotFoundException("Event type not found");
    } else {
        // An event type should only be linked to behavior groups from the same bundle.
        String integrityCheckHql = "SELECT id FROM BehaviorGroup WHERE id IN (:behaviorGroupIds) " + "AND bundle != (SELECT application.bundle FROM EventType WHERE id = :eventTypeId)";
        List<UUID> differentBundle = entityManager.createQuery(integrityCheckHql, UUID.class).setParameter("behaviorGroupIds", behaviorGroupIds).setParameter("eventTypeId", eventTypeId).getResultList();
        if (!differentBundle.isEmpty()) {
            throw new BadRequestException("Some behavior groups can't be linked to the event " + "type because they belong to a different bundle: " + differentBundle);
        }
        /*
             * All event type behaviors that should no longer exist must be deleted.
             * Deleted event type behaviors must obviously be owned by the current account.
             */
        String deleteQuery = "DELETE FROM EventTypeBehavior b " + "WHERE b.eventType.id = :eventTypeId " + "AND EXISTS (SELECT 1 FROM BehaviorGroup WHERE accountId = :accountId AND id = b.behaviorGroup.id)";
        if (!behaviorGroupIds.isEmpty()) {
            deleteQuery += " AND b.behaviorGroup.id NOT IN (:behaviorGroupIds)";
        }
        javax.persistence.Query q = entityManager.createQuery(deleteQuery).setParameter("accountId", accountId).setParameter("eventTypeId", eventTypeId);
        if (!behaviorGroupIds.isEmpty()) {
            q = q.setParameter("behaviorGroupIds", behaviorGroupIds);
        }
        q.executeUpdate();
        for (UUID behaviorGroupId : behaviorGroupIds) {
            /*
                 * Then, we'll insert all event type behaviors from the given behaviorGroupIds list.
                 * If an event type behavior already exists, nothing will happen (no exception will be thrown).
                 */
            String insertQuery = "INSERT INTO event_type_behavior (event_type_id, behavior_group_id, created) " + "SELECT :eventTypeId, :behaviorGroupId, :created " + "WHERE EXISTS (SELECT 1 FROM behavior_group WHERE account_id = :accountId AND id = :behaviorGroupId) " + "ON CONFLICT (event_type_id, behavior_group_id) DO NOTHING";
            entityManager.createNativeQuery(insertQuery).setParameter("eventTypeId", eventTypeId).setParameter("behaviorGroupId", behaviorGroupId).setParameter("created", LocalDateTime.now(UTC)).setParameter("accountId", accountId).executeUpdate();
        }
        return true;
    }
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) UUID(java.util.UUID) Transactional(javax.transaction.Transactional)

Example 3 with EventType

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

the class BehaviorGroupRepository method findEventTypesByBehaviorGroupId.

public List<EventType> findEventTypesByBehaviorGroupId(String accountId, UUID behaviorGroupId) {
    BehaviorGroup behaviorGroup = entityManager.find(BehaviorGroup.class, behaviorGroupId);
    if (behaviorGroup == null) {
        throw new NotFoundException("Behavior group not found");
    }
    String query = "SELECT e FROM EventType e LEFT JOIN FETCH e.application JOIN e.behaviors b " + "WHERE (b.behaviorGroup.accountId = :accountId OR b.behaviorGroup.accountId IS NULL) AND b.behaviorGroup.id = :behaviorGroupId";
    return entityManager.createQuery(query, EventType.class).setParameter("accountId", accountId).setParameter("behaviorGroupId", behaviorGroupId).getResultList();
}
Also used : EventType(com.redhat.cloud.notifications.models.EventType) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) NotFoundException(javax.ws.rs.NotFoundException)

Example 4 with EventType

use of com.redhat.cloud.notifications.models.EventType 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 5 with EventType

use of com.redhat.cloud.notifications.models.EventType 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)

Aggregations

EventType (com.redhat.cloud.notifications.models.EventType)36 QuarkusTest (io.quarkus.test.junit.QuarkusTest)16 Test (org.junit.jupiter.api.Test)16 Application (com.redhat.cloud.notifications.models.Application)14 Bundle (com.redhat.cloud.notifications.models.Bundle)11 Transactional (javax.transaction.Transactional)10 BehaviorGroup (com.redhat.cloud.notifications.models.BehaviorGroup)9 Action (com.redhat.cloud.notifications.ingress.Action)7 TestHelpers.serializeAction (com.redhat.cloud.notifications.TestHelpers.serializeAction)6 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)6 UUID (java.util.UUID)5 Event (com.redhat.cloud.notifications.models.Event)4 NotFoundException (javax.ws.rs.NotFoundException)4 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)3 InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)3 Template (com.redhat.cloud.notifications.models.Template)3 Header (io.restassured.http.Header)3 Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2