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