class AlertDefinitionDAO {

  @Transactional
  public void create(AlertDefinitionEntity alertDefinition)
      throws AmbariException {
    EntityManager entityManager = entityManagerProvider.get();
    entityManager.persist(alertDefinition);

    AlertGroupEntity group = dispatchDao.findDefaultServiceGroup(alertDefinition.getClusterId(),
        alertDefinition.getServiceName());

    if (null == group) {
      // create the default alert group for the new service; this MUST be done
      // before adding definitions so that they are properly added to the
      // default group
      String serviceName = alertDefinition.getServiceName();
      group = dispatchDao.createDefaultGroup(alertDefinition.getClusterId(), serviceName);
    }

    group.addAlertDefinition(alertDefinition);
    AlertGroupsUpdateEvent alertGroupsUpdateEvent = new AlertGroupsUpdateEvent(Collections.singletonList(
        new AlertGroupUpdate(group)),
        UpdateEventType.UPDATE);
    STOMPUpdatePublisher.publish(alertGroupsUpdateEvent);
    dispatchDao.merge(group);

    // publish the alert definition registration
    AlertDefinition coerced = alertDefinitionFactory.coerce(alertDefinition);
    if (null != coerced) {
      AlertDefinitionRegistrationEvent event = new AlertDefinitionRegistrationEvent(
          alertDefinition.getClusterId(), coerced);
      eventPublisher.publish(event);
    } else {
      LOG.warn("Unable to broadcast alert registration event for {}",
          alertDefinition.getDefinitionName());
    }

    entityManager.refresh(alertDefinition);
  }

}